@@ -17,147 +17,147 @@ |
||
17 | 17 | class FqcnLocator extends Locator |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var array $FQCNs |
|
22 | - */ |
|
23 | - protected $FQCNs = array(); |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array $namespaces |
|
27 | - */ |
|
28 | - protected $namespaces = array(); |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @access protected |
|
33 | - * @param string $namespace |
|
34 | - * @param string $namespace_base_dir |
|
35 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
36 | - */ |
|
37 | - protected function setNamespace($namespace, $namespace_base_dir) |
|
38 | - { |
|
39 | - if (! is_string($namespace)) { |
|
40 | - throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
41 | - } |
|
42 | - if (! is_string($namespace_base_dir)) { |
|
43 | - throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
44 | - } |
|
45 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * @access public |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function getFQCNs() |
|
54 | - { |
|
55 | - return $this->FQCNs; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @access public |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function count() |
|
64 | - { |
|
65 | - return count($this->FQCNs); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * given a valid namespace, will find all files that match the provided mask |
|
71 | - * |
|
72 | - * @access public |
|
73 | - * @param string|array $namespaces |
|
74 | - * @return FilesystemIterator |
|
75 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
76 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
77 | - */ |
|
78 | - public function locate($namespaces) |
|
79 | - { |
|
80 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | - throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
82 | - } |
|
83 | - foreach ((array) $namespaces as $namespace) { |
|
84 | - foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
|
85 | - $this->FQCNs[ $key ] = $file; |
|
86 | - } |
|
87 | - } |
|
88 | - return $this->FQCNs; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
93 | - |
|
94 | - /** |
|
95 | - * given a partial namespace, will find all files in that folder |
|
96 | - * ** PLZ NOTE ** |
|
97 | - * This assumes that all files within the specified folder should be loaded |
|
98 | - * |
|
99 | - * @access protected |
|
100 | - * @param array $partial_namespace |
|
101 | - * @return FilesystemIterator |
|
102 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
103 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
104 | - */ |
|
105 | - protected function FindFQCNsByNamespace($partial_namespace) |
|
106 | - { |
|
107 | - $iterator = new FilesystemIterator( |
|
108 | - $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
109 | - ); |
|
110 | - foreach ($this->flags as $flag) { |
|
111 | - $iterator->setFlags($flag); |
|
112 | - } |
|
113 | - if (iterator_count($iterator) === 0) { |
|
114 | - return array(); |
|
115 | - } |
|
116 | - foreach ($iterator as $file) { |
|
117 | - $file = \EEH_File::standardise_directory_separators($file); |
|
118 | - foreach ($this->namespaces as $namespace => $base_dir) { |
|
119 | - $namespace .= Psr4Autoloader::NS; |
|
120 | - if (strpos($file, $base_dir) === 0) { |
|
121 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
122 | - array($base_dir, DS, '.php'), |
|
123 | - array($namespace, Psr4Autoloader::NS, ''), |
|
124 | - $file |
|
125 | - ); |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - return $this->FQCNs; |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * getDirectoryFromPartialNamespace |
|
135 | - * |
|
136 | - * @access protected |
|
137 | - * @param string $partial_namespace almost fully qualified class name ? |
|
138 | - * @return string |
|
139 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
140 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
141 | - */ |
|
142 | - protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
143 | - { |
|
144 | - if (empty($partial_namespace)) { |
|
145 | - throw new InvalidClassException($partial_namespace); |
|
146 | - } |
|
147 | - // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
148 | - $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
149 | - // breakup the incoming namespace into segments then loop thru them |
|
150 | - $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
151 | - // we're only interested in the first element, so pull that from the array |
|
152 | - $namespace = array_shift($namespace_segments); |
|
153 | - // check if there's a base directory registered for that namespace |
|
154 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
155 | - // nope? then the incoming namespace is invalid |
|
156 | - if (empty($prefix) || empty($prefix[0])) { |
|
157 | - throw new InvalidClassException($partial_namespace); |
|
158 | - } |
|
159 | - $this->setNamespace($namespace, $prefix[0]); |
|
160 | - // but if it's good, add that base directory to the rest of the path, and return it |
|
161 | - return $prefix[0] . implode(DS, $namespace_segments) . DS; |
|
162 | - } |
|
20 | + /** |
|
21 | + * @var array $FQCNs |
|
22 | + */ |
|
23 | + protected $FQCNs = array(); |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array $namespaces |
|
27 | + */ |
|
28 | + protected $namespaces = array(); |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @access protected |
|
33 | + * @param string $namespace |
|
34 | + * @param string $namespace_base_dir |
|
35 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
36 | + */ |
|
37 | + protected function setNamespace($namespace, $namespace_base_dir) |
|
38 | + { |
|
39 | + if (! is_string($namespace)) { |
|
40 | + throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
41 | + } |
|
42 | + if (! is_string($namespace_base_dir)) { |
|
43 | + throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
44 | + } |
|
45 | + $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * @access public |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function getFQCNs() |
|
54 | + { |
|
55 | + return $this->FQCNs; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @access public |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function count() |
|
64 | + { |
|
65 | + return count($this->FQCNs); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * given a valid namespace, will find all files that match the provided mask |
|
71 | + * |
|
72 | + * @access public |
|
73 | + * @param string|array $namespaces |
|
74 | + * @return FilesystemIterator |
|
75 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
76 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
77 | + */ |
|
78 | + public function locate($namespaces) |
|
79 | + { |
|
80 | + if (! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | + throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
82 | + } |
|
83 | + foreach ((array) $namespaces as $namespace) { |
|
84 | + foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
|
85 | + $this->FQCNs[ $key ] = $file; |
|
86 | + } |
|
87 | + } |
|
88 | + return $this->FQCNs; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
93 | + |
|
94 | + /** |
|
95 | + * given a partial namespace, will find all files in that folder |
|
96 | + * ** PLZ NOTE ** |
|
97 | + * This assumes that all files within the specified folder should be loaded |
|
98 | + * |
|
99 | + * @access protected |
|
100 | + * @param array $partial_namespace |
|
101 | + * @return FilesystemIterator |
|
102 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
103 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
104 | + */ |
|
105 | + protected function FindFQCNsByNamespace($partial_namespace) |
|
106 | + { |
|
107 | + $iterator = new FilesystemIterator( |
|
108 | + $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
109 | + ); |
|
110 | + foreach ($this->flags as $flag) { |
|
111 | + $iterator->setFlags($flag); |
|
112 | + } |
|
113 | + if (iterator_count($iterator) === 0) { |
|
114 | + return array(); |
|
115 | + } |
|
116 | + foreach ($iterator as $file) { |
|
117 | + $file = \EEH_File::standardise_directory_separators($file); |
|
118 | + foreach ($this->namespaces as $namespace => $base_dir) { |
|
119 | + $namespace .= Psr4Autoloader::NS; |
|
120 | + if (strpos($file, $base_dir) === 0) { |
|
121 | + $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
122 | + array($base_dir, DS, '.php'), |
|
123 | + array($namespace, Psr4Autoloader::NS, ''), |
|
124 | + $file |
|
125 | + ); |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + return $this->FQCNs; |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * getDirectoryFromPartialNamespace |
|
135 | + * |
|
136 | + * @access protected |
|
137 | + * @param string $partial_namespace almost fully qualified class name ? |
|
138 | + * @return string |
|
139 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
140 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
141 | + */ |
|
142 | + protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
143 | + { |
|
144 | + if (empty($partial_namespace)) { |
|
145 | + throw new InvalidClassException($partial_namespace); |
|
146 | + } |
|
147 | + // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
148 | + $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
149 | + // breakup the incoming namespace into segments then loop thru them |
|
150 | + $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
151 | + // we're only interested in the first element, so pull that from the array |
|
152 | + $namespace = array_shift($namespace_segments); |
|
153 | + // check if there's a base directory registered for that namespace |
|
154 | + $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
155 | + // nope? then the incoming namespace is invalid |
|
156 | + if (empty($prefix) || empty($prefix[0])) { |
|
157 | + throw new InvalidClassException($partial_namespace); |
|
158 | + } |
|
159 | + $this->setNamespace($namespace, $prefix[0]); |
|
160 | + // but if it's good, add that base directory to the rest of the path, and return it |
|
161 | + return $prefix[0] . implode(DS, $namespace_segments) . DS; |
|
162 | + } |
|
163 | 163 | } |
@@ -36,13 +36,13 @@ discard block |
||
36 | 36 | */ |
37 | 37 | protected function setNamespace($namespace, $namespace_base_dir) |
38 | 38 | { |
39 | - if (! is_string($namespace)) { |
|
39 | + if ( ! is_string($namespace)) { |
|
40 | 40 | throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
41 | 41 | } |
42 | - if (! is_string($namespace_base_dir)) { |
|
42 | + if ( ! is_string($namespace_base_dir)) { |
|
43 | 43 | throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
44 | 44 | } |
45 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
45 | + $this->namespaces[$namespace] = $namespace_base_dir; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function locate($namespaces) |
79 | 79 | { |
80 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
80 | + if ( ! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | 81 | throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
82 | 82 | } |
83 | 83 | foreach ((array) $namespaces as $namespace) { |
84 | 84 | foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
85 | - $this->FQCNs[ $key ] = $file; |
|
85 | + $this->FQCNs[$key] = $file; |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | return $this->FQCNs; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | foreach ($this->namespaces as $namespace => $base_dir) { |
119 | 119 | $namespace .= Psr4Autoloader::NS; |
120 | 120 | if (strpos($file, $base_dir) === 0) { |
121 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
121 | + $this->FQCNs[] = Psr4Autoloader::NS.str_replace( |
|
122 | 122 | array($base_dir, DS, '.php'), |
123 | 123 | array($namespace, Psr4Autoloader::NS, ''), |
124 | 124 | $file |
@@ -151,13 +151,13 @@ discard block |
||
151 | 151 | // we're only interested in the first element, so pull that from the array |
152 | 152 | $namespace = array_shift($namespace_segments); |
153 | 153 | // check if there's a base directory registered for that namespace |
154 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
154 | + $prefix = $psr4_loader->prefixes($namespace.Psr4Autoloader::NS); |
|
155 | 155 | // nope? then the incoming namespace is invalid |
156 | 156 | if (empty($prefix) || empty($prefix[0])) { |
157 | 157 | throw new InvalidClassException($partial_namespace); |
158 | 158 | } |
159 | 159 | $this->setNamespace($namespace, $prefix[0]); |
160 | 160 | // but if it's good, add that base directory to the rest of the path, and return it |
161 | - return $prefix[0] . implode(DS, $namespace_segments) . DS; |
|
161 | + return $prefix[0].implode(DS, $namespace_segments).DS; |
|
162 | 162 | } |
163 | 163 | } |
@@ -13,248 +13,248 @@ |
||
13 | 13 | class TableManager extends \EE_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @var TableAnalysis $table_analysis |
|
18 | - */ |
|
19 | - private $table_analysis; |
|
16 | + /** |
|
17 | + * @var TableAnalysis $table_analysis |
|
18 | + */ |
|
19 | + private $table_analysis; |
|
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * TableManager constructor. |
|
24 | - * |
|
25 | - * @param TableAnalysis $TableAnalysis |
|
26 | - */ |
|
27 | - public function __construct(TableAnalysis $TableAnalysis) |
|
28 | - { |
|
29 | - $this->table_analysis = $TableAnalysis; |
|
30 | - } |
|
22 | + /** |
|
23 | + * TableManager constructor. |
|
24 | + * |
|
25 | + * @param TableAnalysis $TableAnalysis |
|
26 | + */ |
|
27 | + public function __construct(TableAnalysis $TableAnalysis) |
|
28 | + { |
|
29 | + $this->table_analysis = $TableAnalysis; |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Gets the injected table analyzer, or throws an exception |
|
35 | - * |
|
36 | - * @return TableAnalysis |
|
37 | - * @throws \EE_Error |
|
38 | - */ |
|
39 | - protected function getTableAnalysis() |
|
40 | - { |
|
41 | - if ($this->table_analysis instanceof TableAnalysis) { |
|
42 | - return $this->table_analysis; |
|
43 | - } else { |
|
44 | - throw new \EE_Error( |
|
45 | - sprintf( |
|
46 | - __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
47 | - get_class($this) |
|
48 | - ) |
|
49 | - ); |
|
50 | - } |
|
51 | - } |
|
33 | + /** |
|
34 | + * Gets the injected table analyzer, or throws an exception |
|
35 | + * |
|
36 | + * @return TableAnalysis |
|
37 | + * @throws \EE_Error |
|
38 | + */ |
|
39 | + protected function getTableAnalysis() |
|
40 | + { |
|
41 | + if ($this->table_analysis instanceof TableAnalysis) { |
|
42 | + return $this->table_analysis; |
|
43 | + } else { |
|
44 | + throw new \EE_Error( |
|
45 | + sprintf( |
|
46 | + __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
47 | + get_class($this) |
|
48 | + ) |
|
49 | + ); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * @param string $table_name which can optionally start with $wpdb->prefix or not |
|
56 | - * @param string $column_name |
|
57 | - * @param string $column_info |
|
58 | - * @return bool|false|int |
|
59 | - */ |
|
60 | - public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL') |
|
61 | - { |
|
62 | - if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) { |
|
63 | - return false; |
|
64 | - } |
|
65 | - global $wpdb; |
|
66 | - $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
67 | - $columns = $this->getTableColumns($table_name); |
|
68 | - if (! in_array($column_name, $columns)) { |
|
69 | - $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}"; |
|
70 | - return $wpdb->query($alter_query); |
|
71 | - } |
|
72 | - return true; |
|
73 | - } |
|
54 | + /** |
|
55 | + * @param string $table_name which can optionally start with $wpdb->prefix or not |
|
56 | + * @param string $column_name |
|
57 | + * @param string $column_info |
|
58 | + * @return bool|false|int |
|
59 | + */ |
|
60 | + public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL') |
|
61 | + { |
|
62 | + if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) { |
|
63 | + return false; |
|
64 | + } |
|
65 | + global $wpdb; |
|
66 | + $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
67 | + $columns = $this->getTableColumns($table_name); |
|
68 | + if (! in_array($column_name, $columns)) { |
|
69 | + $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}"; |
|
70 | + return $wpdb->query($alter_query); |
|
71 | + } |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | 75 | |
76 | - /** |
|
77 | - * Gets the name of all columns on the table. $table_name can |
|
78 | - * optionally start with $wpdb->prefix or not |
|
79 | - * |
|
80 | - * @global \wpdb $wpdb |
|
81 | - * @param string $table_name |
|
82 | - * @return array |
|
83 | - */ |
|
84 | - public function getTableColumns($table_name) |
|
85 | - { |
|
86 | - global $wpdb; |
|
87 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
88 | - $field_array = array(); |
|
89 | - if (! empty($table_name)) { |
|
90 | - $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} "); |
|
91 | - if ($columns !== false) { |
|
92 | - foreach ($columns as $column) { |
|
93 | - $field_array[] = $column->Field; |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - return $field_array; |
|
98 | - } |
|
76 | + /** |
|
77 | + * Gets the name of all columns on the table. $table_name can |
|
78 | + * optionally start with $wpdb->prefix or not |
|
79 | + * |
|
80 | + * @global \wpdb $wpdb |
|
81 | + * @param string $table_name |
|
82 | + * @return array |
|
83 | + */ |
|
84 | + public function getTableColumns($table_name) |
|
85 | + { |
|
86 | + global $wpdb; |
|
87 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
88 | + $field_array = array(); |
|
89 | + if (! empty($table_name)) { |
|
90 | + $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} "); |
|
91 | + if ($columns !== false) { |
|
92 | + foreach ($columns as $column) { |
|
93 | + $field_array[] = $column->Field; |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + return $field_array; |
|
98 | + } |
|
99 | 99 | |
100 | 100 | |
101 | - /** |
|
102 | - * Drops the specified table from the database. $table_name can |
|
103 | - * optionally start with $wpdb->prefix or not |
|
104 | - * |
|
105 | - * @global \wpdb $wpdb |
|
106 | - * @param string $table_name |
|
107 | - * @return int |
|
108 | - */ |
|
109 | - public function dropTable($table_name) |
|
110 | - { |
|
111 | - global $wpdb; |
|
112 | - if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
113 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
114 | - return $wpdb->query("DROP TABLE IF EXISTS {$table_name}"); |
|
115 | - } |
|
116 | - return 0; |
|
117 | - } |
|
101 | + /** |
|
102 | + * Drops the specified table from the database. $table_name can |
|
103 | + * optionally start with $wpdb->prefix or not |
|
104 | + * |
|
105 | + * @global \wpdb $wpdb |
|
106 | + * @param string $table_name |
|
107 | + * @return int |
|
108 | + */ |
|
109 | + public function dropTable($table_name) |
|
110 | + { |
|
111 | + global $wpdb; |
|
112 | + if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
113 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
114 | + return $wpdb->query("DROP TABLE IF EXISTS {$table_name}"); |
|
115 | + } |
|
116 | + return 0; |
|
117 | + } |
|
118 | 118 | |
119 | 119 | |
120 | - /** |
|
121 | - * Drops all the tables mentioned in a single MYSQL query. Double-checks |
|
122 | - * each table name provided has a wpdb prefix attached, and that it exists. |
|
123 | - * Returns the list actually deleted |
|
124 | - * |
|
125 | - * @global WPDB $wpdb |
|
126 | - * @param array $table_names |
|
127 | - * @return array of table names which we deleted |
|
128 | - */ |
|
129 | - public function dropTables($table_names) |
|
130 | - { |
|
131 | - $tables_to_delete = array(); |
|
132 | - foreach ($table_names as $table_name) { |
|
133 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
134 | - if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
135 | - $tables_to_delete[ $table_name ] = $table_name; |
|
136 | - } |
|
137 | - } |
|
138 | - if (! empty($tables_to_delete)) { |
|
139 | - global $wpdb; |
|
140 | - // make sure we only have a unique strings in the array. |
|
141 | - $tables_to_delete = array_unique($tables_to_delete); |
|
142 | - $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
143 | - } |
|
144 | - return $tables_to_delete; |
|
145 | - } |
|
120 | + /** |
|
121 | + * Drops all the tables mentioned in a single MYSQL query. Double-checks |
|
122 | + * each table name provided has a wpdb prefix attached, and that it exists. |
|
123 | + * Returns the list actually deleted |
|
124 | + * |
|
125 | + * @global WPDB $wpdb |
|
126 | + * @param array $table_names |
|
127 | + * @return array of table names which we deleted |
|
128 | + */ |
|
129 | + public function dropTables($table_names) |
|
130 | + { |
|
131 | + $tables_to_delete = array(); |
|
132 | + foreach ($table_names as $table_name) { |
|
133 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
134 | + if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
135 | + $tables_to_delete[ $table_name ] = $table_name; |
|
136 | + } |
|
137 | + } |
|
138 | + if (! empty($tables_to_delete)) { |
|
139 | + global $wpdb; |
|
140 | + // make sure we only have a unique strings in the array. |
|
141 | + $tables_to_delete = array_unique($tables_to_delete); |
|
142 | + $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
143 | + } |
|
144 | + return $tables_to_delete; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | |
148 | - /** |
|
149 | - * Drops the specified index from the specified table. $table_name can |
|
150 | - * optionally start with $wpdb->prefix or not |
|
151 | - * |
|
152 | - * @global \wpdb $wpdb |
|
153 | - * @param string $table_name |
|
154 | - * @param string $index_name |
|
155 | - * @return int the number of indexes dropped. False if there was a datbase error |
|
156 | - */ |
|
157 | - public function dropIndex($table_name, $index_name) |
|
158 | - { |
|
159 | - if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) { |
|
160 | - return 0; |
|
161 | - } |
|
162 | - global $wpdb; |
|
163 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
164 | - $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'"; |
|
165 | - if ($this->getTableAnalysis()->tableExists($table_name) |
|
166 | - && $wpdb->get_var($index_exists_query) |
|
167 | - === $table_name // using get_var with the $index_exists_query returns the table's name |
|
168 | - ) { |
|
169 | - return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}"); |
|
170 | - } |
|
171 | - return 0; |
|
172 | - } |
|
148 | + /** |
|
149 | + * Drops the specified index from the specified table. $table_name can |
|
150 | + * optionally start with $wpdb->prefix or not |
|
151 | + * |
|
152 | + * @global \wpdb $wpdb |
|
153 | + * @param string $table_name |
|
154 | + * @param string $index_name |
|
155 | + * @return int the number of indexes dropped. False if there was a datbase error |
|
156 | + */ |
|
157 | + public function dropIndex($table_name, $index_name) |
|
158 | + { |
|
159 | + if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) { |
|
160 | + return 0; |
|
161 | + } |
|
162 | + global $wpdb; |
|
163 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
164 | + $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'"; |
|
165 | + if ($this->getTableAnalysis()->tableExists($table_name) |
|
166 | + && $wpdb->get_var($index_exists_query) |
|
167 | + === $table_name // using get_var with the $index_exists_query returns the table's name |
|
168 | + ) { |
|
169 | + return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}"); |
|
170 | + } |
|
171 | + return 0; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | |
175 | - /** |
|
176 | - * Just creates the requested table. $table_name can |
|
177 | - * optionally start with $wpdb->prefix or not |
|
178 | - * |
|
179 | - * @param string $table_name |
|
180 | - * @param string $create_sql defining the table's columns and indexes |
|
181 | - * @param string $engine (no need to specify "ENGINE=", that's implied) |
|
182 | - * @return void |
|
183 | - * @throws \EE_Error |
|
184 | - */ |
|
185 | - public function createTable($table_name, $create_sql, $engine = 'MyISAM') |
|
186 | - { |
|
187 | - // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns ) |
|
188 | - if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) { |
|
189 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
190 | - /** @var \wpdb $wpdb */ |
|
191 | - global $wpdb; |
|
192 | - $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
175 | + /** |
|
176 | + * Just creates the requested table. $table_name can |
|
177 | + * optionally start with $wpdb->prefix or not |
|
178 | + * |
|
179 | + * @param string $table_name |
|
180 | + * @param string $create_sql defining the table's columns and indexes |
|
181 | + * @param string $engine (no need to specify "ENGINE=", that's implied) |
|
182 | + * @return void |
|
183 | + * @throws \EE_Error |
|
184 | + */ |
|
185 | + public function createTable($table_name, $create_sql, $engine = 'MyISAM') |
|
186 | + { |
|
187 | + // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns ) |
|
188 | + if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) { |
|
189 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
190 | + /** @var \wpdb $wpdb */ |
|
191 | + global $wpdb; |
|
192 | + $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
193 | 193 | |
194 | - // get $wpdb to echo errors, but buffer them. This way at least WE know an error |
|
195 | - // happened. And then we can choose to tell the end user |
|
196 | - $old_show_errors_policy = $wpdb->show_errors(true); |
|
197 | - $old_error_suppression_policy = $wpdb->suppress_errors(false); |
|
198 | - ob_start(); |
|
199 | - dbDelta($SQL); |
|
200 | - $output = ob_get_contents(); |
|
201 | - ob_end_clean(); |
|
202 | - $wpdb->show_errors($old_show_errors_policy); |
|
203 | - $wpdb->suppress_errors($old_error_suppression_policy); |
|
204 | - if (! empty($output)) { |
|
205 | - throw new \EE_Error($output); |
|
206 | - } |
|
207 | - } else { |
|
208 | - throw new \EE_Error( |
|
209 | - sprintf( |
|
210 | - __( |
|
211 | - 'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s', |
|
212 | - 'event_espresso' |
|
213 | - ), |
|
214 | - '<br />', |
|
215 | - $create_sql |
|
216 | - ) |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
194 | + // get $wpdb to echo errors, but buffer them. This way at least WE know an error |
|
195 | + // happened. And then we can choose to tell the end user |
|
196 | + $old_show_errors_policy = $wpdb->show_errors(true); |
|
197 | + $old_error_suppression_policy = $wpdb->suppress_errors(false); |
|
198 | + ob_start(); |
|
199 | + dbDelta($SQL); |
|
200 | + $output = ob_get_contents(); |
|
201 | + ob_end_clean(); |
|
202 | + $wpdb->show_errors($old_show_errors_policy); |
|
203 | + $wpdb->suppress_errors($old_error_suppression_policy); |
|
204 | + if (! empty($output)) { |
|
205 | + throw new \EE_Error($output); |
|
206 | + } |
|
207 | + } else { |
|
208 | + throw new \EE_Error( |
|
209 | + sprintf( |
|
210 | + __( |
|
211 | + 'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s', |
|
212 | + 'event_espresso' |
|
213 | + ), |
|
214 | + '<br />', |
|
215 | + $create_sql |
|
216 | + ) |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | 221 | |
222 | - /** |
|
223 | - * Drops the specified index if it's size differs from $desired_index_size. |
|
224 | - * WordPress' dbdelta method doesn't automatically change index sizes, so this |
|
225 | - * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal. |
|
226 | - * If the table doesn't exist, or it exists but the index does not, or returns false |
|
227 | - * |
|
228 | - * @param string $table_name |
|
229 | - * @param string $index_name |
|
230 | - * @param string $column_name if none is provided, we assume the column name matches the index (often |
|
231 | - * true in EE) |
|
232 | - * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4. |
|
233 | - * @return bool whether an index was dropped or not |
|
234 | - * @throws /EE_Error if table analysis object isn't defined |
|
235 | - */ |
|
236 | - public function dropIndexIfSizeNot( |
|
237 | - $table_name, |
|
238 | - $index_name, |
|
239 | - $column_name = null, |
|
240 | - $desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE |
|
241 | - ) { |
|
242 | - if ($column_name === null) { |
|
243 | - $column_name = $index_name; |
|
244 | - } |
|
245 | - if (! $this->getTableAnalysis()->tableExists($table_name)) { |
|
246 | - return false; |
|
247 | - } |
|
248 | - $index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name); |
|
249 | - if (empty($index_entries)) { |
|
250 | - return false; |
|
251 | - } |
|
252 | - foreach ($index_entries as $index_entry) { |
|
253 | - if ($column_name === $index_entry->Column_name |
|
254 | - && (string) $desired_index_size !== $index_entry->Sub_part) { |
|
255 | - return $this->dropIndex($table_name, $index_name); |
|
256 | - } |
|
257 | - } |
|
258 | - return false; |
|
259 | - } |
|
222 | + /** |
|
223 | + * Drops the specified index if it's size differs from $desired_index_size. |
|
224 | + * WordPress' dbdelta method doesn't automatically change index sizes, so this |
|
225 | + * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal. |
|
226 | + * If the table doesn't exist, or it exists but the index does not, or returns false |
|
227 | + * |
|
228 | + * @param string $table_name |
|
229 | + * @param string $index_name |
|
230 | + * @param string $column_name if none is provided, we assume the column name matches the index (often |
|
231 | + * true in EE) |
|
232 | + * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4. |
|
233 | + * @return bool whether an index was dropped or not |
|
234 | + * @throws /EE_Error if table analysis object isn't defined |
|
235 | + */ |
|
236 | + public function dropIndexIfSizeNot( |
|
237 | + $table_name, |
|
238 | + $index_name, |
|
239 | + $column_name = null, |
|
240 | + $desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE |
|
241 | + ) { |
|
242 | + if ($column_name === null) { |
|
243 | + $column_name = $index_name; |
|
244 | + } |
|
245 | + if (! $this->getTableAnalysis()->tableExists($table_name)) { |
|
246 | + return false; |
|
247 | + } |
|
248 | + $index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name); |
|
249 | + if (empty($index_entries)) { |
|
250 | + return false; |
|
251 | + } |
|
252 | + foreach ($index_entries as $index_entry) { |
|
253 | + if ($column_name === $index_entry->Column_name |
|
254 | + && (string) $desired_index_size !== $index_entry->Sub_part) { |
|
255 | + return $this->dropIndex($table_name, $index_name); |
|
256 | + } |
|
257 | + } |
|
258 | + return false; |
|
259 | + } |
|
260 | 260 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | global $wpdb; |
66 | 66 | $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
67 | 67 | $columns = $this->getTableColumns($table_name); |
68 | - if (! in_array($column_name, $columns)) { |
|
68 | + if ( ! in_array($column_name, $columns)) { |
|
69 | 69 | $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}"; |
70 | 70 | return $wpdb->query($alter_query); |
71 | 71 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | global $wpdb; |
87 | 87 | $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
88 | 88 | $field_array = array(); |
89 | - if (! empty($table_name)) { |
|
89 | + if ( ! empty($table_name)) { |
|
90 | 90 | $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} "); |
91 | 91 | if ($columns !== false) { |
92 | 92 | foreach ($columns as $column) { |
@@ -132,14 +132,14 @@ discard block |
||
132 | 132 | foreach ($table_names as $table_name) { |
133 | 133 | $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
134 | 134 | if ($this->getTableAnalysis()->tableExists($table_name)) { |
135 | - $tables_to_delete[ $table_name ] = $table_name; |
|
135 | + $tables_to_delete[$table_name] = $table_name; |
|
136 | 136 | } |
137 | 137 | } |
138 | - if (! empty($tables_to_delete)) { |
|
138 | + if ( ! empty($tables_to_delete)) { |
|
139 | 139 | global $wpdb; |
140 | 140 | // make sure we only have a unique strings in the array. |
141 | 141 | $tables_to_delete = array_unique($tables_to_delete); |
142 | - $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
142 | + $wpdb->query('DROP TABLE '.implode(', ', $tables_to_delete)); |
|
143 | 143 | } |
144 | 144 | return $tables_to_delete; |
145 | 145 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
190 | 190 | /** @var \wpdb $wpdb */ |
191 | 191 | global $wpdb; |
192 | - $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
192 | + $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} ".$wpdb->get_charset_collate(); |
|
193 | 193 | |
194 | 194 | // get $wpdb to echo errors, but buffer them. This way at least WE know an error |
195 | 195 | // happened. And then we can choose to tell the end user |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | ob_end_clean(); |
202 | 202 | $wpdb->show_errors($old_show_errors_policy); |
203 | 203 | $wpdb->suppress_errors($old_error_suppression_policy); |
204 | - if (! empty($output)) { |
|
204 | + if ( ! empty($output)) { |
|
205 | 205 | throw new \EE_Error($output); |
206 | 206 | } |
207 | 207 | } else { |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | if ($column_name === null) { |
243 | 243 | $column_name = $index_name; |
244 | 244 | } |
245 | - if (! $this->getTableAnalysis()->tableExists($table_name)) { |
|
245 | + if ( ! $this->getTableAnalysis()->tableExists($table_name)) { |
|
246 | 246 | return false; |
247 | 247 | } |
248 | 248 | $index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name); |
@@ -18,104 +18,104 @@ |
||
18 | 18 | class CommandHandlerManager implements CommandHandlerManagerInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var CommandHandlerInterface[] $command_handlers |
|
23 | - */ |
|
24 | - protected $command_handlers; |
|
21 | + /** |
|
22 | + * @var CommandHandlerInterface[] $command_handlers |
|
23 | + */ |
|
24 | + protected $command_handlers; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @type LoaderInterface $loader |
|
28 | - */ |
|
29 | - private $loader; |
|
26 | + /** |
|
27 | + * @type LoaderInterface $loader |
|
28 | + */ |
|
29 | + private $loader; |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * CommandHandlerManager constructor |
|
34 | - * |
|
35 | - * @param LoaderInterface $loader |
|
36 | - */ |
|
37 | - public function __construct(LoaderInterface $loader) |
|
38 | - { |
|
39 | - $this->loader = $loader; |
|
40 | - } |
|
32 | + /** |
|
33 | + * CommandHandlerManager constructor |
|
34 | + * |
|
35 | + * @param LoaderInterface $loader |
|
36 | + */ |
|
37 | + public function __construct(LoaderInterface $loader) |
|
38 | + { |
|
39 | + $this->loader = $loader; |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * By default, Commands and CommandHandlers would normally |
|
45 | - * reside in the same folder under the same namespace, |
|
46 | - * and the names of the two classes would only differ in that |
|
47 | - * one ends in "Command" and the other ends in "CommandHandler". |
|
48 | - * However, if you wanted to utilize a CommandHandler from somewhere else, |
|
49 | - * then this method allows you to add that CommandHandler and specify the FQCN |
|
50 | - * (Fully Qualified ClassName) for the Command class that it should be used for. |
|
51 | - * For example: |
|
52 | - * by default the "Vendor\some\namespace\DoSomethingCommand" |
|
53 | - * would resolve to using "Vendor\some\namespace\DoSomethingCommandHandler" |
|
54 | - * but if you wanted to instead process that commend using: |
|
55 | - * "Vendor\a\totally\different\namespace\for\DoSomethingCommandHandler" |
|
56 | - * then the following code: |
|
57 | - * $CommandHandlerManager = $this->loader->getShared( 'CommandHandlerManagerInterface' ); |
|
58 | - * $CommandHandlerManager->addCommandHandler( |
|
59 | - * new Vendor\a\totally\different\namespace\for\DoSomethingCommandHandler(), |
|
60 | - * 'Vendor\some\namespace\DoSomethingCommand' |
|
61 | - * ); |
|
62 | - * would result in the alternate CommandHandler being used to process that Command |
|
63 | - * |
|
64 | - * @param CommandHandlerInterface $command_handler |
|
65 | - * @param string $fqcn_for_command Fully Qualified ClassName for Command |
|
66 | - * @return void |
|
67 | - * @throws InvalidCommandHandlerException |
|
68 | - */ |
|
69 | - public function addCommandHandler(CommandHandlerInterface $command_handler, $fqcn_for_command = '') |
|
70 | - { |
|
71 | - $command = ! empty($fqcn_for_command) |
|
72 | - ? $fqcn_for_command |
|
73 | - : str_replace('CommandHandler', 'Command', get_class($command_handler)); |
|
74 | - if (empty($command)) { |
|
75 | - throw new InvalidCommandHandlerException($command); |
|
76 | - } |
|
77 | - $this->command_handlers[ $command ] = $command_handler; |
|
78 | - } |
|
43 | + /** |
|
44 | + * By default, Commands and CommandHandlers would normally |
|
45 | + * reside in the same folder under the same namespace, |
|
46 | + * and the names of the two classes would only differ in that |
|
47 | + * one ends in "Command" and the other ends in "CommandHandler". |
|
48 | + * However, if you wanted to utilize a CommandHandler from somewhere else, |
|
49 | + * then this method allows you to add that CommandHandler and specify the FQCN |
|
50 | + * (Fully Qualified ClassName) for the Command class that it should be used for. |
|
51 | + * For example: |
|
52 | + * by default the "Vendor\some\namespace\DoSomethingCommand" |
|
53 | + * would resolve to using "Vendor\some\namespace\DoSomethingCommandHandler" |
|
54 | + * but if you wanted to instead process that commend using: |
|
55 | + * "Vendor\a\totally\different\namespace\for\DoSomethingCommandHandler" |
|
56 | + * then the following code: |
|
57 | + * $CommandHandlerManager = $this->loader->getShared( 'CommandHandlerManagerInterface' ); |
|
58 | + * $CommandHandlerManager->addCommandHandler( |
|
59 | + * new Vendor\a\totally\different\namespace\for\DoSomethingCommandHandler(), |
|
60 | + * 'Vendor\some\namespace\DoSomethingCommand' |
|
61 | + * ); |
|
62 | + * would result in the alternate CommandHandler being used to process that Command |
|
63 | + * |
|
64 | + * @param CommandHandlerInterface $command_handler |
|
65 | + * @param string $fqcn_for_command Fully Qualified ClassName for Command |
|
66 | + * @return void |
|
67 | + * @throws InvalidCommandHandlerException |
|
68 | + */ |
|
69 | + public function addCommandHandler(CommandHandlerInterface $command_handler, $fqcn_for_command = '') |
|
70 | + { |
|
71 | + $command = ! empty($fqcn_for_command) |
|
72 | + ? $fqcn_for_command |
|
73 | + : str_replace('CommandHandler', 'Command', get_class($command_handler)); |
|
74 | + if (empty($command)) { |
|
75 | + throw new InvalidCommandHandlerException($command); |
|
76 | + } |
|
77 | + $this->command_handlers[ $command ] = $command_handler; |
|
78 | + } |
|
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * @param CommandInterface $command |
|
83 | - * @param CommandBusInterface $command_bus |
|
84 | - * @return mixed |
|
85 | - * @throws DomainException |
|
86 | - * @throws CommandHandlerNotFoundException |
|
87 | - */ |
|
88 | - public function getCommandHandler(CommandInterface $command, CommandBusInterface $command_bus = null) |
|
89 | - { |
|
90 | - $command_name = get_class($command); |
|
91 | - $command_handler = apply_filters( |
|
92 | - 'FHEE__EventEspresso_core_services_commands_CommandHandlerManager__getCommandHandler__command_handler', |
|
93 | - str_replace('Command', 'CommandHandler', $command_name), |
|
94 | - $command |
|
95 | - ); |
|
96 | - $handler = null; |
|
97 | - // has a command handler already been set for this class ? |
|
98 | - // if not, can we find one via the FQCN ? |
|
99 | - if (isset($this->command_handlers[ $command_name ])) { |
|
100 | - $handler = $this->command_handlers[ $command_name ]; |
|
101 | - } elseif (class_exists($command_handler)) { |
|
102 | - $handler = $this->loader->getShared($command_handler); |
|
103 | - } |
|
104 | - // if Handler requires an instance of the CommandBus, but that has not yet been set |
|
105 | - if ($handler instanceof CompositeCommandHandler && ! $handler->commandBus() instanceof CommandBusInterface) { |
|
106 | - if (! $command_bus instanceof CommandBusInterface) { |
|
107 | - throw new DomainException( |
|
108 | - esc_html__( |
|
109 | - 'CompositeCommandHandler classes require an instance of the CommandBus.', |
|
110 | - 'event_espresso' |
|
111 | - ) |
|
112 | - ); |
|
113 | - } |
|
114 | - $handler->setCommandBus($command_bus); |
|
115 | - } |
|
116 | - if ($handler instanceof CommandHandlerInterface) { |
|
117 | - return $handler; |
|
118 | - } |
|
119 | - throw new CommandHandlerNotFoundException($command_handler); |
|
120 | - } |
|
81 | + /** |
|
82 | + * @param CommandInterface $command |
|
83 | + * @param CommandBusInterface $command_bus |
|
84 | + * @return mixed |
|
85 | + * @throws DomainException |
|
86 | + * @throws CommandHandlerNotFoundException |
|
87 | + */ |
|
88 | + public function getCommandHandler(CommandInterface $command, CommandBusInterface $command_bus = null) |
|
89 | + { |
|
90 | + $command_name = get_class($command); |
|
91 | + $command_handler = apply_filters( |
|
92 | + 'FHEE__EventEspresso_core_services_commands_CommandHandlerManager__getCommandHandler__command_handler', |
|
93 | + str_replace('Command', 'CommandHandler', $command_name), |
|
94 | + $command |
|
95 | + ); |
|
96 | + $handler = null; |
|
97 | + // has a command handler already been set for this class ? |
|
98 | + // if not, can we find one via the FQCN ? |
|
99 | + if (isset($this->command_handlers[ $command_name ])) { |
|
100 | + $handler = $this->command_handlers[ $command_name ]; |
|
101 | + } elseif (class_exists($command_handler)) { |
|
102 | + $handler = $this->loader->getShared($command_handler); |
|
103 | + } |
|
104 | + // if Handler requires an instance of the CommandBus, but that has not yet been set |
|
105 | + if ($handler instanceof CompositeCommandHandler && ! $handler->commandBus() instanceof CommandBusInterface) { |
|
106 | + if (! $command_bus instanceof CommandBusInterface) { |
|
107 | + throw new DomainException( |
|
108 | + esc_html__( |
|
109 | + 'CompositeCommandHandler classes require an instance of the CommandBus.', |
|
110 | + 'event_espresso' |
|
111 | + ) |
|
112 | + ); |
|
113 | + } |
|
114 | + $handler->setCommandBus($command_bus); |
|
115 | + } |
|
116 | + if ($handler instanceof CommandHandlerInterface) { |
|
117 | + return $handler; |
|
118 | + } |
|
119 | + throw new CommandHandlerNotFoundException($command_handler); |
|
120 | + } |
|
121 | 121 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | if (empty($command)) { |
75 | 75 | throw new InvalidCommandHandlerException($command); |
76 | 76 | } |
77 | - $this->command_handlers[ $command ] = $command_handler; |
|
77 | + $this->command_handlers[$command] = $command_handler; |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | |
@@ -96,14 +96,14 @@ discard block |
||
96 | 96 | $handler = null; |
97 | 97 | // has a command handler already been set for this class ? |
98 | 98 | // if not, can we find one via the FQCN ? |
99 | - if (isset($this->command_handlers[ $command_name ])) { |
|
100 | - $handler = $this->command_handlers[ $command_name ]; |
|
99 | + if (isset($this->command_handlers[$command_name])) { |
|
100 | + $handler = $this->command_handlers[$command_name]; |
|
101 | 101 | } elseif (class_exists($command_handler)) { |
102 | 102 | $handler = $this->loader->getShared($command_handler); |
103 | 103 | } |
104 | 104 | // if Handler requires an instance of the CommandBus, but that has not yet been set |
105 | 105 | if ($handler instanceof CompositeCommandHandler && ! $handler->commandBus() instanceof CommandBusInterface) { |
106 | - if (! $command_bus instanceof CommandBusInterface) { |
|
106 | + if ( ! $command_bus instanceof CommandBusInterface) { |
|
107 | 107 | throw new DomainException( |
108 | 108 | esc_html__( |
109 | 109 | 'CompositeCommandHandler classes require an instance of the CommandBus.', |
@@ -14,253 +14,253 @@ |
||
14 | 14 | class Notice implements NoticeInterface |
15 | 15 | { |
16 | 16 | |
17 | - const ERROR = 'error'; |
|
18 | - |
|
19 | - const SUCCESS = 'success'; |
|
20 | - |
|
21 | - const ATTENTION = 'attention'; // alias for warning |
|
22 | - |
|
23 | - const INFORMATION = 'information'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string $type |
|
27 | - */ |
|
28 | - private $type; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string $message |
|
33 | - */ |
|
34 | - private $message; |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * @var string $file |
|
39 | - */ |
|
40 | - private $file; |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @var string $func |
|
45 | - */ |
|
46 | - private $func; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string $line |
|
51 | - */ |
|
52 | - private $line; |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @var boolean $dismissible |
|
57 | - */ |
|
58 | - private $dismissible; |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * Notice constructor. |
|
63 | - * |
|
64 | - * @param string $type |
|
65 | - * @param string $message |
|
66 | - * @param bool $dismissible |
|
67 | - * @param string $file |
|
68 | - * @param string $func |
|
69 | - * @param string $line |
|
70 | - * @throws InvalidDataTypeException |
|
71 | - */ |
|
72 | - public function __construct($type, $message, $dismissible = true, $file = '', $func = '', $line = '') |
|
73 | - { |
|
74 | - $this->setType($type); |
|
75 | - $this->setMessage($message); |
|
76 | - $this->setDismissible($dismissible); |
|
77 | - $this->setFile($file); |
|
78 | - $this->setFunc($func); |
|
79 | - $this->setLine($line); |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @return array |
|
85 | - */ |
|
86 | - private function types() |
|
87 | - { |
|
88 | - return (array) apply_filters( |
|
89 | - 'FHEE__EventEspresso_core_services_notices_Notice__types', |
|
90 | - array( |
|
91 | - Notice::ERROR, |
|
92 | - Notice::SUCCESS, |
|
93 | - Notice::ATTENTION, |
|
94 | - Notice::INFORMATION, |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function type() |
|
104 | - { |
|
105 | - return $this->type; |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function message() |
|
113 | - { |
|
114 | - return $this->message; |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function file() |
|
122 | - { |
|
123 | - return $this->file; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function func() |
|
131 | - { |
|
132 | - return $this->func; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function line() |
|
140 | - { |
|
141 | - return $this->line; |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - public function isDismissible() |
|
149 | - { |
|
150 | - return $this->dismissible; |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @param string $type |
|
156 | - * @throws InvalidDataTypeException |
|
157 | - */ |
|
158 | - private function setType($type) |
|
159 | - { |
|
160 | - if (! in_array($type, $this->types(), true)) { |
|
161 | - throw new InvalidDataTypeException( |
|
162 | - '$type', |
|
163 | - $type, |
|
164 | - $this->invalidTypeMessage() |
|
165 | - ); |
|
166 | - } |
|
167 | - $this->type = $type; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * gets the $invalid_type_message string |
|
173 | - */ |
|
174 | - private function invalidTypeMessage() |
|
175 | - { |
|
176 | - return apply_filters( |
|
177 | - 'FHEE__EventEspresso_core_services_notices_Notice__invalidTypeMessage', |
|
178 | - sprintf( |
|
179 | - esc_html__( |
|
180 | - ' one of the following notice types was expected: %1$s %2$s', |
|
181 | - 'event_espresso' |
|
182 | - ), |
|
183 | - '<br />', |
|
184 | - var_export($this->types(), true) |
|
185 | - ) |
|
186 | - ); |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * @param string $message |
|
192 | - * @throws InvalidDataTypeException |
|
193 | - */ |
|
194 | - private function setMessage($message) |
|
195 | - { |
|
196 | - if (empty($message) || ! is_string($message)) { |
|
197 | - throw new InvalidDataTypeException( |
|
198 | - '$message', |
|
199 | - $message, |
|
200 | - esc_html__('non empty string', 'event_espresso') |
|
201 | - ); |
|
202 | - } |
|
203 | - $this->message = $message; |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * @param string $file |
|
209 | - * @throws InvalidDataTypeException |
|
210 | - */ |
|
211 | - private function setFile($file) |
|
212 | - { |
|
213 | - if ($this->type === Notice::ERROR && (empty($file) || ! is_string($file))) { |
|
214 | - throw new InvalidDataTypeException( |
|
215 | - '$file', |
|
216 | - $file, |
|
217 | - esc_html__('non empty string', 'event_espresso') |
|
218 | - ); |
|
219 | - } |
|
220 | - $this->file = $file; |
|
221 | - } |
|
222 | - |
|
223 | - |
|
224 | - /** |
|
225 | - * @param string $func |
|
226 | - * @throws InvalidDataTypeException |
|
227 | - */ |
|
228 | - private function setFunc($func) |
|
229 | - { |
|
230 | - if ($this->type === Notice::ERROR && (empty($func) || ! is_string($func))) { |
|
231 | - throw new InvalidDataTypeException( |
|
232 | - '$func', |
|
233 | - $func, |
|
234 | - esc_html__('non empty string', 'event_espresso') |
|
235 | - ); |
|
236 | - } |
|
237 | - $this->func = $func; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * @param int $line |
|
243 | - * @throws InvalidDataTypeException |
|
244 | - */ |
|
245 | - private function setLine($line) |
|
246 | - { |
|
247 | - $line = absint($line); |
|
248 | - if ($this->type === Notice::ERROR && $line === 0) { |
|
249 | - throw new InvalidDataTypeException( |
|
250 | - '$line', |
|
251 | - $line, |
|
252 | - esc_html__('integer', 'event_espresso') |
|
253 | - ); |
|
254 | - } |
|
255 | - $this->line = $line; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * @param boolean $dismissible |
|
261 | - */ |
|
262 | - private function setDismissible($dismissible = true) |
|
263 | - { |
|
264 | - $this->dismissible = filter_var($dismissible, FILTER_VALIDATE_BOOLEAN); |
|
265 | - } |
|
17 | + const ERROR = 'error'; |
|
18 | + |
|
19 | + const SUCCESS = 'success'; |
|
20 | + |
|
21 | + const ATTENTION = 'attention'; // alias for warning |
|
22 | + |
|
23 | + const INFORMATION = 'information'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string $type |
|
27 | + */ |
|
28 | + private $type; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string $message |
|
33 | + */ |
|
34 | + private $message; |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * @var string $file |
|
39 | + */ |
|
40 | + private $file; |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @var string $func |
|
45 | + */ |
|
46 | + private $func; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string $line |
|
51 | + */ |
|
52 | + private $line; |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @var boolean $dismissible |
|
57 | + */ |
|
58 | + private $dismissible; |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * Notice constructor. |
|
63 | + * |
|
64 | + * @param string $type |
|
65 | + * @param string $message |
|
66 | + * @param bool $dismissible |
|
67 | + * @param string $file |
|
68 | + * @param string $func |
|
69 | + * @param string $line |
|
70 | + * @throws InvalidDataTypeException |
|
71 | + */ |
|
72 | + public function __construct($type, $message, $dismissible = true, $file = '', $func = '', $line = '') |
|
73 | + { |
|
74 | + $this->setType($type); |
|
75 | + $this->setMessage($message); |
|
76 | + $this->setDismissible($dismissible); |
|
77 | + $this->setFile($file); |
|
78 | + $this->setFunc($func); |
|
79 | + $this->setLine($line); |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @return array |
|
85 | + */ |
|
86 | + private function types() |
|
87 | + { |
|
88 | + return (array) apply_filters( |
|
89 | + 'FHEE__EventEspresso_core_services_notices_Notice__types', |
|
90 | + array( |
|
91 | + Notice::ERROR, |
|
92 | + Notice::SUCCESS, |
|
93 | + Notice::ATTENTION, |
|
94 | + Notice::INFORMATION, |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function type() |
|
104 | + { |
|
105 | + return $this->type; |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function message() |
|
113 | + { |
|
114 | + return $this->message; |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function file() |
|
122 | + { |
|
123 | + return $this->file; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function func() |
|
131 | + { |
|
132 | + return $this->func; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function line() |
|
140 | + { |
|
141 | + return $this->line; |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + public function isDismissible() |
|
149 | + { |
|
150 | + return $this->dismissible; |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @param string $type |
|
156 | + * @throws InvalidDataTypeException |
|
157 | + */ |
|
158 | + private function setType($type) |
|
159 | + { |
|
160 | + if (! in_array($type, $this->types(), true)) { |
|
161 | + throw new InvalidDataTypeException( |
|
162 | + '$type', |
|
163 | + $type, |
|
164 | + $this->invalidTypeMessage() |
|
165 | + ); |
|
166 | + } |
|
167 | + $this->type = $type; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * gets the $invalid_type_message string |
|
173 | + */ |
|
174 | + private function invalidTypeMessage() |
|
175 | + { |
|
176 | + return apply_filters( |
|
177 | + 'FHEE__EventEspresso_core_services_notices_Notice__invalidTypeMessage', |
|
178 | + sprintf( |
|
179 | + esc_html__( |
|
180 | + ' one of the following notice types was expected: %1$s %2$s', |
|
181 | + 'event_espresso' |
|
182 | + ), |
|
183 | + '<br />', |
|
184 | + var_export($this->types(), true) |
|
185 | + ) |
|
186 | + ); |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * @param string $message |
|
192 | + * @throws InvalidDataTypeException |
|
193 | + */ |
|
194 | + private function setMessage($message) |
|
195 | + { |
|
196 | + if (empty($message) || ! is_string($message)) { |
|
197 | + throw new InvalidDataTypeException( |
|
198 | + '$message', |
|
199 | + $message, |
|
200 | + esc_html__('non empty string', 'event_espresso') |
|
201 | + ); |
|
202 | + } |
|
203 | + $this->message = $message; |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * @param string $file |
|
209 | + * @throws InvalidDataTypeException |
|
210 | + */ |
|
211 | + private function setFile($file) |
|
212 | + { |
|
213 | + if ($this->type === Notice::ERROR && (empty($file) || ! is_string($file))) { |
|
214 | + throw new InvalidDataTypeException( |
|
215 | + '$file', |
|
216 | + $file, |
|
217 | + esc_html__('non empty string', 'event_espresso') |
|
218 | + ); |
|
219 | + } |
|
220 | + $this->file = $file; |
|
221 | + } |
|
222 | + |
|
223 | + |
|
224 | + /** |
|
225 | + * @param string $func |
|
226 | + * @throws InvalidDataTypeException |
|
227 | + */ |
|
228 | + private function setFunc($func) |
|
229 | + { |
|
230 | + if ($this->type === Notice::ERROR && (empty($func) || ! is_string($func))) { |
|
231 | + throw new InvalidDataTypeException( |
|
232 | + '$func', |
|
233 | + $func, |
|
234 | + esc_html__('non empty string', 'event_espresso') |
|
235 | + ); |
|
236 | + } |
|
237 | + $this->func = $func; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * @param int $line |
|
243 | + * @throws InvalidDataTypeException |
|
244 | + */ |
|
245 | + private function setLine($line) |
|
246 | + { |
|
247 | + $line = absint($line); |
|
248 | + if ($this->type === Notice::ERROR && $line === 0) { |
|
249 | + throw new InvalidDataTypeException( |
|
250 | + '$line', |
|
251 | + $line, |
|
252 | + esc_html__('integer', 'event_espresso') |
|
253 | + ); |
|
254 | + } |
|
255 | + $this->line = $line; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * @param boolean $dismissible |
|
261 | + */ |
|
262 | + private function setDismissible($dismissible = true) |
|
263 | + { |
|
264 | + $this->dismissible = filter_var($dismissible, FILTER_VALIDATE_BOOLEAN); |
|
265 | + } |
|
266 | 266 | } |
@@ -157,7 +157,7 @@ |
||
157 | 157 | */ |
158 | 158 | private function setType($type) |
159 | 159 | { |
160 | - if (! in_array($type, $this->types(), true)) { |
|
160 | + if ( ! in_array($type, $this->types(), true)) { |
|
161 | 161 | throw new InvalidDataTypeException( |
162 | 162 | '$type', |
163 | 163 | $type, |
@@ -18,371 +18,371 @@ |
||
18 | 18 | class Iframe |
19 | 19 | { |
20 | 20 | |
21 | - /* |
|
21 | + /* |
|
22 | 22 | * HTML for notices and ajax gif |
23 | 23 | * @var string $title |
24 | 24 | */ |
25 | - protected $title = ''; |
|
25 | + protected $title = ''; |
|
26 | 26 | |
27 | - /* |
|
27 | + /* |
|
28 | 28 | * HTML for the content being displayed |
29 | 29 | * @var string $content |
30 | 30 | */ |
31 | - protected $content = ''; |
|
31 | + protected $content = ''; |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | * whether or not to call wp_head() and wp_footer() |
35 | 35 | * @var boolean $enqueue_wp_assets |
36 | 36 | */ |
37 | - protected $enqueue_wp_assets = false; |
|
37 | + protected $enqueue_wp_assets = false; |
|
38 | 38 | |
39 | - /* |
|
39 | + /* |
|
40 | 40 | * an array of CSS URLs |
41 | 41 | * @var array $css |
42 | 42 | */ |
43 | - protected $css = array(); |
|
43 | + protected $css = array(); |
|
44 | 44 | |
45 | - /* |
|
45 | + /* |
|
46 | 46 | * an array of JS URLs to be set in the HTML header. |
47 | 47 | * @var array $header_js |
48 | 48 | */ |
49 | - protected $header_js = array(); |
|
49 | + protected $header_js = array(); |
|
50 | 50 | |
51 | - /* |
|
51 | + /* |
|
52 | 52 | * an array of additional attributes to be added to <script> tags for header JS |
53 | 53 | * @var array $footer_js |
54 | 54 | */ |
55 | - protected $header_js_attributes = array(); |
|
55 | + protected $header_js_attributes = array(); |
|
56 | 56 | |
57 | - /* |
|
57 | + /* |
|
58 | 58 | * an array of JS URLs to be displayed before the HTML </body> tag |
59 | 59 | * @var array $footer_js |
60 | 60 | */ |
61 | - protected $footer_js = array(); |
|
61 | + protected $footer_js = array(); |
|
62 | 62 | |
63 | - /* |
|
63 | + /* |
|
64 | 64 | * an array of additional attributes to be added to <script> tags for footer JS |
65 | 65 | * @var array $footer_js_attributes |
66 | 66 | */ |
67 | - protected $footer_js_attributes = array(); |
|
67 | + protected $footer_js_attributes = array(); |
|
68 | 68 | |
69 | - /* |
|
69 | + /* |
|
70 | 70 | * an array of JSON vars to be set in the HTML header. |
71 | 71 | * @var array $localized_vars |
72 | 72 | */ |
73 | - protected $localized_vars = array(); |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Iframe constructor |
|
78 | - * |
|
79 | - * @param string $title |
|
80 | - * @param string $content |
|
81 | - * @throws DomainException |
|
82 | - */ |
|
83 | - public function __construct($title, $content) |
|
84 | - { |
|
85 | - global $wp_version; |
|
86 | - if (! defined('EE_IFRAME_DIR_URL')) { |
|
87 | - define('EE_IFRAME_DIR_URL', plugin_dir_url(__FILE__)); |
|
88 | - } |
|
89 | - $this->setContent($content); |
|
90 | - $this->setTitle($title); |
|
91 | - $this->addStylesheets( |
|
92 | - apply_filters( |
|
93 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_css', |
|
94 | - array( |
|
95 | - 'site_theme' => get_stylesheet_directory_uri() . DS |
|
96 | - . 'style.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
97 | - 'dashicons' => includes_url('css/dashicons.min.css?ver=' . $wp_version), |
|
98 | - 'espresso_default' => EE_GLOBAL_ASSETS_URL |
|
99 | - . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
100 | - ), |
|
101 | - $this |
|
102 | - ) |
|
103 | - ); |
|
104 | - $this->addScripts( |
|
105 | - apply_filters( |
|
106 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_js', |
|
107 | - array( |
|
108 | - 'jquery' => includes_url('js/jquery/jquery.js?ver=' . $wp_version), |
|
109 | - 'espresso_core' => EE_GLOBAL_ASSETS_URL |
|
110 | - . 'scripts/espresso_core.js?ver=' . EVENT_ESPRESSO_VERSION, |
|
111 | - ), |
|
112 | - $this |
|
113 | - ) |
|
114 | - ); |
|
115 | - if (apply_filters( |
|
116 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__load_default_theme_stylesheet', |
|
117 | - false |
|
118 | - )) { |
|
119 | - $this->addStylesheets( |
|
120 | - apply_filters( |
|
121 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_theme_stylesheet', |
|
122 | - array('default_theme_stylesheet' => get_stylesheet_uri()), |
|
123 | - $this |
|
124 | - ) |
|
125 | - ); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * @param string $title |
|
132 | - * @throws DomainException |
|
133 | - */ |
|
134 | - public function setTitle($title) |
|
135 | - { |
|
136 | - if (empty($title)) { |
|
137 | - throw new DomainException( |
|
138 | - esc_html__('You must provide a page title in order to create an iframe.', 'event_espresso') |
|
139 | - ); |
|
140 | - } |
|
141 | - $this->title = $title; |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $content |
|
147 | - * @throws DomainException |
|
148 | - */ |
|
149 | - public function setContent($content) |
|
150 | - { |
|
151 | - if (empty($content)) { |
|
152 | - throw new DomainException( |
|
153 | - esc_html__('You must provide content in order to create an iframe.', 'event_espresso') |
|
154 | - ); |
|
155 | - } |
|
156 | - $this->content = $content; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param boolean $enqueue_wp_assets |
|
162 | - */ |
|
163 | - public function setEnqueueWpAssets($enqueue_wp_assets) |
|
164 | - { |
|
165 | - $this->enqueue_wp_assets = filter_var($enqueue_wp_assets, FILTER_VALIDATE_BOOLEAN); |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @param array $stylesheets |
|
171 | - * @throws DomainException |
|
172 | - */ |
|
173 | - public function addStylesheets(array $stylesheets) |
|
174 | - { |
|
175 | - if (empty($stylesheets)) { |
|
176 | - throw new DomainException( |
|
177 | - esc_html__( |
|
178 | - 'A non-empty array of URLs, is required to add a CSS stylesheet to an iframe.', |
|
179 | - 'event_espresso' |
|
180 | - ) |
|
181 | - ); |
|
182 | - } |
|
183 | - foreach ($stylesheets as $handle => $stylesheet) { |
|
184 | - $this->css[ $handle ] = $stylesheet; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * @param array $scripts |
|
191 | - * @param bool $add_to_header |
|
192 | - * @throws DomainException |
|
193 | - */ |
|
194 | - public function addScripts(array $scripts, $add_to_header = false) |
|
195 | - { |
|
196 | - if (empty($scripts)) { |
|
197 | - throw new DomainException( |
|
198 | - esc_html__( |
|
199 | - 'A non-empty array of URLs, is required to add Javascript to an iframe.', |
|
200 | - 'event_espresso' |
|
201 | - ) |
|
202 | - ); |
|
203 | - } |
|
204 | - foreach ($scripts as $handle => $script) { |
|
205 | - if ($add_to_header) { |
|
206 | - $this->header_js[ $handle ] = $script; |
|
207 | - } else { |
|
208 | - $this->footer_js[ $handle ] = $script; |
|
209 | - } |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * @param array $script_attributes |
|
216 | - * @param bool $add_to_header |
|
217 | - * @throws DomainException |
|
218 | - */ |
|
219 | - public function addScriptAttributes(array $script_attributes, $add_to_header = false) |
|
220 | - { |
|
221 | - if (empty($script_attributes)) { |
|
222 | - throw new DomainException( |
|
223 | - esc_html__( |
|
224 | - 'A non-empty array of strings, is required to add attributes to iframe Javascript.', |
|
225 | - 'event_espresso' |
|
226 | - ) |
|
227 | - ); |
|
228 | - } |
|
229 | - foreach ($script_attributes as $handle => $script_attribute) { |
|
230 | - if ($add_to_header) { |
|
231 | - $this->header_js_attributes[ $handle ] = $script_attribute; |
|
232 | - } else { |
|
233 | - $this->footer_js_attributes[ $handle ] = $script_attribute; |
|
234 | - } |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * @param array $vars |
|
241 | - * @param string $var_name |
|
242 | - * @throws DomainException |
|
243 | - */ |
|
244 | - public function addLocalizedVars(array $vars, $var_name = 'eei18n') |
|
245 | - { |
|
246 | - if (empty($vars)) { |
|
247 | - throw new DomainException( |
|
248 | - esc_html__( |
|
249 | - 'A non-empty array of vars, is required to add localized Javascript vars to an iframe.', |
|
250 | - 'event_espresso' |
|
251 | - ) |
|
252 | - ); |
|
253 | - } |
|
254 | - foreach ($vars as $handle => $var) { |
|
255 | - if ($var_name === 'eei18n') { |
|
256 | - EE_Registry::$i18n_js_strings[ $handle ] = $var; |
|
257 | - } elseif ($var_name === 'eeCAL' && $handle === 'espresso_calendar') { |
|
258 | - $this->localized_vars[ $var_name ] = $var; |
|
259 | - } else { |
|
260 | - if (! isset($this->localized_vars[ $var_name ])) { |
|
261 | - $this->localized_vars[ $var_name ] = array(); |
|
262 | - } |
|
263 | - $this->localized_vars[ $var_name ][ $handle ] = $var; |
|
264 | - } |
|
265 | - } |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * @param string $utm_content |
|
271 | - * @throws DomainException |
|
272 | - */ |
|
273 | - public function display($utm_content = '') |
|
274 | - { |
|
275 | - $this->content .= EEH_Template::powered_by_event_espresso( |
|
276 | - '', |
|
277 | - '', |
|
278 | - ! empty($utm_content) ? array('utm_content' => $utm_content) : array() |
|
279 | - ); |
|
280 | - EE_System::do_not_cache(); |
|
281 | - echo $this->getTemplate(); |
|
282 | - exit; |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * @return string |
|
288 | - * @throws DomainException |
|
289 | - */ |
|
290 | - public function getTemplate() |
|
291 | - { |
|
292 | - return EEH_Template::display_template( |
|
293 | - __DIR__ . DIRECTORY_SEPARATOR . 'iframe_wrapper.template.php', |
|
294 | - array( |
|
295 | - 'title' => apply_filters( |
|
296 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__title', |
|
297 | - $this->title, |
|
298 | - $this |
|
299 | - ), |
|
300 | - 'content' => apply_filters( |
|
301 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__content', |
|
302 | - $this->content, |
|
303 | - $this |
|
304 | - ), |
|
305 | - 'enqueue_wp_assets' => apply_filters( |
|
306 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__enqueue_wp_assets', |
|
307 | - $this->enqueue_wp_assets, |
|
308 | - $this |
|
309 | - ), |
|
310 | - 'css' => (array) apply_filters( |
|
311 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__css_urls', |
|
312 | - $this->css, |
|
313 | - $this |
|
314 | - ), |
|
315 | - 'header_js' => (array) apply_filters( |
|
316 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_urls', |
|
317 | - $this->header_js, |
|
318 | - $this |
|
319 | - ), |
|
320 | - 'header_js_attributes' => (array) apply_filters( |
|
321 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_attributes', |
|
322 | - $this->header_js_attributes, |
|
323 | - $this |
|
324 | - ), |
|
325 | - 'footer_js' => (array) apply_filters( |
|
326 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_urls', |
|
327 | - $this->footer_js, |
|
328 | - $this |
|
329 | - ), |
|
330 | - 'footer_js_attributes' => (array) apply_filters( |
|
331 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_attributes', |
|
332 | - $this->footer_js_attributes, |
|
333 | - $this |
|
334 | - ), |
|
335 | - 'eei18n' => apply_filters( |
|
336 | - 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__eei18n_js_strings', |
|
337 | - EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars(), |
|
338 | - $this |
|
339 | - ), |
|
340 | - 'notices' => EEH_Template::display_template( |
|
341 | - EE_TEMPLATES . 'espresso-ajax-notices.template.php', |
|
342 | - array(), |
|
343 | - true |
|
344 | - ), |
|
345 | - ), |
|
346 | - true, |
|
347 | - true |
|
348 | - ); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * localizeJsonVars |
|
354 | - * |
|
355 | - * @return string |
|
356 | - */ |
|
357 | - public function localizeJsonVars() |
|
358 | - { |
|
359 | - $JSON = ''; |
|
360 | - foreach ((array) $this->localized_vars as $var_name => $vars) { |
|
361 | - $this->localized_vars[ $var_name ] = $this->encodeJsonVars($vars); |
|
362 | - $JSON .= "/* <![CDATA[ */ var {$var_name} = "; |
|
363 | - $JSON .= wp_json_encode($this->localized_vars[ $var_name ]); |
|
364 | - $JSON .= '; /* ]]> */'; |
|
365 | - } |
|
366 | - return $JSON; |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - /** |
|
371 | - * @param bool|int|float|string|array $var |
|
372 | - * @return array |
|
373 | - */ |
|
374 | - public function encodeJsonVars($var) |
|
375 | - { |
|
376 | - if (is_array($var)) { |
|
377 | - $localized_vars = array(); |
|
378 | - foreach ((array) $var as $key => $value) { |
|
379 | - $localized_vars[ $key ] = $this->encodeJsonVars($value); |
|
380 | - } |
|
381 | - return $localized_vars; |
|
382 | - } |
|
383 | - if (is_scalar($var)) { |
|
384 | - return html_entity_decode((string) $var, ENT_QUOTES, 'UTF-8'); |
|
385 | - } |
|
386 | - return null; |
|
387 | - } |
|
73 | + protected $localized_vars = array(); |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Iframe constructor |
|
78 | + * |
|
79 | + * @param string $title |
|
80 | + * @param string $content |
|
81 | + * @throws DomainException |
|
82 | + */ |
|
83 | + public function __construct($title, $content) |
|
84 | + { |
|
85 | + global $wp_version; |
|
86 | + if (! defined('EE_IFRAME_DIR_URL')) { |
|
87 | + define('EE_IFRAME_DIR_URL', plugin_dir_url(__FILE__)); |
|
88 | + } |
|
89 | + $this->setContent($content); |
|
90 | + $this->setTitle($title); |
|
91 | + $this->addStylesheets( |
|
92 | + apply_filters( |
|
93 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_css', |
|
94 | + array( |
|
95 | + 'site_theme' => get_stylesheet_directory_uri() . DS |
|
96 | + . 'style.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
97 | + 'dashicons' => includes_url('css/dashicons.min.css?ver=' . $wp_version), |
|
98 | + 'espresso_default' => EE_GLOBAL_ASSETS_URL |
|
99 | + . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
100 | + ), |
|
101 | + $this |
|
102 | + ) |
|
103 | + ); |
|
104 | + $this->addScripts( |
|
105 | + apply_filters( |
|
106 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_js', |
|
107 | + array( |
|
108 | + 'jquery' => includes_url('js/jquery/jquery.js?ver=' . $wp_version), |
|
109 | + 'espresso_core' => EE_GLOBAL_ASSETS_URL |
|
110 | + . 'scripts/espresso_core.js?ver=' . EVENT_ESPRESSO_VERSION, |
|
111 | + ), |
|
112 | + $this |
|
113 | + ) |
|
114 | + ); |
|
115 | + if (apply_filters( |
|
116 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__load_default_theme_stylesheet', |
|
117 | + false |
|
118 | + )) { |
|
119 | + $this->addStylesheets( |
|
120 | + apply_filters( |
|
121 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_theme_stylesheet', |
|
122 | + array('default_theme_stylesheet' => get_stylesheet_uri()), |
|
123 | + $this |
|
124 | + ) |
|
125 | + ); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * @param string $title |
|
132 | + * @throws DomainException |
|
133 | + */ |
|
134 | + public function setTitle($title) |
|
135 | + { |
|
136 | + if (empty($title)) { |
|
137 | + throw new DomainException( |
|
138 | + esc_html__('You must provide a page title in order to create an iframe.', 'event_espresso') |
|
139 | + ); |
|
140 | + } |
|
141 | + $this->title = $title; |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $content |
|
147 | + * @throws DomainException |
|
148 | + */ |
|
149 | + public function setContent($content) |
|
150 | + { |
|
151 | + if (empty($content)) { |
|
152 | + throw new DomainException( |
|
153 | + esc_html__('You must provide content in order to create an iframe.', 'event_espresso') |
|
154 | + ); |
|
155 | + } |
|
156 | + $this->content = $content; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param boolean $enqueue_wp_assets |
|
162 | + */ |
|
163 | + public function setEnqueueWpAssets($enqueue_wp_assets) |
|
164 | + { |
|
165 | + $this->enqueue_wp_assets = filter_var($enqueue_wp_assets, FILTER_VALIDATE_BOOLEAN); |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @param array $stylesheets |
|
171 | + * @throws DomainException |
|
172 | + */ |
|
173 | + public function addStylesheets(array $stylesheets) |
|
174 | + { |
|
175 | + if (empty($stylesheets)) { |
|
176 | + throw new DomainException( |
|
177 | + esc_html__( |
|
178 | + 'A non-empty array of URLs, is required to add a CSS stylesheet to an iframe.', |
|
179 | + 'event_espresso' |
|
180 | + ) |
|
181 | + ); |
|
182 | + } |
|
183 | + foreach ($stylesheets as $handle => $stylesheet) { |
|
184 | + $this->css[ $handle ] = $stylesheet; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * @param array $scripts |
|
191 | + * @param bool $add_to_header |
|
192 | + * @throws DomainException |
|
193 | + */ |
|
194 | + public function addScripts(array $scripts, $add_to_header = false) |
|
195 | + { |
|
196 | + if (empty($scripts)) { |
|
197 | + throw new DomainException( |
|
198 | + esc_html__( |
|
199 | + 'A non-empty array of URLs, is required to add Javascript to an iframe.', |
|
200 | + 'event_espresso' |
|
201 | + ) |
|
202 | + ); |
|
203 | + } |
|
204 | + foreach ($scripts as $handle => $script) { |
|
205 | + if ($add_to_header) { |
|
206 | + $this->header_js[ $handle ] = $script; |
|
207 | + } else { |
|
208 | + $this->footer_js[ $handle ] = $script; |
|
209 | + } |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * @param array $script_attributes |
|
216 | + * @param bool $add_to_header |
|
217 | + * @throws DomainException |
|
218 | + */ |
|
219 | + public function addScriptAttributes(array $script_attributes, $add_to_header = false) |
|
220 | + { |
|
221 | + if (empty($script_attributes)) { |
|
222 | + throw new DomainException( |
|
223 | + esc_html__( |
|
224 | + 'A non-empty array of strings, is required to add attributes to iframe Javascript.', |
|
225 | + 'event_espresso' |
|
226 | + ) |
|
227 | + ); |
|
228 | + } |
|
229 | + foreach ($script_attributes as $handle => $script_attribute) { |
|
230 | + if ($add_to_header) { |
|
231 | + $this->header_js_attributes[ $handle ] = $script_attribute; |
|
232 | + } else { |
|
233 | + $this->footer_js_attributes[ $handle ] = $script_attribute; |
|
234 | + } |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * @param array $vars |
|
241 | + * @param string $var_name |
|
242 | + * @throws DomainException |
|
243 | + */ |
|
244 | + public function addLocalizedVars(array $vars, $var_name = 'eei18n') |
|
245 | + { |
|
246 | + if (empty($vars)) { |
|
247 | + throw new DomainException( |
|
248 | + esc_html__( |
|
249 | + 'A non-empty array of vars, is required to add localized Javascript vars to an iframe.', |
|
250 | + 'event_espresso' |
|
251 | + ) |
|
252 | + ); |
|
253 | + } |
|
254 | + foreach ($vars as $handle => $var) { |
|
255 | + if ($var_name === 'eei18n') { |
|
256 | + EE_Registry::$i18n_js_strings[ $handle ] = $var; |
|
257 | + } elseif ($var_name === 'eeCAL' && $handle === 'espresso_calendar') { |
|
258 | + $this->localized_vars[ $var_name ] = $var; |
|
259 | + } else { |
|
260 | + if (! isset($this->localized_vars[ $var_name ])) { |
|
261 | + $this->localized_vars[ $var_name ] = array(); |
|
262 | + } |
|
263 | + $this->localized_vars[ $var_name ][ $handle ] = $var; |
|
264 | + } |
|
265 | + } |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * @param string $utm_content |
|
271 | + * @throws DomainException |
|
272 | + */ |
|
273 | + public function display($utm_content = '') |
|
274 | + { |
|
275 | + $this->content .= EEH_Template::powered_by_event_espresso( |
|
276 | + '', |
|
277 | + '', |
|
278 | + ! empty($utm_content) ? array('utm_content' => $utm_content) : array() |
|
279 | + ); |
|
280 | + EE_System::do_not_cache(); |
|
281 | + echo $this->getTemplate(); |
|
282 | + exit; |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * @return string |
|
288 | + * @throws DomainException |
|
289 | + */ |
|
290 | + public function getTemplate() |
|
291 | + { |
|
292 | + return EEH_Template::display_template( |
|
293 | + __DIR__ . DIRECTORY_SEPARATOR . 'iframe_wrapper.template.php', |
|
294 | + array( |
|
295 | + 'title' => apply_filters( |
|
296 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__title', |
|
297 | + $this->title, |
|
298 | + $this |
|
299 | + ), |
|
300 | + 'content' => apply_filters( |
|
301 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__content', |
|
302 | + $this->content, |
|
303 | + $this |
|
304 | + ), |
|
305 | + 'enqueue_wp_assets' => apply_filters( |
|
306 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__enqueue_wp_assets', |
|
307 | + $this->enqueue_wp_assets, |
|
308 | + $this |
|
309 | + ), |
|
310 | + 'css' => (array) apply_filters( |
|
311 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__css_urls', |
|
312 | + $this->css, |
|
313 | + $this |
|
314 | + ), |
|
315 | + 'header_js' => (array) apply_filters( |
|
316 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_urls', |
|
317 | + $this->header_js, |
|
318 | + $this |
|
319 | + ), |
|
320 | + 'header_js_attributes' => (array) apply_filters( |
|
321 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_attributes', |
|
322 | + $this->header_js_attributes, |
|
323 | + $this |
|
324 | + ), |
|
325 | + 'footer_js' => (array) apply_filters( |
|
326 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_urls', |
|
327 | + $this->footer_js, |
|
328 | + $this |
|
329 | + ), |
|
330 | + 'footer_js_attributes' => (array) apply_filters( |
|
331 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_attributes', |
|
332 | + $this->footer_js_attributes, |
|
333 | + $this |
|
334 | + ), |
|
335 | + 'eei18n' => apply_filters( |
|
336 | + 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__eei18n_js_strings', |
|
337 | + EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars(), |
|
338 | + $this |
|
339 | + ), |
|
340 | + 'notices' => EEH_Template::display_template( |
|
341 | + EE_TEMPLATES . 'espresso-ajax-notices.template.php', |
|
342 | + array(), |
|
343 | + true |
|
344 | + ), |
|
345 | + ), |
|
346 | + true, |
|
347 | + true |
|
348 | + ); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * localizeJsonVars |
|
354 | + * |
|
355 | + * @return string |
|
356 | + */ |
|
357 | + public function localizeJsonVars() |
|
358 | + { |
|
359 | + $JSON = ''; |
|
360 | + foreach ((array) $this->localized_vars as $var_name => $vars) { |
|
361 | + $this->localized_vars[ $var_name ] = $this->encodeJsonVars($vars); |
|
362 | + $JSON .= "/* <![CDATA[ */ var {$var_name} = "; |
|
363 | + $JSON .= wp_json_encode($this->localized_vars[ $var_name ]); |
|
364 | + $JSON .= '; /* ]]> */'; |
|
365 | + } |
|
366 | + return $JSON; |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + /** |
|
371 | + * @param bool|int|float|string|array $var |
|
372 | + * @return array |
|
373 | + */ |
|
374 | + public function encodeJsonVars($var) |
|
375 | + { |
|
376 | + if (is_array($var)) { |
|
377 | + $localized_vars = array(); |
|
378 | + foreach ((array) $var as $key => $value) { |
|
379 | + $localized_vars[ $key ] = $this->encodeJsonVars($value); |
|
380 | + } |
|
381 | + return $localized_vars; |
|
382 | + } |
|
383 | + if (is_scalar($var)) { |
|
384 | + return html_entity_decode((string) $var, ENT_QUOTES, 'UTF-8'); |
|
385 | + } |
|
386 | + return null; |
|
387 | + } |
|
388 | 388 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | public function __construct($title, $content) |
84 | 84 | { |
85 | 85 | global $wp_version; |
86 | - if (! defined('EE_IFRAME_DIR_URL')) { |
|
86 | + if ( ! defined('EE_IFRAME_DIR_URL')) { |
|
87 | 87 | define('EE_IFRAME_DIR_URL', plugin_dir_url(__FILE__)); |
88 | 88 | } |
89 | 89 | $this->setContent($content); |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | apply_filters( |
93 | 93 | 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_css', |
94 | 94 | array( |
95 | - 'site_theme' => get_stylesheet_directory_uri() . DS |
|
96 | - . 'style.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
97 | - 'dashicons' => includes_url('css/dashicons.min.css?ver=' . $wp_version), |
|
95 | + 'site_theme' => get_stylesheet_directory_uri().DS |
|
96 | + . 'style.css?ver='.EVENT_ESPRESSO_VERSION, |
|
97 | + 'dashicons' => includes_url('css/dashicons.min.css?ver='.$wp_version), |
|
98 | 98 | 'espresso_default' => EE_GLOBAL_ASSETS_URL |
99 | - . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
99 | + . 'css/espresso_default.css?ver='.EVENT_ESPRESSO_VERSION, |
|
100 | 100 | ), |
101 | 101 | $this |
102 | 102 | ) |
@@ -105,9 +105,9 @@ discard block |
||
105 | 105 | apply_filters( |
106 | 106 | 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_js', |
107 | 107 | array( |
108 | - 'jquery' => includes_url('js/jquery/jquery.js?ver=' . $wp_version), |
|
108 | + 'jquery' => includes_url('js/jquery/jquery.js?ver='.$wp_version), |
|
109 | 109 | 'espresso_core' => EE_GLOBAL_ASSETS_URL |
110 | - . 'scripts/espresso_core.js?ver=' . EVENT_ESPRESSO_VERSION, |
|
110 | + . 'scripts/espresso_core.js?ver='.EVENT_ESPRESSO_VERSION, |
|
111 | 111 | ), |
112 | 112 | $this |
113 | 113 | ) |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | ); |
182 | 182 | } |
183 | 183 | foreach ($stylesheets as $handle => $stylesheet) { |
184 | - $this->css[ $handle ] = $stylesheet; |
|
184 | + $this->css[$handle] = $stylesheet; |
|
185 | 185 | } |
186 | 186 | } |
187 | 187 | |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | } |
204 | 204 | foreach ($scripts as $handle => $script) { |
205 | 205 | if ($add_to_header) { |
206 | - $this->header_js[ $handle ] = $script; |
|
206 | + $this->header_js[$handle] = $script; |
|
207 | 207 | } else { |
208 | - $this->footer_js[ $handle ] = $script; |
|
208 | + $this->footer_js[$handle] = $script; |
|
209 | 209 | } |
210 | 210 | } |
211 | 211 | } |
@@ -228,9 +228,9 @@ discard block |
||
228 | 228 | } |
229 | 229 | foreach ($script_attributes as $handle => $script_attribute) { |
230 | 230 | if ($add_to_header) { |
231 | - $this->header_js_attributes[ $handle ] = $script_attribute; |
|
231 | + $this->header_js_attributes[$handle] = $script_attribute; |
|
232 | 232 | } else { |
233 | - $this->footer_js_attributes[ $handle ] = $script_attribute; |
|
233 | + $this->footer_js_attributes[$handle] = $script_attribute; |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | } |
@@ -253,14 +253,14 @@ discard block |
||
253 | 253 | } |
254 | 254 | foreach ($vars as $handle => $var) { |
255 | 255 | if ($var_name === 'eei18n') { |
256 | - EE_Registry::$i18n_js_strings[ $handle ] = $var; |
|
256 | + EE_Registry::$i18n_js_strings[$handle] = $var; |
|
257 | 257 | } elseif ($var_name === 'eeCAL' && $handle === 'espresso_calendar') { |
258 | - $this->localized_vars[ $var_name ] = $var; |
|
258 | + $this->localized_vars[$var_name] = $var; |
|
259 | 259 | } else { |
260 | - if (! isset($this->localized_vars[ $var_name ])) { |
|
261 | - $this->localized_vars[ $var_name ] = array(); |
|
260 | + if ( ! isset($this->localized_vars[$var_name])) { |
|
261 | + $this->localized_vars[$var_name] = array(); |
|
262 | 262 | } |
263 | - $this->localized_vars[ $var_name ][ $handle ] = $var; |
|
263 | + $this->localized_vars[$var_name][$handle] = $var; |
|
264 | 264 | } |
265 | 265 | } |
266 | 266 | } |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | public function getTemplate() |
291 | 291 | { |
292 | 292 | return EEH_Template::display_template( |
293 | - __DIR__ . DIRECTORY_SEPARATOR . 'iframe_wrapper.template.php', |
|
293 | + __DIR__.DIRECTORY_SEPARATOR.'iframe_wrapper.template.php', |
|
294 | 294 | array( |
295 | 295 | 'title' => apply_filters( |
296 | 296 | 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__title', |
@@ -334,11 +334,11 @@ discard block |
||
334 | 334 | ), |
335 | 335 | 'eei18n' => apply_filters( |
336 | 336 | 'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__eei18n_js_strings', |
337 | - EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars(), |
|
337 | + EE_Registry::localize_i18n_js_strings().$this->localizeJsonVars(), |
|
338 | 338 | $this |
339 | 339 | ), |
340 | 340 | 'notices' => EEH_Template::display_template( |
341 | - EE_TEMPLATES . 'espresso-ajax-notices.template.php', |
|
341 | + EE_TEMPLATES.'espresso-ajax-notices.template.php', |
|
342 | 342 | array(), |
343 | 343 | true |
344 | 344 | ), |
@@ -358,9 +358,9 @@ discard block |
||
358 | 358 | { |
359 | 359 | $JSON = ''; |
360 | 360 | foreach ((array) $this->localized_vars as $var_name => $vars) { |
361 | - $this->localized_vars[ $var_name ] = $this->encodeJsonVars($vars); |
|
361 | + $this->localized_vars[$var_name] = $this->encodeJsonVars($vars); |
|
362 | 362 | $JSON .= "/* <![CDATA[ */ var {$var_name} = "; |
363 | - $JSON .= wp_json_encode($this->localized_vars[ $var_name ]); |
|
363 | + $JSON .= wp_json_encode($this->localized_vars[$var_name]); |
|
364 | 364 | $JSON .= '; /* ]]> */'; |
365 | 365 | } |
366 | 366 | return $JSON; |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | if (is_array($var)) { |
377 | 377 | $localized_vars = array(); |
378 | 378 | foreach ((array) $var as $key => $value) { |
379 | - $localized_vars[ $key ] = $this->encodeJsonVars($value); |
|
379 | + $localized_vars[$key] = $this->encodeJsonVars($value); |
|
380 | 380 | } |
381 | 381 | return $localized_vars; |
382 | 382 | } |
@@ -13,274 +13,274 @@ |
||
13 | 13 | { |
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * @var string $iframe_name |
|
18 | - */ |
|
19 | - private $iframe_name; |
|
16 | + /** |
|
17 | + * @var string $iframe_name |
|
18 | + */ |
|
19 | + private $iframe_name; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var string $route_name |
|
23 | - */ |
|
24 | - private $route_name; |
|
21 | + /** |
|
22 | + * @var string $route_name |
|
23 | + */ |
|
24 | + private $route_name; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var string $slug |
|
28 | - */ |
|
29 | - private $slug; |
|
26 | + /** |
|
27 | + * @var string $slug |
|
28 | + */ |
|
29 | + private $slug; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @var boolean $append_filterable_content |
|
33 | - */ |
|
34 | - private $append_filterable_content; |
|
31 | + /** |
|
32 | + * @var boolean $append_filterable_content |
|
33 | + */ |
|
34 | + private $append_filterable_content; |
|
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * IframeEmbedButton constructor. |
|
39 | - * |
|
40 | - * @param string $iframe_name i18n name for the iframe. This will be used in HTML |
|
41 | - * @param string $route_name the name of the registered route |
|
42 | - * @param string $slug URL slug used for the thing the iframe button is being embedded in. |
|
43 | - * will most likely be "event" since that's the only usage atm |
|
44 | - */ |
|
45 | - public function __construct($iframe_name, $route_name, $slug = 'event') |
|
46 | - { |
|
47 | - $this->iframe_name = $iframe_name; |
|
48 | - $this->route_name = $route_name; |
|
49 | - $this->slug = $slug; |
|
50 | - } |
|
37 | + /** |
|
38 | + * IframeEmbedButton constructor. |
|
39 | + * |
|
40 | + * @param string $iframe_name i18n name for the iframe. This will be used in HTML |
|
41 | + * @param string $route_name the name of the registered route |
|
42 | + * @param string $slug URL slug used for the thing the iframe button is being embedded in. |
|
43 | + * will most likely be "event" since that's the only usage atm |
|
44 | + */ |
|
45 | + public function __construct($iframe_name, $route_name, $slug = 'event') |
|
46 | + { |
|
47 | + $this->iframe_name = $iframe_name; |
|
48 | + $this->route_name = $route_name; |
|
49 | + $this->slug = $slug; |
|
50 | + } |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * Adds an iframe embed code button to the Event editor. |
|
55 | - */ |
|
56 | - public function addEventEditorIframeEmbedButtonFilter() |
|
57 | - { |
|
58 | - // add button for iframe code to event editor. |
|
59 | - add_filter( |
|
60 | - 'get_sample_permalink_html', |
|
61 | - array($this, 'appendIframeEmbedButtonToSamplePermalinkHtml'), |
|
62 | - 10, |
|
63 | - 2 |
|
64 | - ); |
|
65 | - add_action( |
|
66 | - 'admin_enqueue_scripts', |
|
67 | - array($this, 'embedButtonAssets'), |
|
68 | - 10 |
|
69 | - ); |
|
70 | - } |
|
53 | + /** |
|
54 | + * Adds an iframe embed code button to the Event editor. |
|
55 | + */ |
|
56 | + public function addEventEditorIframeEmbedButtonFilter() |
|
57 | + { |
|
58 | + // add button for iframe code to event editor. |
|
59 | + add_filter( |
|
60 | + 'get_sample_permalink_html', |
|
61 | + array($this, 'appendIframeEmbedButtonToSamplePermalinkHtml'), |
|
62 | + 10, |
|
63 | + 2 |
|
64 | + ); |
|
65 | + add_action( |
|
66 | + 'admin_enqueue_scripts', |
|
67 | + array($this, 'embedButtonAssets'), |
|
68 | + 10 |
|
69 | + ); |
|
70 | + } |
|
71 | 71 | |
72 | 72 | |
73 | - /** |
|
74 | - * @param $permalink_string |
|
75 | - * @param $id |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function appendIframeEmbedButtonToSamplePermalinkHtml($permalink_string, $id) |
|
79 | - { |
|
80 | - return $this->eventEditorIframeEmbedButton( |
|
81 | - $permalink_string, |
|
82 | - $id |
|
83 | - ); |
|
84 | - } |
|
73 | + /** |
|
74 | + * @param $permalink_string |
|
75 | + * @param $id |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function appendIframeEmbedButtonToSamplePermalinkHtml($permalink_string, $id) |
|
79 | + { |
|
80 | + return $this->eventEditorIframeEmbedButton( |
|
81 | + $permalink_string, |
|
82 | + $id |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * iframe embed code button to the Event editor. |
|
89 | - * |
|
90 | - * @param string $permalink_string |
|
91 | - * @param int $id |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function eventEditorIframeEmbedButton( |
|
95 | - $permalink_string, |
|
96 | - $id |
|
97 | - ) { |
|
98 | - // make sure this is ONLY when editing and the event id has been set. |
|
99 | - if (! empty($id)) { |
|
100 | - $post = get_post($id); |
|
101 | - // if NOT event then let's get out. |
|
102 | - if ($post->post_type !== 'espresso_events') { |
|
103 | - return $permalink_string; |
|
104 | - } |
|
105 | - $permalink_string .= $this->embedButtonHtml( |
|
106 | - array($this->slug => $id), |
|
107 | - 'button-small' |
|
108 | - ); |
|
109 | - } |
|
110 | - return $permalink_string; |
|
111 | - } |
|
87 | + /** |
|
88 | + * iframe embed code button to the Event editor. |
|
89 | + * |
|
90 | + * @param string $permalink_string |
|
91 | + * @param int $id |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function eventEditorIframeEmbedButton( |
|
95 | + $permalink_string, |
|
96 | + $id |
|
97 | + ) { |
|
98 | + // make sure this is ONLY when editing and the event id has been set. |
|
99 | + if (! empty($id)) { |
|
100 | + $post = get_post($id); |
|
101 | + // if NOT event then let's get out. |
|
102 | + if ($post->post_type !== 'espresso_events') { |
|
103 | + return $permalink_string; |
|
104 | + } |
|
105 | + $permalink_string .= $this->embedButtonHtml( |
|
106 | + array($this->slug => $id), |
|
107 | + 'button-small' |
|
108 | + ); |
|
109 | + } |
|
110 | + return $permalink_string; |
|
111 | + } |
|
112 | 112 | |
113 | 113 | |
114 | - /** |
|
115 | - * Adds an iframe embed code button via a WP do_action() as determined by the first parameter |
|
116 | - * |
|
117 | - * @param string $action name of the WP do_action() to hook into |
|
118 | - */ |
|
119 | - public function addActionIframeEmbedButton($action) |
|
120 | - { |
|
121 | - // add button for iframe code to event editor. |
|
122 | - add_action( |
|
123 | - $action, |
|
124 | - array($this, 'addActionIframeEmbedButtonCallback'), |
|
125 | - 10, |
|
126 | - 2 |
|
127 | - ); |
|
128 | - } |
|
114 | + /** |
|
115 | + * Adds an iframe embed code button via a WP do_action() as determined by the first parameter |
|
116 | + * |
|
117 | + * @param string $action name of the WP do_action() to hook into |
|
118 | + */ |
|
119 | + public function addActionIframeEmbedButton($action) |
|
120 | + { |
|
121 | + // add button for iframe code to event editor. |
|
122 | + add_action( |
|
123 | + $action, |
|
124 | + array($this, 'addActionIframeEmbedButtonCallback'), |
|
125 | + 10, |
|
126 | + 2 |
|
127 | + ); |
|
128 | + } |
|
129 | 129 | |
130 | 130 | |
131 | - /** |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function addActionIframeEmbedButtonCallback() |
|
135 | - { |
|
136 | - echo $this->embedButtonHtml(); |
|
137 | - } |
|
131 | + /** |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function addActionIframeEmbedButtonCallback() |
|
135 | + { |
|
136 | + echo $this->embedButtonHtml(); |
|
137 | + } |
|
138 | 138 | |
139 | 139 | |
140 | - /** |
|
141 | - * Adds an iframe embed code button via a WP apply_filters() as determined by the first parameter |
|
142 | - * |
|
143 | - * @param string $filter name of the WP apply_filters() to hook into |
|
144 | - * @param bool $append if true, will add iframe embed button to end of content, |
|
145 | - * else if false, will add to the beginning of the content |
|
146 | - */ |
|
147 | - public function addFilterIframeEmbedButton($filter, $append = true) |
|
148 | - { |
|
149 | - $this->append_filterable_content = $append; |
|
150 | - // add button for iframe code to event editor. |
|
151 | - add_filter( |
|
152 | - $filter, |
|
153 | - array($this, 'addFilterIframeEmbedButtonCallback'), |
|
154 | - 10 |
|
155 | - ); |
|
156 | - } |
|
140 | + /** |
|
141 | + * Adds an iframe embed code button via a WP apply_filters() as determined by the first parameter |
|
142 | + * |
|
143 | + * @param string $filter name of the WP apply_filters() to hook into |
|
144 | + * @param bool $append if true, will add iframe embed button to end of content, |
|
145 | + * else if false, will add to the beginning of the content |
|
146 | + */ |
|
147 | + public function addFilterIframeEmbedButton($filter, $append = true) |
|
148 | + { |
|
149 | + $this->append_filterable_content = $append; |
|
150 | + // add button for iframe code to event editor. |
|
151 | + add_filter( |
|
152 | + $filter, |
|
153 | + array($this, 'addFilterIframeEmbedButtonCallback'), |
|
154 | + 10 |
|
155 | + ); |
|
156 | + } |
|
157 | 157 | |
158 | 158 | |
159 | - /** |
|
160 | - * @param array|string $filterable_content |
|
161 | - * @return array|string |
|
162 | - */ |
|
163 | - public function addFilterIframeEmbedButtonCallback($filterable_content) |
|
164 | - { |
|
165 | - $embedButtonHtml = $this->embedButtonHtml(); |
|
166 | - if (is_array($filterable_content)) { |
|
167 | - $filterable_content = $this->append_filterable_content |
|
168 | - ? $filterable_content + array($this->route_name => $embedButtonHtml) |
|
169 | - : array($this->route_name => $embedButtonHtml) + $filterable_content; |
|
170 | - } else { |
|
171 | - $filterable_content = $this->append_filterable_content |
|
172 | - ? $filterable_content . $embedButtonHtml |
|
173 | - : $embedButtonHtml . $filterable_content; |
|
174 | - } |
|
175 | - return $filterable_content; |
|
176 | - } |
|
159 | + /** |
|
160 | + * @param array|string $filterable_content |
|
161 | + * @return array|string |
|
162 | + */ |
|
163 | + public function addFilterIframeEmbedButtonCallback($filterable_content) |
|
164 | + { |
|
165 | + $embedButtonHtml = $this->embedButtonHtml(); |
|
166 | + if (is_array($filterable_content)) { |
|
167 | + $filterable_content = $this->append_filterable_content |
|
168 | + ? $filterable_content + array($this->route_name => $embedButtonHtml) |
|
169 | + : array($this->route_name => $embedButtonHtml) + $filterable_content; |
|
170 | + } else { |
|
171 | + $filterable_content = $this->append_filterable_content |
|
172 | + ? $filterable_content . $embedButtonHtml |
|
173 | + : $embedButtonHtml . $filterable_content; |
|
174 | + } |
|
175 | + return $filterable_content; |
|
176 | + } |
|
177 | 177 | |
178 | 178 | |
179 | - /** |
|
180 | - * iframe_embed_html |
|
181 | - * |
|
182 | - * @param array $query_args |
|
183 | - * @param string $button_class |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - public function embedButtonHtml($query_args = array(), $button_class = '') |
|
187 | - { |
|
188 | - // incoming args will replace the defaults listed here in the second array (union preserves first array) |
|
189 | - $query_args = (array) $query_args + array($this->route_name => 'iframe'); |
|
190 | - $query_args = (array) apply_filters( |
|
191 | - 'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__embedButtonHtml__query_args', |
|
192 | - $query_args |
|
193 | - ); |
|
194 | - // add this route to our localized vars |
|
195 | - $iframe_module_routes = isset(\EE_Registry::$i18n_js_strings['iframe_module_routes']) |
|
196 | - ? \EE_Registry::$i18n_js_strings['iframe_module_routes'] |
|
197 | - : array(); |
|
198 | - $iframe_module_routes[ $this->route_name ] = $this->route_name; |
|
199 | - \EE_Registry::$i18n_js_strings['iframe_module_routes'] = $iframe_module_routes; |
|
200 | - $iframe_embed_html = \EEH_HTML::link( |
|
201 | - '#', |
|
202 | - sprintf(esc_html__('Embed %1$s', 'event_espresso'), $this->iframe_name), |
|
203 | - sprintf( |
|
204 | - esc_html__( |
|
205 | - 'click here to generate code for embedding %1$s iframe into another site.', |
|
206 | - 'event_espresso' |
|
207 | - ), |
|
208 | - \EEH_Inflector::add_indefinite_article($this->iframe_name) |
|
209 | - ), |
|
210 | - "{$this->route_name}-iframe-embed-trigger-js", |
|
211 | - 'iframe-embed-trigger-js button ' . $button_class, |
|
212 | - '', |
|
213 | - ' data-iframe_embed_button="#' . $this->route_name . '-iframe-js" tabindex="-1"' |
|
214 | - ); |
|
215 | - $iframe_embed_html .= \EEH_HTML::div( |
|
216 | - '', |
|
217 | - "{$this->route_name}-iframe-js", |
|
218 | - 'iframe-embed-wrapper-js', |
|
219 | - 'display:none;' |
|
220 | - ); |
|
221 | - $iframe_embed_html .= esc_html( |
|
222 | - \EEH_HTML::div( |
|
223 | - '<iframe src="' . add_query_arg($query_args, site_url()) . '" width="100%" height="100%"></iframe>', |
|
224 | - '', |
|
225 | - '', |
|
226 | - 'width:100%; height: 500px;' |
|
227 | - ) |
|
228 | - ); |
|
229 | - $iframe_embed_html .= \EEH_HTML::divx(); |
|
230 | - return $iframe_embed_html; |
|
231 | - } |
|
179 | + /** |
|
180 | + * iframe_embed_html |
|
181 | + * |
|
182 | + * @param array $query_args |
|
183 | + * @param string $button_class |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + public function embedButtonHtml($query_args = array(), $button_class = '') |
|
187 | + { |
|
188 | + // incoming args will replace the defaults listed here in the second array (union preserves first array) |
|
189 | + $query_args = (array) $query_args + array($this->route_name => 'iframe'); |
|
190 | + $query_args = (array) apply_filters( |
|
191 | + 'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__embedButtonHtml__query_args', |
|
192 | + $query_args |
|
193 | + ); |
|
194 | + // add this route to our localized vars |
|
195 | + $iframe_module_routes = isset(\EE_Registry::$i18n_js_strings['iframe_module_routes']) |
|
196 | + ? \EE_Registry::$i18n_js_strings['iframe_module_routes'] |
|
197 | + : array(); |
|
198 | + $iframe_module_routes[ $this->route_name ] = $this->route_name; |
|
199 | + \EE_Registry::$i18n_js_strings['iframe_module_routes'] = $iframe_module_routes; |
|
200 | + $iframe_embed_html = \EEH_HTML::link( |
|
201 | + '#', |
|
202 | + sprintf(esc_html__('Embed %1$s', 'event_espresso'), $this->iframe_name), |
|
203 | + sprintf( |
|
204 | + esc_html__( |
|
205 | + 'click here to generate code for embedding %1$s iframe into another site.', |
|
206 | + 'event_espresso' |
|
207 | + ), |
|
208 | + \EEH_Inflector::add_indefinite_article($this->iframe_name) |
|
209 | + ), |
|
210 | + "{$this->route_name}-iframe-embed-trigger-js", |
|
211 | + 'iframe-embed-trigger-js button ' . $button_class, |
|
212 | + '', |
|
213 | + ' data-iframe_embed_button="#' . $this->route_name . '-iframe-js" tabindex="-1"' |
|
214 | + ); |
|
215 | + $iframe_embed_html .= \EEH_HTML::div( |
|
216 | + '', |
|
217 | + "{$this->route_name}-iframe-js", |
|
218 | + 'iframe-embed-wrapper-js', |
|
219 | + 'display:none;' |
|
220 | + ); |
|
221 | + $iframe_embed_html .= esc_html( |
|
222 | + \EEH_HTML::div( |
|
223 | + '<iframe src="' . add_query_arg($query_args, site_url()) . '" width="100%" height="100%"></iframe>', |
|
224 | + '', |
|
225 | + '', |
|
226 | + 'width:100%; height: 500px;' |
|
227 | + ) |
|
228 | + ); |
|
229 | + $iframe_embed_html .= \EEH_HTML::divx(); |
|
230 | + return $iframe_embed_html; |
|
231 | + } |
|
232 | 232 | |
233 | 233 | |
234 | - /** |
|
235 | - * enqueue iframe button js |
|
236 | - */ |
|
237 | - public function embedButtonAssets() |
|
238 | - { |
|
239 | - \EE_Registry::$i18n_js_strings['iframe_embed_title'] = esc_html__( |
|
240 | - 'copy and paste the following into any other site\'s content to display this event:', |
|
241 | - 'event_espresso' |
|
242 | - ); |
|
243 | - \EE_Registry::$i18n_js_strings['iframe_embed_close_msg'] = esc_html__( |
|
244 | - 'click anywhere outside of this window to close it.', |
|
245 | - 'event_espresso' |
|
246 | - ); |
|
247 | - wp_register_script( |
|
248 | - 'iframe_embed_button', |
|
249 | - plugin_dir_url(__FILE__) . 'iframe-embed-button.js', |
|
250 | - array('ee-dialog'), |
|
251 | - EVENT_ESPRESSO_VERSION, |
|
252 | - true |
|
253 | - ); |
|
254 | - wp_enqueue_script('iframe_embed_button'); |
|
255 | - } |
|
234 | + /** |
|
235 | + * enqueue iframe button js |
|
236 | + */ |
|
237 | + public function embedButtonAssets() |
|
238 | + { |
|
239 | + \EE_Registry::$i18n_js_strings['iframe_embed_title'] = esc_html__( |
|
240 | + 'copy and paste the following into any other site\'s content to display this event:', |
|
241 | + 'event_espresso' |
|
242 | + ); |
|
243 | + \EE_Registry::$i18n_js_strings['iframe_embed_close_msg'] = esc_html__( |
|
244 | + 'click anywhere outside of this window to close it.', |
|
245 | + 'event_espresso' |
|
246 | + ); |
|
247 | + wp_register_script( |
|
248 | + 'iframe_embed_button', |
|
249 | + plugin_dir_url(__FILE__) . 'iframe-embed-button.js', |
|
250 | + array('ee-dialog'), |
|
251 | + EVENT_ESPRESSO_VERSION, |
|
252 | + true |
|
253 | + ); |
|
254 | + wp_enqueue_script('iframe_embed_button'); |
|
255 | + } |
|
256 | 256 | |
257 | 257 | |
258 | - /** |
|
259 | - * generates embed button sections for admin pages |
|
260 | - * |
|
261 | - * @param array $embed_buttons |
|
262 | - * @return string |
|
263 | - */ |
|
264 | - public function addIframeEmbedButtonsSection(array $embed_buttons) |
|
265 | - { |
|
266 | - $embed_buttons = (array) apply_filters( |
|
267 | - 'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__addIframeEmbedButtonsSection__embed_buttons', |
|
268 | - $embed_buttons |
|
269 | - ); |
|
270 | - if (empty($embed_buttons)) { |
|
271 | - return ''; |
|
272 | - } |
|
273 | - // add button for iframe code to event editor. |
|
274 | - $html = \EEH_HTML::br(2); |
|
275 | - $html .= \EEH_HTML::h3(esc_html__('iFrame Embed Code', 'event_espresso')); |
|
276 | - $html .= \EEH_HTML::p( |
|
277 | - esc_html__( |
|
278 | - 'Click the following button(s) to generate iframe HTML that will allow you to embed your event content within the content of other websites.', |
|
279 | - 'event_espresso' |
|
280 | - ) |
|
281 | - ); |
|
282 | - $html .= ' ' . implode(' ', $embed_buttons) . ' '; |
|
283 | - $html .= \EEH_HTML::br(2); |
|
284 | - return $html; |
|
285 | - } |
|
258 | + /** |
|
259 | + * generates embed button sections for admin pages |
|
260 | + * |
|
261 | + * @param array $embed_buttons |
|
262 | + * @return string |
|
263 | + */ |
|
264 | + public function addIframeEmbedButtonsSection(array $embed_buttons) |
|
265 | + { |
|
266 | + $embed_buttons = (array) apply_filters( |
|
267 | + 'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__addIframeEmbedButtonsSection__embed_buttons', |
|
268 | + $embed_buttons |
|
269 | + ); |
|
270 | + if (empty($embed_buttons)) { |
|
271 | + return ''; |
|
272 | + } |
|
273 | + // add button for iframe code to event editor. |
|
274 | + $html = \EEH_HTML::br(2); |
|
275 | + $html .= \EEH_HTML::h3(esc_html__('iFrame Embed Code', 'event_espresso')); |
|
276 | + $html .= \EEH_HTML::p( |
|
277 | + esc_html__( |
|
278 | + 'Click the following button(s) to generate iframe HTML that will allow you to embed your event content within the content of other websites.', |
|
279 | + 'event_espresso' |
|
280 | + ) |
|
281 | + ); |
|
282 | + $html .= ' ' . implode(' ', $embed_buttons) . ' '; |
|
283 | + $html .= \EEH_HTML::br(2); |
|
284 | + return $html; |
|
285 | + } |
|
286 | 286 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | $id |
97 | 97 | ) { |
98 | 98 | // make sure this is ONLY when editing and the event id has been set. |
99 | - if (! empty($id)) { |
|
99 | + if ( ! empty($id)) { |
|
100 | 100 | $post = get_post($id); |
101 | 101 | // if NOT event then let's get out. |
102 | 102 | if ($post->post_type !== 'espresso_events') { |
@@ -169,8 +169,8 @@ discard block |
||
169 | 169 | : array($this->route_name => $embedButtonHtml) + $filterable_content; |
170 | 170 | } else { |
171 | 171 | $filterable_content = $this->append_filterable_content |
172 | - ? $filterable_content . $embedButtonHtml |
|
173 | - : $embedButtonHtml . $filterable_content; |
|
172 | + ? $filterable_content.$embedButtonHtml |
|
173 | + : $embedButtonHtml.$filterable_content; |
|
174 | 174 | } |
175 | 175 | return $filterable_content; |
176 | 176 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | $iframe_module_routes = isset(\EE_Registry::$i18n_js_strings['iframe_module_routes']) |
196 | 196 | ? \EE_Registry::$i18n_js_strings['iframe_module_routes'] |
197 | 197 | : array(); |
198 | - $iframe_module_routes[ $this->route_name ] = $this->route_name; |
|
198 | + $iframe_module_routes[$this->route_name] = $this->route_name; |
|
199 | 199 | \EE_Registry::$i18n_js_strings['iframe_module_routes'] = $iframe_module_routes; |
200 | 200 | $iframe_embed_html = \EEH_HTML::link( |
201 | 201 | '#', |
@@ -208,9 +208,9 @@ discard block |
||
208 | 208 | \EEH_Inflector::add_indefinite_article($this->iframe_name) |
209 | 209 | ), |
210 | 210 | "{$this->route_name}-iframe-embed-trigger-js", |
211 | - 'iframe-embed-trigger-js button ' . $button_class, |
|
211 | + 'iframe-embed-trigger-js button '.$button_class, |
|
212 | 212 | '', |
213 | - ' data-iframe_embed_button="#' . $this->route_name . '-iframe-js" tabindex="-1"' |
|
213 | + ' data-iframe_embed_button="#'.$this->route_name.'-iframe-js" tabindex="-1"' |
|
214 | 214 | ); |
215 | 215 | $iframe_embed_html .= \EEH_HTML::div( |
216 | 216 | '', |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | ); |
221 | 221 | $iframe_embed_html .= esc_html( |
222 | 222 | \EEH_HTML::div( |
223 | - '<iframe src="' . add_query_arg($query_args, site_url()) . '" width="100%" height="100%"></iframe>', |
|
223 | + '<iframe src="'.add_query_arg($query_args, site_url()).'" width="100%" height="100%"></iframe>', |
|
224 | 224 | '', |
225 | 225 | '', |
226 | 226 | 'width:100%; height: 500px;' |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | ); |
247 | 247 | wp_register_script( |
248 | 248 | 'iframe_embed_button', |
249 | - plugin_dir_url(__FILE__) . 'iframe-embed-button.js', |
|
249 | + plugin_dir_url(__FILE__).'iframe-embed-button.js', |
|
250 | 250 | array('ee-dialog'), |
251 | 251 | EVENT_ESPRESSO_VERSION, |
252 | 252 | true |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | 'event_espresso' |
280 | 280 | ) |
281 | 281 | ); |
282 | - $html .= ' ' . implode(' ', $embed_buttons) . ' '; |
|
282 | + $html .= ' '.implode(' ', $embed_buttons).' '; |
|
283 | 283 | $html .= \EEH_HTML::br(2); |
284 | 284 | return $html; |
285 | 285 | } |
@@ -22,338 +22,338 @@ |
||
22 | 22 | class Base |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @deprecated use all-caps version |
|
27 | - */ |
|
28 | - // @codingStandardsIgnoreStart |
|
29 | - const header_prefix_for_ee = 'X-EE-'; |
|
30 | - // @codingStandardsIgnoreEnd |
|
31 | - |
|
32 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
33 | - |
|
34 | - /** |
|
35 | - * @deprecated use all-caps version instead |
|
36 | - */ |
|
37 | - // @codingStandardsIgnoreStart |
|
38 | - const header_prefix_for_wp = 'X-WP-'; |
|
39 | - // @codingStandardsIgnoreEnd |
|
40 | - |
|
41 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
42 | - |
|
43 | - /** |
|
44 | - * Contains debug info we'll send back in the response headers |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected $debug_info = array(); |
|
49 | - |
|
50 | - /** |
|
51 | - * Indicates whether or not the API is in debug mode |
|
52 | - * |
|
53 | - * @var boolean |
|
54 | - */ |
|
55 | - protected $debug_mode = false; |
|
56 | - |
|
57 | - /** |
|
58 | - * Indicates the version that was requested |
|
59 | - * |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - protected $requested_version; |
|
63 | - |
|
64 | - /** |
|
65 | - * flat array of headers to send in the response |
|
66 | - * |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - protected $response_headers = array(); |
|
70 | - |
|
71 | - |
|
72 | - public function __construct() |
|
73 | - { |
|
74 | - $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
75 | - // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
76 | - add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Sets the version the user requested |
|
82 | - * |
|
83 | - * @param string $version eg '4.8' |
|
84 | - */ |
|
85 | - public function setRequestedVersion($version) |
|
86 | - { |
|
87 | - $this->requested_version = $version; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Sets some debug info that we'll send back in headers |
|
93 | - * |
|
94 | - * @param string $key |
|
95 | - * @param string|array $info |
|
96 | - */ |
|
97 | - protected function setDebugInfo($key, $info) |
|
98 | - { |
|
99 | - $this->debug_info[ $key ] = $info; |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Sets headers for the response |
|
105 | - * |
|
106 | - * @param string $header_key , excluding the "X-EE-" part |
|
107 | - * @param array|string $value if an array, multiple headers will be added, one |
|
108 | - * for each key in the array |
|
109 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
110 | - * the standard WP one |
|
111 | - */ |
|
112 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
113 | - { |
|
114 | - if (is_array($value)) { |
|
115 | - foreach ($value as $value_key => $value_value) { |
|
116 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
117 | - } |
|
118 | - } else { |
|
119 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
120 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns a flat array of headers to be added to the response |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - protected function getResponseHeaders() |
|
131 | - { |
|
132 | - return apply_filters( |
|
133 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
134 | - $this->response_headers, |
|
135 | - $this, |
|
136 | - $this->requested_version |
|
137 | - ); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
143 | - * |
|
144 | - * @param WP_Error $wp_error_response |
|
145 | - * @return WP_Error |
|
146 | - */ |
|
147 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
148 | - { |
|
149 | - $notices_during_checkin = EE_Error::get_raw_notices(); |
|
150 | - if (! empty($notices_during_checkin['errors'])) { |
|
151 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
152 | - $wp_error_response->add( |
|
153 | - sanitize_key($error_code), |
|
154 | - strip_tags($error_message) |
|
155 | - ); |
|
156 | - } |
|
157 | - } |
|
158 | - return $wp_error_response; |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Sends a response, but also makes sure to attach headers that |
|
164 | - * are handy for debugging. |
|
165 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
166 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
167 | - * the response, others? |
|
168 | - * |
|
169 | - * @param array|WP_Error|Exception|RestException $response |
|
170 | - * @return WP_REST_Response |
|
171 | - */ |
|
172 | - public function sendResponse($response) |
|
173 | - { |
|
174 | - if ($response instanceof RestException) { |
|
175 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
176 | - } |
|
177 | - if ($response instanceof Exception) { |
|
178 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
179 | - $response = new WP_Error($code, $response->getMessage()); |
|
180 | - } |
|
181 | - if ($response instanceof WP_Error) { |
|
182 | - $response = $this->addEeErrorsToResponse($response); |
|
183 | - $rest_response = $this->createRestResponseFromWpError($response); |
|
184 | - } else { |
|
185 | - $rest_response = new WP_REST_Response($response, 200); |
|
186 | - } |
|
187 | - $headers = array(); |
|
188 | - if ($this->debug_mode && is_array($this->debug_info)) { |
|
189 | - foreach ($this->debug_info as $debug_key => $debug_info) { |
|
190 | - if (is_array($debug_info)) { |
|
191 | - $debug_info = wp_json_encode($debug_info); |
|
192 | - } |
|
193 | - $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
194 | - } |
|
195 | - } |
|
196 | - $headers = array_merge( |
|
197 | - $headers, |
|
198 | - $this->getResponseHeaders(), |
|
199 | - $this->getHeadersFromEeNotices() |
|
200 | - ); |
|
201 | - $rest_response->set_headers($headers); |
|
202 | - return $rest_response; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Converts the \WP_Error into `WP_REST_Response. |
|
208 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
209 | - * (which is protected) |
|
210 | - * |
|
211 | - * @param WP_Error $wp_error |
|
212 | - * @return WP_REST_Response |
|
213 | - */ |
|
214 | - protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
215 | - { |
|
216 | - $error_data = $wp_error->get_error_data(); |
|
217 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
218 | - $status = $error_data['status']; |
|
219 | - } else { |
|
220 | - $status = 500; |
|
221 | - } |
|
222 | - $errors = array(); |
|
223 | - foreach ((array) $wp_error->errors as $code => $messages) { |
|
224 | - foreach ((array) $messages as $message) { |
|
225 | - $errors[] = array( |
|
226 | - 'code' => $code, |
|
227 | - 'message' => $message, |
|
228 | - 'data' => $wp_error->get_error_data($code), |
|
229 | - ); |
|
230 | - } |
|
231 | - } |
|
232 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
233 | - if (count($errors) > 1) { |
|
234 | - // Remove the primary error. |
|
235 | - array_shift($errors); |
|
236 | - $data['additional_errors'] = $errors; |
|
237 | - } |
|
238 | - return new WP_REST_Response($data, $status); |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * Array of headers derived from EE success, attention, and error messages |
|
244 | - * |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - protected function getHeadersFromEeNotices() |
|
248 | - { |
|
249 | - $headers = array(); |
|
250 | - $notices = EE_Error::get_raw_notices(); |
|
251 | - foreach ($notices as $notice_type => $sub_notices) { |
|
252 | - if (! is_array($sub_notices)) { |
|
253 | - continue; |
|
254 | - } |
|
255 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
256 | - $headers[ 'X-EE4-Notices-' |
|
257 | - . EEH_Inflector::humanize($notice_type) |
|
258 | - . '[' |
|
259 | - . $notice_code |
|
260 | - . ']' ] = strip_tags($sub_notice); |
|
261 | - } |
|
262 | - } |
|
263 | - return apply_filters( |
|
264 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
265 | - $headers, |
|
266 | - $this->requested_version, |
|
267 | - $notices |
|
268 | - ); |
|
269 | - } |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * Finds which version of the API was requested given the route, and returns it. |
|
274 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
275 | - * "4.8.29". |
|
276 | - * We should know hte requested version in this model though, so if no route is |
|
277 | - * provided just use what we set earlier |
|
278 | - * |
|
279 | - * @param string $route |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - public function getRequestedVersion($route = null) |
|
283 | - { |
|
284 | - if ($route === null) { |
|
285 | - return $this->requested_version; |
|
286 | - } |
|
287 | - $matches = $this->parseRoute( |
|
288 | - $route, |
|
289 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
290 | - array('version') |
|
291 | - ); |
|
292 | - if (isset($matches['version'])) { |
|
293 | - return $matches['version']; |
|
294 | - } else { |
|
295 | - return EED_Core_Rest_Api::latest_rest_api_version(); |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * Applies the regex to the route, then creates an array using the values of |
|
302 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
303 | - * For example, if you call |
|
304 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
305 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
306 | - * |
|
307 | - * @param string $route |
|
308 | - * @param string $regex |
|
309 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
310 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
311 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
312 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
313 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
314 | - * @throws EE_Error if it couldn't be parsed |
|
315 | - */ |
|
316 | - public function parseRoute($route, $regex, $match_keys) |
|
317 | - { |
|
318 | - $indexed_matches = array(); |
|
319 | - $success = preg_match($regex, $route, $matches); |
|
320 | - if (is_array($matches)) { |
|
321 | - // skip the overall regex match. Who cares |
|
322 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
323 | - if (! isset($matches[ $i ])) { |
|
324 | - $success = false; |
|
325 | - } else { |
|
326 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
327 | - } |
|
328 | - } |
|
329 | - } |
|
330 | - if (! $success) { |
|
331 | - throw new EE_Error( |
|
332 | - __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
333 | - 'endpoint_parsing_error' |
|
334 | - ); |
|
335 | - } |
|
336 | - return $indexed_matches; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
342 | - * |
|
343 | - * @param \WP_REST_Request $request |
|
344 | - * @return array |
|
345 | - */ |
|
346 | - protected function getBodyParams(\WP_REST_Request $request) |
|
347 | - { |
|
348 | - // $request->get_params(); |
|
349 | - return array_merge( |
|
350 | - (array) $request->get_body_params(), |
|
351 | - (array) $request->get_json_params() |
|
352 | - ); |
|
353 | - // return array_diff_key( |
|
354 | - // $request->get_params(), |
|
355 | - // $request->get_url_params(), |
|
356 | - // $request->get_query_params() |
|
357 | - // ); |
|
358 | - } |
|
25 | + /** |
|
26 | + * @deprecated use all-caps version |
|
27 | + */ |
|
28 | + // @codingStandardsIgnoreStart |
|
29 | + const header_prefix_for_ee = 'X-EE-'; |
|
30 | + // @codingStandardsIgnoreEnd |
|
31 | + |
|
32 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
33 | + |
|
34 | + /** |
|
35 | + * @deprecated use all-caps version instead |
|
36 | + */ |
|
37 | + // @codingStandardsIgnoreStart |
|
38 | + const header_prefix_for_wp = 'X-WP-'; |
|
39 | + // @codingStandardsIgnoreEnd |
|
40 | + |
|
41 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
42 | + |
|
43 | + /** |
|
44 | + * Contains debug info we'll send back in the response headers |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected $debug_info = array(); |
|
49 | + |
|
50 | + /** |
|
51 | + * Indicates whether or not the API is in debug mode |
|
52 | + * |
|
53 | + * @var boolean |
|
54 | + */ |
|
55 | + protected $debug_mode = false; |
|
56 | + |
|
57 | + /** |
|
58 | + * Indicates the version that was requested |
|
59 | + * |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + protected $requested_version; |
|
63 | + |
|
64 | + /** |
|
65 | + * flat array of headers to send in the response |
|
66 | + * |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + protected $response_headers = array(); |
|
70 | + |
|
71 | + |
|
72 | + public function __construct() |
|
73 | + { |
|
74 | + $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
75 | + // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
76 | + add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Sets the version the user requested |
|
82 | + * |
|
83 | + * @param string $version eg '4.8' |
|
84 | + */ |
|
85 | + public function setRequestedVersion($version) |
|
86 | + { |
|
87 | + $this->requested_version = $version; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Sets some debug info that we'll send back in headers |
|
93 | + * |
|
94 | + * @param string $key |
|
95 | + * @param string|array $info |
|
96 | + */ |
|
97 | + protected function setDebugInfo($key, $info) |
|
98 | + { |
|
99 | + $this->debug_info[ $key ] = $info; |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Sets headers for the response |
|
105 | + * |
|
106 | + * @param string $header_key , excluding the "X-EE-" part |
|
107 | + * @param array|string $value if an array, multiple headers will be added, one |
|
108 | + * for each key in the array |
|
109 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
110 | + * the standard WP one |
|
111 | + */ |
|
112 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
113 | + { |
|
114 | + if (is_array($value)) { |
|
115 | + foreach ($value as $value_key => $value_value) { |
|
116 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
117 | + } |
|
118 | + } else { |
|
119 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
120 | + $this->response_headers[ $prefix . $header_key ] = $value; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns a flat array of headers to be added to the response |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + protected function getResponseHeaders() |
|
131 | + { |
|
132 | + return apply_filters( |
|
133 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
134 | + $this->response_headers, |
|
135 | + $this, |
|
136 | + $this->requested_version |
|
137 | + ); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
143 | + * |
|
144 | + * @param WP_Error $wp_error_response |
|
145 | + * @return WP_Error |
|
146 | + */ |
|
147 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
148 | + { |
|
149 | + $notices_during_checkin = EE_Error::get_raw_notices(); |
|
150 | + if (! empty($notices_during_checkin['errors'])) { |
|
151 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
152 | + $wp_error_response->add( |
|
153 | + sanitize_key($error_code), |
|
154 | + strip_tags($error_message) |
|
155 | + ); |
|
156 | + } |
|
157 | + } |
|
158 | + return $wp_error_response; |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Sends a response, but also makes sure to attach headers that |
|
164 | + * are handy for debugging. |
|
165 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
166 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
167 | + * the response, others? |
|
168 | + * |
|
169 | + * @param array|WP_Error|Exception|RestException $response |
|
170 | + * @return WP_REST_Response |
|
171 | + */ |
|
172 | + public function sendResponse($response) |
|
173 | + { |
|
174 | + if ($response instanceof RestException) { |
|
175 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
176 | + } |
|
177 | + if ($response instanceof Exception) { |
|
178 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
179 | + $response = new WP_Error($code, $response->getMessage()); |
|
180 | + } |
|
181 | + if ($response instanceof WP_Error) { |
|
182 | + $response = $this->addEeErrorsToResponse($response); |
|
183 | + $rest_response = $this->createRestResponseFromWpError($response); |
|
184 | + } else { |
|
185 | + $rest_response = new WP_REST_Response($response, 200); |
|
186 | + } |
|
187 | + $headers = array(); |
|
188 | + if ($this->debug_mode && is_array($this->debug_info)) { |
|
189 | + foreach ($this->debug_info as $debug_key => $debug_info) { |
|
190 | + if (is_array($debug_info)) { |
|
191 | + $debug_info = wp_json_encode($debug_info); |
|
192 | + } |
|
193 | + $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
194 | + } |
|
195 | + } |
|
196 | + $headers = array_merge( |
|
197 | + $headers, |
|
198 | + $this->getResponseHeaders(), |
|
199 | + $this->getHeadersFromEeNotices() |
|
200 | + ); |
|
201 | + $rest_response->set_headers($headers); |
|
202 | + return $rest_response; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Converts the \WP_Error into `WP_REST_Response. |
|
208 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
209 | + * (which is protected) |
|
210 | + * |
|
211 | + * @param WP_Error $wp_error |
|
212 | + * @return WP_REST_Response |
|
213 | + */ |
|
214 | + protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
215 | + { |
|
216 | + $error_data = $wp_error->get_error_data(); |
|
217 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
218 | + $status = $error_data['status']; |
|
219 | + } else { |
|
220 | + $status = 500; |
|
221 | + } |
|
222 | + $errors = array(); |
|
223 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
224 | + foreach ((array) $messages as $message) { |
|
225 | + $errors[] = array( |
|
226 | + 'code' => $code, |
|
227 | + 'message' => $message, |
|
228 | + 'data' => $wp_error->get_error_data($code), |
|
229 | + ); |
|
230 | + } |
|
231 | + } |
|
232 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
233 | + if (count($errors) > 1) { |
|
234 | + // Remove the primary error. |
|
235 | + array_shift($errors); |
|
236 | + $data['additional_errors'] = $errors; |
|
237 | + } |
|
238 | + return new WP_REST_Response($data, $status); |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * Array of headers derived from EE success, attention, and error messages |
|
244 | + * |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + protected function getHeadersFromEeNotices() |
|
248 | + { |
|
249 | + $headers = array(); |
|
250 | + $notices = EE_Error::get_raw_notices(); |
|
251 | + foreach ($notices as $notice_type => $sub_notices) { |
|
252 | + if (! is_array($sub_notices)) { |
|
253 | + continue; |
|
254 | + } |
|
255 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
256 | + $headers[ 'X-EE4-Notices-' |
|
257 | + . EEH_Inflector::humanize($notice_type) |
|
258 | + . '[' |
|
259 | + . $notice_code |
|
260 | + . ']' ] = strip_tags($sub_notice); |
|
261 | + } |
|
262 | + } |
|
263 | + return apply_filters( |
|
264 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
265 | + $headers, |
|
266 | + $this->requested_version, |
|
267 | + $notices |
|
268 | + ); |
|
269 | + } |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * Finds which version of the API was requested given the route, and returns it. |
|
274 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
275 | + * "4.8.29". |
|
276 | + * We should know hte requested version in this model though, so if no route is |
|
277 | + * provided just use what we set earlier |
|
278 | + * |
|
279 | + * @param string $route |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + public function getRequestedVersion($route = null) |
|
283 | + { |
|
284 | + if ($route === null) { |
|
285 | + return $this->requested_version; |
|
286 | + } |
|
287 | + $matches = $this->parseRoute( |
|
288 | + $route, |
|
289 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
290 | + array('version') |
|
291 | + ); |
|
292 | + if (isset($matches['version'])) { |
|
293 | + return $matches['version']; |
|
294 | + } else { |
|
295 | + return EED_Core_Rest_Api::latest_rest_api_version(); |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * Applies the regex to the route, then creates an array using the values of |
|
302 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
303 | + * For example, if you call |
|
304 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
305 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
306 | + * |
|
307 | + * @param string $route |
|
308 | + * @param string $regex |
|
309 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
310 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
311 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
312 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
313 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
314 | + * @throws EE_Error if it couldn't be parsed |
|
315 | + */ |
|
316 | + public function parseRoute($route, $regex, $match_keys) |
|
317 | + { |
|
318 | + $indexed_matches = array(); |
|
319 | + $success = preg_match($regex, $route, $matches); |
|
320 | + if (is_array($matches)) { |
|
321 | + // skip the overall regex match. Who cares |
|
322 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
323 | + if (! isset($matches[ $i ])) { |
|
324 | + $success = false; |
|
325 | + } else { |
|
326 | + $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
327 | + } |
|
328 | + } |
|
329 | + } |
|
330 | + if (! $success) { |
|
331 | + throw new EE_Error( |
|
332 | + __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
333 | + 'endpoint_parsing_error' |
|
334 | + ); |
|
335 | + } |
|
336 | + return $indexed_matches; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
342 | + * |
|
343 | + * @param \WP_REST_Request $request |
|
344 | + * @return array |
|
345 | + */ |
|
346 | + protected function getBodyParams(\WP_REST_Request $request) |
|
347 | + { |
|
348 | + // $request->get_params(); |
|
349 | + return array_merge( |
|
350 | + (array) $request->get_body_params(), |
|
351 | + (array) $request->get_json_params() |
|
352 | + ); |
|
353 | + // return array_diff_key( |
|
354 | + // $request->get_params(), |
|
355 | + // $request->get_url_params(), |
|
356 | + // $request->get_query_params() |
|
357 | + // ); |
|
358 | + } |
|
359 | 359 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | protected function setDebugInfo($key, $info) |
98 | 98 | { |
99 | - $this->debug_info[ $key ] = $info; |
|
99 | + $this->debug_info[$key] = $info; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | |
@@ -113,11 +113,11 @@ discard block |
||
113 | 113 | { |
114 | 114 | if (is_array($value)) { |
115 | 115 | foreach ($value as $value_key => $value_value) { |
116 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
116 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); |
|
117 | 117 | } |
118 | 118 | } else { |
119 | 119 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
120 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
120 | + $this->response_headers[$prefix.$header_key] = $value; |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 | |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
148 | 148 | { |
149 | 149 | $notices_during_checkin = EE_Error::get_raw_notices(); |
150 | - if (! empty($notices_during_checkin['errors'])) { |
|
150 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
151 | 151 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
152 | 152 | $wp_error_response->add( |
153 | 153 | sanitize_key($error_code), |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | if (is_array($debug_info)) { |
191 | 191 | $debug_info = wp_json_encode($debug_info); |
192 | 192 | } |
193 | - $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
193 | + $headers['X-EE4-Debug-'.ucwords($debug_key)] = $debug_info; |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | $headers = array_merge( |
@@ -249,15 +249,15 @@ discard block |
||
249 | 249 | $headers = array(); |
250 | 250 | $notices = EE_Error::get_raw_notices(); |
251 | 251 | foreach ($notices as $notice_type => $sub_notices) { |
252 | - if (! is_array($sub_notices)) { |
|
252 | + if ( ! is_array($sub_notices)) { |
|
253 | 253 | continue; |
254 | 254 | } |
255 | 255 | foreach ($sub_notices as $notice_code => $sub_notice) { |
256 | - $headers[ 'X-EE4-Notices-' |
|
256 | + $headers['X-EE4-Notices-' |
|
257 | 257 | . EEH_Inflector::humanize($notice_type) |
258 | 258 | . '[' |
259 | 259 | . $notice_code |
260 | - . ']' ] = strip_tags($sub_notice); |
|
260 | + . ']'] = strip_tags($sub_notice); |
|
261 | 261 | } |
262 | 262 | } |
263 | 263 | return apply_filters( |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | } |
287 | 287 | $matches = $this->parseRoute( |
288 | 288 | $route, |
289 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
289 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
290 | 290 | array('version') |
291 | 291 | ); |
292 | 292 | if (isset($matches['version'])) { |
@@ -320,14 +320,14 @@ discard block |
||
320 | 320 | if (is_array($matches)) { |
321 | 321 | // skip the overall regex match. Who cares |
322 | 322 | for ($i = 1; $i <= count($match_keys); $i++) { |
323 | - if (! isset($matches[ $i ])) { |
|
323 | + if ( ! isset($matches[$i])) { |
|
324 | 324 | $success = false; |
325 | 325 | } else { |
326 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
326 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
327 | 327 | } |
328 | 328 | } |
329 | 329 | } |
330 | - if (! $success) { |
|
330 | + if ( ! $success) { |
|
331 | 331 | throw new EE_Error( |
332 | 332 | __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
333 | 333 | 'endpoint_parsing_error' |
@@ -20,85 +20,85 @@ |
||
20 | 20 | class Read |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @param WP_REST_Request $request |
|
25 | - * @param string $version |
|
26 | - * @return EE_Config|WP_Error |
|
27 | - */ |
|
28 | - public static function handleRequest(WP_REST_Request $request, $version) |
|
29 | - { |
|
30 | - $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
31 | - if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
32 | - return EE_Config::instance(); |
|
33 | - } else { |
|
34 | - return new WP_Error( |
|
35 | - 'cannot_read_config', |
|
36 | - sprintf( |
|
37 | - __( |
|
38 | - 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
39 | - 'event_espresso' |
|
40 | - ), |
|
41 | - $cap |
|
42 | - ), |
|
43 | - array('status' => 403) |
|
44 | - ); |
|
45 | - } |
|
46 | - } |
|
23 | + /** |
|
24 | + * @param WP_REST_Request $request |
|
25 | + * @param string $version |
|
26 | + * @return EE_Config|WP_Error |
|
27 | + */ |
|
28 | + public static function handleRequest(WP_REST_Request $request, $version) |
|
29 | + { |
|
30 | + $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
31 | + if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
32 | + return EE_Config::instance(); |
|
33 | + } else { |
|
34 | + return new WP_Error( |
|
35 | + 'cannot_read_config', |
|
36 | + sprintf( |
|
37 | + __( |
|
38 | + 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
39 | + 'event_espresso' |
|
40 | + ), |
|
41 | + $cap |
|
42 | + ), |
|
43 | + array('status' => 403) |
|
44 | + ); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Handles the request for public site info |
|
51 | - * |
|
52 | - * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
53 | - * current user could be authenticated using basic auth |
|
54 | - * data |
|
55 | - * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
56 | - * basic auth data was somehow received |
|
57 | - * @param WP_REST_Request $request |
|
58 | - * @param string $version |
|
59 | - * @return array|WP_Error |
|
60 | - */ |
|
61 | - public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
62 | - { |
|
63 | - global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
64 | - $insecure_usage_of_basic_auth = apply_filters( |
|
65 | - // @codingStandardsIgnoreStart |
|
66 | - 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
67 | - // @codingStandardsIgnoreEnd |
|
68 | - $wp_json_basic_auth_success && ! is_ssl(), |
|
69 | - $request |
|
70 | - ); |
|
71 | - if ($insecure_usage_of_basic_auth) { |
|
72 | - $warning = sprintf( |
|
73 | - esc_html__( |
|
74 | - // @codingStandardsIgnoreStart |
|
75 | - 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
76 | - // @codingStandardsIgnoreEnd |
|
77 | - 'event_espresso' |
|
78 | - ), |
|
79 | - '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
80 | - '</a>' |
|
81 | - ); |
|
82 | - } else { |
|
83 | - $warning = ''; |
|
84 | - } |
|
85 | - return apply_filters( |
|
86 | - 'FHEE__EventEspresso_core_libraries_rest_api_controllers_config__handleRequestSiteInfo__return_val', |
|
87 | - array( |
|
88 | - 'default_timezone' => array( |
|
89 | - 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
90 | - 'string' => get_option('timezone_string'), |
|
91 | - 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
92 | - ), |
|
93 | - 'default_currency' => EE_Config::instance()->currency, |
|
94 | - 'authentication' => array( |
|
95 | - 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
96 | - 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
97 | - 'warning' => $warning, |
|
98 | - ), |
|
99 | - ) |
|
100 | - ); |
|
101 | - } |
|
49 | + /** |
|
50 | + * Handles the request for public site info |
|
51 | + * |
|
52 | + * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
53 | + * current user could be authenticated using basic auth |
|
54 | + * data |
|
55 | + * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
56 | + * basic auth data was somehow received |
|
57 | + * @param WP_REST_Request $request |
|
58 | + * @param string $version |
|
59 | + * @return array|WP_Error |
|
60 | + */ |
|
61 | + public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
62 | + { |
|
63 | + global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
64 | + $insecure_usage_of_basic_auth = apply_filters( |
|
65 | + // @codingStandardsIgnoreStart |
|
66 | + 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
67 | + // @codingStandardsIgnoreEnd |
|
68 | + $wp_json_basic_auth_success && ! is_ssl(), |
|
69 | + $request |
|
70 | + ); |
|
71 | + if ($insecure_usage_of_basic_auth) { |
|
72 | + $warning = sprintf( |
|
73 | + esc_html__( |
|
74 | + // @codingStandardsIgnoreStart |
|
75 | + 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
76 | + // @codingStandardsIgnoreEnd |
|
77 | + 'event_espresso' |
|
78 | + ), |
|
79 | + '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
80 | + '</a>' |
|
81 | + ); |
|
82 | + } else { |
|
83 | + $warning = ''; |
|
84 | + } |
|
85 | + return apply_filters( |
|
86 | + 'FHEE__EventEspresso_core_libraries_rest_api_controllers_config__handleRequestSiteInfo__return_val', |
|
87 | + array( |
|
88 | + 'default_timezone' => array( |
|
89 | + 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
90 | + 'string' => get_option('timezone_string'), |
|
91 | + 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
92 | + ), |
|
93 | + 'default_currency' => EE_Config::instance()->currency, |
|
94 | + 'authentication' => array( |
|
95 | + 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
96 | + 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
97 | + 'warning' => $warning, |
|
98 | + ), |
|
99 | + ) |
|
100 | + ); |
|
101 | + } |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | // End of file Read.php |
@@ -35,1323 +35,1323 @@ |
||
35 | 35 | { |
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @var CalculatedModelFields |
|
40 | - */ |
|
41 | - protected $fields_calculator; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Read constructor. |
|
46 | - */ |
|
47 | - public function __construct() |
|
48 | - { |
|
49 | - parent::__construct(); |
|
50 | - $this->fields_calculator = new CalculatedModelFields(); |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
56 | - * |
|
57 | - * @param WP_REST_Request $request |
|
58 | - * @param string $version |
|
59 | - * @param string $model_name |
|
60 | - * @return \WP_REST_Response|WP_Error |
|
61 | - */ |
|
62 | - public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
63 | - { |
|
64 | - $controller = new Read(); |
|
65 | - try { |
|
66 | - $controller->setRequestedVersion($version); |
|
67 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | - return $controller->sendResponse( |
|
69 | - new WP_Error( |
|
70 | - 'endpoint_parsing_error', |
|
71 | - sprintf( |
|
72 | - __( |
|
73 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
74 | - 'event_espresso' |
|
75 | - ), |
|
76 | - $model_name |
|
77 | - ) |
|
78 | - ) |
|
79 | - ); |
|
80 | - } |
|
81 | - return $controller->sendResponse( |
|
82 | - $controller->getEntitiesFromModel( |
|
83 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
84 | - $request |
|
85 | - ) |
|
86 | - ); |
|
87 | - } catch (Exception $e) { |
|
88 | - return $controller->sendResponse($e); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Prepares and returns schema for any OPTIONS request. |
|
95 | - * |
|
96 | - * @param string $version The API endpoint version being used. |
|
97 | - * @param string $model_name Something like `Event` or `Registration` |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public static function handleSchemaRequest($version, $model_name) |
|
101 | - { |
|
102 | - $controller = new Read(); |
|
103 | - try { |
|
104 | - $controller->setRequestedVersion($version); |
|
105 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | - return array(); |
|
107 | - } |
|
108 | - // get the model for this version |
|
109 | - $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
110 | - $model_schema = new JsonModelSchema($model); |
|
111 | - return $model_schema->getModelSchemaForRelations( |
|
112 | - $controller->getModelVersionInfo()->relationSettings($model), |
|
113 | - $controller->customizeSchemaForRestResponse( |
|
114 | - $model, |
|
115 | - $model_schema->getModelSchemaForFields( |
|
116 | - $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
117 | - $model_schema->getInitialSchemaStructure() |
|
118 | - ) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } catch (Exception $e) { |
|
122 | - return array(); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * This loops through each field in the given schema for the model and does the following: |
|
129 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
130 | - * - transform default values into the correct format for a REST API response. |
|
131 | - * |
|
132 | - * @param EEM_Base $model |
|
133 | - * @param array $schema |
|
134 | - * @return array The final schema. |
|
135 | - */ |
|
136 | - protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
137 | - { |
|
138 | - foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
139 | - $schema = $this->translateDefaultsForRestResponse( |
|
140 | - $field_name, |
|
141 | - $field, |
|
142 | - $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
143 | - ); |
|
144 | - } |
|
145 | - return $schema; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
151 | - * response. |
|
152 | - * |
|
153 | - * @param $field_name |
|
154 | - * @param EE_Model_Field_Base $field |
|
155 | - * @param array $schema |
|
156 | - * @return array |
|
157 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
158 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
159 | - */ |
|
160 | - protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
161 | - { |
|
162 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
165 | - if ($default_key === 'raw') { |
|
166 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
167 | - ModelDataTranslator::prepareFieldValueForJson( |
|
168 | - $field, |
|
169 | - $default_value, |
|
170 | - $this->getModelVersionInfo()->requestedVersion() |
|
171 | - ); |
|
172 | - } |
|
173 | - } |
|
174 | - } else { |
|
175 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | - $field, |
|
177 | - $schema['properties'][ $field_name ]['default'], |
|
178 | - $this->getModelVersionInfo()->requestedVersion() |
|
179 | - ); |
|
180 | - } |
|
181 | - } |
|
182 | - return $schema; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Adds additional fields to the schema |
|
188 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
189 | - * needs to be added to the schema. |
|
190 | - * |
|
191 | - * @param $field_name |
|
192 | - * @param EE_Model_Field_Base $field |
|
193 | - * @param array $schema |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
197 | - { |
|
198 | - if ($field instanceof EE_Datetime_Field) { |
|
199 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
200 | - // modify the description |
|
201 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
202 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
203 | - wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
204 | - ); |
|
205 | - } |
|
206 | - return $schema; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
212 | - * |
|
213 | - * @return string |
|
214 | - */ |
|
215 | - protected function getRouteFromRequest() |
|
216 | - { |
|
217 | - if (isset($GLOBALS['wp']) |
|
218 | - && $GLOBALS['wp'] instanceof \WP |
|
219 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
220 | - ) { |
|
221 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
222 | - } else { |
|
223 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Gets a single entity related to the model indicated in the path and its id |
|
230 | - * |
|
231 | - * @param WP_REST_Request $request |
|
232 | - * @param string $version |
|
233 | - * @param string $model_name |
|
234 | - * @return \WP_REST_Response|WP_Error |
|
235 | - */ |
|
236 | - public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
237 | - { |
|
238 | - $controller = new Read(); |
|
239 | - try { |
|
240 | - $controller->setRequestedVersion($version); |
|
241 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | - return $controller->sendResponse( |
|
243 | - new WP_Error( |
|
244 | - 'endpoint_parsing_error', |
|
245 | - sprintf( |
|
246 | - __( |
|
247 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
248 | - 'event_espresso' |
|
249 | - ), |
|
250 | - $model_name |
|
251 | - ) |
|
252 | - ) |
|
253 | - ); |
|
254 | - } |
|
255 | - return $controller->sendResponse( |
|
256 | - $controller->getEntityFromModel( |
|
257 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
258 | - $request |
|
259 | - ) |
|
260 | - ); |
|
261 | - } catch (Exception $e) { |
|
262 | - return $controller->sendResponse($e); |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
269 | - * to the item with the given id |
|
270 | - * |
|
271 | - * @param WP_REST_Request $request |
|
272 | - * @param string $version |
|
273 | - * @param string $model_name |
|
274 | - * @param string $related_model_name |
|
275 | - * @return \WP_REST_Response|WP_Error |
|
276 | - */ |
|
277 | - public static function handleRequestGetRelated( |
|
278 | - WP_REST_Request $request, |
|
279 | - $version, |
|
280 | - $model_name, |
|
281 | - $related_model_name |
|
282 | - ) { |
|
283 | - $controller = new Read(); |
|
284 | - try { |
|
285 | - $controller->setRequestedVersion($version); |
|
286 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | - return $controller->sendResponse( |
|
288 | - new WP_Error( |
|
289 | - 'endpoint_parsing_error', |
|
290 | - sprintf( |
|
291 | - __( |
|
292 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
293 | - 'event_espresso' |
|
294 | - ), |
|
295 | - $model_name |
|
296 | - ) |
|
297 | - ) |
|
298 | - ); |
|
299 | - } |
|
300 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
301 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | - return $controller->sendResponse( |
|
303 | - new WP_Error( |
|
304 | - 'endpoint_parsing_error', |
|
305 | - sprintf( |
|
306 | - __( |
|
307 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
308 | - 'event_espresso' |
|
309 | - ), |
|
310 | - $related_model_name |
|
311 | - ) |
|
312 | - ) |
|
313 | - ); |
|
314 | - } |
|
315 | - return $controller->sendResponse( |
|
316 | - $controller->getEntitiesFromRelation( |
|
317 | - $request->get_param('id'), |
|
318 | - $main_model->related_settings_for($related_model_name), |
|
319 | - $request |
|
320 | - ) |
|
321 | - ); |
|
322 | - } catch (Exception $e) { |
|
323 | - return $controller->sendResponse($e); |
|
324 | - } |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - /** |
|
329 | - * Gets a collection for the given model and filters |
|
330 | - * |
|
331 | - * @param EEM_Base $model |
|
332 | - * @param WP_REST_Request $request |
|
333 | - * @return array|WP_Error |
|
334 | - */ |
|
335 | - public function getEntitiesFromModel($model, $request) |
|
336 | - { |
|
337 | - $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
338 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | - $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
340 | - return new WP_Error( |
|
341 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
342 | - sprintf( |
|
343 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
344 | - $model_name_plural, |
|
345 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
346 | - ), |
|
347 | - array('status' => 403) |
|
348 | - ); |
|
349 | - } |
|
350 | - if (! $request->get_header('no_rest_headers')) { |
|
351 | - $this->setHeadersFromQueryParams($model, $query_params); |
|
352 | - } |
|
353 | - /** @type array $results */ |
|
354 | - $results = $model->get_all_wpdb_results($query_params); |
|
355 | - $nice_results = array(); |
|
356 | - foreach ($results as $result) { |
|
357 | - $nice_results[] = $this->createEntityFromWpdbResult( |
|
358 | - $model, |
|
359 | - $result, |
|
360 | - $request |
|
361 | - ); |
|
362 | - } |
|
363 | - return $nice_results; |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Gets the collection for given relation object |
|
369 | - * The same as Read::get_entities_from_model(), except if the relation |
|
370 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
371 | - * the join-model-object into the results |
|
372 | - * |
|
373 | - * @param array $primary_model_query_params query params for finding the item from which |
|
374 | - * relations will be based |
|
375 | - * @param \EE_Model_Relation_Base $relation |
|
376 | - * @param WP_REST_Request $request |
|
377 | - * @return WP_Error|array |
|
378 | - * @throws RestException |
|
379 | - */ |
|
380 | - protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
381 | - { |
|
382 | - $context = $this->validateContext($request->get_param('caps')); |
|
383 | - $model = $relation->get_this_model(); |
|
384 | - $related_model = $relation->get_other_model(); |
|
385 | - if (! isset($primary_model_query_params[0])) { |
|
386 | - $primary_model_query_params[0] = array(); |
|
387 | - } |
|
388 | - // check if they can access the 1st model object |
|
389 | - $primary_model_query_params = array( |
|
390 | - 0 => $primary_model_query_params[0], |
|
391 | - 'limit' => 1, |
|
392 | - ); |
|
393 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
394 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
395 | - $primary_model_query_params |
|
396 | - ); |
|
397 | - } |
|
398 | - $restricted_query_params = $primary_model_query_params; |
|
399 | - $restricted_query_params['caps'] = $context; |
|
400 | - $this->setDebugInfo('main model query params', $restricted_query_params); |
|
401 | - $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
402 | - if (! ( |
|
403 | - Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
404 | - && $model->exists($restricted_query_params) |
|
405 | - ) |
|
406 | - ) { |
|
407 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
408 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
409 | - } else { |
|
410 | - $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
411 | - $related_model->get_this_model_name() |
|
412 | - ); |
|
413 | - } |
|
414 | - return new WP_Error( |
|
415 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
416 | - sprintf( |
|
417 | - __( |
|
418 | - 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
419 | - 'event_espresso' |
|
420 | - ), |
|
421 | - $related_model_name_maybe_plural, |
|
422 | - $relation->get_this_model()->get_this_model_name(), |
|
423 | - implode( |
|
424 | - ',', |
|
425 | - array_keys( |
|
426 | - Capabilities::getMissingPermissions($related_model, $context) |
|
427 | - ) |
|
428 | - ) |
|
429 | - ), |
|
430 | - array('status' => 403) |
|
431 | - ); |
|
432 | - } |
|
433 | - $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
434 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
435 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
436 | - . '.' |
|
437 | - . $where_condition_key ] = $where_condition_value; |
|
438 | - } |
|
439 | - $query_params['default_where_conditions'] = 'none'; |
|
440 | - $query_params['caps'] = $context; |
|
441 | - if (! $request->get_header('no_rest_headers')) { |
|
442 | - $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
443 | - } |
|
444 | - /** @type array $results */ |
|
445 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
446 | - $nice_results = array(); |
|
447 | - foreach ($results as $result) { |
|
448 | - $nice_result = $this->createEntityFromWpdbResult( |
|
449 | - $relation->get_other_model(), |
|
450 | - $result, |
|
451 | - $request |
|
452 | - ); |
|
453 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
454 | - // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
455 | - // if there are conflicts we prefer the properties from the main model |
|
456 | - $join_model_result = $this->createEntityFromWpdbResult( |
|
457 | - $relation->get_join_model(), |
|
458 | - $result, |
|
459 | - $request |
|
460 | - ); |
|
461 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
462 | - // but keep the meta stuff from the main model |
|
463 | - if (isset($nice_result['meta'])) { |
|
464 | - $joined_result['meta'] = $nice_result['meta']; |
|
465 | - } |
|
466 | - $nice_result = $joined_result; |
|
467 | - } |
|
468 | - $nice_results[] = $nice_result; |
|
469 | - } |
|
470 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
471 | - return array_shift($nice_results); |
|
472 | - } else { |
|
473 | - return $nice_results; |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * Gets the collection for given relation object |
|
480 | - * The same as Read::get_entities_from_model(), except if the relation |
|
481 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
482 | - * the join-model-object into the results |
|
483 | - * |
|
484 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
485 | - * @param \EE_Model_Relation_Base $relation |
|
486 | - * @param WP_REST_Request $request |
|
487 | - * @return array|WP_Error |
|
488 | - * @throws EE_Error |
|
489 | - */ |
|
490 | - public function getEntitiesFromRelation($id, $relation, $request) |
|
491 | - { |
|
492 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | - throw new EE_Error( |
|
494 | - sprintf( |
|
495 | - __( |
|
496 | - // @codingStandardsIgnoreStart |
|
497 | - 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
498 | - // @codingStandardsIgnoreEnd |
|
499 | - 'event_espresso' |
|
500 | - ), |
|
501 | - $relation->get_this_model()->get_this_model_name() |
|
502 | - ) |
|
503 | - ); |
|
504 | - } |
|
505 | - return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
506 | - array( |
|
507 | - array( |
|
508 | - $relation->get_this_model()->primary_key_name() => $id, |
|
509 | - ), |
|
510 | - ), |
|
511 | - $relation, |
|
512 | - $request |
|
513 | - ); |
|
514 | - } |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * Sets the headers that are based on the model and query params, |
|
519 | - * like the total records. This should only be called on the original request |
|
520 | - * from the client, not on subsequent internal |
|
521 | - * |
|
522 | - * @param EEM_Base $model |
|
523 | - * @param array $query_params |
|
524 | - * @return void |
|
525 | - */ |
|
526 | - protected function setHeadersFromQueryParams($model, $query_params) |
|
527 | - { |
|
528 | - $this->setDebugInfo('model query params', $query_params); |
|
529 | - $this->setDebugInfo( |
|
530 | - 'missing caps', |
|
531 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
532 | - ); |
|
533 | - // normally the limit to a 2-part array, where the 2nd item is the limit |
|
534 | - if (! isset($query_params['limit'])) { |
|
535 | - $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
536 | - } |
|
537 | - if (is_array($query_params['limit'])) { |
|
538 | - $limit_parts = $query_params['limit']; |
|
539 | - } else { |
|
540 | - $limit_parts = explode(',', $query_params['limit']); |
|
541 | - if (count($limit_parts) == 1) { |
|
542 | - $limit_parts = array(0, $limit_parts[0]); |
|
543 | - } |
|
544 | - } |
|
545 | - // remove the group by and having parts of the query, as those will |
|
546 | - // make the sql query return an array of values, instead of just a single value |
|
547 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
548 | - $count = $model->count($query_params, null, true); |
|
549 | - $pages = $count / $limit_parts[1]; |
|
550 | - $this->setResponseHeader('Total', $count, false); |
|
551 | - $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
552 | - $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
553 | - } |
|
554 | - |
|
555 | - |
|
556 | - /** |
|
557 | - * Changes database results into REST API entities |
|
558 | - * |
|
559 | - * @param EEM_Base $model |
|
560 | - * @param array $db_row like results from $wpdb->get_results() |
|
561 | - * @param WP_REST_Request $rest_request |
|
562 | - * @param string $deprecated no longer used |
|
563 | - * @return array ready for being converted into json for sending to client |
|
564 | - */ |
|
565 | - public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
566 | - { |
|
567 | - if (! $rest_request instanceof WP_REST_Request) { |
|
568 | - // ok so this was called in the old style, where the 3rd arg was |
|
569 | - // $include, and the 4th arg was $context |
|
570 | - // now setup the request just to avoid fatal errors, although we won't be able |
|
571 | - // to truly make use of it because it's kinda devoid of info |
|
572 | - $rest_request = new WP_REST_Request(); |
|
573 | - $rest_request->set_param('include', $rest_request); |
|
574 | - $rest_request->set_param('caps', $deprecated); |
|
575 | - } |
|
576 | - if ($rest_request->get_param('caps') == null) { |
|
577 | - $rest_request->set_param('caps', EEM_Base::caps_read); |
|
578 | - } |
|
579 | - $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
580 | - $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
581 | - $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
582 | - $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
583 | - $entity_array = apply_filters( |
|
584 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
585 | - $entity_array, |
|
586 | - $model, |
|
587 | - $rest_request->get_param('caps'), |
|
588 | - $rest_request, |
|
589 | - $this |
|
590 | - ); |
|
591 | - $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
592 | - $entity_array = apply_filters( |
|
593 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
594 | - $entity_array, |
|
595 | - $model, |
|
596 | - $rest_request->get_param('caps'), |
|
597 | - $rest_request, |
|
598 | - $this |
|
599 | - ); |
|
600 | - $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
601 | - $entity_array, |
|
602 | - $model, |
|
603 | - $rest_request->get_param('caps'), |
|
604 | - $this->getModelVersionInfo(), |
|
605 | - $model->get_index_primary_key_string( |
|
606 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
607 | - ) |
|
608 | - ); |
|
609 | - $this->setDebugInfo( |
|
610 | - 'inaccessible fields', |
|
611 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
612 | - ); |
|
613 | - return apply_filters( |
|
614 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
615 | - $result_without_inaccessible_fields, |
|
616 | - $model, |
|
617 | - $rest_request->get_param('caps') |
|
618 | - ); |
|
619 | - } |
|
620 | - |
|
621 | - |
|
622 | - /** |
|
623 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
624 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
625 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
626 | - * |
|
627 | - * @param EEM_Base $model |
|
628 | - * @param array $db_row |
|
629 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
630 | - */ |
|
631 | - protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
632 | - { |
|
633 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
634 | - $result = array_intersect_key( |
|
635 | - $result, |
|
636 | - $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
637 | - ); |
|
638 | - // if this is a CPT, we need to set the global $post to it, |
|
639 | - // otherwise shortcodes etc won't work properly while rendering it |
|
640 | - if ($model instanceof \EEM_CPT_Base) { |
|
641 | - $do_chevy_shuffle = true; |
|
642 | - } else { |
|
643 | - $do_chevy_shuffle = false; |
|
644 | - } |
|
645 | - if ($do_chevy_shuffle) { |
|
646 | - global $post; |
|
647 | - $old_post = $post; |
|
648 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | - if (! $post instanceof \WP_Post) { |
|
650 | - // well that's weird, because $result is what we JUST fetched from the database |
|
651 | - throw new RestException( |
|
652 | - 'error_fetching_post_from_database_results', |
|
653 | - esc_html__( |
|
654 | - 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
655 | - 'event_espresso' |
|
656 | - ) |
|
657 | - ); |
|
658 | - } |
|
659 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
660 | - $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
661 | - $model_object_classname, |
|
662 | - $result, |
|
663 | - false, |
|
664 | - false |
|
665 | - ); |
|
666 | - } |
|
667 | - foreach ($result as $field_name => $field_value) { |
|
668 | - $field_obj = $model->field_settings_for($field_name); |
|
669 | - if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
670 | - unset($result[ $field_name ]); |
|
671 | - } elseif ($this->isSubclassOfOne( |
|
672 | - $field_obj, |
|
673 | - $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
674 | - ) |
|
675 | - ) { |
|
676 | - $result[ $field_name ] = array( |
|
677 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
678 | - 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
679 | - ); |
|
680 | - } elseif ($this->isSubclassOfOne( |
|
681 | - $field_obj, |
|
682 | - $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
683 | - ) |
|
684 | - ) { |
|
685 | - $result[ $field_name ] = array( |
|
686 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
687 | - 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
688 | - ); |
|
689 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
690 | - $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
691 | - $timezone = $field_value->getTimezone(); |
|
692 | - EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
693 | - $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | - $field_obj, |
|
695 | - $field_value, |
|
696 | - $this->getModelVersionInfo()->requestedVersion() |
|
697 | - ); |
|
698 | - EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
699 | - $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | - $field_obj, |
|
701 | - $field_value, |
|
702 | - $this->getModelVersionInfo()->requestedVersion() |
|
703 | - ); |
|
704 | - } else { |
|
705 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | - } |
|
707 | - } |
|
708 | - if ($do_chevy_shuffle) { |
|
709 | - $post = $old_post; |
|
710 | - } |
|
711 | - return $result; |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
717 | - * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
718 | - * representation using $field_obj->prepare_for_set_from_db()) |
|
719 | - * |
|
720 | - * @param EE_Model_Field_Base $field_obj |
|
721 | - * @param mixed $value as it's stored on a model object |
|
722 | - * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
723 | - * @return mixed |
|
724 | - * @throws ObjectDetectedException if $value contains a PHP object |
|
725 | - */ |
|
726 | - protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
727 | - { |
|
728 | - $value = $field_obj->prepare_for_set_from_db($value); |
|
729 | - switch ($format) { |
|
730 | - case 'pretty': |
|
731 | - $value = $field_obj->prepare_for_pretty_echoing($value); |
|
732 | - break; |
|
733 | - case 'normal': |
|
734 | - default: |
|
735 | - $value = $field_obj->prepare_for_get($value); |
|
736 | - break; |
|
737 | - } |
|
738 | - return ModelDataTranslator::prepareFieldValuesForJson( |
|
739 | - $field_obj, |
|
740 | - $value, |
|
741 | - $this->getModelVersionInfo()->requestedVersion() |
|
742 | - ); |
|
743 | - } |
|
744 | - |
|
745 | - |
|
746 | - /** |
|
747 | - * Adds a few extra fields to the entity response |
|
748 | - * |
|
749 | - * @param EEM_Base $model |
|
750 | - * @param array $db_row |
|
751 | - * @param array $entity_array |
|
752 | - * @return array modified entity |
|
753 | - */ |
|
754 | - protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
755 | - { |
|
756 | - if ($model instanceof EEM_CPT_Base) { |
|
757 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
758 | - } |
|
759 | - return $entity_array; |
|
760 | - } |
|
761 | - |
|
762 | - |
|
763 | - /** |
|
764 | - * Gets links we want to add to the response |
|
765 | - * |
|
766 | - * @global \WP_REST_Server $wp_rest_server |
|
767 | - * @param EEM_Base $model |
|
768 | - * @param array $db_row |
|
769 | - * @param array $entity_array |
|
770 | - * @return array the _links item in the entity |
|
771 | - */ |
|
772 | - protected function getEntityLinks($model, $db_row, $entity_array) |
|
773 | - { |
|
774 | - // add basic links |
|
775 | - $links = array(); |
|
776 | - if ($model->has_primary_key_field()) { |
|
777 | - $links['self'] = array( |
|
778 | - array( |
|
779 | - 'href' => $this->getVersionedLinkTo( |
|
780 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
781 | - . '/' |
|
782 | - . $entity_array[ $model->primary_key_name() ] |
|
783 | - ), |
|
784 | - ), |
|
785 | - ); |
|
786 | - } |
|
787 | - $links['collection'] = array( |
|
788 | - array( |
|
789 | - 'href' => $this->getVersionedLinkTo( |
|
790 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
791 | - ), |
|
792 | - ), |
|
793 | - ); |
|
794 | - // add links to related models |
|
795 | - if ($model->has_primary_key_field()) { |
|
796 | - foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
797 | - $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
798 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
799 | - array( |
|
800 | - 'href' => $this->getVersionedLinkTo( |
|
801 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
802 | - . '/' |
|
803 | - . $entity_array[ $model->primary_key_name() ] |
|
804 | - . '/' |
|
805 | - . $related_model_part |
|
806 | - ), |
|
807 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
808 | - ), |
|
809 | - ); |
|
810 | - } |
|
811 | - } |
|
812 | - return $links; |
|
813 | - } |
|
814 | - |
|
815 | - |
|
816 | - /** |
|
817 | - * Adds the included models indicated in the request to the entity provided |
|
818 | - * |
|
819 | - * @param EEM_Base $model |
|
820 | - * @param WP_REST_Request $rest_request |
|
821 | - * @param array $entity_array |
|
822 | - * @param array $db_row |
|
823 | - * @return array the modified entity |
|
824 | - */ |
|
825 | - protected function includeRequestedModels( |
|
826 | - EEM_Base $model, |
|
827 | - WP_REST_Request $rest_request, |
|
828 | - $entity_array, |
|
829 | - $db_row = array() |
|
830 | - ) { |
|
831 | - // if $db_row not included, hope the entity array has what we need |
|
832 | - if (! $db_row) { |
|
833 | - $db_row = $entity_array; |
|
834 | - } |
|
835 | - $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
836 | - $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
837 | - // if they passed in * or didn't specify any includes, return everything |
|
838 | - if (! in_array('*', $includes_for_this_model) |
|
839 | - && ! empty($includes_for_this_model) |
|
840 | - ) { |
|
841 | - if ($model->has_primary_key_field()) { |
|
842 | - // always include the primary key. ya just gotta know that at least |
|
843 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
844 | - } |
|
845 | - if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
846 | - $includes_for_this_model[] = '_calculated_fields'; |
|
847 | - } |
|
848 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
849 | - } |
|
850 | - $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
851 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
852 | - $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
853 | - $rest_request->get_param('include'), |
|
854 | - $relation_name |
|
855 | - ); |
|
856 | - $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
857 | - $rest_request->get_param('calculate'), |
|
858 | - $relation_name |
|
859 | - ); |
|
860 | - // did they specify they wanted to include a related model, or |
|
861 | - // specific fields from a related model? |
|
862 | - // or did they specify to calculate a field from a related model? |
|
863 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
864 | - // if so, we should include at least some part of the related model |
|
865 | - $pretend_related_request = new WP_REST_Request(); |
|
866 | - $pretend_related_request->set_query_params( |
|
867 | - array( |
|
868 | - 'caps' => $rest_request->get_param('caps'), |
|
869 | - 'include' => $related_fields_to_include, |
|
870 | - 'calculate' => $related_fields_to_calculate, |
|
871 | - ) |
|
872 | - ); |
|
873 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
874 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
875 | - $model->get_index_primary_key_string( |
|
876 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
877 | - ) |
|
878 | - ); |
|
879 | - $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
880 | - $primary_model_query_params, |
|
881 | - $relation_obj, |
|
882 | - $pretend_related_request |
|
883 | - ); |
|
884 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
885 | - instanceof |
|
886 | - WP_Error |
|
887 | - ? null |
|
888 | - : $related_results; |
|
889 | - } |
|
890 | - } |
|
891 | - return $entity_array; |
|
892 | - } |
|
893 | - |
|
894 | - |
|
895 | - /** |
|
896 | - * Returns a new array with all the names of models removed. Eg |
|
897 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
898 | - * |
|
899 | - * @param array $arr |
|
900 | - * @return array |
|
901 | - */ |
|
902 | - private function removeModelNamesFromArray($arr) |
|
903 | - { |
|
904 | - return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
905 | - } |
|
906 | - |
|
907 | - |
|
908 | - /** |
|
909 | - * Gets the calculated fields for the response |
|
910 | - * |
|
911 | - * @param EEM_Base $model |
|
912 | - * @param array $wpdb_row |
|
913 | - * @param WP_REST_Request $rest_request |
|
914 | - * @return \stdClass the _calculations item in the entity |
|
915 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
916 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
917 | - */ |
|
918 | - protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
919 | - { |
|
920 | - $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
921 | - $rest_request->get_param('calculate'), |
|
922 | - '' |
|
923 | - ); |
|
924 | - // note: setting calculate=* doesn't do anything |
|
925 | - $calculated_fields_to_return = new \stdClass(); |
|
926 | - foreach ($calculated_fields as $field_to_calculate) { |
|
927 | - try { |
|
928 | - $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
929 | - null, |
|
930 | - $this->fields_calculator->retrieveCalculatedFieldValue( |
|
931 | - $model, |
|
932 | - $field_to_calculate, |
|
933 | - $wpdb_row, |
|
934 | - $rest_request, |
|
935 | - $this |
|
936 | - ), |
|
937 | - $this->getModelVersionInfo()->requestedVersion() |
|
938 | - ); |
|
939 | - } catch (RestException $e) { |
|
940 | - // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
941 | - $this->setResponseHeader( |
|
942 | - 'Notices-Field-Calculation-Errors[' |
|
943 | - . $e->getStringCode() |
|
944 | - . '][' |
|
945 | - . $model->get_this_model_name() |
|
946 | - . '][' |
|
947 | - . $field_to_calculate |
|
948 | - . ']', |
|
949 | - $e->getMessage(), |
|
950 | - true |
|
951 | - ); |
|
952 | - } |
|
953 | - } |
|
954 | - return $calculated_fields_to_return; |
|
955 | - } |
|
956 | - |
|
957 | - |
|
958 | - /** |
|
959 | - * Gets the full URL to the resource, taking the requested version into account |
|
960 | - * |
|
961 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
962 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
963 | - */ |
|
964 | - public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
965 | - { |
|
966 | - return rest_url( |
|
967 | - EED_Core_Rest_Api::get_versioned_route_to( |
|
968 | - $link_part_after_version_and_slash, |
|
969 | - $this->getModelVersionInfo()->requestedVersion() |
|
970 | - ) |
|
971 | - ); |
|
972 | - } |
|
973 | - |
|
974 | - |
|
975 | - /** |
|
976 | - * Gets the correct lowercase name for the relation in the API according |
|
977 | - * to the relation's type |
|
978 | - * |
|
979 | - * @param string $relation_name |
|
980 | - * @param \EE_Model_Relation_Base $relation_obj |
|
981 | - * @return string |
|
982 | - */ |
|
983 | - public static function getRelatedEntityName($relation_name, $relation_obj) |
|
984 | - { |
|
985 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
986 | - return strtolower($relation_name); |
|
987 | - } else { |
|
988 | - return EEH_Inflector::pluralize_and_lower($relation_name); |
|
989 | - } |
|
990 | - } |
|
991 | - |
|
992 | - |
|
993 | - /** |
|
994 | - * Gets the one model object with the specified id for the specified model |
|
995 | - * |
|
996 | - * @param EEM_Base $model |
|
997 | - * @param WP_REST_Request $request |
|
998 | - * @return array|WP_Error |
|
999 | - */ |
|
1000 | - public function getEntityFromModel($model, $request) |
|
1001 | - { |
|
1002 | - $context = $this->validateContext($request->get_param('caps')); |
|
1003 | - return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - /** |
|
1008 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
1009 | - * version so just treat it as a default read |
|
1010 | - * |
|
1011 | - * @param string $context |
|
1012 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1013 | - */ |
|
1014 | - public function validateContext($context) |
|
1015 | - { |
|
1016 | - if (! $context) { |
|
1017 | - $context = EEM_Base::caps_read; |
|
1018 | - } |
|
1019 | - $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1020 | - if (in_array($context, $valid_contexts)) { |
|
1021 | - return $context; |
|
1022 | - } else { |
|
1023 | - return EEM_Base::caps_read; |
|
1024 | - } |
|
1025 | - } |
|
1026 | - |
|
1027 | - |
|
1028 | - /** |
|
1029 | - * Verifies the passed in value is an allowable default where conditions value. |
|
1030 | - * |
|
1031 | - * @param $default_query_params |
|
1032 | - * @return string |
|
1033 | - */ |
|
1034 | - public function validateDefaultQueryParams($default_query_params) |
|
1035 | - { |
|
1036 | - $valid_default_where_conditions_for_api_calls = array( |
|
1037 | - EEM_Base::default_where_conditions_all, |
|
1038 | - EEM_Base::default_where_conditions_minimum_all, |
|
1039 | - EEM_Base::default_where_conditions_minimum_others, |
|
1040 | - ); |
|
1041 | - if (! $default_query_params) { |
|
1042 | - $default_query_params = EEM_Base::default_where_conditions_all; |
|
1043 | - } |
|
1044 | - if (in_array( |
|
1045 | - $default_query_params, |
|
1046 | - $valid_default_where_conditions_for_api_calls, |
|
1047 | - true |
|
1048 | - )) { |
|
1049 | - return $default_query_params; |
|
1050 | - } else { |
|
1051 | - return EEM_Base::default_where_conditions_all; |
|
1052 | - } |
|
1053 | - } |
|
1054 | - |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1058 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1059 | - * can be left as-is, but it's quite possible this will change someday. |
|
1060 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1061 | - * |
|
1062 | - * @param EEM_Base $model |
|
1063 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1064 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1065 | - * that absolutely no results should be returned |
|
1066 | - * @throws EE_Error |
|
1067 | - * @throws RestException |
|
1068 | - */ |
|
1069 | - public function createModelQueryParams($model, $query_parameters) |
|
1070 | - { |
|
1071 | - $model_query_params = array(); |
|
1072 | - if (isset($query_parameters['where'])) { |
|
1073 | - $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1074 | - $query_parameters['where'], |
|
1075 | - $model, |
|
1076 | - $this->getModelVersionInfo()->requestedVersion() |
|
1077 | - ); |
|
1078 | - } |
|
1079 | - if (isset($query_parameters['order_by'])) { |
|
1080 | - $order_by = $query_parameters['order_by']; |
|
1081 | - } elseif (isset($query_parameters['orderby'])) { |
|
1082 | - $order_by = $query_parameters['orderby']; |
|
1083 | - } else { |
|
1084 | - $order_by = null; |
|
1085 | - } |
|
1086 | - if ($order_by !== null) { |
|
1087 | - if (is_array($order_by)) { |
|
1088 | - $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1089 | - } else { |
|
1090 | - // it's a single item |
|
1091 | - $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1092 | - } |
|
1093 | - $model_query_params['order_by'] = $order_by; |
|
1094 | - } |
|
1095 | - if (isset($query_parameters['group_by'])) { |
|
1096 | - $group_by = $query_parameters['group_by']; |
|
1097 | - } elseif (isset($query_parameters['groupby'])) { |
|
1098 | - $group_by = $query_parameters['groupby']; |
|
1099 | - } else { |
|
1100 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1101 | - } |
|
1102 | - // make sure they're all real names |
|
1103 | - if (is_array($group_by)) { |
|
1104 | - $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1105 | - } |
|
1106 | - if ($group_by !== null) { |
|
1107 | - $model_query_params['group_by'] = $group_by; |
|
1108 | - } |
|
1109 | - if (isset($query_parameters['having'])) { |
|
1110 | - $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1111 | - $query_parameters['having'], |
|
1112 | - $model, |
|
1113 | - $this->getModelVersionInfo()->requestedVersion() |
|
1114 | - ); |
|
1115 | - } |
|
1116 | - if (isset($query_parameters['order'])) { |
|
1117 | - $model_query_params['order'] = $query_parameters['order']; |
|
1118 | - } |
|
1119 | - if (isset($query_parameters['mine'])) { |
|
1120 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1121 | - } |
|
1122 | - if (isset($query_parameters['limit'])) { |
|
1123 | - // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1124 | - if (! is_array($query_parameters['limit'])) { |
|
1125 | - $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1126 | - } else { |
|
1127 | - $limit_array = $query_parameters['limit']; |
|
1128 | - } |
|
1129 | - $sanitized_limit = array(); |
|
1130 | - foreach ($limit_array as $key => $limit_part) { |
|
1131 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | - throw new EE_Error( |
|
1133 | - sprintf( |
|
1134 | - __( |
|
1135 | - // @codingStandardsIgnoreStart |
|
1136 | - '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.', |
|
1137 | - // @codingStandardsIgnoreEnd |
|
1138 | - 'event_espresso' |
|
1139 | - ), |
|
1140 | - wp_json_encode($query_parameters['limit']) |
|
1141 | - ) |
|
1142 | - ); |
|
1143 | - } |
|
1144 | - $sanitized_limit[] = (int) $limit_part; |
|
1145 | - } |
|
1146 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1147 | - } else { |
|
1148 | - $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1149 | - } |
|
1150 | - if (isset($query_parameters['caps'])) { |
|
1151 | - $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1152 | - } else { |
|
1153 | - $model_query_params['caps'] = EEM_Base::caps_read; |
|
1154 | - } |
|
1155 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1156 | - $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1157 | - $query_parameters['default_where_conditions'] |
|
1158 | - ); |
|
1159 | - } |
|
1160 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1161 | - } |
|
1162 | - |
|
1163 | - |
|
1164 | - /** |
|
1165 | - * Changes the REST-style query params for use in the models |
|
1166 | - * |
|
1167 | - * @deprecated |
|
1168 | - * @param EEM_Base $model |
|
1169 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1170 | - * @return array |
|
1171 | - */ |
|
1172 | - public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1173 | - { |
|
1174 | - $model_ready_query_params = array(); |
|
1175 | - foreach ($query_params as $key => $value) { |
|
1176 | - if (is_array($value)) { |
|
1177 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | - } else { |
|
1179 | - $model_ready_query_params[ $key ] = $value; |
|
1180 | - } |
|
1181 | - } |
|
1182 | - return $model_ready_query_params; |
|
1183 | - } |
|
1184 | - |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1188 | - * @param $model |
|
1189 | - * @param $query_params |
|
1190 | - * @return array |
|
1191 | - */ |
|
1192 | - public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1193 | - { |
|
1194 | - $model_ready_query_params = array(); |
|
1195 | - foreach ($query_params as $key => $value) { |
|
1196 | - if (is_array($value)) { |
|
1197 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | - } else { |
|
1199 | - $model_ready_query_params[ $key ] = $value; |
|
1200 | - } |
|
1201 | - } |
|
1202 | - return $model_ready_query_params; |
|
1203 | - } |
|
1204 | - |
|
1205 | - |
|
1206 | - /** |
|
1207 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1208 | - * If no prefix is specified, returns items with no period. |
|
1209 | - * |
|
1210 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1211 | - * @param string $prefix "Event" or "foobar" |
|
1212 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1213 | - * we only return strings starting with that and a period; if no prefix was |
|
1214 | - * specified we return all items containing NO periods |
|
1215 | - */ |
|
1216 | - public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1217 | - { |
|
1218 | - if (is_string($string_to_explode)) { |
|
1219 | - $exploded_contents = explode(',', $string_to_explode); |
|
1220 | - } elseif (is_array($string_to_explode)) { |
|
1221 | - $exploded_contents = $string_to_explode; |
|
1222 | - } else { |
|
1223 | - $exploded_contents = array(); |
|
1224 | - } |
|
1225 | - // if the string was empty, we want an empty array |
|
1226 | - $exploded_contents = array_filter($exploded_contents); |
|
1227 | - $contents_with_prefix = array(); |
|
1228 | - foreach ($exploded_contents as $item) { |
|
1229 | - $item = trim($item); |
|
1230 | - // if no prefix was provided, so we look for items with no "." in them |
|
1231 | - if (! $prefix) { |
|
1232 | - // does this item have a period? |
|
1233 | - if (strpos($item, '.') === false) { |
|
1234 | - // if not, then its what we're looking for |
|
1235 | - $contents_with_prefix[] = $item; |
|
1236 | - } |
|
1237 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1238 | - // this item has the prefix and a period, grab it |
|
1239 | - $contents_with_prefix[] = substr( |
|
1240 | - $item, |
|
1241 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1242 | - ); |
|
1243 | - } elseif ($item === $prefix) { |
|
1244 | - // this item is JUST the prefix |
|
1245 | - // so let's grab everything after, which is a blank string |
|
1246 | - $contents_with_prefix[] = ''; |
|
1247 | - } |
|
1248 | - } |
|
1249 | - return $contents_with_prefix; |
|
1250 | - } |
|
1251 | - |
|
1252 | - |
|
1253 | - /** |
|
1254 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1255 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1256 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1257 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1258 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1259 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1260 | - * name and then a period). |
|
1261 | - * @param string $include_string @see Read:handle_request_get_all |
|
1262 | - * @param string $model_name |
|
1263 | - * @return array of fields for this model. If $model_name is provided, then |
|
1264 | - * the fields for that model, with the model's name removed from each. |
|
1265 | - * If $include_string was blank or '*' returns an empty array |
|
1266 | - */ |
|
1267 | - public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1268 | - { |
|
1269 | - if (is_array($include_string)) { |
|
1270 | - $include_string = implode(',', $include_string); |
|
1271 | - } |
|
1272 | - if ($include_string === '*' || $include_string === '') { |
|
1273 | - return array(); |
|
1274 | - } |
|
1275 | - $includes = explode(',', $include_string); |
|
1276 | - $extracted_fields_to_include = array(); |
|
1277 | - if ($model_name) { |
|
1278 | - foreach ($includes as $field_to_include) { |
|
1279 | - $field_to_include = trim($field_to_include); |
|
1280 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1281 | - // found the model name at the exact start |
|
1282 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1283 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1284 | - } elseif ($field_to_include == $model_name) { |
|
1285 | - $extracted_fields_to_include[] = '*'; |
|
1286 | - } |
|
1287 | - } |
|
1288 | - } else { |
|
1289 | - // look for ones with no period |
|
1290 | - foreach ($includes as $field_to_include) { |
|
1291 | - $field_to_include = trim($field_to_include); |
|
1292 | - if (strpos($field_to_include, '.') === false |
|
1293 | - && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1294 | - ) { |
|
1295 | - $extracted_fields_to_include[] = $field_to_include; |
|
1296 | - } |
|
1297 | - } |
|
1298 | - } |
|
1299 | - return $extracted_fields_to_include; |
|
1300 | - } |
|
1301 | - |
|
1302 | - |
|
1303 | - /** |
|
1304 | - * Gets the single item using the model according to the request in the context given, otherwise |
|
1305 | - * returns that it's inaccessible to the current user |
|
1306 | - * |
|
1307 | - * @param EEM_Base $model |
|
1308 | - * @param WP_REST_Request $request |
|
1309 | - * @param null $context |
|
1310 | - * @return array|WP_Error |
|
1311 | - */ |
|
1312 | - public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1313 | - { |
|
1314 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1315 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1316 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1317 | - } |
|
1318 | - $restricted_query_params = $query_params; |
|
1319 | - $restricted_query_params['caps'] = $context; |
|
1320 | - $this->setDebugInfo('model query params', $restricted_query_params); |
|
1321 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1322 | - if (! empty($model_rows)) { |
|
1323 | - return $this->createEntityFromWpdbResult( |
|
1324 | - $model, |
|
1325 | - array_shift($model_rows), |
|
1326 | - $request |
|
1327 | - ); |
|
1328 | - } else { |
|
1329 | - // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1330 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1331 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1332 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1333 | - // you got shafted- it existed but we didn't want to tell you! |
|
1334 | - return new WP_Error( |
|
1335 | - 'rest_user_cannot_' . $context, |
|
1336 | - sprintf( |
|
1337 | - __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1338 | - $context, |
|
1339 | - strtolower($model->get_this_model_name()), |
|
1340 | - Capabilities::getMissingPermissionsString( |
|
1341 | - $model, |
|
1342 | - $context |
|
1343 | - ) |
|
1344 | - ), |
|
1345 | - array('status' => 403) |
|
1346 | - ); |
|
1347 | - } else { |
|
1348 | - // it's not you. It just doesn't exist |
|
1349 | - return new WP_Error( |
|
1350 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1351 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1352 | - array('status' => 404) |
|
1353 | - ); |
|
1354 | - } |
|
1355 | - } |
|
1356 | - } |
|
38 | + /** |
|
39 | + * @var CalculatedModelFields |
|
40 | + */ |
|
41 | + protected $fields_calculator; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Read constructor. |
|
46 | + */ |
|
47 | + public function __construct() |
|
48 | + { |
|
49 | + parent::__construct(); |
|
50 | + $this->fields_calculator = new CalculatedModelFields(); |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
56 | + * |
|
57 | + * @param WP_REST_Request $request |
|
58 | + * @param string $version |
|
59 | + * @param string $model_name |
|
60 | + * @return \WP_REST_Response|WP_Error |
|
61 | + */ |
|
62 | + public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
63 | + { |
|
64 | + $controller = new Read(); |
|
65 | + try { |
|
66 | + $controller->setRequestedVersion($version); |
|
67 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | + return $controller->sendResponse( |
|
69 | + new WP_Error( |
|
70 | + 'endpoint_parsing_error', |
|
71 | + sprintf( |
|
72 | + __( |
|
73 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
74 | + 'event_espresso' |
|
75 | + ), |
|
76 | + $model_name |
|
77 | + ) |
|
78 | + ) |
|
79 | + ); |
|
80 | + } |
|
81 | + return $controller->sendResponse( |
|
82 | + $controller->getEntitiesFromModel( |
|
83 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
84 | + $request |
|
85 | + ) |
|
86 | + ); |
|
87 | + } catch (Exception $e) { |
|
88 | + return $controller->sendResponse($e); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Prepares and returns schema for any OPTIONS request. |
|
95 | + * |
|
96 | + * @param string $version The API endpoint version being used. |
|
97 | + * @param string $model_name Something like `Event` or `Registration` |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public static function handleSchemaRequest($version, $model_name) |
|
101 | + { |
|
102 | + $controller = new Read(); |
|
103 | + try { |
|
104 | + $controller->setRequestedVersion($version); |
|
105 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | + return array(); |
|
107 | + } |
|
108 | + // get the model for this version |
|
109 | + $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
110 | + $model_schema = new JsonModelSchema($model); |
|
111 | + return $model_schema->getModelSchemaForRelations( |
|
112 | + $controller->getModelVersionInfo()->relationSettings($model), |
|
113 | + $controller->customizeSchemaForRestResponse( |
|
114 | + $model, |
|
115 | + $model_schema->getModelSchemaForFields( |
|
116 | + $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
117 | + $model_schema->getInitialSchemaStructure() |
|
118 | + ) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } catch (Exception $e) { |
|
122 | + return array(); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * This loops through each field in the given schema for the model and does the following: |
|
129 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
130 | + * - transform default values into the correct format for a REST API response. |
|
131 | + * |
|
132 | + * @param EEM_Base $model |
|
133 | + * @param array $schema |
|
134 | + * @return array The final schema. |
|
135 | + */ |
|
136 | + protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
137 | + { |
|
138 | + foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
139 | + $schema = $this->translateDefaultsForRestResponse( |
|
140 | + $field_name, |
|
141 | + $field, |
|
142 | + $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
143 | + ); |
|
144 | + } |
|
145 | + return $schema; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
151 | + * response. |
|
152 | + * |
|
153 | + * @param $field_name |
|
154 | + * @param EE_Model_Field_Base $field |
|
155 | + * @param array $schema |
|
156 | + * @return array |
|
157 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
158 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
159 | + */ |
|
160 | + protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
161 | + { |
|
162 | + if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | + if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | + foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
165 | + if ($default_key === 'raw') { |
|
166 | + $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
167 | + ModelDataTranslator::prepareFieldValueForJson( |
|
168 | + $field, |
|
169 | + $default_value, |
|
170 | + $this->getModelVersionInfo()->requestedVersion() |
|
171 | + ); |
|
172 | + } |
|
173 | + } |
|
174 | + } else { |
|
175 | + $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | + $field, |
|
177 | + $schema['properties'][ $field_name ]['default'], |
|
178 | + $this->getModelVersionInfo()->requestedVersion() |
|
179 | + ); |
|
180 | + } |
|
181 | + } |
|
182 | + return $schema; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Adds additional fields to the schema |
|
188 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
189 | + * needs to be added to the schema. |
|
190 | + * |
|
191 | + * @param $field_name |
|
192 | + * @param EE_Model_Field_Base $field |
|
193 | + * @param array $schema |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
197 | + { |
|
198 | + if ($field instanceof EE_Datetime_Field) { |
|
199 | + $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
200 | + // modify the description |
|
201 | + $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
202 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
203 | + wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
204 | + ); |
|
205 | + } |
|
206 | + return $schema; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
212 | + * |
|
213 | + * @return string |
|
214 | + */ |
|
215 | + protected function getRouteFromRequest() |
|
216 | + { |
|
217 | + if (isset($GLOBALS['wp']) |
|
218 | + && $GLOBALS['wp'] instanceof \WP |
|
219 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
220 | + ) { |
|
221 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
222 | + } else { |
|
223 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Gets a single entity related to the model indicated in the path and its id |
|
230 | + * |
|
231 | + * @param WP_REST_Request $request |
|
232 | + * @param string $version |
|
233 | + * @param string $model_name |
|
234 | + * @return \WP_REST_Response|WP_Error |
|
235 | + */ |
|
236 | + public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
237 | + { |
|
238 | + $controller = new Read(); |
|
239 | + try { |
|
240 | + $controller->setRequestedVersion($version); |
|
241 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | + return $controller->sendResponse( |
|
243 | + new WP_Error( |
|
244 | + 'endpoint_parsing_error', |
|
245 | + sprintf( |
|
246 | + __( |
|
247 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
248 | + 'event_espresso' |
|
249 | + ), |
|
250 | + $model_name |
|
251 | + ) |
|
252 | + ) |
|
253 | + ); |
|
254 | + } |
|
255 | + return $controller->sendResponse( |
|
256 | + $controller->getEntityFromModel( |
|
257 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
258 | + $request |
|
259 | + ) |
|
260 | + ); |
|
261 | + } catch (Exception $e) { |
|
262 | + return $controller->sendResponse($e); |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
269 | + * to the item with the given id |
|
270 | + * |
|
271 | + * @param WP_REST_Request $request |
|
272 | + * @param string $version |
|
273 | + * @param string $model_name |
|
274 | + * @param string $related_model_name |
|
275 | + * @return \WP_REST_Response|WP_Error |
|
276 | + */ |
|
277 | + public static function handleRequestGetRelated( |
|
278 | + WP_REST_Request $request, |
|
279 | + $version, |
|
280 | + $model_name, |
|
281 | + $related_model_name |
|
282 | + ) { |
|
283 | + $controller = new Read(); |
|
284 | + try { |
|
285 | + $controller->setRequestedVersion($version); |
|
286 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | + return $controller->sendResponse( |
|
288 | + new WP_Error( |
|
289 | + 'endpoint_parsing_error', |
|
290 | + sprintf( |
|
291 | + __( |
|
292 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
293 | + 'event_espresso' |
|
294 | + ), |
|
295 | + $model_name |
|
296 | + ) |
|
297 | + ) |
|
298 | + ); |
|
299 | + } |
|
300 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
301 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | + return $controller->sendResponse( |
|
303 | + new WP_Error( |
|
304 | + 'endpoint_parsing_error', |
|
305 | + sprintf( |
|
306 | + __( |
|
307 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
308 | + 'event_espresso' |
|
309 | + ), |
|
310 | + $related_model_name |
|
311 | + ) |
|
312 | + ) |
|
313 | + ); |
|
314 | + } |
|
315 | + return $controller->sendResponse( |
|
316 | + $controller->getEntitiesFromRelation( |
|
317 | + $request->get_param('id'), |
|
318 | + $main_model->related_settings_for($related_model_name), |
|
319 | + $request |
|
320 | + ) |
|
321 | + ); |
|
322 | + } catch (Exception $e) { |
|
323 | + return $controller->sendResponse($e); |
|
324 | + } |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + /** |
|
329 | + * Gets a collection for the given model and filters |
|
330 | + * |
|
331 | + * @param EEM_Base $model |
|
332 | + * @param WP_REST_Request $request |
|
333 | + * @return array|WP_Error |
|
334 | + */ |
|
335 | + public function getEntitiesFromModel($model, $request) |
|
336 | + { |
|
337 | + $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
338 | + if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | + $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
340 | + return new WP_Error( |
|
341 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
342 | + sprintf( |
|
343 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
344 | + $model_name_plural, |
|
345 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
346 | + ), |
|
347 | + array('status' => 403) |
|
348 | + ); |
|
349 | + } |
|
350 | + if (! $request->get_header('no_rest_headers')) { |
|
351 | + $this->setHeadersFromQueryParams($model, $query_params); |
|
352 | + } |
|
353 | + /** @type array $results */ |
|
354 | + $results = $model->get_all_wpdb_results($query_params); |
|
355 | + $nice_results = array(); |
|
356 | + foreach ($results as $result) { |
|
357 | + $nice_results[] = $this->createEntityFromWpdbResult( |
|
358 | + $model, |
|
359 | + $result, |
|
360 | + $request |
|
361 | + ); |
|
362 | + } |
|
363 | + return $nice_results; |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Gets the collection for given relation object |
|
369 | + * The same as Read::get_entities_from_model(), except if the relation |
|
370 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
371 | + * the join-model-object into the results |
|
372 | + * |
|
373 | + * @param array $primary_model_query_params query params for finding the item from which |
|
374 | + * relations will be based |
|
375 | + * @param \EE_Model_Relation_Base $relation |
|
376 | + * @param WP_REST_Request $request |
|
377 | + * @return WP_Error|array |
|
378 | + * @throws RestException |
|
379 | + */ |
|
380 | + protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
381 | + { |
|
382 | + $context = $this->validateContext($request->get_param('caps')); |
|
383 | + $model = $relation->get_this_model(); |
|
384 | + $related_model = $relation->get_other_model(); |
|
385 | + if (! isset($primary_model_query_params[0])) { |
|
386 | + $primary_model_query_params[0] = array(); |
|
387 | + } |
|
388 | + // check if they can access the 1st model object |
|
389 | + $primary_model_query_params = array( |
|
390 | + 0 => $primary_model_query_params[0], |
|
391 | + 'limit' => 1, |
|
392 | + ); |
|
393 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
394 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
395 | + $primary_model_query_params |
|
396 | + ); |
|
397 | + } |
|
398 | + $restricted_query_params = $primary_model_query_params; |
|
399 | + $restricted_query_params['caps'] = $context; |
|
400 | + $this->setDebugInfo('main model query params', $restricted_query_params); |
|
401 | + $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
402 | + if (! ( |
|
403 | + Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
404 | + && $model->exists($restricted_query_params) |
|
405 | + ) |
|
406 | + ) { |
|
407 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
408 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
409 | + } else { |
|
410 | + $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
411 | + $related_model->get_this_model_name() |
|
412 | + ); |
|
413 | + } |
|
414 | + return new WP_Error( |
|
415 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
416 | + sprintf( |
|
417 | + __( |
|
418 | + 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
419 | + 'event_espresso' |
|
420 | + ), |
|
421 | + $related_model_name_maybe_plural, |
|
422 | + $relation->get_this_model()->get_this_model_name(), |
|
423 | + implode( |
|
424 | + ',', |
|
425 | + array_keys( |
|
426 | + Capabilities::getMissingPermissions($related_model, $context) |
|
427 | + ) |
|
428 | + ) |
|
429 | + ), |
|
430 | + array('status' => 403) |
|
431 | + ); |
|
432 | + } |
|
433 | + $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
434 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
435 | + $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
436 | + . '.' |
|
437 | + . $where_condition_key ] = $where_condition_value; |
|
438 | + } |
|
439 | + $query_params['default_where_conditions'] = 'none'; |
|
440 | + $query_params['caps'] = $context; |
|
441 | + if (! $request->get_header('no_rest_headers')) { |
|
442 | + $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
443 | + } |
|
444 | + /** @type array $results */ |
|
445 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
446 | + $nice_results = array(); |
|
447 | + foreach ($results as $result) { |
|
448 | + $nice_result = $this->createEntityFromWpdbResult( |
|
449 | + $relation->get_other_model(), |
|
450 | + $result, |
|
451 | + $request |
|
452 | + ); |
|
453 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
454 | + // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
455 | + // if there are conflicts we prefer the properties from the main model |
|
456 | + $join_model_result = $this->createEntityFromWpdbResult( |
|
457 | + $relation->get_join_model(), |
|
458 | + $result, |
|
459 | + $request |
|
460 | + ); |
|
461 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
462 | + // but keep the meta stuff from the main model |
|
463 | + if (isset($nice_result['meta'])) { |
|
464 | + $joined_result['meta'] = $nice_result['meta']; |
|
465 | + } |
|
466 | + $nice_result = $joined_result; |
|
467 | + } |
|
468 | + $nice_results[] = $nice_result; |
|
469 | + } |
|
470 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
471 | + return array_shift($nice_results); |
|
472 | + } else { |
|
473 | + return $nice_results; |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * Gets the collection for given relation object |
|
480 | + * The same as Read::get_entities_from_model(), except if the relation |
|
481 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
482 | + * the join-model-object into the results |
|
483 | + * |
|
484 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
485 | + * @param \EE_Model_Relation_Base $relation |
|
486 | + * @param WP_REST_Request $request |
|
487 | + * @return array|WP_Error |
|
488 | + * @throws EE_Error |
|
489 | + */ |
|
490 | + public function getEntitiesFromRelation($id, $relation, $request) |
|
491 | + { |
|
492 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | + throw new EE_Error( |
|
494 | + sprintf( |
|
495 | + __( |
|
496 | + // @codingStandardsIgnoreStart |
|
497 | + 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
498 | + // @codingStandardsIgnoreEnd |
|
499 | + 'event_espresso' |
|
500 | + ), |
|
501 | + $relation->get_this_model()->get_this_model_name() |
|
502 | + ) |
|
503 | + ); |
|
504 | + } |
|
505 | + return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
506 | + array( |
|
507 | + array( |
|
508 | + $relation->get_this_model()->primary_key_name() => $id, |
|
509 | + ), |
|
510 | + ), |
|
511 | + $relation, |
|
512 | + $request |
|
513 | + ); |
|
514 | + } |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * Sets the headers that are based on the model and query params, |
|
519 | + * like the total records. This should only be called on the original request |
|
520 | + * from the client, not on subsequent internal |
|
521 | + * |
|
522 | + * @param EEM_Base $model |
|
523 | + * @param array $query_params |
|
524 | + * @return void |
|
525 | + */ |
|
526 | + protected function setHeadersFromQueryParams($model, $query_params) |
|
527 | + { |
|
528 | + $this->setDebugInfo('model query params', $query_params); |
|
529 | + $this->setDebugInfo( |
|
530 | + 'missing caps', |
|
531 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
532 | + ); |
|
533 | + // normally the limit to a 2-part array, where the 2nd item is the limit |
|
534 | + if (! isset($query_params['limit'])) { |
|
535 | + $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
536 | + } |
|
537 | + if (is_array($query_params['limit'])) { |
|
538 | + $limit_parts = $query_params['limit']; |
|
539 | + } else { |
|
540 | + $limit_parts = explode(',', $query_params['limit']); |
|
541 | + if (count($limit_parts) == 1) { |
|
542 | + $limit_parts = array(0, $limit_parts[0]); |
|
543 | + } |
|
544 | + } |
|
545 | + // remove the group by and having parts of the query, as those will |
|
546 | + // make the sql query return an array of values, instead of just a single value |
|
547 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
548 | + $count = $model->count($query_params, null, true); |
|
549 | + $pages = $count / $limit_parts[1]; |
|
550 | + $this->setResponseHeader('Total', $count, false); |
|
551 | + $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
552 | + $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
553 | + } |
|
554 | + |
|
555 | + |
|
556 | + /** |
|
557 | + * Changes database results into REST API entities |
|
558 | + * |
|
559 | + * @param EEM_Base $model |
|
560 | + * @param array $db_row like results from $wpdb->get_results() |
|
561 | + * @param WP_REST_Request $rest_request |
|
562 | + * @param string $deprecated no longer used |
|
563 | + * @return array ready for being converted into json for sending to client |
|
564 | + */ |
|
565 | + public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
566 | + { |
|
567 | + if (! $rest_request instanceof WP_REST_Request) { |
|
568 | + // ok so this was called in the old style, where the 3rd arg was |
|
569 | + // $include, and the 4th arg was $context |
|
570 | + // now setup the request just to avoid fatal errors, although we won't be able |
|
571 | + // to truly make use of it because it's kinda devoid of info |
|
572 | + $rest_request = new WP_REST_Request(); |
|
573 | + $rest_request->set_param('include', $rest_request); |
|
574 | + $rest_request->set_param('caps', $deprecated); |
|
575 | + } |
|
576 | + if ($rest_request->get_param('caps') == null) { |
|
577 | + $rest_request->set_param('caps', EEM_Base::caps_read); |
|
578 | + } |
|
579 | + $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
580 | + $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
581 | + $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
582 | + $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
583 | + $entity_array = apply_filters( |
|
584 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
585 | + $entity_array, |
|
586 | + $model, |
|
587 | + $rest_request->get_param('caps'), |
|
588 | + $rest_request, |
|
589 | + $this |
|
590 | + ); |
|
591 | + $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
592 | + $entity_array = apply_filters( |
|
593 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
594 | + $entity_array, |
|
595 | + $model, |
|
596 | + $rest_request->get_param('caps'), |
|
597 | + $rest_request, |
|
598 | + $this |
|
599 | + ); |
|
600 | + $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
601 | + $entity_array, |
|
602 | + $model, |
|
603 | + $rest_request->get_param('caps'), |
|
604 | + $this->getModelVersionInfo(), |
|
605 | + $model->get_index_primary_key_string( |
|
606 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
607 | + ) |
|
608 | + ); |
|
609 | + $this->setDebugInfo( |
|
610 | + 'inaccessible fields', |
|
611 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
612 | + ); |
|
613 | + return apply_filters( |
|
614 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
615 | + $result_without_inaccessible_fields, |
|
616 | + $model, |
|
617 | + $rest_request->get_param('caps') |
|
618 | + ); |
|
619 | + } |
|
620 | + |
|
621 | + |
|
622 | + /** |
|
623 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
624 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
625 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
626 | + * |
|
627 | + * @param EEM_Base $model |
|
628 | + * @param array $db_row |
|
629 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
630 | + */ |
|
631 | + protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
632 | + { |
|
633 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
634 | + $result = array_intersect_key( |
|
635 | + $result, |
|
636 | + $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
637 | + ); |
|
638 | + // if this is a CPT, we need to set the global $post to it, |
|
639 | + // otherwise shortcodes etc won't work properly while rendering it |
|
640 | + if ($model instanceof \EEM_CPT_Base) { |
|
641 | + $do_chevy_shuffle = true; |
|
642 | + } else { |
|
643 | + $do_chevy_shuffle = false; |
|
644 | + } |
|
645 | + if ($do_chevy_shuffle) { |
|
646 | + global $post; |
|
647 | + $old_post = $post; |
|
648 | + $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | + if (! $post instanceof \WP_Post) { |
|
650 | + // well that's weird, because $result is what we JUST fetched from the database |
|
651 | + throw new RestException( |
|
652 | + 'error_fetching_post_from_database_results', |
|
653 | + esc_html__( |
|
654 | + 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
655 | + 'event_espresso' |
|
656 | + ) |
|
657 | + ); |
|
658 | + } |
|
659 | + $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
660 | + $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
661 | + $model_object_classname, |
|
662 | + $result, |
|
663 | + false, |
|
664 | + false |
|
665 | + ); |
|
666 | + } |
|
667 | + foreach ($result as $field_name => $field_value) { |
|
668 | + $field_obj = $model->field_settings_for($field_name); |
|
669 | + if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
670 | + unset($result[ $field_name ]); |
|
671 | + } elseif ($this->isSubclassOfOne( |
|
672 | + $field_obj, |
|
673 | + $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
674 | + ) |
|
675 | + ) { |
|
676 | + $result[ $field_name ] = array( |
|
677 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
678 | + 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
679 | + ); |
|
680 | + } elseif ($this->isSubclassOfOne( |
|
681 | + $field_obj, |
|
682 | + $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
683 | + ) |
|
684 | + ) { |
|
685 | + $result[ $field_name ] = array( |
|
686 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
687 | + 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
688 | + ); |
|
689 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
690 | + $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
691 | + $timezone = $field_value->getTimezone(); |
|
692 | + EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
693 | + $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | + $field_obj, |
|
695 | + $field_value, |
|
696 | + $this->getModelVersionInfo()->requestedVersion() |
|
697 | + ); |
|
698 | + EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
699 | + $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | + $field_obj, |
|
701 | + $field_value, |
|
702 | + $this->getModelVersionInfo()->requestedVersion() |
|
703 | + ); |
|
704 | + } else { |
|
705 | + $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | + } |
|
707 | + } |
|
708 | + if ($do_chevy_shuffle) { |
|
709 | + $post = $old_post; |
|
710 | + } |
|
711 | + return $result; |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
717 | + * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
718 | + * representation using $field_obj->prepare_for_set_from_db()) |
|
719 | + * |
|
720 | + * @param EE_Model_Field_Base $field_obj |
|
721 | + * @param mixed $value as it's stored on a model object |
|
722 | + * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
723 | + * @return mixed |
|
724 | + * @throws ObjectDetectedException if $value contains a PHP object |
|
725 | + */ |
|
726 | + protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
727 | + { |
|
728 | + $value = $field_obj->prepare_for_set_from_db($value); |
|
729 | + switch ($format) { |
|
730 | + case 'pretty': |
|
731 | + $value = $field_obj->prepare_for_pretty_echoing($value); |
|
732 | + break; |
|
733 | + case 'normal': |
|
734 | + default: |
|
735 | + $value = $field_obj->prepare_for_get($value); |
|
736 | + break; |
|
737 | + } |
|
738 | + return ModelDataTranslator::prepareFieldValuesForJson( |
|
739 | + $field_obj, |
|
740 | + $value, |
|
741 | + $this->getModelVersionInfo()->requestedVersion() |
|
742 | + ); |
|
743 | + } |
|
744 | + |
|
745 | + |
|
746 | + /** |
|
747 | + * Adds a few extra fields to the entity response |
|
748 | + * |
|
749 | + * @param EEM_Base $model |
|
750 | + * @param array $db_row |
|
751 | + * @param array $entity_array |
|
752 | + * @return array modified entity |
|
753 | + */ |
|
754 | + protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
755 | + { |
|
756 | + if ($model instanceof EEM_CPT_Base) { |
|
757 | + $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
758 | + } |
|
759 | + return $entity_array; |
|
760 | + } |
|
761 | + |
|
762 | + |
|
763 | + /** |
|
764 | + * Gets links we want to add to the response |
|
765 | + * |
|
766 | + * @global \WP_REST_Server $wp_rest_server |
|
767 | + * @param EEM_Base $model |
|
768 | + * @param array $db_row |
|
769 | + * @param array $entity_array |
|
770 | + * @return array the _links item in the entity |
|
771 | + */ |
|
772 | + protected function getEntityLinks($model, $db_row, $entity_array) |
|
773 | + { |
|
774 | + // add basic links |
|
775 | + $links = array(); |
|
776 | + if ($model->has_primary_key_field()) { |
|
777 | + $links['self'] = array( |
|
778 | + array( |
|
779 | + 'href' => $this->getVersionedLinkTo( |
|
780 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
781 | + . '/' |
|
782 | + . $entity_array[ $model->primary_key_name() ] |
|
783 | + ), |
|
784 | + ), |
|
785 | + ); |
|
786 | + } |
|
787 | + $links['collection'] = array( |
|
788 | + array( |
|
789 | + 'href' => $this->getVersionedLinkTo( |
|
790 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
791 | + ), |
|
792 | + ), |
|
793 | + ); |
|
794 | + // add links to related models |
|
795 | + if ($model->has_primary_key_field()) { |
|
796 | + foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
797 | + $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
798 | + $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
799 | + array( |
|
800 | + 'href' => $this->getVersionedLinkTo( |
|
801 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
802 | + . '/' |
|
803 | + . $entity_array[ $model->primary_key_name() ] |
|
804 | + . '/' |
|
805 | + . $related_model_part |
|
806 | + ), |
|
807 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
808 | + ), |
|
809 | + ); |
|
810 | + } |
|
811 | + } |
|
812 | + return $links; |
|
813 | + } |
|
814 | + |
|
815 | + |
|
816 | + /** |
|
817 | + * Adds the included models indicated in the request to the entity provided |
|
818 | + * |
|
819 | + * @param EEM_Base $model |
|
820 | + * @param WP_REST_Request $rest_request |
|
821 | + * @param array $entity_array |
|
822 | + * @param array $db_row |
|
823 | + * @return array the modified entity |
|
824 | + */ |
|
825 | + protected function includeRequestedModels( |
|
826 | + EEM_Base $model, |
|
827 | + WP_REST_Request $rest_request, |
|
828 | + $entity_array, |
|
829 | + $db_row = array() |
|
830 | + ) { |
|
831 | + // if $db_row not included, hope the entity array has what we need |
|
832 | + if (! $db_row) { |
|
833 | + $db_row = $entity_array; |
|
834 | + } |
|
835 | + $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
836 | + $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
837 | + // if they passed in * or didn't specify any includes, return everything |
|
838 | + if (! in_array('*', $includes_for_this_model) |
|
839 | + && ! empty($includes_for_this_model) |
|
840 | + ) { |
|
841 | + if ($model->has_primary_key_field()) { |
|
842 | + // always include the primary key. ya just gotta know that at least |
|
843 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
844 | + } |
|
845 | + if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
846 | + $includes_for_this_model[] = '_calculated_fields'; |
|
847 | + } |
|
848 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
849 | + } |
|
850 | + $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
851 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
852 | + $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
853 | + $rest_request->get_param('include'), |
|
854 | + $relation_name |
|
855 | + ); |
|
856 | + $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
857 | + $rest_request->get_param('calculate'), |
|
858 | + $relation_name |
|
859 | + ); |
|
860 | + // did they specify they wanted to include a related model, or |
|
861 | + // specific fields from a related model? |
|
862 | + // or did they specify to calculate a field from a related model? |
|
863 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
864 | + // if so, we should include at least some part of the related model |
|
865 | + $pretend_related_request = new WP_REST_Request(); |
|
866 | + $pretend_related_request->set_query_params( |
|
867 | + array( |
|
868 | + 'caps' => $rest_request->get_param('caps'), |
|
869 | + 'include' => $related_fields_to_include, |
|
870 | + 'calculate' => $related_fields_to_calculate, |
|
871 | + ) |
|
872 | + ); |
|
873 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
874 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
875 | + $model->get_index_primary_key_string( |
|
876 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
877 | + ) |
|
878 | + ); |
|
879 | + $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
880 | + $primary_model_query_params, |
|
881 | + $relation_obj, |
|
882 | + $pretend_related_request |
|
883 | + ); |
|
884 | + $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
885 | + instanceof |
|
886 | + WP_Error |
|
887 | + ? null |
|
888 | + : $related_results; |
|
889 | + } |
|
890 | + } |
|
891 | + return $entity_array; |
|
892 | + } |
|
893 | + |
|
894 | + |
|
895 | + /** |
|
896 | + * Returns a new array with all the names of models removed. Eg |
|
897 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
898 | + * |
|
899 | + * @param array $arr |
|
900 | + * @return array |
|
901 | + */ |
|
902 | + private function removeModelNamesFromArray($arr) |
|
903 | + { |
|
904 | + return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
905 | + } |
|
906 | + |
|
907 | + |
|
908 | + /** |
|
909 | + * Gets the calculated fields for the response |
|
910 | + * |
|
911 | + * @param EEM_Base $model |
|
912 | + * @param array $wpdb_row |
|
913 | + * @param WP_REST_Request $rest_request |
|
914 | + * @return \stdClass the _calculations item in the entity |
|
915 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
916 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
917 | + */ |
|
918 | + protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
919 | + { |
|
920 | + $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
921 | + $rest_request->get_param('calculate'), |
|
922 | + '' |
|
923 | + ); |
|
924 | + // note: setting calculate=* doesn't do anything |
|
925 | + $calculated_fields_to_return = new \stdClass(); |
|
926 | + foreach ($calculated_fields as $field_to_calculate) { |
|
927 | + try { |
|
928 | + $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
929 | + null, |
|
930 | + $this->fields_calculator->retrieveCalculatedFieldValue( |
|
931 | + $model, |
|
932 | + $field_to_calculate, |
|
933 | + $wpdb_row, |
|
934 | + $rest_request, |
|
935 | + $this |
|
936 | + ), |
|
937 | + $this->getModelVersionInfo()->requestedVersion() |
|
938 | + ); |
|
939 | + } catch (RestException $e) { |
|
940 | + // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
941 | + $this->setResponseHeader( |
|
942 | + 'Notices-Field-Calculation-Errors[' |
|
943 | + . $e->getStringCode() |
|
944 | + . '][' |
|
945 | + . $model->get_this_model_name() |
|
946 | + . '][' |
|
947 | + . $field_to_calculate |
|
948 | + . ']', |
|
949 | + $e->getMessage(), |
|
950 | + true |
|
951 | + ); |
|
952 | + } |
|
953 | + } |
|
954 | + return $calculated_fields_to_return; |
|
955 | + } |
|
956 | + |
|
957 | + |
|
958 | + /** |
|
959 | + * Gets the full URL to the resource, taking the requested version into account |
|
960 | + * |
|
961 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
962 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
963 | + */ |
|
964 | + public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
965 | + { |
|
966 | + return rest_url( |
|
967 | + EED_Core_Rest_Api::get_versioned_route_to( |
|
968 | + $link_part_after_version_and_slash, |
|
969 | + $this->getModelVersionInfo()->requestedVersion() |
|
970 | + ) |
|
971 | + ); |
|
972 | + } |
|
973 | + |
|
974 | + |
|
975 | + /** |
|
976 | + * Gets the correct lowercase name for the relation in the API according |
|
977 | + * to the relation's type |
|
978 | + * |
|
979 | + * @param string $relation_name |
|
980 | + * @param \EE_Model_Relation_Base $relation_obj |
|
981 | + * @return string |
|
982 | + */ |
|
983 | + public static function getRelatedEntityName($relation_name, $relation_obj) |
|
984 | + { |
|
985 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
986 | + return strtolower($relation_name); |
|
987 | + } else { |
|
988 | + return EEH_Inflector::pluralize_and_lower($relation_name); |
|
989 | + } |
|
990 | + } |
|
991 | + |
|
992 | + |
|
993 | + /** |
|
994 | + * Gets the one model object with the specified id for the specified model |
|
995 | + * |
|
996 | + * @param EEM_Base $model |
|
997 | + * @param WP_REST_Request $request |
|
998 | + * @return array|WP_Error |
|
999 | + */ |
|
1000 | + public function getEntityFromModel($model, $request) |
|
1001 | + { |
|
1002 | + $context = $this->validateContext($request->get_param('caps')); |
|
1003 | + return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1004 | + } |
|
1005 | + |
|
1006 | + |
|
1007 | + /** |
|
1008 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
1009 | + * version so just treat it as a default read |
|
1010 | + * |
|
1011 | + * @param string $context |
|
1012 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1013 | + */ |
|
1014 | + public function validateContext($context) |
|
1015 | + { |
|
1016 | + if (! $context) { |
|
1017 | + $context = EEM_Base::caps_read; |
|
1018 | + } |
|
1019 | + $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1020 | + if (in_array($context, $valid_contexts)) { |
|
1021 | + return $context; |
|
1022 | + } else { |
|
1023 | + return EEM_Base::caps_read; |
|
1024 | + } |
|
1025 | + } |
|
1026 | + |
|
1027 | + |
|
1028 | + /** |
|
1029 | + * Verifies the passed in value is an allowable default where conditions value. |
|
1030 | + * |
|
1031 | + * @param $default_query_params |
|
1032 | + * @return string |
|
1033 | + */ |
|
1034 | + public function validateDefaultQueryParams($default_query_params) |
|
1035 | + { |
|
1036 | + $valid_default_where_conditions_for_api_calls = array( |
|
1037 | + EEM_Base::default_where_conditions_all, |
|
1038 | + EEM_Base::default_where_conditions_minimum_all, |
|
1039 | + EEM_Base::default_where_conditions_minimum_others, |
|
1040 | + ); |
|
1041 | + if (! $default_query_params) { |
|
1042 | + $default_query_params = EEM_Base::default_where_conditions_all; |
|
1043 | + } |
|
1044 | + if (in_array( |
|
1045 | + $default_query_params, |
|
1046 | + $valid_default_where_conditions_for_api_calls, |
|
1047 | + true |
|
1048 | + )) { |
|
1049 | + return $default_query_params; |
|
1050 | + } else { |
|
1051 | + return EEM_Base::default_where_conditions_all; |
|
1052 | + } |
|
1053 | + } |
|
1054 | + |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1058 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1059 | + * can be left as-is, but it's quite possible this will change someday. |
|
1060 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1061 | + * |
|
1062 | + * @param EEM_Base $model |
|
1063 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1064 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1065 | + * that absolutely no results should be returned |
|
1066 | + * @throws EE_Error |
|
1067 | + * @throws RestException |
|
1068 | + */ |
|
1069 | + public function createModelQueryParams($model, $query_parameters) |
|
1070 | + { |
|
1071 | + $model_query_params = array(); |
|
1072 | + if (isset($query_parameters['where'])) { |
|
1073 | + $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1074 | + $query_parameters['where'], |
|
1075 | + $model, |
|
1076 | + $this->getModelVersionInfo()->requestedVersion() |
|
1077 | + ); |
|
1078 | + } |
|
1079 | + if (isset($query_parameters['order_by'])) { |
|
1080 | + $order_by = $query_parameters['order_by']; |
|
1081 | + } elseif (isset($query_parameters['orderby'])) { |
|
1082 | + $order_by = $query_parameters['orderby']; |
|
1083 | + } else { |
|
1084 | + $order_by = null; |
|
1085 | + } |
|
1086 | + if ($order_by !== null) { |
|
1087 | + if (is_array($order_by)) { |
|
1088 | + $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1089 | + } else { |
|
1090 | + // it's a single item |
|
1091 | + $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1092 | + } |
|
1093 | + $model_query_params['order_by'] = $order_by; |
|
1094 | + } |
|
1095 | + if (isset($query_parameters['group_by'])) { |
|
1096 | + $group_by = $query_parameters['group_by']; |
|
1097 | + } elseif (isset($query_parameters['groupby'])) { |
|
1098 | + $group_by = $query_parameters['groupby']; |
|
1099 | + } else { |
|
1100 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1101 | + } |
|
1102 | + // make sure they're all real names |
|
1103 | + if (is_array($group_by)) { |
|
1104 | + $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1105 | + } |
|
1106 | + if ($group_by !== null) { |
|
1107 | + $model_query_params['group_by'] = $group_by; |
|
1108 | + } |
|
1109 | + if (isset($query_parameters['having'])) { |
|
1110 | + $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1111 | + $query_parameters['having'], |
|
1112 | + $model, |
|
1113 | + $this->getModelVersionInfo()->requestedVersion() |
|
1114 | + ); |
|
1115 | + } |
|
1116 | + if (isset($query_parameters['order'])) { |
|
1117 | + $model_query_params['order'] = $query_parameters['order']; |
|
1118 | + } |
|
1119 | + if (isset($query_parameters['mine'])) { |
|
1120 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1121 | + } |
|
1122 | + if (isset($query_parameters['limit'])) { |
|
1123 | + // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1124 | + if (! is_array($query_parameters['limit'])) { |
|
1125 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1126 | + } else { |
|
1127 | + $limit_array = $query_parameters['limit']; |
|
1128 | + } |
|
1129 | + $sanitized_limit = array(); |
|
1130 | + foreach ($limit_array as $key => $limit_part) { |
|
1131 | + if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | + throw new EE_Error( |
|
1133 | + sprintf( |
|
1134 | + __( |
|
1135 | + // @codingStandardsIgnoreStart |
|
1136 | + '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.', |
|
1137 | + // @codingStandardsIgnoreEnd |
|
1138 | + 'event_espresso' |
|
1139 | + ), |
|
1140 | + wp_json_encode($query_parameters['limit']) |
|
1141 | + ) |
|
1142 | + ); |
|
1143 | + } |
|
1144 | + $sanitized_limit[] = (int) $limit_part; |
|
1145 | + } |
|
1146 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1147 | + } else { |
|
1148 | + $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1149 | + } |
|
1150 | + if (isset($query_parameters['caps'])) { |
|
1151 | + $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1152 | + } else { |
|
1153 | + $model_query_params['caps'] = EEM_Base::caps_read; |
|
1154 | + } |
|
1155 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1156 | + $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1157 | + $query_parameters['default_where_conditions'] |
|
1158 | + ); |
|
1159 | + } |
|
1160 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1161 | + } |
|
1162 | + |
|
1163 | + |
|
1164 | + /** |
|
1165 | + * Changes the REST-style query params for use in the models |
|
1166 | + * |
|
1167 | + * @deprecated |
|
1168 | + * @param EEM_Base $model |
|
1169 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1170 | + * @return array |
|
1171 | + */ |
|
1172 | + public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1173 | + { |
|
1174 | + $model_ready_query_params = array(); |
|
1175 | + foreach ($query_params as $key => $value) { |
|
1176 | + if (is_array($value)) { |
|
1177 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | + } else { |
|
1179 | + $model_ready_query_params[ $key ] = $value; |
|
1180 | + } |
|
1181 | + } |
|
1182 | + return $model_ready_query_params; |
|
1183 | + } |
|
1184 | + |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1188 | + * @param $model |
|
1189 | + * @param $query_params |
|
1190 | + * @return array |
|
1191 | + */ |
|
1192 | + public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1193 | + { |
|
1194 | + $model_ready_query_params = array(); |
|
1195 | + foreach ($query_params as $key => $value) { |
|
1196 | + if (is_array($value)) { |
|
1197 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | + } else { |
|
1199 | + $model_ready_query_params[ $key ] = $value; |
|
1200 | + } |
|
1201 | + } |
|
1202 | + return $model_ready_query_params; |
|
1203 | + } |
|
1204 | + |
|
1205 | + |
|
1206 | + /** |
|
1207 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1208 | + * If no prefix is specified, returns items with no period. |
|
1209 | + * |
|
1210 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1211 | + * @param string $prefix "Event" or "foobar" |
|
1212 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1213 | + * we only return strings starting with that and a period; if no prefix was |
|
1214 | + * specified we return all items containing NO periods |
|
1215 | + */ |
|
1216 | + public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1217 | + { |
|
1218 | + if (is_string($string_to_explode)) { |
|
1219 | + $exploded_contents = explode(',', $string_to_explode); |
|
1220 | + } elseif (is_array($string_to_explode)) { |
|
1221 | + $exploded_contents = $string_to_explode; |
|
1222 | + } else { |
|
1223 | + $exploded_contents = array(); |
|
1224 | + } |
|
1225 | + // if the string was empty, we want an empty array |
|
1226 | + $exploded_contents = array_filter($exploded_contents); |
|
1227 | + $contents_with_prefix = array(); |
|
1228 | + foreach ($exploded_contents as $item) { |
|
1229 | + $item = trim($item); |
|
1230 | + // if no prefix was provided, so we look for items with no "." in them |
|
1231 | + if (! $prefix) { |
|
1232 | + // does this item have a period? |
|
1233 | + if (strpos($item, '.') === false) { |
|
1234 | + // if not, then its what we're looking for |
|
1235 | + $contents_with_prefix[] = $item; |
|
1236 | + } |
|
1237 | + } elseif (strpos($item, $prefix . '.') === 0) { |
|
1238 | + // this item has the prefix and a period, grab it |
|
1239 | + $contents_with_prefix[] = substr( |
|
1240 | + $item, |
|
1241 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1242 | + ); |
|
1243 | + } elseif ($item === $prefix) { |
|
1244 | + // this item is JUST the prefix |
|
1245 | + // so let's grab everything after, which is a blank string |
|
1246 | + $contents_with_prefix[] = ''; |
|
1247 | + } |
|
1248 | + } |
|
1249 | + return $contents_with_prefix; |
|
1250 | + } |
|
1251 | + |
|
1252 | + |
|
1253 | + /** |
|
1254 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1255 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1256 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1257 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1258 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1259 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1260 | + * name and then a period). |
|
1261 | + * @param string $include_string @see Read:handle_request_get_all |
|
1262 | + * @param string $model_name |
|
1263 | + * @return array of fields for this model. If $model_name is provided, then |
|
1264 | + * the fields for that model, with the model's name removed from each. |
|
1265 | + * If $include_string was blank or '*' returns an empty array |
|
1266 | + */ |
|
1267 | + public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1268 | + { |
|
1269 | + if (is_array($include_string)) { |
|
1270 | + $include_string = implode(',', $include_string); |
|
1271 | + } |
|
1272 | + if ($include_string === '*' || $include_string === '') { |
|
1273 | + return array(); |
|
1274 | + } |
|
1275 | + $includes = explode(',', $include_string); |
|
1276 | + $extracted_fields_to_include = array(); |
|
1277 | + if ($model_name) { |
|
1278 | + foreach ($includes as $field_to_include) { |
|
1279 | + $field_to_include = trim($field_to_include); |
|
1280 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1281 | + // found the model name at the exact start |
|
1282 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1283 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1284 | + } elseif ($field_to_include == $model_name) { |
|
1285 | + $extracted_fields_to_include[] = '*'; |
|
1286 | + } |
|
1287 | + } |
|
1288 | + } else { |
|
1289 | + // look for ones with no period |
|
1290 | + foreach ($includes as $field_to_include) { |
|
1291 | + $field_to_include = trim($field_to_include); |
|
1292 | + if (strpos($field_to_include, '.') === false |
|
1293 | + && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1294 | + ) { |
|
1295 | + $extracted_fields_to_include[] = $field_to_include; |
|
1296 | + } |
|
1297 | + } |
|
1298 | + } |
|
1299 | + return $extracted_fields_to_include; |
|
1300 | + } |
|
1301 | + |
|
1302 | + |
|
1303 | + /** |
|
1304 | + * Gets the single item using the model according to the request in the context given, otherwise |
|
1305 | + * returns that it's inaccessible to the current user |
|
1306 | + * |
|
1307 | + * @param EEM_Base $model |
|
1308 | + * @param WP_REST_Request $request |
|
1309 | + * @param null $context |
|
1310 | + * @return array|WP_Error |
|
1311 | + */ |
|
1312 | + public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1313 | + { |
|
1314 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1315 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1316 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1317 | + } |
|
1318 | + $restricted_query_params = $query_params; |
|
1319 | + $restricted_query_params['caps'] = $context; |
|
1320 | + $this->setDebugInfo('model query params', $restricted_query_params); |
|
1321 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1322 | + if (! empty($model_rows)) { |
|
1323 | + return $this->createEntityFromWpdbResult( |
|
1324 | + $model, |
|
1325 | + array_shift($model_rows), |
|
1326 | + $request |
|
1327 | + ); |
|
1328 | + } else { |
|
1329 | + // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1330 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1331 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1332 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
1333 | + // you got shafted- it existed but we didn't want to tell you! |
|
1334 | + return new WP_Error( |
|
1335 | + 'rest_user_cannot_' . $context, |
|
1336 | + sprintf( |
|
1337 | + __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1338 | + $context, |
|
1339 | + strtolower($model->get_this_model_name()), |
|
1340 | + Capabilities::getMissingPermissionsString( |
|
1341 | + $model, |
|
1342 | + $context |
|
1343 | + ) |
|
1344 | + ), |
|
1345 | + array('status' => 403) |
|
1346 | + ); |
|
1347 | + } else { |
|
1348 | + // it's not you. It just doesn't exist |
|
1349 | + return new WP_Error( |
|
1350 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1351 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1352 | + array('status' => 404) |
|
1353 | + ); |
|
1354 | + } |
|
1355 | + } |
|
1356 | + } |
|
1357 | 1357 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | $controller = new Read(); |
65 | 65 | try { |
66 | 66 | $controller->setRequestedVersion($version); |
67 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
67 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | 68 | return $controller->sendResponse( |
69 | 69 | new WP_Error( |
70 | 70 | 'endpoint_parsing_error', |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $controller = new Read(); |
103 | 103 | try { |
104 | 104 | $controller->setRequestedVersion($version); |
105 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
105 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | 106 | return array(); |
107 | 107 | } |
108 | 108 | // get the model for this version |
@@ -159,11 +159,11 @@ discard block |
||
159 | 159 | */ |
160 | 160 | protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
161 | 161 | { |
162 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
162 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
163 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
164 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
165 | 165 | if ($default_key === 'raw') { |
166 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
166 | + $schema['properties'][$field_name]['default'][$default_key] = |
|
167 | 167 | ModelDataTranslator::prepareFieldValueForJson( |
168 | 168 | $field, |
169 | 169 | $default_value, |
@@ -172,9 +172,9 @@ discard block |
||
172 | 172 | } |
173 | 173 | } |
174 | 174 | } else { |
175 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
175 | + $schema['properties'][$field_name]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | 176 | $field, |
177 | - $schema['properties'][ $field_name ]['default'], |
|
177 | + $schema['properties'][$field_name]['default'], |
|
178 | 178 | $this->getModelVersionInfo()->requestedVersion() |
179 | 179 | ); |
180 | 180 | } |
@@ -196,9 +196,9 @@ discard block |
||
196 | 196 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
197 | 197 | { |
198 | 198 | if ($field instanceof EE_Datetime_Field) { |
199 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
199 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
200 | 200 | // modify the description |
201 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
201 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
202 | 202 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
203 | 203 | wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
204 | 204 | ); |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $controller = new Read(); |
239 | 239 | try { |
240 | 240 | $controller->setRequestedVersion($version); |
241 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
241 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | 242 | return $controller->sendResponse( |
243 | 243 | new WP_Error( |
244 | 244 | 'endpoint_parsing_error', |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | $controller = new Read(); |
284 | 284 | try { |
285 | 285 | $controller->setRequestedVersion($version); |
286 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
286 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | 287 | return $controller->sendResponse( |
288 | 288 | new WP_Error( |
289 | 289 | 'endpoint_parsing_error', |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | ); |
299 | 299 | } |
300 | 300 | $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
301 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
301 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | 302 | return $controller->sendResponse( |
303 | 303 | new WP_Error( |
304 | 304 | 'endpoint_parsing_error', |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | public function getEntitiesFromModel($model, $request) |
336 | 336 | { |
337 | 337 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
338 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
338 | + if ( ! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | 339 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
340 | 340 | return new WP_Error( |
341 | 341 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | array('status' => 403) |
348 | 348 | ); |
349 | 349 | } |
350 | - if (! $request->get_header('no_rest_headers')) { |
|
350 | + if ( ! $request->get_header('no_rest_headers')) { |
|
351 | 351 | $this->setHeadersFromQueryParams($model, $query_params); |
352 | 352 | } |
353 | 353 | /** @type array $results */ |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | $context = $this->validateContext($request->get_param('caps')); |
383 | 383 | $model = $relation->get_this_model(); |
384 | 384 | $related_model = $relation->get_other_model(); |
385 | - if (! isset($primary_model_query_params[0])) { |
|
385 | + if ( ! isset($primary_model_query_params[0])) { |
|
386 | 386 | $primary_model_query_params[0] = array(); |
387 | 387 | } |
388 | 388 | // check if they can access the 1st model object |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | $restricted_query_params['caps'] = $context; |
400 | 400 | $this->setDebugInfo('main model query params', $restricted_query_params); |
401 | 401 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
402 | - if (! ( |
|
402 | + if ( ! ( |
|
403 | 403 | Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
404 | 404 | && $model->exists($restricted_query_params) |
405 | 405 | ) |
@@ -432,13 +432,13 @@ discard block |
||
432 | 432 | } |
433 | 433 | $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
434 | 434 | foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
435 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
435 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
436 | 436 | . '.' |
437 | - . $where_condition_key ] = $where_condition_value; |
|
437 | + . $where_condition_key] = $where_condition_value; |
|
438 | 438 | } |
439 | 439 | $query_params['default_where_conditions'] = 'none'; |
440 | 440 | $query_params['caps'] = $context; |
441 | - if (! $request->get_header('no_rest_headers')) { |
|
441 | + if ( ! $request->get_header('no_rest_headers')) { |
|
442 | 442 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
443 | 443 | } |
444 | 444 | /** @type array $results */ |
@@ -489,7 +489,7 @@ discard block |
||
489 | 489 | */ |
490 | 490 | public function getEntitiesFromRelation($id, $relation, $request) |
491 | 491 | { |
492 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
492 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | 493 | throw new EE_Error( |
494 | 494 | sprintf( |
495 | 495 | __( |
@@ -531,7 +531,7 @@ discard block |
||
531 | 531 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
532 | 532 | ); |
533 | 533 | // normally the limit to a 2-part array, where the 2nd item is the limit |
534 | - if (! isset($query_params['limit'])) { |
|
534 | + if ( ! isset($query_params['limit'])) { |
|
535 | 535 | $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
536 | 536 | } |
537 | 537 | if (is_array($query_params['limit'])) { |
@@ -564,7 +564,7 @@ discard block |
||
564 | 564 | */ |
565 | 565 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
566 | 566 | { |
567 | - if (! $rest_request instanceof WP_REST_Request) { |
|
567 | + if ( ! $rest_request instanceof WP_REST_Request) { |
|
568 | 568 | // ok so this was called in the old style, where the 3rd arg was |
569 | 569 | // $include, and the 4th arg was $context |
570 | 570 | // now setup the request just to avoid fatal errors, although we won't be able |
@@ -645,8 +645,8 @@ discard block |
||
645 | 645 | if ($do_chevy_shuffle) { |
646 | 646 | global $post; |
647 | 647 | $old_post = $post; |
648 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | - if (! $post instanceof \WP_Post) { |
|
648 | + $post = get_post($result[$model->primary_key_name()]); |
|
649 | + if ( ! $post instanceof \WP_Post) { |
|
650 | 650 | // well that's weird, because $result is what we JUST fetched from the database |
651 | 651 | throw new RestException( |
652 | 652 | 'error_fetching_post_from_database_results', |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | ) |
657 | 657 | ); |
658 | 658 | } |
659 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
659 | + $model_object_classname = 'EE_'.$model->get_this_model_name(); |
|
660 | 660 | $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
661 | 661 | $model_object_classname, |
662 | 662 | $result, |
@@ -667,13 +667,13 @@ discard block |
||
667 | 667 | foreach ($result as $field_name => $field_value) { |
668 | 668 | $field_obj = $model->field_settings_for($field_name); |
669 | 669 | if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
670 | - unset($result[ $field_name ]); |
|
670 | + unset($result[$field_name]); |
|
671 | 671 | } elseif ($this->isSubclassOfOne( |
672 | 672 | $field_obj, |
673 | 673 | $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
674 | 674 | ) |
675 | 675 | ) { |
676 | - $result[ $field_name ] = array( |
|
676 | + $result[$field_name] = array( |
|
677 | 677 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
678 | 678 | 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
679 | 679 | ); |
@@ -682,7 +682,7 @@ discard block |
||
682 | 682 | $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
683 | 683 | ) |
684 | 684 | ) { |
685 | - $result[ $field_name ] = array( |
|
685 | + $result[$field_name] = array( |
|
686 | 686 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
687 | 687 | 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
688 | 688 | ); |
@@ -690,19 +690,19 @@ discard block |
||
690 | 690 | $field_value = $field_obj->prepare_for_set_from_db($field_value); |
691 | 691 | $timezone = $field_value->getTimezone(); |
692 | 692 | EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
693 | - $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
693 | + $result[$field_name.'_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | 694 | $field_obj, |
695 | 695 | $field_value, |
696 | 696 | $this->getModelVersionInfo()->requestedVersion() |
697 | 697 | ); |
698 | 698 | EEH_DTT_Helper::setTimezone($field_value, $timezone); |
699 | - $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
699 | + $result[$field_name] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | 700 | $field_obj, |
701 | 701 | $field_value, |
702 | 702 | $this->getModelVersionInfo()->requestedVersion() |
703 | 703 | ); |
704 | 704 | } else { |
705 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
705 | + $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | 706 | } |
707 | 707 | } |
708 | 708 | if ($do_chevy_shuffle) { |
@@ -754,7 +754,7 @@ discard block |
||
754 | 754 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
755 | 755 | { |
756 | 756 | if ($model instanceof EEM_CPT_Base) { |
757 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
757 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
758 | 758 | } |
759 | 759 | return $entity_array; |
760 | 760 | } |
@@ -779,7 +779,7 @@ discard block |
||
779 | 779 | 'href' => $this->getVersionedLinkTo( |
780 | 780 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
781 | 781 | . '/' |
782 | - . $entity_array[ $model->primary_key_name() ] |
|
782 | + . $entity_array[$model->primary_key_name()] |
|
783 | 783 | ), |
784 | 784 | ), |
785 | 785 | ); |
@@ -795,12 +795,12 @@ discard block |
||
795 | 795 | if ($model->has_primary_key_field()) { |
796 | 796 | foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
797 | 797 | $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
798 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
798 | + $links[EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
799 | 799 | array( |
800 | 800 | 'href' => $this->getVersionedLinkTo( |
801 | 801 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
802 | 802 | . '/' |
803 | - . $entity_array[ $model->primary_key_name() ] |
|
803 | + . $entity_array[$model->primary_key_name()] |
|
804 | 804 | . '/' |
805 | 805 | . $related_model_part |
806 | 806 | ), |
@@ -829,13 +829,13 @@ discard block |
||
829 | 829 | $db_row = array() |
830 | 830 | ) { |
831 | 831 | // if $db_row not included, hope the entity array has what we need |
832 | - if (! $db_row) { |
|
832 | + if ( ! $db_row) { |
|
833 | 833 | $db_row = $entity_array; |
834 | 834 | } |
835 | 835 | $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
836 | 836 | $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
837 | 837 | // if they passed in * or didn't specify any includes, return everything |
838 | - if (! in_array('*', $includes_for_this_model) |
|
838 | + if ( ! in_array('*', $includes_for_this_model) |
|
839 | 839 | && ! empty($includes_for_this_model) |
840 | 840 | ) { |
841 | 841 | if ($model->has_primary_key_field()) { |
@@ -881,7 +881,7 @@ discard block |
||
881 | 881 | $relation_obj, |
882 | 882 | $pretend_related_request |
883 | 883 | ); |
884 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
884 | + $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
885 | 885 | instanceof |
886 | 886 | WP_Error |
887 | 887 | ? null |
@@ -1013,7 +1013,7 @@ discard block |
||
1013 | 1013 | */ |
1014 | 1014 | public function validateContext($context) |
1015 | 1015 | { |
1016 | - if (! $context) { |
|
1016 | + if ( ! $context) { |
|
1017 | 1017 | $context = EEM_Base::caps_read; |
1018 | 1018 | } |
1019 | 1019 | $valid_contexts = EEM_Base::valid_cap_contexts(); |
@@ -1038,7 +1038,7 @@ discard block |
||
1038 | 1038 | EEM_Base::default_where_conditions_minimum_all, |
1039 | 1039 | EEM_Base::default_where_conditions_minimum_others, |
1040 | 1040 | ); |
1041 | - if (! $default_query_params) { |
|
1041 | + if ( ! $default_query_params) { |
|
1042 | 1042 | $default_query_params = EEM_Base::default_where_conditions_all; |
1043 | 1043 | } |
1044 | 1044 | if (in_array( |
@@ -1121,14 +1121,14 @@ discard block |
||
1121 | 1121 | } |
1122 | 1122 | if (isset($query_parameters['limit'])) { |
1123 | 1123 | // limit should be either a string like '23' or '23,43', or an array with two items in it |
1124 | - if (! is_array($query_parameters['limit'])) { |
|
1124 | + if ( ! is_array($query_parameters['limit'])) { |
|
1125 | 1125 | $limit_array = explode(',', (string) $query_parameters['limit']); |
1126 | 1126 | } else { |
1127 | 1127 | $limit_array = $query_parameters['limit']; |
1128 | 1128 | } |
1129 | 1129 | $sanitized_limit = array(); |
1130 | 1130 | foreach ($limit_array as $key => $limit_part) { |
1131 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1131 | + if ($this->debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | 1132 | throw new EE_Error( |
1133 | 1133 | sprintf( |
1134 | 1134 | __( |
@@ -1174,9 +1174,9 @@ discard block |
||
1174 | 1174 | $model_ready_query_params = array(); |
1175 | 1175 | foreach ($query_params as $key => $value) { |
1176 | 1176 | if (is_array($value)) { |
1177 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1177 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | 1178 | } else { |
1179 | - $model_ready_query_params[ $key ] = $value; |
|
1179 | + $model_ready_query_params[$key] = $value; |
|
1180 | 1180 | } |
1181 | 1181 | } |
1182 | 1182 | return $model_ready_query_params; |
@@ -1194,9 +1194,9 @@ discard block |
||
1194 | 1194 | $model_ready_query_params = array(); |
1195 | 1195 | foreach ($query_params as $key => $value) { |
1196 | 1196 | if (is_array($value)) { |
1197 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1197 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | 1198 | } else { |
1199 | - $model_ready_query_params[ $key ] = $value; |
|
1199 | + $model_ready_query_params[$key] = $value; |
|
1200 | 1200 | } |
1201 | 1201 | } |
1202 | 1202 | return $model_ready_query_params; |
@@ -1228,17 +1228,17 @@ discard block |
||
1228 | 1228 | foreach ($exploded_contents as $item) { |
1229 | 1229 | $item = trim($item); |
1230 | 1230 | // if no prefix was provided, so we look for items with no "." in them |
1231 | - if (! $prefix) { |
|
1231 | + if ( ! $prefix) { |
|
1232 | 1232 | // does this item have a period? |
1233 | 1233 | if (strpos($item, '.') === false) { |
1234 | 1234 | // if not, then its what we're looking for |
1235 | 1235 | $contents_with_prefix[] = $item; |
1236 | 1236 | } |
1237 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1237 | + } elseif (strpos($item, $prefix.'.') === 0) { |
|
1238 | 1238 | // this item has the prefix and a period, grab it |
1239 | 1239 | $contents_with_prefix[] = substr( |
1240 | 1240 | $item, |
1241 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1241 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1242 | 1242 | ); |
1243 | 1243 | } elseif ($item === $prefix) { |
1244 | 1244 | // this item is JUST the prefix |
@@ -1277,9 +1277,9 @@ discard block |
||
1277 | 1277 | if ($model_name) { |
1278 | 1278 | foreach ($includes as $field_to_include) { |
1279 | 1279 | $field_to_include = trim($field_to_include); |
1280 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1280 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1281 | 1281 | // found the model name at the exact start |
1282 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1282 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1283 | 1283 | $extracted_fields_to_include[] = $field_sans_model_name; |
1284 | 1284 | } elseif ($field_to_include == $model_name) { |
1285 | 1285 | $extracted_fields_to_include[] = '*'; |
@@ -1319,7 +1319,7 @@ discard block |
||
1319 | 1319 | $restricted_query_params['caps'] = $context; |
1320 | 1320 | $this->setDebugInfo('model query params', $restricted_query_params); |
1321 | 1321 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
1322 | - if (! empty($model_rows)) { |
|
1322 | + if ( ! empty($model_rows)) { |
|
1323 | 1323 | return $this->createEntityFromWpdbResult( |
1324 | 1324 | $model, |
1325 | 1325 | array_shift($model_rows), |
@@ -1329,10 +1329,10 @@ discard block |
||
1329 | 1329 | // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
1330 | 1330 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
1331 | 1331 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
1332 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1332 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
1333 | 1333 | // you got shafted- it existed but we didn't want to tell you! |
1334 | 1334 | return new WP_Error( |
1335 | - 'rest_user_cannot_' . $context, |
|
1335 | + 'rest_user_cannot_'.$context, |
|
1336 | 1336 | sprintf( |
1337 | 1337 | __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
1338 | 1338 | $context, |