@@ -3,50 +3,50 @@ |
||
3 | 3 | |
4 | 4 | class EE_Boolean_Field extends EE_Integer_Field |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('boolean'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('boolean'); |
|
16 | + } |
|
17 | 17 | |
18 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | - { |
|
20 | - if ($value_inputted_for_field_on_model_object) { |
|
21 | - return true; |
|
22 | - } else { |
|
23 | - return false; |
|
24 | - } |
|
25 | - } |
|
18 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | + { |
|
20 | + if ($value_inputted_for_field_on_model_object) { |
|
21 | + return true; |
|
22 | + } else { |
|
23 | + return false; |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Make sure we're returning booleans |
|
29 | - * |
|
30 | - * @param string $value_inputted_for_field_on_model_object |
|
31 | - * @return boolean |
|
32 | - */ |
|
33 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | - { |
|
35 | - return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | - } |
|
27 | + /** |
|
28 | + * Make sure we're returning booleans |
|
29 | + * |
|
30 | + * @param string $value_inputted_for_field_on_model_object |
|
31 | + * @return boolean |
|
32 | + */ |
|
33 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | + { |
|
35 | + return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Gets a nice Yes/No value for this field |
|
40 | - * |
|
41 | - * @param boolean $value_on_field_to_be_outputted |
|
42 | - * @return string Yes or No |
|
43 | - */ |
|
44 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | - { |
|
46 | - return apply_filters( |
|
47 | - 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | - $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | - $value_on_field_to_be_outputted |
|
50 | - ); |
|
51 | - } |
|
38 | + /** |
|
39 | + * Gets a nice Yes/No value for this field |
|
40 | + * |
|
41 | + * @param boolean $value_on_field_to_be_outputted |
|
42 | + * @return string Yes or No |
|
43 | + */ |
|
44 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | + { |
|
46 | + return apply_filters( |
|
47 | + 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | + $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | + $value_on_field_to_be_outputted |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | } |
@@ -1,5 +1,5 @@ |
||
1 | 1 | <?php |
2 | -defined( 'EVENT_ESPRESSO_VERSION') || exit; |
|
2 | +defined('EVENT_ESPRESSO_VERSION') || exit; |
|
3 | 3 | |
4 | 4 | class EE_Boolean_Field extends EE_Integer_Field |
5 | 5 | { |
@@ -3,16 +3,16 @@ |
||
3 | 3 | |
4 | 4 | class EE_DB_Only_Float_Field extends EE_DB_Only_Field_Base |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('number'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('number'); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -4,27 +4,27 @@ |
||
4 | 4 | class EE_Primary_Key_Int_Field extends EE_Primary_Key_Field_Base |
5 | 5 | { |
6 | 6 | |
7 | - public function __construct($table_column, $nicename) |
|
8 | - { |
|
9 | - parent::__construct($table_column, $nicename, 0); |
|
10 | - $this->setSchemaType('integer'); |
|
11 | - } |
|
7 | + public function __construct($table_column, $nicename) |
|
8 | + { |
|
9 | + parent::__construct($table_column, $nicename, 0); |
|
10 | + $this->setSchemaType('integer'); |
|
11 | + } |
|
12 | 12 | |
13 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
14 | - { |
|
15 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
16 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
17 | - } |
|
18 | - return absint($value_inputted_for_field_on_model_object); |
|
19 | - } |
|
13 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
14 | + { |
|
15 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
16 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
17 | + } |
|
18 | + return absint($value_inputted_for_field_on_model_object); |
|
19 | + } |
|
20 | 20 | |
21 | - function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
22 | - { |
|
23 | - return intval($value_found_in_db_for_model_object); |
|
24 | - } |
|
21 | + function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
22 | + { |
|
23 | + return intval($value_found_in_db_for_model_object); |
|
24 | + } |
|
25 | 25 | |
26 | - function is_auto_increment() |
|
27 | - { |
|
28 | - return true; |
|
29 | - } |
|
26 | + function is_auto_increment() |
|
27 | + { |
|
28 | + return true; |
|
29 | + } |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -4,6 +4,9 @@ |
||
4 | 4 | class EE_Primary_Key_Int_Field extends EE_Primary_Key_Field_Base |
5 | 5 | { |
6 | 6 | |
7 | + /** |
|
8 | + * @param string $table_column |
|
9 | + */ |
|
7 | 10 | public function __construct($table_column, $nicename) |
8 | 11 | { |
9 | 12 | parent::__construct($table_column, $nicename, 0); |
@@ -12,131 +12,131 @@ |
||
12 | 12 | class EE_Enum_Text_Field extends EE_Text_Field_Base |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array $_allowed_enum_values |
|
17 | - */ |
|
18 | - public $_allowed_enum_values; |
|
19 | - |
|
20 | - /** |
|
21 | - * @param string $table_column |
|
22 | - * @param string $nice_name |
|
23 | - * @param boolean $nullable |
|
24 | - * @param mixed $default_value |
|
25 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
26 | - */ |
|
27 | - function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
28 | - { |
|
29 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
30 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
31 | - $this->setSchemaType('object'); |
|
32 | - } |
|
33 | - |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * Returns the list of allowed enum options, but filterable. |
|
38 | - * This is used internally |
|
39 | - * |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - protected function _allowed_enum_values() |
|
43 | - { |
|
44 | - return apply_filters( |
|
45 | - 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
46 | - $this->_allowed_enum_values, |
|
47 | - $this |
|
48 | - ); |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
55 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
56 | - * |
|
57 | - * @param string $value_inputted_for_field_on_model_object |
|
58 | - * @return string |
|
59 | - * @throws EE_Error |
|
60 | - */ |
|
61 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
62 | - { |
|
63 | - if ( |
|
64 | - $value_inputted_for_field_on_model_object !== null |
|
65 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
66 | - ) { |
|
67 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
68 | - $msg = sprintf( |
|
69 | - __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
70 | - $value_inputted_for_field_on_model_object, |
|
71 | - $this->_name |
|
72 | - ); |
|
73 | - $msg2 = sprintf( |
|
74 | - __('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
75 | - $this->_name, |
|
76 | - implode(', ', array_keys($this->_allowed_enum_values())), |
|
77 | - $value_inputted_for_field_on_model_object |
|
78 | - ); |
|
79 | - EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
80 | - } |
|
81 | - return $this->get_default_value(); |
|
82 | - } |
|
83 | - return $value_inputted_for_field_on_model_object; |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * Gets the pretty version of the enum's value. |
|
89 | - * |
|
90 | - * @param int |string $value_on_field_to_be_outputted |
|
91 | - * @param null $schema |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
95 | - { |
|
96 | - $options = $this->_allowed_enum_values(); |
|
97 | - if (isset($options[$value_on_field_to_be_outputted])) { |
|
98 | - return $options[$value_on_field_to_be_outputted]; |
|
99 | - } else { |
|
100 | - return $value_on_field_to_be_outputted; |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
108 | - * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
109 | - * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
110 | - * |
|
111 | - * @param mixed $value_in_db |
|
112 | - * @return mixed |
|
113 | - */ |
|
114 | - public function prepare_for_set_from_db($value_in_db) |
|
115 | - { |
|
116 | - return $value_in_db; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - public function getSchemaProperties() |
|
121 | - { |
|
122 | - return array( |
|
123 | - 'raw' => array( |
|
124 | - 'description' => sprintf( |
|
125 | - __('%s - the value in the database.', 'event_espresso'), |
|
126 | - $this->get_nicename() |
|
127 | - ), |
|
128 | - 'type' => 'string', |
|
129 | - 'enum' => array_keys($this->_allowed_enum_values) |
|
130 | - ), |
|
131 | - 'pretty' => array( |
|
132 | - 'description' => sprintf( |
|
133 | - __('%s - the value for display.', 'event_espresso'), |
|
134 | - $this->get_nicename() |
|
135 | - ), |
|
136 | - 'type' => 'string', |
|
137 | - 'enum' => array_values($this->_allowed_enum_values), |
|
138 | - 'read_only' => true |
|
139 | - ) |
|
140 | - ); |
|
141 | - } |
|
15 | + /** |
|
16 | + * @var array $_allowed_enum_values |
|
17 | + */ |
|
18 | + public $_allowed_enum_values; |
|
19 | + |
|
20 | + /** |
|
21 | + * @param string $table_column |
|
22 | + * @param string $nice_name |
|
23 | + * @param boolean $nullable |
|
24 | + * @param mixed $default_value |
|
25 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
26 | + */ |
|
27 | + function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
28 | + { |
|
29 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
30 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
31 | + $this->setSchemaType('object'); |
|
32 | + } |
|
33 | + |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * Returns the list of allowed enum options, but filterable. |
|
38 | + * This is used internally |
|
39 | + * |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + protected function _allowed_enum_values() |
|
43 | + { |
|
44 | + return apply_filters( |
|
45 | + 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
46 | + $this->_allowed_enum_values, |
|
47 | + $this |
|
48 | + ); |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
55 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
56 | + * |
|
57 | + * @param string $value_inputted_for_field_on_model_object |
|
58 | + * @return string |
|
59 | + * @throws EE_Error |
|
60 | + */ |
|
61 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
62 | + { |
|
63 | + if ( |
|
64 | + $value_inputted_for_field_on_model_object !== null |
|
65 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
66 | + ) { |
|
67 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
68 | + $msg = sprintf( |
|
69 | + __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
70 | + $value_inputted_for_field_on_model_object, |
|
71 | + $this->_name |
|
72 | + ); |
|
73 | + $msg2 = sprintf( |
|
74 | + __('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
75 | + $this->_name, |
|
76 | + implode(', ', array_keys($this->_allowed_enum_values())), |
|
77 | + $value_inputted_for_field_on_model_object |
|
78 | + ); |
|
79 | + EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
80 | + } |
|
81 | + return $this->get_default_value(); |
|
82 | + } |
|
83 | + return $value_inputted_for_field_on_model_object; |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * Gets the pretty version of the enum's value. |
|
89 | + * |
|
90 | + * @param int |string $value_on_field_to_be_outputted |
|
91 | + * @param null $schema |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
95 | + { |
|
96 | + $options = $this->_allowed_enum_values(); |
|
97 | + if (isset($options[$value_on_field_to_be_outputted])) { |
|
98 | + return $options[$value_on_field_to_be_outputted]; |
|
99 | + } else { |
|
100 | + return $value_on_field_to_be_outputted; |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
108 | + * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
109 | + * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
110 | + * |
|
111 | + * @param mixed $value_in_db |
|
112 | + * @return mixed |
|
113 | + */ |
|
114 | + public function prepare_for_set_from_db($value_in_db) |
|
115 | + { |
|
116 | + return $value_in_db; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + public function getSchemaProperties() |
|
121 | + { |
|
122 | + return array( |
|
123 | + 'raw' => array( |
|
124 | + 'description' => sprintf( |
|
125 | + __('%s - the value in the database.', 'event_espresso'), |
|
126 | + $this->get_nicename() |
|
127 | + ), |
|
128 | + 'type' => 'string', |
|
129 | + 'enum' => array_keys($this->_allowed_enum_values) |
|
130 | + ), |
|
131 | + 'pretty' => array( |
|
132 | + 'description' => sprintf( |
|
133 | + __('%s - the value for display.', 'event_espresso'), |
|
134 | + $this->get_nicename() |
|
135 | + ), |
|
136 | + 'type' => 'string', |
|
137 | + 'enum' => array_values($this->_allowed_enum_values), |
|
138 | + 'read_only' => true |
|
139 | + ) |
|
140 | + ); |
|
141 | + } |
|
142 | 142 | } |
@@ -15,57 +15,57 @@ |
||
15 | 15 | class EE_Full_HTML_Field extends EE_Text_Field_Base |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @param string $table_column |
|
20 | - * @param string $nicename |
|
21 | - * @param bool $nullable |
|
22 | - * @param null $default_value |
|
23 | - */ |
|
24 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
25 | - { |
|
26 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
27 | - $this->setSchemaType('object'); |
|
28 | - } |
|
18 | + /** |
|
19 | + * @param string $table_column |
|
20 | + * @param string $nicename |
|
21 | + * @param bool $nullable |
|
22 | + * @param null $default_value |
|
23 | + */ |
|
24 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
25 | + { |
|
26 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
27 | + $this->setSchemaType('object'); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
33 | - * |
|
34 | - * @param type $value_on_field_to_be_outputted |
|
35 | - * @param type $schema |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
39 | - { |
|
40 | - if ($schema == 'form_input') { |
|
41 | - return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
42 | - } elseif ($schema == 'no_wpautop') { |
|
43 | - return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
44 | - } else { |
|
45 | - return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
46 | - } |
|
47 | - } |
|
31 | + /** |
|
32 | + * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
33 | + * |
|
34 | + * @param type $value_on_field_to_be_outputted |
|
35 | + * @param type $schema |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
39 | + { |
|
40 | + if ($schema == 'form_input') { |
|
41 | + return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
42 | + } elseif ($schema == 'no_wpautop') { |
|
43 | + return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
44 | + } else { |
|
45 | + return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | 50 | |
51 | - public function getSchemaProperties() |
|
52 | - { |
|
53 | - return array( |
|
54 | - 'raw' => array( |
|
55 | - 'description' => sprintf( |
|
56 | - __('%s - the value in the database.', 'event_espresso'), |
|
57 | - $this->get_nicename() |
|
58 | - ), |
|
59 | - 'type' => 'string' |
|
60 | - ), |
|
61 | - 'rendered' => array( |
|
62 | - 'description' => sprintf( |
|
63 | - __('%s - transformed for display.', 'event_espresso'), |
|
64 | - $this->get_nicename() |
|
65 | - ), |
|
66 | - 'type' => 'string', |
|
67 | - 'readonly' => true |
|
68 | - ) |
|
69 | - ); |
|
70 | - } |
|
51 | + public function getSchemaProperties() |
|
52 | + { |
|
53 | + return array( |
|
54 | + 'raw' => array( |
|
55 | + 'description' => sprintf( |
|
56 | + __('%s - the value in the database.', 'event_espresso'), |
|
57 | + $this->get_nicename() |
|
58 | + ), |
|
59 | + 'type' => 'string' |
|
60 | + ), |
|
61 | + 'rendered' => array( |
|
62 | + 'description' => sprintf( |
|
63 | + __('%s - transformed for display.', 'event_espresso'), |
|
64 | + $this->get_nicename() |
|
65 | + ), |
|
66 | + 'type' => 'string', |
|
67 | + 'readonly' => true |
|
68 | + ) |
|
69 | + ); |
|
70 | + } |
|
71 | 71 | } |
72 | 72 | \ No newline at end of file |
@@ -13,7 +13,7 @@ |
||
13 | 13 | * @param string $table_column |
14 | 14 | * @param string $nicename |
15 | 15 | * @param bool $nullable |
16 | - * @param null $default_value |
|
16 | + * @param string $default_value |
|
17 | 17 | */ |
18 | 18 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
19 | 19 | { |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | /** |
33 | 33 | * cancelTicketSelections |
34 | 34 | * |
35 | - * @return string |
|
35 | + * @return false|null |
|
36 | 36 | */ |
37 | 37 | public function cancelTicketSelections() |
38 | 38 | { |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * process_ticket_selections |
101 | 101 | * |
102 | - * @return array|bool |
|
102 | + * @return boolean|null |
|
103 | 103 | * @throws \EE_Error |
104 | 104 | */ |
105 | 105 | public function processTicketSelections() |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | * validate_post_data |
274 | 274 | * |
275 | 275 | * @param int $id |
276 | - * @return array|FALSE |
|
276 | + * @return string |
|
277 | 277 | */ |
278 | 278 | private function validatePostData($id = 0) |
279 | 279 | { |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | * |
391 | 391 | * @param \EE_Ticket $ticket |
392 | 392 | * @param int $qty |
393 | - * @return TRUE on success, FALSE on fail |
|
393 | + * @return boolean on success, FALSE on fail |
|
394 | 394 | * @throws \EE_Error |
395 | 395 | */ |
396 | 396 | private function addTicketToCart(\EE_Ticket $ticket = null, $qty = 1) |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace EventEspresso\modules\ticket_selector; |
3 | 3 | |
4 | 4 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -19,574 +19,574 @@ discard block |
||
19 | 19 | class ProcessTicketSelector |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * array of datetimes and the spaces available for them |
|
24 | - * |
|
25 | - * @access private |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - private static $_available_spaces = array(); |
|
29 | - |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * cancelTicketSelections |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function cancelTicketSelections() |
|
38 | - { |
|
39 | - // check nonce |
|
40 | - if ( ! $this->processTicketSelectorNonce('cancel_ticket_selections')) { |
|
41 | - return false; |
|
42 | - } |
|
43 | - \EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
44 | - if (\EE_Registry::instance()->REQ->is_set('event_id')) { |
|
45 | - wp_safe_redirect( |
|
46 | - \EEH_Event_View::event_link_url( |
|
47 | - \EE_Registry::instance()->REQ->get('event_id') |
|
48 | - ) |
|
49 | - ); |
|
50 | - } else { |
|
51 | - wp_safe_redirect( |
|
52 | - site_url('/' . \EE_Registry::instance()->CFG->core->event_cpt_slug . '/') |
|
53 | - ); |
|
54 | - } |
|
55 | - exit(); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * processTicketSelectorNonce |
|
62 | - * |
|
63 | - * @param string $nonce_name |
|
64 | - * @param string $id |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - private function processTicketSelectorNonce($nonce_name, $id = '') |
|
68 | - { |
|
69 | - $nonce_name_with_id = ! empty($id) ? "{$nonce_name}_nonce_{$id}" : "{$nonce_name}_nonce"; |
|
70 | - if ( |
|
71 | - ! is_admin() |
|
72 | - && ( |
|
73 | - ! \EE_Registry::instance()->REQ->is_set($nonce_name_with_id) |
|
74 | - || ! wp_verify_nonce( |
|
75 | - \EE_Registry::instance()->REQ->get($nonce_name_with_id), |
|
76 | - $nonce_name |
|
77 | - ) |
|
78 | - ) |
|
79 | - ) { |
|
80 | - \EE_Error::add_error( |
|
81 | - sprintf( |
|
82 | - __( |
|
83 | - 'We\'re sorry but your request failed to pass a security check.%sPlease click the back button on your browser and try again.', |
|
84 | - 'event_espresso' |
|
85 | - ), |
|
86 | - '<br/>' |
|
87 | - ), |
|
88 | - __FILE__, |
|
89 | - __FUNCTION__, |
|
90 | - __LINE__ |
|
91 | - ); |
|
92 | - return false; |
|
93 | - } |
|
94 | - return true; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * process_ticket_selections |
|
101 | - * |
|
102 | - * @return array|bool |
|
103 | - * @throws \EE_Error |
|
104 | - */ |
|
105 | - public function processTicketSelections() |
|
106 | - { |
|
107 | - do_action('EED_Ticket_Selector__process_ticket_selections__before'); |
|
108 | - // do we have an event id? |
|
109 | - if ( ! \EE_Registry::instance()->REQ->is_set('tkt-slctr-event-id')) { |
|
110 | - // $_POST['tkt-slctr-event-id'] was not set ?!?!?!? |
|
111 | - \EE_Error::add_error( |
|
112 | - sprintf( |
|
113 | - __( |
|
114 | - 'An event id was not provided or was not received.%sPlease click the back button on your browser and try again.', |
|
115 | - 'event_espresso' |
|
116 | - ), |
|
117 | - '<br/>' |
|
118 | - ), |
|
119 | - __FILE__, |
|
120 | - __FUNCTION__, |
|
121 | - __LINE__ |
|
122 | - ); |
|
123 | - } |
|
124 | - //if event id is valid |
|
125 | - $id = absint(\EE_Registry::instance()->REQ->get('tkt-slctr-event-id')); |
|
126 | - // check nonce |
|
127 | - if ( ! $this->processTicketSelectorNonce('process_ticket_selections', $id)) { |
|
128 | - return false; |
|
129 | - } |
|
130 | - // d( \EE_Registry::instance()->REQ ); |
|
131 | - self::$_available_spaces = array( |
|
132 | - 'tickets' => array(), |
|
133 | - 'datetimes' => array(), |
|
134 | - ); |
|
135 | - //we should really only have 1 registration in the works now (ie, no MER) so clear any previous items in the cart. |
|
136 | - // When MER happens this will probably need to be tweaked, possibly wrapped in a conditional checking for some constant defined in MER etc. |
|
137 | - \EE_Registry::instance()->load_core('Session'); |
|
138 | - // unless otherwise requested, clear the session |
|
139 | - if (apply_filters('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', true)) { |
|
140 | - \EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
141 | - } |
|
142 | - //d( \EE_Registry::instance()->SSN ); |
|
143 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
144 | - // validate/sanitize data |
|
145 | - $valid = $this->validatePostData($id); |
|
146 | - //EEH_Debug_Tools::printr( $_REQUEST, '$_REQUEST', __FILE__, __LINE__ ); |
|
147 | - //EEH_Debug_Tools::printr( $valid, '$valid', __FILE__, __LINE__ ); |
|
148 | - //EEH_Debug_Tools::printr( $valid[ 'total_tickets' ], 'total_tickets', __FILE__, __LINE__ ); |
|
149 | - //EEH_Debug_Tools::printr( $valid[ 'max_atndz' ], 'max_atndz', __FILE__, __LINE__ ); |
|
150 | - //check total tickets ordered vs max number of attendees that can register |
|
151 | - if ($valid['total_tickets'] > $valid['max_atndz']) { |
|
152 | - // ordering too many tickets !!! |
|
153 | - $total_tickets_string = _n( |
|
154 | - 'You have attempted to purchase %s ticket.', |
|
155 | - 'You have attempted to purchase %s tickets.', |
|
156 | - $valid['total_tickets'], |
|
157 | - 'event_espresso' |
|
158 | - ); |
|
159 | - $limit_error_1 = sprintf($total_tickets_string, $valid['total_tickets']); |
|
160 | - // dev only message |
|
161 | - $max_atndz_string = _n( |
|
162 | - 'The registration limit for this event is %s ticket per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
163 | - 'The registration limit for this event is %s tickets per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
164 | - $valid['max_atndz'], |
|
165 | - 'event_espresso' |
|
166 | - ); |
|
167 | - $limit_error_2 = sprintf($max_atndz_string, $valid['max_atndz'], $valid['max_atndz']); |
|
168 | - \EE_Error::add_error($limit_error_1 . '<br/>' . $limit_error_2, __FILE__, __FUNCTION__, __LINE__); |
|
169 | - } else { |
|
170 | - // all data appears to be valid |
|
171 | - $tckts_slctd = false; |
|
172 | - $tickets_added = 0; |
|
173 | - $valid = apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data', $valid); |
|
174 | - if ($valid['total_tickets'] > 0) { |
|
175 | - // load cart |
|
176 | - \EE_Registry::instance()->load_core('Cart'); |
|
177 | - // cycle thru the number of data rows sent from the event listing |
|
178 | - for ($x = 0; $x < $valid['rows']; $x++) { |
|
179 | - // does this row actually contain a ticket quantity? |
|
180 | - if (isset($valid['qty'][$x]) && $valid['qty'][$x] > 0) { |
|
181 | - // YES we have a ticket quantity |
|
182 | - $tckts_slctd = true; |
|
183 | - // d( $valid['ticket_obj'][$x] ); |
|
184 | - if ($valid['ticket_obj'][$x] instanceof \EE_Ticket) { |
|
185 | - // then add ticket to cart |
|
186 | - $tickets_added += $this->addTicketToCart( |
|
187 | - $valid['ticket_obj'][$x], |
|
188 | - $valid['qty'][$x] |
|
189 | - ); |
|
190 | - if (\EE_Error::has_error()) { |
|
191 | - break; |
|
192 | - } |
|
193 | - } else { |
|
194 | - // nothing added to cart retrieved |
|
195 | - \EE_Error::add_error( |
|
196 | - sprintf( |
|
197 | - __( |
|
198 | - 'A valid ticket could not be retrieved for the event.%sPlease click the back button on your browser and try again.', |
|
199 | - 'event_espresso' |
|
200 | - ), |
|
201 | - '<br/>' |
|
202 | - ), |
|
203 | - __FILE__, __FUNCTION__, __LINE__ |
|
204 | - ); |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
209 | - do_action( |
|
210 | - 'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
|
211 | - \EE_Registry::instance()->CART, |
|
212 | - $this |
|
213 | - ); |
|
214 | - //d( \EE_Registry::instance()->CART ); |
|
215 | - //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL REDIRECT HERE BEFORE CART UPDATE |
|
216 | - if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', $tckts_slctd)) { |
|
217 | - if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__success', $tickets_added)) { |
|
218 | - do_action( |
|
219 | - 'FHEE__EE_Ticket_Selector__process_ticket_selections__before_redirecting_to_checkout', |
|
220 | - \EE_Registry::instance()->CART, |
|
221 | - $this |
|
222 | - ); |
|
223 | - \EE_Registry::instance()->CART->recalculate_all_cart_totals(); |
|
224 | - \EE_Registry::instance()->CART->save_cart(false); |
|
225 | - // exit('KILL REDIRECT AFTER CART UPDATE'); // <<<<<<<< OR HERE TO KILL REDIRECT AFTER CART UPDATE |
|
226 | - // just return TRUE for registrations being made from admin |
|
227 | - if (is_admin()) { |
|
228 | - return true; |
|
229 | - } |
|
230 | - \EE_Error::get_notices(false, true); |
|
231 | - wp_safe_redirect( |
|
232 | - apply_filters( |
|
233 | - 'FHEE__EE_Ticket_Selector__process_ticket_selections__success_redirect_url', |
|
234 | - \EE_Registry::instance()->CFG->core->reg_page_url() |
|
235 | - ) |
|
236 | - ); |
|
237 | - exit(); |
|
238 | - } else { |
|
239 | - if ( ! \EE_Error::has_error() && ! \EE_Error::has_error(true, 'attention')) { |
|
240 | - // nothing added to cart |
|
241 | - \EE_Error::add_attention(__('No tickets were added for the event', 'event_espresso'), |
|
242 | - __FILE__, __FUNCTION__, __LINE__); |
|
243 | - } |
|
244 | - } |
|
245 | - } else { |
|
246 | - // no ticket quantities were selected |
|
247 | - \EE_Error::add_error(__('You need to select a ticket quantity before you can proceed.', |
|
248 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
249 | - } |
|
250 | - } |
|
251 | - //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL BEFORE REDIRECT |
|
252 | - // at this point, just return if registration is being made from admin |
|
253 | - if (is_admin()) { |
|
254 | - return false; |
|
255 | - } |
|
256 | - if ($valid['return_url']) { |
|
257 | - \EE_Error::get_notices(false, true); |
|
258 | - wp_safe_redirect($valid['return_url']); |
|
259 | - exit(); |
|
260 | - } elseif (isset($event_to_add['id'])) { |
|
261 | - \EE_Error::get_notices(false, true); |
|
262 | - wp_safe_redirect(get_permalink($event_to_add['id'])); |
|
263 | - exit(); |
|
264 | - } else { |
|
265 | - echo \EE_Error::get_notices(); |
|
266 | - } |
|
267 | - return false; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * validate_post_data |
|
274 | - * |
|
275 | - * @param int $id |
|
276 | - * @return array|FALSE |
|
277 | - */ |
|
278 | - private function validatePostData($id = 0) |
|
279 | - { |
|
280 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
281 | - if ( ! $id) { |
|
282 | - \EE_Error::add_error( |
|
283 | - __('The event id provided was not valid.', 'event_espresso'), |
|
284 | - __FILE__, |
|
285 | - __FUNCTION__, |
|
286 | - __LINE__ |
|
287 | - ); |
|
288 | - return false; |
|
289 | - } |
|
290 | - // start with an empty array() |
|
291 | - $valid_data = array(); |
|
292 | - // grab valid id |
|
293 | - $valid_data['id'] = $id; |
|
294 | - // array of other form names |
|
295 | - $inputs_to_clean = array( |
|
296 | - 'event_id' => 'tkt-slctr-event-id', |
|
297 | - 'max_atndz' => 'tkt-slctr-max-atndz-', |
|
298 | - 'rows' => 'tkt-slctr-rows-', |
|
299 | - 'qty' => 'tkt-slctr-qty-', |
|
300 | - 'ticket_id' => 'tkt-slctr-ticket-id-', |
|
301 | - 'return_url' => 'tkt-slctr-return-url-', |
|
302 | - ); |
|
303 | - // let's track the total number of tickets ordered.' |
|
304 | - $valid_data['total_tickets'] = 0; |
|
305 | - // cycle through $inputs_to_clean array |
|
306 | - foreach ($inputs_to_clean as $what => $input_to_clean) { |
|
307 | - // check for POST data |
|
308 | - if (\EE_Registry::instance()->REQ->is_set($input_to_clean . $id)) { |
|
309 | - // grab value |
|
310 | - $input_value = \EE_Registry::instance()->REQ->get($input_to_clean . $id); |
|
311 | - switch ($what) { |
|
312 | - // integers |
|
313 | - case 'event_id': |
|
314 | - $valid_data[$what] = absint($input_value); |
|
315 | - // get event via the event id we put in the form |
|
316 | - $valid_data['event'] = \EE_Registry::instance() |
|
317 | - ->load_model('Event') |
|
318 | - ->get_one_by_ID($valid_data['event_id']); |
|
319 | - break; |
|
320 | - case 'rows': |
|
321 | - case 'max_atndz': |
|
322 | - $valid_data[$what] = absint($input_value); |
|
323 | - break; |
|
324 | - // arrays of integers |
|
325 | - case 'qty': |
|
326 | - /** @var array $row_qty */ |
|
327 | - $row_qty = $input_value; |
|
328 | - // if qty is coming from a radio button input, then we need to assemble an array of rows |
|
329 | - if ( ! is_array($row_qty)) { |
|
330 | - // get number of rows |
|
331 | - $rows = \EE_Registry::instance()->REQ->is_set('tkt-slctr-rows-' . $id) |
|
332 | - ? absint(\EE_Registry::instance()->REQ->get('tkt-slctr-rows-' . $id)) |
|
333 | - : 1; |
|
334 | - // explode ints by the dash |
|
335 | - $row_qty = explode('-', $row_qty); |
|
336 | - $row = isset($row_qty[0]) ? absint($row_qty[0]) : 1; |
|
337 | - $qty = isset($row_qty[1]) ? absint($row_qty[1]) : 0; |
|
338 | - $row_qty = array($row => $qty); |
|
339 | - for ($x = 1; $x <= $rows; $x++) { |
|
340 | - if ( ! isset($row_qty[$x])) { |
|
341 | - $row_qty[$x] = 0; |
|
342 | - } |
|
343 | - } |
|
344 | - } |
|
345 | - ksort($row_qty); |
|
346 | - // cycle thru values |
|
347 | - foreach ($row_qty as $qty) { |
|
348 | - $qty = absint($qty); |
|
349 | - // sanitize as integers |
|
350 | - $valid_data[$what][] = $qty; |
|
351 | - $valid_data['total_tickets'] += $qty; |
|
352 | - } |
|
353 | - break; |
|
354 | - // array of integers |
|
355 | - case 'ticket_id': |
|
356 | - $value_array = array(); |
|
357 | - // cycle thru values |
|
358 | - foreach ((array)$input_value as $key => $value) { |
|
359 | - // allow only numbers, letters, spaces, commas and dashes |
|
360 | - $value_array[$key] = wp_strip_all_tags($value); |
|
361 | - // get ticket via the ticket id we put in the form |
|
362 | - $ticket_obj = \EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($value); |
|
363 | - $valid_data['ticket_obj'][$key] = $ticket_obj; |
|
364 | - } |
|
365 | - $valid_data[$what] = $value_array; |
|
366 | - break; |
|
367 | - case 'return_url' : |
|
368 | - // grab and sanitize return-url |
|
369 | - $input_value = esc_url_raw($input_value); |
|
370 | - // was the request coming from an iframe ? if so, then: |
|
371 | - if (strpos($input_value, 'event_list=iframe')) { |
|
372 | - // get anchor fragment |
|
373 | - $input_value = explode('#', $input_value); |
|
374 | - $input_value = end($input_value); |
|
375 | - // use event list url instead, but append anchor |
|
376 | - $input_value = \EEH_Event_View::event_archive_url() . '#' . $input_value; |
|
377 | - } |
|
378 | - $valid_data[$what] = $input_value; |
|
379 | - break; |
|
380 | - } // end switch $what |
|
381 | - } |
|
382 | - } // end foreach $inputs_to_clean |
|
383 | - return $valid_data; |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * adds a ticket to the cart |
|
390 | - * |
|
391 | - * @param \EE_Ticket $ticket |
|
392 | - * @param int $qty |
|
393 | - * @return TRUE on success, FALSE on fail |
|
394 | - * @throws \EE_Error |
|
395 | - */ |
|
396 | - private function addTicketToCart(\EE_Ticket $ticket = null, $qty = 1) |
|
397 | - { |
|
398 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
399 | - // get the number of spaces left for this datetime ticket |
|
400 | - $available_spaces = $this->ticketDatetimeAvailability($ticket); |
|
401 | - // compare available spaces against the number of tickets being purchased |
|
402 | - if ($available_spaces >= $qty) { |
|
403 | - // allow addons to prevent a ticket from being added to cart |
|
404 | - if ( |
|
405 | - ! apply_filters( |
|
406 | - 'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_add_to_cart', |
|
407 | - true, |
|
408 | - $ticket, |
|
409 | - $qty, |
|
410 | - $available_spaces |
|
411 | - ) |
|
412 | - ) { |
|
413 | - return false; |
|
414 | - } |
|
415 | - $qty = absint(apply_filters('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty', $qty, $ticket)); |
|
416 | - // add event to cart |
|
417 | - if (\EE_Registry::instance()->CART->add_ticket_to_cart($ticket, $qty)) { |
|
418 | - $this->recalculateTicketDatetimeAvailability($ticket, $qty); |
|
419 | - return true; |
|
420 | - } |
|
421 | - return false; |
|
422 | - } |
|
423 | - // tickets can not be purchased but let's find the exact number left |
|
424 | - // for the last ticket selected PRIOR to subtracting tickets |
|
425 | - $available_spaces = $this->ticketDatetimeAvailability($ticket, true); |
|
426 | - // greedy greedy greedy eh? |
|
427 | - if ($available_spaces > 0) { |
|
428 | - if ( |
|
429 | - apply_filters( |
|
430 | - 'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_display_availability_error', |
|
431 | - true, |
|
432 | - $ticket, |
|
433 | - $qty, |
|
434 | - $available_spaces |
|
435 | - ) |
|
436 | - ) { |
|
437 | - $this->displayAvailabilityError($available_spaces); |
|
438 | - } |
|
439 | - } else { |
|
440 | - \EE_Error::add_error( |
|
441 | - __( |
|
442 | - 'We\'re sorry, but there are no available spaces left for this event at this particular date and time.', |
|
443 | - 'event_espresso' |
|
444 | - ), |
|
445 | - __FILE__, __FUNCTION__, __LINE__ |
|
446 | - ); |
|
447 | - } |
|
448 | - return false; |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * @param int $available_spaces |
|
455 | - * @throws \EE_Error |
|
456 | - */ |
|
457 | - private function displayAvailabilityError($available_spaces = 1) |
|
458 | - { |
|
459 | - // add error messaging - we're using the _n function that will generate |
|
460 | - // the appropriate singular or plural message based on the number of $available_spaces |
|
461 | - if (\EE_Registry::instance()->CART->all_ticket_quantity_count()) { |
|
462 | - $msg = sprintf( |
|
463 | - _n( |
|
464 | - 'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
465 | - 'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
466 | - $available_spaces, |
|
467 | - 'event_espresso' |
|
468 | - ), |
|
469 | - $available_spaces, |
|
470 | - '<br />' |
|
471 | - ); |
|
472 | - } else { |
|
473 | - $msg = sprintf( |
|
474 | - _n( |
|
475 | - 'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
476 | - 'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
477 | - $available_spaces, |
|
478 | - 'event_espresso' |
|
479 | - ), |
|
480 | - $available_spaces, |
|
481 | - '<br />' |
|
482 | - ); |
|
483 | - } |
|
484 | - \EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
485 | - } |
|
486 | - |
|
487 | - |
|
488 | - |
|
489 | - /** |
|
490 | - * ticketDatetimeAvailability |
|
491 | - * creates an array of tickets plus all of the datetimes available to each ticket |
|
492 | - * and tracks the spaces remaining for each of those datetimes |
|
493 | - * |
|
494 | - * @param \EE_Ticket $ticket - selected ticket |
|
495 | - * @param bool $get_original_ticket_spaces |
|
496 | - * @return int |
|
497 | - * @throws \EE_Error |
|
498 | - */ |
|
499 | - private function ticketDatetimeAvailability(\EE_Ticket $ticket, $get_original_ticket_spaces = false) |
|
500 | - { |
|
501 | - // if the $_available_spaces array has not been set up yet... |
|
502 | - if ( ! isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
503 | - $this->setInitialTicketDatetimeAvailability($ticket); |
|
504 | - } |
|
505 | - $available_spaces = $ticket->qty() - $ticket->sold(); |
|
506 | - if (isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
507 | - // loop thru tickets, which will ALSO include individual ticket records AND a total |
|
508 | - foreach (self::$_available_spaces['tickets'][$ticket->ID()] as $DTD_ID => $spaces) { |
|
509 | - // if we want the original datetime availability BEFORE we started subtracting tickets ? |
|
510 | - if ($get_original_ticket_spaces) { |
|
511 | - // then grab the available spaces from the "tickets" array |
|
512 | - // and compare with the above to get the lowest number |
|
513 | - $available_spaces = min( |
|
514 | - $available_spaces, |
|
515 | - self::$_available_spaces['tickets'][$ticket->ID()][$DTD_ID] |
|
516 | - ); |
|
517 | - } else { |
|
518 | - // we want the updated ticket availability as stored in the "datetimes" array |
|
519 | - $available_spaces = min($available_spaces, self::$_available_spaces['datetimes'][$DTD_ID]); |
|
520 | - } |
|
521 | - } |
|
522 | - } |
|
523 | - return $available_spaces; |
|
524 | - } |
|
525 | - |
|
526 | - |
|
527 | - |
|
528 | - /** |
|
529 | - * @param \EE_Ticket $ticket |
|
530 | - * @return void |
|
531 | - * @throws \EE_Error |
|
532 | - */ |
|
533 | - private function setInitialTicketDatetimeAvailability(\EE_Ticket $ticket) |
|
534 | - { |
|
535 | - // first, get all of the datetimes that are available to this ticket |
|
536 | - $datetimes = $ticket->get_many_related( |
|
537 | - 'Datetime', |
|
538 | - array( |
|
539 | - array( |
|
540 | - 'DTT_EVT_end' => array( |
|
541 | - '>=', |
|
542 | - \EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
543 | - ), |
|
544 | - ), |
|
545 | - 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
546 | - ) |
|
547 | - ); |
|
548 | - if ( ! empty($datetimes)) { |
|
549 | - // now loop thru all of the datetimes |
|
550 | - foreach ($datetimes as $datetime) { |
|
551 | - if ($datetime instanceof \EE_Datetime) { |
|
552 | - // the number of spaces available for the datetime without considering individual ticket quantities |
|
553 | - $spaces_remaining = $datetime->spaces_remaining(); |
|
554 | - // save the total available spaces ( the lesser of the ticket qty minus the number of tickets sold |
|
555 | - // or the datetime spaces remaining) to this ticket using the datetime ID as the key |
|
556 | - self::$_available_spaces['tickets'][$ticket->ID()][$datetime->ID()] = min( |
|
557 | - $ticket->qty() - $ticket->sold(), |
|
558 | - $spaces_remaining |
|
559 | - ); |
|
560 | - // if the remaining spaces for this datetime is already set, |
|
561 | - // then compare that against the datetime spaces remaining, and take the lowest number, |
|
562 | - // else just take the datetime spaces remaining, and assign to the datetimes array |
|
563 | - self::$_available_spaces['datetimes'][$datetime->ID()] = isset( |
|
564 | - self::$_available_spaces['datetimes'][$datetime->ID()] |
|
565 | - ) |
|
566 | - ? min(self::$_available_spaces['datetimes'][$datetime->ID()], $spaces_remaining) |
|
567 | - : $spaces_remaining; |
|
568 | - } |
|
569 | - } |
|
570 | - } |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - |
|
575 | - /** |
|
576 | - * @param \EE_Ticket $ticket |
|
577 | - * @param int $qty |
|
578 | - * @return void |
|
579 | - */ |
|
580 | - private function recalculateTicketDatetimeAvailability(\EE_Ticket $ticket, $qty = 0) |
|
581 | - { |
|
582 | - if (isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
583 | - // loop thru tickets, which will ALSO include individual ticket records AND a total |
|
584 | - foreach (self::$_available_spaces['tickets'][$ticket->ID()] as $DTD_ID => $spaces) { |
|
585 | - // subtract the qty of selected tickets from each datetime's available spaces this ticket has access to, |
|
586 | - self::$_available_spaces['datetimes'][$DTD_ID] -= $qty; |
|
587 | - } |
|
588 | - } |
|
589 | - } |
|
22 | + /** |
|
23 | + * array of datetimes and the spaces available for them |
|
24 | + * |
|
25 | + * @access private |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + private static $_available_spaces = array(); |
|
29 | + |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * cancelTicketSelections |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function cancelTicketSelections() |
|
38 | + { |
|
39 | + // check nonce |
|
40 | + if ( ! $this->processTicketSelectorNonce('cancel_ticket_selections')) { |
|
41 | + return false; |
|
42 | + } |
|
43 | + \EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
44 | + if (\EE_Registry::instance()->REQ->is_set('event_id')) { |
|
45 | + wp_safe_redirect( |
|
46 | + \EEH_Event_View::event_link_url( |
|
47 | + \EE_Registry::instance()->REQ->get('event_id') |
|
48 | + ) |
|
49 | + ); |
|
50 | + } else { |
|
51 | + wp_safe_redirect( |
|
52 | + site_url('/' . \EE_Registry::instance()->CFG->core->event_cpt_slug . '/') |
|
53 | + ); |
|
54 | + } |
|
55 | + exit(); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * processTicketSelectorNonce |
|
62 | + * |
|
63 | + * @param string $nonce_name |
|
64 | + * @param string $id |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + private function processTicketSelectorNonce($nonce_name, $id = '') |
|
68 | + { |
|
69 | + $nonce_name_with_id = ! empty($id) ? "{$nonce_name}_nonce_{$id}" : "{$nonce_name}_nonce"; |
|
70 | + if ( |
|
71 | + ! is_admin() |
|
72 | + && ( |
|
73 | + ! \EE_Registry::instance()->REQ->is_set($nonce_name_with_id) |
|
74 | + || ! wp_verify_nonce( |
|
75 | + \EE_Registry::instance()->REQ->get($nonce_name_with_id), |
|
76 | + $nonce_name |
|
77 | + ) |
|
78 | + ) |
|
79 | + ) { |
|
80 | + \EE_Error::add_error( |
|
81 | + sprintf( |
|
82 | + __( |
|
83 | + 'We\'re sorry but your request failed to pass a security check.%sPlease click the back button on your browser and try again.', |
|
84 | + 'event_espresso' |
|
85 | + ), |
|
86 | + '<br/>' |
|
87 | + ), |
|
88 | + __FILE__, |
|
89 | + __FUNCTION__, |
|
90 | + __LINE__ |
|
91 | + ); |
|
92 | + return false; |
|
93 | + } |
|
94 | + return true; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * process_ticket_selections |
|
101 | + * |
|
102 | + * @return array|bool |
|
103 | + * @throws \EE_Error |
|
104 | + */ |
|
105 | + public function processTicketSelections() |
|
106 | + { |
|
107 | + do_action('EED_Ticket_Selector__process_ticket_selections__before'); |
|
108 | + // do we have an event id? |
|
109 | + if ( ! \EE_Registry::instance()->REQ->is_set('tkt-slctr-event-id')) { |
|
110 | + // $_POST['tkt-slctr-event-id'] was not set ?!?!?!? |
|
111 | + \EE_Error::add_error( |
|
112 | + sprintf( |
|
113 | + __( |
|
114 | + 'An event id was not provided or was not received.%sPlease click the back button on your browser and try again.', |
|
115 | + 'event_espresso' |
|
116 | + ), |
|
117 | + '<br/>' |
|
118 | + ), |
|
119 | + __FILE__, |
|
120 | + __FUNCTION__, |
|
121 | + __LINE__ |
|
122 | + ); |
|
123 | + } |
|
124 | + //if event id is valid |
|
125 | + $id = absint(\EE_Registry::instance()->REQ->get('tkt-slctr-event-id')); |
|
126 | + // check nonce |
|
127 | + if ( ! $this->processTicketSelectorNonce('process_ticket_selections', $id)) { |
|
128 | + return false; |
|
129 | + } |
|
130 | + // d( \EE_Registry::instance()->REQ ); |
|
131 | + self::$_available_spaces = array( |
|
132 | + 'tickets' => array(), |
|
133 | + 'datetimes' => array(), |
|
134 | + ); |
|
135 | + //we should really only have 1 registration in the works now (ie, no MER) so clear any previous items in the cart. |
|
136 | + // When MER happens this will probably need to be tweaked, possibly wrapped in a conditional checking for some constant defined in MER etc. |
|
137 | + \EE_Registry::instance()->load_core('Session'); |
|
138 | + // unless otherwise requested, clear the session |
|
139 | + if (apply_filters('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', true)) { |
|
140 | + \EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
141 | + } |
|
142 | + //d( \EE_Registry::instance()->SSN ); |
|
143 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
144 | + // validate/sanitize data |
|
145 | + $valid = $this->validatePostData($id); |
|
146 | + //EEH_Debug_Tools::printr( $_REQUEST, '$_REQUEST', __FILE__, __LINE__ ); |
|
147 | + //EEH_Debug_Tools::printr( $valid, '$valid', __FILE__, __LINE__ ); |
|
148 | + //EEH_Debug_Tools::printr( $valid[ 'total_tickets' ], 'total_tickets', __FILE__, __LINE__ ); |
|
149 | + //EEH_Debug_Tools::printr( $valid[ 'max_atndz' ], 'max_atndz', __FILE__, __LINE__ ); |
|
150 | + //check total tickets ordered vs max number of attendees that can register |
|
151 | + if ($valid['total_tickets'] > $valid['max_atndz']) { |
|
152 | + // ordering too many tickets !!! |
|
153 | + $total_tickets_string = _n( |
|
154 | + 'You have attempted to purchase %s ticket.', |
|
155 | + 'You have attempted to purchase %s tickets.', |
|
156 | + $valid['total_tickets'], |
|
157 | + 'event_espresso' |
|
158 | + ); |
|
159 | + $limit_error_1 = sprintf($total_tickets_string, $valid['total_tickets']); |
|
160 | + // dev only message |
|
161 | + $max_atndz_string = _n( |
|
162 | + 'The registration limit for this event is %s ticket per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
163 | + 'The registration limit for this event is %s tickets per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
164 | + $valid['max_atndz'], |
|
165 | + 'event_espresso' |
|
166 | + ); |
|
167 | + $limit_error_2 = sprintf($max_atndz_string, $valid['max_atndz'], $valid['max_atndz']); |
|
168 | + \EE_Error::add_error($limit_error_1 . '<br/>' . $limit_error_2, __FILE__, __FUNCTION__, __LINE__); |
|
169 | + } else { |
|
170 | + // all data appears to be valid |
|
171 | + $tckts_slctd = false; |
|
172 | + $tickets_added = 0; |
|
173 | + $valid = apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data', $valid); |
|
174 | + if ($valid['total_tickets'] > 0) { |
|
175 | + // load cart |
|
176 | + \EE_Registry::instance()->load_core('Cart'); |
|
177 | + // cycle thru the number of data rows sent from the event listing |
|
178 | + for ($x = 0; $x < $valid['rows']; $x++) { |
|
179 | + // does this row actually contain a ticket quantity? |
|
180 | + if (isset($valid['qty'][$x]) && $valid['qty'][$x] > 0) { |
|
181 | + // YES we have a ticket quantity |
|
182 | + $tckts_slctd = true; |
|
183 | + // d( $valid['ticket_obj'][$x] ); |
|
184 | + if ($valid['ticket_obj'][$x] instanceof \EE_Ticket) { |
|
185 | + // then add ticket to cart |
|
186 | + $tickets_added += $this->addTicketToCart( |
|
187 | + $valid['ticket_obj'][$x], |
|
188 | + $valid['qty'][$x] |
|
189 | + ); |
|
190 | + if (\EE_Error::has_error()) { |
|
191 | + break; |
|
192 | + } |
|
193 | + } else { |
|
194 | + // nothing added to cart retrieved |
|
195 | + \EE_Error::add_error( |
|
196 | + sprintf( |
|
197 | + __( |
|
198 | + 'A valid ticket could not be retrieved for the event.%sPlease click the back button on your browser and try again.', |
|
199 | + 'event_espresso' |
|
200 | + ), |
|
201 | + '<br/>' |
|
202 | + ), |
|
203 | + __FILE__, __FUNCTION__, __LINE__ |
|
204 | + ); |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | + do_action( |
|
210 | + 'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
|
211 | + \EE_Registry::instance()->CART, |
|
212 | + $this |
|
213 | + ); |
|
214 | + //d( \EE_Registry::instance()->CART ); |
|
215 | + //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL REDIRECT HERE BEFORE CART UPDATE |
|
216 | + if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', $tckts_slctd)) { |
|
217 | + if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__success', $tickets_added)) { |
|
218 | + do_action( |
|
219 | + 'FHEE__EE_Ticket_Selector__process_ticket_selections__before_redirecting_to_checkout', |
|
220 | + \EE_Registry::instance()->CART, |
|
221 | + $this |
|
222 | + ); |
|
223 | + \EE_Registry::instance()->CART->recalculate_all_cart_totals(); |
|
224 | + \EE_Registry::instance()->CART->save_cart(false); |
|
225 | + // exit('KILL REDIRECT AFTER CART UPDATE'); // <<<<<<<< OR HERE TO KILL REDIRECT AFTER CART UPDATE |
|
226 | + // just return TRUE for registrations being made from admin |
|
227 | + if (is_admin()) { |
|
228 | + return true; |
|
229 | + } |
|
230 | + \EE_Error::get_notices(false, true); |
|
231 | + wp_safe_redirect( |
|
232 | + apply_filters( |
|
233 | + 'FHEE__EE_Ticket_Selector__process_ticket_selections__success_redirect_url', |
|
234 | + \EE_Registry::instance()->CFG->core->reg_page_url() |
|
235 | + ) |
|
236 | + ); |
|
237 | + exit(); |
|
238 | + } else { |
|
239 | + if ( ! \EE_Error::has_error() && ! \EE_Error::has_error(true, 'attention')) { |
|
240 | + // nothing added to cart |
|
241 | + \EE_Error::add_attention(__('No tickets were added for the event', 'event_espresso'), |
|
242 | + __FILE__, __FUNCTION__, __LINE__); |
|
243 | + } |
|
244 | + } |
|
245 | + } else { |
|
246 | + // no ticket quantities were selected |
|
247 | + \EE_Error::add_error(__('You need to select a ticket quantity before you can proceed.', |
|
248 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
249 | + } |
|
250 | + } |
|
251 | + //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL BEFORE REDIRECT |
|
252 | + // at this point, just return if registration is being made from admin |
|
253 | + if (is_admin()) { |
|
254 | + return false; |
|
255 | + } |
|
256 | + if ($valid['return_url']) { |
|
257 | + \EE_Error::get_notices(false, true); |
|
258 | + wp_safe_redirect($valid['return_url']); |
|
259 | + exit(); |
|
260 | + } elseif (isset($event_to_add['id'])) { |
|
261 | + \EE_Error::get_notices(false, true); |
|
262 | + wp_safe_redirect(get_permalink($event_to_add['id'])); |
|
263 | + exit(); |
|
264 | + } else { |
|
265 | + echo \EE_Error::get_notices(); |
|
266 | + } |
|
267 | + return false; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * validate_post_data |
|
274 | + * |
|
275 | + * @param int $id |
|
276 | + * @return array|FALSE |
|
277 | + */ |
|
278 | + private function validatePostData($id = 0) |
|
279 | + { |
|
280 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
281 | + if ( ! $id) { |
|
282 | + \EE_Error::add_error( |
|
283 | + __('The event id provided was not valid.', 'event_espresso'), |
|
284 | + __FILE__, |
|
285 | + __FUNCTION__, |
|
286 | + __LINE__ |
|
287 | + ); |
|
288 | + return false; |
|
289 | + } |
|
290 | + // start with an empty array() |
|
291 | + $valid_data = array(); |
|
292 | + // grab valid id |
|
293 | + $valid_data['id'] = $id; |
|
294 | + // array of other form names |
|
295 | + $inputs_to_clean = array( |
|
296 | + 'event_id' => 'tkt-slctr-event-id', |
|
297 | + 'max_atndz' => 'tkt-slctr-max-atndz-', |
|
298 | + 'rows' => 'tkt-slctr-rows-', |
|
299 | + 'qty' => 'tkt-slctr-qty-', |
|
300 | + 'ticket_id' => 'tkt-slctr-ticket-id-', |
|
301 | + 'return_url' => 'tkt-slctr-return-url-', |
|
302 | + ); |
|
303 | + // let's track the total number of tickets ordered.' |
|
304 | + $valid_data['total_tickets'] = 0; |
|
305 | + // cycle through $inputs_to_clean array |
|
306 | + foreach ($inputs_to_clean as $what => $input_to_clean) { |
|
307 | + // check for POST data |
|
308 | + if (\EE_Registry::instance()->REQ->is_set($input_to_clean . $id)) { |
|
309 | + // grab value |
|
310 | + $input_value = \EE_Registry::instance()->REQ->get($input_to_clean . $id); |
|
311 | + switch ($what) { |
|
312 | + // integers |
|
313 | + case 'event_id': |
|
314 | + $valid_data[$what] = absint($input_value); |
|
315 | + // get event via the event id we put in the form |
|
316 | + $valid_data['event'] = \EE_Registry::instance() |
|
317 | + ->load_model('Event') |
|
318 | + ->get_one_by_ID($valid_data['event_id']); |
|
319 | + break; |
|
320 | + case 'rows': |
|
321 | + case 'max_atndz': |
|
322 | + $valid_data[$what] = absint($input_value); |
|
323 | + break; |
|
324 | + // arrays of integers |
|
325 | + case 'qty': |
|
326 | + /** @var array $row_qty */ |
|
327 | + $row_qty = $input_value; |
|
328 | + // if qty is coming from a radio button input, then we need to assemble an array of rows |
|
329 | + if ( ! is_array($row_qty)) { |
|
330 | + // get number of rows |
|
331 | + $rows = \EE_Registry::instance()->REQ->is_set('tkt-slctr-rows-' . $id) |
|
332 | + ? absint(\EE_Registry::instance()->REQ->get('tkt-slctr-rows-' . $id)) |
|
333 | + : 1; |
|
334 | + // explode ints by the dash |
|
335 | + $row_qty = explode('-', $row_qty); |
|
336 | + $row = isset($row_qty[0]) ? absint($row_qty[0]) : 1; |
|
337 | + $qty = isset($row_qty[1]) ? absint($row_qty[1]) : 0; |
|
338 | + $row_qty = array($row => $qty); |
|
339 | + for ($x = 1; $x <= $rows; $x++) { |
|
340 | + if ( ! isset($row_qty[$x])) { |
|
341 | + $row_qty[$x] = 0; |
|
342 | + } |
|
343 | + } |
|
344 | + } |
|
345 | + ksort($row_qty); |
|
346 | + // cycle thru values |
|
347 | + foreach ($row_qty as $qty) { |
|
348 | + $qty = absint($qty); |
|
349 | + // sanitize as integers |
|
350 | + $valid_data[$what][] = $qty; |
|
351 | + $valid_data['total_tickets'] += $qty; |
|
352 | + } |
|
353 | + break; |
|
354 | + // array of integers |
|
355 | + case 'ticket_id': |
|
356 | + $value_array = array(); |
|
357 | + // cycle thru values |
|
358 | + foreach ((array)$input_value as $key => $value) { |
|
359 | + // allow only numbers, letters, spaces, commas and dashes |
|
360 | + $value_array[$key] = wp_strip_all_tags($value); |
|
361 | + // get ticket via the ticket id we put in the form |
|
362 | + $ticket_obj = \EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($value); |
|
363 | + $valid_data['ticket_obj'][$key] = $ticket_obj; |
|
364 | + } |
|
365 | + $valid_data[$what] = $value_array; |
|
366 | + break; |
|
367 | + case 'return_url' : |
|
368 | + // grab and sanitize return-url |
|
369 | + $input_value = esc_url_raw($input_value); |
|
370 | + // was the request coming from an iframe ? if so, then: |
|
371 | + if (strpos($input_value, 'event_list=iframe')) { |
|
372 | + // get anchor fragment |
|
373 | + $input_value = explode('#', $input_value); |
|
374 | + $input_value = end($input_value); |
|
375 | + // use event list url instead, but append anchor |
|
376 | + $input_value = \EEH_Event_View::event_archive_url() . '#' . $input_value; |
|
377 | + } |
|
378 | + $valid_data[$what] = $input_value; |
|
379 | + break; |
|
380 | + } // end switch $what |
|
381 | + } |
|
382 | + } // end foreach $inputs_to_clean |
|
383 | + return $valid_data; |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * adds a ticket to the cart |
|
390 | + * |
|
391 | + * @param \EE_Ticket $ticket |
|
392 | + * @param int $qty |
|
393 | + * @return TRUE on success, FALSE on fail |
|
394 | + * @throws \EE_Error |
|
395 | + */ |
|
396 | + private function addTicketToCart(\EE_Ticket $ticket = null, $qty = 1) |
|
397 | + { |
|
398 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
399 | + // get the number of spaces left for this datetime ticket |
|
400 | + $available_spaces = $this->ticketDatetimeAvailability($ticket); |
|
401 | + // compare available spaces against the number of tickets being purchased |
|
402 | + if ($available_spaces >= $qty) { |
|
403 | + // allow addons to prevent a ticket from being added to cart |
|
404 | + if ( |
|
405 | + ! apply_filters( |
|
406 | + 'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_add_to_cart', |
|
407 | + true, |
|
408 | + $ticket, |
|
409 | + $qty, |
|
410 | + $available_spaces |
|
411 | + ) |
|
412 | + ) { |
|
413 | + return false; |
|
414 | + } |
|
415 | + $qty = absint(apply_filters('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty', $qty, $ticket)); |
|
416 | + // add event to cart |
|
417 | + if (\EE_Registry::instance()->CART->add_ticket_to_cart($ticket, $qty)) { |
|
418 | + $this->recalculateTicketDatetimeAvailability($ticket, $qty); |
|
419 | + return true; |
|
420 | + } |
|
421 | + return false; |
|
422 | + } |
|
423 | + // tickets can not be purchased but let's find the exact number left |
|
424 | + // for the last ticket selected PRIOR to subtracting tickets |
|
425 | + $available_spaces = $this->ticketDatetimeAvailability($ticket, true); |
|
426 | + // greedy greedy greedy eh? |
|
427 | + if ($available_spaces > 0) { |
|
428 | + if ( |
|
429 | + apply_filters( |
|
430 | + 'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_display_availability_error', |
|
431 | + true, |
|
432 | + $ticket, |
|
433 | + $qty, |
|
434 | + $available_spaces |
|
435 | + ) |
|
436 | + ) { |
|
437 | + $this->displayAvailabilityError($available_spaces); |
|
438 | + } |
|
439 | + } else { |
|
440 | + \EE_Error::add_error( |
|
441 | + __( |
|
442 | + 'We\'re sorry, but there are no available spaces left for this event at this particular date and time.', |
|
443 | + 'event_espresso' |
|
444 | + ), |
|
445 | + __FILE__, __FUNCTION__, __LINE__ |
|
446 | + ); |
|
447 | + } |
|
448 | + return false; |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * @param int $available_spaces |
|
455 | + * @throws \EE_Error |
|
456 | + */ |
|
457 | + private function displayAvailabilityError($available_spaces = 1) |
|
458 | + { |
|
459 | + // add error messaging - we're using the _n function that will generate |
|
460 | + // the appropriate singular or plural message based on the number of $available_spaces |
|
461 | + if (\EE_Registry::instance()->CART->all_ticket_quantity_count()) { |
|
462 | + $msg = sprintf( |
|
463 | + _n( |
|
464 | + 'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
465 | + 'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
466 | + $available_spaces, |
|
467 | + 'event_espresso' |
|
468 | + ), |
|
469 | + $available_spaces, |
|
470 | + '<br />' |
|
471 | + ); |
|
472 | + } else { |
|
473 | + $msg = sprintf( |
|
474 | + _n( |
|
475 | + 'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
476 | + 'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
477 | + $available_spaces, |
|
478 | + 'event_espresso' |
|
479 | + ), |
|
480 | + $available_spaces, |
|
481 | + '<br />' |
|
482 | + ); |
|
483 | + } |
|
484 | + \EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
485 | + } |
|
486 | + |
|
487 | + |
|
488 | + |
|
489 | + /** |
|
490 | + * ticketDatetimeAvailability |
|
491 | + * creates an array of tickets plus all of the datetimes available to each ticket |
|
492 | + * and tracks the spaces remaining for each of those datetimes |
|
493 | + * |
|
494 | + * @param \EE_Ticket $ticket - selected ticket |
|
495 | + * @param bool $get_original_ticket_spaces |
|
496 | + * @return int |
|
497 | + * @throws \EE_Error |
|
498 | + */ |
|
499 | + private function ticketDatetimeAvailability(\EE_Ticket $ticket, $get_original_ticket_spaces = false) |
|
500 | + { |
|
501 | + // if the $_available_spaces array has not been set up yet... |
|
502 | + if ( ! isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
503 | + $this->setInitialTicketDatetimeAvailability($ticket); |
|
504 | + } |
|
505 | + $available_spaces = $ticket->qty() - $ticket->sold(); |
|
506 | + if (isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
507 | + // loop thru tickets, which will ALSO include individual ticket records AND a total |
|
508 | + foreach (self::$_available_spaces['tickets'][$ticket->ID()] as $DTD_ID => $spaces) { |
|
509 | + // if we want the original datetime availability BEFORE we started subtracting tickets ? |
|
510 | + if ($get_original_ticket_spaces) { |
|
511 | + // then grab the available spaces from the "tickets" array |
|
512 | + // and compare with the above to get the lowest number |
|
513 | + $available_spaces = min( |
|
514 | + $available_spaces, |
|
515 | + self::$_available_spaces['tickets'][$ticket->ID()][$DTD_ID] |
|
516 | + ); |
|
517 | + } else { |
|
518 | + // we want the updated ticket availability as stored in the "datetimes" array |
|
519 | + $available_spaces = min($available_spaces, self::$_available_spaces['datetimes'][$DTD_ID]); |
|
520 | + } |
|
521 | + } |
|
522 | + } |
|
523 | + return $available_spaces; |
|
524 | + } |
|
525 | + |
|
526 | + |
|
527 | + |
|
528 | + /** |
|
529 | + * @param \EE_Ticket $ticket |
|
530 | + * @return void |
|
531 | + * @throws \EE_Error |
|
532 | + */ |
|
533 | + private function setInitialTicketDatetimeAvailability(\EE_Ticket $ticket) |
|
534 | + { |
|
535 | + // first, get all of the datetimes that are available to this ticket |
|
536 | + $datetimes = $ticket->get_many_related( |
|
537 | + 'Datetime', |
|
538 | + array( |
|
539 | + array( |
|
540 | + 'DTT_EVT_end' => array( |
|
541 | + '>=', |
|
542 | + \EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
543 | + ), |
|
544 | + ), |
|
545 | + 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
546 | + ) |
|
547 | + ); |
|
548 | + if ( ! empty($datetimes)) { |
|
549 | + // now loop thru all of the datetimes |
|
550 | + foreach ($datetimes as $datetime) { |
|
551 | + if ($datetime instanceof \EE_Datetime) { |
|
552 | + // the number of spaces available for the datetime without considering individual ticket quantities |
|
553 | + $spaces_remaining = $datetime->spaces_remaining(); |
|
554 | + // save the total available spaces ( the lesser of the ticket qty minus the number of tickets sold |
|
555 | + // or the datetime spaces remaining) to this ticket using the datetime ID as the key |
|
556 | + self::$_available_spaces['tickets'][$ticket->ID()][$datetime->ID()] = min( |
|
557 | + $ticket->qty() - $ticket->sold(), |
|
558 | + $spaces_remaining |
|
559 | + ); |
|
560 | + // if the remaining spaces for this datetime is already set, |
|
561 | + // then compare that against the datetime spaces remaining, and take the lowest number, |
|
562 | + // else just take the datetime spaces remaining, and assign to the datetimes array |
|
563 | + self::$_available_spaces['datetimes'][$datetime->ID()] = isset( |
|
564 | + self::$_available_spaces['datetimes'][$datetime->ID()] |
|
565 | + ) |
|
566 | + ? min(self::$_available_spaces['datetimes'][$datetime->ID()], $spaces_remaining) |
|
567 | + : $spaces_remaining; |
|
568 | + } |
|
569 | + } |
|
570 | + } |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + |
|
575 | + /** |
|
576 | + * @param \EE_Ticket $ticket |
|
577 | + * @param int $qty |
|
578 | + * @return void |
|
579 | + */ |
|
580 | + private function recalculateTicketDatetimeAvailability(\EE_Ticket $ticket, $qty = 0) |
|
581 | + { |
|
582 | + if (isset(self::$_available_spaces['tickets'][$ticket->ID()])) { |
|
583 | + // loop thru tickets, which will ALSO include individual ticket records AND a total |
|
584 | + foreach (self::$_available_spaces['tickets'][$ticket->ID()] as $DTD_ID => $spaces) { |
|
585 | + // subtract the qty of selected tickets from each datetime's available spaces this ticket has access to, |
|
586 | + self::$_available_spaces['datetimes'][$DTD_ID] -= $qty; |
|
587 | + } |
|
588 | + } |
|
589 | + } |
|
590 | 590 | |
591 | 591 | |
592 | 592 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | ); |
50 | 50 | } else { |
51 | 51 | wp_safe_redirect( |
52 | - site_url('/' . \EE_Registry::instance()->CFG->core->event_cpt_slug . '/') |
|
52 | + site_url('/'.\EE_Registry::instance()->CFG->core->event_cpt_slug.'/') |
|
53 | 53 | ); |
54 | 54 | } |
55 | 55 | exit(); |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | 'event_espresso' |
166 | 166 | ); |
167 | 167 | $limit_error_2 = sprintf($max_atndz_string, $valid['max_atndz'], $valid['max_atndz']); |
168 | - \EE_Error::add_error($limit_error_1 . '<br/>' . $limit_error_2, __FILE__, __FUNCTION__, __LINE__); |
|
168 | + \EE_Error::add_error($limit_error_1.'<br/>'.$limit_error_2, __FILE__, __FUNCTION__, __LINE__); |
|
169 | 169 | } else { |
170 | 170 | // all data appears to be valid |
171 | 171 | $tckts_slctd = false; |
@@ -305,9 +305,9 @@ discard block |
||
305 | 305 | // cycle through $inputs_to_clean array |
306 | 306 | foreach ($inputs_to_clean as $what => $input_to_clean) { |
307 | 307 | // check for POST data |
308 | - if (\EE_Registry::instance()->REQ->is_set($input_to_clean . $id)) { |
|
308 | + if (\EE_Registry::instance()->REQ->is_set($input_to_clean.$id)) { |
|
309 | 309 | // grab value |
310 | - $input_value = \EE_Registry::instance()->REQ->get($input_to_clean . $id); |
|
310 | + $input_value = \EE_Registry::instance()->REQ->get($input_to_clean.$id); |
|
311 | 311 | switch ($what) { |
312 | 312 | // integers |
313 | 313 | case 'event_id': |
@@ -328,8 +328,8 @@ discard block |
||
328 | 328 | // if qty is coming from a radio button input, then we need to assemble an array of rows |
329 | 329 | if ( ! is_array($row_qty)) { |
330 | 330 | // get number of rows |
331 | - $rows = \EE_Registry::instance()->REQ->is_set('tkt-slctr-rows-' . $id) |
|
332 | - ? absint(\EE_Registry::instance()->REQ->get('tkt-slctr-rows-' . $id)) |
|
331 | + $rows = \EE_Registry::instance()->REQ->is_set('tkt-slctr-rows-'.$id) |
|
332 | + ? absint(\EE_Registry::instance()->REQ->get('tkt-slctr-rows-'.$id)) |
|
333 | 333 | : 1; |
334 | 334 | // explode ints by the dash |
335 | 335 | $row_qty = explode('-', $row_qty); |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | case 'ticket_id': |
356 | 356 | $value_array = array(); |
357 | 357 | // cycle thru values |
358 | - foreach ((array)$input_value as $key => $value) { |
|
358 | + foreach ((array) $input_value as $key => $value) { |
|
359 | 359 | // allow only numbers, letters, spaces, commas and dashes |
360 | 360 | $value_array[$key] = wp_strip_all_tags($value); |
361 | 361 | // get ticket via the ticket id we put in the form |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | $input_value = explode('#', $input_value); |
374 | 374 | $input_value = end($input_value); |
375 | 375 | // use event list url instead, but append anchor |
376 | - $input_value = \EEH_Event_View::event_archive_url() . '#' . $input_value; |
|
376 | + $input_value = \EEH_Event_View::event_archive_url().'#'.$input_value; |
|
377 | 377 | } |
378 | 378 | $valid_data[$what] = $input_value; |
379 | 379 | break; |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | use EE_Datetime_Field; |
10 | 10 | |
11 | 11 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
12 | - exit('No direct script access allowed'); |
|
12 | + exit('No direct script access allowed'); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | |
@@ -27,1182 +27,1182 @@ discard block |
||
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * @var Calculated_Model_Fields |
|
32 | - */ |
|
33 | - protected $_fields_calculator; |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * Read constructor. |
|
39 | - */ |
|
40 | - public function __construct() |
|
41 | - { |
|
42 | - parent::__construct(); |
|
43 | - $this->_fields_calculator = new Calculated_Model_Fields(); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
50 | - * |
|
51 | - * @param \WP_REST_Request $request |
|
52 | - * @return \WP_REST_Response|\WP_Error |
|
53 | - */ |
|
54 | - public static function handle_request_get_all(\WP_REST_Request $request) |
|
55 | - { |
|
56 | - $controller = new Read(); |
|
57 | - try { |
|
58 | - $matches = $controller->parse_route( |
|
59 | - $request->get_route(), |
|
60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
61 | - array('version', 'model') |
|
62 | - ); |
|
63 | - $controller->set_requested_version($matches['version']); |
|
64 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | - return $controller->send_response( |
|
67 | - new \WP_Error( |
|
68 | - 'endpoint_parsing_error', |
|
69 | - sprintf( |
|
70 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
71 | - 'event_espresso'), |
|
72 | - $model_name_singular |
|
73 | - ) |
|
74 | - ) |
|
75 | - ); |
|
76 | - } |
|
77 | - return $controller->send_response( |
|
78 | - $controller->get_entities_from_model( |
|
79 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
80 | - $request |
|
81 | - ) |
|
82 | - ); |
|
83 | - } catch (\Exception $e) { |
|
84 | - return $controller->send_response($e); |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Prepares and returns schema for any OPTIONS request. |
|
91 | - * |
|
92 | - * @param string $model_name Something like `Event` or `Registration` |
|
93 | - * @param string $version The API endpoint version being used. |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public static function handle_schema_request($model_name, $version) |
|
97 | - { |
|
98 | - $controller = new Read(); |
|
99 | - try { |
|
100 | - $controller->set_requested_version($version); |
|
101 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
102 | - return array(); |
|
103 | - } |
|
104 | - //get the model for this version |
|
105 | - $model = $controller->get_model_version_info()->load_model($model_name); |
|
106 | - $model_schema = new JsonModelSchema($model); |
|
107 | - return $model_schema->getModelSchemaForRelations( |
|
108 | - $controller->get_model_version_info()->relation_settings($model), |
|
109 | - $controller->_add_extra_fields_to_schema( |
|
110 | - $model, |
|
111 | - $model_schema->getModelSchemaForFields( |
|
112 | - $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
113 | - $model_schema->getInitialSchemaStructure() |
|
114 | - ) |
|
115 | - ) |
|
116 | - ); |
|
117 | - } catch (\Exception $e) { |
|
118 | - return array(); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * Adds additional fields to the schema |
|
125 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
126 | - * needs to be added to the schema. |
|
127 | - * |
|
128 | - * @param \EEM_Base $model |
|
129 | - * @param string $schema |
|
130 | - */ |
|
131 | - protected function _add_extra_fields_to_schema(\EEM_Base $model, $schema) |
|
132 | - { |
|
133 | - foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
134 | - if ($field instanceof EE_Datetime_Field) { |
|
135 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
136 | - //modify the description |
|
137 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
138 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
139 | - $field->get_nicename() |
|
140 | - ); |
|
141 | - } |
|
142 | - } |
|
143 | - return $schema; |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - protected function get_route_from_request() { |
|
154 | - if (isset($GLOBALS['wp']) |
|
155 | - && $GLOBALS['wp'] instanceof \WP |
|
156 | - && isset($GLOBALS['wp']->query_vars['rest_route'] ) |
|
157 | - ) { |
|
158 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
159 | - } else { |
|
160 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Gets a single entity related to the model indicated in the path and its id |
|
168 | - * |
|
169 | - * @param \WP_REST_Request $request |
|
170 | - * @return \WP_REST_Response|\WP_Error |
|
171 | - */ |
|
172 | - public static function handle_request_get_one(\WP_REST_Request $request) |
|
173 | - { |
|
174 | - $controller = new Read(); |
|
175 | - try { |
|
176 | - $matches = $controller->parse_route( |
|
177 | - $request->get_route(), |
|
178 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
179 | - array('version', 'model', 'id')); |
|
180 | - $controller->set_requested_version($matches['version']); |
|
181 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
182 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
183 | - return $controller->send_response( |
|
184 | - new \WP_Error( |
|
185 | - 'endpoint_parsing_error', |
|
186 | - sprintf( |
|
187 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
188 | - 'event_espresso'), |
|
189 | - $model_name_singular |
|
190 | - ) |
|
191 | - ) |
|
192 | - ); |
|
193 | - } |
|
194 | - return $controller->send_response( |
|
195 | - $controller->get_entity_from_model( |
|
196 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
197 | - $request |
|
198 | - ) |
|
199 | - ); |
|
200 | - } catch (\Exception $e) { |
|
201 | - return $controller->send_response($e); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
209 | - * to the item with the given id |
|
210 | - * |
|
211 | - * @param \WP_REST_Request $request |
|
212 | - * @return \WP_REST_Response|\WP_Error |
|
213 | - */ |
|
214 | - public static function handle_request_get_related(\WP_REST_Request $request) |
|
215 | - { |
|
216 | - $controller = new Read(); |
|
217 | - try { |
|
218 | - $matches = $controller->parse_route( |
|
219 | - $request->get_route(), |
|
220 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
221 | - array('version', 'model', 'id', 'related_model') |
|
222 | - ); |
|
223 | - $controller->set_requested_version($matches['version']); |
|
224 | - $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
225 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
226 | - return $controller->send_response( |
|
227 | - new \WP_Error( |
|
228 | - 'endpoint_parsing_error', |
|
229 | - sprintf( |
|
230 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
231 | - 'event_espresso'), |
|
232 | - $main_model_name_singular |
|
233 | - ) |
|
234 | - ) |
|
235 | - ); |
|
236 | - } |
|
237 | - $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
238 | - //assume the related model name is plural and try to find the model's name |
|
239 | - $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
240 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
241 | - //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
242 | - $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
243 | - } |
|
244 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
245 | - return $controller->send_response( |
|
246 | - new \WP_Error( |
|
247 | - 'endpoint_parsing_error', |
|
248 | - sprintf( |
|
249 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
250 | - 'event_espresso'), |
|
251 | - $related_model_name_singular |
|
252 | - ) |
|
253 | - ) |
|
254 | - ); |
|
255 | - } |
|
256 | - return $controller->send_response( |
|
257 | - $controller->get_entities_from_relation( |
|
258 | - $request->get_param('id'), |
|
259 | - $main_model->related_settings_for($related_model_name_singular), |
|
260 | - $request |
|
261 | - ) |
|
262 | - ); |
|
263 | - } catch (\Exception $e) { |
|
264 | - return $controller->send_response($e); |
|
265 | - } |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Gets a collection for the given model and filters |
|
272 | - * |
|
273 | - * @param \EEM_Base $model |
|
274 | - * @param \WP_REST_Request $request |
|
275 | - * @return array|\WP_Error |
|
276 | - */ |
|
277 | - public function get_entities_from_model($model, $request) |
|
278 | - { |
|
279 | - $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
280 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
281 | - $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
282 | - return new \WP_Error( |
|
283 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
284 | - sprintf( |
|
285 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
286 | - $model_name_plural, |
|
287 | - Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
288 | - ), |
|
289 | - array('status' => 403) |
|
290 | - ); |
|
291 | - } |
|
292 | - if (! $request->get_header('no_rest_headers')) { |
|
293 | - $this->_set_headers_from_query_params($model, $query_params); |
|
294 | - } |
|
295 | - /** @type array $results */ |
|
296 | - $results = $model->get_all_wpdb_results($query_params); |
|
297 | - $nice_results = array(); |
|
298 | - foreach ($results as $result) { |
|
299 | - $nice_results[] = $this->create_entity_from_wpdb_result( |
|
300 | - $model, |
|
301 | - $result, |
|
302 | - $request |
|
303 | - ); |
|
304 | - } |
|
305 | - return $nice_results; |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * @param array $primary_model_query_params query params for finding the item from which |
|
312 | - * relations will be based |
|
313 | - * @param \EE_Model_Relation_Base $relation |
|
314 | - * @param \WP_REST_Request $request |
|
315 | - * @return \WP_Error|array |
|
316 | - */ |
|
317 | - protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
318 | - { |
|
319 | - $context = $this->validate_context($request->get_param('caps')); |
|
320 | - $model = $relation->get_this_model(); |
|
321 | - $related_model = $relation->get_other_model(); |
|
322 | - if (! isset($primary_model_query_params[0])) { |
|
323 | - $primary_model_query_params[0] = array(); |
|
324 | - } |
|
325 | - //check if they can access the 1st model object |
|
326 | - $primary_model_query_params = array( |
|
327 | - 0 => $primary_model_query_params[0], |
|
328 | - 'limit' => 1, |
|
329 | - ); |
|
330 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
331 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
332 | - } |
|
333 | - $restricted_query_params = $primary_model_query_params; |
|
334 | - $restricted_query_params['caps'] = $context; |
|
335 | - $this->_set_debug_info('main model query params', $restricted_query_params); |
|
336 | - $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
337 | - if ( |
|
338 | - ! ( |
|
339 | - Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
340 | - && $model->exists($restricted_query_params) |
|
341 | - ) |
|
342 | - ) { |
|
343 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
344 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
345 | - } else { |
|
346 | - $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
347 | - } |
|
348 | - return new \WP_Error( |
|
349 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
350 | - sprintf( |
|
351 | - __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
352 | - 'event_espresso'), |
|
353 | - $related_model_name_maybe_plural, |
|
354 | - $relation->get_this_model()->get_this_model_name(), |
|
355 | - implode( |
|
356 | - ',', |
|
357 | - array_keys( |
|
358 | - Capabilities::get_missing_permissions($related_model, $context) |
|
359 | - ) |
|
360 | - ) |
|
361 | - ), |
|
362 | - array('status' => 403) |
|
363 | - ); |
|
364 | - } |
|
365 | - $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
366 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
367 | - $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
368 | - . '.' |
|
369 | - . $where_condition_key] = $where_condition_value; |
|
370 | - } |
|
371 | - $query_params['default_where_conditions'] = 'none'; |
|
372 | - $query_params['caps'] = $context; |
|
373 | - if (! $request->get_header('no_rest_headers')) { |
|
374 | - $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
375 | - } |
|
376 | - /** @type array $results */ |
|
377 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
378 | - $nice_results = array(); |
|
379 | - foreach ($results as $result) { |
|
380 | - $nice_result = $this->create_entity_from_wpdb_result( |
|
381 | - $relation->get_other_model(), |
|
382 | - $result, |
|
383 | - $request |
|
384 | - ); |
|
385 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
386 | - //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
387 | - //if there are conflicts we prefer the properties from the main model |
|
388 | - $join_model_result = $this->create_entity_from_wpdb_result( |
|
389 | - $relation->get_join_model(), |
|
390 | - $result, |
|
391 | - $request |
|
392 | - ); |
|
393 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
394 | - //but keep the meta stuff from the main model |
|
395 | - if (isset($nice_result['meta'])) { |
|
396 | - $joined_result['meta'] = $nice_result['meta']; |
|
397 | - } |
|
398 | - $nice_result = $joined_result; |
|
399 | - } |
|
400 | - $nice_results[] = $nice_result; |
|
401 | - } |
|
402 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
403 | - return array_shift($nice_results); |
|
404 | - } else { |
|
405 | - return $nice_results; |
|
406 | - } |
|
407 | - } |
|
408 | - |
|
409 | - |
|
410 | - |
|
411 | - /** |
|
412 | - * Gets the collection for given relation object |
|
413 | - * The same as Read::get_entities_from_model(), except if the relation |
|
414 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
415 | - * the join-model-object into the results |
|
416 | - * |
|
417 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
418 | - * @param \EE_Model_Relation_Base $relation |
|
419 | - * @param \WP_REST_Request $request |
|
420 | - * @return array|\WP_Error |
|
421 | - * @throws \EE_Error |
|
422 | - */ |
|
423 | - public function get_entities_from_relation($id, $relation, $request) |
|
424 | - { |
|
425 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
426 | - throw new \EE_Error( |
|
427 | - sprintf( |
|
428 | - __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
429 | - 'event_espresso'), |
|
430 | - $relation->get_this_model()->get_this_model_name() |
|
431 | - ) |
|
432 | - ); |
|
433 | - } |
|
434 | - return $this->_get_entities_from_relation( |
|
435 | - array( |
|
436 | - array( |
|
437 | - $relation->get_this_model()->primary_key_name() => $id, |
|
438 | - ), |
|
439 | - ), |
|
440 | - $relation, |
|
441 | - $request |
|
442 | - ); |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * Sets the headers that are based on the model and query params, |
|
449 | - * like the total records. This should only be called on the original request |
|
450 | - * from the client, not on subsequent internal |
|
451 | - * |
|
452 | - * @param \EEM_Base $model |
|
453 | - * @param array $query_params |
|
454 | - * @return void |
|
455 | - */ |
|
456 | - protected function _set_headers_from_query_params($model, $query_params) |
|
457 | - { |
|
458 | - $this->_set_debug_info('model query params', $query_params); |
|
459 | - $this->_set_debug_info('missing caps', |
|
460 | - Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
461 | - //normally the limit to a 2-part array, where the 2nd item is the limit |
|
462 | - if (! isset($query_params['limit'])) { |
|
463 | - $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
464 | - } |
|
465 | - if (is_array($query_params['limit'])) { |
|
466 | - $limit_parts = $query_params['limit']; |
|
467 | - } else { |
|
468 | - $limit_parts = explode(',', $query_params['limit']); |
|
469 | - if (count($limit_parts) == 1) { |
|
470 | - $limit_parts = array(0, $limit_parts[0]); |
|
471 | - } |
|
472 | - } |
|
473 | - //remove the group by and having parts of the query, as those will |
|
474 | - //make the sql query return an array of values, instead of just a single value |
|
475 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
476 | - $count = $model->count($query_params, null, true); |
|
477 | - $pages = $count / $limit_parts[1]; |
|
478 | - $this->_set_response_header('Total', $count, false); |
|
479 | - $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
480 | - $this->_set_response_header('TotalPages', ceil($pages), false); |
|
481 | - } |
|
482 | - |
|
483 | - |
|
484 | - |
|
485 | - /** |
|
486 | - * Changes database results into REST API entities |
|
487 | - * |
|
488 | - * @param \EEM_Base $model |
|
489 | - * @param array $db_row like results from $wpdb->get_results() |
|
490 | - * @param \WP_REST_Request $rest_request |
|
491 | - * @param string $deprecated no longer used |
|
492 | - * @return array ready for being converted into json for sending to client |
|
493 | - */ |
|
494 | - public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
495 | - { |
|
496 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
497 | - //ok so this was called in the old style, where the 3rd arg was |
|
498 | - //$include, and the 4th arg was $context |
|
499 | - //now setup the request just to avoid fatal errors, although we won't be able |
|
500 | - //to truly make use of it because it's kinda devoid of info |
|
501 | - $rest_request = new \WP_REST_Request(); |
|
502 | - $rest_request->set_param('include', $rest_request); |
|
503 | - $rest_request->set_param('caps', $deprecated); |
|
504 | - } |
|
505 | - if ($rest_request->get_param('caps') == null) { |
|
506 | - $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
507 | - } |
|
508 | - $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
509 | - $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
510 | - $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
511 | - $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
512 | - $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
513 | - $entity_array = apply_filters( |
|
514 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
515 | - $entity_array, |
|
516 | - $model, |
|
517 | - $rest_request->get_param('caps'), |
|
518 | - $rest_request, |
|
519 | - $this |
|
520 | - ); |
|
521 | - $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
522 | - $entity_array, |
|
523 | - $model, |
|
524 | - $rest_request->get_param('caps'), |
|
525 | - $this->get_model_version_info(), |
|
526 | - $model->get_index_primary_key_string( |
|
527 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
528 | - ) |
|
529 | - ); |
|
530 | - $this->_set_debug_info( |
|
531 | - 'inaccessible fields', |
|
532 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
533 | - ); |
|
534 | - return apply_filters( |
|
535 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
536 | - $result_without_inaccessible_fields, |
|
537 | - $model, |
|
538 | - $rest_request->get_param('caps') |
|
539 | - ); |
|
540 | - } |
|
541 | - |
|
542 | - |
|
543 | - |
|
544 | - /** |
|
545 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
546 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
547 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
548 | - * |
|
549 | - * @param \EEM_Base $model |
|
550 | - * @param array $db_row |
|
551 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
552 | - */ |
|
553 | - protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
554 | - { |
|
555 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
556 | - $result = array_intersect_key($result, |
|
557 | - $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
558 | - foreach ($result as $field_name => $raw_field_value) { |
|
559 | - $field_obj = $model->field_settings_for($field_name); |
|
560 | - $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
561 | - if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
562 | - unset($result[$field_name]); |
|
563 | - } elseif ( |
|
564 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
565 | - ) { |
|
566 | - $result[$field_name] = array( |
|
567 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
568 | - 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
569 | - ); |
|
570 | - } elseif ( |
|
571 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
572 | - ) { |
|
573 | - $result[$field_name] = array( |
|
574 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
575 | - 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
576 | - ); |
|
577 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
578 | - if ($field_value instanceof \DateTime) { |
|
579 | - $timezone = $field_value->getTimezone(); |
|
580 | - $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
581 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
582 | - $field_obj, |
|
583 | - $field_value, |
|
584 | - $this->get_model_version_info()->requested_version() |
|
585 | - ); |
|
586 | - $field_value->setTimezone($timezone); |
|
587 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
588 | - $field_obj, |
|
589 | - $field_value, |
|
590 | - $this->get_model_version_info()->requested_version() |
|
591 | - ); |
|
592 | - } |
|
593 | - } else { |
|
594 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
595 | - $field_obj, |
|
596 | - $field_obj->prepare_for_get($field_value), |
|
597 | - $this->get_model_version_info()->requested_version() |
|
598 | - ); |
|
599 | - } |
|
600 | - } |
|
601 | - return $result; |
|
602 | - } |
|
603 | - |
|
604 | - |
|
605 | - |
|
606 | - /** |
|
607 | - * Adds a few extra fields to the entity response |
|
608 | - * |
|
609 | - * @param \EEM_Base $model |
|
610 | - * @param array $db_row |
|
611 | - * @param array $entity_array |
|
612 | - * @return array modified entity |
|
613 | - */ |
|
614 | - protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
615 | - { |
|
616 | - if ($model instanceof \EEM_CPT_Base) { |
|
617 | - $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
618 | - } |
|
619 | - return $entity_array; |
|
620 | - } |
|
621 | - |
|
622 | - |
|
623 | - |
|
624 | - /** |
|
625 | - * Gets links we want to add to the response |
|
626 | - * |
|
627 | - * @global \WP_REST_Server $wp_rest_server |
|
628 | - * @param \EEM_Base $model |
|
629 | - * @param array $db_row |
|
630 | - * @param array $entity_array |
|
631 | - * @return array the _links item in the entity |
|
632 | - */ |
|
633 | - protected function _get_entity_links($model, $db_row, $entity_array) |
|
634 | - { |
|
635 | - //add basic links |
|
636 | - $links = array(); |
|
637 | - if ($model->has_primary_key_field()) { |
|
638 | - $links['self'] = array( |
|
639 | - array( |
|
640 | - 'href' => $this->get_versioned_link_to( |
|
641 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
642 | - . '/' |
|
643 | - . $entity_array[$model->primary_key_name()] |
|
644 | - ), |
|
645 | - ), |
|
646 | - ); |
|
647 | - } |
|
648 | - $links['collection'] = array( |
|
649 | - array( |
|
650 | - 'href' => $this->get_versioned_link_to( |
|
651 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
652 | - ), |
|
653 | - ), |
|
654 | - ); |
|
655 | - //add links to related models |
|
656 | - if ($model->has_primary_key_field()) { |
|
657 | - foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
658 | - $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
659 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
660 | - array( |
|
661 | - 'href' => $this->get_versioned_link_to( |
|
662 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
663 | - . '/' |
|
664 | - . $entity_array[$model->primary_key_name()] |
|
665 | - . '/' |
|
666 | - . $related_model_part |
|
667 | - ), |
|
668 | - 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
669 | - ), |
|
670 | - ); |
|
671 | - } |
|
672 | - } |
|
673 | - return $links; |
|
674 | - } |
|
675 | - |
|
676 | - |
|
677 | - |
|
678 | - /** |
|
679 | - * Adds the included models indicated in the request to the entity provided |
|
680 | - * |
|
681 | - * @param \EEM_Base $model |
|
682 | - * @param \WP_REST_Request $rest_request |
|
683 | - * @param array $entity_array |
|
684 | - * @param array $db_row |
|
685 | - * @return array the modified entity |
|
686 | - */ |
|
687 | - protected function _include_requested_models( |
|
688 | - \EEM_Base $model, |
|
689 | - \WP_REST_Request $rest_request, |
|
690 | - $entity_array, |
|
691 | - $db_row = array() |
|
692 | - ) { |
|
693 | - //if $db_row not included, hope the entity array has what we need |
|
694 | - if (! $db_row) { |
|
695 | - $db_row = $entity_array; |
|
696 | - } |
|
697 | - $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
698 | - $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
699 | - //if they passed in * or didn't specify any includes, return everything |
|
700 | - if (! in_array('*', $includes_for_this_model) |
|
701 | - && ! empty($includes_for_this_model) |
|
702 | - ) { |
|
703 | - if ($model->has_primary_key_field()) { |
|
704 | - //always include the primary key. ya just gotta know that at least |
|
705 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
706 | - } |
|
707 | - if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
708 | - $includes_for_this_model[] = '_calculated_fields'; |
|
709 | - } |
|
710 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
711 | - } |
|
712 | - $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
713 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
714 | - $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
715 | - $rest_request->get_param('include'), |
|
716 | - $relation_name |
|
717 | - ); |
|
718 | - $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
719 | - $rest_request->get_param('calculate'), |
|
720 | - $relation_name |
|
721 | - ); |
|
722 | - //did they specify they wanted to include a related model, or |
|
723 | - //specific fields from a related model? |
|
724 | - //or did they specify to calculate a field from a related model? |
|
725 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
726 | - //if so, we should include at least some part of the related model |
|
727 | - $pretend_related_request = new \WP_REST_Request(); |
|
728 | - $pretend_related_request->set_query_params( |
|
729 | - array( |
|
730 | - 'caps' => $rest_request->get_param('caps'), |
|
731 | - 'include' => $related_fields_to_include, |
|
732 | - 'calculate' => $related_fields_to_calculate, |
|
733 | - ) |
|
734 | - ); |
|
735 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
736 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
737 | - $model->get_index_primary_key_string( |
|
738 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
739 | - ) |
|
740 | - ); |
|
741 | - $related_results = $this->_get_entities_from_relation( |
|
742 | - $primary_model_query_params, |
|
743 | - $relation_obj, |
|
744 | - $pretend_related_request |
|
745 | - ); |
|
746 | - $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
747 | - instanceof |
|
748 | - \WP_Error |
|
749 | - ? null |
|
750 | - : $related_results; |
|
751 | - } |
|
752 | - } |
|
753 | - return $entity_array; |
|
754 | - } |
|
755 | - |
|
756 | - |
|
757 | - |
|
758 | - /** |
|
759 | - * Returns a new array with all the names of models removed. Eg |
|
760 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
761 | - * |
|
762 | - * @param array $arr |
|
763 | - * @return array |
|
764 | - */ |
|
765 | - private function _remove_model_names_from_array($arr) |
|
766 | - { |
|
767 | - return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
768 | - } |
|
769 | - |
|
770 | - |
|
771 | - |
|
772 | - /** |
|
773 | - * Gets the calculated fields for the response |
|
774 | - * |
|
775 | - * @param \EEM_Base $model |
|
776 | - * @param array $wpdb_row |
|
777 | - * @param \WP_REST_Request $rest_request |
|
778 | - * @return \stdClass the _calculations item in the entity |
|
779 | - */ |
|
780 | - protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
781 | - { |
|
782 | - $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
783 | - $rest_request->get_param('calculate'), |
|
784 | - '' |
|
785 | - ); |
|
786 | - //note: setting calculate=* doesn't do anything |
|
787 | - $calculated_fields_to_return = new \stdClass(); |
|
788 | - foreach ($calculated_fields as $field_to_calculate) { |
|
789 | - try { |
|
790 | - $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
791 | - null, |
|
792 | - $this->_fields_calculator->retrieve_calculated_field_value( |
|
793 | - $model, |
|
794 | - $field_to_calculate, |
|
795 | - $wpdb_row, |
|
796 | - $rest_request, |
|
797 | - $this |
|
798 | - ), |
|
799 | - $this->get_model_version_info()->requested_version() |
|
800 | - ); |
|
801 | - } catch (Rest_Exception $e) { |
|
802 | - //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
803 | - $this->_set_response_header( |
|
804 | - 'Notices-Field-Calculation-Errors[' |
|
805 | - . $e->get_string_code() |
|
806 | - . '][' |
|
807 | - . $model->get_this_model_name() |
|
808 | - . '][' |
|
809 | - . $field_to_calculate |
|
810 | - . ']', |
|
811 | - $e->getMessage(), |
|
812 | - true |
|
813 | - ); |
|
814 | - } |
|
815 | - } |
|
816 | - return $calculated_fields_to_return; |
|
817 | - } |
|
818 | - |
|
819 | - |
|
820 | - |
|
821 | - /** |
|
822 | - * Gets the full URL to the resource, taking the requested version into account |
|
823 | - * |
|
824 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
825 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
826 | - */ |
|
827 | - public function get_versioned_link_to($link_part_after_version_and_slash) |
|
828 | - { |
|
829 | - return rest_url( |
|
830 | - \EED_Core_Rest_Api::ee_api_namespace |
|
831 | - . $this->get_model_version_info()->requested_version() |
|
832 | - . '/' |
|
833 | - . $link_part_after_version_and_slash |
|
834 | - ); |
|
835 | - } |
|
836 | - |
|
837 | - |
|
838 | - |
|
839 | - /** |
|
840 | - * Gets the correct lowercase name for the relation in the API according |
|
841 | - * to the relation's type |
|
842 | - * |
|
843 | - * @param string $relation_name |
|
844 | - * @param \EE_Model_Relation_Base $relation_obj |
|
845 | - * @return string |
|
846 | - */ |
|
847 | - public static function get_related_entity_name($relation_name, $relation_obj) |
|
848 | - { |
|
849 | - if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
850 | - return strtolower($relation_name); |
|
851 | - } else { |
|
852 | - return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
853 | - } |
|
854 | - } |
|
855 | - |
|
856 | - |
|
857 | - |
|
858 | - /** |
|
859 | - * Gets the one model object with the specified id for the specified model |
|
860 | - * |
|
861 | - * @param \EEM_Base $model |
|
862 | - * @param \WP_REST_Request $request |
|
863 | - * @return array|\WP_Error |
|
864 | - */ |
|
865 | - public function get_entity_from_model($model, $request) |
|
866 | - { |
|
867 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
868 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
869 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
870 | - } |
|
871 | - $restricted_query_params = $query_params; |
|
872 | - $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
873 | - $this->_set_debug_info('model query params', $restricted_query_params); |
|
874 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
875 | - if (! empty ($model_rows)) { |
|
876 | - return $this->create_entity_from_wpdb_result( |
|
877 | - $model, |
|
878 | - array_shift($model_rows), |
|
879 | - $request); |
|
880 | - } else { |
|
881 | - //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
882 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
883 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
884 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
885 | - //you got shafted- it existed but we didn't want to tell you! |
|
886 | - return new \WP_Error( |
|
887 | - 'rest_user_cannot_read', |
|
888 | - sprintf( |
|
889 | - __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
890 | - strtolower($model->get_this_model_name()), |
|
891 | - Capabilities::get_missing_permissions_string( |
|
892 | - $model, |
|
893 | - $this->validate_context($request->get_param('caps'))) |
|
894 | - ), |
|
895 | - array('status' => 403) |
|
896 | - ); |
|
897 | - } else { |
|
898 | - //it's not you. It just doesn't exist |
|
899 | - return new \WP_Error( |
|
900 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
901 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
902 | - array('status' => 404) |
|
903 | - ); |
|
904 | - } |
|
905 | - } |
|
906 | - } |
|
907 | - |
|
908 | - |
|
909 | - |
|
910 | - /** |
|
911 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
912 | - * version so just treat it as a default read |
|
913 | - * |
|
914 | - * @param string $context |
|
915 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
916 | - */ |
|
917 | - public function validate_context($context) |
|
918 | - { |
|
919 | - if (! $context) { |
|
920 | - $context = \EEM_Base::caps_read; |
|
921 | - } |
|
922 | - $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
923 | - if (in_array($context, $valid_contexts)) { |
|
924 | - return $context; |
|
925 | - } else { |
|
926 | - return \EEM_Base::caps_read; |
|
927 | - } |
|
928 | - } |
|
929 | - |
|
930 | - |
|
931 | - |
|
932 | - /** |
|
933 | - * Verifies the passed in value is an allowable default where conditions value. |
|
934 | - * |
|
935 | - * @param $default_query_params |
|
936 | - * @return string |
|
937 | - */ |
|
938 | - public function validate_default_query_params($default_query_params) |
|
939 | - { |
|
940 | - $valid_default_where_conditions_for_api_calls = array( |
|
941 | - \EEM_Base::default_where_conditions_all, |
|
942 | - \EEM_Base::default_where_conditions_minimum_all, |
|
943 | - \EEM_Base::default_where_conditions_minimum_others, |
|
944 | - ); |
|
945 | - if (! $default_query_params) { |
|
946 | - $default_query_params = \EEM_Base::default_where_conditions_all; |
|
947 | - } |
|
948 | - if ( |
|
949 | - in_array( |
|
950 | - $default_query_params, |
|
951 | - $valid_default_where_conditions_for_api_calls, |
|
952 | - true |
|
953 | - ) |
|
954 | - ) { |
|
955 | - return $default_query_params; |
|
956 | - } else { |
|
957 | - return \EEM_Base::default_where_conditions_all; |
|
958 | - } |
|
959 | - } |
|
960 | - |
|
961 | - |
|
962 | - |
|
963 | - /** |
|
964 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
965 | - * Note: right now the query parameter keys for fields (and related fields) |
|
966 | - * can be left as-is, but it's quite possible this will change someday. |
|
967 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
968 | - * |
|
969 | - * @param \EEM_Base $model |
|
970 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
971 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
972 | - * that absolutely no results should be returned |
|
973 | - * @throws \EE_Error |
|
974 | - */ |
|
975 | - public function create_model_query_params($model, $query_parameters) |
|
976 | - { |
|
977 | - $model_query_params = array(); |
|
978 | - if (isset($query_parameters['where'])) { |
|
979 | - $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
980 | - $query_parameters['where'], |
|
981 | - $model, |
|
982 | - $this->get_model_version_info()->requested_version() |
|
983 | - ); |
|
984 | - } |
|
985 | - if (isset($query_parameters['order_by'])) { |
|
986 | - $order_by = $query_parameters['order_by']; |
|
987 | - } elseif (isset($query_parameters['orderby'])) { |
|
988 | - $order_by = $query_parameters['orderby']; |
|
989 | - } else { |
|
990 | - $order_by = null; |
|
991 | - } |
|
992 | - if ($order_by !== null) { |
|
993 | - if (is_array($order_by)) { |
|
994 | - $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
995 | - } else { |
|
996 | - //it's a single item |
|
997 | - $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
998 | - } |
|
999 | - $model_query_params['order_by'] = $order_by; |
|
1000 | - } |
|
1001 | - if (isset($query_parameters['group_by'])) { |
|
1002 | - $group_by = $query_parameters['group_by']; |
|
1003 | - } elseif (isset($query_parameters['groupby'])) { |
|
1004 | - $group_by = $query_parameters['groupby']; |
|
1005 | - } else { |
|
1006 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1007 | - } |
|
1008 | - //make sure they're all real names |
|
1009 | - if (is_array($group_by)) { |
|
1010 | - $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
1011 | - } |
|
1012 | - if ($group_by !== null) { |
|
1013 | - $model_query_params['group_by'] = $group_by; |
|
1014 | - } |
|
1015 | - if (isset($query_parameters['having'])) { |
|
1016 | - $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1017 | - $query_parameters['having'], |
|
1018 | - $model, |
|
1019 | - $this->get_model_version_info()->requested_version() |
|
1020 | - ); |
|
1021 | - } |
|
1022 | - if (isset($query_parameters['order'])) { |
|
1023 | - $model_query_params['order'] = $query_parameters['order']; |
|
1024 | - } |
|
1025 | - if (isset($query_parameters['mine'])) { |
|
1026 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1027 | - } |
|
1028 | - if (isset($query_parameters['limit'])) { |
|
1029 | - //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1030 | - if (! is_array($query_parameters['limit'])) { |
|
1031 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1032 | - } else { |
|
1033 | - $limit_array = $query_parameters['limit']; |
|
1034 | - } |
|
1035 | - $sanitized_limit = array(); |
|
1036 | - foreach ($limit_array as $key => $limit_part) { |
|
1037 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1038 | - throw new \EE_Error( |
|
1039 | - sprintf( |
|
1040 | - __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1041 | - 'event_espresso'), |
|
1042 | - wp_json_encode($query_parameters['limit']) |
|
1043 | - ) |
|
1044 | - ); |
|
1045 | - } |
|
1046 | - $sanitized_limit[] = (int)$limit_part; |
|
1047 | - } |
|
1048 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1049 | - } else { |
|
1050 | - $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
1051 | - } |
|
1052 | - if (isset($query_parameters['caps'])) { |
|
1053 | - $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
1054 | - } else { |
|
1055 | - $model_query_params['caps'] = \EEM_Base::caps_read; |
|
1056 | - } |
|
1057 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1058 | - $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
1059 | - } |
|
1060 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1061 | - } |
|
1062 | - |
|
1063 | - |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * Changes the REST-style query params for use in the models |
|
1067 | - * |
|
1068 | - * @deprecated |
|
1069 | - * @param \EEM_Base $model |
|
1070 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1071 | - * @return array |
|
1072 | - */ |
|
1073 | - public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
1074 | - { |
|
1075 | - $model_ready_query_params = array(); |
|
1076 | - foreach ($query_params as $key => $value) { |
|
1077 | - if (is_array($value)) { |
|
1078 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1079 | - } else { |
|
1080 | - $model_ready_query_params[$key] = $value; |
|
1081 | - } |
|
1082 | - } |
|
1083 | - return $model_ready_query_params; |
|
1084 | - } |
|
1085 | - |
|
1086 | - |
|
1087 | - |
|
1088 | - /** |
|
1089 | - * @deprecated |
|
1090 | - * @param $model |
|
1091 | - * @param $query_params |
|
1092 | - * @return array |
|
1093 | - */ |
|
1094 | - public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1095 | - { |
|
1096 | - $model_ready_query_params = array(); |
|
1097 | - foreach ($query_params as $key => $value) { |
|
1098 | - if (is_array($value)) { |
|
1099 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1100 | - } else { |
|
1101 | - $model_ready_query_params[$key] = $value; |
|
1102 | - } |
|
1103 | - } |
|
1104 | - return $model_ready_query_params; |
|
1105 | - } |
|
1106 | - |
|
1107 | - |
|
1108 | - |
|
1109 | - /** |
|
1110 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1111 | - * If no prefix is specified, returns items with no period. |
|
1112 | - * |
|
1113 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1114 | - * @param string $prefix "Event" or "foobar" |
|
1115 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1116 | - * we only return strings starting with that and a period; if no prefix was |
|
1117 | - * specified we return all items containing NO periods |
|
1118 | - */ |
|
1119 | - public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1120 | - { |
|
1121 | - if (is_string($string_to_explode)) { |
|
1122 | - $exploded_contents = explode(',', $string_to_explode); |
|
1123 | - } else if (is_array($string_to_explode)) { |
|
1124 | - $exploded_contents = $string_to_explode; |
|
1125 | - } else { |
|
1126 | - $exploded_contents = array(); |
|
1127 | - } |
|
1128 | - //if the string was empty, we want an empty array |
|
1129 | - $exploded_contents = array_filter($exploded_contents); |
|
1130 | - $contents_with_prefix = array(); |
|
1131 | - foreach ($exploded_contents as $item) { |
|
1132 | - $item = trim($item); |
|
1133 | - //if no prefix was provided, so we look for items with no "." in them |
|
1134 | - if (! $prefix) { |
|
1135 | - //does this item have a period? |
|
1136 | - if (strpos($item, '.') === false) { |
|
1137 | - //if not, then its what we're looking for |
|
1138 | - $contents_with_prefix[] = $item; |
|
1139 | - } |
|
1140 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1141 | - //this item has the prefix and a period, grab it |
|
1142 | - $contents_with_prefix[] = substr( |
|
1143 | - $item, |
|
1144 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1145 | - ); |
|
1146 | - } else if ($item === $prefix) { |
|
1147 | - //this item is JUST the prefix |
|
1148 | - //so let's grab everything after, which is a blank string |
|
1149 | - $contents_with_prefix[] = ''; |
|
1150 | - } |
|
1151 | - } |
|
1152 | - return $contents_with_prefix; |
|
1153 | - } |
|
1154 | - |
|
1155 | - |
|
1156 | - |
|
1157 | - /** |
|
1158 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1159 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1160 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1161 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1162 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1163 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1164 | - * name and then a period). |
|
1165 | - * @param string $include_string @see Read:handle_request_get_all |
|
1166 | - * @param string $model_name |
|
1167 | - * @return array of fields for this model. If $model_name is provided, then |
|
1168 | - * the fields for that model, with the model's name removed from each. |
|
1169 | - * If $include_string was blank or '*' returns an empty array |
|
1170 | - */ |
|
1171 | - public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1172 | - { |
|
1173 | - if (is_array($include_string)) { |
|
1174 | - $include_string = implode(',', $include_string); |
|
1175 | - } |
|
1176 | - if ($include_string === '*' || $include_string === '') { |
|
1177 | - return array(); |
|
1178 | - } |
|
1179 | - $includes = explode(',', $include_string); |
|
1180 | - $extracted_fields_to_include = array(); |
|
1181 | - if ($model_name) { |
|
1182 | - foreach ($includes as $field_to_include) { |
|
1183 | - $field_to_include = trim($field_to_include); |
|
1184 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1185 | - //found the model name at the exact start |
|
1186 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1187 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1188 | - } elseif ($field_to_include == $model_name) { |
|
1189 | - $extracted_fields_to_include[] = '*'; |
|
1190 | - } |
|
1191 | - } |
|
1192 | - } else { |
|
1193 | - //look for ones with no period |
|
1194 | - foreach ($includes as $field_to_include) { |
|
1195 | - $field_to_include = trim($field_to_include); |
|
1196 | - if ( |
|
1197 | - strpos($field_to_include, '.') === false |
|
1198 | - && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1199 | - ) { |
|
1200 | - $extracted_fields_to_include[] = $field_to_include; |
|
1201 | - } |
|
1202 | - } |
|
1203 | - } |
|
1204 | - return $extracted_fields_to_include; |
|
1205 | - } |
|
30 | + /** |
|
31 | + * @var Calculated_Model_Fields |
|
32 | + */ |
|
33 | + protected $_fields_calculator; |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * Read constructor. |
|
39 | + */ |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | + parent::__construct(); |
|
43 | + $this->_fields_calculator = new Calculated_Model_Fields(); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
50 | + * |
|
51 | + * @param \WP_REST_Request $request |
|
52 | + * @return \WP_REST_Response|\WP_Error |
|
53 | + */ |
|
54 | + public static function handle_request_get_all(\WP_REST_Request $request) |
|
55 | + { |
|
56 | + $controller = new Read(); |
|
57 | + try { |
|
58 | + $matches = $controller->parse_route( |
|
59 | + $request->get_route(), |
|
60 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
61 | + array('version', 'model') |
|
62 | + ); |
|
63 | + $controller->set_requested_version($matches['version']); |
|
64 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
65 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | + return $controller->send_response( |
|
67 | + new \WP_Error( |
|
68 | + 'endpoint_parsing_error', |
|
69 | + sprintf( |
|
70 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
71 | + 'event_espresso'), |
|
72 | + $model_name_singular |
|
73 | + ) |
|
74 | + ) |
|
75 | + ); |
|
76 | + } |
|
77 | + return $controller->send_response( |
|
78 | + $controller->get_entities_from_model( |
|
79 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
80 | + $request |
|
81 | + ) |
|
82 | + ); |
|
83 | + } catch (\Exception $e) { |
|
84 | + return $controller->send_response($e); |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Prepares and returns schema for any OPTIONS request. |
|
91 | + * |
|
92 | + * @param string $model_name Something like `Event` or `Registration` |
|
93 | + * @param string $version The API endpoint version being used. |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public static function handle_schema_request($model_name, $version) |
|
97 | + { |
|
98 | + $controller = new Read(); |
|
99 | + try { |
|
100 | + $controller->set_requested_version($version); |
|
101 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
102 | + return array(); |
|
103 | + } |
|
104 | + //get the model for this version |
|
105 | + $model = $controller->get_model_version_info()->load_model($model_name); |
|
106 | + $model_schema = new JsonModelSchema($model); |
|
107 | + return $model_schema->getModelSchemaForRelations( |
|
108 | + $controller->get_model_version_info()->relation_settings($model), |
|
109 | + $controller->_add_extra_fields_to_schema( |
|
110 | + $model, |
|
111 | + $model_schema->getModelSchemaForFields( |
|
112 | + $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
113 | + $model_schema->getInitialSchemaStructure() |
|
114 | + ) |
|
115 | + ) |
|
116 | + ); |
|
117 | + } catch (\Exception $e) { |
|
118 | + return array(); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * Adds additional fields to the schema |
|
125 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
126 | + * needs to be added to the schema. |
|
127 | + * |
|
128 | + * @param \EEM_Base $model |
|
129 | + * @param string $schema |
|
130 | + */ |
|
131 | + protected function _add_extra_fields_to_schema(\EEM_Base $model, $schema) |
|
132 | + { |
|
133 | + foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
134 | + if ($field instanceof EE_Datetime_Field) { |
|
135 | + $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
136 | + //modify the description |
|
137 | + $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
138 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
139 | + $field->get_nicename() |
|
140 | + ); |
|
141 | + } |
|
142 | + } |
|
143 | + return $schema; |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + protected function get_route_from_request() { |
|
154 | + if (isset($GLOBALS['wp']) |
|
155 | + && $GLOBALS['wp'] instanceof \WP |
|
156 | + && isset($GLOBALS['wp']->query_vars['rest_route'] ) |
|
157 | + ) { |
|
158 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
159 | + } else { |
|
160 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Gets a single entity related to the model indicated in the path and its id |
|
168 | + * |
|
169 | + * @param \WP_REST_Request $request |
|
170 | + * @return \WP_REST_Response|\WP_Error |
|
171 | + */ |
|
172 | + public static function handle_request_get_one(\WP_REST_Request $request) |
|
173 | + { |
|
174 | + $controller = new Read(); |
|
175 | + try { |
|
176 | + $matches = $controller->parse_route( |
|
177 | + $request->get_route(), |
|
178 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
179 | + array('version', 'model', 'id')); |
|
180 | + $controller->set_requested_version($matches['version']); |
|
181 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
182 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
183 | + return $controller->send_response( |
|
184 | + new \WP_Error( |
|
185 | + 'endpoint_parsing_error', |
|
186 | + sprintf( |
|
187 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
188 | + 'event_espresso'), |
|
189 | + $model_name_singular |
|
190 | + ) |
|
191 | + ) |
|
192 | + ); |
|
193 | + } |
|
194 | + return $controller->send_response( |
|
195 | + $controller->get_entity_from_model( |
|
196 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
197 | + $request |
|
198 | + ) |
|
199 | + ); |
|
200 | + } catch (\Exception $e) { |
|
201 | + return $controller->send_response($e); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
209 | + * to the item with the given id |
|
210 | + * |
|
211 | + * @param \WP_REST_Request $request |
|
212 | + * @return \WP_REST_Response|\WP_Error |
|
213 | + */ |
|
214 | + public static function handle_request_get_related(\WP_REST_Request $request) |
|
215 | + { |
|
216 | + $controller = new Read(); |
|
217 | + try { |
|
218 | + $matches = $controller->parse_route( |
|
219 | + $request->get_route(), |
|
220 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
221 | + array('version', 'model', 'id', 'related_model') |
|
222 | + ); |
|
223 | + $controller->set_requested_version($matches['version']); |
|
224 | + $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
225 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
226 | + return $controller->send_response( |
|
227 | + new \WP_Error( |
|
228 | + 'endpoint_parsing_error', |
|
229 | + sprintf( |
|
230 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
231 | + 'event_espresso'), |
|
232 | + $main_model_name_singular |
|
233 | + ) |
|
234 | + ) |
|
235 | + ); |
|
236 | + } |
|
237 | + $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
238 | + //assume the related model name is plural and try to find the model's name |
|
239 | + $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
240 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
241 | + //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
242 | + $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
243 | + } |
|
244 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
245 | + return $controller->send_response( |
|
246 | + new \WP_Error( |
|
247 | + 'endpoint_parsing_error', |
|
248 | + sprintf( |
|
249 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
250 | + 'event_espresso'), |
|
251 | + $related_model_name_singular |
|
252 | + ) |
|
253 | + ) |
|
254 | + ); |
|
255 | + } |
|
256 | + return $controller->send_response( |
|
257 | + $controller->get_entities_from_relation( |
|
258 | + $request->get_param('id'), |
|
259 | + $main_model->related_settings_for($related_model_name_singular), |
|
260 | + $request |
|
261 | + ) |
|
262 | + ); |
|
263 | + } catch (\Exception $e) { |
|
264 | + return $controller->send_response($e); |
|
265 | + } |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Gets a collection for the given model and filters |
|
272 | + * |
|
273 | + * @param \EEM_Base $model |
|
274 | + * @param \WP_REST_Request $request |
|
275 | + * @return array|\WP_Error |
|
276 | + */ |
|
277 | + public function get_entities_from_model($model, $request) |
|
278 | + { |
|
279 | + $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
280 | + if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
281 | + $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
282 | + return new \WP_Error( |
|
283 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
284 | + sprintf( |
|
285 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
286 | + $model_name_plural, |
|
287 | + Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
288 | + ), |
|
289 | + array('status' => 403) |
|
290 | + ); |
|
291 | + } |
|
292 | + if (! $request->get_header('no_rest_headers')) { |
|
293 | + $this->_set_headers_from_query_params($model, $query_params); |
|
294 | + } |
|
295 | + /** @type array $results */ |
|
296 | + $results = $model->get_all_wpdb_results($query_params); |
|
297 | + $nice_results = array(); |
|
298 | + foreach ($results as $result) { |
|
299 | + $nice_results[] = $this->create_entity_from_wpdb_result( |
|
300 | + $model, |
|
301 | + $result, |
|
302 | + $request |
|
303 | + ); |
|
304 | + } |
|
305 | + return $nice_results; |
|
306 | + } |
|
307 | + |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * @param array $primary_model_query_params query params for finding the item from which |
|
312 | + * relations will be based |
|
313 | + * @param \EE_Model_Relation_Base $relation |
|
314 | + * @param \WP_REST_Request $request |
|
315 | + * @return \WP_Error|array |
|
316 | + */ |
|
317 | + protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
318 | + { |
|
319 | + $context = $this->validate_context($request->get_param('caps')); |
|
320 | + $model = $relation->get_this_model(); |
|
321 | + $related_model = $relation->get_other_model(); |
|
322 | + if (! isset($primary_model_query_params[0])) { |
|
323 | + $primary_model_query_params[0] = array(); |
|
324 | + } |
|
325 | + //check if they can access the 1st model object |
|
326 | + $primary_model_query_params = array( |
|
327 | + 0 => $primary_model_query_params[0], |
|
328 | + 'limit' => 1, |
|
329 | + ); |
|
330 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
331 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
332 | + } |
|
333 | + $restricted_query_params = $primary_model_query_params; |
|
334 | + $restricted_query_params['caps'] = $context; |
|
335 | + $this->_set_debug_info('main model query params', $restricted_query_params); |
|
336 | + $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
337 | + if ( |
|
338 | + ! ( |
|
339 | + Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
340 | + && $model->exists($restricted_query_params) |
|
341 | + ) |
|
342 | + ) { |
|
343 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
344 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
345 | + } else { |
|
346 | + $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
347 | + } |
|
348 | + return new \WP_Error( |
|
349 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
350 | + sprintf( |
|
351 | + __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
352 | + 'event_espresso'), |
|
353 | + $related_model_name_maybe_plural, |
|
354 | + $relation->get_this_model()->get_this_model_name(), |
|
355 | + implode( |
|
356 | + ',', |
|
357 | + array_keys( |
|
358 | + Capabilities::get_missing_permissions($related_model, $context) |
|
359 | + ) |
|
360 | + ) |
|
361 | + ), |
|
362 | + array('status' => 403) |
|
363 | + ); |
|
364 | + } |
|
365 | + $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
366 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
367 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
368 | + . '.' |
|
369 | + . $where_condition_key] = $where_condition_value; |
|
370 | + } |
|
371 | + $query_params['default_where_conditions'] = 'none'; |
|
372 | + $query_params['caps'] = $context; |
|
373 | + if (! $request->get_header('no_rest_headers')) { |
|
374 | + $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
375 | + } |
|
376 | + /** @type array $results */ |
|
377 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
378 | + $nice_results = array(); |
|
379 | + foreach ($results as $result) { |
|
380 | + $nice_result = $this->create_entity_from_wpdb_result( |
|
381 | + $relation->get_other_model(), |
|
382 | + $result, |
|
383 | + $request |
|
384 | + ); |
|
385 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
386 | + //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
387 | + //if there are conflicts we prefer the properties from the main model |
|
388 | + $join_model_result = $this->create_entity_from_wpdb_result( |
|
389 | + $relation->get_join_model(), |
|
390 | + $result, |
|
391 | + $request |
|
392 | + ); |
|
393 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
394 | + //but keep the meta stuff from the main model |
|
395 | + if (isset($nice_result['meta'])) { |
|
396 | + $joined_result['meta'] = $nice_result['meta']; |
|
397 | + } |
|
398 | + $nice_result = $joined_result; |
|
399 | + } |
|
400 | + $nice_results[] = $nice_result; |
|
401 | + } |
|
402 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
403 | + return array_shift($nice_results); |
|
404 | + } else { |
|
405 | + return $nice_results; |
|
406 | + } |
|
407 | + } |
|
408 | + |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * Gets the collection for given relation object |
|
413 | + * The same as Read::get_entities_from_model(), except if the relation |
|
414 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
415 | + * the join-model-object into the results |
|
416 | + * |
|
417 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
418 | + * @param \EE_Model_Relation_Base $relation |
|
419 | + * @param \WP_REST_Request $request |
|
420 | + * @return array|\WP_Error |
|
421 | + * @throws \EE_Error |
|
422 | + */ |
|
423 | + public function get_entities_from_relation($id, $relation, $request) |
|
424 | + { |
|
425 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
426 | + throw new \EE_Error( |
|
427 | + sprintf( |
|
428 | + __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
429 | + 'event_espresso'), |
|
430 | + $relation->get_this_model()->get_this_model_name() |
|
431 | + ) |
|
432 | + ); |
|
433 | + } |
|
434 | + return $this->_get_entities_from_relation( |
|
435 | + array( |
|
436 | + array( |
|
437 | + $relation->get_this_model()->primary_key_name() => $id, |
|
438 | + ), |
|
439 | + ), |
|
440 | + $relation, |
|
441 | + $request |
|
442 | + ); |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * Sets the headers that are based on the model and query params, |
|
449 | + * like the total records. This should only be called on the original request |
|
450 | + * from the client, not on subsequent internal |
|
451 | + * |
|
452 | + * @param \EEM_Base $model |
|
453 | + * @param array $query_params |
|
454 | + * @return void |
|
455 | + */ |
|
456 | + protected function _set_headers_from_query_params($model, $query_params) |
|
457 | + { |
|
458 | + $this->_set_debug_info('model query params', $query_params); |
|
459 | + $this->_set_debug_info('missing caps', |
|
460 | + Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
461 | + //normally the limit to a 2-part array, where the 2nd item is the limit |
|
462 | + if (! isset($query_params['limit'])) { |
|
463 | + $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
464 | + } |
|
465 | + if (is_array($query_params['limit'])) { |
|
466 | + $limit_parts = $query_params['limit']; |
|
467 | + } else { |
|
468 | + $limit_parts = explode(',', $query_params['limit']); |
|
469 | + if (count($limit_parts) == 1) { |
|
470 | + $limit_parts = array(0, $limit_parts[0]); |
|
471 | + } |
|
472 | + } |
|
473 | + //remove the group by and having parts of the query, as those will |
|
474 | + //make the sql query return an array of values, instead of just a single value |
|
475 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
476 | + $count = $model->count($query_params, null, true); |
|
477 | + $pages = $count / $limit_parts[1]; |
|
478 | + $this->_set_response_header('Total', $count, false); |
|
479 | + $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
480 | + $this->_set_response_header('TotalPages', ceil($pages), false); |
|
481 | + } |
|
482 | + |
|
483 | + |
|
484 | + |
|
485 | + /** |
|
486 | + * Changes database results into REST API entities |
|
487 | + * |
|
488 | + * @param \EEM_Base $model |
|
489 | + * @param array $db_row like results from $wpdb->get_results() |
|
490 | + * @param \WP_REST_Request $rest_request |
|
491 | + * @param string $deprecated no longer used |
|
492 | + * @return array ready for being converted into json for sending to client |
|
493 | + */ |
|
494 | + public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
495 | + { |
|
496 | + if (! $rest_request instanceof \WP_REST_Request) { |
|
497 | + //ok so this was called in the old style, where the 3rd arg was |
|
498 | + //$include, and the 4th arg was $context |
|
499 | + //now setup the request just to avoid fatal errors, although we won't be able |
|
500 | + //to truly make use of it because it's kinda devoid of info |
|
501 | + $rest_request = new \WP_REST_Request(); |
|
502 | + $rest_request->set_param('include', $rest_request); |
|
503 | + $rest_request->set_param('caps', $deprecated); |
|
504 | + } |
|
505 | + if ($rest_request->get_param('caps') == null) { |
|
506 | + $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
507 | + } |
|
508 | + $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
509 | + $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
510 | + $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
511 | + $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
512 | + $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
513 | + $entity_array = apply_filters( |
|
514 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
515 | + $entity_array, |
|
516 | + $model, |
|
517 | + $rest_request->get_param('caps'), |
|
518 | + $rest_request, |
|
519 | + $this |
|
520 | + ); |
|
521 | + $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
522 | + $entity_array, |
|
523 | + $model, |
|
524 | + $rest_request->get_param('caps'), |
|
525 | + $this->get_model_version_info(), |
|
526 | + $model->get_index_primary_key_string( |
|
527 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
528 | + ) |
|
529 | + ); |
|
530 | + $this->_set_debug_info( |
|
531 | + 'inaccessible fields', |
|
532 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
533 | + ); |
|
534 | + return apply_filters( |
|
535 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
536 | + $result_without_inaccessible_fields, |
|
537 | + $model, |
|
538 | + $rest_request->get_param('caps') |
|
539 | + ); |
|
540 | + } |
|
541 | + |
|
542 | + |
|
543 | + |
|
544 | + /** |
|
545 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
546 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
547 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
548 | + * |
|
549 | + * @param \EEM_Base $model |
|
550 | + * @param array $db_row |
|
551 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
552 | + */ |
|
553 | + protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
554 | + { |
|
555 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
556 | + $result = array_intersect_key($result, |
|
557 | + $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
558 | + foreach ($result as $field_name => $raw_field_value) { |
|
559 | + $field_obj = $model->field_settings_for($field_name); |
|
560 | + $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
561 | + if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
562 | + unset($result[$field_name]); |
|
563 | + } elseif ( |
|
564 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
565 | + ) { |
|
566 | + $result[$field_name] = array( |
|
567 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
568 | + 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
569 | + ); |
|
570 | + } elseif ( |
|
571 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
572 | + ) { |
|
573 | + $result[$field_name] = array( |
|
574 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
575 | + 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
576 | + ); |
|
577 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
578 | + if ($field_value instanceof \DateTime) { |
|
579 | + $timezone = $field_value->getTimezone(); |
|
580 | + $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
581 | + $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
582 | + $field_obj, |
|
583 | + $field_value, |
|
584 | + $this->get_model_version_info()->requested_version() |
|
585 | + ); |
|
586 | + $field_value->setTimezone($timezone); |
|
587 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
588 | + $field_obj, |
|
589 | + $field_value, |
|
590 | + $this->get_model_version_info()->requested_version() |
|
591 | + ); |
|
592 | + } |
|
593 | + } else { |
|
594 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
595 | + $field_obj, |
|
596 | + $field_obj->prepare_for_get($field_value), |
|
597 | + $this->get_model_version_info()->requested_version() |
|
598 | + ); |
|
599 | + } |
|
600 | + } |
|
601 | + return $result; |
|
602 | + } |
|
603 | + |
|
604 | + |
|
605 | + |
|
606 | + /** |
|
607 | + * Adds a few extra fields to the entity response |
|
608 | + * |
|
609 | + * @param \EEM_Base $model |
|
610 | + * @param array $db_row |
|
611 | + * @param array $entity_array |
|
612 | + * @return array modified entity |
|
613 | + */ |
|
614 | + protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
615 | + { |
|
616 | + if ($model instanceof \EEM_CPT_Base) { |
|
617 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
618 | + } |
|
619 | + return $entity_array; |
|
620 | + } |
|
621 | + |
|
622 | + |
|
623 | + |
|
624 | + /** |
|
625 | + * Gets links we want to add to the response |
|
626 | + * |
|
627 | + * @global \WP_REST_Server $wp_rest_server |
|
628 | + * @param \EEM_Base $model |
|
629 | + * @param array $db_row |
|
630 | + * @param array $entity_array |
|
631 | + * @return array the _links item in the entity |
|
632 | + */ |
|
633 | + protected function _get_entity_links($model, $db_row, $entity_array) |
|
634 | + { |
|
635 | + //add basic links |
|
636 | + $links = array(); |
|
637 | + if ($model->has_primary_key_field()) { |
|
638 | + $links['self'] = array( |
|
639 | + array( |
|
640 | + 'href' => $this->get_versioned_link_to( |
|
641 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
642 | + . '/' |
|
643 | + . $entity_array[$model->primary_key_name()] |
|
644 | + ), |
|
645 | + ), |
|
646 | + ); |
|
647 | + } |
|
648 | + $links['collection'] = array( |
|
649 | + array( |
|
650 | + 'href' => $this->get_versioned_link_to( |
|
651 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
652 | + ), |
|
653 | + ), |
|
654 | + ); |
|
655 | + //add links to related models |
|
656 | + if ($model->has_primary_key_field()) { |
|
657 | + foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
658 | + $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
659 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
660 | + array( |
|
661 | + 'href' => $this->get_versioned_link_to( |
|
662 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
663 | + . '/' |
|
664 | + . $entity_array[$model->primary_key_name()] |
|
665 | + . '/' |
|
666 | + . $related_model_part |
|
667 | + ), |
|
668 | + 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
669 | + ), |
|
670 | + ); |
|
671 | + } |
|
672 | + } |
|
673 | + return $links; |
|
674 | + } |
|
675 | + |
|
676 | + |
|
677 | + |
|
678 | + /** |
|
679 | + * Adds the included models indicated in the request to the entity provided |
|
680 | + * |
|
681 | + * @param \EEM_Base $model |
|
682 | + * @param \WP_REST_Request $rest_request |
|
683 | + * @param array $entity_array |
|
684 | + * @param array $db_row |
|
685 | + * @return array the modified entity |
|
686 | + */ |
|
687 | + protected function _include_requested_models( |
|
688 | + \EEM_Base $model, |
|
689 | + \WP_REST_Request $rest_request, |
|
690 | + $entity_array, |
|
691 | + $db_row = array() |
|
692 | + ) { |
|
693 | + //if $db_row not included, hope the entity array has what we need |
|
694 | + if (! $db_row) { |
|
695 | + $db_row = $entity_array; |
|
696 | + } |
|
697 | + $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
698 | + $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
699 | + //if they passed in * or didn't specify any includes, return everything |
|
700 | + if (! in_array('*', $includes_for_this_model) |
|
701 | + && ! empty($includes_for_this_model) |
|
702 | + ) { |
|
703 | + if ($model->has_primary_key_field()) { |
|
704 | + //always include the primary key. ya just gotta know that at least |
|
705 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
706 | + } |
|
707 | + if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
708 | + $includes_for_this_model[] = '_calculated_fields'; |
|
709 | + } |
|
710 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
711 | + } |
|
712 | + $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
713 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
714 | + $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
715 | + $rest_request->get_param('include'), |
|
716 | + $relation_name |
|
717 | + ); |
|
718 | + $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
719 | + $rest_request->get_param('calculate'), |
|
720 | + $relation_name |
|
721 | + ); |
|
722 | + //did they specify they wanted to include a related model, or |
|
723 | + //specific fields from a related model? |
|
724 | + //or did they specify to calculate a field from a related model? |
|
725 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
726 | + //if so, we should include at least some part of the related model |
|
727 | + $pretend_related_request = new \WP_REST_Request(); |
|
728 | + $pretend_related_request->set_query_params( |
|
729 | + array( |
|
730 | + 'caps' => $rest_request->get_param('caps'), |
|
731 | + 'include' => $related_fields_to_include, |
|
732 | + 'calculate' => $related_fields_to_calculate, |
|
733 | + ) |
|
734 | + ); |
|
735 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
736 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
737 | + $model->get_index_primary_key_string( |
|
738 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
739 | + ) |
|
740 | + ); |
|
741 | + $related_results = $this->_get_entities_from_relation( |
|
742 | + $primary_model_query_params, |
|
743 | + $relation_obj, |
|
744 | + $pretend_related_request |
|
745 | + ); |
|
746 | + $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
747 | + instanceof |
|
748 | + \WP_Error |
|
749 | + ? null |
|
750 | + : $related_results; |
|
751 | + } |
|
752 | + } |
|
753 | + return $entity_array; |
|
754 | + } |
|
755 | + |
|
756 | + |
|
757 | + |
|
758 | + /** |
|
759 | + * Returns a new array with all the names of models removed. Eg |
|
760 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
761 | + * |
|
762 | + * @param array $arr |
|
763 | + * @return array |
|
764 | + */ |
|
765 | + private function _remove_model_names_from_array($arr) |
|
766 | + { |
|
767 | + return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
768 | + } |
|
769 | + |
|
770 | + |
|
771 | + |
|
772 | + /** |
|
773 | + * Gets the calculated fields for the response |
|
774 | + * |
|
775 | + * @param \EEM_Base $model |
|
776 | + * @param array $wpdb_row |
|
777 | + * @param \WP_REST_Request $rest_request |
|
778 | + * @return \stdClass the _calculations item in the entity |
|
779 | + */ |
|
780 | + protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
781 | + { |
|
782 | + $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
783 | + $rest_request->get_param('calculate'), |
|
784 | + '' |
|
785 | + ); |
|
786 | + //note: setting calculate=* doesn't do anything |
|
787 | + $calculated_fields_to_return = new \stdClass(); |
|
788 | + foreach ($calculated_fields as $field_to_calculate) { |
|
789 | + try { |
|
790 | + $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
791 | + null, |
|
792 | + $this->_fields_calculator->retrieve_calculated_field_value( |
|
793 | + $model, |
|
794 | + $field_to_calculate, |
|
795 | + $wpdb_row, |
|
796 | + $rest_request, |
|
797 | + $this |
|
798 | + ), |
|
799 | + $this->get_model_version_info()->requested_version() |
|
800 | + ); |
|
801 | + } catch (Rest_Exception $e) { |
|
802 | + //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
803 | + $this->_set_response_header( |
|
804 | + 'Notices-Field-Calculation-Errors[' |
|
805 | + . $e->get_string_code() |
|
806 | + . '][' |
|
807 | + . $model->get_this_model_name() |
|
808 | + . '][' |
|
809 | + . $field_to_calculate |
|
810 | + . ']', |
|
811 | + $e->getMessage(), |
|
812 | + true |
|
813 | + ); |
|
814 | + } |
|
815 | + } |
|
816 | + return $calculated_fields_to_return; |
|
817 | + } |
|
818 | + |
|
819 | + |
|
820 | + |
|
821 | + /** |
|
822 | + * Gets the full URL to the resource, taking the requested version into account |
|
823 | + * |
|
824 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
825 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
826 | + */ |
|
827 | + public function get_versioned_link_to($link_part_after_version_and_slash) |
|
828 | + { |
|
829 | + return rest_url( |
|
830 | + \EED_Core_Rest_Api::ee_api_namespace |
|
831 | + . $this->get_model_version_info()->requested_version() |
|
832 | + . '/' |
|
833 | + . $link_part_after_version_and_slash |
|
834 | + ); |
|
835 | + } |
|
836 | + |
|
837 | + |
|
838 | + |
|
839 | + /** |
|
840 | + * Gets the correct lowercase name for the relation in the API according |
|
841 | + * to the relation's type |
|
842 | + * |
|
843 | + * @param string $relation_name |
|
844 | + * @param \EE_Model_Relation_Base $relation_obj |
|
845 | + * @return string |
|
846 | + */ |
|
847 | + public static function get_related_entity_name($relation_name, $relation_obj) |
|
848 | + { |
|
849 | + if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
850 | + return strtolower($relation_name); |
|
851 | + } else { |
|
852 | + return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
853 | + } |
|
854 | + } |
|
855 | + |
|
856 | + |
|
857 | + |
|
858 | + /** |
|
859 | + * Gets the one model object with the specified id for the specified model |
|
860 | + * |
|
861 | + * @param \EEM_Base $model |
|
862 | + * @param \WP_REST_Request $request |
|
863 | + * @return array|\WP_Error |
|
864 | + */ |
|
865 | + public function get_entity_from_model($model, $request) |
|
866 | + { |
|
867 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
868 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
869 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
870 | + } |
|
871 | + $restricted_query_params = $query_params; |
|
872 | + $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
873 | + $this->_set_debug_info('model query params', $restricted_query_params); |
|
874 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
875 | + if (! empty ($model_rows)) { |
|
876 | + return $this->create_entity_from_wpdb_result( |
|
877 | + $model, |
|
878 | + array_shift($model_rows), |
|
879 | + $request); |
|
880 | + } else { |
|
881 | + //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
882 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
883 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
884 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
885 | + //you got shafted- it existed but we didn't want to tell you! |
|
886 | + return new \WP_Error( |
|
887 | + 'rest_user_cannot_read', |
|
888 | + sprintf( |
|
889 | + __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
890 | + strtolower($model->get_this_model_name()), |
|
891 | + Capabilities::get_missing_permissions_string( |
|
892 | + $model, |
|
893 | + $this->validate_context($request->get_param('caps'))) |
|
894 | + ), |
|
895 | + array('status' => 403) |
|
896 | + ); |
|
897 | + } else { |
|
898 | + //it's not you. It just doesn't exist |
|
899 | + return new \WP_Error( |
|
900 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
901 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
902 | + array('status' => 404) |
|
903 | + ); |
|
904 | + } |
|
905 | + } |
|
906 | + } |
|
907 | + |
|
908 | + |
|
909 | + |
|
910 | + /** |
|
911 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
912 | + * version so just treat it as a default read |
|
913 | + * |
|
914 | + * @param string $context |
|
915 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
916 | + */ |
|
917 | + public function validate_context($context) |
|
918 | + { |
|
919 | + if (! $context) { |
|
920 | + $context = \EEM_Base::caps_read; |
|
921 | + } |
|
922 | + $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
923 | + if (in_array($context, $valid_contexts)) { |
|
924 | + return $context; |
|
925 | + } else { |
|
926 | + return \EEM_Base::caps_read; |
|
927 | + } |
|
928 | + } |
|
929 | + |
|
930 | + |
|
931 | + |
|
932 | + /** |
|
933 | + * Verifies the passed in value is an allowable default where conditions value. |
|
934 | + * |
|
935 | + * @param $default_query_params |
|
936 | + * @return string |
|
937 | + */ |
|
938 | + public function validate_default_query_params($default_query_params) |
|
939 | + { |
|
940 | + $valid_default_where_conditions_for_api_calls = array( |
|
941 | + \EEM_Base::default_where_conditions_all, |
|
942 | + \EEM_Base::default_where_conditions_minimum_all, |
|
943 | + \EEM_Base::default_where_conditions_minimum_others, |
|
944 | + ); |
|
945 | + if (! $default_query_params) { |
|
946 | + $default_query_params = \EEM_Base::default_where_conditions_all; |
|
947 | + } |
|
948 | + if ( |
|
949 | + in_array( |
|
950 | + $default_query_params, |
|
951 | + $valid_default_where_conditions_for_api_calls, |
|
952 | + true |
|
953 | + ) |
|
954 | + ) { |
|
955 | + return $default_query_params; |
|
956 | + } else { |
|
957 | + return \EEM_Base::default_where_conditions_all; |
|
958 | + } |
|
959 | + } |
|
960 | + |
|
961 | + |
|
962 | + |
|
963 | + /** |
|
964 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
965 | + * Note: right now the query parameter keys for fields (and related fields) |
|
966 | + * can be left as-is, but it's quite possible this will change someday. |
|
967 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
968 | + * |
|
969 | + * @param \EEM_Base $model |
|
970 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
971 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
972 | + * that absolutely no results should be returned |
|
973 | + * @throws \EE_Error |
|
974 | + */ |
|
975 | + public function create_model_query_params($model, $query_parameters) |
|
976 | + { |
|
977 | + $model_query_params = array(); |
|
978 | + if (isset($query_parameters['where'])) { |
|
979 | + $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
980 | + $query_parameters['where'], |
|
981 | + $model, |
|
982 | + $this->get_model_version_info()->requested_version() |
|
983 | + ); |
|
984 | + } |
|
985 | + if (isset($query_parameters['order_by'])) { |
|
986 | + $order_by = $query_parameters['order_by']; |
|
987 | + } elseif (isset($query_parameters['orderby'])) { |
|
988 | + $order_by = $query_parameters['orderby']; |
|
989 | + } else { |
|
990 | + $order_by = null; |
|
991 | + } |
|
992 | + if ($order_by !== null) { |
|
993 | + if (is_array($order_by)) { |
|
994 | + $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
995 | + } else { |
|
996 | + //it's a single item |
|
997 | + $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
998 | + } |
|
999 | + $model_query_params['order_by'] = $order_by; |
|
1000 | + } |
|
1001 | + if (isset($query_parameters['group_by'])) { |
|
1002 | + $group_by = $query_parameters['group_by']; |
|
1003 | + } elseif (isset($query_parameters['groupby'])) { |
|
1004 | + $group_by = $query_parameters['groupby']; |
|
1005 | + } else { |
|
1006 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1007 | + } |
|
1008 | + //make sure they're all real names |
|
1009 | + if (is_array($group_by)) { |
|
1010 | + $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
1011 | + } |
|
1012 | + if ($group_by !== null) { |
|
1013 | + $model_query_params['group_by'] = $group_by; |
|
1014 | + } |
|
1015 | + if (isset($query_parameters['having'])) { |
|
1016 | + $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1017 | + $query_parameters['having'], |
|
1018 | + $model, |
|
1019 | + $this->get_model_version_info()->requested_version() |
|
1020 | + ); |
|
1021 | + } |
|
1022 | + if (isset($query_parameters['order'])) { |
|
1023 | + $model_query_params['order'] = $query_parameters['order']; |
|
1024 | + } |
|
1025 | + if (isset($query_parameters['mine'])) { |
|
1026 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1027 | + } |
|
1028 | + if (isset($query_parameters['limit'])) { |
|
1029 | + //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1030 | + if (! is_array($query_parameters['limit'])) { |
|
1031 | + $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1032 | + } else { |
|
1033 | + $limit_array = $query_parameters['limit']; |
|
1034 | + } |
|
1035 | + $sanitized_limit = array(); |
|
1036 | + foreach ($limit_array as $key => $limit_part) { |
|
1037 | + if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1038 | + throw new \EE_Error( |
|
1039 | + sprintf( |
|
1040 | + __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1041 | + 'event_espresso'), |
|
1042 | + wp_json_encode($query_parameters['limit']) |
|
1043 | + ) |
|
1044 | + ); |
|
1045 | + } |
|
1046 | + $sanitized_limit[] = (int)$limit_part; |
|
1047 | + } |
|
1048 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1049 | + } else { |
|
1050 | + $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
1051 | + } |
|
1052 | + if (isset($query_parameters['caps'])) { |
|
1053 | + $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
1054 | + } else { |
|
1055 | + $model_query_params['caps'] = \EEM_Base::caps_read; |
|
1056 | + } |
|
1057 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1058 | + $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
1059 | + } |
|
1060 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1061 | + } |
|
1062 | + |
|
1063 | + |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * Changes the REST-style query params for use in the models |
|
1067 | + * |
|
1068 | + * @deprecated |
|
1069 | + * @param \EEM_Base $model |
|
1070 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1071 | + * @return array |
|
1072 | + */ |
|
1073 | + public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
1074 | + { |
|
1075 | + $model_ready_query_params = array(); |
|
1076 | + foreach ($query_params as $key => $value) { |
|
1077 | + if (is_array($value)) { |
|
1078 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1079 | + } else { |
|
1080 | + $model_ready_query_params[$key] = $value; |
|
1081 | + } |
|
1082 | + } |
|
1083 | + return $model_ready_query_params; |
|
1084 | + } |
|
1085 | + |
|
1086 | + |
|
1087 | + |
|
1088 | + /** |
|
1089 | + * @deprecated |
|
1090 | + * @param $model |
|
1091 | + * @param $query_params |
|
1092 | + * @return array |
|
1093 | + */ |
|
1094 | + public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1095 | + { |
|
1096 | + $model_ready_query_params = array(); |
|
1097 | + foreach ($query_params as $key => $value) { |
|
1098 | + if (is_array($value)) { |
|
1099 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1100 | + } else { |
|
1101 | + $model_ready_query_params[$key] = $value; |
|
1102 | + } |
|
1103 | + } |
|
1104 | + return $model_ready_query_params; |
|
1105 | + } |
|
1106 | + |
|
1107 | + |
|
1108 | + |
|
1109 | + /** |
|
1110 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1111 | + * If no prefix is specified, returns items with no period. |
|
1112 | + * |
|
1113 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1114 | + * @param string $prefix "Event" or "foobar" |
|
1115 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1116 | + * we only return strings starting with that and a period; if no prefix was |
|
1117 | + * specified we return all items containing NO periods |
|
1118 | + */ |
|
1119 | + public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1120 | + { |
|
1121 | + if (is_string($string_to_explode)) { |
|
1122 | + $exploded_contents = explode(',', $string_to_explode); |
|
1123 | + } else if (is_array($string_to_explode)) { |
|
1124 | + $exploded_contents = $string_to_explode; |
|
1125 | + } else { |
|
1126 | + $exploded_contents = array(); |
|
1127 | + } |
|
1128 | + //if the string was empty, we want an empty array |
|
1129 | + $exploded_contents = array_filter($exploded_contents); |
|
1130 | + $contents_with_prefix = array(); |
|
1131 | + foreach ($exploded_contents as $item) { |
|
1132 | + $item = trim($item); |
|
1133 | + //if no prefix was provided, so we look for items with no "." in them |
|
1134 | + if (! $prefix) { |
|
1135 | + //does this item have a period? |
|
1136 | + if (strpos($item, '.') === false) { |
|
1137 | + //if not, then its what we're looking for |
|
1138 | + $contents_with_prefix[] = $item; |
|
1139 | + } |
|
1140 | + } else if (strpos($item, $prefix . '.') === 0) { |
|
1141 | + //this item has the prefix and a period, grab it |
|
1142 | + $contents_with_prefix[] = substr( |
|
1143 | + $item, |
|
1144 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1145 | + ); |
|
1146 | + } else if ($item === $prefix) { |
|
1147 | + //this item is JUST the prefix |
|
1148 | + //so let's grab everything after, which is a blank string |
|
1149 | + $contents_with_prefix[] = ''; |
|
1150 | + } |
|
1151 | + } |
|
1152 | + return $contents_with_prefix; |
|
1153 | + } |
|
1154 | + |
|
1155 | + |
|
1156 | + |
|
1157 | + /** |
|
1158 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1159 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1160 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1161 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1162 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1163 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1164 | + * name and then a period). |
|
1165 | + * @param string $include_string @see Read:handle_request_get_all |
|
1166 | + * @param string $model_name |
|
1167 | + * @return array of fields for this model. If $model_name is provided, then |
|
1168 | + * the fields for that model, with the model's name removed from each. |
|
1169 | + * If $include_string was blank or '*' returns an empty array |
|
1170 | + */ |
|
1171 | + public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1172 | + { |
|
1173 | + if (is_array($include_string)) { |
|
1174 | + $include_string = implode(',', $include_string); |
|
1175 | + } |
|
1176 | + if ($include_string === '*' || $include_string === '') { |
|
1177 | + return array(); |
|
1178 | + } |
|
1179 | + $includes = explode(',', $include_string); |
|
1180 | + $extracted_fields_to_include = array(); |
|
1181 | + if ($model_name) { |
|
1182 | + foreach ($includes as $field_to_include) { |
|
1183 | + $field_to_include = trim($field_to_include); |
|
1184 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1185 | + //found the model name at the exact start |
|
1186 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1187 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1188 | + } elseif ($field_to_include == $model_name) { |
|
1189 | + $extracted_fields_to_include[] = '*'; |
|
1190 | + } |
|
1191 | + } |
|
1192 | + } else { |
|
1193 | + //look for ones with no period |
|
1194 | + foreach ($includes as $field_to_include) { |
|
1195 | + $field_to_include = trim($field_to_include); |
|
1196 | + if ( |
|
1197 | + strpos($field_to_include, '.') === false |
|
1198 | + && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1199 | + ) { |
|
1200 | + $extracted_fields_to_include[] = $field_to_include; |
|
1201 | + } |
|
1202 | + } |
|
1203 | + } |
|
1204 | + return $extracted_fields_to_include; |
|
1205 | + } |
|
1206 | 1206 | } |
1207 | 1207 | |
1208 | 1208 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | use EventEspresso\core\entities\models\JsonModelSchema; |
9 | 9 | use EE_Datetime_Field; |
10 | 10 | |
11 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
11 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
12 | 12 | exit('No direct script access allowed'); |
13 | 13 | } |
14 | 14 | |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | try { |
58 | 58 | $matches = $controller->parse_route( |
59 | 59 | $request->get_route(), |
60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
60 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)~', |
|
61 | 61 | array('version', 'model') |
62 | 62 | ); |
63 | 63 | $controller->set_requested_version($matches['version']); |
64 | 64 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
65 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | 66 | return $controller->send_response( |
67 | 67 | new \WP_Error( |
68 | 68 | 'endpoint_parsing_error', |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | $controller = new Read(); |
99 | 99 | try { |
100 | 100 | $controller->set_requested_version($version); |
101 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
101 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
102 | 102 | return array(); |
103 | 103 | } |
104 | 104 | //get the model for this version |
@@ -132,9 +132,9 @@ discard block |
||
132 | 132 | { |
133 | 133 | foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
134 | 134 | if ($field instanceof EE_Datetime_Field) { |
135 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
135 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
136 | 136 | //modify the description |
137 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
137 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
138 | 138 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
139 | 139 | $field->get_nicename() |
140 | 140 | ); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | protected function get_route_from_request() { |
154 | 154 | if (isset($GLOBALS['wp']) |
155 | 155 | && $GLOBALS['wp'] instanceof \WP |
156 | - && isset($GLOBALS['wp']->query_vars['rest_route'] ) |
|
156 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
157 | 157 | ) { |
158 | 158 | return $GLOBALS['wp']->query_vars['rest_route']; |
159 | 159 | } else { |
@@ -175,11 +175,11 @@ discard block |
||
175 | 175 | try { |
176 | 176 | $matches = $controller->parse_route( |
177 | 177 | $request->get_route(), |
178 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
178 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)~', |
|
179 | 179 | array('version', 'model', 'id')); |
180 | 180 | $controller->set_requested_version($matches['version']); |
181 | 181 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
182 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
182 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
183 | 183 | return $controller->send_response( |
184 | 184 | new \WP_Error( |
185 | 185 | 'endpoint_parsing_error', |
@@ -217,12 +217,12 @@ discard block |
||
217 | 217 | try { |
218 | 218 | $matches = $controller->parse_route( |
219 | 219 | $request->get_route(), |
220 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
220 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)/(.*)~', |
|
221 | 221 | array('version', 'model', 'id', 'related_model') |
222 | 222 | ); |
223 | 223 | $controller->set_requested_version($matches['version']); |
224 | 224 | $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
225 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
225 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
226 | 226 | return $controller->send_response( |
227 | 227 | new \WP_Error( |
228 | 228 | 'endpoint_parsing_error', |
@@ -237,11 +237,11 @@ discard block |
||
237 | 237 | $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
238 | 238 | //assume the related model name is plural and try to find the model's name |
239 | 239 | $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
240 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
240 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
241 | 241 | //so the word didn't singularize well. Maybe that's just because it's a singular word? |
242 | 242 | $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
243 | 243 | } |
244 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
244 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
245 | 245 | return $controller->send_response( |
246 | 246 | new \WP_Error( |
247 | 247 | 'endpoint_parsing_error', |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | public function get_entities_from_model($model, $request) |
278 | 278 | { |
279 | 279 | $query_params = $this->create_model_query_params($model, $request->get_params()); |
280 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
280 | + if ( ! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
281 | 281 | $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
282 | 282 | return new \WP_Error( |
283 | 283 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | array('status' => 403) |
290 | 290 | ); |
291 | 291 | } |
292 | - if (! $request->get_header('no_rest_headers')) { |
|
292 | + if ( ! $request->get_header('no_rest_headers')) { |
|
293 | 293 | $this->_set_headers_from_query_params($model, $query_params); |
294 | 294 | } |
295 | 295 | /** @type array $results */ |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | $context = $this->validate_context($request->get_param('caps')); |
320 | 320 | $model = $relation->get_this_model(); |
321 | 321 | $related_model = $relation->get_other_model(); |
322 | - if (! isset($primary_model_query_params[0])) { |
|
322 | + if ( ! isset($primary_model_query_params[0])) { |
|
323 | 323 | $primary_model_query_params[0] = array(); |
324 | 324 | } |
325 | 325 | //check if they can access the 1st model object |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | } |
371 | 371 | $query_params['default_where_conditions'] = 'none'; |
372 | 372 | $query_params['caps'] = $context; |
373 | - if (! $request->get_header('no_rest_headers')) { |
|
373 | + if ( ! $request->get_header('no_rest_headers')) { |
|
374 | 374 | $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
375 | 375 | } |
376 | 376 | /** @type array $results */ |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | */ |
423 | 423 | public function get_entities_from_relation($id, $relation, $request) |
424 | 424 | { |
425 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
425 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
426 | 426 | throw new \EE_Error( |
427 | 427 | sprintf( |
428 | 428 | __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | $this->_set_debug_info('missing caps', |
460 | 460 | Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
461 | 461 | //normally the limit to a 2-part array, where the 2nd item is the limit |
462 | - if (! isset($query_params['limit'])) { |
|
462 | + if ( ! isset($query_params['limit'])) { |
|
463 | 463 | $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
464 | 464 | } |
465 | 465 | if (is_array($query_params['limit'])) { |
@@ -493,7 +493,7 @@ discard block |
||
493 | 493 | */ |
494 | 494 | public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
495 | 495 | { |
496 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
496 | + if ( ! $rest_request instanceof \WP_REST_Request) { |
|
497 | 497 | //ok so this was called in the old style, where the 3rd arg was |
498 | 498 | //$include, and the 4th arg was $context |
499 | 499 | //now setup the request just to avoid fatal errors, although we won't be able |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | if ($field_value instanceof \DateTime) { |
579 | 579 | $timezone = $field_value->getTimezone(); |
580 | 580 | $field_value->setTimezone(new \DateTimeZone('UTC')); |
581 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
581 | + $result[$field_name.'_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
582 | 582 | $field_obj, |
583 | 583 | $field_value, |
584 | 584 | $this->get_model_version_info()->requested_version() |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | if ($model->has_primary_key_field()) { |
657 | 657 | foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
658 | 658 | $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
659 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
659 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
660 | 660 | array( |
661 | 661 | 'href' => $this->get_versioned_link_to( |
662 | 662 | \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
@@ -691,13 +691,13 @@ discard block |
||
691 | 691 | $db_row = array() |
692 | 692 | ) { |
693 | 693 | //if $db_row not included, hope the entity array has what we need |
694 | - if (! $db_row) { |
|
694 | + if ( ! $db_row) { |
|
695 | 695 | $db_row = $entity_array; |
696 | 696 | } |
697 | 697 | $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
698 | 698 | $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
699 | 699 | //if they passed in * or didn't specify any includes, return everything |
700 | - if (! in_array('*', $includes_for_this_model) |
|
700 | + if ( ! in_array('*', $includes_for_this_model) |
|
701 | 701 | && ! empty($includes_for_this_model) |
702 | 702 | ) { |
703 | 703 | if ($model->has_primary_key_field()) { |
@@ -872,7 +872,7 @@ discard block |
||
872 | 872 | $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
873 | 873 | $this->_set_debug_info('model query params', $restricted_query_params); |
874 | 874 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
875 | - if (! empty ($model_rows)) { |
|
875 | + if ( ! empty ($model_rows)) { |
|
876 | 876 | return $this->create_entity_from_wpdb_result( |
877 | 877 | $model, |
878 | 878 | array_shift($model_rows), |
@@ -881,7 +881,7 @@ discard block |
||
881 | 881 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
882 | 882 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
883 | 883 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
884 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
884 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
885 | 885 | //you got shafted- it existed but we didn't want to tell you! |
886 | 886 | return new \WP_Error( |
887 | 887 | 'rest_user_cannot_read', |
@@ -916,7 +916,7 @@ discard block |
||
916 | 916 | */ |
917 | 917 | public function validate_context($context) |
918 | 918 | { |
919 | - if (! $context) { |
|
919 | + if ( ! $context) { |
|
920 | 920 | $context = \EEM_Base::caps_read; |
921 | 921 | } |
922 | 922 | $valid_contexts = \EEM_Base::valid_cap_contexts(); |
@@ -942,7 +942,7 @@ discard block |
||
942 | 942 | \EEM_Base::default_where_conditions_minimum_all, |
943 | 943 | \EEM_Base::default_where_conditions_minimum_others, |
944 | 944 | ); |
945 | - if (! $default_query_params) { |
|
945 | + if ( ! $default_query_params) { |
|
946 | 946 | $default_query_params = \EEM_Base::default_where_conditions_all; |
947 | 947 | } |
948 | 948 | if ( |
@@ -1027,14 +1027,14 @@ discard block |
||
1027 | 1027 | } |
1028 | 1028 | if (isset($query_parameters['limit'])) { |
1029 | 1029 | //limit should be either a string like '23' or '23,43', or an array with two items in it |
1030 | - if (! is_array($query_parameters['limit'])) { |
|
1031 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1030 | + if ( ! is_array($query_parameters['limit'])) { |
|
1031 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1032 | 1032 | } else { |
1033 | 1033 | $limit_array = $query_parameters['limit']; |
1034 | 1034 | } |
1035 | 1035 | $sanitized_limit = array(); |
1036 | 1036 | foreach ($limit_array as $key => $limit_part) { |
1037 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1037 | + if ($this->_debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1038 | 1038 | throw new \EE_Error( |
1039 | 1039 | sprintf( |
1040 | 1040 | __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
@@ -1043,7 +1043,7 @@ discard block |
||
1043 | 1043 | ) |
1044 | 1044 | ); |
1045 | 1045 | } |
1046 | - $sanitized_limit[] = (int)$limit_part; |
|
1046 | + $sanitized_limit[] = (int) $limit_part; |
|
1047 | 1047 | } |
1048 | 1048 | $model_query_params['limit'] = implode(',', $sanitized_limit); |
1049 | 1049 | } else { |
@@ -1131,17 +1131,17 @@ discard block |
||
1131 | 1131 | foreach ($exploded_contents as $item) { |
1132 | 1132 | $item = trim($item); |
1133 | 1133 | //if no prefix was provided, so we look for items with no "." in them |
1134 | - if (! $prefix) { |
|
1134 | + if ( ! $prefix) { |
|
1135 | 1135 | //does this item have a period? |
1136 | 1136 | if (strpos($item, '.') === false) { |
1137 | 1137 | //if not, then its what we're looking for |
1138 | 1138 | $contents_with_prefix[] = $item; |
1139 | 1139 | } |
1140 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1140 | + } else if (strpos($item, $prefix.'.') === 0) { |
|
1141 | 1141 | //this item has the prefix and a period, grab it |
1142 | 1142 | $contents_with_prefix[] = substr( |
1143 | 1143 | $item, |
1144 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1144 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1145 | 1145 | ); |
1146 | 1146 | } else if ($item === $prefix) { |
1147 | 1147 | //this item is JUST the prefix |
@@ -1181,9 +1181,9 @@ discard block |
||
1181 | 1181 | if ($model_name) { |
1182 | 1182 | foreach ($includes as $field_to_include) { |
1183 | 1183 | $field_to_include = trim($field_to_include); |
1184 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1184 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1185 | 1185 | //found the model name at the exact start |
1186 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1186 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1187 | 1187 | $extracted_fields_to_include[] = $field_sans_model_name; |
1188 | 1188 | } elseif ($field_to_include == $model_name) { |
1189 | 1189 | $extracted_fields_to_include[] = '*'; |
@@ -33,11 +33,11 @@ discard block |
||
33 | 33 | public static function set_hooks() { |
34 | 34 | // create download buttons |
35 | 35 | add_filter( |
36 | - 'FHEE__espresso_list_of_event_dates__datetime_html', |
|
37 | - array( 'EED_Ical', 'generate_add_to_iCal_button' ), |
|
38 | - 10, |
|
39 | - 2 |
|
40 | - ); |
|
36 | + 'FHEE__espresso_list_of_event_dates__datetime_html', |
|
37 | + array( 'EED_Ical', 'generate_add_to_iCal_button' ), |
|
38 | + 10, |
|
39 | + 2 |
|
40 | + ); |
|
41 | 41 | // process ics download request |
42 | 42 | EE_Config::register_route( 'download_ics_file', 'EED_Ical', 'download_ics_file' ); |
43 | 43 | } |
@@ -66,15 +66,15 @@ discard block |
||
66 | 66 | |
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * generate_add_to_iCal_button |
|
71 | - * |
|
72 | - * @access public |
|
73 | - * @param $html |
|
74 | - * @param $datetime |
|
75 | - * @return string |
|
76 | - * @throws \EE_Error |
|
77 | - */ |
|
69 | + /** |
|
70 | + * generate_add_to_iCal_button |
|
71 | + * |
|
72 | + * @access public |
|
73 | + * @param $html |
|
74 | + * @param $datetime |
|
75 | + * @return string |
|
76 | + * @throws \EE_Error |
|
77 | + */ |
|
78 | 78 | public static function generate_add_to_iCal_button( $html, $datetime ) { |
79 | 79 | // first verify a proper datetime object has been received |
80 | 80 | if ( $datetime instanceof EE_Datetime ) { |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | $html .= '</form>'; |
94 | 94 | break; |
95 | 95 | // buttons are just links that have been styled to appear as buttons, |
96 | - // but may not be blend with a theme as well as submit buttons |
|
96 | + // but may not be blend with a theme as well as submit buttons |
|
97 | 97 | case 'button' : |
98 | 98 | $html .= '<a class="ee-ical-btn small ee-button ee-roundish" href="' . $URL; |
99 | 99 | $html .= '" title="' . __( 'Add to iCal Calendar', 'event_espresso' ) . '">'; |
@@ -114,13 +114,13 @@ discard block |
||
114 | 114 | |
115 | 115 | |
116 | 116 | |
117 | - /** |
|
118 | - * download_ics_file |
|
119 | - * |
|
120 | - * @access public |
|
121 | - * @return void |
|
122 | - * @throws \EE_Error |
|
123 | - */ |
|
117 | + /** |
|
118 | + * download_ics_file |
|
119 | + * |
|
120 | + * @access public |
|
121 | + * @return void |
|
122 | + * @throws \EE_Error |
|
123 | + */ |
|
124 | 124 | public static function download_ics_file() { |
125 | 125 | if ( EE_Registry::instance()->REQ->is_set( 'ics_id' )) { |
126 | 126 | $DTT_ID = absint( EE_Registry::instance()->REQ->get( 'ics_id' )); |
@@ -228,8 +228,8 @@ discard block |
||
228 | 228 | //Escape special chars within the description |
229 | 229 | $description = EED_Ical::_escape_ICal_data( $description ); |
230 | 230 | |
231 | - //Remove line breaks and output in iCal format |
|
232 | - $description = str_replace( array( "\r\n", "\n"), '\n', $description ); |
|
231 | + //Remove line breaks and output in iCal format |
|
232 | + $description = str_replace( array( "\r\n", "\n"), '\n', $description ); |
|
233 | 233 | |
234 | 234 | return $description; |
235 | 235 | } |
@@ -1,4 +1,6 @@ |
||
1 | -<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | + exit('No direct script access allowed'); |
|
3 | +} |
|
2 | 4 | |
3 | 5 | /** |
4 | 6 | * EED_Ical Class |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | * @return EED_Ical|EED_Module |
20 | 20 | */ |
21 | 21 | public static function instance() { |
22 | - return parent::get_instance( __CLASS__ ); |
|
22 | + return parent::get_instance(__CLASS__); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | |
@@ -34,12 +34,12 @@ discard block |
||
34 | 34 | // create download buttons |
35 | 35 | add_filter( |
36 | 36 | 'FHEE__espresso_list_of_event_dates__datetime_html', |
37 | - array( 'EED_Ical', 'generate_add_to_iCal_button' ), |
|
37 | + array('EED_Ical', 'generate_add_to_iCal_button'), |
|
38 | 38 | 10, |
39 | 39 | 2 |
40 | 40 | ); |
41 | 41 | // process ics download request |
42 | - EE_Config::register_route( 'download_ics_file', 'EED_Ical', 'download_ics_file' ); |
|
42 | + EE_Config::register_route('download_ics_file', 'EED_Ical', 'download_ics_file'); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @param WP $WP |
63 | 63 | * @return void |
64 | 64 | */ |
65 | - public function run( $WP ) {} |
|
65 | + public function run($WP) {} |
|
66 | 66 | |
67 | 67 | |
68 | 68 | |
@@ -75,35 +75,35 @@ discard block |
||
75 | 75 | * @return string |
76 | 76 | * @throws \EE_Error |
77 | 77 | */ |
78 | - public static function generate_add_to_iCal_button( $html, $datetime ) { |
|
78 | + public static function generate_add_to_iCal_button($html, $datetime) { |
|
79 | 79 | // first verify a proper datetime object has been received |
80 | - if ( $datetime instanceof EE_Datetime ) { |
|
80 | + if ($datetime instanceof EE_Datetime) { |
|
81 | 81 | // set whether a link or submit button is shown |
82 | - $iCal_type = apply_filters( 'FHEE__EED_Ical__generate_add_to_iCal_button__iCal_type', 'submit' ); |
|
82 | + $iCal_type = apply_filters('FHEE__EED_Ical__generate_add_to_iCal_button__iCal_type', 'submit'); |
|
83 | 83 | // generate a link to the route we registered in set_hooks() |
84 | - $URL = add_query_arg( array( 'ee' => 'download_ics_file', 'ics_id' => $datetime->ID() ), site_url() ); |
|
84 | + $URL = add_query_arg(array('ee' => 'download_ics_file', 'ics_id' => $datetime->ID()), site_url()); |
|
85 | 85 | // what type ? |
86 | - switch ( $iCal_type ) { |
|
86 | + switch ($iCal_type) { |
|
87 | 87 | // submit buttons appear as buttons and are very compatible with a theme's style |
88 | 88 | case 'submit' : |
89 | - $html .= '<form id="download-iCal-frm-' . $datetime->ID(); |
|
90 | - $html .= '" class="download-iCal-frm" action="' . $URL . '" method="post" >'; |
|
89 | + $html .= '<form id="download-iCal-frm-'.$datetime->ID(); |
|
90 | + $html .= '" class="download-iCal-frm" action="'.$URL.'" method="post" >'; |
|
91 | 91 | $html .= '<input type="submit" class="ee-ical-sbmt" value="" title="'; |
92 | - $html .= __( 'Add to iCal Calendar', 'event_espresso' ) . '"/>'; |
|
92 | + $html .= __('Add to iCal Calendar', 'event_espresso').'"/>'; |
|
93 | 93 | $html .= '</form>'; |
94 | 94 | break; |
95 | 95 | // buttons are just links that have been styled to appear as buttons, |
96 | 96 | // but may not be blend with a theme as well as submit buttons |
97 | 97 | case 'button' : |
98 | - $html .= '<a class="ee-ical-btn small ee-button ee-roundish" href="' . $URL; |
|
99 | - $html .= '" title="' . __( 'Add to iCal Calendar', 'event_espresso' ) . '">'; |
|
98 | + $html .= '<a class="ee-ical-btn small ee-button ee-roundish" href="'.$URL; |
|
99 | + $html .= '" title="'.__('Add to iCal Calendar', 'event_espresso').'">'; |
|
100 | 100 | $html .= ' <span class="dashicons dashicons-calendar"></span>'; |
101 | 101 | $html .= '</a>'; |
102 | 102 | break; |
103 | 103 | // links are just links that use the calendar dashicon |
104 | 104 | case 'icon' : |
105 | - $html .= '<a class="ee-ical-lnk" href="' . $URL . '" title="'; |
|
106 | - $html .= __( 'Add to iCal Calendar', 'event_espresso' ) . '">'; |
|
105 | + $html .= '<a class="ee-ical-lnk" href="'.$URL.'" title="'; |
|
106 | + $html .= __('Add to iCal Calendar', 'event_espresso').'">'; |
|
107 | 107 | $html .= ' <span class="dashicons dashicons-calendar"></span>'; |
108 | 108 | $html .= '</a>'; |
109 | 109 | break; |
@@ -122,29 +122,29 @@ discard block |
||
122 | 122 | * @throws \EE_Error |
123 | 123 | */ |
124 | 124 | public static function download_ics_file() { |
125 | - if ( EE_Registry::instance()->REQ->is_set( 'ics_id' )) { |
|
126 | - $DTT_ID = absint( EE_Registry::instance()->REQ->get( 'ics_id' )); |
|
127 | - $datetime = EE_Registry::instance()->load_model( 'Datetime' )->get_one_by_ID( $DTT_ID ); |
|
128 | - if ( $datetime instanceof EE_Datetime ) { |
|
125 | + if (EE_Registry::instance()->REQ->is_set('ics_id')) { |
|
126 | + $DTT_ID = absint(EE_Registry::instance()->REQ->get('ics_id')); |
|
127 | + $datetime = EE_Registry::instance()->load_model('Datetime')->get_one_by_ID($DTT_ID); |
|
128 | + if ($datetime instanceof EE_Datetime) { |
|
129 | 129 | // get related event, venues, and event categories |
130 | 130 | $event = $datetime->event(); |
131 | 131 | // get related category Term object and it's name |
132 | 132 | $category = $event->first_event_category(); |
133 | - if ( $category instanceof EE_Term ) { |
|
133 | + if ($category instanceof EE_Term) { |
|
134 | 134 | $category = $category->name(); |
135 | 135 | } |
136 | 136 | $location = ''; |
137 | 137 | // get first related venue and convert to CSV string |
138 | - $venue = $event->venues(array( 'limit'=>1 )); |
|
139 | - if ( is_array( $venue ) && ! empty( $venue )) { |
|
140 | - $venue = array_shift( $venue ); |
|
141 | - if ( $venue instanceof EE_Venue ) { |
|
142 | - $location = espresso_venue_raw_address( 'inline', $venue->ID(), FALSE ); |
|
138 | + $venue = $event->venues(array('limit'=>1)); |
|
139 | + if (is_array($venue) && ! empty($venue)) { |
|
140 | + $venue = array_shift($venue); |
|
141 | + if ($venue instanceof EE_Venue) { |
|
142 | + $location = espresso_venue_raw_address('inline', $venue->ID(), FALSE); |
|
143 | 143 | } |
144 | 144 | } |
145 | 145 | |
146 | 146 | //Generate filename |
147 | - $filename = $event->slug() . '-' . $datetime->start_date( 'Y-m-d' ) . '.ics'; |
|
147 | + $filename = $event->slug().'-'.$datetime->start_date('Y-m-d').'.ics'; |
|
148 | 148 | |
149 | 149 | //Check the datetime status has not been cancelled and set the ics value accordingly |
150 | 150 | $status = $datetime->get_active_status(); |
@@ -153,62 +153,62 @@ discard block |
||
153 | 153 | // Create array of ics details, escape strings, convert timestamps to ics format, etc |
154 | 154 | $ics_data = array( |
155 | 155 | 'ORGANIZER_NAME' => EE_Registry::instance()->CFG->organization->name, |
156 | - 'UID' => md5( $event->name() . $event->ID() . $datetime->ID() ), |
|
156 | + 'UID' => md5($event->name().$event->ID().$datetime->ID()), |
|
157 | 157 | 'ORGANIZER' => EE_Registry::instance()->CFG->organization->email, |
158 | - 'DTSTAMP' => date( EED_Ical::iCal_datetime_format ), |
|
158 | + 'DTSTAMP' => date(EED_Ical::iCal_datetime_format), |
|
159 | 159 | 'LOCATION' => $location, |
160 | 160 | 'SUMMARY' => $event->name(), |
161 | - 'DESCRIPTION' => wp_strip_all_tags( $event->description() ), |
|
161 | + 'DESCRIPTION' => wp_strip_all_tags($event->description()), |
|
162 | 162 | 'STATUS' => $status, |
163 | 163 | 'CATEGORIES' => $category, |
164 | - 'URL;VALUE=URI' => get_permalink( $event->ID() ), |
|
165 | - 'DTSTART' => date( EED_Ical::iCal_datetime_format, $datetime->start() ), |
|
166 | - 'DTEND' => date( EED_Ical::iCal_datetime_format, $datetime->end() ), |
|
164 | + 'URL;VALUE=URI' => get_permalink($event->ID()), |
|
165 | + 'DTSTART' => date(EED_Ical::iCal_datetime_format, $datetime->start()), |
|
166 | + 'DTEND' => date(EED_Ical::iCal_datetime_format, $datetime->end()), |
|
167 | 167 | ); |
168 | 168 | |
169 | 169 | //Filter the values used within the ics output. |
170 | 170 | //NOTE - all values within ics_data will be escaped automatically. |
171 | - $ics_data = apply_filters( 'FHEE__EED_Ical__download_ics_file_ics_data', $ics_data, $datetime ); |
|
171 | + $ics_data = apply_filters('FHEE__EED_Ical__download_ics_file_ics_data', $ics_data, $datetime); |
|
172 | 172 | |
173 | 173 | //Escape all ics data |
174 | - foreach( $ics_data as $key => $value ) { |
|
174 | + foreach ($ics_data as $key => $value) { |
|
175 | 175 | //Description is escaped differently from all all values |
176 | - if( $key === 'DESCRIPTION' ) { |
|
177 | - $ics_data[$key] = EED_Ical::_escape_ICal_description( wp_strip_all_tags( $value ) ); |
|
176 | + if ($key === 'DESCRIPTION') { |
|
177 | + $ics_data[$key] = EED_Ical::_escape_ICal_description(wp_strip_all_tags($value)); |
|
178 | 178 | } else { |
179 | - $ics_data[$key] = EED_Ical::_escape_ICal_data( $value ); |
|
179 | + $ics_data[$key] = EED_Ical::_escape_ICal_data($value); |
|
180 | 180 | } |
181 | 181 | } |
182 | 182 | |
183 | 183 | //Pull the organizer name from ics_data and remove it from the array. |
184 | - $organizer_name = isset( $ics_data['ORGANIZER_NAME'] ) ? $ics_data['ORGANIZER_NAME'] : ''; |
|
185 | - unset( $ics_data['ORGANIZER_NAME'] ); |
|
184 | + $organizer_name = isset($ics_data['ORGANIZER_NAME']) ? $ics_data['ORGANIZER_NAME'] : ''; |
|
185 | + unset($ics_data['ORGANIZER_NAME']); |
|
186 | 186 | |
187 | 187 | // set headers |
188 | - header( 'Content-type: text/calendar; charset=utf-8' ); |
|
189 | - header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
|
190 | - header( 'Cache-Control: private, max-age=0, must-revalidate' ); |
|
191 | - header( 'Pragma: public' ); |
|
192 | - header( 'Content-Type: application/octet-stream' ); |
|
193 | - header( 'Content-Type: application/force-download' ); |
|
194 | - header( 'Cache-Control: no-cache, must-revalidate' ); |
|
195 | - header( 'Content-Transfer-Encoding: binary' ); |
|
196 | - header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); // past date |
|
197 | - ini_set( 'zlib.output_compression', '0' ); |
|
188 | + header('Content-type: text/calendar; charset=utf-8'); |
|
189 | + header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|
190 | + header('Cache-Control: private, max-age=0, must-revalidate'); |
|
191 | + header('Pragma: public'); |
|
192 | + header('Content-Type: application/octet-stream'); |
|
193 | + header('Content-Type: application/force-download'); |
|
194 | + header('Cache-Control: no-cache, must-revalidate'); |
|
195 | + header('Content-Transfer-Encoding: binary'); |
|
196 | + header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // past date |
|
197 | + ini_set('zlib.output_compression', '0'); |
|
198 | 198 | // echo the output |
199 | - echo "BEGIN:VCALENDAR" . PHP_EOL; |
|
200 | - echo "VERSION:2.0" . PHP_EOL; |
|
201 | - echo "PRODID:-//{$organizer_name}//NONSGML PDA Calendar Version 1.0//EN" . PHP_EOL; |
|
202 | - echo "CALSCALE:GREGORIAN" . PHP_EOL; |
|
203 | - echo "BEGIN:VEVENT" . PHP_EOL; |
|
199 | + echo "BEGIN:VCALENDAR".PHP_EOL; |
|
200 | + echo "VERSION:2.0".PHP_EOL; |
|
201 | + echo "PRODID:-//{$organizer_name}//NONSGML PDA Calendar Version 1.0//EN".PHP_EOL; |
|
202 | + echo "CALSCALE:GREGORIAN".PHP_EOL; |
|
203 | + echo "BEGIN:VEVENT".PHP_EOL; |
|
204 | 204 | |
205 | 205 | //Output all remaining values from ics_data. |
206 | - foreach( $ics_data as $key => $value ) { |
|
207 | - echo $key . ':' . $value . PHP_EOL; |
|
206 | + foreach ($ics_data as $key => $value) { |
|
207 | + echo $key.':'.$value.PHP_EOL; |
|
208 | 208 | } |
209 | 209 | |
210 | - echo "END:VEVENT" . PHP_EOL; |
|
211 | - echo "END:VCALENDAR" . PHP_EOL; |
|
210 | + echo "END:VEVENT".PHP_EOL; |
|
211 | + echo "END:VCALENDAR".PHP_EOL; |
|
212 | 212 | } |
213 | 213 | } |
214 | 214 | die(); |
@@ -223,8 +223,8 @@ discard block |
||
223 | 223 | * @param string $string |
224 | 224 | * @return string |
225 | 225 | */ |
226 | - private static function _escape_ICal_data( $string = '' ) { |
|
227 | - return preg_replace( '/([\,;])/', '\\\$1', $string ); |
|
226 | + private static function _escape_ICal_data($string = '') { |
|
227 | + return preg_replace('/([\,;])/', '\\\$1', $string); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -234,13 +234,13 @@ discard block |
||
234 | 234 | * @param string $description |
235 | 235 | * @return string |
236 | 236 | */ |
237 | - private static function _escape_ICal_description( $description = '' ) { |
|
237 | + private static function _escape_ICal_description($description = '') { |
|
238 | 238 | |
239 | 239 | //Escape special chars within the description |
240 | - $description = EED_Ical::_escape_ICal_data( $description ); |
|
240 | + $description = EED_Ical::_escape_ICal_data($description); |
|
241 | 241 | |
242 | 242 | //Remove line breaks and output in iCal format |
243 | - $description = str_replace( array( "\r\n", "\n"), '\n', $description ); |
|
243 | + $description = str_replace(array("\r\n", "\n"), '\n', $description); |
|
244 | 244 | |
245 | 245 | return $description; |
246 | 246 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
3 | - exit('NO direct script access allowed'); |
|
3 | + exit('NO direct script access allowed'); |
|
4 | 4 | } |
5 | 5 | /** |
6 | 6 | * Event Espresso |
@@ -16,35 +16,35 @@ discard block |
||
16 | 16 | |
17 | 17 | |
18 | 18 | if ( ! function_exists('espresso_get_template_part')) { |
19 | - /** |
|
20 | - * espresso_get_template_part |
|
21 | - * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, and doesn't add base versions of files |
|
22 | - * so not a very useful function at all except that it adds familiarity PLUS filtering based off of the entire template part name |
|
23 | - * |
|
24 | - * @param string $slug The slug name for the generic template. |
|
25 | - * @param string $name The name of the specialised template. |
|
26 | - * @return string the html output for the formatted money value |
|
27 | - */ |
|
28 | - function espresso_get_template_part($slug = null, $name = null) |
|
29 | - { |
|
30 | - EEH_Template::get_template_part($slug, $name); |
|
31 | - } |
|
19 | + /** |
|
20 | + * espresso_get_template_part |
|
21 | + * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, and doesn't add base versions of files |
|
22 | + * so not a very useful function at all except that it adds familiarity PLUS filtering based off of the entire template part name |
|
23 | + * |
|
24 | + * @param string $slug The slug name for the generic template. |
|
25 | + * @param string $name The name of the specialised template. |
|
26 | + * @return string the html output for the formatted money value |
|
27 | + */ |
|
28 | + function espresso_get_template_part($slug = null, $name = null) |
|
29 | + { |
|
30 | + EEH_Template::get_template_part($slug, $name); |
|
31 | + } |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | |
35 | 35 | if ( ! function_exists('espresso_get_object_css_class')) { |
36 | - /** |
|
37 | - * espresso_get_object_css_class - attempts to generate a css class based on the type of EE object passed |
|
38 | - * |
|
39 | - * @param EE_Base_Class $object the EE object the css class is being generated for |
|
40 | - * @param string $prefix added to the beginning of the generated class |
|
41 | - * @param string $suffix added to the end of the generated class |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - function espresso_get_object_css_class($object = null, $prefix = '', $suffix = '') |
|
45 | - { |
|
46 | - return EEH_Template::get_object_css_class($object, $prefix, $suffix); |
|
47 | - } |
|
36 | + /** |
|
37 | + * espresso_get_object_css_class - attempts to generate a css class based on the type of EE object passed |
|
38 | + * |
|
39 | + * @param EE_Base_Class $object the EE object the css class is being generated for |
|
40 | + * @param string $prefix added to the beginning of the generated class |
|
41 | + * @param string $suffix added to the end of the generated class |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + function espresso_get_object_css_class($object = null, $prefix = '', $suffix = '') |
|
45 | + { |
|
46 | + return EEH_Template::get_object_css_class($object, $prefix, $suffix); |
|
47 | + } |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | |
@@ -59,648 +59,648 @@ discard block |
||
59 | 59 | class EEH_Template |
60 | 60 | { |
61 | 61 | |
62 | - private static $_espresso_themes = array(); |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
|
67 | - * |
|
68 | - * @return boolean |
|
69 | - */ |
|
70 | - public static function is_espresso_theme() |
|
71 | - { |
|
72 | - return wp_get_theme()->get('TextDomain') == 'event_espresso' ? true : false; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then |
|
77 | - * load it's functions.php file ( if not already loaded ) |
|
78 | - * |
|
79 | - * @return void |
|
80 | - */ |
|
81 | - public static function load_espresso_theme_functions() |
|
82 | - { |
|
83 | - if ( ! defined('EE_THEME_FUNCTIONS_LOADED')) { |
|
84 | - if (is_readable(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php')) { |
|
85 | - require_once(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php'); |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public static function get_espresso_themes() |
|
97 | - { |
|
98 | - if (empty(EEH_Template::$_espresso_themes)) { |
|
99 | - $espresso_themes = glob(EE_PUBLIC . '*', GLOB_ONLYDIR); |
|
100 | - if (empty($espresso_themes)) { |
|
101 | - return array(); |
|
102 | - } |
|
103 | - if (($key = array_search('global_assets', $espresso_themes)) !== false) { |
|
104 | - unset($espresso_themes[$key]); |
|
105 | - } |
|
106 | - EEH_Template::$_espresso_themes = array(); |
|
107 | - foreach ($espresso_themes as $espresso_theme) { |
|
108 | - EEH_Template::$_espresso_themes[basename($espresso_theme)] = $espresso_theme; |
|
109 | - } |
|
110 | - } |
|
111 | - return EEH_Template::$_espresso_themes; |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * EEH_Template::get_template_part |
|
117 | - * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, |
|
118 | - * and doesn't add base versions of files so not a very useful function at all except that it adds familiarity PLUS |
|
119 | - * filtering based off of the entire template part name |
|
120 | - * |
|
121 | - * @param string $slug The slug name for the generic template. |
|
122 | - * @param string $name The name of the specialised template. |
|
123 | - * @param array $template_args |
|
124 | - * @param bool $return_string |
|
125 | - * @return string the html output for the formatted money value |
|
126 | - */ |
|
127 | - public static function get_template_part( |
|
128 | - $slug = null, |
|
129 | - $name = null, |
|
130 | - $template_args = array(), |
|
131 | - $return_string = false |
|
132 | - ) { |
|
133 | - do_action("get_template_part_{$slug}-{$name}", $slug, $name); |
|
134 | - $templates = array(); |
|
135 | - $name = (string)$name; |
|
136 | - if ($name != '') { |
|
137 | - $templates[] = "{$slug}-{$name}.php"; |
|
138 | - } |
|
139 | - // allow template parts to be turned off via something like: add_filter( 'FHEE__content_espresso_events_tickets_template__display_datetimes', '__return_false' ); |
|
140 | - if (apply_filters("FHEE__EEH_Template__get_template_part__display__{$slug}_{$name}", true)) { |
|
141 | - EEH_Template::locate_template($templates, $template_args, true, $return_string); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * locate_template |
|
148 | - * locate a template file by looking in the following places, in the following order: |
|
149 | - * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
|
150 | - * <assumed full absolute server path> |
|
151 | - * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
|
152 | - * <server path up to>/wp-content/uploads/espresso/templates/ |
|
153 | - * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
|
154 | - * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
|
155 | - * <server path up to>/wp-content/plugins/<EE4 folder>/ |
|
156 | - * as soon as the template is found in one of these locations, it will be returned or loaded |
|
157 | - * Example: |
|
158 | - * You are using the WordPress Twenty Sixteen theme, |
|
159 | - * and you want to customize the "some-event.template.php" template, |
|
160 | - * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
|
161 | - * Assuming WP is installed on your server in the "/home/public_html/" folder, |
|
162 | - * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
|
163 | - * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
|
164 | - * /relative/path/to/some-event.template.php |
|
165 | - * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
166 | - * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
|
167 | - * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
168 | - * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
169 | - * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
|
170 | - * Had you passed an absolute path to your template that was in some other location, |
|
171 | - * ie: "/absolute/path/to/some-event.template.php" |
|
172 | - * then the search would have been : |
|
173 | - * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
|
174 | - * /absolute/path/to/some-event.template.php |
|
175 | - * and stopped there upon finding it in the second location |
|
176 | - * |
|
177 | - * @param array|string $templates array of template file names including extension (or just a single string) |
|
178 | - * @param array $template_args an array of arguments to be extracted for use in the template |
|
179 | - * @param boolean $load whether to pass the located template path on to the |
|
180 | - * EEH_Template::display_template() method or simply return it |
|
181 | - * @param boolean $return_string whether to send output immediately to screen, or capture and return as a |
|
182 | - * string |
|
183 | - * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will |
|
184 | - * generate a custom template or not. Used in places where you don't actually |
|
185 | - * load the template, you just want to know if there's a custom version of it. |
|
186 | - * @return mixed |
|
187 | - */ |
|
188 | - public static function locate_template( |
|
189 | - $templates = array(), |
|
190 | - $template_args = array(), |
|
191 | - $load = true, |
|
192 | - $return_string = true, |
|
193 | - $check_if_custom = false |
|
194 | - ) { |
|
195 | - // first use WP locate_template to check for template in the current theme folder |
|
196 | - $template_path = locate_template($templates); |
|
197 | - |
|
198 | - if ($check_if_custom && ! empty($template_path)) { |
|
199 | - return true; |
|
200 | - } |
|
201 | - |
|
202 | - // not in the theme |
|
203 | - if (empty($template_path)) { |
|
204 | - // not even a template to look for ? |
|
205 | - if (empty($templates)) { |
|
206 | - // get post_type |
|
207 | - $post_type = EE_Registry::instance()->REQ->get('post_type'); |
|
208 | - // get array of EE Custom Post Types |
|
209 | - $EE_CPTs = EE_Register_CPTs::get_CPTs(); |
|
210 | - // build template name based on request |
|
211 | - if (isset($EE_CPTs[$post_type])) { |
|
212 | - $archive_or_single = is_archive() ? 'archive' : ''; |
|
213 | - $archive_or_single = is_single() ? 'single' : $archive_or_single; |
|
214 | - $templates = $archive_or_single . '-' . $post_type . '.php'; |
|
215 | - } |
|
216 | - } |
|
217 | - // currently active EE template theme |
|
218 | - $current_theme = EE_Config::get_current_theme(); |
|
219 | - |
|
220 | - // array of paths to folders that may contain templates |
|
221 | - $template_folder_paths = array( |
|
222 | - // first check the /wp-content/uploads/espresso/templates/(current EE theme)/ folder for an EE theme template file |
|
223 | - EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, |
|
224 | - // then in the root of the /wp-content/uploads/espresso/templates/ folder |
|
225 | - EVENT_ESPRESSO_TEMPLATE_DIR, |
|
226 | - ); |
|
227 | - |
|
228 | - //add core plugin folders for checking only if we're not $check_if_custom |
|
229 | - if ( ! $check_if_custom) { |
|
230 | - $core_paths = array( |
|
231 | - // in the /wp-content/plugins/(EE4 folder)/public/(current EE theme)/ folder within the plugin |
|
232 | - EE_PUBLIC . $current_theme, |
|
233 | - // in the /wp-content/plugins/(EE4 folder)/core/templates/(current EE theme)/ folder within the plugin |
|
234 | - EE_TEMPLATES . $current_theme, |
|
235 | - // or maybe relative from the plugin root: /wp-content/plugins/(EE4 folder)/ |
|
236 | - EE_PLUGIN_DIR_PATH, |
|
237 | - ); |
|
238 | - $template_folder_paths = array_merge($template_folder_paths, $core_paths); |
|
239 | - } |
|
240 | - |
|
241 | - // now filter that array |
|
242 | - $template_folder_paths = apply_filters('FHEE__EEH_Template__locate_template__template_folder_paths', |
|
243 | - $template_folder_paths); |
|
244 | - $templates = is_array($templates) ? $templates : array($templates); |
|
245 | - $template_folder_paths = is_array($template_folder_paths) ? $template_folder_paths : array($template_folder_paths); |
|
246 | - // array to hold all possible template paths |
|
247 | - $full_template_paths = array(); |
|
248 | - |
|
249 | - // loop through $templates |
|
250 | - foreach ($templates as $template) { |
|
251 | - // normalize directory separators |
|
252 | - $template = EEH_File::standardise_directory_separators($template); |
|
253 | - $file_name = basename($template); |
|
254 | - $template_path_minus_file_name = substr($template, 0, (strlen($file_name) * -1)); |
|
255 | - // while looping through all template folder paths |
|
256 | - foreach ($template_folder_paths as $template_folder_path) { |
|
257 | - // normalize directory separators |
|
258 | - $template_folder_path = EEH_File::standardise_directory_separators($template_folder_path); |
|
259 | - // determine if any common base path exists between the two paths |
|
260 | - $common_base_path = EEH_Template::_find_common_base_path( |
|
261 | - array($template_folder_path, $template_path_minus_file_name) |
|
262 | - ); |
|
263 | - if ($common_base_path !== '') { |
|
264 | - // both paths have a common base, so just tack the filename onto our search path |
|
265 | - $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $file_name; |
|
266 | - } else { |
|
267 | - // no common base path, so let's just concatenate |
|
268 | - $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $template; |
|
269 | - } |
|
270 | - // build up our template locations array by adding our resolved paths |
|
271 | - $full_template_paths[] = $resolved_path; |
|
272 | - } |
|
273 | - // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first |
|
274 | - array_unshift($full_template_paths, $template); |
|
275 | - // path to the directory of the current theme: /wp-content/themes/(current WP theme)/ |
|
276 | - array_unshift($full_template_paths, get_stylesheet_directory() . DS . $file_name); |
|
277 | - } |
|
278 | - // filter final array of full template paths |
|
279 | - $full_template_paths = apply_filters('FHEE__EEH_Template__locate_template__full_template_paths', |
|
280 | - $full_template_paths, $file_name); |
|
281 | - // now loop through our final array of template location paths and check each location |
|
282 | - foreach ((array)$full_template_paths as $full_template_path) { |
|
283 | - if (is_readable($full_template_path)) { |
|
284 | - $template_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $full_template_path); |
|
285 | - // hook that can be used to display the full template path that will be used |
|
286 | - do_action('AHEE__EEH_Template__locate_template__full_template_path', $template_path); |
|
287 | - break; |
|
288 | - } |
|
289 | - } |
|
290 | - } |
|
291 | - // if we got it and you want to see it... |
|
292 | - if ($template_path && $load && ! $check_if_custom) { |
|
293 | - if ($return_string) { |
|
294 | - return EEH_Template::display_template($template_path, $template_args, true); |
|
295 | - } else { |
|
296 | - EEH_Template::display_template($template_path, $template_args, false); |
|
297 | - } |
|
298 | - } |
|
299 | - return $check_if_custom && ! empty($template_path) ? true : $template_path; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * _find_common_base_path |
|
305 | - * given two paths, this determines if there is a common base path between the two |
|
306 | - * |
|
307 | - * @param array $paths |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - protected static function _find_common_base_path($paths) |
|
311 | - { |
|
312 | - $last_offset = 0; |
|
313 | - $common_base_path = ''; |
|
314 | - while (($index = strpos($paths[0], DS, $last_offset)) !== false) { |
|
315 | - $dir_length = $index - $last_offset + 1; |
|
316 | - $directory = substr($paths[0], $last_offset, $dir_length); |
|
317 | - foreach ($paths as $path) { |
|
318 | - if (substr($path, $last_offset, $dir_length) != $directory) { |
|
319 | - return $common_base_path; |
|
320 | - } |
|
321 | - } |
|
322 | - $common_base_path .= $directory; |
|
323 | - $last_offset = $index + 1; |
|
324 | - } |
|
325 | - return substr($common_base_path, 0, -1); |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * load and display a template |
|
331 | - * |
|
332 | - * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
|
333 | - * @param array $template_args an array of arguments to be extracted for use in the template |
|
334 | - * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
|
335 | - * @param bool $throw_exceptions if set to true, will throw an exception if the template is either |
|
336 | - * not found or is not readable |
|
337 | - * @return mixed string |
|
338 | - * @throws \DomainException |
|
339 | - */ |
|
62 | + private static $_espresso_themes = array(); |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
|
67 | + * |
|
68 | + * @return boolean |
|
69 | + */ |
|
70 | + public static function is_espresso_theme() |
|
71 | + { |
|
72 | + return wp_get_theme()->get('TextDomain') == 'event_espresso' ? true : false; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then |
|
77 | + * load it's functions.php file ( if not already loaded ) |
|
78 | + * |
|
79 | + * @return void |
|
80 | + */ |
|
81 | + public static function load_espresso_theme_functions() |
|
82 | + { |
|
83 | + if ( ! defined('EE_THEME_FUNCTIONS_LOADED')) { |
|
84 | + if (is_readable(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php')) { |
|
85 | + require_once(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php'); |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public static function get_espresso_themes() |
|
97 | + { |
|
98 | + if (empty(EEH_Template::$_espresso_themes)) { |
|
99 | + $espresso_themes = glob(EE_PUBLIC . '*', GLOB_ONLYDIR); |
|
100 | + if (empty($espresso_themes)) { |
|
101 | + return array(); |
|
102 | + } |
|
103 | + if (($key = array_search('global_assets', $espresso_themes)) !== false) { |
|
104 | + unset($espresso_themes[$key]); |
|
105 | + } |
|
106 | + EEH_Template::$_espresso_themes = array(); |
|
107 | + foreach ($espresso_themes as $espresso_theme) { |
|
108 | + EEH_Template::$_espresso_themes[basename($espresso_theme)] = $espresso_theme; |
|
109 | + } |
|
110 | + } |
|
111 | + return EEH_Template::$_espresso_themes; |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * EEH_Template::get_template_part |
|
117 | + * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, |
|
118 | + * and doesn't add base versions of files so not a very useful function at all except that it adds familiarity PLUS |
|
119 | + * filtering based off of the entire template part name |
|
120 | + * |
|
121 | + * @param string $slug The slug name for the generic template. |
|
122 | + * @param string $name The name of the specialised template. |
|
123 | + * @param array $template_args |
|
124 | + * @param bool $return_string |
|
125 | + * @return string the html output for the formatted money value |
|
126 | + */ |
|
127 | + public static function get_template_part( |
|
128 | + $slug = null, |
|
129 | + $name = null, |
|
130 | + $template_args = array(), |
|
131 | + $return_string = false |
|
132 | + ) { |
|
133 | + do_action("get_template_part_{$slug}-{$name}", $slug, $name); |
|
134 | + $templates = array(); |
|
135 | + $name = (string)$name; |
|
136 | + if ($name != '') { |
|
137 | + $templates[] = "{$slug}-{$name}.php"; |
|
138 | + } |
|
139 | + // allow template parts to be turned off via something like: add_filter( 'FHEE__content_espresso_events_tickets_template__display_datetimes', '__return_false' ); |
|
140 | + if (apply_filters("FHEE__EEH_Template__get_template_part__display__{$slug}_{$name}", true)) { |
|
141 | + EEH_Template::locate_template($templates, $template_args, true, $return_string); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * locate_template |
|
148 | + * locate a template file by looking in the following places, in the following order: |
|
149 | + * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
|
150 | + * <assumed full absolute server path> |
|
151 | + * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
|
152 | + * <server path up to>/wp-content/uploads/espresso/templates/ |
|
153 | + * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
|
154 | + * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
|
155 | + * <server path up to>/wp-content/plugins/<EE4 folder>/ |
|
156 | + * as soon as the template is found in one of these locations, it will be returned or loaded |
|
157 | + * Example: |
|
158 | + * You are using the WordPress Twenty Sixteen theme, |
|
159 | + * and you want to customize the "some-event.template.php" template, |
|
160 | + * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
|
161 | + * Assuming WP is installed on your server in the "/home/public_html/" folder, |
|
162 | + * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
|
163 | + * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
|
164 | + * /relative/path/to/some-event.template.php |
|
165 | + * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
166 | + * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
|
167 | + * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
168 | + * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
|
169 | + * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
|
170 | + * Had you passed an absolute path to your template that was in some other location, |
|
171 | + * ie: "/absolute/path/to/some-event.template.php" |
|
172 | + * then the search would have been : |
|
173 | + * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
|
174 | + * /absolute/path/to/some-event.template.php |
|
175 | + * and stopped there upon finding it in the second location |
|
176 | + * |
|
177 | + * @param array|string $templates array of template file names including extension (or just a single string) |
|
178 | + * @param array $template_args an array of arguments to be extracted for use in the template |
|
179 | + * @param boolean $load whether to pass the located template path on to the |
|
180 | + * EEH_Template::display_template() method or simply return it |
|
181 | + * @param boolean $return_string whether to send output immediately to screen, or capture and return as a |
|
182 | + * string |
|
183 | + * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will |
|
184 | + * generate a custom template or not. Used in places where you don't actually |
|
185 | + * load the template, you just want to know if there's a custom version of it. |
|
186 | + * @return mixed |
|
187 | + */ |
|
188 | + public static function locate_template( |
|
189 | + $templates = array(), |
|
190 | + $template_args = array(), |
|
191 | + $load = true, |
|
192 | + $return_string = true, |
|
193 | + $check_if_custom = false |
|
194 | + ) { |
|
195 | + // first use WP locate_template to check for template in the current theme folder |
|
196 | + $template_path = locate_template($templates); |
|
197 | + |
|
198 | + if ($check_if_custom && ! empty($template_path)) { |
|
199 | + return true; |
|
200 | + } |
|
201 | + |
|
202 | + // not in the theme |
|
203 | + if (empty($template_path)) { |
|
204 | + // not even a template to look for ? |
|
205 | + if (empty($templates)) { |
|
206 | + // get post_type |
|
207 | + $post_type = EE_Registry::instance()->REQ->get('post_type'); |
|
208 | + // get array of EE Custom Post Types |
|
209 | + $EE_CPTs = EE_Register_CPTs::get_CPTs(); |
|
210 | + // build template name based on request |
|
211 | + if (isset($EE_CPTs[$post_type])) { |
|
212 | + $archive_or_single = is_archive() ? 'archive' : ''; |
|
213 | + $archive_or_single = is_single() ? 'single' : $archive_or_single; |
|
214 | + $templates = $archive_or_single . '-' . $post_type . '.php'; |
|
215 | + } |
|
216 | + } |
|
217 | + // currently active EE template theme |
|
218 | + $current_theme = EE_Config::get_current_theme(); |
|
219 | + |
|
220 | + // array of paths to folders that may contain templates |
|
221 | + $template_folder_paths = array( |
|
222 | + // first check the /wp-content/uploads/espresso/templates/(current EE theme)/ folder for an EE theme template file |
|
223 | + EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, |
|
224 | + // then in the root of the /wp-content/uploads/espresso/templates/ folder |
|
225 | + EVENT_ESPRESSO_TEMPLATE_DIR, |
|
226 | + ); |
|
227 | + |
|
228 | + //add core plugin folders for checking only if we're not $check_if_custom |
|
229 | + if ( ! $check_if_custom) { |
|
230 | + $core_paths = array( |
|
231 | + // in the /wp-content/plugins/(EE4 folder)/public/(current EE theme)/ folder within the plugin |
|
232 | + EE_PUBLIC . $current_theme, |
|
233 | + // in the /wp-content/plugins/(EE4 folder)/core/templates/(current EE theme)/ folder within the plugin |
|
234 | + EE_TEMPLATES . $current_theme, |
|
235 | + // or maybe relative from the plugin root: /wp-content/plugins/(EE4 folder)/ |
|
236 | + EE_PLUGIN_DIR_PATH, |
|
237 | + ); |
|
238 | + $template_folder_paths = array_merge($template_folder_paths, $core_paths); |
|
239 | + } |
|
240 | + |
|
241 | + // now filter that array |
|
242 | + $template_folder_paths = apply_filters('FHEE__EEH_Template__locate_template__template_folder_paths', |
|
243 | + $template_folder_paths); |
|
244 | + $templates = is_array($templates) ? $templates : array($templates); |
|
245 | + $template_folder_paths = is_array($template_folder_paths) ? $template_folder_paths : array($template_folder_paths); |
|
246 | + // array to hold all possible template paths |
|
247 | + $full_template_paths = array(); |
|
248 | + |
|
249 | + // loop through $templates |
|
250 | + foreach ($templates as $template) { |
|
251 | + // normalize directory separators |
|
252 | + $template = EEH_File::standardise_directory_separators($template); |
|
253 | + $file_name = basename($template); |
|
254 | + $template_path_minus_file_name = substr($template, 0, (strlen($file_name) * -1)); |
|
255 | + // while looping through all template folder paths |
|
256 | + foreach ($template_folder_paths as $template_folder_path) { |
|
257 | + // normalize directory separators |
|
258 | + $template_folder_path = EEH_File::standardise_directory_separators($template_folder_path); |
|
259 | + // determine if any common base path exists between the two paths |
|
260 | + $common_base_path = EEH_Template::_find_common_base_path( |
|
261 | + array($template_folder_path, $template_path_minus_file_name) |
|
262 | + ); |
|
263 | + if ($common_base_path !== '') { |
|
264 | + // both paths have a common base, so just tack the filename onto our search path |
|
265 | + $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $file_name; |
|
266 | + } else { |
|
267 | + // no common base path, so let's just concatenate |
|
268 | + $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $template; |
|
269 | + } |
|
270 | + // build up our template locations array by adding our resolved paths |
|
271 | + $full_template_paths[] = $resolved_path; |
|
272 | + } |
|
273 | + // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first |
|
274 | + array_unshift($full_template_paths, $template); |
|
275 | + // path to the directory of the current theme: /wp-content/themes/(current WP theme)/ |
|
276 | + array_unshift($full_template_paths, get_stylesheet_directory() . DS . $file_name); |
|
277 | + } |
|
278 | + // filter final array of full template paths |
|
279 | + $full_template_paths = apply_filters('FHEE__EEH_Template__locate_template__full_template_paths', |
|
280 | + $full_template_paths, $file_name); |
|
281 | + // now loop through our final array of template location paths and check each location |
|
282 | + foreach ((array)$full_template_paths as $full_template_path) { |
|
283 | + if (is_readable($full_template_path)) { |
|
284 | + $template_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $full_template_path); |
|
285 | + // hook that can be used to display the full template path that will be used |
|
286 | + do_action('AHEE__EEH_Template__locate_template__full_template_path', $template_path); |
|
287 | + break; |
|
288 | + } |
|
289 | + } |
|
290 | + } |
|
291 | + // if we got it and you want to see it... |
|
292 | + if ($template_path && $load && ! $check_if_custom) { |
|
293 | + if ($return_string) { |
|
294 | + return EEH_Template::display_template($template_path, $template_args, true); |
|
295 | + } else { |
|
296 | + EEH_Template::display_template($template_path, $template_args, false); |
|
297 | + } |
|
298 | + } |
|
299 | + return $check_if_custom && ! empty($template_path) ? true : $template_path; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * _find_common_base_path |
|
305 | + * given two paths, this determines if there is a common base path between the two |
|
306 | + * |
|
307 | + * @param array $paths |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + protected static function _find_common_base_path($paths) |
|
311 | + { |
|
312 | + $last_offset = 0; |
|
313 | + $common_base_path = ''; |
|
314 | + while (($index = strpos($paths[0], DS, $last_offset)) !== false) { |
|
315 | + $dir_length = $index - $last_offset + 1; |
|
316 | + $directory = substr($paths[0], $last_offset, $dir_length); |
|
317 | + foreach ($paths as $path) { |
|
318 | + if (substr($path, $last_offset, $dir_length) != $directory) { |
|
319 | + return $common_base_path; |
|
320 | + } |
|
321 | + } |
|
322 | + $common_base_path .= $directory; |
|
323 | + $last_offset = $index + 1; |
|
324 | + } |
|
325 | + return substr($common_base_path, 0, -1); |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * load and display a template |
|
331 | + * |
|
332 | + * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
|
333 | + * @param array $template_args an array of arguments to be extracted for use in the template |
|
334 | + * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
|
335 | + * @param bool $throw_exceptions if set to true, will throw an exception if the template is either |
|
336 | + * not found or is not readable |
|
337 | + * @return mixed string |
|
338 | + * @throws \DomainException |
|
339 | + */ |
|
340 | 340 | public static function display_template( |
341 | - $template_path = false, |
|
342 | - $template_args = array(), |
|
343 | - $return_string = false, |
|
344 | - $throw_exceptions = false |
|
345 | - ) { |
|
346 | - |
|
347 | - /** |
|
348 | - * These two filters are intended for last minute changes to templates being loaded and/or template arg |
|
349 | - * modifications. NOTE... modifying these things can cause breakage as most templates running through |
|
350 | - * the display_template method are templates we DON'T want modified (usually because of js |
|
351 | - * dependencies etc). So unless you know what you are doing, do NOT filter templates or template args |
|
352 | - * using this. |
|
353 | - * |
|
354 | - * @since 4.6.0 |
|
355 | - */ |
|
356 | - $template_path = (string) apply_filters('FHEE__EEH_Template__display_template__template_path', $template_path); |
|
357 | - $template_args = (array) apply_filters('FHEE__EEH_Template__display_template__template_args', $template_args); |
|
358 | - |
|
359 | - // you gimme nuttin - YOU GET NUTTIN !! |
|
360 | - if ( ! $template_path || ! is_readable($template_path)) { |
|
361 | - return ''; |
|
362 | - } |
|
363 | - // if $template_args are not in an array, then make it so |
|
364 | - if ( ! is_array($template_args) && ! is_object($template_args)) { |
|
365 | - $template_args = array($template_args); |
|
366 | - } |
|
367 | - extract( $template_args, EXTR_SKIP ); |
|
368 | - // ignore whether template is accessible ? |
|
369 | - if ( $throw_exceptions && ! is_readable( $template_path ) ) { |
|
370 | - throw new \DomainException( |
|
371 | - esc_html__( |
|
372 | - 'Invalid, unreadable, or missing file.', |
|
373 | - 'event_espresso' |
|
374 | - ) |
|
375 | - ); |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - if ($return_string) { |
|
380 | - // because we want to return a string, we are going to capture the output |
|
381 | - ob_start(); |
|
382 | - include($template_path); |
|
383 | - return ob_get_clean(); |
|
384 | - } else { |
|
385 | - include($template_path); |
|
386 | - } |
|
387 | - return ''; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - /** |
|
392 | - * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
|
393 | - * |
|
394 | - * @param EE_Base_Class $object the EE object the css class is being generated for |
|
395 | - * @param string $prefix added to the beginning of the generated class |
|
396 | - * @param string $suffix added to the end of the generated class |
|
397 | - * @return string |
|
398 | - */ |
|
399 | - public static function get_object_css_class($object = null, $prefix = '', $suffix = '') |
|
400 | - { |
|
401 | - // in the beginning... |
|
402 | - $prefix = ! empty($prefix) ? rtrim($prefix, '-') . '-' : ''; |
|
403 | - // da muddle |
|
404 | - $class = ''; |
|
405 | - // the end |
|
406 | - $suffix = ! empty($suffix) ? '-' . ltrim($suffix, '-') : ''; |
|
407 | - // is the passed object an EE object ? |
|
408 | - if ($object instanceof EE_Base_Class) { |
|
409 | - // grab the exact type of object |
|
410 | - $obj_class = get_class($object); |
|
411 | - // depending on the type of object... |
|
412 | - switch ($obj_class) { |
|
413 | - // no specifics just yet... |
|
414 | - default : |
|
415 | - $class = strtolower(str_replace('_', '-', $obj_class)); |
|
416 | - $class .= method_exists($obj_class, 'name') ? '-' . sanitize_title($object->name()) : ''; |
|
417 | - |
|
418 | - } |
|
419 | - } |
|
420 | - return $prefix . $class . $suffix; |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - |
|
425 | - /** |
|
426 | - * EEH_Template::format_currency |
|
427 | - * This helper takes a raw float value and formats it according to the default config country currency settings, or |
|
428 | - * the country currency settings from the supplied country ISO code |
|
429 | - * |
|
430 | - * @param float $amount raw money value |
|
431 | - * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
|
432 | - * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
|
433 | - * @param string $CNT_ISO 2 letter ISO code for a country |
|
434 | - * @param string $cur_code_span_class |
|
435 | - * @return string the html output for the formatted money value |
|
436 | - * @throws \EE_Error |
|
437 | - */ |
|
438 | - public static function format_currency( |
|
439 | - $amount = null, |
|
440 | - $return_raw = false, |
|
441 | - $display_code = true, |
|
442 | - $CNT_ISO = '', |
|
443 | - $cur_code_span_class = 'currency-code' |
|
444 | - ) { |
|
445 | - // ensure amount was received |
|
446 | - if ($amount === null) { |
|
447 | - $msg = __('In order to format currency, an amount needs to be passed.', 'event_espresso'); |
|
448 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
449 | - return ''; |
|
450 | - } |
|
451 | - //ensure amount is float |
|
452 | - $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float)$amount); |
|
453 | - $CNT_ISO = apply_filters('FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount); |
|
454 | - // filter raw amount (allows 0.00 to be changed to "free" for example) |
|
455 | - $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount', $amount, $return_raw); |
|
456 | - // still a number or was amount converted to a string like "free" ? |
|
457 | - if (is_float($amount_formatted)) { |
|
458 | - // was a country ISO code passed ? if so generate currency config object for that country |
|
459 | - $mny = $CNT_ISO !== '' ? new EE_Currency_Config($CNT_ISO) : null; |
|
460 | - // verify results |
|
461 | - if ( ! $mny instanceof EE_Currency_Config) { |
|
462 | - // set default config country currency settings |
|
463 | - $mny = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config |
|
464 | - ? EE_Registry::instance()->CFG->currency |
|
465 | - : new EE_Currency_Config(); |
|
466 | - } |
|
467 | - // format float |
|
468 | - $amount_formatted = number_format($amount, $mny->dec_plc, $mny->dec_mrk, $mny->thsnds); |
|
469 | - // add formatting ? |
|
470 | - if ( ! $return_raw) { |
|
471 | - // add currency sign |
|
472 | - if ($mny->sign_b4) { |
|
473 | - if ($amount >= 0) { |
|
474 | - $amount_formatted = $mny->sign . $amount_formatted; |
|
475 | - } else { |
|
476 | - $amount_formatted = '-' . $mny->sign . str_replace('-', '', $amount_formatted); |
|
477 | - } |
|
478 | - |
|
479 | - } else { |
|
480 | - $amount_formatted = $amount_formatted . $mny->sign; |
|
481 | - } |
|
482 | - |
|
483 | - // filter to allow global setting of display_code |
|
484 | - $display_code = apply_filters('FHEE__EEH_Template__format_currency__display_code', $display_code); |
|
485 | - |
|
486 | - // add currency code ? |
|
487 | - $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
|
488 | - } |
|
489 | - // filter results |
|
490 | - $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount_formatted', |
|
491 | - $amount_formatted, $mny, $return_raw); |
|
492 | - } |
|
493 | - // clean up vars |
|
494 | - unset($mny); |
|
495 | - // return formatted currency amount |
|
496 | - return $amount_formatted; |
|
497 | - } |
|
498 | - |
|
499 | - |
|
500 | - /** |
|
501 | - * This function is used for outputting the localized label for a given status id in the schema requested (and |
|
502 | - * possibly plural). The intended use of this function is only for cases where wanting a label outside of a |
|
503 | - * related status model or model object (i.e. in documentation etc.) |
|
504 | - * |
|
505 | - * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no |
|
506 | - * match, then 'Unknown' will be returned. |
|
507 | - * @param boolean $plural Whether to return plural or not |
|
508 | - * @param string $schema 'UPPER', 'lower', or 'Sentence' |
|
509 | - * @return string The localized label for the status id. |
|
510 | - */ |
|
511 | - public static function pretty_status($status_id, $plural = false, $schema = 'upper') |
|
512 | - { |
|
513 | - /** @type EEM_Status $EEM_Status */ |
|
514 | - $EEM_Status = EE_Registry::instance()->load_model('Status'); |
|
515 | - $status = $EEM_Status->localized_status(array($status_id => __('unknown', 'event_espresso')), $plural, |
|
516 | - $schema); |
|
517 | - return $status[$status_id]; |
|
518 | - } |
|
519 | - |
|
520 | - |
|
521 | - /** |
|
522 | - * This helper just returns a button or link for the given parameters |
|
523 | - * |
|
524 | - * @param string $url the url for the link |
|
525 | - * @param string $label What is the label you want displayed for the button |
|
526 | - * @param string $class what class is used for the button (defaults to 'button-primary') |
|
527 | - * @param string $icon |
|
528 | - * @param string $title |
|
529 | - * @return string the html output for the button |
|
530 | - */ |
|
531 | - public static function get_button_or_link($url, $label, $class = 'button-primary', $icon = '', $title = '') |
|
532 | - { |
|
533 | - $icon_html = ''; |
|
534 | - if ( ! empty($icon)) { |
|
535 | - $dashicons = preg_split("(ee-icon |dashicons )", $icon); |
|
536 | - $dashicons = array_filter($dashicons); |
|
537 | - $count = count($dashicons); |
|
538 | - $icon_html .= $count > 1 ? '<span class="ee-composite-dashicon">' : ''; |
|
539 | - foreach ($dashicons as $dashicon) { |
|
540 | - $type = strpos($dashicon, 'ee-icon') !== false ? 'ee-icon ' : 'dashicons '; |
|
541 | - $icon_html .= '<span class="' . $type . $dashicon . '"></span>'; |
|
542 | - } |
|
543 | - $icon_html .= $count > 1 ? '</span>' : ''; |
|
544 | - } |
|
545 | - $label = ! empty($icon) ? $icon_html . $label : $label; |
|
546 | - $button = '<a id="' . sanitize_title_with_dashes($label) . '" href="' . $url . '" class="' . $class . '" title="' . $title . '">' . $label . '</a>'; |
|
547 | - return $button; |
|
548 | - } |
|
549 | - |
|
550 | - |
|
551 | - /** |
|
552 | - * This returns a generated link that will load the related help tab on admin pages. |
|
553 | - * |
|
554 | - * @param string $help_tab_id the id for the connected help tab |
|
555 | - * @param bool|string $page The page identifier for the page the help tab is on |
|
556 | - * @param bool|string $action The action (route) for the admin page the help tab is on. |
|
557 | - * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
|
558 | - * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
|
559 | - * @return string generated link |
|
560 | - */ |
|
561 | - public static function get_help_tab_link( |
|
562 | - $help_tab_id, |
|
563 | - $page = false, |
|
564 | - $action = false, |
|
565 | - $icon_style = false, |
|
566 | - $help_text = false |
|
567 | - ) { |
|
568 | - |
|
569 | - if ( ! $page) { |
|
570 | - $page = isset($_REQUEST['page']) && ! empty($_REQUEST['page']) ? sanitize_key($_REQUEST['page']) : $page; |
|
571 | - } |
|
572 | - |
|
573 | - if ( ! $action) { |
|
574 | - $action = isset($_REQUEST['action']) && ! empty($_REQUEST['action']) ? sanitize_key($_REQUEST['action']) : $action; |
|
575 | - } |
|
576 | - |
|
577 | - $action = empty($action) ? 'default' : $action; |
|
578 | - |
|
579 | - |
|
580 | - $help_tab_lnk = $page . '-' . $action . '-' . $help_tab_id; |
|
581 | - $icon = ! $icon_style ? ' dashicons-editor-help' : $icon_style; |
|
582 | - $help_text = ! $help_text ? '' : $help_text; |
|
583 | - return '<a id="' . $help_tab_lnk . '" class="ee-clickable dashicons espresso-help-tab-lnk ee-icon-size-22' . $icon . '" title="' . esc_attr__('Click to open the \'Help\' tab for more information about this feature.', |
|
584 | - 'event_espresso') . '" > ' . $help_text . ' </a>'; |
|
585 | - } |
|
586 | - |
|
587 | - |
|
588 | - /** |
|
589 | - * This helper generates the html structure for the jquery joyride plugin with the given params. |
|
590 | - * |
|
591 | - * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
|
592 | - * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
|
593 | - * @param EE_Help_Tour |
|
594 | - * @return string html |
|
595 | - */ |
|
596 | - public static function help_tour_stops_generator(EE_Help_Tour $tour) |
|
597 | - { |
|
598 | - $id = $tour->get_slug(); |
|
599 | - $stops = $tour->get_stops(); |
|
600 | - |
|
601 | - $content = '<ol style="display:none" id="' . $id . '">'; |
|
602 | - |
|
603 | - foreach ($stops as $stop) { |
|
604 | - $data_id = ! empty($stop['id']) ? ' data-id="' . $stop['id'] . '"' : ''; |
|
605 | - $data_class = empty($data_id) && ! empty($stop['class']) ? ' data-class="' . $stop['class'] . '"' : ''; |
|
606 | - |
|
607 | - //if container is set to modal then let's make sure we set the options accordingly |
|
608 | - if (empty($data_id) && empty($data_class)) { |
|
609 | - $stop['options']['modal'] = true; |
|
610 | - $stop['options']['expose'] = true; |
|
611 | - } |
|
612 | - |
|
613 | - $custom_class = ! empty($stop['custom_class']) ? ' class="' . $stop['custom_class'] . '"' : ''; |
|
614 | - $button_text = ! empty($stop['button_text']) ? ' data-button="' . $stop['button_text'] . '"' : ''; |
|
615 | - $inner_content = isset($stop['content']) ? $stop['content'] : ''; |
|
616 | - |
|
617 | - //options |
|
618 | - if (isset($stop['options']) && is_array($stop['options'])) { |
|
619 | - $options = ' data-options="'; |
|
620 | - foreach ($stop['options'] as $option => $value) { |
|
621 | - $options .= $option . ':' . $value . ';'; |
|
622 | - } |
|
623 | - $options .= '"'; |
|
624 | - } else { |
|
625 | - $options = ''; |
|
626 | - } |
|
627 | - |
|
628 | - //let's put all together |
|
629 | - $content .= '<li' . $data_id . $data_class . $custom_class . $button_text . $options . '>' . $inner_content . '</li>'; |
|
630 | - } |
|
631 | - |
|
632 | - $content .= '</ol>'; |
|
633 | - return $content; |
|
634 | - } |
|
635 | - |
|
636 | - |
|
637 | - /** |
|
638 | - * This is a helper method to generate a status legend for a given status array. |
|
639 | - * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods |
|
640 | - * status_array. |
|
641 | - * |
|
642 | - * @param array $status_array array of statuses that will make up the legend. In format: |
|
643 | - * array( |
|
644 | - * 'status_item' => 'status_name' |
|
645 | - * ) |
|
646 | - * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in |
|
647 | - * the legend. |
|
648 | - * @throws EE_Error |
|
649 | - * @return string html structure for status. |
|
650 | - */ |
|
651 | - public static function status_legend($status_array, $active_status = '') |
|
652 | - { |
|
653 | - if ( ! is_array($status_array)) { |
|
654 | - throw new EE_Error(esc_html__('The EEH_Template::status_legend helper required the incoming status_array argument to be an array!', |
|
655 | - 'event_espresso')); |
|
656 | - } |
|
657 | - |
|
658 | - $setup_array = array(); |
|
659 | - foreach ($status_array as $item => $status) { |
|
660 | - $setup_array[$item] = array( |
|
661 | - 'class' => 'ee-status-legend ee-status-legend-' . $status, |
|
662 | - 'desc' => EEH_Template::pretty_status($status, false, 'sentence'), |
|
663 | - 'status' => $status, |
|
664 | - ); |
|
665 | - } |
|
666 | - |
|
667 | - $content = '<div class="ee-list-table-legend-container">' . "\n"; |
|
668 | - $content .= '<h4 class="status-legend-title">' . esc_html__('Status Legend', 'event_espresso') . '</h4>' . "\n"; |
|
669 | - $content .= '<dl class="ee-list-table-legend">' . "\n\t"; |
|
670 | - foreach ($setup_array as $item => $details) { |
|
671 | - $active_class = $active_status == $details['status'] ? ' class="ee-is-active-status"' : ''; |
|
672 | - $content .= '<dt id="ee-legend-item-tooltip-' . $item . '"' . $active_class . '>' . "\n\t\t"; |
|
673 | - $content .= '<span class="' . $details['class'] . '"></span>' . "\n\t\t"; |
|
674 | - $content .= '<span class="ee-legend-description">' . $details['desc'] . '</span>' . "\n\t"; |
|
675 | - $content .= '</dt>' . "\n"; |
|
676 | - } |
|
677 | - $content .= '</dl>' . "\n"; |
|
678 | - $content .= '</div>' . "\n"; |
|
679 | - return $content; |
|
680 | - } |
|
681 | - |
|
682 | - |
|
683 | - /** |
|
684 | - * Gets HTML for laying out a deeply-nested array (and objects) in a format |
|
685 | - * that's nice for presenting in the wp admin |
|
686 | - * |
|
687 | - * @param mixed $data |
|
688 | - * @return string |
|
689 | - */ |
|
690 | - public static function layout_array_as_table($data) |
|
691 | - { |
|
692 | - if (is_object($data) || $data instanceof __PHP_Incomplete_Class) { |
|
693 | - $data = (array)$data; |
|
694 | - } |
|
695 | - ob_start(); |
|
696 | - if (is_array($data)) { |
|
697 | - if (EEH_Array::is_associative_array($data)) { |
|
698 | - ?> |
|
341 | + $template_path = false, |
|
342 | + $template_args = array(), |
|
343 | + $return_string = false, |
|
344 | + $throw_exceptions = false |
|
345 | + ) { |
|
346 | + |
|
347 | + /** |
|
348 | + * These two filters are intended for last minute changes to templates being loaded and/or template arg |
|
349 | + * modifications. NOTE... modifying these things can cause breakage as most templates running through |
|
350 | + * the display_template method are templates we DON'T want modified (usually because of js |
|
351 | + * dependencies etc). So unless you know what you are doing, do NOT filter templates or template args |
|
352 | + * using this. |
|
353 | + * |
|
354 | + * @since 4.6.0 |
|
355 | + */ |
|
356 | + $template_path = (string) apply_filters('FHEE__EEH_Template__display_template__template_path', $template_path); |
|
357 | + $template_args = (array) apply_filters('FHEE__EEH_Template__display_template__template_args', $template_args); |
|
358 | + |
|
359 | + // you gimme nuttin - YOU GET NUTTIN !! |
|
360 | + if ( ! $template_path || ! is_readable($template_path)) { |
|
361 | + return ''; |
|
362 | + } |
|
363 | + // if $template_args are not in an array, then make it so |
|
364 | + if ( ! is_array($template_args) && ! is_object($template_args)) { |
|
365 | + $template_args = array($template_args); |
|
366 | + } |
|
367 | + extract( $template_args, EXTR_SKIP ); |
|
368 | + // ignore whether template is accessible ? |
|
369 | + if ( $throw_exceptions && ! is_readable( $template_path ) ) { |
|
370 | + throw new \DomainException( |
|
371 | + esc_html__( |
|
372 | + 'Invalid, unreadable, or missing file.', |
|
373 | + 'event_espresso' |
|
374 | + ) |
|
375 | + ); |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + if ($return_string) { |
|
380 | + // because we want to return a string, we are going to capture the output |
|
381 | + ob_start(); |
|
382 | + include($template_path); |
|
383 | + return ob_get_clean(); |
|
384 | + } else { |
|
385 | + include($template_path); |
|
386 | + } |
|
387 | + return ''; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + /** |
|
392 | + * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
|
393 | + * |
|
394 | + * @param EE_Base_Class $object the EE object the css class is being generated for |
|
395 | + * @param string $prefix added to the beginning of the generated class |
|
396 | + * @param string $suffix added to the end of the generated class |
|
397 | + * @return string |
|
398 | + */ |
|
399 | + public static function get_object_css_class($object = null, $prefix = '', $suffix = '') |
|
400 | + { |
|
401 | + // in the beginning... |
|
402 | + $prefix = ! empty($prefix) ? rtrim($prefix, '-') . '-' : ''; |
|
403 | + // da muddle |
|
404 | + $class = ''; |
|
405 | + // the end |
|
406 | + $suffix = ! empty($suffix) ? '-' . ltrim($suffix, '-') : ''; |
|
407 | + // is the passed object an EE object ? |
|
408 | + if ($object instanceof EE_Base_Class) { |
|
409 | + // grab the exact type of object |
|
410 | + $obj_class = get_class($object); |
|
411 | + // depending on the type of object... |
|
412 | + switch ($obj_class) { |
|
413 | + // no specifics just yet... |
|
414 | + default : |
|
415 | + $class = strtolower(str_replace('_', '-', $obj_class)); |
|
416 | + $class .= method_exists($obj_class, 'name') ? '-' . sanitize_title($object->name()) : ''; |
|
417 | + |
|
418 | + } |
|
419 | + } |
|
420 | + return $prefix . $class . $suffix; |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + |
|
425 | + /** |
|
426 | + * EEH_Template::format_currency |
|
427 | + * This helper takes a raw float value and formats it according to the default config country currency settings, or |
|
428 | + * the country currency settings from the supplied country ISO code |
|
429 | + * |
|
430 | + * @param float $amount raw money value |
|
431 | + * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
|
432 | + * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
|
433 | + * @param string $CNT_ISO 2 letter ISO code for a country |
|
434 | + * @param string $cur_code_span_class |
|
435 | + * @return string the html output for the formatted money value |
|
436 | + * @throws \EE_Error |
|
437 | + */ |
|
438 | + public static function format_currency( |
|
439 | + $amount = null, |
|
440 | + $return_raw = false, |
|
441 | + $display_code = true, |
|
442 | + $CNT_ISO = '', |
|
443 | + $cur_code_span_class = 'currency-code' |
|
444 | + ) { |
|
445 | + // ensure amount was received |
|
446 | + if ($amount === null) { |
|
447 | + $msg = __('In order to format currency, an amount needs to be passed.', 'event_espresso'); |
|
448 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
449 | + return ''; |
|
450 | + } |
|
451 | + //ensure amount is float |
|
452 | + $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float)$amount); |
|
453 | + $CNT_ISO = apply_filters('FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount); |
|
454 | + // filter raw amount (allows 0.00 to be changed to "free" for example) |
|
455 | + $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount', $amount, $return_raw); |
|
456 | + // still a number or was amount converted to a string like "free" ? |
|
457 | + if (is_float($amount_formatted)) { |
|
458 | + // was a country ISO code passed ? if so generate currency config object for that country |
|
459 | + $mny = $CNT_ISO !== '' ? new EE_Currency_Config($CNT_ISO) : null; |
|
460 | + // verify results |
|
461 | + if ( ! $mny instanceof EE_Currency_Config) { |
|
462 | + // set default config country currency settings |
|
463 | + $mny = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config |
|
464 | + ? EE_Registry::instance()->CFG->currency |
|
465 | + : new EE_Currency_Config(); |
|
466 | + } |
|
467 | + // format float |
|
468 | + $amount_formatted = number_format($amount, $mny->dec_plc, $mny->dec_mrk, $mny->thsnds); |
|
469 | + // add formatting ? |
|
470 | + if ( ! $return_raw) { |
|
471 | + // add currency sign |
|
472 | + if ($mny->sign_b4) { |
|
473 | + if ($amount >= 0) { |
|
474 | + $amount_formatted = $mny->sign . $amount_formatted; |
|
475 | + } else { |
|
476 | + $amount_formatted = '-' . $mny->sign . str_replace('-', '', $amount_formatted); |
|
477 | + } |
|
478 | + |
|
479 | + } else { |
|
480 | + $amount_formatted = $amount_formatted . $mny->sign; |
|
481 | + } |
|
482 | + |
|
483 | + // filter to allow global setting of display_code |
|
484 | + $display_code = apply_filters('FHEE__EEH_Template__format_currency__display_code', $display_code); |
|
485 | + |
|
486 | + // add currency code ? |
|
487 | + $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
|
488 | + } |
|
489 | + // filter results |
|
490 | + $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount_formatted', |
|
491 | + $amount_formatted, $mny, $return_raw); |
|
492 | + } |
|
493 | + // clean up vars |
|
494 | + unset($mny); |
|
495 | + // return formatted currency amount |
|
496 | + return $amount_formatted; |
|
497 | + } |
|
498 | + |
|
499 | + |
|
500 | + /** |
|
501 | + * This function is used for outputting the localized label for a given status id in the schema requested (and |
|
502 | + * possibly plural). The intended use of this function is only for cases where wanting a label outside of a |
|
503 | + * related status model or model object (i.e. in documentation etc.) |
|
504 | + * |
|
505 | + * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no |
|
506 | + * match, then 'Unknown' will be returned. |
|
507 | + * @param boolean $plural Whether to return plural or not |
|
508 | + * @param string $schema 'UPPER', 'lower', or 'Sentence' |
|
509 | + * @return string The localized label for the status id. |
|
510 | + */ |
|
511 | + public static function pretty_status($status_id, $plural = false, $schema = 'upper') |
|
512 | + { |
|
513 | + /** @type EEM_Status $EEM_Status */ |
|
514 | + $EEM_Status = EE_Registry::instance()->load_model('Status'); |
|
515 | + $status = $EEM_Status->localized_status(array($status_id => __('unknown', 'event_espresso')), $plural, |
|
516 | + $schema); |
|
517 | + return $status[$status_id]; |
|
518 | + } |
|
519 | + |
|
520 | + |
|
521 | + /** |
|
522 | + * This helper just returns a button or link for the given parameters |
|
523 | + * |
|
524 | + * @param string $url the url for the link |
|
525 | + * @param string $label What is the label you want displayed for the button |
|
526 | + * @param string $class what class is used for the button (defaults to 'button-primary') |
|
527 | + * @param string $icon |
|
528 | + * @param string $title |
|
529 | + * @return string the html output for the button |
|
530 | + */ |
|
531 | + public static function get_button_or_link($url, $label, $class = 'button-primary', $icon = '', $title = '') |
|
532 | + { |
|
533 | + $icon_html = ''; |
|
534 | + if ( ! empty($icon)) { |
|
535 | + $dashicons = preg_split("(ee-icon |dashicons )", $icon); |
|
536 | + $dashicons = array_filter($dashicons); |
|
537 | + $count = count($dashicons); |
|
538 | + $icon_html .= $count > 1 ? '<span class="ee-composite-dashicon">' : ''; |
|
539 | + foreach ($dashicons as $dashicon) { |
|
540 | + $type = strpos($dashicon, 'ee-icon') !== false ? 'ee-icon ' : 'dashicons '; |
|
541 | + $icon_html .= '<span class="' . $type . $dashicon . '"></span>'; |
|
542 | + } |
|
543 | + $icon_html .= $count > 1 ? '</span>' : ''; |
|
544 | + } |
|
545 | + $label = ! empty($icon) ? $icon_html . $label : $label; |
|
546 | + $button = '<a id="' . sanitize_title_with_dashes($label) . '" href="' . $url . '" class="' . $class . '" title="' . $title . '">' . $label . '</a>'; |
|
547 | + return $button; |
|
548 | + } |
|
549 | + |
|
550 | + |
|
551 | + /** |
|
552 | + * This returns a generated link that will load the related help tab on admin pages. |
|
553 | + * |
|
554 | + * @param string $help_tab_id the id for the connected help tab |
|
555 | + * @param bool|string $page The page identifier for the page the help tab is on |
|
556 | + * @param bool|string $action The action (route) for the admin page the help tab is on. |
|
557 | + * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
|
558 | + * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
|
559 | + * @return string generated link |
|
560 | + */ |
|
561 | + public static function get_help_tab_link( |
|
562 | + $help_tab_id, |
|
563 | + $page = false, |
|
564 | + $action = false, |
|
565 | + $icon_style = false, |
|
566 | + $help_text = false |
|
567 | + ) { |
|
568 | + |
|
569 | + if ( ! $page) { |
|
570 | + $page = isset($_REQUEST['page']) && ! empty($_REQUEST['page']) ? sanitize_key($_REQUEST['page']) : $page; |
|
571 | + } |
|
572 | + |
|
573 | + if ( ! $action) { |
|
574 | + $action = isset($_REQUEST['action']) && ! empty($_REQUEST['action']) ? sanitize_key($_REQUEST['action']) : $action; |
|
575 | + } |
|
576 | + |
|
577 | + $action = empty($action) ? 'default' : $action; |
|
578 | + |
|
579 | + |
|
580 | + $help_tab_lnk = $page . '-' . $action . '-' . $help_tab_id; |
|
581 | + $icon = ! $icon_style ? ' dashicons-editor-help' : $icon_style; |
|
582 | + $help_text = ! $help_text ? '' : $help_text; |
|
583 | + return '<a id="' . $help_tab_lnk . '" class="ee-clickable dashicons espresso-help-tab-lnk ee-icon-size-22' . $icon . '" title="' . esc_attr__('Click to open the \'Help\' tab for more information about this feature.', |
|
584 | + 'event_espresso') . '" > ' . $help_text . ' </a>'; |
|
585 | + } |
|
586 | + |
|
587 | + |
|
588 | + /** |
|
589 | + * This helper generates the html structure for the jquery joyride plugin with the given params. |
|
590 | + * |
|
591 | + * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
|
592 | + * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
|
593 | + * @param EE_Help_Tour |
|
594 | + * @return string html |
|
595 | + */ |
|
596 | + public static function help_tour_stops_generator(EE_Help_Tour $tour) |
|
597 | + { |
|
598 | + $id = $tour->get_slug(); |
|
599 | + $stops = $tour->get_stops(); |
|
600 | + |
|
601 | + $content = '<ol style="display:none" id="' . $id . '">'; |
|
602 | + |
|
603 | + foreach ($stops as $stop) { |
|
604 | + $data_id = ! empty($stop['id']) ? ' data-id="' . $stop['id'] . '"' : ''; |
|
605 | + $data_class = empty($data_id) && ! empty($stop['class']) ? ' data-class="' . $stop['class'] . '"' : ''; |
|
606 | + |
|
607 | + //if container is set to modal then let's make sure we set the options accordingly |
|
608 | + if (empty($data_id) && empty($data_class)) { |
|
609 | + $stop['options']['modal'] = true; |
|
610 | + $stop['options']['expose'] = true; |
|
611 | + } |
|
612 | + |
|
613 | + $custom_class = ! empty($stop['custom_class']) ? ' class="' . $stop['custom_class'] . '"' : ''; |
|
614 | + $button_text = ! empty($stop['button_text']) ? ' data-button="' . $stop['button_text'] . '"' : ''; |
|
615 | + $inner_content = isset($stop['content']) ? $stop['content'] : ''; |
|
616 | + |
|
617 | + //options |
|
618 | + if (isset($stop['options']) && is_array($stop['options'])) { |
|
619 | + $options = ' data-options="'; |
|
620 | + foreach ($stop['options'] as $option => $value) { |
|
621 | + $options .= $option . ':' . $value . ';'; |
|
622 | + } |
|
623 | + $options .= '"'; |
|
624 | + } else { |
|
625 | + $options = ''; |
|
626 | + } |
|
627 | + |
|
628 | + //let's put all together |
|
629 | + $content .= '<li' . $data_id . $data_class . $custom_class . $button_text . $options . '>' . $inner_content . '</li>'; |
|
630 | + } |
|
631 | + |
|
632 | + $content .= '</ol>'; |
|
633 | + return $content; |
|
634 | + } |
|
635 | + |
|
636 | + |
|
637 | + /** |
|
638 | + * This is a helper method to generate a status legend for a given status array. |
|
639 | + * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods |
|
640 | + * status_array. |
|
641 | + * |
|
642 | + * @param array $status_array array of statuses that will make up the legend. In format: |
|
643 | + * array( |
|
644 | + * 'status_item' => 'status_name' |
|
645 | + * ) |
|
646 | + * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in |
|
647 | + * the legend. |
|
648 | + * @throws EE_Error |
|
649 | + * @return string html structure for status. |
|
650 | + */ |
|
651 | + public static function status_legend($status_array, $active_status = '') |
|
652 | + { |
|
653 | + if ( ! is_array($status_array)) { |
|
654 | + throw new EE_Error(esc_html__('The EEH_Template::status_legend helper required the incoming status_array argument to be an array!', |
|
655 | + 'event_espresso')); |
|
656 | + } |
|
657 | + |
|
658 | + $setup_array = array(); |
|
659 | + foreach ($status_array as $item => $status) { |
|
660 | + $setup_array[$item] = array( |
|
661 | + 'class' => 'ee-status-legend ee-status-legend-' . $status, |
|
662 | + 'desc' => EEH_Template::pretty_status($status, false, 'sentence'), |
|
663 | + 'status' => $status, |
|
664 | + ); |
|
665 | + } |
|
666 | + |
|
667 | + $content = '<div class="ee-list-table-legend-container">' . "\n"; |
|
668 | + $content .= '<h4 class="status-legend-title">' . esc_html__('Status Legend', 'event_espresso') . '</h4>' . "\n"; |
|
669 | + $content .= '<dl class="ee-list-table-legend">' . "\n\t"; |
|
670 | + foreach ($setup_array as $item => $details) { |
|
671 | + $active_class = $active_status == $details['status'] ? ' class="ee-is-active-status"' : ''; |
|
672 | + $content .= '<dt id="ee-legend-item-tooltip-' . $item . '"' . $active_class . '>' . "\n\t\t"; |
|
673 | + $content .= '<span class="' . $details['class'] . '"></span>' . "\n\t\t"; |
|
674 | + $content .= '<span class="ee-legend-description">' . $details['desc'] . '</span>' . "\n\t"; |
|
675 | + $content .= '</dt>' . "\n"; |
|
676 | + } |
|
677 | + $content .= '</dl>' . "\n"; |
|
678 | + $content .= '</div>' . "\n"; |
|
679 | + return $content; |
|
680 | + } |
|
681 | + |
|
682 | + |
|
683 | + /** |
|
684 | + * Gets HTML for laying out a deeply-nested array (and objects) in a format |
|
685 | + * that's nice for presenting in the wp admin |
|
686 | + * |
|
687 | + * @param mixed $data |
|
688 | + * @return string |
|
689 | + */ |
|
690 | + public static function layout_array_as_table($data) |
|
691 | + { |
|
692 | + if (is_object($data) || $data instanceof __PHP_Incomplete_Class) { |
|
693 | + $data = (array)$data; |
|
694 | + } |
|
695 | + ob_start(); |
|
696 | + if (is_array($data)) { |
|
697 | + if (EEH_Array::is_associative_array($data)) { |
|
698 | + ?> |
|
699 | 699 | <table class="widefat"> |
700 | 700 | <tbody> |
701 | 701 | <?php |
702 | - foreach ($data as $data_key => $data_values) { |
|
703 | - ?> |
|
702 | + foreach ($data as $data_key => $data_values) { |
|
703 | + ?> |
|
704 | 704 | <tr> |
705 | 705 | <td> |
706 | 706 | <?php echo $data_key; ?> |
@@ -710,248 +710,248 @@ discard block |
||
710 | 710 | </td> |
711 | 711 | </tr> |
712 | 712 | <?php |
713 | - } ?> |
|
713 | + } ?> |
|
714 | 714 | </tbody> |
715 | 715 | </table> |
716 | 716 | <?php |
717 | - } else { |
|
718 | - ?> |
|
717 | + } else { |
|
718 | + ?> |
|
719 | 719 | <ul> |
720 | 720 | <?php |
721 | - foreach ($data as $datum) { |
|
722 | - echo "<li>"; |
|
723 | - echo self::layout_array_as_table($datum); |
|
724 | - echo "</li>"; |
|
725 | - } ?> |
|
721 | + foreach ($data as $datum) { |
|
722 | + echo "<li>"; |
|
723 | + echo self::layout_array_as_table($datum); |
|
724 | + echo "</li>"; |
|
725 | + } ?> |
|
726 | 726 | </ul> |
727 | 727 | <?php |
728 | - } |
|
729 | - } else { |
|
730 | - //simple value |
|
731 | - echo esc_html($data); |
|
732 | - } |
|
733 | - return ob_get_clean(); |
|
734 | - } |
|
735 | - |
|
736 | - |
|
737 | - /** |
|
738 | - * wrapper for self::get_paging_html() that simply echos the generated paging html |
|
739 | - * |
|
740 | - * @since 4.4.0 |
|
741 | - * @see self:get_paging_html() for argument docs. |
|
742 | - * @param $total_items |
|
743 | - * @param $current |
|
744 | - * @param $per_page |
|
745 | - * @param $url |
|
746 | - * @param bool $show_num_field |
|
747 | - * @param string $paged_arg_name |
|
748 | - * @param array $items_label |
|
749 | - * @return string |
|
750 | - */ |
|
751 | - public static function paging_html( |
|
752 | - $total_items, |
|
753 | - $current, |
|
754 | - $per_page, |
|
755 | - $url, |
|
756 | - $show_num_field = true, |
|
757 | - $paged_arg_name = 'paged', |
|
758 | - $items_label = array() |
|
759 | - ) { |
|
760 | - echo self::get_paging_html($total_items, $current, $per_page, $url, $show_num_field, $paged_arg_name, |
|
761 | - $items_label); |
|
762 | - } |
|
763 | - |
|
764 | - |
|
765 | - /** |
|
766 | - * A method for generating paging similar to WP_List_Table |
|
767 | - * |
|
768 | - * @since 4.4.0 |
|
769 | - * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
|
770 | - * @param integer $total_items How many total items there are to page. |
|
771 | - * @param integer $current What the current page is. |
|
772 | - * @param integer $per_page How many items per page. |
|
773 | - * @param string $url What the base url for page links is. |
|
774 | - * @param boolean $show_num_field Whether to show the input for changing page number. |
|
775 | - * @param string $paged_arg_name The name of the key for the paged query argument. |
|
776 | - * @param array $items_label An array of singular/plural values for the items label: |
|
777 | - * array( |
|
778 | - * 'single' => 'item', |
|
779 | - * 'plural' => 'items' |
|
780 | - * ) |
|
781 | - * @return string |
|
782 | - */ |
|
783 | - public static function get_paging_html( |
|
784 | - $total_items, |
|
785 | - $current, |
|
786 | - $per_page, |
|
787 | - $url, |
|
788 | - $show_num_field = true, |
|
789 | - $paged_arg_name = 'paged', |
|
790 | - $items_label = array() |
|
791 | - ) { |
|
792 | - $page_links = array(); |
|
793 | - $disable_first = $disable_last = ''; |
|
794 | - $total_items = (int)$total_items; |
|
795 | - $per_page = (int)$per_page; |
|
796 | - $current = (int)$current; |
|
797 | - $paged_arg_name = empty($paged_arg_name) ? 'paged' : sanitize_key($paged_arg_name); |
|
798 | - |
|
799 | - //filter items_label |
|
800 | - $items_label = apply_filters( |
|
801 | - 'FHEE__EEH_Template__get_paging_html__items_label', |
|
802 | - $items_label |
|
803 | - ); |
|
804 | - |
|
805 | - if (empty($items_label) |
|
806 | - || ! is_array($items_label) |
|
807 | - || ! isset($items_label['single']) |
|
808 | - || ! isset($items_label['plural']) |
|
809 | - ) { |
|
810 | - $items_label = array( |
|
811 | - 'single' => __('1 item', 'event_espresso'), |
|
812 | - 'plural' => __('%s items', 'event_espresso'), |
|
813 | - ); |
|
814 | - } else { |
|
815 | - $items_label = array( |
|
816 | - 'single' => '1 ' . esc_html($items_label['single']), |
|
817 | - 'plural' => '%s ' . esc_html($items_label['plural']), |
|
818 | - ); |
|
819 | - } |
|
820 | - |
|
821 | - $total_pages = ceil($total_items / $per_page); |
|
822 | - |
|
823 | - if ($total_pages <= 1) { |
|
824 | - return ''; |
|
825 | - } |
|
826 | - |
|
827 | - $item_label = $total_items > 1 ? sprintf($items_label['plural'], $total_items) : $items_label['single']; |
|
828 | - |
|
829 | - $output = '<span class="displaying-num">' . $item_label . '</span>'; |
|
830 | - |
|
831 | - if ($current === 1) { |
|
832 | - $disable_first = ' disabled'; |
|
833 | - } |
|
834 | - if ($current == $total_pages) { |
|
835 | - $disable_last = ' disabled'; |
|
836 | - } |
|
837 | - |
|
838 | - $page_links[] = sprintf("<a class='%s' title='%s' href='%s'>%s</a>", |
|
839 | - 'first-page' . $disable_first, |
|
840 | - esc_attr__('Go to the first page'), |
|
841 | - esc_url(remove_query_arg($paged_arg_name, $url)), |
|
842 | - '«' |
|
843 | - ); |
|
844 | - |
|
845 | - $page_links[] = sprintf( |
|
846 | - '<a class="%s" title="%s" href="%s">%s</a>', |
|
847 | - 'prev-page' . $disable_first, |
|
848 | - esc_attr__('Go to the previous page'), |
|
849 | - esc_url(add_query_arg($paged_arg_name, max(1, $current - 1), $url)), |
|
850 | - '‹' |
|
851 | - ); |
|
852 | - |
|
853 | - if ( ! $show_num_field) { |
|
854 | - $html_current_page = $current; |
|
855 | - } else { |
|
856 | - $html_current_page = sprintf("<input class='current-page' title='%s' type='text' name=$paged_arg_name value='%s' size='%d' />", |
|
857 | - esc_attr__('Current page'), |
|
858 | - $current, |
|
859 | - strlen($total_pages) |
|
860 | - ); |
|
861 | - } |
|
862 | - |
|
863 | - $html_total_pages = sprintf( |
|
864 | - '<span class="total-pages">%s</span>', |
|
865 | - number_format_i18n($total_pages) |
|
866 | - ); |
|
867 | - $page_links[] = sprintf( |
|
868 | - _x('%3$s%1$s of %2$s%4$s', 'paging'), |
|
869 | - $html_current_page, |
|
870 | - $html_total_pages, |
|
871 | - '<span class="paging-input">', |
|
872 | - '</span>' |
|
873 | - ); |
|
874 | - |
|
875 | - $page_links[] = sprintf( |
|
876 | - '<a class="%s" title="%s" href="%s">%s</a>', |
|
877 | - 'next-page' . $disable_last, |
|
878 | - esc_attr__('Go to the next page'), |
|
879 | - esc_url(add_query_arg($paged_arg_name, min($total_pages, $current + 1), $url)), |
|
880 | - '›' |
|
881 | - ); |
|
882 | - |
|
883 | - $page_links[] = sprintf( |
|
884 | - '<a class="%s" title="%s" href="%s">%s</a>', |
|
885 | - 'last-page' . $disable_last, |
|
886 | - esc_attr__('Go to the last page'), |
|
887 | - esc_url(add_query_arg($paged_arg_name, $total_pages, $url)), |
|
888 | - '»' |
|
889 | - ); |
|
890 | - |
|
891 | - $output .= "\n" . '<span class="pagination-links">' . join("\n", $page_links) . '</span>'; |
|
892 | - // set page class |
|
893 | - if ($total_pages) { |
|
894 | - $page_class = $total_pages < 2 ? ' one-page' : ''; |
|
895 | - } else { |
|
896 | - $page_class = ' no-pages'; |
|
897 | - } |
|
898 | - |
|
899 | - return '<div class="tablenav"><div class="tablenav-pages' . $page_class . '">' . $output . '</div></div>'; |
|
900 | - } |
|
901 | - |
|
902 | - |
|
903 | - /** |
|
904 | - * @param string $wrap_class |
|
905 | - * @param string $wrap_id |
|
906 | - * @return string |
|
907 | - */ |
|
908 | - public static function powered_by_event_espresso($wrap_class = '', $wrap_id = '', array $query_args = array()) |
|
909 | - { |
|
910 | - $admin = is_admin() && ! (defined('DOING_AJAX') && DOING_AJAX); |
|
911 | - if ( |
|
912 | - ! $admin && |
|
913 | - ! apply_filters( |
|
914 | - 'FHEE__EEH_Template__powered_by_event_espresso__show_reg_footer', |
|
915 | - EE_Registry::instance()->CFG->admin->show_reg_footer |
|
916 | - ) |
|
917 | - ) { |
|
918 | - return ''; |
|
919 | - } |
|
920 | - $tag = $admin ? 'span' : 'div'; |
|
921 | - $attributes = ! empty($wrap_id) ? " id=\"{$wrap_id}\"" : ''; |
|
922 | - $wrap_class = $admin ? "{$wrap_class} float-left" : $wrap_class; |
|
923 | - $attributes .= ! empty($wrap_class) |
|
924 | - ? " class=\"{$wrap_class} powered-by-event-espresso-credit\"" |
|
925 | - : ' class="powered-by-event-espresso-credit"'; |
|
926 | - $query_args = array_merge( |
|
927 | - array( |
|
928 | - 'ap_id' => EE_Registry::instance()->CFG->admin->affiliate_id(), |
|
929 | - 'utm_source' => 'powered_by_event_espresso', |
|
930 | - 'utm_medium' => 'link', |
|
931 | - 'utm_campaign' => 'powered_by', |
|
932 | - ), |
|
933 | - $query_args |
|
934 | - ); |
|
935 | - $powered_by = apply_filters('FHEE__EEH_Template__powered_by_event_espresso_text', |
|
936 | - $admin ? 'Event Espresso - ' . EVENT_ESPRESSO_VERSION : 'Event Espresso'); |
|
937 | - $url = add_query_arg($query_args, 'https://eventespresso.com/'); |
|
938 | - $url = apply_filters('FHEE__EEH_Template__powered_by_event_espresso__url', $url); |
|
939 | - return (string)apply_filters( |
|
940 | - 'FHEE__EEH_Template__powered_by_event_espresso__html', |
|
941 | - sprintf( |
|
942 | - esc_html_x( |
|
943 | - '%3$s%1$sOnline event registration and ticketing powered by %2$s%3$s', |
|
944 | - 'Online event registration and ticketing powered by [link to eventespresso.com]', |
|
945 | - 'event_espresso' |
|
946 | - ), |
|
947 | - "<{$tag}{$attributes}>", |
|
948 | - "<a href=\"{$url}\" target=\"_blank\" rel=\"nofollow\">{$powered_by}</a></{$tag}>", |
|
949 | - $admin ? '' : '<br />' |
|
950 | - ), |
|
951 | - $wrap_class, |
|
952 | - $wrap_id |
|
953 | - ); |
|
954 | - } |
|
728 | + } |
|
729 | + } else { |
|
730 | + //simple value |
|
731 | + echo esc_html($data); |
|
732 | + } |
|
733 | + return ob_get_clean(); |
|
734 | + } |
|
735 | + |
|
736 | + |
|
737 | + /** |
|
738 | + * wrapper for self::get_paging_html() that simply echos the generated paging html |
|
739 | + * |
|
740 | + * @since 4.4.0 |
|
741 | + * @see self:get_paging_html() for argument docs. |
|
742 | + * @param $total_items |
|
743 | + * @param $current |
|
744 | + * @param $per_page |
|
745 | + * @param $url |
|
746 | + * @param bool $show_num_field |
|
747 | + * @param string $paged_arg_name |
|
748 | + * @param array $items_label |
|
749 | + * @return string |
|
750 | + */ |
|
751 | + public static function paging_html( |
|
752 | + $total_items, |
|
753 | + $current, |
|
754 | + $per_page, |
|
755 | + $url, |
|
756 | + $show_num_field = true, |
|
757 | + $paged_arg_name = 'paged', |
|
758 | + $items_label = array() |
|
759 | + ) { |
|
760 | + echo self::get_paging_html($total_items, $current, $per_page, $url, $show_num_field, $paged_arg_name, |
|
761 | + $items_label); |
|
762 | + } |
|
763 | + |
|
764 | + |
|
765 | + /** |
|
766 | + * A method for generating paging similar to WP_List_Table |
|
767 | + * |
|
768 | + * @since 4.4.0 |
|
769 | + * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
|
770 | + * @param integer $total_items How many total items there are to page. |
|
771 | + * @param integer $current What the current page is. |
|
772 | + * @param integer $per_page How many items per page. |
|
773 | + * @param string $url What the base url for page links is. |
|
774 | + * @param boolean $show_num_field Whether to show the input for changing page number. |
|
775 | + * @param string $paged_arg_name The name of the key for the paged query argument. |
|
776 | + * @param array $items_label An array of singular/plural values for the items label: |
|
777 | + * array( |
|
778 | + * 'single' => 'item', |
|
779 | + * 'plural' => 'items' |
|
780 | + * ) |
|
781 | + * @return string |
|
782 | + */ |
|
783 | + public static function get_paging_html( |
|
784 | + $total_items, |
|
785 | + $current, |
|
786 | + $per_page, |
|
787 | + $url, |
|
788 | + $show_num_field = true, |
|
789 | + $paged_arg_name = 'paged', |
|
790 | + $items_label = array() |
|
791 | + ) { |
|
792 | + $page_links = array(); |
|
793 | + $disable_first = $disable_last = ''; |
|
794 | + $total_items = (int)$total_items; |
|
795 | + $per_page = (int)$per_page; |
|
796 | + $current = (int)$current; |
|
797 | + $paged_arg_name = empty($paged_arg_name) ? 'paged' : sanitize_key($paged_arg_name); |
|
798 | + |
|
799 | + //filter items_label |
|
800 | + $items_label = apply_filters( |
|
801 | + 'FHEE__EEH_Template__get_paging_html__items_label', |
|
802 | + $items_label |
|
803 | + ); |
|
804 | + |
|
805 | + if (empty($items_label) |
|
806 | + || ! is_array($items_label) |
|
807 | + || ! isset($items_label['single']) |
|
808 | + || ! isset($items_label['plural']) |
|
809 | + ) { |
|
810 | + $items_label = array( |
|
811 | + 'single' => __('1 item', 'event_espresso'), |
|
812 | + 'plural' => __('%s items', 'event_espresso'), |
|
813 | + ); |
|
814 | + } else { |
|
815 | + $items_label = array( |
|
816 | + 'single' => '1 ' . esc_html($items_label['single']), |
|
817 | + 'plural' => '%s ' . esc_html($items_label['plural']), |
|
818 | + ); |
|
819 | + } |
|
820 | + |
|
821 | + $total_pages = ceil($total_items / $per_page); |
|
822 | + |
|
823 | + if ($total_pages <= 1) { |
|
824 | + return ''; |
|
825 | + } |
|
826 | + |
|
827 | + $item_label = $total_items > 1 ? sprintf($items_label['plural'], $total_items) : $items_label['single']; |
|
828 | + |
|
829 | + $output = '<span class="displaying-num">' . $item_label . '</span>'; |
|
830 | + |
|
831 | + if ($current === 1) { |
|
832 | + $disable_first = ' disabled'; |
|
833 | + } |
|
834 | + if ($current == $total_pages) { |
|
835 | + $disable_last = ' disabled'; |
|
836 | + } |
|
837 | + |
|
838 | + $page_links[] = sprintf("<a class='%s' title='%s' href='%s'>%s</a>", |
|
839 | + 'first-page' . $disable_first, |
|
840 | + esc_attr__('Go to the first page'), |
|
841 | + esc_url(remove_query_arg($paged_arg_name, $url)), |
|
842 | + '«' |
|
843 | + ); |
|
844 | + |
|
845 | + $page_links[] = sprintf( |
|
846 | + '<a class="%s" title="%s" href="%s">%s</a>', |
|
847 | + 'prev-page' . $disable_first, |
|
848 | + esc_attr__('Go to the previous page'), |
|
849 | + esc_url(add_query_arg($paged_arg_name, max(1, $current - 1), $url)), |
|
850 | + '‹' |
|
851 | + ); |
|
852 | + |
|
853 | + if ( ! $show_num_field) { |
|
854 | + $html_current_page = $current; |
|
855 | + } else { |
|
856 | + $html_current_page = sprintf("<input class='current-page' title='%s' type='text' name=$paged_arg_name value='%s' size='%d' />", |
|
857 | + esc_attr__('Current page'), |
|
858 | + $current, |
|
859 | + strlen($total_pages) |
|
860 | + ); |
|
861 | + } |
|
862 | + |
|
863 | + $html_total_pages = sprintf( |
|
864 | + '<span class="total-pages">%s</span>', |
|
865 | + number_format_i18n($total_pages) |
|
866 | + ); |
|
867 | + $page_links[] = sprintf( |
|
868 | + _x('%3$s%1$s of %2$s%4$s', 'paging'), |
|
869 | + $html_current_page, |
|
870 | + $html_total_pages, |
|
871 | + '<span class="paging-input">', |
|
872 | + '</span>' |
|
873 | + ); |
|
874 | + |
|
875 | + $page_links[] = sprintf( |
|
876 | + '<a class="%s" title="%s" href="%s">%s</a>', |
|
877 | + 'next-page' . $disable_last, |
|
878 | + esc_attr__('Go to the next page'), |
|
879 | + esc_url(add_query_arg($paged_arg_name, min($total_pages, $current + 1), $url)), |
|
880 | + '›' |
|
881 | + ); |
|
882 | + |
|
883 | + $page_links[] = sprintf( |
|
884 | + '<a class="%s" title="%s" href="%s">%s</a>', |
|
885 | + 'last-page' . $disable_last, |
|
886 | + esc_attr__('Go to the last page'), |
|
887 | + esc_url(add_query_arg($paged_arg_name, $total_pages, $url)), |
|
888 | + '»' |
|
889 | + ); |
|
890 | + |
|
891 | + $output .= "\n" . '<span class="pagination-links">' . join("\n", $page_links) . '</span>'; |
|
892 | + // set page class |
|
893 | + if ($total_pages) { |
|
894 | + $page_class = $total_pages < 2 ? ' one-page' : ''; |
|
895 | + } else { |
|
896 | + $page_class = ' no-pages'; |
|
897 | + } |
|
898 | + |
|
899 | + return '<div class="tablenav"><div class="tablenav-pages' . $page_class . '">' . $output . '</div></div>'; |
|
900 | + } |
|
901 | + |
|
902 | + |
|
903 | + /** |
|
904 | + * @param string $wrap_class |
|
905 | + * @param string $wrap_id |
|
906 | + * @return string |
|
907 | + */ |
|
908 | + public static function powered_by_event_espresso($wrap_class = '', $wrap_id = '', array $query_args = array()) |
|
909 | + { |
|
910 | + $admin = is_admin() && ! (defined('DOING_AJAX') && DOING_AJAX); |
|
911 | + if ( |
|
912 | + ! $admin && |
|
913 | + ! apply_filters( |
|
914 | + 'FHEE__EEH_Template__powered_by_event_espresso__show_reg_footer', |
|
915 | + EE_Registry::instance()->CFG->admin->show_reg_footer |
|
916 | + ) |
|
917 | + ) { |
|
918 | + return ''; |
|
919 | + } |
|
920 | + $tag = $admin ? 'span' : 'div'; |
|
921 | + $attributes = ! empty($wrap_id) ? " id=\"{$wrap_id}\"" : ''; |
|
922 | + $wrap_class = $admin ? "{$wrap_class} float-left" : $wrap_class; |
|
923 | + $attributes .= ! empty($wrap_class) |
|
924 | + ? " class=\"{$wrap_class} powered-by-event-espresso-credit\"" |
|
925 | + : ' class="powered-by-event-espresso-credit"'; |
|
926 | + $query_args = array_merge( |
|
927 | + array( |
|
928 | + 'ap_id' => EE_Registry::instance()->CFG->admin->affiliate_id(), |
|
929 | + 'utm_source' => 'powered_by_event_espresso', |
|
930 | + 'utm_medium' => 'link', |
|
931 | + 'utm_campaign' => 'powered_by', |
|
932 | + ), |
|
933 | + $query_args |
|
934 | + ); |
|
935 | + $powered_by = apply_filters('FHEE__EEH_Template__powered_by_event_espresso_text', |
|
936 | + $admin ? 'Event Espresso - ' . EVENT_ESPRESSO_VERSION : 'Event Espresso'); |
|
937 | + $url = add_query_arg($query_args, 'https://eventespresso.com/'); |
|
938 | + $url = apply_filters('FHEE__EEH_Template__powered_by_event_espresso__url', $url); |
|
939 | + return (string)apply_filters( |
|
940 | + 'FHEE__EEH_Template__powered_by_event_espresso__html', |
|
941 | + sprintf( |
|
942 | + esc_html_x( |
|
943 | + '%3$s%1$sOnline event registration and ticketing powered by %2$s%3$s', |
|
944 | + 'Online event registration and ticketing powered by [link to eventespresso.com]', |
|
945 | + 'event_espresso' |
|
946 | + ), |
|
947 | + "<{$tag}{$attributes}>", |
|
948 | + "<a href=\"{$url}\" target=\"_blank\" rel=\"nofollow\">{$powered_by}</a></{$tag}>", |
|
949 | + $admin ? '' : '<br />' |
|
950 | + ), |
|
951 | + $wrap_class, |
|
952 | + $wrap_id |
|
953 | + ); |
|
954 | + } |
|
955 | 955 | |
956 | 956 | |
957 | 957 | } //end EEH_Template class |
@@ -960,33 +960,33 @@ discard block |
||
960 | 960 | |
961 | 961 | |
962 | 962 | if ( ! function_exists('espresso_pagination')) { |
963 | - /** |
|
964 | - * espresso_pagination |
|
965 | - * |
|
966 | - * @access public |
|
967 | - * @return void |
|
968 | - */ |
|
969 | - function espresso_pagination() |
|
970 | - { |
|
971 | - global $wp_query; |
|
972 | - $big = 999999999; // need an unlikely integer |
|
973 | - $pagination = paginate_links( |
|
974 | - array( |
|
975 | - 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), |
|
976 | - 'format' => '?paged=%#%', |
|
977 | - 'current' => max(1, get_query_var('paged')), |
|
978 | - 'total' => $wp_query->max_num_pages, |
|
979 | - 'show_all' => true, |
|
980 | - 'end_size' => 10, |
|
981 | - 'mid_size' => 6, |
|
982 | - 'prev_next' => true, |
|
983 | - 'prev_text' => __('‹ PREV', 'event_espresso'), |
|
984 | - 'next_text' => __('NEXT ›', 'event_espresso'), |
|
985 | - 'type' => 'plain', |
|
986 | - 'add_args' => false, |
|
987 | - 'add_fragment' => '', |
|
988 | - ) |
|
989 | - ); |
|
990 | - echo ! empty($pagination) ? '<div class="ee-pagination-dv clear">' . $pagination . '</div>' : ''; |
|
991 | - } |
|
963 | + /** |
|
964 | + * espresso_pagination |
|
965 | + * |
|
966 | + * @access public |
|
967 | + * @return void |
|
968 | + */ |
|
969 | + function espresso_pagination() |
|
970 | + { |
|
971 | + global $wp_query; |
|
972 | + $big = 999999999; // need an unlikely integer |
|
973 | + $pagination = paginate_links( |
|
974 | + array( |
|
975 | + 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), |
|
976 | + 'format' => '?paged=%#%', |
|
977 | + 'current' => max(1, get_query_var('paged')), |
|
978 | + 'total' => $wp_query->max_num_pages, |
|
979 | + 'show_all' => true, |
|
980 | + 'end_size' => 10, |
|
981 | + 'mid_size' => 6, |
|
982 | + 'prev_next' => true, |
|
983 | + 'prev_text' => __('‹ PREV', 'event_espresso'), |
|
984 | + 'next_text' => __('NEXT ›', 'event_espresso'), |
|
985 | + 'type' => 'plain', |
|
986 | + 'add_args' => false, |
|
987 | + 'add_fragment' => '', |
|
988 | + ) |
|
989 | + ); |
|
990 | + echo ! empty($pagination) ? '<div class="ee-pagination-dv clear">' . $pagination . '</div>' : ''; |
|
991 | + } |
|
992 | 992 | } |
993 | 993 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
3 | 3 | exit('NO direct script access allowed'); |
4 | 4 | } |
5 | 5 | /** |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | public static function load_espresso_theme_functions() |
82 | 82 | { |
83 | 83 | if ( ! defined('EE_THEME_FUNCTIONS_LOADED')) { |
84 | - if (is_readable(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php')) { |
|
85 | - require_once(EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php'); |
|
84 | + if (is_readable(EE_PUBLIC.EE_Config::get_current_theme().DS.'functions.php')) { |
|
85 | + require_once(EE_PUBLIC.EE_Config::get_current_theme().DS.'functions.php'); |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | public static function get_espresso_themes() |
97 | 97 | { |
98 | 98 | if (empty(EEH_Template::$_espresso_themes)) { |
99 | - $espresso_themes = glob(EE_PUBLIC . '*', GLOB_ONLYDIR); |
|
99 | + $espresso_themes = glob(EE_PUBLIC.'*', GLOB_ONLYDIR); |
|
100 | 100 | if (empty($espresso_themes)) { |
101 | 101 | return array(); |
102 | 102 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | ) { |
133 | 133 | do_action("get_template_part_{$slug}-{$name}", $slug, $name); |
134 | 134 | $templates = array(); |
135 | - $name = (string)$name; |
|
135 | + $name = (string) $name; |
|
136 | 136 | if ($name != '') { |
137 | 137 | $templates[] = "{$slug}-{$name}.php"; |
138 | 138 | } |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | if (isset($EE_CPTs[$post_type])) { |
212 | 212 | $archive_or_single = is_archive() ? 'archive' : ''; |
213 | 213 | $archive_or_single = is_single() ? 'single' : $archive_or_single; |
214 | - $templates = $archive_or_single . '-' . $post_type . '.php'; |
|
214 | + $templates = $archive_or_single.'-'.$post_type.'.php'; |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | // currently active EE template theme |
@@ -220,18 +220,18 @@ discard block |
||
220 | 220 | // array of paths to folders that may contain templates |
221 | 221 | $template_folder_paths = array( |
222 | 222 | // first check the /wp-content/uploads/espresso/templates/(current EE theme)/ folder for an EE theme template file |
223 | - EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, |
|
223 | + EVENT_ESPRESSO_TEMPLATE_DIR.$current_theme, |
|
224 | 224 | // then in the root of the /wp-content/uploads/espresso/templates/ folder |
225 | 225 | EVENT_ESPRESSO_TEMPLATE_DIR, |
226 | 226 | ); |
227 | 227 | |
228 | 228 | //add core plugin folders for checking only if we're not $check_if_custom |
229 | 229 | if ( ! $check_if_custom) { |
230 | - $core_paths = array( |
|
230 | + $core_paths = array( |
|
231 | 231 | // in the /wp-content/plugins/(EE4 folder)/public/(current EE theme)/ folder within the plugin |
232 | - EE_PUBLIC . $current_theme, |
|
232 | + EE_PUBLIC.$current_theme, |
|
233 | 233 | // in the /wp-content/plugins/(EE4 folder)/core/templates/(current EE theme)/ folder within the plugin |
234 | - EE_TEMPLATES . $current_theme, |
|
234 | + EE_TEMPLATES.$current_theme, |
|
235 | 235 | // or maybe relative from the plugin root: /wp-content/plugins/(EE4 folder)/ |
236 | 236 | EE_PLUGIN_DIR_PATH, |
237 | 237 | ); |
@@ -262,10 +262,10 @@ discard block |
||
262 | 262 | ); |
263 | 263 | if ($common_base_path !== '') { |
264 | 264 | // both paths have a common base, so just tack the filename onto our search path |
265 | - $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $file_name; |
|
265 | + $resolved_path = EEH_File::end_with_directory_separator($template_folder_path).$file_name; |
|
266 | 266 | } else { |
267 | 267 | // no common base path, so let's just concatenate |
268 | - $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $template; |
|
268 | + $resolved_path = EEH_File::end_with_directory_separator($template_folder_path).$template; |
|
269 | 269 | } |
270 | 270 | // build up our template locations array by adding our resolved paths |
271 | 271 | $full_template_paths[] = $resolved_path; |
@@ -273,13 +273,13 @@ discard block |
||
273 | 273 | // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first |
274 | 274 | array_unshift($full_template_paths, $template); |
275 | 275 | // path to the directory of the current theme: /wp-content/themes/(current WP theme)/ |
276 | - array_unshift($full_template_paths, get_stylesheet_directory() . DS . $file_name); |
|
276 | + array_unshift($full_template_paths, get_stylesheet_directory().DS.$file_name); |
|
277 | 277 | } |
278 | 278 | // filter final array of full template paths |
279 | 279 | $full_template_paths = apply_filters('FHEE__EEH_Template__locate_template__full_template_paths', |
280 | 280 | $full_template_paths, $file_name); |
281 | 281 | // now loop through our final array of template location paths and check each location |
282 | - foreach ((array)$full_template_paths as $full_template_path) { |
|
282 | + foreach ((array) $full_template_paths as $full_template_path) { |
|
283 | 283 | if (is_readable($full_template_path)) { |
284 | 284 | $template_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $full_template_path); |
285 | 285 | // hook that can be used to display the full template path that will be used |
@@ -364,9 +364,9 @@ discard block |
||
364 | 364 | if ( ! is_array($template_args) && ! is_object($template_args)) { |
365 | 365 | $template_args = array($template_args); |
366 | 366 | } |
367 | - extract( $template_args, EXTR_SKIP ); |
|
367 | + extract($template_args, EXTR_SKIP); |
|
368 | 368 | // ignore whether template is accessible ? |
369 | - if ( $throw_exceptions && ! is_readable( $template_path ) ) { |
|
369 | + if ($throw_exceptions && ! is_readable($template_path)) { |
|
370 | 370 | throw new \DomainException( |
371 | 371 | esc_html__( |
372 | 372 | 'Invalid, unreadable, or missing file.', |
@@ -399,11 +399,11 @@ discard block |
||
399 | 399 | public static function get_object_css_class($object = null, $prefix = '', $suffix = '') |
400 | 400 | { |
401 | 401 | // in the beginning... |
402 | - $prefix = ! empty($prefix) ? rtrim($prefix, '-') . '-' : ''; |
|
402 | + $prefix = ! empty($prefix) ? rtrim($prefix, '-').'-' : ''; |
|
403 | 403 | // da muddle |
404 | 404 | $class = ''; |
405 | 405 | // the end |
406 | - $suffix = ! empty($suffix) ? '-' . ltrim($suffix, '-') : ''; |
|
406 | + $suffix = ! empty($suffix) ? '-'.ltrim($suffix, '-') : ''; |
|
407 | 407 | // is the passed object an EE object ? |
408 | 408 | if ($object instanceof EE_Base_Class) { |
409 | 409 | // grab the exact type of object |
@@ -413,11 +413,11 @@ discard block |
||
413 | 413 | // no specifics just yet... |
414 | 414 | default : |
415 | 415 | $class = strtolower(str_replace('_', '-', $obj_class)); |
416 | - $class .= method_exists($obj_class, 'name') ? '-' . sanitize_title($object->name()) : ''; |
|
416 | + $class .= method_exists($obj_class, 'name') ? '-'.sanitize_title($object->name()) : ''; |
|
417 | 417 | |
418 | 418 | } |
419 | 419 | } |
420 | - return $prefix . $class . $suffix; |
|
420 | + return $prefix.$class.$suffix; |
|
421 | 421 | } |
422 | 422 | |
423 | 423 | |
@@ -449,7 +449,7 @@ discard block |
||
449 | 449 | return ''; |
450 | 450 | } |
451 | 451 | //ensure amount is float |
452 | - $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float)$amount); |
|
452 | + $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float) $amount); |
|
453 | 453 | $CNT_ISO = apply_filters('FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount); |
454 | 454 | // filter raw amount (allows 0.00 to be changed to "free" for example) |
455 | 455 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount', $amount, $return_raw); |
@@ -471,20 +471,20 @@ discard block |
||
471 | 471 | // add currency sign |
472 | 472 | if ($mny->sign_b4) { |
473 | 473 | if ($amount >= 0) { |
474 | - $amount_formatted = $mny->sign . $amount_formatted; |
|
474 | + $amount_formatted = $mny->sign.$amount_formatted; |
|
475 | 475 | } else { |
476 | - $amount_formatted = '-' . $mny->sign . str_replace('-', '', $amount_formatted); |
|
476 | + $amount_formatted = '-'.$mny->sign.str_replace('-', '', $amount_formatted); |
|
477 | 477 | } |
478 | 478 | |
479 | 479 | } else { |
480 | - $amount_formatted = $amount_formatted . $mny->sign; |
|
480 | + $amount_formatted = $amount_formatted.$mny->sign; |
|
481 | 481 | } |
482 | 482 | |
483 | 483 | // filter to allow global setting of display_code |
484 | 484 | $display_code = apply_filters('FHEE__EEH_Template__format_currency__display_code', $display_code); |
485 | 485 | |
486 | 486 | // add currency code ? |
487 | - $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
|
487 | + $amount_formatted = $display_code ? $amount_formatted.' <span class="'.$cur_code_span_class.'">('.$mny->code.')</span>' : $amount_formatted; |
|
488 | 488 | } |
489 | 489 | // filter results |
490 | 490 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount_formatted', |
@@ -538,12 +538,12 @@ discard block |
||
538 | 538 | $icon_html .= $count > 1 ? '<span class="ee-composite-dashicon">' : ''; |
539 | 539 | foreach ($dashicons as $dashicon) { |
540 | 540 | $type = strpos($dashicon, 'ee-icon') !== false ? 'ee-icon ' : 'dashicons '; |
541 | - $icon_html .= '<span class="' . $type . $dashicon . '"></span>'; |
|
541 | + $icon_html .= '<span class="'.$type.$dashicon.'"></span>'; |
|
542 | 542 | } |
543 | 543 | $icon_html .= $count > 1 ? '</span>' : ''; |
544 | 544 | } |
545 | - $label = ! empty($icon) ? $icon_html . $label : $label; |
|
546 | - $button = '<a id="' . sanitize_title_with_dashes($label) . '" href="' . $url . '" class="' . $class . '" title="' . $title . '">' . $label . '</a>'; |
|
545 | + $label = ! empty($icon) ? $icon_html.$label : $label; |
|
546 | + $button = '<a id="'.sanitize_title_with_dashes($label).'" href="'.$url.'" class="'.$class.'" title="'.$title.'">'.$label.'</a>'; |
|
547 | 547 | return $button; |
548 | 548 | } |
549 | 549 | |
@@ -577,11 +577,11 @@ discard block |
||
577 | 577 | $action = empty($action) ? 'default' : $action; |
578 | 578 | |
579 | 579 | |
580 | - $help_tab_lnk = $page . '-' . $action . '-' . $help_tab_id; |
|
580 | + $help_tab_lnk = $page.'-'.$action.'-'.$help_tab_id; |
|
581 | 581 | $icon = ! $icon_style ? ' dashicons-editor-help' : $icon_style; |
582 | 582 | $help_text = ! $help_text ? '' : $help_text; |
583 | - return '<a id="' . $help_tab_lnk . '" class="ee-clickable dashicons espresso-help-tab-lnk ee-icon-size-22' . $icon . '" title="' . esc_attr__('Click to open the \'Help\' tab for more information about this feature.', |
|
584 | - 'event_espresso') . '" > ' . $help_text . ' </a>'; |
|
583 | + return '<a id="'.$help_tab_lnk.'" class="ee-clickable dashicons espresso-help-tab-lnk ee-icon-size-22'.$icon.'" title="'.esc_attr__('Click to open the \'Help\' tab for more information about this feature.', |
|
584 | + 'event_espresso').'" > '.$help_text.' </a>'; |
|
585 | 585 | } |
586 | 586 | |
587 | 587 | |
@@ -598,11 +598,11 @@ discard block |
||
598 | 598 | $id = $tour->get_slug(); |
599 | 599 | $stops = $tour->get_stops(); |
600 | 600 | |
601 | - $content = '<ol style="display:none" id="' . $id . '">'; |
|
601 | + $content = '<ol style="display:none" id="'.$id.'">'; |
|
602 | 602 | |
603 | 603 | foreach ($stops as $stop) { |
604 | - $data_id = ! empty($stop['id']) ? ' data-id="' . $stop['id'] . '"' : ''; |
|
605 | - $data_class = empty($data_id) && ! empty($stop['class']) ? ' data-class="' . $stop['class'] . '"' : ''; |
|
604 | + $data_id = ! empty($stop['id']) ? ' data-id="'.$stop['id'].'"' : ''; |
|
605 | + $data_class = empty($data_id) && ! empty($stop['class']) ? ' data-class="'.$stop['class'].'"' : ''; |
|
606 | 606 | |
607 | 607 | //if container is set to modal then let's make sure we set the options accordingly |
608 | 608 | if (empty($data_id) && empty($data_class)) { |
@@ -610,15 +610,15 @@ discard block |
||
610 | 610 | $stop['options']['expose'] = true; |
611 | 611 | } |
612 | 612 | |
613 | - $custom_class = ! empty($stop['custom_class']) ? ' class="' . $stop['custom_class'] . '"' : ''; |
|
614 | - $button_text = ! empty($stop['button_text']) ? ' data-button="' . $stop['button_text'] . '"' : ''; |
|
613 | + $custom_class = ! empty($stop['custom_class']) ? ' class="'.$stop['custom_class'].'"' : ''; |
|
614 | + $button_text = ! empty($stop['button_text']) ? ' data-button="'.$stop['button_text'].'"' : ''; |
|
615 | 615 | $inner_content = isset($stop['content']) ? $stop['content'] : ''; |
616 | 616 | |
617 | 617 | //options |
618 | 618 | if (isset($stop['options']) && is_array($stop['options'])) { |
619 | 619 | $options = ' data-options="'; |
620 | 620 | foreach ($stop['options'] as $option => $value) { |
621 | - $options .= $option . ':' . $value . ';'; |
|
621 | + $options .= $option.':'.$value.';'; |
|
622 | 622 | } |
623 | 623 | $options .= '"'; |
624 | 624 | } else { |
@@ -626,7 +626,7 @@ discard block |
||
626 | 626 | } |
627 | 627 | |
628 | 628 | //let's put all together |
629 | - $content .= '<li' . $data_id . $data_class . $custom_class . $button_text . $options . '>' . $inner_content . '</li>'; |
|
629 | + $content .= '<li'.$data_id.$data_class.$custom_class.$button_text.$options.'>'.$inner_content.'</li>'; |
|
630 | 630 | } |
631 | 631 | |
632 | 632 | $content .= '</ol>'; |
@@ -658,24 +658,24 @@ discard block |
||
658 | 658 | $setup_array = array(); |
659 | 659 | foreach ($status_array as $item => $status) { |
660 | 660 | $setup_array[$item] = array( |
661 | - 'class' => 'ee-status-legend ee-status-legend-' . $status, |
|
661 | + 'class' => 'ee-status-legend ee-status-legend-'.$status, |
|
662 | 662 | 'desc' => EEH_Template::pretty_status($status, false, 'sentence'), |
663 | 663 | 'status' => $status, |
664 | 664 | ); |
665 | 665 | } |
666 | 666 | |
667 | - $content = '<div class="ee-list-table-legend-container">' . "\n"; |
|
668 | - $content .= '<h4 class="status-legend-title">' . esc_html__('Status Legend', 'event_espresso') . '</h4>' . "\n"; |
|
669 | - $content .= '<dl class="ee-list-table-legend">' . "\n\t"; |
|
667 | + $content = '<div class="ee-list-table-legend-container">'."\n"; |
|
668 | + $content .= '<h4 class="status-legend-title">'.esc_html__('Status Legend', 'event_espresso').'</h4>'."\n"; |
|
669 | + $content .= '<dl class="ee-list-table-legend">'."\n\t"; |
|
670 | 670 | foreach ($setup_array as $item => $details) { |
671 | 671 | $active_class = $active_status == $details['status'] ? ' class="ee-is-active-status"' : ''; |
672 | - $content .= '<dt id="ee-legend-item-tooltip-' . $item . '"' . $active_class . '>' . "\n\t\t"; |
|
673 | - $content .= '<span class="' . $details['class'] . '"></span>' . "\n\t\t"; |
|
674 | - $content .= '<span class="ee-legend-description">' . $details['desc'] . '</span>' . "\n\t"; |
|
675 | - $content .= '</dt>' . "\n"; |
|
672 | + $content .= '<dt id="ee-legend-item-tooltip-'.$item.'"'.$active_class.'>'."\n\t\t"; |
|
673 | + $content .= '<span class="'.$details['class'].'"></span>'."\n\t\t"; |
|
674 | + $content .= '<span class="ee-legend-description">'.$details['desc'].'</span>'."\n\t"; |
|
675 | + $content .= '</dt>'."\n"; |
|
676 | 676 | } |
677 | - $content .= '</dl>' . "\n"; |
|
678 | - $content .= '</div>' . "\n"; |
|
677 | + $content .= '</dl>'."\n"; |
|
678 | + $content .= '</div>'."\n"; |
|
679 | 679 | return $content; |
680 | 680 | } |
681 | 681 | |
@@ -690,7 +690,7 @@ discard block |
||
690 | 690 | public static function layout_array_as_table($data) |
691 | 691 | { |
692 | 692 | if (is_object($data) || $data instanceof __PHP_Incomplete_Class) { |
693 | - $data = (array)$data; |
|
693 | + $data = (array) $data; |
|
694 | 694 | } |
695 | 695 | ob_start(); |
696 | 696 | if (is_array($data)) { |
@@ -791,9 +791,9 @@ discard block |
||
791 | 791 | ) { |
792 | 792 | $page_links = array(); |
793 | 793 | $disable_first = $disable_last = ''; |
794 | - $total_items = (int)$total_items; |
|
795 | - $per_page = (int)$per_page; |
|
796 | - $current = (int)$current; |
|
794 | + $total_items = (int) $total_items; |
|
795 | + $per_page = (int) $per_page; |
|
796 | + $current = (int) $current; |
|
797 | 797 | $paged_arg_name = empty($paged_arg_name) ? 'paged' : sanitize_key($paged_arg_name); |
798 | 798 | |
799 | 799 | //filter items_label |
@@ -813,8 +813,8 @@ discard block |
||
813 | 813 | ); |
814 | 814 | } else { |
815 | 815 | $items_label = array( |
816 | - 'single' => '1 ' . esc_html($items_label['single']), |
|
817 | - 'plural' => '%s ' . esc_html($items_label['plural']), |
|
816 | + 'single' => '1 '.esc_html($items_label['single']), |
|
817 | + 'plural' => '%s '.esc_html($items_label['plural']), |
|
818 | 818 | ); |
819 | 819 | } |
820 | 820 | |
@@ -826,7 +826,7 @@ discard block |
||
826 | 826 | |
827 | 827 | $item_label = $total_items > 1 ? sprintf($items_label['plural'], $total_items) : $items_label['single']; |
828 | 828 | |
829 | - $output = '<span class="displaying-num">' . $item_label . '</span>'; |
|
829 | + $output = '<span class="displaying-num">'.$item_label.'</span>'; |
|
830 | 830 | |
831 | 831 | if ($current === 1) { |
832 | 832 | $disable_first = ' disabled'; |
@@ -836,7 +836,7 @@ discard block |
||
836 | 836 | } |
837 | 837 | |
838 | 838 | $page_links[] = sprintf("<a class='%s' title='%s' href='%s'>%s</a>", |
839 | - 'first-page' . $disable_first, |
|
839 | + 'first-page'.$disable_first, |
|
840 | 840 | esc_attr__('Go to the first page'), |
841 | 841 | esc_url(remove_query_arg($paged_arg_name, $url)), |
842 | 842 | '«' |
@@ -844,7 +844,7 @@ discard block |
||
844 | 844 | |
845 | 845 | $page_links[] = sprintf( |
846 | 846 | '<a class="%s" title="%s" href="%s">%s</a>', |
847 | - 'prev-page' . $disable_first, |
|
847 | + 'prev-page'.$disable_first, |
|
848 | 848 | esc_attr__('Go to the previous page'), |
849 | 849 | esc_url(add_query_arg($paged_arg_name, max(1, $current - 1), $url)), |
850 | 850 | '‹' |
@@ -864,7 +864,7 @@ discard block |
||
864 | 864 | '<span class="total-pages">%s</span>', |
865 | 865 | number_format_i18n($total_pages) |
866 | 866 | ); |
867 | - $page_links[] = sprintf( |
|
867 | + $page_links[] = sprintf( |
|
868 | 868 | _x('%3$s%1$s of %2$s%4$s', 'paging'), |
869 | 869 | $html_current_page, |
870 | 870 | $html_total_pages, |
@@ -874,7 +874,7 @@ discard block |
||
874 | 874 | |
875 | 875 | $page_links[] = sprintf( |
876 | 876 | '<a class="%s" title="%s" href="%s">%s</a>', |
877 | - 'next-page' . $disable_last, |
|
877 | + 'next-page'.$disable_last, |
|
878 | 878 | esc_attr__('Go to the next page'), |
879 | 879 | esc_url(add_query_arg($paged_arg_name, min($total_pages, $current + 1), $url)), |
880 | 880 | '›' |
@@ -882,13 +882,13 @@ discard block |
||
882 | 882 | |
883 | 883 | $page_links[] = sprintf( |
884 | 884 | '<a class="%s" title="%s" href="%s">%s</a>', |
885 | - 'last-page' . $disable_last, |
|
885 | + 'last-page'.$disable_last, |
|
886 | 886 | esc_attr__('Go to the last page'), |
887 | 887 | esc_url(add_query_arg($paged_arg_name, $total_pages, $url)), |
888 | 888 | '»' |
889 | 889 | ); |
890 | 890 | |
891 | - $output .= "\n" . '<span class="pagination-links">' . join("\n", $page_links) . '</span>'; |
|
891 | + $output .= "\n".'<span class="pagination-links">'.join("\n", $page_links).'</span>'; |
|
892 | 892 | // set page class |
893 | 893 | if ($total_pages) { |
894 | 894 | $page_class = $total_pages < 2 ? ' one-page' : ''; |
@@ -896,7 +896,7 @@ discard block |
||
896 | 896 | $page_class = ' no-pages'; |
897 | 897 | } |
898 | 898 | |
899 | - return '<div class="tablenav"><div class="tablenav-pages' . $page_class . '">' . $output . '</div></div>'; |
|
899 | + return '<div class="tablenav"><div class="tablenav-pages'.$page_class.'">'.$output.'</div></div>'; |
|
900 | 900 | } |
901 | 901 | |
902 | 902 | |
@@ -933,10 +933,10 @@ discard block |
||
933 | 933 | $query_args |
934 | 934 | ); |
935 | 935 | $powered_by = apply_filters('FHEE__EEH_Template__powered_by_event_espresso_text', |
936 | - $admin ? 'Event Espresso - ' . EVENT_ESPRESSO_VERSION : 'Event Espresso'); |
|
936 | + $admin ? 'Event Espresso - '.EVENT_ESPRESSO_VERSION : 'Event Espresso'); |
|
937 | 937 | $url = add_query_arg($query_args, 'https://eventespresso.com/'); |
938 | 938 | $url = apply_filters('FHEE__EEH_Template__powered_by_event_espresso__url', $url); |
939 | - return (string)apply_filters( |
|
939 | + return (string) apply_filters( |
|
940 | 940 | 'FHEE__EEH_Template__powered_by_event_espresso__html', |
941 | 941 | sprintf( |
942 | 942 | esc_html_x( |
@@ -987,6 +987,6 @@ discard block |
||
987 | 987 | 'add_fragment' => '', |
988 | 988 | ) |
989 | 989 | ); |
990 | - echo ! empty($pagination) ? '<div class="ee-pagination-dv clear">' . $pagination . '</div>' : ''; |
|
990 | + echo ! empty($pagination) ? '<div class="ee-pagination-dv clear">'.$pagination.'</div>' : ''; |
|
991 | 991 | } |
992 | 992 | } |
993 | 993 | \ No newline at end of file |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * _find_common_base_path |
305 | 305 | * given two paths, this determines if there is a common base path between the two |
306 | 306 | * |
307 | - * @param array $paths |
|
307 | + * @param string[] $paths |
|
308 | 308 | * @return string |
309 | 309 | */ |
310 | 310 | protected static function _find_common_base_path($paths) |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
335 | 335 | * @param bool $throw_exceptions if set to true, will throw an exception if the template is either |
336 | 336 | * not found or is not readable |
337 | - * @return mixed string |
|
337 | + * @return string string |
|
338 | 338 | * @throws \DomainException |
339 | 339 | */ |
340 | 340 | public static function display_template( |