@@ -12,7 +12,7 @@ |
||
12 | 12 | * @param string $table_column |
13 | 13 | * @param string $nicename |
14 | 14 | * @param bool $nullable |
15 | - * @param null $default_value |
|
15 | + * @param integer $default_value |
|
16 | 16 | */ |
17 | 17 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
18 | 18 | { |
@@ -8,91 +8,91 @@ |
||
8 | 8 | class EE_Money_Field extends EE_Float_Field |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * @param string $table_column |
|
13 | - * @param string $nicename |
|
14 | - * @param bool $nullable |
|
15 | - * @param null $default_value |
|
16 | - */ |
|
17 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
18 | - { |
|
19 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | - $this->setSchemaType('object'); |
|
21 | - } |
|
11 | + /** |
|
12 | + * @param string $table_column |
|
13 | + * @param string $nicename |
|
14 | + * @param bool $nullable |
|
15 | + * @param null $default_value |
|
16 | + */ |
|
17 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
18 | + { |
|
19 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | + $this->setSchemaType('object'); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * Schemas: |
|
26 | - * 'localized_float': "3,023.00" |
|
27 | - * 'no_currency_code': "$3,023.00" |
|
28 | - * null: "$3,023.00<span>USD</span>" |
|
29 | - * |
|
30 | - * @param string $value_on_field_to_be_outputted |
|
31 | - * @param string $schema |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
35 | - { |
|
36 | - $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
24 | + /** |
|
25 | + * Schemas: |
|
26 | + * 'localized_float': "3,023.00" |
|
27 | + * 'no_currency_code': "$3,023.00" |
|
28 | + * null: "$3,023.00<span>USD</span>" |
|
29 | + * |
|
30 | + * @param string $value_on_field_to_be_outputted |
|
31 | + * @param string $schema |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
35 | + { |
|
36 | + $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
37 | 37 | |
38 | - if ($schema == 'localized_float') { |
|
39 | - return $pretty_float; |
|
40 | - } |
|
41 | - if ($schema == 'no_currency_code') { |
|
38 | + if ($schema == 'localized_float') { |
|
39 | + return $pretty_float; |
|
40 | + } |
|
41 | + if ($schema == 'no_currency_code') { |
|
42 | 42 | // echo "schema no currency!"; |
43 | - $display_code = false; |
|
44 | - } else { |
|
45 | - $display_code = true; |
|
46 | - } |
|
47 | - //we don't use the $pretty_float because format_currency will take care of it. |
|
48 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
49 | - } |
|
43 | + $display_code = false; |
|
44 | + } else { |
|
45 | + $display_code = true; |
|
46 | + } |
|
47 | + //we don't use the $pretty_float because format_currency will take care of it. |
|
48 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
53 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
54 | - * Also, interprets periods and commas according to the country's currency settings. |
|
55 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
56 | - * |
|
57 | - * @param string $value_inputted_for_field_on_model_object |
|
58 | - * @return float |
|
59 | - */ |
|
60 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | - { |
|
62 | - //remove any currencies etc. |
|
51 | + /** |
|
52 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
53 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
54 | + * Also, interprets periods and commas according to the country's currency settings. |
|
55 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
56 | + * |
|
57 | + * @param string $value_inputted_for_field_on_model_object |
|
58 | + * @return float |
|
59 | + */ |
|
60 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | + { |
|
62 | + //remove any currencies etc. |
|
63 | 63 | // if(is_string($value_inputted_for_field_on_model_object)){ |
64 | 64 | // $value_inputted_for_field_on_model_object = preg_replace("/[^0-9,.]/", "", $value_inputted_for_field_on_model_object); |
65 | 65 | // } |
66 | - //now it's a float-style string or number |
|
67 | - $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
68 | - //round to the correctly number of decimal places for this currency |
|
69 | - $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
70 | - return $rounded_value; |
|
71 | - } |
|
66 | + //now it's a float-style string or number |
|
67 | + $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
68 | + //round to the correctly number of decimal places for this currency |
|
69 | + $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
70 | + return $rounded_value; |
|
71 | + } |
|
72 | 72 | |
73 | - function prepare_for_get($value_of_field_on_model_object) |
|
74 | - { |
|
75 | - $c = EE_Registry::instance()->CFG->currency; |
|
76 | - return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
77 | - } |
|
73 | + function prepare_for_get($value_of_field_on_model_object) |
|
74 | + { |
|
75 | + $c = EE_Registry::instance()->CFG->currency; |
|
76 | + return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
77 | + } |
|
78 | 78 | |
79 | - public function getSchemaProperties() |
|
80 | - { |
|
81 | - return array( |
|
82 | - 'raw' => array( |
|
83 | - 'description' => sprintf( |
|
84 | - __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
85 | - $this->get_nicename() |
|
86 | - ), |
|
87 | - 'type' => 'number' |
|
88 | - ), |
|
89 | - 'pretty' => array( |
|
90 | - 'description' => sprintf( |
|
91 | - __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
92 | - $this->get_nicename() |
|
93 | - ), |
|
94 | - 'type' => 'string' |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
79 | + public function getSchemaProperties() |
|
80 | + { |
|
81 | + return array( |
|
82 | + 'raw' => array( |
|
83 | + 'description' => sprintf( |
|
84 | + __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
85 | + $this->get_nicename() |
|
86 | + ), |
|
87 | + 'type' => 'number' |
|
88 | + ), |
|
89 | + 'pretty' => array( |
|
90 | + 'description' => sprintf( |
|
91 | + __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
92 | + $this->get_nicename() |
|
93 | + ), |
|
94 | + 'type' => 'string' |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | 98 | } |
99 | 99 | \ No newline at end of file |
@@ -18,844 +18,844 @@ |
||
18 | 18 | class EED_Core_Rest_Api extends \EED_Module |
19 | 19 | { |
20 | 20 | |
21 | - const ee_api_namespace = 'ee/v'; |
|
21 | + const ee_api_namespace = 'ee/v'; |
|
22 | 22 | |
23 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
24 | - |
|
25 | - const saved_routes_option_names = 'ee_core_routes'; |
|
26 | - |
|
27 | - /** |
|
28 | - * string used in _links response bodies to make them globally unique. |
|
29 | - * |
|
30 | - * @see http://v2.wp-api.org/extending/linking/ |
|
31 | - */ |
|
32 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var Calculated_Model_Fields |
|
36 | - */ |
|
37 | - protected static $_field_calculator = null; |
|
38 | - |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * @return EED_Core_Rest_Api |
|
43 | - */ |
|
44 | - public static function instance() |
|
45 | - { |
|
46 | - self::$_field_calculator = new Calculated_Model_Fields(); |
|
47 | - return parent::get_instance(__CLASS__); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
54 | - * |
|
55 | - * @access public |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public static function set_hooks() |
|
59 | - { |
|
60 | - self::set_hooks_both(); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
67 | - * |
|
68 | - * @access public |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public static function set_hooks_admin() |
|
72 | - { |
|
73 | - self::set_hooks_both(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - |
|
78 | - public static function set_hooks_both() |
|
79 | - { |
|
80 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
81 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
82 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
83 | - add_filter('rest_index', |
|
84 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filter_ee_metadata_into_index')); |
|
85 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * sets up hooks which only need to be included as part of REST API requests; |
|
92 | - * other requests like to the frontend or admin etc don't need them |
|
93 | - */ |
|
94 | - public static function set_hooks_rest_api() |
|
95 | - { |
|
96 | - //set hooks which account for changes made to the API |
|
97 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
98 | - EED_Core_Rest_Api::maybe_notify_of_basic_auth_removal(); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * public wrapper of _set_hooks_for_changes. |
|
105 | - * Loads all the hooks which make requests to old versions of the API |
|
106 | - * appear the same as they always did |
|
107 | - */ |
|
108 | - public static function set_hooks_for_changes() |
|
109 | - { |
|
110 | - self::_set_hooks_for_changes(); |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * If the user appears to be using WP API basic auth, tell them (via a persistent |
|
117 | - * admin notice and an email) that we're going to remove it soon, so they should |
|
118 | - * replace it with application passwords. |
|
119 | - */ |
|
120 | - public static function maybe_notify_of_basic_auth_removal() |
|
121 | - { |
|
122 | - if ( |
|
123 | - apply_filters( |
|
124 | - 'FHEE__EED_Core_Rest_Api__maybe_notify_of_basic_auth_removal__override', |
|
125 | - ! isset($_SERVER['PHP_AUTH_USER']) |
|
126 | - && ! isset($_SERVER['HTTP_AUTHORIZATION']) |
|
127 | - ) |
|
128 | - ) { |
|
129 | - //sure it's a WP API request, but they aren't using basic auth, so don't bother them |
|
130 | - return; |
|
131 | - } |
|
132 | - //ok they're using the WP API with Basic Auth |
|
133 | - $message = sprintf( |
|
134 | - __('We noticed you\'re using the WP API, which is used by the Event Espresso 4 mobile apps. Because of security and compatibility concerns, we will soon be removing our default authentication mechanism, WP API Basic Auth, from Event Espresso. It is recommended you instead install the %1$sWP Application Passwords plugin%2$s and use it with the EE4 Mobile apps. See %3$sour mobile app documentation%2$s for more information. %4$sIf you have installed the WP API Basic Auth plugin separately, or are not using the Event Espresso 4 mobile apps, you can disregard this message.%4$sThe Event Espresso Team', |
|
135 | - 'event_espresso'), |
|
136 | - '<a href="https://wordpress.org/plugins/application-passwords/">', |
|
137 | - '</a>', |
|
138 | - '<a href="https://eventespresso.com/wiki/ee4-event-apps/#authentication">', |
|
139 | - '<br/>' |
|
140 | - ); |
|
141 | - EE_Error::add_persistent_admin_notice('using_basic_auth', $message); |
|
142 | - if ( ! get_option('ee_notified_admin_on_basic_auth_removal', false)) { |
|
143 | - add_option('ee_notified_admin_on_basic_auth_removal', true); |
|
144 | - //piggy back off EE_Error::set_content_type, which sets the content type to HTML |
|
145 | - add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
146 | - //and send the message to the site admin too |
|
147 | - wp_mail(get_option('admin_email'), |
|
148 | - __('Notice of Removal of WP API Basic Auth From Event Espresso 4', 'event_espresso'), $message); |
|
149 | - remove_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Loads all the hooks which make requests to old versions of the API |
|
157 | - * appear the same as they always did |
|
158 | - */ |
|
159 | - protected static function _set_hooks_for_changes() |
|
160 | - { |
|
161 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
162 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
163 | - //ignore the base parent class |
|
164 | - if ($classname_in_namespace === 'Changes_In_Base') { |
|
165 | - continue; |
|
166 | - } |
|
167 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
168 | - if (class_exists($full_classname)) { |
|
169 | - $instance_of_class = new $full_classname; |
|
170 | - if ($instance_of_class instanceof Changes_In_Base) { |
|
171 | - $instance_of_class->set_hooks(); |
|
172 | - } |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
181 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
182 | - */ |
|
183 | - public static function register_routes() |
|
184 | - { |
|
185 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
186 | - foreach ($relative_urls as $endpoint => $routes) { |
|
187 | - foreach ($routes as $route) { |
|
188 | - $route_args = array( |
|
189 | - array( |
|
190 | - 'callback' => $route['callback'], |
|
191 | - 'methods' => $route['methods'], |
|
192 | - 'args' => isset($route['args']) ? $route['args'] : array(), |
|
193 | - ) |
|
194 | - ); |
|
195 | - if (isset($route['schema_callback'])) { |
|
196 | - $model_name = isset($route['schema_callback'][0]) |
|
197 | - ? $route['schema_callback'][0] |
|
198 | - : ''; |
|
199 | - $version = isset( $route['schema_callback'][1]) |
|
200 | - ? $route['schema_callback'][1] |
|
201 | - : ''; |
|
202 | - if (! empty($model_name) && ! empty($version)) { |
|
203 | - $route_args['schema'] = function () use ($model_name, $version) { |
|
204 | - return ModelRead::handle_schema_request( |
|
205 | - $model_name, |
|
206 | - $version |
|
207 | - ); |
|
208 | - }; |
|
209 | - } |
|
210 | - } |
|
211 | - register_rest_route( |
|
212 | - $namespace, |
|
213 | - $endpoint, |
|
214 | - $route_args |
|
215 | - ); |
|
216 | - } |
|
217 | - } |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Checks if there was a version change or something that merits invalidating the cached |
|
225 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
226 | - * next time the WP API is used |
|
227 | - */ |
|
228 | - public static function invalidate_cached_route_data_on_version_change() |
|
229 | - { |
|
230 | - if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
231 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
232 | - } |
|
233 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
234 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
235 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
244 | - */ |
|
245 | - public static function invalidate_cached_route_data() |
|
246 | - { |
|
247 | - //delete the saved EE REST API routes |
|
248 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
249 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * Gets the EE route data |
|
257 | - * |
|
258 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
259 | - * @type string|array $callback |
|
260 | - * @type string $methods |
|
261 | - * @type boolean $hidden_endpoint |
|
262 | - * } |
|
263 | - */ |
|
264 | - public static function get_ee_route_data() |
|
265 | - { |
|
266 | - $ee_routes = array(); |
|
267 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
268 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version($version, |
|
269 | - $hidden_endpoints); |
|
270 | - } |
|
271 | - return $ee_routes; |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Gets the EE route data from the wp options if it exists already, |
|
278 | - * otherwise re-generates it and saves it to the option |
|
279 | - * |
|
280 | - * @param string $version |
|
281 | - * @param boolean $hidden_endpoints |
|
282 | - * @return array |
|
283 | - */ |
|
284 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
285 | - { |
|
286 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
287 | - if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
288 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
289 | - } |
|
290 | - return $ee_routes; |
|
291 | - } |
|
292 | - |
|
293 | - |
|
294 | - |
|
295 | - /** |
|
296 | - * Saves the EE REST API route data to a wp option and returns it |
|
297 | - * |
|
298 | - * @param string $version |
|
299 | - * @param boolean $hidden_endpoints |
|
300 | - * @return mixed|null|void |
|
301 | - */ |
|
302 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
303 | - { |
|
304 | - $instance = self::instance(); |
|
305 | - $routes = apply_filters( |
|
306 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
307 | - array_replace_recursive( |
|
308 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
309 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
310 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
311 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
312 | - ) |
|
313 | - ); |
|
314 | - $option_name = self::saved_routes_option_names . $version; |
|
315 | - if (get_option($option_name)) { |
|
316 | - update_option($option_name, $routes, true); |
|
317 | - } else { |
|
318 | - add_option($option_name, $routes, null, 'no'); |
|
319 | - } |
|
320 | - return $routes; |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * Calculates all the EE routes and saves it to a wordpress option so we don't |
|
327 | - * need to calculate it on every request |
|
328 | - * |
|
329 | - * @deprecated since version 4.9.1 |
|
330 | - * @return void |
|
331 | - */ |
|
332 | - public static function save_ee_routes() |
|
333 | - { |
|
334 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
335 | - $instance = self::instance(); |
|
336 | - $routes = apply_filters( |
|
337 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
338 | - array_replace_recursive( |
|
339 | - $instance->_register_config_routes(), |
|
340 | - $instance->_register_meta_routes(), |
|
341 | - $instance->_register_model_routes(), |
|
342 | - $instance->_register_rpc_routes() |
|
343 | - ) |
|
344 | - ); |
|
345 | - update_option(self::saved_routes_option_names, $routes, true); |
|
346 | - } |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * Gets all the route information relating to EE models |
|
353 | - * |
|
354 | - * @return array @see get_ee_route_data |
|
355 | - * @deprecated since version 4.9.1 |
|
356 | - */ |
|
357 | - protected function _register_model_routes() |
|
358 | - { |
|
359 | - $model_routes = array(); |
|
360 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
361 | - $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
362 | - . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
363 | - } |
|
364 | - return $model_routes; |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Gets the route data for EE models in the specified version |
|
371 | - * |
|
372 | - * @param string $version |
|
373 | - * @param boolean $hidden_endpoint |
|
374 | - * @return array |
|
375 | - */ |
|
376 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
377 | - { |
|
378 | - $model_version_info = new Model_Version_Info($version); |
|
379 | - $models_to_register = apply_filters( |
|
380 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
381 | - $model_version_info->models_for_requested_version() |
|
382 | - ); |
|
383 | - //let's not bother having endpoints for extra metas |
|
384 | - unset($models_to_register['Extra_Meta']); |
|
385 | - unset($models_to_register['Extra_Join']); |
|
386 | - $model_routes = array(); |
|
387 | - foreach ($models_to_register as $model_name => $model_classname) { |
|
388 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
389 | - |
|
390 | - //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
391 | - if (! $model instanceof EEM_Base) { |
|
392 | - continue; |
|
393 | - } |
|
394 | - |
|
395 | - //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
396 | - $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name); |
|
397 | - $singular_model_route = $plural_model_route . '/(?P<id>\d+)'; |
|
398 | - $model_routes[$plural_model_route] = array( |
|
399 | - array( |
|
400 | - 'callback' => array( |
|
401 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
402 | - 'handle_request_get_all', |
|
403 | - ), |
|
404 | - 'methods' => WP_REST_Server::READABLE, |
|
405 | - 'hidden_endpoint' => $hidden_endpoint, |
|
406 | - 'args' => $this->_get_read_query_params($model, $version), |
|
407 | - '_links' => array( |
|
408 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
409 | - ), |
|
410 | - 'schema_callback' => array($model_name, $version) |
|
411 | - ), |
|
412 | - // array( |
|
413 | - // 'callback' => array( |
|
414 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
415 | - // 'handle_request_create_one' ), |
|
416 | - // 'methods' => WP_REST_Server::CREATABLE, |
|
417 | - // 'hidden_endpoint' => $hidden_endpoint |
|
418 | - // ) |
|
419 | - ); |
|
420 | - $model_routes[$singular_model_route] = array( |
|
421 | - array( |
|
422 | - 'callback' => array( |
|
423 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
424 | - 'handle_request_get_one', |
|
425 | - ), |
|
426 | - 'methods' => WP_REST_Server::READABLE, |
|
427 | - 'hidden_endpoint' => $hidden_endpoint, |
|
428 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
429 | - ), |
|
430 | - // array( |
|
431 | - // 'callback' => array( |
|
432 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
433 | - // 'handle_request_edit_one' ), |
|
434 | - // 'methods' => WP_REST_Server::EDITABLE, |
|
435 | - // 'hidden_endpoint' => $hidden_endpoint |
|
436 | - // ), |
|
437 | - ); |
|
438 | - //@todo: also handle DELETE for a single item |
|
439 | - foreach ($model_version_info->relation_settings($model) as $relation_name => $relation_obj) { |
|
440 | - $related_model_name_endpoint_part = ModelRead::get_related_entity_name( |
|
441 | - $relation_name, |
|
442 | - $relation_obj |
|
443 | - ); |
|
444 | - $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array( |
|
445 | - array( |
|
446 | - 'callback' => array( |
|
447 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
448 | - 'handle_request_get_related', |
|
449 | - ), |
|
450 | - 'methods' => WP_REST_Server::READABLE, |
|
451 | - 'hidden_endpoint' => $hidden_endpoint, |
|
452 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
453 | - ), |
|
454 | - // array( |
|
455 | - // 'callback' => array( |
|
456 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
457 | - // 'handle_request_create_or_update_related' ), |
|
458 | - // 'methods' => WP_REST_Server::EDITABLE, |
|
459 | - // 'hidden_endpoint' => $hidden_endpoint |
|
460 | - // ) |
|
461 | - ); |
|
462 | - //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish) |
|
463 | - } |
|
464 | - } |
|
465 | - return $model_routes; |
|
466 | - } |
|
467 | - |
|
468 | - |
|
469 | - |
|
470 | - /** |
|
471 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
472 | - * routes that don't conform to the traditional REST CRUD-style). |
|
473 | - * |
|
474 | - * @deprecated since 4.9.1 |
|
475 | - */ |
|
476 | - protected function _register_rpc_routes() |
|
477 | - { |
|
478 | - $routes = array(); |
|
479 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
480 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version($version, |
|
481 | - $hidden_endpoint); |
|
482 | - } |
|
483 | - return $routes; |
|
484 | - } |
|
485 | - |
|
486 | - |
|
487 | - |
|
488 | - /** |
|
489 | - * @param string $version |
|
490 | - * @param boolean $hidden_endpoint |
|
491 | - * @return array |
|
492 | - */ |
|
493 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
494 | - { |
|
495 | - $this_versions_routes = array(); |
|
496 | - //checkin endpoint |
|
497 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
498 | - array( |
|
499 | - 'callback' => array( |
|
500 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
501 | - 'handle_request_toggle_checkin', |
|
502 | - ), |
|
503 | - 'methods' => WP_REST_Server::CREATABLE, |
|
504 | - 'hidden_endpoint' => $hidden_endpoint, |
|
505 | - 'args' => array( |
|
506 | - 'force' => array( |
|
507 | - 'required' => false, |
|
508 | - 'default' => false, |
|
509 | - 'description' => __('Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
510 | - 'event_espresso'), |
|
511 | - ), |
|
512 | - ), |
|
513 | - ), |
|
514 | - ); |
|
515 | - return apply_filters( |
|
516 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
517 | - $this_versions_routes, |
|
518 | - $version, |
|
519 | - $hidden_endpoint |
|
520 | - ); |
|
521 | - } |
|
522 | - |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * Gets the query params that can be used when request one or many |
|
527 | - * |
|
528 | - * @param EEM_Base $model |
|
529 | - * @param string $version |
|
530 | - * @return array |
|
531 | - */ |
|
532 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
533 | - { |
|
534 | - return apply_filters( |
|
535 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
536 | - array( |
|
537 | - 'include' => array( |
|
538 | - 'required' => false, |
|
539 | - 'default' => '*', |
|
540 | - ), |
|
541 | - 'calculate' => array( |
|
542 | - 'required' => false, |
|
543 | - 'default' => '', |
|
544 | - 'enum' => self::$_field_calculator->retrieve_calculated_fields_for_model($model), |
|
545 | - ), |
|
546 | - ), |
|
547 | - $model, |
|
548 | - $version |
|
549 | - ); |
|
550 | - } |
|
551 | - |
|
552 | - |
|
553 | - |
|
554 | - /** |
|
555 | - * Gets info about reading query params that are acceptable |
|
556 | - * |
|
557 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
558 | - * @param string $version |
|
559 | - * @return array describing the args acceptable when querying this model |
|
560 | - * @throws \EE_Error |
|
561 | - */ |
|
562 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
563 | - { |
|
564 | - $default_orderby = array(); |
|
565 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
566 | - $default_orderby[$key_field->get_name()] = 'ASC'; |
|
567 | - } |
|
568 | - return array_merge( |
|
569 | - $this->_get_response_selection_query_params($model, $version), |
|
570 | - array( |
|
571 | - 'where' => array( |
|
572 | - 'required' => false, |
|
573 | - 'default' => array(), |
|
574 | - ), |
|
575 | - 'limit' => array( |
|
576 | - 'required' => false, |
|
577 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
578 | - ), |
|
579 | - 'order_by' => array( |
|
580 | - 'required' => false, |
|
581 | - 'default' => $default_orderby, |
|
582 | - ), |
|
583 | - 'group_by' => array( |
|
584 | - 'required' => false, |
|
585 | - 'default' => null, |
|
586 | - ), |
|
587 | - 'having' => array( |
|
588 | - 'required' => false, |
|
589 | - 'default' => null, |
|
590 | - ), |
|
591 | - 'caps' => array( |
|
592 | - 'required' => false, |
|
593 | - 'default' => EEM_Base::caps_read, |
|
594 | - ), |
|
595 | - ) |
|
596 | - ); |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - |
|
601 | - /** |
|
602 | - * Gets routes for the config |
|
603 | - * |
|
604 | - * @return array @see _register_model_routes |
|
605 | - * @deprecated since version 4.9.1 |
|
606 | - */ |
|
607 | - protected function _register_config_routes() |
|
608 | - { |
|
609 | - $config_routes = array(); |
|
610 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
611 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version($version, |
|
612 | - $hidden_endpoint); |
|
613 | - } |
|
614 | - return $config_routes; |
|
615 | - } |
|
616 | - |
|
617 | - |
|
618 | - |
|
619 | - /** |
|
620 | - * Gets routes for the config for the specified version |
|
621 | - * |
|
622 | - * @param string $version |
|
623 | - * @param boolean $hidden_endpoint |
|
624 | - * @return array |
|
625 | - */ |
|
626 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
627 | - { |
|
628 | - return array( |
|
629 | - 'config' => array( |
|
630 | - array( |
|
631 | - 'callback' => array( |
|
632 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
633 | - 'handle_request', |
|
634 | - ), |
|
635 | - 'methods' => WP_REST_Server::READABLE, |
|
636 | - 'hidden_endpoint' => $hidden_endpoint, |
|
637 | - ), |
|
638 | - ), |
|
639 | - 'site_info' => array( |
|
640 | - array( |
|
641 | - 'callback' => array( |
|
642 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
643 | - 'handle_request_site_info', |
|
644 | - ), |
|
645 | - 'methods' => WP_REST_Server::READABLE, |
|
646 | - 'hidden_endpoint' => $hidden_endpoint, |
|
647 | - ), |
|
648 | - ), |
|
649 | - ); |
|
650 | - } |
|
651 | - |
|
652 | - |
|
653 | - |
|
654 | - /** |
|
655 | - * Gets the meta info routes |
|
656 | - * |
|
657 | - * @return array @see _register_model_routes |
|
658 | - * @deprecated since version 4.9.1 |
|
659 | - */ |
|
660 | - protected function _register_meta_routes() |
|
661 | - { |
|
662 | - $meta_routes = array(); |
|
663 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
664 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version($version, |
|
665 | - $hidden_endpoint); |
|
666 | - } |
|
667 | - return $meta_routes; |
|
668 | - } |
|
669 | - |
|
670 | - |
|
671 | - |
|
672 | - /** |
|
673 | - * @param string $version |
|
674 | - * @param boolean $hidden_endpoint |
|
675 | - * @return array |
|
676 | - */ |
|
677 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
678 | - { |
|
679 | - return array( |
|
680 | - 'resources' => array( |
|
681 | - array( |
|
682 | - 'callback' => array( |
|
683 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
684 | - 'handle_request_models_meta', |
|
685 | - ), |
|
686 | - 'methods' => WP_REST_Server::READABLE, |
|
687 | - 'hidden_endpoint' => $hidden_endpoint, |
|
688 | - ), |
|
689 | - ), |
|
690 | - ); |
|
691 | - } |
|
692 | - |
|
693 | - |
|
694 | - |
|
695 | - /** |
|
696 | - * Tries to hide old 4.6 endpoints from the |
|
697 | - * |
|
698 | - * @param array $route_data |
|
699 | - * @return array |
|
700 | - */ |
|
701 | - public static function hide_old_endpoints($route_data) |
|
702 | - { |
|
703 | - //allow API clients to override which endpoints get hidden, in case |
|
704 | - //they want to discover particular endpoints |
|
705 | - //also, we don't have access to the request so we have to just grab it from the superglobal |
|
706 | - $force_show_ee_namespace = ltrim( |
|
707 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
708 | - '/' |
|
709 | - ); |
|
710 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
711 | - foreach ($relative_urls as $endpoint => $routes) { |
|
712 | - foreach ($routes as $route) { |
|
713 | - //by default, hide "hidden_endpoint"s, unless the request indicates |
|
714 | - //to $force_show_ee_namespace, in which case only show that one |
|
715 | - //namespace's endpoints (and hide all others) |
|
716 | - if (($route['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
717 | - || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
718 | - ) { |
|
719 | - $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($endpoint, '/'); |
|
720 | - unset($route_data[$full_route]); |
|
721 | - } |
|
722 | - } |
|
723 | - } |
|
724 | - } |
|
725 | - return $route_data; |
|
726 | - } |
|
727 | - |
|
728 | - |
|
729 | - |
|
730 | - /** |
|
731 | - * Returns an array describing which versions of core support serving requests for. |
|
732 | - * Keys are core versions' major and minor version, and values are the |
|
733 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
734 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
735 | - * the answers table entirely, in which case it would be very difficult for |
|
736 | - * it to serve 4.6-style responses. |
|
737 | - * Versions of core that are missing from this array are unknowns. |
|
738 | - * previous ver |
|
739 | - * |
|
740 | - * @return array |
|
741 | - */ |
|
742 | - public static function version_compatibilities() |
|
743 | - { |
|
744 | - return apply_filters( |
|
745 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
746 | - array( |
|
747 | - '4.8.29' => '4.8.29', |
|
748 | - '4.8.33' => '4.8.29', |
|
749 | - '4.8.34' => '4.8.29', |
|
750 | - '4.8.36' => '4.8.29', |
|
751 | - ) |
|
752 | - ); |
|
753 | - } |
|
754 | - |
|
755 | - |
|
756 | - |
|
757 | - /** |
|
758 | - * Gets the latest API version served. Eg if there |
|
759 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
760 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
761 | - * |
|
762 | - * @return string |
|
763 | - */ |
|
764 | - public static function latest_rest_api_version() |
|
765 | - { |
|
766 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
767 | - $versions_served_keys = array_keys($versions_served); |
|
768 | - return end($versions_served_keys); |
|
769 | - } |
|
770 | - |
|
771 | - |
|
772 | - |
|
773 | - /** |
|
774 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
775 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
776 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
777 | - * We also indicate whether or not this version should be put in the index or not |
|
778 | - * |
|
779 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
780 | - * are whether or not they should be hidden |
|
781 | - */ |
|
782 | - public static function versions_served() |
|
783 | - { |
|
784 | - $versions_served = array(); |
|
785 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
786 | - $lowest_compatible_version = end($possibly_served_versions); |
|
787 | - reset($possibly_served_versions); |
|
788 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
789 | - $latest_version = end($versions_served_historically); |
|
790 | - reset($versions_served_historically); |
|
791 | - //for each version of core we have ever served: |
|
792 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
793 | - //if it's not above the current core version, and it's compatible with the current version of core |
|
794 | - if ($key_versioned_endpoint == $latest_version) { |
|
795 | - //don't hide the latest version in the index |
|
796 | - $versions_served[$key_versioned_endpoint] = false; |
|
797 | - } else if ( |
|
798 | - $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
799 | - && $key_versioned_endpoint >= $lowest_compatible_version |
|
800 | - ) { |
|
801 | - //include, but hide, previous versions which are still supported |
|
802 | - $versions_served[$key_versioned_endpoint] = true; |
|
803 | - } elseif ( |
|
804 | - apply_filters( |
|
805 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
806 | - false, |
|
807 | - $possibly_served_versions |
|
808 | - ) |
|
809 | - ) { |
|
810 | - //if a version is no longer supported, don't include it in index or list of versions served |
|
811 | - $versions_served[$key_versioned_endpoint] = true; |
|
812 | - } |
|
813 | - } |
|
814 | - return $versions_served; |
|
815 | - } |
|
816 | - |
|
817 | - |
|
818 | - |
|
819 | - /** |
|
820 | - * Gets the major and minor version of EE core's version string |
|
821 | - * |
|
822 | - * @return string |
|
823 | - */ |
|
824 | - public static function core_version() |
|
825 | - { |
|
826 | - return apply_filters('FHEE__EED_Core_REST_API__core_version', |
|
827 | - implode('.', array_slice(explode('.', espresso_version()), 0, 3))); |
|
828 | - } |
|
829 | - |
|
830 | - |
|
831 | - |
|
832 | - /** |
|
833 | - * Gets the default limit that should be used when querying for resources |
|
834 | - * |
|
835 | - * @return int |
|
836 | - */ |
|
837 | - public static function get_default_query_limit() |
|
838 | - { |
|
839 | - //we actually don't use a const because we want folks to always use |
|
840 | - //this method, not the const directly |
|
841 | - return apply_filters( |
|
842 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
843 | - 50 |
|
844 | - ); |
|
845 | - } |
|
846 | - |
|
847 | - |
|
848 | - |
|
849 | - /** |
|
850 | - * run - initial module setup |
|
851 | - * |
|
852 | - * @access public |
|
853 | - * @param WP $WP |
|
854 | - * @return void |
|
855 | - */ |
|
856 | - public function run($WP) |
|
857 | - { |
|
858 | - } |
|
23 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
24 | + |
|
25 | + const saved_routes_option_names = 'ee_core_routes'; |
|
26 | + |
|
27 | + /** |
|
28 | + * string used in _links response bodies to make them globally unique. |
|
29 | + * |
|
30 | + * @see http://v2.wp-api.org/extending/linking/ |
|
31 | + */ |
|
32 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var Calculated_Model_Fields |
|
36 | + */ |
|
37 | + protected static $_field_calculator = null; |
|
38 | + |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * @return EED_Core_Rest_Api |
|
43 | + */ |
|
44 | + public static function instance() |
|
45 | + { |
|
46 | + self::$_field_calculator = new Calculated_Model_Fields(); |
|
47 | + return parent::get_instance(__CLASS__); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
54 | + * |
|
55 | + * @access public |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public static function set_hooks() |
|
59 | + { |
|
60 | + self::set_hooks_both(); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
67 | + * |
|
68 | + * @access public |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public static function set_hooks_admin() |
|
72 | + { |
|
73 | + self::set_hooks_both(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + |
|
78 | + public static function set_hooks_both() |
|
79 | + { |
|
80 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
81 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
82 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
83 | + add_filter('rest_index', |
|
84 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filter_ee_metadata_into_index')); |
|
85 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * sets up hooks which only need to be included as part of REST API requests; |
|
92 | + * other requests like to the frontend or admin etc don't need them |
|
93 | + */ |
|
94 | + public static function set_hooks_rest_api() |
|
95 | + { |
|
96 | + //set hooks which account for changes made to the API |
|
97 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
98 | + EED_Core_Rest_Api::maybe_notify_of_basic_auth_removal(); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * public wrapper of _set_hooks_for_changes. |
|
105 | + * Loads all the hooks which make requests to old versions of the API |
|
106 | + * appear the same as they always did |
|
107 | + */ |
|
108 | + public static function set_hooks_for_changes() |
|
109 | + { |
|
110 | + self::_set_hooks_for_changes(); |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * If the user appears to be using WP API basic auth, tell them (via a persistent |
|
117 | + * admin notice and an email) that we're going to remove it soon, so they should |
|
118 | + * replace it with application passwords. |
|
119 | + */ |
|
120 | + public static function maybe_notify_of_basic_auth_removal() |
|
121 | + { |
|
122 | + if ( |
|
123 | + apply_filters( |
|
124 | + 'FHEE__EED_Core_Rest_Api__maybe_notify_of_basic_auth_removal__override', |
|
125 | + ! isset($_SERVER['PHP_AUTH_USER']) |
|
126 | + && ! isset($_SERVER['HTTP_AUTHORIZATION']) |
|
127 | + ) |
|
128 | + ) { |
|
129 | + //sure it's a WP API request, but they aren't using basic auth, so don't bother them |
|
130 | + return; |
|
131 | + } |
|
132 | + //ok they're using the WP API with Basic Auth |
|
133 | + $message = sprintf( |
|
134 | + __('We noticed you\'re using the WP API, which is used by the Event Espresso 4 mobile apps. Because of security and compatibility concerns, we will soon be removing our default authentication mechanism, WP API Basic Auth, from Event Espresso. It is recommended you instead install the %1$sWP Application Passwords plugin%2$s and use it with the EE4 Mobile apps. See %3$sour mobile app documentation%2$s for more information. %4$sIf you have installed the WP API Basic Auth plugin separately, or are not using the Event Espresso 4 mobile apps, you can disregard this message.%4$sThe Event Espresso Team', |
|
135 | + 'event_espresso'), |
|
136 | + '<a href="https://wordpress.org/plugins/application-passwords/">', |
|
137 | + '</a>', |
|
138 | + '<a href="https://eventespresso.com/wiki/ee4-event-apps/#authentication">', |
|
139 | + '<br/>' |
|
140 | + ); |
|
141 | + EE_Error::add_persistent_admin_notice('using_basic_auth', $message); |
|
142 | + if ( ! get_option('ee_notified_admin_on_basic_auth_removal', false)) { |
|
143 | + add_option('ee_notified_admin_on_basic_auth_removal', true); |
|
144 | + //piggy back off EE_Error::set_content_type, which sets the content type to HTML |
|
145 | + add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
146 | + //and send the message to the site admin too |
|
147 | + wp_mail(get_option('admin_email'), |
|
148 | + __('Notice of Removal of WP API Basic Auth From Event Espresso 4', 'event_espresso'), $message); |
|
149 | + remove_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Loads all the hooks which make requests to old versions of the API |
|
157 | + * appear the same as they always did |
|
158 | + */ |
|
159 | + protected static function _set_hooks_for_changes() |
|
160 | + { |
|
161 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
162 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
163 | + //ignore the base parent class |
|
164 | + if ($classname_in_namespace === 'Changes_In_Base') { |
|
165 | + continue; |
|
166 | + } |
|
167 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
168 | + if (class_exists($full_classname)) { |
|
169 | + $instance_of_class = new $full_classname; |
|
170 | + if ($instance_of_class instanceof Changes_In_Base) { |
|
171 | + $instance_of_class->set_hooks(); |
|
172 | + } |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
181 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
182 | + */ |
|
183 | + public static function register_routes() |
|
184 | + { |
|
185 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
186 | + foreach ($relative_urls as $endpoint => $routes) { |
|
187 | + foreach ($routes as $route) { |
|
188 | + $route_args = array( |
|
189 | + array( |
|
190 | + 'callback' => $route['callback'], |
|
191 | + 'methods' => $route['methods'], |
|
192 | + 'args' => isset($route['args']) ? $route['args'] : array(), |
|
193 | + ) |
|
194 | + ); |
|
195 | + if (isset($route['schema_callback'])) { |
|
196 | + $model_name = isset($route['schema_callback'][0]) |
|
197 | + ? $route['schema_callback'][0] |
|
198 | + : ''; |
|
199 | + $version = isset( $route['schema_callback'][1]) |
|
200 | + ? $route['schema_callback'][1] |
|
201 | + : ''; |
|
202 | + if (! empty($model_name) && ! empty($version)) { |
|
203 | + $route_args['schema'] = function () use ($model_name, $version) { |
|
204 | + return ModelRead::handle_schema_request( |
|
205 | + $model_name, |
|
206 | + $version |
|
207 | + ); |
|
208 | + }; |
|
209 | + } |
|
210 | + } |
|
211 | + register_rest_route( |
|
212 | + $namespace, |
|
213 | + $endpoint, |
|
214 | + $route_args |
|
215 | + ); |
|
216 | + } |
|
217 | + } |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Checks if there was a version change or something that merits invalidating the cached |
|
225 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
226 | + * next time the WP API is used |
|
227 | + */ |
|
228 | + public static function invalidate_cached_route_data_on_version_change() |
|
229 | + { |
|
230 | + if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
231 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
232 | + } |
|
233 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
234 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
235 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
244 | + */ |
|
245 | + public static function invalidate_cached_route_data() |
|
246 | + { |
|
247 | + //delete the saved EE REST API routes |
|
248 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
249 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * Gets the EE route data |
|
257 | + * |
|
258 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
259 | + * @type string|array $callback |
|
260 | + * @type string $methods |
|
261 | + * @type boolean $hidden_endpoint |
|
262 | + * } |
|
263 | + */ |
|
264 | + public static function get_ee_route_data() |
|
265 | + { |
|
266 | + $ee_routes = array(); |
|
267 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
268 | + $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version($version, |
|
269 | + $hidden_endpoints); |
|
270 | + } |
|
271 | + return $ee_routes; |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Gets the EE route data from the wp options if it exists already, |
|
278 | + * otherwise re-generates it and saves it to the option |
|
279 | + * |
|
280 | + * @param string $version |
|
281 | + * @param boolean $hidden_endpoints |
|
282 | + * @return array |
|
283 | + */ |
|
284 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
285 | + { |
|
286 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
287 | + if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
288 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
289 | + } |
|
290 | + return $ee_routes; |
|
291 | + } |
|
292 | + |
|
293 | + |
|
294 | + |
|
295 | + /** |
|
296 | + * Saves the EE REST API route data to a wp option and returns it |
|
297 | + * |
|
298 | + * @param string $version |
|
299 | + * @param boolean $hidden_endpoints |
|
300 | + * @return mixed|null|void |
|
301 | + */ |
|
302 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
303 | + { |
|
304 | + $instance = self::instance(); |
|
305 | + $routes = apply_filters( |
|
306 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
307 | + array_replace_recursive( |
|
308 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
309 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
310 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
311 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
312 | + ) |
|
313 | + ); |
|
314 | + $option_name = self::saved_routes_option_names . $version; |
|
315 | + if (get_option($option_name)) { |
|
316 | + update_option($option_name, $routes, true); |
|
317 | + } else { |
|
318 | + add_option($option_name, $routes, null, 'no'); |
|
319 | + } |
|
320 | + return $routes; |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * Calculates all the EE routes and saves it to a wordpress option so we don't |
|
327 | + * need to calculate it on every request |
|
328 | + * |
|
329 | + * @deprecated since version 4.9.1 |
|
330 | + * @return void |
|
331 | + */ |
|
332 | + public static function save_ee_routes() |
|
333 | + { |
|
334 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
335 | + $instance = self::instance(); |
|
336 | + $routes = apply_filters( |
|
337 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
338 | + array_replace_recursive( |
|
339 | + $instance->_register_config_routes(), |
|
340 | + $instance->_register_meta_routes(), |
|
341 | + $instance->_register_model_routes(), |
|
342 | + $instance->_register_rpc_routes() |
|
343 | + ) |
|
344 | + ); |
|
345 | + update_option(self::saved_routes_option_names, $routes, true); |
|
346 | + } |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * Gets all the route information relating to EE models |
|
353 | + * |
|
354 | + * @return array @see get_ee_route_data |
|
355 | + * @deprecated since version 4.9.1 |
|
356 | + */ |
|
357 | + protected function _register_model_routes() |
|
358 | + { |
|
359 | + $model_routes = array(); |
|
360 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
361 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
362 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
363 | + } |
|
364 | + return $model_routes; |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Gets the route data for EE models in the specified version |
|
371 | + * |
|
372 | + * @param string $version |
|
373 | + * @param boolean $hidden_endpoint |
|
374 | + * @return array |
|
375 | + */ |
|
376 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
377 | + { |
|
378 | + $model_version_info = new Model_Version_Info($version); |
|
379 | + $models_to_register = apply_filters( |
|
380 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
381 | + $model_version_info->models_for_requested_version() |
|
382 | + ); |
|
383 | + //let's not bother having endpoints for extra metas |
|
384 | + unset($models_to_register['Extra_Meta']); |
|
385 | + unset($models_to_register['Extra_Join']); |
|
386 | + $model_routes = array(); |
|
387 | + foreach ($models_to_register as $model_name => $model_classname) { |
|
388 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
389 | + |
|
390 | + //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
391 | + if (! $model instanceof EEM_Base) { |
|
392 | + continue; |
|
393 | + } |
|
394 | + |
|
395 | + //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
396 | + $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name); |
|
397 | + $singular_model_route = $plural_model_route . '/(?P<id>\d+)'; |
|
398 | + $model_routes[$plural_model_route] = array( |
|
399 | + array( |
|
400 | + 'callback' => array( |
|
401 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
402 | + 'handle_request_get_all', |
|
403 | + ), |
|
404 | + 'methods' => WP_REST_Server::READABLE, |
|
405 | + 'hidden_endpoint' => $hidden_endpoint, |
|
406 | + 'args' => $this->_get_read_query_params($model, $version), |
|
407 | + '_links' => array( |
|
408 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
409 | + ), |
|
410 | + 'schema_callback' => array($model_name, $version) |
|
411 | + ), |
|
412 | + // array( |
|
413 | + // 'callback' => array( |
|
414 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
415 | + // 'handle_request_create_one' ), |
|
416 | + // 'methods' => WP_REST_Server::CREATABLE, |
|
417 | + // 'hidden_endpoint' => $hidden_endpoint |
|
418 | + // ) |
|
419 | + ); |
|
420 | + $model_routes[$singular_model_route] = array( |
|
421 | + array( |
|
422 | + 'callback' => array( |
|
423 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
424 | + 'handle_request_get_one', |
|
425 | + ), |
|
426 | + 'methods' => WP_REST_Server::READABLE, |
|
427 | + 'hidden_endpoint' => $hidden_endpoint, |
|
428 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
429 | + ), |
|
430 | + // array( |
|
431 | + // 'callback' => array( |
|
432 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
433 | + // 'handle_request_edit_one' ), |
|
434 | + // 'methods' => WP_REST_Server::EDITABLE, |
|
435 | + // 'hidden_endpoint' => $hidden_endpoint |
|
436 | + // ), |
|
437 | + ); |
|
438 | + //@todo: also handle DELETE for a single item |
|
439 | + foreach ($model_version_info->relation_settings($model) as $relation_name => $relation_obj) { |
|
440 | + $related_model_name_endpoint_part = ModelRead::get_related_entity_name( |
|
441 | + $relation_name, |
|
442 | + $relation_obj |
|
443 | + ); |
|
444 | + $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array( |
|
445 | + array( |
|
446 | + 'callback' => array( |
|
447 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
448 | + 'handle_request_get_related', |
|
449 | + ), |
|
450 | + 'methods' => WP_REST_Server::READABLE, |
|
451 | + 'hidden_endpoint' => $hidden_endpoint, |
|
452 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
453 | + ), |
|
454 | + // array( |
|
455 | + // 'callback' => array( |
|
456 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
457 | + // 'handle_request_create_or_update_related' ), |
|
458 | + // 'methods' => WP_REST_Server::EDITABLE, |
|
459 | + // 'hidden_endpoint' => $hidden_endpoint |
|
460 | + // ) |
|
461 | + ); |
|
462 | + //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish) |
|
463 | + } |
|
464 | + } |
|
465 | + return $model_routes; |
|
466 | + } |
|
467 | + |
|
468 | + |
|
469 | + |
|
470 | + /** |
|
471 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
472 | + * routes that don't conform to the traditional REST CRUD-style). |
|
473 | + * |
|
474 | + * @deprecated since 4.9.1 |
|
475 | + */ |
|
476 | + protected function _register_rpc_routes() |
|
477 | + { |
|
478 | + $routes = array(); |
|
479 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
480 | + $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version($version, |
|
481 | + $hidden_endpoint); |
|
482 | + } |
|
483 | + return $routes; |
|
484 | + } |
|
485 | + |
|
486 | + |
|
487 | + |
|
488 | + /** |
|
489 | + * @param string $version |
|
490 | + * @param boolean $hidden_endpoint |
|
491 | + * @return array |
|
492 | + */ |
|
493 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
494 | + { |
|
495 | + $this_versions_routes = array(); |
|
496 | + //checkin endpoint |
|
497 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
498 | + array( |
|
499 | + 'callback' => array( |
|
500 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
501 | + 'handle_request_toggle_checkin', |
|
502 | + ), |
|
503 | + 'methods' => WP_REST_Server::CREATABLE, |
|
504 | + 'hidden_endpoint' => $hidden_endpoint, |
|
505 | + 'args' => array( |
|
506 | + 'force' => array( |
|
507 | + 'required' => false, |
|
508 | + 'default' => false, |
|
509 | + 'description' => __('Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
510 | + 'event_espresso'), |
|
511 | + ), |
|
512 | + ), |
|
513 | + ), |
|
514 | + ); |
|
515 | + return apply_filters( |
|
516 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
517 | + $this_versions_routes, |
|
518 | + $version, |
|
519 | + $hidden_endpoint |
|
520 | + ); |
|
521 | + } |
|
522 | + |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * Gets the query params that can be used when request one or many |
|
527 | + * |
|
528 | + * @param EEM_Base $model |
|
529 | + * @param string $version |
|
530 | + * @return array |
|
531 | + */ |
|
532 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
533 | + { |
|
534 | + return apply_filters( |
|
535 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
536 | + array( |
|
537 | + 'include' => array( |
|
538 | + 'required' => false, |
|
539 | + 'default' => '*', |
|
540 | + ), |
|
541 | + 'calculate' => array( |
|
542 | + 'required' => false, |
|
543 | + 'default' => '', |
|
544 | + 'enum' => self::$_field_calculator->retrieve_calculated_fields_for_model($model), |
|
545 | + ), |
|
546 | + ), |
|
547 | + $model, |
|
548 | + $version |
|
549 | + ); |
|
550 | + } |
|
551 | + |
|
552 | + |
|
553 | + |
|
554 | + /** |
|
555 | + * Gets info about reading query params that are acceptable |
|
556 | + * |
|
557 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
558 | + * @param string $version |
|
559 | + * @return array describing the args acceptable when querying this model |
|
560 | + * @throws \EE_Error |
|
561 | + */ |
|
562 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
563 | + { |
|
564 | + $default_orderby = array(); |
|
565 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
566 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
567 | + } |
|
568 | + return array_merge( |
|
569 | + $this->_get_response_selection_query_params($model, $version), |
|
570 | + array( |
|
571 | + 'where' => array( |
|
572 | + 'required' => false, |
|
573 | + 'default' => array(), |
|
574 | + ), |
|
575 | + 'limit' => array( |
|
576 | + 'required' => false, |
|
577 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
578 | + ), |
|
579 | + 'order_by' => array( |
|
580 | + 'required' => false, |
|
581 | + 'default' => $default_orderby, |
|
582 | + ), |
|
583 | + 'group_by' => array( |
|
584 | + 'required' => false, |
|
585 | + 'default' => null, |
|
586 | + ), |
|
587 | + 'having' => array( |
|
588 | + 'required' => false, |
|
589 | + 'default' => null, |
|
590 | + ), |
|
591 | + 'caps' => array( |
|
592 | + 'required' => false, |
|
593 | + 'default' => EEM_Base::caps_read, |
|
594 | + ), |
|
595 | + ) |
|
596 | + ); |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + |
|
601 | + /** |
|
602 | + * Gets routes for the config |
|
603 | + * |
|
604 | + * @return array @see _register_model_routes |
|
605 | + * @deprecated since version 4.9.1 |
|
606 | + */ |
|
607 | + protected function _register_config_routes() |
|
608 | + { |
|
609 | + $config_routes = array(); |
|
610 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
611 | + $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version($version, |
|
612 | + $hidden_endpoint); |
|
613 | + } |
|
614 | + return $config_routes; |
|
615 | + } |
|
616 | + |
|
617 | + |
|
618 | + |
|
619 | + /** |
|
620 | + * Gets routes for the config for the specified version |
|
621 | + * |
|
622 | + * @param string $version |
|
623 | + * @param boolean $hidden_endpoint |
|
624 | + * @return array |
|
625 | + */ |
|
626 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
627 | + { |
|
628 | + return array( |
|
629 | + 'config' => array( |
|
630 | + array( |
|
631 | + 'callback' => array( |
|
632 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
633 | + 'handle_request', |
|
634 | + ), |
|
635 | + 'methods' => WP_REST_Server::READABLE, |
|
636 | + 'hidden_endpoint' => $hidden_endpoint, |
|
637 | + ), |
|
638 | + ), |
|
639 | + 'site_info' => array( |
|
640 | + array( |
|
641 | + 'callback' => array( |
|
642 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
643 | + 'handle_request_site_info', |
|
644 | + ), |
|
645 | + 'methods' => WP_REST_Server::READABLE, |
|
646 | + 'hidden_endpoint' => $hidden_endpoint, |
|
647 | + ), |
|
648 | + ), |
|
649 | + ); |
|
650 | + } |
|
651 | + |
|
652 | + |
|
653 | + |
|
654 | + /** |
|
655 | + * Gets the meta info routes |
|
656 | + * |
|
657 | + * @return array @see _register_model_routes |
|
658 | + * @deprecated since version 4.9.1 |
|
659 | + */ |
|
660 | + protected function _register_meta_routes() |
|
661 | + { |
|
662 | + $meta_routes = array(); |
|
663 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
664 | + $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version($version, |
|
665 | + $hidden_endpoint); |
|
666 | + } |
|
667 | + return $meta_routes; |
|
668 | + } |
|
669 | + |
|
670 | + |
|
671 | + |
|
672 | + /** |
|
673 | + * @param string $version |
|
674 | + * @param boolean $hidden_endpoint |
|
675 | + * @return array |
|
676 | + */ |
|
677 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
678 | + { |
|
679 | + return array( |
|
680 | + 'resources' => array( |
|
681 | + array( |
|
682 | + 'callback' => array( |
|
683 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
684 | + 'handle_request_models_meta', |
|
685 | + ), |
|
686 | + 'methods' => WP_REST_Server::READABLE, |
|
687 | + 'hidden_endpoint' => $hidden_endpoint, |
|
688 | + ), |
|
689 | + ), |
|
690 | + ); |
|
691 | + } |
|
692 | + |
|
693 | + |
|
694 | + |
|
695 | + /** |
|
696 | + * Tries to hide old 4.6 endpoints from the |
|
697 | + * |
|
698 | + * @param array $route_data |
|
699 | + * @return array |
|
700 | + */ |
|
701 | + public static function hide_old_endpoints($route_data) |
|
702 | + { |
|
703 | + //allow API clients to override which endpoints get hidden, in case |
|
704 | + //they want to discover particular endpoints |
|
705 | + //also, we don't have access to the request so we have to just grab it from the superglobal |
|
706 | + $force_show_ee_namespace = ltrim( |
|
707 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
708 | + '/' |
|
709 | + ); |
|
710 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
711 | + foreach ($relative_urls as $endpoint => $routes) { |
|
712 | + foreach ($routes as $route) { |
|
713 | + //by default, hide "hidden_endpoint"s, unless the request indicates |
|
714 | + //to $force_show_ee_namespace, in which case only show that one |
|
715 | + //namespace's endpoints (and hide all others) |
|
716 | + if (($route['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
717 | + || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
718 | + ) { |
|
719 | + $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($endpoint, '/'); |
|
720 | + unset($route_data[$full_route]); |
|
721 | + } |
|
722 | + } |
|
723 | + } |
|
724 | + } |
|
725 | + return $route_data; |
|
726 | + } |
|
727 | + |
|
728 | + |
|
729 | + |
|
730 | + /** |
|
731 | + * Returns an array describing which versions of core support serving requests for. |
|
732 | + * Keys are core versions' major and minor version, and values are the |
|
733 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
734 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
735 | + * the answers table entirely, in which case it would be very difficult for |
|
736 | + * it to serve 4.6-style responses. |
|
737 | + * Versions of core that are missing from this array are unknowns. |
|
738 | + * previous ver |
|
739 | + * |
|
740 | + * @return array |
|
741 | + */ |
|
742 | + public static function version_compatibilities() |
|
743 | + { |
|
744 | + return apply_filters( |
|
745 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
746 | + array( |
|
747 | + '4.8.29' => '4.8.29', |
|
748 | + '4.8.33' => '4.8.29', |
|
749 | + '4.8.34' => '4.8.29', |
|
750 | + '4.8.36' => '4.8.29', |
|
751 | + ) |
|
752 | + ); |
|
753 | + } |
|
754 | + |
|
755 | + |
|
756 | + |
|
757 | + /** |
|
758 | + * Gets the latest API version served. Eg if there |
|
759 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
760 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
761 | + * |
|
762 | + * @return string |
|
763 | + */ |
|
764 | + public static function latest_rest_api_version() |
|
765 | + { |
|
766 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
767 | + $versions_served_keys = array_keys($versions_served); |
|
768 | + return end($versions_served_keys); |
|
769 | + } |
|
770 | + |
|
771 | + |
|
772 | + |
|
773 | + /** |
|
774 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
775 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
776 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
777 | + * We also indicate whether or not this version should be put in the index or not |
|
778 | + * |
|
779 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
780 | + * are whether or not they should be hidden |
|
781 | + */ |
|
782 | + public static function versions_served() |
|
783 | + { |
|
784 | + $versions_served = array(); |
|
785 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
786 | + $lowest_compatible_version = end($possibly_served_versions); |
|
787 | + reset($possibly_served_versions); |
|
788 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
789 | + $latest_version = end($versions_served_historically); |
|
790 | + reset($versions_served_historically); |
|
791 | + //for each version of core we have ever served: |
|
792 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
793 | + //if it's not above the current core version, and it's compatible with the current version of core |
|
794 | + if ($key_versioned_endpoint == $latest_version) { |
|
795 | + //don't hide the latest version in the index |
|
796 | + $versions_served[$key_versioned_endpoint] = false; |
|
797 | + } else if ( |
|
798 | + $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
799 | + && $key_versioned_endpoint >= $lowest_compatible_version |
|
800 | + ) { |
|
801 | + //include, but hide, previous versions which are still supported |
|
802 | + $versions_served[$key_versioned_endpoint] = true; |
|
803 | + } elseif ( |
|
804 | + apply_filters( |
|
805 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
806 | + false, |
|
807 | + $possibly_served_versions |
|
808 | + ) |
|
809 | + ) { |
|
810 | + //if a version is no longer supported, don't include it in index or list of versions served |
|
811 | + $versions_served[$key_versioned_endpoint] = true; |
|
812 | + } |
|
813 | + } |
|
814 | + return $versions_served; |
|
815 | + } |
|
816 | + |
|
817 | + |
|
818 | + |
|
819 | + /** |
|
820 | + * Gets the major and minor version of EE core's version string |
|
821 | + * |
|
822 | + * @return string |
|
823 | + */ |
|
824 | + public static function core_version() |
|
825 | + { |
|
826 | + return apply_filters('FHEE__EED_Core_REST_API__core_version', |
|
827 | + implode('.', array_slice(explode('.', espresso_version()), 0, 3))); |
|
828 | + } |
|
829 | + |
|
830 | + |
|
831 | + |
|
832 | + /** |
|
833 | + * Gets the default limit that should be used when querying for resources |
|
834 | + * |
|
835 | + * @return int |
|
836 | + */ |
|
837 | + public static function get_default_query_limit() |
|
838 | + { |
|
839 | + //we actually don't use a const because we want folks to always use |
|
840 | + //this method, not the const directly |
|
841 | + return apply_filters( |
|
842 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
843 | + 50 |
|
844 | + ); |
|
845 | + } |
|
846 | + |
|
847 | + |
|
848 | + |
|
849 | + /** |
|
850 | + * run - initial module setup |
|
851 | + * |
|
852 | + * @access public |
|
853 | + * @param WP $WP |
|
854 | + * @return void |
|
855 | + */ |
|
856 | + public function run($WP) |
|
857 | + { |
|
858 | + } |
|
859 | 859 | |
860 | 860 | |
861 | 861 |
@@ -158,13 +158,13 @@ discard block |
||
158 | 158 | */ |
159 | 159 | protected static function _set_hooks_for_changes() |
160 | 160 | { |
161 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
161 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES.'rest_api'.DS.'changes'), false); |
|
162 | 162 | foreach ($folder_contents as $classname_in_namespace => $filepath) { |
163 | 163 | //ignore the base parent class |
164 | 164 | if ($classname_in_namespace === 'Changes_In_Base') { |
165 | 165 | continue; |
166 | 166 | } |
167 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
167 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\'.$classname_in_namespace; |
|
168 | 168 | if (class_exists($full_classname)) { |
169 | 169 | $instance_of_class = new $full_classname; |
170 | 170 | if ($instance_of_class instanceof Changes_In_Base) { |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | $model_name = isset($route['schema_callback'][0]) |
197 | 197 | ? $route['schema_callback'][0] |
198 | 198 | : ''; |
199 | - $version = isset( $route['schema_callback'][1]) |
|
199 | + $version = isset($route['schema_callback'][1]) |
|
200 | 200 | ? $route['schema_callback'][1] |
201 | 201 | : ''; |
202 | - if (! empty($model_name) && ! empty($version)) { |
|
203 | - $route_args['schema'] = function () use ($model_name, $version) { |
|
202 | + if ( ! empty($model_name) && ! empty($version)) { |
|
203 | + $route_args['schema'] = function() use ($model_name, $version) { |
|
204 | 204 | return ModelRead::handle_schema_request( |
205 | 205 | $model_name, |
206 | 206 | $version |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | { |
247 | 247 | //delete the saved EE REST API routes |
248 | 248 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
249 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
249 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names.$version); |
|
250 | 250 | } |
251 | 251 | } |
252 | 252 | |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | { |
266 | 266 | $ee_routes = array(); |
267 | 267 | foreach (self::versions_served() as $version => $hidden_endpoints) { |
268 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version($version, |
|
268 | + $ee_routes[self::ee_api_namespace.$version] = self::_get_ee_route_data_for_version($version, |
|
269 | 269 | $hidden_endpoints); |
270 | 270 | } |
271 | 271 | return $ee_routes; |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | */ |
284 | 284 | protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
285 | 285 | { |
286 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
286 | + $ee_routes = get_option(self::saved_routes_option_names.$version, null); |
|
287 | 287 | if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
288 | 288 | $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
289 | 289 | } |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
312 | 312 | ) |
313 | 313 | ); |
314 | - $option_name = self::saved_routes_option_names . $version; |
|
314 | + $option_name = self::saved_routes_option_names.$version; |
|
315 | 315 | if (get_option($option_name)) { |
316 | 316 | update_option($option_name, $routes, true); |
317 | 317 | } else { |
@@ -388,13 +388,13 @@ discard block |
||
388 | 388 | $model = \EE_Registry::instance()->load_model($model_name); |
389 | 389 | |
390 | 390 | //if this isn't a valid model then let's skip iterate to the next item in the loop. |
391 | - if (! $model instanceof EEM_Base) { |
|
391 | + if ( ! $model instanceof EEM_Base) { |
|
392 | 392 | continue; |
393 | 393 | } |
394 | 394 | |
395 | 395 | //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
396 | 396 | $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name); |
397 | - $singular_model_route = $plural_model_route . '/(?P<id>\d+)'; |
|
397 | + $singular_model_route = $plural_model_route.'/(?P<id>\d+)'; |
|
398 | 398 | $model_routes[$plural_model_route] = array( |
399 | 399 | array( |
400 | 400 | 'callback' => array( |
@@ -405,7 +405,7 @@ discard block |
||
405 | 405 | 'hidden_endpoint' => $hidden_endpoint, |
406 | 406 | 'args' => $this->_get_read_query_params($model, $version), |
407 | 407 | '_links' => array( |
408 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
408 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace.$version.$singular_model_route), |
|
409 | 409 | ), |
410 | 410 | 'schema_callback' => array($model_name, $version) |
411 | 411 | ), |
@@ -441,7 +441,7 @@ discard block |
||
441 | 441 | $relation_name, |
442 | 442 | $relation_obj |
443 | 443 | ); |
444 | - $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array( |
|
444 | + $model_routes[$singular_model_route.'/'.$related_model_name_endpoint_part] = array( |
|
445 | 445 | array( |
446 | 446 | 'callback' => array( |
447 | 447 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -477,7 +477,7 @@ discard block |
||
477 | 477 | { |
478 | 478 | $routes = array(); |
479 | 479 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
480 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version($version, |
|
480 | + $routes[self::ee_api_namespace.$version] = $this->_get_rpc_route_data_for_version($version, |
|
481 | 481 | $hidden_endpoint); |
482 | 482 | } |
483 | 483 | return $routes; |
@@ -608,7 +608,7 @@ discard block |
||
608 | 608 | { |
609 | 609 | $config_routes = array(); |
610 | 610 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
611 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version($version, |
|
611 | + $config_routes[self::ee_api_namespace.$version] = $this->_get_config_route_data_for_version($version, |
|
612 | 612 | $hidden_endpoint); |
613 | 613 | } |
614 | 614 | return $config_routes; |
@@ -661,7 +661,7 @@ discard block |
||
661 | 661 | { |
662 | 662 | $meta_routes = array(); |
663 | 663 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
664 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version($version, |
|
664 | + $meta_routes[self::ee_api_namespace.$version] = $this->_get_meta_route_data_for_version($version, |
|
665 | 665 | $hidden_endpoint); |
666 | 666 | } |
667 | 667 | return $meta_routes; |
@@ -716,7 +716,7 @@ discard block |
||
716 | 716 | if (($route['hidden_endpoint'] && $force_show_ee_namespace === '') |
717 | 717 | || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
718 | 718 | ) { |
719 | - $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($endpoint, '/'); |
|
719 | + $full_route = '/'.ltrim($namespace, '/').'/'.ltrim($endpoint, '/'); |
|
720 | 720 | unset($route_data[$full_route]); |
721 | 721 | } |
722 | 722 | } |
@@ -89,12 +89,12 @@ discard block |
||
89 | 89 | |
90 | 90 | |
91 | 91 | |
92 | - /** |
|
93 | - * Model_Version_Info constructor. |
|
94 | - * |
|
95 | - * @param string $requested_version |
|
96 | - */ |
|
97 | - public function __construct( $requested_version ) { |
|
92 | + /** |
|
93 | + * Model_Version_Info constructor. |
|
94 | + * |
|
95 | + * @param string $requested_version |
|
96 | + */ |
|
97 | + public function __construct( $requested_version ) { |
|
98 | 98 | $this->_requested_version = $requested_version; |
99 | 99 | $this->_model_changes = array( |
100 | 100 | '4.8.29' => array( |
@@ -209,10 +209,10 @@ discard block |
||
209 | 209 | } |
210 | 210 | } |
211 | 211 | $this->_cached_models_for_requested_version = apply_filters( |
212 | - 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
213 | - $all_models_in_current_version, |
|
214 | - $this |
|
215 | - ); |
|
212 | + 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
213 | + $all_models_in_current_version, |
|
214 | + $this |
|
215 | + ); |
|
216 | 216 | } |
217 | 217 | return $this->_cached_models_for_requested_version; |
218 | 218 | } |
@@ -1,8 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | -if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
5 | - exit( 'No direct script access allowed' ); |
|
4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | /** |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * |
95 | 95 | * @param string $requested_version |
96 | 96 | */ |
97 | - public function __construct( $requested_version ) { |
|
97 | + public function __construct($requested_version) { |
|
98 | 98 | $this->_requested_version = $requested_version; |
99 | 99 | $this->_model_changes = array( |
100 | 100 | '4.8.29' => array( |
@@ -116,13 +116,13 @@ discard block |
||
116 | 116 | 'table_column' => 'N/A', |
117 | 117 | 'always_available' => true, |
118 | 118 | ); |
119 | - foreach( $this->_resource_changes as $version => $model_classnames ) { |
|
120 | - foreach( $model_classnames as $model_classname => $extra_fields ) { |
|
121 | - foreach( $extra_fields as $fieldname => $field_data ) { |
|
122 | - $this->_resource_changes[ $model_classname ][ $fieldname ][ 'name' ] = $fieldname; |
|
123 | - foreach( $defaults as $attribute => $default_value ) { |
|
124 | - if( ! isset( $this->_resource_changes[ $model_classname ][ $fieldname ][ $attribute ] ) ) { |
|
125 | - $this->_resource_changes[ $model_classname ][ $fieldname ][ $attribute ] = $default_value; |
|
119 | + foreach ($this->_resource_changes as $version => $model_classnames) { |
|
120 | + foreach ($model_classnames as $model_classname => $extra_fields) { |
|
121 | + foreach ($extra_fields as $fieldname => $field_data) { |
|
122 | + $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
123 | + foreach ($defaults as $attribute => $default_value) { |
|
124 | + if ( ! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
125 | + $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 | } |
@@ -138,11 +138,11 @@ discard block |
||
138 | 138 | * @return array |
139 | 139 | */ |
140 | 140 | public function model_changes_between_requested_version_and_current() { |
141 | - if( $this->_cached_model_changes_between_requested_version_and_current === null ) { |
|
141 | + if ($this->_cached_model_changes_between_requested_version_and_current === null) { |
|
142 | 142 | $model_changes = array(); |
143 | - foreach( $this->model_changes() as $version => $models_changed_in_version ) { |
|
144 | - if( $version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version() ) { |
|
145 | - $model_changes[ $version ] = $models_changed_in_version; |
|
143 | + foreach ($this->model_changes() as $version => $models_changed_in_version) { |
|
144 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
145 | + $model_changes[$version] = $models_changed_in_version; |
|
146 | 146 | } |
147 | 147 | } |
148 | 148 | $this->_cached_model_changes_between_requested_version_and_current = $model_changes; |
@@ -158,11 +158,11 @@ discard block |
||
158 | 158 | * @return array |
159 | 159 | */ |
160 | 160 | public function resource_changes_between_requested_version_and_current() { |
161 | - if( $this->_cached_resource_changes_between_requested_version_and_current === null ) { |
|
161 | + if ($this->_cached_resource_changes_between_requested_version_and_current === null) { |
|
162 | 162 | $resource_changes = array(); |
163 | - foreach( $this->resource_changes() as $version => $model_classnames ) { |
|
164 | - if( $version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version() ) { |
|
165 | - $resource_changes[ $version ] = $model_classnames; |
|
163 | + foreach ($this->resource_changes() as $version => $model_classnames) { |
|
164 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
165 | + $resource_changes[$version] = $model_classnames; |
|
166 | 166 | } |
167 | 167 | } |
168 | 168 | $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
175 | 175 | * @return string like '4.6' |
176 | 176 | */ |
177 | - public function requested_version(){ |
|
177 | + public function requested_version() { |
|
178 | 178 | return $this->_requested_version; |
179 | 179 | } |
180 | 180 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | * If a version is missing then we don't know anything about what changes it introduced from the previous version |
189 | 189 | * @return array |
190 | 190 | */ |
191 | - public function model_changes(){ |
|
191 | + public function model_changes() { |
|
192 | 192 | return $this->_model_changes; |
193 | 193 | } |
194 | 194 | |
@@ -199,12 +199,12 @@ discard block |
||
199 | 199 | * @return array keys are model names, values are their classname |
200 | 200 | */ |
201 | 201 | public function models_for_requested_version() { |
202 | - if( $this->_cached_models_for_requested_version === null ) { |
|
202 | + if ($this->_cached_models_for_requested_version === null) { |
|
203 | 203 | $all_models_in_current_version = \EE_Registry::instance()->non_abstract_db_models; |
204 | - foreach( $this->model_changes_between_requested_version_and_current() as $version => $models_changed ) { |
|
205 | - foreach( $models_changed as $model_name => $new_indicator_or_fields_added ) { |
|
206 | - if( $new_indicator_or_fields_added === Model_Version_Info::model_added ) { |
|
207 | - unset( $all_models_in_current_version[ $model_name ] ); |
|
204 | + foreach ($this->model_changes_between_requested_version_and_current() as $version => $models_changed) { |
|
205 | + foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
206 | + if ($new_indicator_or_fields_added === Model_Version_Info::model_added) { |
|
207 | + unset($all_models_in_current_version[$model_name]); |
|
208 | 208 | } |
209 | 209 | } |
210 | 210 | } |
@@ -227,11 +227,11 @@ discard block |
||
227 | 227 | * @param string $model_name eg 'Event' |
228 | 228 | * @return boolean |
229 | 229 | */ |
230 | - public function is_model_name_in_this_version( $model_name ) { |
|
230 | + public function is_model_name_in_this_version($model_name) { |
|
231 | 231 | $model_names = $this->models_for_requested_version(); |
232 | - if( isset( $model_names[ $model_name ] ) ) { |
|
232 | + if (isset($model_names[$model_name])) { |
|
233 | 233 | return true; |
234 | - }else{ |
|
234 | + } else { |
|
235 | 235 | return false; |
236 | 236 | } |
237 | 237 | } |
@@ -246,10 +246,10 @@ discard block |
||
246 | 246 | * @return \EEM_Base |
247 | 247 | * @throws \EE_Error |
248 | 248 | */ |
249 | - public function load_model( $model_name ) { |
|
250 | - if( $this->is_model_name_in_this_version( $model_name ) ) { |
|
251 | - return \EE_Registry::instance()->load_model( $model_name ); |
|
252 | - }else{ |
|
249 | + public function load_model($model_name) { |
|
250 | + if ($this->is_model_name_in_this_version($model_name)) { |
|
251 | + return \EE_Registry::instance()->load_model($model_name); |
|
252 | + } else { |
|
253 | 253 | throw new \EE_Error( |
254 | 254 | sprintf( |
255 | 255 | __( |
@@ -271,21 +271,21 @@ discard block |
||
271 | 271 | * @param \EEM_Base $model |
272 | 272 | * @return array|\EE_Model_Field_Base[] |
273 | 273 | */ |
274 | - public function fields_on_model_in_this_version( $model ) { |
|
275 | - if( ! isset( $this->_cached_fields_on_models[ $model->get_this_model_name() ] ) ) { |
|
274 | + public function fields_on_model_in_this_version($model) { |
|
275 | + if ( ! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
276 | 276 | //get all model changes between the requested version and current core version |
277 | 277 | $changes = $this->model_changes_between_requested_version_and_current(); |
278 | 278 | //fetch all fields currently on this model |
279 | 279 | $current_fields = $model->field_settings(); |
280 | 280 | //remove all fields that have been added since |
281 | - foreach( $changes as $version => $changes_in_version ) { |
|
282 | - if( |
|
283 | - isset( $changes_in_version[ $model->get_this_model_name() ] ) |
|
284 | - && $changes_in_version[ $model->get_this_model_name() ] !== Model_Version_Info::model_added |
|
281 | + foreach ($changes as $version => $changes_in_version) { |
|
282 | + if ( |
|
283 | + isset($changes_in_version[$model->get_this_model_name()]) |
|
284 | + && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added |
|
285 | 285 | ) { |
286 | 286 | $current_fields = array_diff_key( |
287 | 287 | $current_fields, |
288 | - array_flip( $changes_in_version[ $model->get_this_model_name() ] ) |
|
288 | + array_flip($changes_in_version[$model->get_this_model_name()]) |
|
289 | 289 | ); |
290 | 290 | } |
291 | 291 | } |
@@ -303,9 +303,9 @@ discard block |
||
303 | 303 | * @param array $classnames |
304 | 304 | * @return boolean |
305 | 305 | */ |
306 | - public function is_subclass_of_one( $object, $classnames ) { |
|
307 | - foreach( $classnames as $classname ) { |
|
308 | - if( is_a( $object, $classname ) ) { |
|
306 | + public function is_subclass_of_one($object, $classnames) { |
|
307 | + foreach ($classnames as $classname) { |
|
308 | + if (is_a($object, $classname)) { |
|
309 | 309 | return true; |
310 | 310 | } |
311 | 311 | } |
@@ -316,10 +316,10 @@ discard block |
||
316 | 316 | * Returns the list of model field classes that that the API basically ignores |
317 | 317 | * @return array |
318 | 318 | */ |
319 | - public function fields_ignored(){ |
|
319 | + public function fields_ignored() { |
|
320 | 320 | return apply_filters( |
321 | 321 | 'FHEE__Controller_Model_Read_fields_ignored', |
322 | - array( 'EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field' ) |
|
322 | + array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
323 | 323 | ); |
324 | 324 | } |
325 | 325 | |
@@ -328,8 +328,8 @@ discard block |
||
328 | 328 | * @param EE_Model_Field_Base |
329 | 329 | * @return boolean |
330 | 330 | */ |
331 | - public function field_is_ignored( $field_obj ){ |
|
332 | - return $this->is_subclass_of_one( $field_obj, $this->fields_ignored() ); |
|
331 | + public function field_is_ignored($field_obj) { |
|
332 | + return $this->is_subclass_of_one($field_obj, $this->fields_ignored()); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | public function fields_that_have_rendered_format() { |
341 | 341 | return apply_filters( |
342 | 342 | 'FHEE__Controller_Model_Read__fields_raw', |
343 | - array( 'EE_Post_Content_Field', 'EE_Full_HTML_Field' ) |
|
343 | + array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
344 | 344 | ); |
345 | 345 | } |
346 | 346 | |
@@ -349,8 +349,8 @@ discard block |
||
349 | 349 | * @param EE_Model_Field_Base |
350 | 350 | * @return boolean |
351 | 351 | */ |
352 | - public function field_has_rendered_format( $field_obj ){ |
|
353 | - return $this->is_subclass_of_one( $field_obj, $this->fields_that_have_rendered_format() ); |
|
352 | + public function field_has_rendered_format($field_obj) { |
|
353 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_rendered_format()); |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | /** |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | public function fields_that_have_pretty_format() { |
363 | 363 | return apply_filters( |
364 | 364 | 'FHEE__Controller_Model_Read__fields_pretty', |
365 | - array( 'EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field' ) |
|
365 | + array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
366 | 366 | ); |
367 | 367 | } |
368 | 368 | |
@@ -371,8 +371,8 @@ discard block |
||
371 | 371 | * @param EE_Model_Field_Base |
372 | 372 | * @return boolean |
373 | 373 | */ |
374 | - public function field_has_pretty_format( $field_obj ){ |
|
375 | - return $this->is_subclass_of_one( $field_obj, $this->fields_that_have_pretty_format() ); |
|
374 | + public function field_has_pretty_format($field_obj) { |
|
375 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_pretty_format()); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | /** |
@@ -389,12 +389,12 @@ discard block |
||
389 | 389 | * @param \EEM_Base $model |
390 | 390 | * @return array |
391 | 391 | */ |
392 | - public function extra_resource_properties_for_model( $model ) { |
|
392 | + public function extra_resource_properties_for_model($model) { |
|
393 | 393 | $extra_properties = array(); |
394 | - foreach( $this->resource_changes_between_requested_version_and_current() as $version => $model_classnames ) { |
|
395 | - foreach( $model_classnames as $model_classname => $properties_added_in_this_version ) { |
|
396 | - if( is_subclass_of( $model, $model_classname ) ) { |
|
397 | - $extra_properties = array_merge( $extra_properties, $properties_added_in_this_version ); |
|
394 | + foreach ($this->resource_changes_between_requested_version_and_current() as $version => $model_classnames) { |
|
395 | + foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
396 | + if (is_subclass_of($model, $model_classname)) { |
|
397 | + $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
398 | 398 | } |
399 | 399 | } |
400 | 400 | } |
@@ -407,11 +407,11 @@ discard block |
||
407 | 407 | * @param \EEM_Base $model |
408 | 408 | * @return \EE_Model_Relation_Base[] |
409 | 409 | */ |
410 | - public function relation_settings( \EEM_Base $model ) { |
|
410 | + public function relation_settings(\EEM_Base $model) { |
|
411 | 411 | $relations = array(); |
412 | - foreach( $model->relation_settings() as $relation_name => $relation_obj ) { |
|
413 | - if( $this->is_model_name_in_this_version( $relation_name ) ) { |
|
414 | - $relations[ $relation_name ] = $relation_obj; |
|
412 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
413 | + if ($this->is_model_name_in_this_version($relation_name)) { |
|
414 | + $relations[$relation_name] = $relation_obj; |
|
415 | 415 | } |
416 | 416 | } |
417 | 417 | //filter the results, but use the old filter name |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | $model_names = $this->models_for_requested_version(); |
232 | 232 | if( isset( $model_names[ $model_name ] ) ) { |
233 | 233 | return true; |
234 | - }else{ |
|
234 | + } else{ |
|
235 | 235 | return false; |
236 | 236 | } |
237 | 237 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | public function load_model( $model_name ) { |
250 | 250 | if( $this->is_model_name_in_this_version( $model_name ) ) { |
251 | 251 | return \EE_Registry::instance()->load_model( $model_name ); |
252 | - }else{ |
|
252 | + } else{ |
|
253 | 253 | throw new \EE_Error( |
254 | 254 | sprintf( |
255 | 255 | __( |
@@ -3,15 +3,15 @@ |
||
3 | 3 | |
4 | 4 | class EE_DB_Only_Int_Field extends EE_DB_Only_Field_Base |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('integer'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('integer'); |
|
16 | + } |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -11,77 +11,77 @@ |
||
11 | 11 | class EE_Infinite_Integer_Field extends EE_Model_Field_Base |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @param string $table_column |
|
16 | - * @param string $nicename |
|
17 | - * @param bool $nullable |
|
18 | - * @param null $default_value |
|
19 | - */ |
|
20 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
21 | - { |
|
22 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
23 | - $this->setSchemaType(array('integer', 'null')); |
|
24 | - } |
|
14 | + /** |
|
15 | + * @param string $table_column |
|
16 | + * @param string $nicename |
|
17 | + * @param bool $nullable |
|
18 | + * @param null $default_value |
|
19 | + */ |
|
20 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
21 | + { |
|
22 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
23 | + $this->setSchemaType(array('integer', 'null')); |
|
24 | + } |
|
25 | 25 | |
26 | 26 | |
27 | - function prepare_for_use_in_db($value_of_field_on_model_object) |
|
28 | - { |
|
29 | - if ($value_of_field_on_model_object === EE_INF) { |
|
30 | - return EE_INF_IN_DB; |
|
31 | - } else { |
|
32 | - return intval($value_of_field_on_model_object); |
|
33 | - } |
|
34 | - } |
|
27 | + function prepare_for_use_in_db($value_of_field_on_model_object) |
|
28 | + { |
|
29 | + if ($value_of_field_on_model_object === EE_INF) { |
|
30 | + return EE_INF_IN_DB; |
|
31 | + } else { |
|
32 | + return intval($value_of_field_on_model_object); |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | - { |
|
38 | - if ($value_inputted_for_field_on_model_object === EE_INF_IN_DB || |
|
39 | - $value_inputted_for_field_on_model_object === EE_INF || |
|
40 | - $value_inputted_for_field_on_model_object === "EE_INF" || |
|
41 | - $value_inputted_for_field_on_model_object === "" |
|
42 | - ) { |
|
43 | - return EE_INF; |
|
44 | - } else { |
|
45 | - return intval($value_inputted_for_field_on_model_object); |
|
46 | - } |
|
47 | - } |
|
36 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | + { |
|
38 | + if ($value_inputted_for_field_on_model_object === EE_INF_IN_DB || |
|
39 | + $value_inputted_for_field_on_model_object === EE_INF || |
|
40 | + $value_inputted_for_field_on_model_object === "EE_INF" || |
|
41 | + $value_inputted_for_field_on_model_object === "" |
|
42 | + ) { |
|
43 | + return EE_INF; |
|
44 | + } else { |
|
45 | + return intval($value_inputted_for_field_on_model_object); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
50 | - { |
|
51 | - $intval = intval($value_inputted_for_field_on_model_object); |
|
52 | - if ($intval == EE_INF_IN_DB) { |
|
53 | - return EE_INF; |
|
54 | - } else { |
|
55 | - return $intval; |
|
56 | - } |
|
57 | - } |
|
49 | + function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
50 | + { |
|
51 | + $intval = intval($value_inputted_for_field_on_model_object); |
|
52 | + if ($intval == EE_INF_IN_DB) { |
|
53 | + return EE_INF; |
|
54 | + } else { |
|
55 | + return $intval; |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * For outputting this field's value. If you want to output it into an input or something, |
|
61 | - * use $schema=='input', as it will replace EE_INF with ''. If you want a readable version, use $schema=='text' |
|
62 | - * as it will replace EE_INF with i18n Infinite |
|
63 | - * |
|
64 | - * @param type $value_on_field_to_be_outputted |
|
65 | - * @param string $schema input, symbol, text; or any string you want to show if the value equals EE_INF |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
69 | - { |
|
70 | - if ($value_on_field_to_be_outputted === EE_INF) { |
|
71 | - switch ($schema) { |
|
72 | - case 'input': |
|
73 | - case 'form_input': |
|
74 | - return ''; |
|
75 | - case 'symbol': |
|
76 | - return "∞"; |
|
77 | - case 'text': |
|
78 | - case null: |
|
79 | - return __("Unlimited", "event_espresso"); |
|
80 | - default: |
|
81 | - return $schema; |
|
82 | - } |
|
83 | - } else { |
|
84 | - return $value_on_field_to_be_outputted; |
|
85 | - } |
|
86 | - } |
|
59 | + /** |
|
60 | + * For outputting this field's value. If you want to output it into an input or something, |
|
61 | + * use $schema=='input', as it will replace EE_INF with ''. If you want a readable version, use $schema=='text' |
|
62 | + * as it will replace EE_INF with i18n Infinite |
|
63 | + * |
|
64 | + * @param type $value_on_field_to_be_outputted |
|
65 | + * @param string $schema input, symbol, text; or any string you want to show if the value equals EE_INF |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
69 | + { |
|
70 | + if ($value_on_field_to_be_outputted === EE_INF) { |
|
71 | + switch ($schema) { |
|
72 | + case 'input': |
|
73 | + case 'form_input': |
|
74 | + return ''; |
|
75 | + case 'symbol': |
|
76 | + return "∞"; |
|
77 | + case 'text': |
|
78 | + case null: |
|
79 | + return __("Unlimited", "event_espresso"); |
|
80 | + default: |
|
81 | + return $schema; |
|
82 | + } |
|
83 | + } else { |
|
84 | + return $value_on_field_to_be_outputted; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | } |
@@ -10,72 +10,72 @@ |
||
10 | 10 | class EE_Serialized_Text_Field extends EE_Text_Field_Base |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param string $table_column |
|
15 | - * @param string $nicename |
|
16 | - * @param bool $nullable |
|
17 | - * @param null $default_value |
|
18 | - */ |
|
19 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
20 | - { |
|
21 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
22 | - $this->setSchemaType(array('object','string')); |
|
23 | - } |
|
13 | + /** |
|
14 | + * @param string $table_column |
|
15 | + * @param string $nicename |
|
16 | + * @param bool $nullable |
|
17 | + * @param null $default_value |
|
18 | + */ |
|
19 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
20 | + { |
|
21 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
22 | + $this->setSchemaType(array('object','string')); |
|
23 | + } |
|
24 | 24 | |
25 | 25 | |
26 | - /** |
|
27 | - * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
28 | - * |
|
29 | - * @param array $value_of_field_on_model_object |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - function prepare_for_use_in_db($value_of_field_on_model_object) |
|
33 | - { |
|
34 | - return maybe_serialize($value_of_field_on_model_object); |
|
35 | - } |
|
26 | + /** |
|
27 | + * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
28 | + * |
|
29 | + * @param array $value_of_field_on_model_object |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + function prepare_for_use_in_db($value_of_field_on_model_object) |
|
33 | + { |
|
34 | + return maybe_serialize($value_of_field_on_model_object); |
|
35 | + } |
|
36 | 36 | |
37 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
38 | - { |
|
39 | - $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
40 | - if (is_string($value_inputted_for_field_on_model_object)) { |
|
41 | - return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
42 | - } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
43 | - return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
44 | - } else {//so they passed NULL or an INT or something wack |
|
45 | - return $value_inputted_for_field_on_model_object; |
|
46 | - } |
|
47 | - } |
|
37 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
38 | + { |
|
39 | + $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
40 | + if (is_string($value_inputted_for_field_on_model_object)) { |
|
41 | + return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
42 | + } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
43 | + return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
44 | + } else {//so they passed NULL or an INT or something wack |
|
45 | + return $value_inputted_for_field_on_model_object; |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Value provided should definetely be a serialized string. We should unserialize into an array |
|
51 | - * |
|
52 | - * @param string $value_found_in_db_for_model_object |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
56 | - { |
|
57 | - return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
58 | - } |
|
49 | + /** |
|
50 | + * Value provided should definetely be a serialized string. We should unserialize into an array |
|
51 | + * |
|
52 | + * @param string $value_found_in_db_for_model_object |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
56 | + { |
|
57 | + return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Gets a string representation of the array |
|
62 | - * |
|
63 | - * @param type $value_on_field_to_be_outputted |
|
64 | - * @param string $schema , possible values are ',', others can be added |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
68 | - { |
|
69 | - switch ($schema) { |
|
70 | - case 'print_r': |
|
71 | - $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
72 | - break; |
|
73 | - case 'as_table': |
|
74 | - $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
75 | - break; |
|
76 | - default: |
|
77 | - $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
78 | - } |
|
79 | - return $pretty_value; |
|
80 | - } |
|
60 | + /** |
|
61 | + * Gets a string representation of the array |
|
62 | + * |
|
63 | + * @param type $value_on_field_to_be_outputted |
|
64 | + * @param string $schema , possible values are ',', others can be added |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
68 | + { |
|
69 | + switch ($schema) { |
|
70 | + case 'print_r': |
|
71 | + $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
72 | + break; |
|
73 | + case 'as_table': |
|
74 | + $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
75 | + break; |
|
76 | + default: |
|
77 | + $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
78 | + } |
|
79 | + return $pretty_value; |
|
80 | + } |
|
81 | 81 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
20 | 20 | { |
21 | 21 | parent::__construct($table_column, $nicename, $nullable, $default_value); |
22 | - $this->setSchemaType(array('object','string')); |
|
22 | + $this->setSchemaType(array('object', 'string')); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 |
@@ -10,115 +10,115 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Enum_Integer_Field extends EE_Integer_Field |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array $_allowed_enum_values |
|
15 | - */ |
|
16 | - public $_allowed_enum_values; |
|
13 | + /** |
|
14 | + * @var array $_allowed_enum_values |
|
15 | + */ |
|
16 | + public $_allowed_enum_values; |
|
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * @param string $table_column |
|
21 | - * @param string $nicename |
|
22 | - * @param boolean $nullable |
|
23 | - * @param int $default_value |
|
24 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
25 | - */ |
|
26 | - public function __construct($table_column, $nicename, $nullable, $default_value, $allowed_enum_values) |
|
27 | - { |
|
28 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
29 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
30 | - $this->setSchemaType('object'); |
|
31 | - } |
|
19 | + /** |
|
20 | + * @param string $table_column |
|
21 | + * @param string $nicename |
|
22 | + * @param boolean $nullable |
|
23 | + * @param int $default_value |
|
24 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
25 | + */ |
|
26 | + public function __construct($table_column, $nicename, $nullable, $default_value, $allowed_enum_values) |
|
27 | + { |
|
28 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
29 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
30 | + $this->setSchemaType('object'); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns the list of allowed enum options, but filterable. |
|
35 | - * This is used internally |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - protected function _allowed_enum_values() |
|
40 | - { |
|
41 | - return (array)apply_filters( |
|
42 | - 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
43 | - $this->_allowed_enum_values, |
|
44 | - $this |
|
45 | - ); |
|
46 | - } |
|
33 | + /** |
|
34 | + * Returns the list of allowed enum options, but filterable. |
|
35 | + * This is used internally |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + protected function _allowed_enum_values() |
|
40 | + { |
|
41 | + return (array)apply_filters( |
|
42 | + 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
43 | + $this->_allowed_enum_values, |
|
44 | + $this |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
50 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
51 | - * |
|
52 | - * @param int $value_inputted_for_field_on_model_object |
|
53 | - * @return int |
|
54 | - * @throws EE_Error |
|
55 | - */ |
|
56 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
57 | - { |
|
58 | - $allowed_enum_values = $this->_allowed_enum_values(); |
|
59 | - if ( |
|
60 | - $value_inputted_for_field_on_model_object !== null |
|
61 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
62 | - ) { |
|
63 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
64 | - $msg = sprintf( |
|
65 | - __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
66 | - $value_inputted_for_field_on_model_object, |
|
67 | - $this->_name |
|
68 | - ); |
|
69 | - $msg2 = sprintf( |
|
70 | - __('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
71 | - $this->_name, |
|
72 | - implode(', ', array_keys($allowed_enum_values)), |
|
73 | - $value_inputted_for_field_on_model_object |
|
74 | - ); |
|
75 | - EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
76 | - } |
|
77 | - return $this->get_default_value(); |
|
78 | - } |
|
79 | - return (int)$value_inputted_for_field_on_model_object; |
|
80 | - } |
|
48 | + /** |
|
49 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
50 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
51 | + * |
|
52 | + * @param int $value_inputted_for_field_on_model_object |
|
53 | + * @return int |
|
54 | + * @throws EE_Error |
|
55 | + */ |
|
56 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
57 | + { |
|
58 | + $allowed_enum_values = $this->_allowed_enum_values(); |
|
59 | + if ( |
|
60 | + $value_inputted_for_field_on_model_object !== null |
|
61 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
62 | + ) { |
|
63 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
64 | + $msg = sprintf( |
|
65 | + __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
66 | + $value_inputted_for_field_on_model_object, |
|
67 | + $this->_name |
|
68 | + ); |
|
69 | + $msg2 = sprintf( |
|
70 | + __('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
71 | + $this->_name, |
|
72 | + implode(', ', array_keys($allowed_enum_values)), |
|
73 | + $value_inputted_for_field_on_model_object |
|
74 | + ); |
|
75 | + EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
76 | + } |
|
77 | + return $this->get_default_value(); |
|
78 | + } |
|
79 | + return (int)$value_inputted_for_field_on_model_object; |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * Gets the pretty version of the enum's value. |
|
86 | - * |
|
87 | - * @param int | string $value_on_field_to_be_outputted |
|
88 | - * @param null $schema |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
92 | - { |
|
93 | - $options = $this->_allowed_enum_values(); |
|
94 | - if (isset($options[$value_on_field_to_be_outputted])) { |
|
95 | - return $options[$value_on_field_to_be_outputted]; |
|
96 | - } else { |
|
97 | - return $value_on_field_to_be_outputted; |
|
98 | - } |
|
99 | - } |
|
84 | + /** |
|
85 | + * Gets the pretty version of the enum's value. |
|
86 | + * |
|
87 | + * @param int | string $value_on_field_to_be_outputted |
|
88 | + * @param null $schema |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
92 | + { |
|
93 | + $options = $this->_allowed_enum_values(); |
|
94 | + if (isset($options[$value_on_field_to_be_outputted])) { |
|
95 | + return $options[$value_on_field_to_be_outputted]; |
|
96 | + } else { |
|
97 | + return $value_on_field_to_be_outputted; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | |
102 | - public function getSchemaProperties() |
|
103 | - { |
|
104 | - return array( |
|
105 | - 'raw' => array( |
|
106 | - 'description' => sprintf( |
|
107 | - __('%s - the value in the database.', 'event_espresso'), |
|
108 | - $this->get_nicename() |
|
109 | - ), |
|
110 | - 'enum' => array_keys($this->_allowed_enum_values()), |
|
111 | - 'type' => 'integer' |
|
112 | - ), |
|
113 | - 'pretty' => array( |
|
114 | - 'description' => sprintf( |
|
115 | - __('%s - the value for display.', 'event_espresso'), |
|
116 | - $this->get_nicename() |
|
117 | - ), |
|
118 | - 'enum' => array_values($this->_allowed_enum_values()), |
|
119 | - 'type' => 'string', |
|
120 | - 'read_only' => true |
|
121 | - ) |
|
122 | - ); |
|
123 | - } |
|
102 | + public function getSchemaProperties() |
|
103 | + { |
|
104 | + return array( |
|
105 | + 'raw' => array( |
|
106 | + 'description' => sprintf( |
|
107 | + __('%s - the value in the database.', 'event_espresso'), |
|
108 | + $this->get_nicename() |
|
109 | + ), |
|
110 | + 'enum' => array_keys($this->_allowed_enum_values()), |
|
111 | + 'type' => 'integer' |
|
112 | + ), |
|
113 | + 'pretty' => array( |
|
114 | + 'description' => sprintf( |
|
115 | + __('%s - the value for display.', 'event_espresso'), |
|
116 | + $this->get_nicename() |
|
117 | + ), |
|
118 | + 'enum' => array_values($this->_allowed_enum_values()), |
|
119 | + 'type' => 'string', |
|
120 | + 'read_only' => true |
|
121 | + ) |
|
122 | + ); |
|
123 | + } |
|
124 | 124 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | */ |
39 | 39 | protected function _allowed_enum_values() |
40 | 40 | { |
41 | - return (array)apply_filters( |
|
41 | + return (array) apply_filters( |
|
42 | 42 | 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
43 | 43 | $this->_allowed_enum_values, |
44 | 44 | $this |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | } |
77 | 77 | return $this->get_default_value(); |
78 | 78 | } |
79 | - return (int)$value_inputted_for_field_on_model_object; |
|
79 | + return (int) $value_inputted_for_field_on_model_object; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 |
@@ -3,50 +3,50 @@ |
||
3 | 3 | |
4 | 4 | class EE_Boolean_Field extends EE_Integer_Field |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('boolean'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('boolean'); |
|
16 | + } |
|
17 | 17 | |
18 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | - { |
|
20 | - if ($value_inputted_for_field_on_model_object) { |
|
21 | - return true; |
|
22 | - } else { |
|
23 | - return false; |
|
24 | - } |
|
25 | - } |
|
18 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | + { |
|
20 | + if ($value_inputted_for_field_on_model_object) { |
|
21 | + return true; |
|
22 | + } else { |
|
23 | + return false; |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Make sure we're returning booleans |
|
29 | - * |
|
30 | - * @param string $value_inputted_for_field_on_model_object |
|
31 | - * @return boolean |
|
32 | - */ |
|
33 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | - { |
|
35 | - return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | - } |
|
27 | + /** |
|
28 | + * Make sure we're returning booleans |
|
29 | + * |
|
30 | + * @param string $value_inputted_for_field_on_model_object |
|
31 | + * @return boolean |
|
32 | + */ |
|
33 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | + { |
|
35 | + return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Gets a nice Yes/No value for this field |
|
40 | - * |
|
41 | - * @param boolean $value_on_field_to_be_outputted |
|
42 | - * @return string Yes or No |
|
43 | - */ |
|
44 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | - { |
|
46 | - return apply_filters( |
|
47 | - 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | - $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | - $value_on_field_to_be_outputted |
|
50 | - ); |
|
51 | - } |
|
38 | + /** |
|
39 | + * Gets a nice Yes/No value for this field |
|
40 | + * |
|
41 | + * @param boolean $value_on_field_to_be_outputted |
|
42 | + * @return string Yes or No |
|
43 | + */ |
|
44 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | + { |
|
46 | + return apply_filters( |
|
47 | + 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | + $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | + $value_on_field_to_be_outputted |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | } |
@@ -1,5 +1,5 @@ |
||
1 | 1 | <?php |
2 | -defined( 'EVENT_ESPRESSO_VERSION') || exit; |
|
2 | +defined('EVENT_ESPRESSO_VERSION') || exit; |
|
3 | 3 | |
4 | 4 | class EE_Boolean_Field extends EE_Integer_Field |
5 | 5 | { |
@@ -8,56 +8,56 @@ |
||
8 | 8 | class EE_Post_Content_Field extends EE_Text_Field_Base |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * @param string $table_column |
|
13 | - * @param string $nicename |
|
14 | - * @param bool $nullable |
|
15 | - * @param null $default_value |
|
16 | - */ |
|
17 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
18 | - { |
|
19 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | - $this->setSchemaType('object'); |
|
21 | - } |
|
11 | + /** |
|
12 | + * @param string $table_column |
|
13 | + * @param string $nicename |
|
14 | + * @param bool $nullable |
|
15 | + * @param null $default_value |
|
16 | + */ |
|
17 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
18 | + { |
|
19 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | + $this->setSchemaType('object'); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * removes all tags which a WP Post wouldn't allow in its content normally |
|
26 | - * |
|
27 | - * @param string $value |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - function prepare_for_set($value) |
|
31 | - { |
|
32 | - if (! current_user_can('unfiltered_html')) { |
|
33 | - $value = wp_kses("$value", wp_kses_allowed_html('post')); |
|
34 | - } |
|
35 | - return parent::prepare_for_set($value); |
|
36 | - } |
|
24 | + /** |
|
25 | + * removes all tags which a WP Post wouldn't allow in its content normally |
|
26 | + * |
|
27 | + * @param string $value |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + function prepare_for_set($value) |
|
31 | + { |
|
32 | + if (! current_user_can('unfiltered_html')) { |
|
33 | + $value = wp_kses("$value", wp_kses_allowed_html('post')); |
|
34 | + } |
|
35 | + return parent::prepare_for_set($value); |
|
36 | + } |
|
37 | 37 | |
38 | - function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
39 | - { |
|
40 | - return $value_found_in_db_for_model_object; |
|
41 | - } |
|
38 | + function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
39 | + { |
|
40 | + return $value_found_in_db_for_model_object; |
|
41 | + } |
|
42 | 42 | |
43 | 43 | |
44 | - public function getSchemaProperties() |
|
45 | - { |
|
46 | - return array( |
|
47 | - 'raw' => array( |
|
48 | - 'description' => sprintf( |
|
49 | - __('%s - the content as it exists in the database.', 'event_espresso'), |
|
50 | - $this->get_nicename() |
|
51 | - ), |
|
52 | - 'type' => 'string' |
|
53 | - ), |
|
54 | - 'rendered' => array( |
|
55 | - 'description' => sprintf( |
|
56 | - __('%s - the content rendered for display.', 'event_espresso'), |
|
57 | - $this->get_nicename() |
|
58 | - ), |
|
59 | - 'type' => 'string' |
|
60 | - ) |
|
61 | - ); |
|
62 | - } |
|
44 | + public function getSchemaProperties() |
|
45 | + { |
|
46 | + return array( |
|
47 | + 'raw' => array( |
|
48 | + 'description' => sprintf( |
|
49 | + __('%s - the content as it exists in the database.', 'event_espresso'), |
|
50 | + $this->get_nicename() |
|
51 | + ), |
|
52 | + 'type' => 'string' |
|
53 | + ), |
|
54 | + 'rendered' => array( |
|
55 | + 'description' => sprintf( |
|
56 | + __('%s - the content rendered for display.', 'event_espresso'), |
|
57 | + $this->get_nicename() |
|
58 | + ), |
|
59 | + 'type' => 'string' |
|
60 | + ) |
|
61 | + ); |
|
62 | + } |
|
63 | 63 | } |
64 | 64 | \ No newline at end of file |