@@ -22,1370 +22,1370 @@ |
||
| 22 | 22 | class EED_Core_Rest_Api extends EED_Module |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 26 | - |
|
| 27 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 28 | - |
|
| 29 | - const saved_routes_option_names = 'ee_core_routes'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * string used in _links response bodies to make them globally unique. |
|
| 33 | - * |
|
| 34 | - * @see http://v2.wp-api.org/extending/linking/ |
|
| 35 | - */ |
|
| 36 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var CalculatedModelFields |
|
| 40 | - */ |
|
| 41 | - protected static $_field_calculator; |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return EED_Core_Rest_Api|EED_Module |
|
| 46 | - */ |
|
| 47 | - public static function instance() |
|
| 48 | - { |
|
| 49 | - return parent::get_instance(EED_Core_Rest_Api::class); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 55 | - * |
|
| 56 | - * @access public |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public static function set_hooks() |
|
| 60 | - { |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 66 | - * |
|
| 67 | - * @access public |
|
| 68 | - * @return void |
|
| 69 | - */ |
|
| 70 | - public static function set_hooks_admin() |
|
| 71 | - { |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - public static function set_hooks_both() |
|
| 76 | - { |
|
| 77 | - add_action('rest_api_init', ['EED_Core_Rest_Api', 'set_hooks_rest_api'], 5); |
|
| 78 | - add_action('rest_api_init', ['EED_Core_Rest_Api', 'register_routes'], 10); |
|
| 79 | - add_filter('rest_route_data', ['EED_Core_Rest_Api', 'hide_old_endpoints'], 10, 2); |
|
| 80 | - add_filter( |
|
| 81 | - 'rest_index', |
|
| 82 | - ['EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex'] |
|
| 83 | - ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @since $VID:$ |
|
| 89 | - */ |
|
| 90 | - public static function loadCalculatedModelFields() |
|
| 91 | - { |
|
| 92 | - EED_Core_Rest_Api::$_field_calculator = LoaderFactory::getLoader()->load( |
|
| 93 | - 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' |
|
| 94 | - ); |
|
| 95 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * sets up hooks which only need to be included as part of REST API requests; |
|
| 101 | - * other requests like to the frontend or admin etc don't need them |
|
| 102 | - * |
|
| 103 | - * @throws EE_Error |
|
| 104 | - */ |
|
| 105 | - public static function set_hooks_rest_api() |
|
| 106 | - { |
|
| 107 | - // set hooks which account for changes made to the API |
|
| 108 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * public wrapper of _set_hooks_for_changes. |
|
| 114 | - * Loads all the hooks which make requests to old versions of the API |
|
| 115 | - * appear the same as they always did |
|
| 116 | - * |
|
| 117 | - * @throws EE_Error |
|
| 118 | - */ |
|
| 119 | - public static function set_hooks_for_changes() |
|
| 120 | - { |
|
| 121 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Loads all the hooks which make requests to old versions of the API |
|
| 127 | - * appear the same as they always did |
|
| 128 | - * |
|
| 129 | - * @throws EE_Error |
|
| 130 | - */ |
|
| 131 | - protected static function _set_hooks_for_changes() |
|
| 132 | - { |
|
| 133 | - $folder_contents = EEH_File::get_contents_of_folders([EE_LIBRARIES . 'rest_api/changes'], false); |
|
| 134 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 135 | - // ignore the base parent class |
|
| 136 | - // and legacy named classes |
|
| 137 | - if ($classname_in_namespace === 'ChangesInBase' |
|
| 138 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 139 | - ) { |
|
| 140 | - continue; |
|
| 141 | - } |
|
| 142 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 143 | - if (class_exists($full_classname)) { |
|
| 144 | - $instance_of_class = new $full_classname; |
|
| 145 | - if ($instance_of_class instanceof ChangesInBase) { |
|
| 146 | - $instance_of_class->setHooks(); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 155 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 156 | - * |
|
| 157 | - * @throws EE_Error |
|
| 158 | - * @throws ReflectionException |
|
| 159 | - */ |
|
| 160 | - public static function register_routes() |
|
| 161 | - { |
|
| 162 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 163 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 164 | - /** |
|
| 165 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 166 | - * but can also contain route options like { |
|
| 167 | - * @type array $schema { |
|
| 168 | - * @type callable $schema_callback |
|
| 169 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 170 | - * WP_REST_Request of course |
|
| 171 | - * } |
|
| 172 | - * } |
|
| 173 | - */ |
|
| 174 | - // when registering routes, register all the endpoints' data at the same time |
|
| 175 | - $multiple_endpoint_args = []; |
|
| 176 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 177 | - /** |
|
| 178 | - * @var array $data_for_single_endpoint { |
|
| 179 | - * @type callable $callback |
|
| 180 | - * @type string methods |
|
| 181 | - * @type array args |
|
| 182 | - * @type array _links |
|
| 183 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 184 | - * WP_REST_Request of course |
|
| 185 | - * } |
|
| 186 | - */ |
|
| 187 | - // skip route options |
|
| 188 | - if (! is_numeric($endpoint_key)) { |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 192 | - throw new EE_Error( |
|
| 193 | - esc_html__( |
|
| 194 | - // @codingStandardsIgnoreStart |
|
| 195 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 196 | - // @codingStandardsIgnoreEnd |
|
| 197 | - 'event_espresso' |
|
| 198 | - ) |
|
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - $callback = $data_for_single_endpoint['callback']; |
|
| 202 | - $single_endpoint_args = [ |
|
| 203 | - 'methods' => $data_for_single_endpoint['methods'], |
|
| 204 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 205 | - : [], |
|
| 206 | - ]; |
|
| 207 | - if (isset($data_for_single_endpoint['_links'])) { |
|
| 208 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 209 | - } |
|
| 210 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 211 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 212 | - $single_endpoint_args['callback'] = static function (WP_REST_Request $request) use ( |
|
| 213 | - $callback, |
|
| 214 | - $callback_args |
|
| 215 | - ) { |
|
| 216 | - array_unshift($callback_args, $request); |
|
| 217 | - return call_user_func_array( |
|
| 218 | - $callback, |
|
| 219 | - $callback_args |
|
| 220 | - ); |
|
| 221 | - }; |
|
| 222 | - } else { |
|
| 223 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 224 | - } |
|
| 225 | - // As of WordPress 5.5, if a permission_callback is not provided, |
|
| 226 | - // the REST API will issue a _doing_it_wrong notice. |
|
| 227 | - // Since the EE REST API defers capabilities to the db model system, |
|
| 228 | - // we will just use the generic WP callback for public endpoints |
|
| 229 | - if (! isset($single_endpoint_args['permission_callback'])) { |
|
| 230 | - $single_endpoint_args['permission_callback'] = '__return_true'; |
|
| 231 | - } |
|
| 232 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 233 | - } |
|
| 234 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 235 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 236 | - $schema_callback = $schema_route_data['schema_callback']; |
|
| 237 | - $callback_args = $schema_route_data['callback_args']; |
|
| 238 | - $multiple_endpoint_args['schema'] = static function () use ($schema_callback, $callback_args) { |
|
| 239 | - return call_user_func_array( |
|
| 240 | - $schema_callback, |
|
| 241 | - $callback_args |
|
| 242 | - ); |
|
| 243 | - }; |
|
| 244 | - } |
|
| 245 | - register_rest_route( |
|
| 246 | - $namespace, |
|
| 247 | - $relative_route, |
|
| 248 | - $multiple_endpoint_args |
|
| 249 | - ); |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Checks if there was a version change or something that merits invalidating the cached |
|
| 257 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 258 | - * next time the WP API is used |
|
| 259 | - */ |
|
| 260 | - public static function invalidate_cached_route_data_on_version_change() |
|
| 261 | - { |
|
| 262 | - if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 263 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 264 | - } |
|
| 265 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
| 266 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 267 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 275 | - */ |
|
| 276 | - public static function invalidate_cached_route_data() |
|
| 277 | - { |
|
| 278 | - // delete the saved EE REST API routes |
|
| 279 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 280 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Gets the EE route data |
|
| 287 | - * |
|
| 288 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 289 | - * @throws EE_Error |
|
| 290 | - * @throws ReflectionException |
|
| 291 | - * @type string|array $callback |
|
| 292 | - * @type string $methods |
|
| 293 | - * @type boolean $hidden_endpoint |
|
| 294 | - * } |
|
| 295 | - */ |
|
| 296 | - public static function get_ee_route_data() |
|
| 297 | - { |
|
| 298 | - $ee_routes = []; |
|
| 299 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoints) { |
|
| 300 | - $ee_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = EED_Core_Rest_Api::_get_ee_route_data_for_version( |
|
| 301 | - $version, |
|
| 302 | - $hidden_endpoints |
|
| 303 | - ); |
|
| 304 | - } |
|
| 305 | - return $ee_routes; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Gets the EE route data from the wp options if it exists already, |
|
| 311 | - * otherwise re-generates it and saves it to the option |
|
| 312 | - * |
|
| 313 | - * @param string $version |
|
| 314 | - * @param boolean $hidden_endpoints |
|
| 315 | - * @return array |
|
| 316 | - * @throws EE_Error |
|
| 317 | - * @throws ReflectionException |
|
| 318 | - */ |
|
| 319 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 320 | - { |
|
| 321 | - $ee_routes = get_option(EED_Core_Rest_Api::saved_routes_option_names . $version, null); |
|
| 322 | - if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 323 | - $ee_routes = EED_Core_Rest_Api::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 324 | - } |
|
| 325 | - return $ee_routes; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Saves the EE REST API route data to a wp option and returns it |
|
| 331 | - * |
|
| 332 | - * @param string $version |
|
| 333 | - * @param boolean $hidden_endpoints |
|
| 334 | - * @return mixed|null |
|
| 335 | - * @throws EE_Error |
|
| 336 | - * @throws ReflectionException |
|
| 337 | - */ |
|
| 338 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 339 | - { |
|
| 340 | - $instance = EED_Core_Rest_Api::instance(); |
|
| 341 | - $routes = apply_filters( |
|
| 342 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 343 | - array_replace_recursive( |
|
| 344 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 345 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 346 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 347 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 348 | - ) |
|
| 349 | - ); |
|
| 350 | - $option_name = EED_Core_Rest_Api::saved_routes_option_names . $version; |
|
| 351 | - if (get_option($option_name)) { |
|
| 352 | - update_option($option_name, $routes, true); |
|
| 353 | - } else { |
|
| 354 | - add_option($option_name, $routes, null, 'no'); |
|
| 355 | - } |
|
| 356 | - return $routes; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 362 | - * need to calculate it on every request |
|
| 363 | - * |
|
| 364 | - * @return void |
|
| 365 | - * @deprecated since version 4.9.1 |
|
| 366 | - */ |
|
| 367 | - public static function save_ee_routes() |
|
| 368 | - { |
|
| 369 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 370 | - $instance = EED_Core_Rest_Api::instance(); |
|
| 371 | - $routes = apply_filters( |
|
| 372 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 373 | - array_replace_recursive( |
|
| 374 | - $instance->_register_config_routes(), |
|
| 375 | - $instance->_register_meta_routes(), |
|
| 376 | - $instance->_register_model_routes(), |
|
| 377 | - $instance->_register_rpc_routes() |
|
| 378 | - ) |
|
| 379 | - ); |
|
| 380 | - update_option(EED_Core_Rest_Api::saved_routes_option_names, $routes, true); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Gets all the route information relating to EE models |
|
| 387 | - * |
|
| 388 | - * @return array @see get_ee_route_data |
|
| 389 | - * @deprecated since version 4.9.1 |
|
| 390 | - */ |
|
| 391 | - protected function _register_model_routes() |
|
| 392 | - { |
|
| 393 | - $model_routes = []; |
|
| 394 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 395 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 396 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 397 | - } |
|
| 398 | - return $model_routes; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Decides whether or not to add write endpoints for this model. |
|
| 404 | - * Currently, this defaults to exclude all global tables and models |
|
| 405 | - * which would allow inserting WP core data (we don't want to duplicate |
|
| 406 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 407 | - * |
|
| 408 | - * @param EEM_Base $model |
|
| 409 | - * @return bool |
|
| 410 | - */ |
|
| 411 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
| 412 | - { |
|
| 413 | - if ($model->is_wp_core_model()) { |
|
| 414 | - return false; |
|
| 415 | - } |
|
| 416 | - foreach ($model->get_tables() as $table) { |
|
| 417 | - if ($table->is_global()) { |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - return true; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 427 | - * in this versioned namespace of EE4 |
|
| 428 | - * |
|
| 429 | - * @param $version |
|
| 430 | - * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 431 | - */ |
|
| 432 | - public static function model_names_with_plural_routes($version) |
|
| 433 | - { |
|
| 434 | - $model_version_info = new ModelVersionInfo($version); |
|
| 435 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 436 | - // let's not bother having endpoints for extra metas |
|
| 437 | - unset( |
|
| 438 | - $models_to_register['Extra_Meta'], |
|
| 439 | - $models_to_register['Extra_Join'], |
|
| 440 | - $models_to_register['Post_Meta'] |
|
| 441 | - ); |
|
| 442 | - return apply_filters( |
|
| 443 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 444 | - $models_to_register |
|
| 445 | - ); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Gets the route data for EE models in the specified version |
|
| 451 | - * |
|
| 452 | - * @param string $version |
|
| 453 | - * @param boolean $hidden_endpoint |
|
| 454 | - * @return array |
|
| 455 | - * @throws EE_Error |
|
| 456 | - * @throws ReflectionException |
|
| 457 | - */ |
|
| 458 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 459 | - { |
|
| 460 | - $model_routes = []; |
|
| 461 | - $model_version_info = new ModelVersionInfo($version); |
|
| 462 | - foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 463 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 464 | - // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 465 | - if (! $model instanceof EEM_Base) { |
|
| 466 | - continue; |
|
| 467 | - } |
|
| 468 | - // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 469 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 470 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 471 | - $model_routes[ $plural_model_route ] = [ |
|
| 472 | - [ |
|
| 473 | - 'callback' => [ |
|
| 474 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 475 | - 'handleRequestGetAll', |
|
| 476 | - ], |
|
| 477 | - 'callback_args' => [$version, $model_name], |
|
| 478 | - 'methods' => WP_REST_Server::READABLE, |
|
| 479 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 480 | - 'args' => $this->_get_read_query_params($model, $version), |
|
| 481 | - '_links' => [ |
|
| 482 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 483 | - ], |
|
| 484 | - ], |
|
| 485 | - 'schema' => [ |
|
| 486 | - 'schema_callback' => [ |
|
| 487 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 488 | - 'handleSchemaRequest', |
|
| 489 | - ], |
|
| 490 | - 'callback_args' => [$version, $model_name], |
|
| 491 | - ], |
|
| 492 | - ]; |
|
| 493 | - $model_routes[ $singular_model_route ] = [ |
|
| 494 | - [ |
|
| 495 | - 'callback' => [ |
|
| 496 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 497 | - 'handleRequestGetOne', |
|
| 498 | - ], |
|
| 499 | - 'callback_args' => [$version, $model_name], |
|
| 500 | - 'methods' => WP_REST_Server::READABLE, |
|
| 501 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 502 | - 'args' => $this->_get_response_selection_query_params($model, $version, true), |
|
| 503 | - ], |
|
| 504 | - ]; |
|
| 505 | - if (apply_filters( |
|
| 506 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 507 | - EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 508 | - $model |
|
| 509 | - )) { |
|
| 510 | - $model_routes[ $plural_model_route ][] = [ |
|
| 511 | - 'callback' => [ |
|
| 512 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 513 | - 'handleRequestInsert', |
|
| 514 | - ], |
|
| 515 | - 'callback_args' => [$version, $model_name], |
|
| 516 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 517 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 518 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 519 | - ]; |
|
| 520 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 521 | - $model_routes[ $singular_model_route ], |
|
| 522 | - [ |
|
| 523 | - [ |
|
| 524 | - 'callback' => [ |
|
| 525 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 526 | - 'handleRequestUpdate', |
|
| 527 | - ], |
|
| 528 | - 'callback_args' => [$version, $model_name], |
|
| 529 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 530 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 531 | - 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 532 | - ], |
|
| 533 | - [ |
|
| 534 | - 'callback' => [ |
|
| 535 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 536 | - 'handleRequestDelete', |
|
| 537 | - ], |
|
| 538 | - 'callback_args' => [$version, $model_name], |
|
| 539 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 540 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 541 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
| 542 | - ], |
|
| 543 | - ] |
|
| 544 | - ); |
|
| 545 | - } |
|
| 546 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 547 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 548 | - $model, |
|
| 549 | - '(?P<id>[^\/]+)', |
|
| 550 | - $relation_obj |
|
| 551 | - ); |
|
| 552 | - $model_routes[ $related_route ] = [ |
|
| 553 | - [ |
|
| 554 | - 'callback' => [ |
|
| 555 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 556 | - 'handleRequestGetRelated', |
|
| 557 | - ], |
|
| 558 | - 'callback_args' => [$version, $model_name, $relation_name], |
|
| 559 | - 'methods' => WP_REST_Server::READABLE, |
|
| 560 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 561 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 562 | - ], |
|
| 563 | - ]; |
|
| 564 | - |
|
| 565 | - $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 566 | - $model_routes[ $related_write_route ] = [ |
|
| 567 | - [ |
|
| 568 | - 'callback' => [ |
|
| 569 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 570 | - 'handleRequestAddRelation', |
|
| 571 | - ], |
|
| 572 | - 'callback_args' => [$version, $model_name, $relation_name], |
|
| 573 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 574 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 575 | - 'args' => $this->_get_add_relation_query_params( |
|
| 576 | - $model, |
|
| 577 | - $relation_obj->get_other_model(), |
|
| 578 | - $version |
|
| 579 | - ), |
|
| 580 | - ], |
|
| 581 | - [ |
|
| 582 | - 'callback' => [ |
|
| 583 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 584 | - 'handleRequestRemoveRelation', |
|
| 585 | - ], |
|
| 586 | - 'callback_args' => [$version, $model_name, $relation_name], |
|
| 587 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 588 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 589 | - 'args' => [], |
|
| 590 | - ], |
|
| 591 | - ]; |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - return $model_routes; |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 600 | - * excluding the preceding slash. |
|
| 601 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 602 | - * |
|
| 603 | - * @param EEM_Base $model |
|
| 604 | - * @return string |
|
| 605 | - */ |
|
| 606 | - public static function get_collection_route(EEM_Base $model) |
|
| 607 | - { |
|
| 608 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 614 | - * excluding the preceding slash. |
|
| 615 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 616 | - * |
|
| 617 | - * @param EEM_Base $model eg Event or Venue |
|
| 618 | - * @param string $id |
|
| 619 | - * @return string |
|
| 620 | - */ |
|
| 621 | - public static function get_entity_route($model, $id) |
|
| 622 | - { |
|
| 623 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 629 | - * excluding the preceding slash. |
|
| 630 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 631 | - * |
|
| 632 | - * @param EEM_Base $model eg Event or Venue |
|
| 633 | - * @param string $id |
|
| 634 | - * @param EE_Model_Relation_Base $relation_obj |
|
| 635 | - * @return string |
|
| 636 | - */ |
|
| 637 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 638 | - { |
|
| 639 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 640 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
| 641 | - $relation_obj |
|
| 642 | - ); |
|
| 643 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 649 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 650 | - * |
|
| 651 | - * @param string $relative_route |
|
| 652 | - * @param string $version |
|
| 653 | - * @return string |
|
| 654 | - */ |
|
| 655 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 656 | - { |
|
| 657 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 663 | - * routes that don't conform to the traditional REST CRUD-style). |
|
| 664 | - * |
|
| 665 | - * @deprecated since 4.9.1 |
|
| 666 | - */ |
|
| 667 | - protected function _register_rpc_routes() |
|
| 668 | - { |
|
| 669 | - $routes = []; |
|
| 670 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 671 | - $routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 672 | - $version, |
|
| 673 | - $hidden_endpoint |
|
| 674 | - ); |
|
| 675 | - } |
|
| 676 | - return $routes; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * @param string $version |
|
| 682 | - * @param boolean $hidden_endpoint |
|
| 683 | - * @return array |
|
| 684 | - */ |
|
| 685 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 686 | - { |
|
| 687 | - $this_versions_routes = []; |
|
| 688 | - // checkin endpoint |
|
| 689 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = [ |
|
| 690 | - [ |
|
| 691 | - 'callback' => [ |
|
| 692 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 693 | - 'handleRequestToggleCheckin', |
|
| 694 | - ], |
|
| 695 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 696 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 697 | - 'args' => [ |
|
| 698 | - 'force' => [ |
|
| 699 | - 'required' => false, |
|
| 700 | - 'default' => false, |
|
| 701 | - 'description' => __( |
|
| 702 | - // @codingStandardsIgnoreStart |
|
| 703 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 704 | - // @codingStandardsIgnoreEnd |
|
| 705 | - 'event_espresso' |
|
| 706 | - ), |
|
| 707 | - ], |
|
| 708 | - ], |
|
| 709 | - 'callback_args' => [$version], |
|
| 710 | - ], |
|
| 711 | - ]; |
|
| 712 | - return apply_filters( |
|
| 713 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 714 | - $this_versions_routes, |
|
| 715 | - $version, |
|
| 716 | - $hidden_endpoint |
|
| 717 | - ); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - |
|
| 721 | - /** |
|
| 722 | - * Gets the query params that can be used when request one or many |
|
| 723 | - * |
|
| 724 | - * @param EEM_Base $model |
|
| 725 | - * @param string $version |
|
| 726 | - * @return array |
|
| 727 | - */ |
|
| 728 | - protected function _get_response_selection_query_params(EEM_Base $model, $version, $single_only = false) |
|
| 729 | - { |
|
| 730 | - EED_Core_Rest_Api::loadCalculatedModelFields(); |
|
| 731 | - $query_params = [ |
|
| 732 | - 'include' => [ |
|
| 733 | - 'required' => false, |
|
| 734 | - 'default' => '*', |
|
| 735 | - 'type' => 'string', |
|
| 736 | - ], |
|
| 737 | - 'calculate' => [ |
|
| 738 | - 'required' => false, |
|
| 739 | - 'default' => '', |
|
| 740 | - 'enum' => EED_Core_Rest_Api::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 741 | - 'type' => 'string', |
|
| 742 | - // because we accept a CSV list of the enumerated strings, WP core validation and sanitization |
|
| 743 | - // freaks out. We'll just validate this argument while handling the request |
|
| 744 | - 'validate_callback' => null, |
|
| 745 | - 'sanitize_callback' => null, |
|
| 746 | - ], |
|
| 747 | - 'password' => [ |
|
| 748 | - 'required' => false, |
|
| 749 | - 'default' => '', |
|
| 750 | - 'type' => 'string', |
|
| 751 | - ], |
|
| 752 | - ]; |
|
| 753 | - return apply_filters( |
|
| 754 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 755 | - $query_params, |
|
| 756 | - $model, |
|
| 757 | - $version |
|
| 758 | - ); |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - |
|
| 762 | - /** |
|
| 763 | - * Gets the parameters acceptable for delete requests |
|
| 764 | - * |
|
| 765 | - * @param EEM_Base $model |
|
| 766 | - * @param string $version |
|
| 767 | - * @return array |
|
| 768 | - */ |
|
| 769 | - protected function _get_delete_query_params(EEM_Base $model, $version) |
|
| 770 | - { |
|
| 771 | - $params_for_delete = [ |
|
| 772 | - 'allow_blocking' => [ |
|
| 773 | - 'required' => false, |
|
| 774 | - 'default' => true, |
|
| 775 | - 'type' => 'boolean', |
|
| 776 | - ], |
|
| 777 | - ]; |
|
| 778 | - $params_for_delete['force'] = [ |
|
| 779 | - 'required' => false, |
|
| 780 | - 'default' => false, |
|
| 781 | - 'type' => 'boolean', |
|
| 782 | - ]; |
|
| 783 | - return apply_filters( |
|
| 784 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 785 | - $params_for_delete, |
|
| 786 | - $model, |
|
| 787 | - $version |
|
| 788 | - ); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - |
|
| 792 | - /** |
|
| 793 | - * @param EEM_Base $source_model |
|
| 794 | - * @param EEM_Base $related_model |
|
| 795 | - * @param $version |
|
| 796 | - * @return array |
|
| 797 | - * @throws EE_Error |
|
| 798 | - * @since $VID:$ |
|
| 799 | - */ |
|
| 800 | - protected function _get_add_relation_query_params(EEM_Base $source_model, EEM_Base $related_model, $version) |
|
| 801 | - { |
|
| 802 | - // if they're related through a HABTM relation, check for any non-FKs |
|
| 803 | - $all_relation_settings = $source_model->relation_settings(); |
|
| 804 | - $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 805 | - $params = []; |
|
| 806 | - if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
| 807 | - foreach ($relation_settings->getNonKeyFields() as $field) { |
|
| 808 | - /* @var $field EE_Model_Field_Base */ |
|
| 809 | - $params[ $field->get_name() ] = [ |
|
| 810 | - 'required' => ! $field->is_nullable(), |
|
| 811 | - 'default' => ModelDataTranslator::prepareFieldValueForJson( |
|
| 812 | - $field, |
|
| 813 | - $field->get_default_value(), |
|
| 814 | - $version |
|
| 815 | - ), |
|
| 816 | - 'type' => $field->getSchemaType(), |
|
| 817 | - 'validate_callback' => null, |
|
| 818 | - 'sanitize_callback' => null, |
|
| 819 | - ]; |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - return $params; |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * Gets info about reading query params that are acceptable |
|
| 828 | - * |
|
| 829 | - * @param EEM_Base $model eg 'Event' or 'Venue' |
|
| 830 | - * @param string $version |
|
| 831 | - * @return array describing the args acceptable when querying this model |
|
| 832 | - * @throws EE_Error |
|
| 833 | - */ |
|
| 834 | - protected function _get_read_query_params(EEM_Base $model, $version) |
|
| 835 | - { |
|
| 836 | - $default_orderby = []; |
|
| 837 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 838 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 839 | - } |
|
| 840 | - return array_merge( |
|
| 841 | - $this->_get_response_selection_query_params($model, $version), |
|
| 842 | - [ |
|
| 843 | - 'where' => [ |
|
| 844 | - 'required' => false, |
|
| 845 | - 'default' => [], |
|
| 846 | - 'type' => 'object', |
|
| 847 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 848 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 849 | - // while handling the request |
|
| 850 | - 'validate_callback' => null, |
|
| 851 | - 'sanitize_callback' => null, |
|
| 852 | - ], |
|
| 853 | - 'limit' => [ |
|
| 854 | - 'required' => false, |
|
| 855 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 856 | - 'type' => [ |
|
| 857 | - 'array', |
|
| 858 | - 'string', |
|
| 859 | - 'integer', |
|
| 860 | - ], |
|
| 861 | - // because we accept a variety of types, WP core validation and sanitization |
|
| 862 | - // freaks out. We'll just validate this argument while handling the request |
|
| 863 | - 'validate_callback' => null, |
|
| 864 | - 'sanitize_callback' => null, |
|
| 865 | - ], |
|
| 866 | - 'order_by' => [ |
|
| 867 | - 'required' => false, |
|
| 868 | - 'default' => $default_orderby, |
|
| 869 | - 'type' => [ |
|
| 870 | - 'object', |
|
| 871 | - 'string', |
|
| 872 | - ],// because we accept a variety of types, WP core validation and sanitization |
|
| 873 | - // freaks out. We'll just validate this argument while handling the request |
|
| 874 | - 'validate_callback' => null, |
|
| 875 | - 'sanitize_callback' => null, |
|
| 876 | - ], |
|
| 877 | - 'group_by' => [ |
|
| 878 | - 'required' => false, |
|
| 879 | - 'default' => null, |
|
| 880 | - 'type' => [ |
|
| 881 | - 'object', |
|
| 882 | - 'string', |
|
| 883 | - ], |
|
| 884 | - // because we accept an almost infinite list of possible groupings, |
|
| 885 | - // WP core validation and sanitization |
|
| 886 | - // freaks out. We'll just validate this argument while handling the request |
|
| 887 | - 'validate_callback' => null, |
|
| 888 | - 'sanitize_callback' => null, |
|
| 889 | - ], |
|
| 890 | - 'having' => [ |
|
| 891 | - 'required' => false, |
|
| 892 | - 'default' => null, |
|
| 893 | - 'type' => 'object', |
|
| 894 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 895 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 896 | - // while handling the request |
|
| 897 | - 'validate_callback' => null, |
|
| 898 | - 'sanitize_callback' => null, |
|
| 899 | - ], |
|
| 900 | - 'caps' => [ |
|
| 901 | - 'required' => false, |
|
| 902 | - 'default' => EEM_Base::caps_read, |
|
| 903 | - 'type' => 'string', |
|
| 904 | - 'enum' => [ |
|
| 905 | - EEM_Base::caps_read, |
|
| 906 | - EEM_Base::caps_read_admin, |
|
| 907 | - EEM_Base::caps_edit, |
|
| 908 | - EEM_Base::caps_delete, |
|
| 909 | - ], |
|
| 910 | - ], |
|
| 911 | - ] |
|
| 912 | - ); |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * Gets parameter information for a model regarding writing data |
|
| 918 | - * |
|
| 919 | - * @param string $model_name |
|
| 920 | - * @param ModelVersionInfo $model_version_info |
|
| 921 | - * @param boolean $create whether this is for request to create (in |
|
| 922 | - * which case we need all required params) or |
|
| 923 | - * just to update (in which case we don't |
|
| 924 | - * need those on every request) |
|
| 925 | - * @return array |
|
| 926 | - * @throws EE_Error |
|
| 927 | - * @throws ReflectionException |
|
| 928 | - */ |
|
| 929 | - protected function _get_write_params( |
|
| 930 | - $model_name, |
|
| 931 | - ModelVersionInfo $model_version_info, |
|
| 932 | - $create = false |
|
| 933 | - ) { |
|
| 934 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 935 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 936 | - |
|
| 937 | - // we do our own validation and sanitization within the controller |
|
| 938 | - $sanitize_callback = function_exists('rest_validate_value_from_schema') |
|
| 939 | - ? ['EED_Core_Rest_Api', 'default_sanitize_callback'] |
|
| 940 | - : null; |
|
| 941 | - $args_info = []; |
|
| 942 | - foreach ($fields as $field_name => $field_obj) { |
|
| 943 | - if ($field_obj->is_auto_increment()) { |
|
| 944 | - // totally ignore auto increment IDs |
|
| 945 | - continue; |
|
| 946 | - } |
|
| 947 | - $arg_info = $field_obj->getSchema(); |
|
| 948 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 949 | - $arg_info['required'] = $required; |
|
| 950 | - // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 951 | - unset($arg_info['readonly']); |
|
| 952 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
| 953 | - if (isset($schema_properties['raw']) |
|
| 954 | - && $field_obj->getSchemaType() === 'object' |
|
| 955 | - ) { |
|
| 956 | - // if there's a "raw" form of this argument, use those properties instead |
|
| 957 | - $arg_info = array_replace( |
|
| 958 | - $arg_info, |
|
| 959 | - $schema_properties['raw'] |
|
| 960 | - ); |
|
| 961 | - } |
|
| 962 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 963 | - $field_obj, |
|
| 964 | - $field_obj->get_default_value(), |
|
| 965 | - $model_version_info->requestedVersion() |
|
| 966 | - ); |
|
| 967 | - $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 968 | - $args_info[ $field_name ] = $arg_info; |
|
| 969 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 970 | - $gmt_arg_info = $arg_info; |
|
| 971 | - $gmt_arg_info['description'] = sprintf( |
|
| 972 | - esc_html__( |
|
| 973 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 974 | - 'event_espresso' |
|
| 975 | - ), |
|
| 976 | - $field_obj->get_nicename(), |
|
| 977 | - $field_name |
|
| 978 | - ); |
|
| 979 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 980 | - } |
|
| 981 | - } |
|
| 982 | - return $args_info; |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - |
|
| 986 | - /** |
|
| 987 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 988 | - * If the value is blank but not required, don't bother validating it. |
|
| 989 | - * Also, it uses our email validation instead of WP API's default. |
|
| 990 | - * |
|
| 991 | - * @param $value |
|
| 992 | - * @param WP_REST_Request $request |
|
| 993 | - * @param $param |
|
| 994 | - * @return bool|true|WP_Error |
|
| 995 | - * @throws InvalidArgumentException |
|
| 996 | - * @throws InvalidInterfaceException |
|
| 997 | - * @throws InvalidDataTypeException |
|
| 998 | - */ |
|
| 999 | - public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 1000 | - { |
|
| 1001 | - $attributes = $request->get_attributes(); |
|
| 1002 | - if (! isset($attributes['args'][ $param ]) |
|
| 1003 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 1004 | - $validation_result = true; |
|
| 1005 | - } else { |
|
| 1006 | - $args = $attributes['args'][ $param ]; |
|
| 1007 | - if (( |
|
| 1008 | - $value === '' |
|
| 1009 | - || $value === null |
|
| 1010 | - ) |
|
| 1011 | - && (! isset($args['required']) |
|
| 1012 | - || $args['required'] === false |
|
| 1013 | - ) |
|
| 1014 | - ) { |
|
| 1015 | - // not required and not provided? that's cool |
|
| 1016 | - $validation_result = true; |
|
| 1017 | - } elseif (isset($args['format']) |
|
| 1018 | - && $args['format'] === 'email' |
|
| 1019 | - ) { |
|
| 1020 | - $validation_result = true; |
|
| 1021 | - if (! EED_Core_Rest_Api::_validate_email($value)) { |
|
| 1022 | - $validation_result = new WP_Error( |
|
| 1023 | - 'rest_invalid_param', |
|
| 1024 | - esc_html__( |
|
| 1025 | - 'The email address is not valid or does not exist.', |
|
| 1026 | - 'event_espresso' |
|
| 1027 | - ) |
|
| 1028 | - ); |
|
| 1029 | - } |
|
| 1030 | - } else { |
|
| 1031 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 1032 | - } |
|
| 1033 | - } |
|
| 1034 | - if (is_wp_error($validation_result)) { |
|
| 1035 | - return $validation_result; |
|
| 1036 | - } |
|
| 1037 | - return rest_sanitize_request_arg($value, $request, $param); |
|
| 1038 | - } |
|
| 1039 | - |
|
| 1040 | - |
|
| 1041 | - /** |
|
| 1042 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 1043 | - * |
|
| 1044 | - * @param $email |
|
| 1045 | - * @return bool |
|
| 1046 | - * @throws InvalidArgumentException |
|
| 1047 | - * @throws InvalidInterfaceException |
|
| 1048 | - * @throws InvalidDataTypeException |
|
| 1049 | - */ |
|
| 1050 | - protected static function _validate_email($email) |
|
| 1051 | - { |
|
| 1052 | - try { |
|
| 1053 | - EmailAddressFactory::create($email); |
|
| 1054 | - return true; |
|
| 1055 | - } catch (EmailValidationException $e) { |
|
| 1056 | - return false; |
|
| 1057 | - } |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * Gets routes for the config |
|
| 1063 | - * |
|
| 1064 | - * @return array @see _register_model_routes |
|
| 1065 | - * @deprecated since version 4.9.1 |
|
| 1066 | - */ |
|
| 1067 | - protected function _register_config_routes() |
|
| 1068 | - { |
|
| 1069 | - $config_routes = []; |
|
| 1070 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 1071 | - $config_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1072 | - $version, |
|
| 1073 | - $hidden_endpoint |
|
| 1074 | - ); |
|
| 1075 | - } |
|
| 1076 | - return $config_routes; |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - |
|
| 1080 | - /** |
|
| 1081 | - * Gets routes for the config for the specified version |
|
| 1082 | - * |
|
| 1083 | - * @param string $version |
|
| 1084 | - * @param boolean $hidden_endpoint |
|
| 1085 | - * @return array |
|
| 1086 | - */ |
|
| 1087 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1088 | - { |
|
| 1089 | - return [ |
|
| 1090 | - 'config' => [ |
|
| 1091 | - [ |
|
| 1092 | - 'callback' => [ |
|
| 1093 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1094 | - 'handleRequest', |
|
| 1095 | - ], |
|
| 1096 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1097 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1098 | - 'callback_args' => [$version], |
|
| 1099 | - ], |
|
| 1100 | - ], |
|
| 1101 | - 'site_info' => [ |
|
| 1102 | - [ |
|
| 1103 | - 'callback' => [ |
|
| 1104 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1105 | - 'handleRequestSiteInfo', |
|
| 1106 | - ], |
|
| 1107 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1108 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1109 | - 'callback_args' => [$version], |
|
| 1110 | - ], |
|
| 1111 | - ], |
|
| 1112 | - ]; |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - |
|
| 1116 | - /** |
|
| 1117 | - * Gets the meta info routes |
|
| 1118 | - * |
|
| 1119 | - * @return array @see _register_model_routes |
|
| 1120 | - * @deprecated since version 4.9.1 |
|
| 1121 | - */ |
|
| 1122 | - protected function _register_meta_routes() |
|
| 1123 | - { |
|
| 1124 | - $meta_routes = []; |
|
| 1125 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 1126 | - $meta_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1127 | - $version, |
|
| 1128 | - $hidden_endpoint |
|
| 1129 | - ); |
|
| 1130 | - } |
|
| 1131 | - return $meta_routes; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - |
|
| 1135 | - /** |
|
| 1136 | - * @param string $version |
|
| 1137 | - * @param boolean $hidden_endpoint |
|
| 1138 | - * @return array |
|
| 1139 | - */ |
|
| 1140 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1141 | - { |
|
| 1142 | - return [ |
|
| 1143 | - 'resources' => [ |
|
| 1144 | - [ |
|
| 1145 | - 'callback' => [ |
|
| 1146 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1147 | - 'handleRequestModelsMeta', |
|
| 1148 | - ], |
|
| 1149 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1150 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1151 | - 'callback_args' => [$version], |
|
| 1152 | - ], |
|
| 1153 | - ], |
|
| 1154 | - ]; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - |
|
| 1158 | - /** |
|
| 1159 | - * Tries to hide old 4.6 endpoints from the |
|
| 1160 | - * |
|
| 1161 | - * @param array $route_data |
|
| 1162 | - * @return array |
|
| 1163 | - * @throws EE_Error |
|
| 1164 | - * @throws ReflectionException |
|
| 1165 | - */ |
|
| 1166 | - public static function hide_old_endpoints($route_data) |
|
| 1167 | - { |
|
| 1168 | - // allow API clients to override which endpoints get hidden, in case |
|
| 1169 | - // they want to discover particular endpoints |
|
| 1170 | - // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1171 | - $force_show_ee_namespace = ltrim( |
|
| 1172 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1173 | - '/' |
|
| 1174 | - ); |
|
| 1175 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1176 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1177 | - foreach ($endpoints as $key => $endpoint) { |
|
| 1178 | - // skip schema and other route options |
|
| 1179 | - if (! is_numeric($key)) { |
|
| 1180 | - continue; |
|
| 1181 | - } |
|
| 1182 | - // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1183 | - // to $force_show_ee_namespace, in which case only show that one |
|
| 1184 | - // namespace's endpoints (and hide all others) |
|
| 1185 | - if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1186 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1187 | - ) { |
|
| 1188 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1189 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1190 | - unset($route_data[ $full_route ]); |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - } |
|
| 1194 | - } |
|
| 1195 | - return $route_data; |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - |
|
| 1199 | - /** |
|
| 1200 | - * Returns an array describing which versions of core support serving requests for. |
|
| 1201 | - * Keys are core versions' major and minor version, and values are the |
|
| 1202 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1203 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1204 | - * the answers table entirely, in which case it would be very difficult for |
|
| 1205 | - * it to serve 4.6-style responses. |
|
| 1206 | - * Versions of core that are missing from this array are unknowns. |
|
| 1207 | - * previous ver |
|
| 1208 | - * |
|
| 1209 | - * @return array |
|
| 1210 | - */ |
|
| 1211 | - public static function version_compatibilities() |
|
| 1212 | - { |
|
| 1213 | - return apply_filters( |
|
| 1214 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1215 | - [ |
|
| 1216 | - '4.8.29' => '4.8.29', |
|
| 1217 | - '4.8.33' => '4.8.29', |
|
| 1218 | - '4.8.34' => '4.8.29', |
|
| 1219 | - '4.8.36' => '4.8.29', |
|
| 1220 | - ] |
|
| 1221 | - ); |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - |
|
| 1225 | - /** |
|
| 1226 | - * Gets the latest API version served. Eg if there |
|
| 1227 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1228 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1229 | - * |
|
| 1230 | - * @return string |
|
| 1231 | - */ |
|
| 1232 | - public static function latest_rest_api_version() |
|
| 1233 | - { |
|
| 1234 | - $versions_served = EED_Core_Rest_Api::versions_served(); |
|
| 1235 | - $versions_served_keys = array_keys($versions_served); |
|
| 1236 | - return end($versions_served_keys); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - |
|
| 1240 | - /** |
|
| 1241 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1242 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1243 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1244 | - * We also indicate whether or not this version should be put in the index or not |
|
| 1245 | - * |
|
| 1246 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1247 | - * are whether or not they should be hidden |
|
| 1248 | - */ |
|
| 1249 | - public static function versions_served() |
|
| 1250 | - { |
|
| 1251 | - $versions_served = []; |
|
| 1252 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1253 | - $lowest_compatible_version = end($possibly_served_versions); |
|
| 1254 | - reset($possibly_served_versions); |
|
| 1255 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1256 | - $latest_version = end($versions_served_historically); |
|
| 1257 | - reset($versions_served_historically); |
|
| 1258 | - // for each version of core we have ever served: |
|
| 1259 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1260 | - // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1261 | - |
|
| 1262 | - if ($key_versioned_endpoint === $latest_version) { |
|
| 1263 | - // don't hide the latest version in the index |
|
| 1264 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1265 | - } elseif (version_compare($key_versioned_endpoint, $lowest_compatible_version, '>=') |
|
| 1266 | - && version_compare($key_versioned_endpoint, EED_Core_Rest_Api::core_version(), '<') |
|
| 1267 | - ) { |
|
| 1268 | - // include, but hide, previous versions which are still supported |
|
| 1269 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1270 | - } elseif (apply_filters( |
|
| 1271 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1272 | - false, |
|
| 1273 | - $possibly_served_versions |
|
| 1274 | - )) { |
|
| 1275 | - // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1276 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1277 | - } |
|
| 1278 | - } |
|
| 1279 | - return $versions_served; |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - |
|
| 1283 | - /** |
|
| 1284 | - * Gets the major and minor version of EE core's version string |
|
| 1285 | - * |
|
| 1286 | - * @return string |
|
| 1287 | - */ |
|
| 1288 | - public static function core_version() |
|
| 1289 | - { |
|
| 1290 | - return apply_filters( |
|
| 1291 | - 'FHEE__EED_Core_REST_API__core_version', |
|
| 1292 | - implode( |
|
| 1293 | - '.', |
|
| 1294 | - array_slice( |
|
| 1295 | - explode( |
|
| 1296 | - '.', |
|
| 1297 | - espresso_version() |
|
| 1298 | - ), |
|
| 1299 | - 0, |
|
| 1300 | - 3 |
|
| 1301 | - ) |
|
| 1302 | - ) |
|
| 1303 | - ); |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * Gets the default limit that should be used when querying for resources |
|
| 1309 | - * |
|
| 1310 | - * @return int |
|
| 1311 | - */ |
|
| 1312 | - public static function get_default_query_limit() |
|
| 1313 | - { |
|
| 1314 | - // we actually don't use a const because we want folks to always use |
|
| 1315 | - // this method, not the const directly |
|
| 1316 | - return apply_filters( |
|
| 1317 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1318 | - 50 |
|
| 1319 | - ); |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - |
|
| 1323 | - /** |
|
| 1324 | - * @param string $version api version string (i.e. '4.8.36') |
|
| 1325 | - * @return array |
|
| 1326 | - */ |
|
| 1327 | - public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1328 | - { |
|
| 1329 | - $version = empty($version) ? EED_Core_Rest_Api::latest_rest_api_version() : $version; |
|
| 1330 | - $model_names = EED_Core_Rest_Api::model_names_with_plural_routes($version); |
|
| 1331 | - $collection_routes = []; |
|
| 1332 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1333 | - $collection_routes[ strtolower($model_name) ] = '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' |
|
| 1334 | - . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1335 | - } |
|
| 1336 | - return $collection_routes; |
|
| 1337 | - } |
|
| 1338 | - |
|
| 1339 | - |
|
| 1340 | - /** |
|
| 1341 | - * Returns an array of primary key names indexed by model names. |
|
| 1342 | - * |
|
| 1343 | - * @param string $version |
|
| 1344 | - * @return array |
|
| 1345 | - */ |
|
| 1346 | - public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1347 | - { |
|
| 1348 | - $version = empty($version) ? EED_Core_Rest_Api::latest_rest_api_version() : $version; |
|
| 1349 | - $model_names = EED_Core_Rest_Api::model_names_with_plural_routes($version); |
|
| 1350 | - $primary_key_items = []; |
|
| 1351 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1352 | - $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1353 | - foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1354 | - if (count($primary_keys) > 1) { |
|
| 1355 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1356 | - } else { |
|
| 1357 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1358 | - } |
|
| 1359 | - } |
|
| 1360 | - } |
|
| 1361 | - return $primary_key_items; |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * Determines the EE REST API debug mode is activated, or not. |
|
| 1367 | - * |
|
| 1368 | - * @return bool |
|
| 1369 | - * @since 4.9.76.p |
|
| 1370 | - */ |
|
| 1371 | - public static function debugMode() |
|
| 1372 | - { |
|
| 1373 | - static $debug_mode = null; // could be class prop |
|
| 1374 | - if ($debug_mode === null) { |
|
| 1375 | - $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
| 1376 | - } |
|
| 1377 | - return $debug_mode; |
|
| 1378 | - } |
|
| 1379 | - |
|
| 1380 | - |
|
| 1381 | - /** |
|
| 1382 | - * run - initial module setup |
|
| 1383 | - * |
|
| 1384 | - * @access public |
|
| 1385 | - * @param WP $WP |
|
| 1386 | - * @return void |
|
| 1387 | - */ |
|
| 1388 | - public function run($WP) |
|
| 1389 | - { |
|
| 1390 | - } |
|
| 25 | + const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 26 | + |
|
| 27 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 28 | + |
|
| 29 | + const saved_routes_option_names = 'ee_core_routes'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * string used in _links response bodies to make them globally unique. |
|
| 33 | + * |
|
| 34 | + * @see http://v2.wp-api.org/extending/linking/ |
|
| 35 | + */ |
|
| 36 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var CalculatedModelFields |
|
| 40 | + */ |
|
| 41 | + protected static $_field_calculator; |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return EED_Core_Rest_Api|EED_Module |
|
| 46 | + */ |
|
| 47 | + public static function instance() |
|
| 48 | + { |
|
| 49 | + return parent::get_instance(EED_Core_Rest_Api::class); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 55 | + * |
|
| 56 | + * @access public |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public static function set_hooks() |
|
| 60 | + { |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 66 | + * |
|
| 67 | + * @access public |
|
| 68 | + * @return void |
|
| 69 | + */ |
|
| 70 | + public static function set_hooks_admin() |
|
| 71 | + { |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + public static function set_hooks_both() |
|
| 76 | + { |
|
| 77 | + add_action('rest_api_init', ['EED_Core_Rest_Api', 'set_hooks_rest_api'], 5); |
|
| 78 | + add_action('rest_api_init', ['EED_Core_Rest_Api', 'register_routes'], 10); |
|
| 79 | + add_filter('rest_route_data', ['EED_Core_Rest_Api', 'hide_old_endpoints'], 10, 2); |
|
| 80 | + add_filter( |
|
| 81 | + 'rest_index', |
|
| 82 | + ['EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex'] |
|
| 83 | + ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @since $VID:$ |
|
| 89 | + */ |
|
| 90 | + public static function loadCalculatedModelFields() |
|
| 91 | + { |
|
| 92 | + EED_Core_Rest_Api::$_field_calculator = LoaderFactory::getLoader()->load( |
|
| 93 | + 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' |
|
| 94 | + ); |
|
| 95 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * sets up hooks which only need to be included as part of REST API requests; |
|
| 101 | + * other requests like to the frontend or admin etc don't need them |
|
| 102 | + * |
|
| 103 | + * @throws EE_Error |
|
| 104 | + */ |
|
| 105 | + public static function set_hooks_rest_api() |
|
| 106 | + { |
|
| 107 | + // set hooks which account for changes made to the API |
|
| 108 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * public wrapper of _set_hooks_for_changes. |
|
| 114 | + * Loads all the hooks which make requests to old versions of the API |
|
| 115 | + * appear the same as they always did |
|
| 116 | + * |
|
| 117 | + * @throws EE_Error |
|
| 118 | + */ |
|
| 119 | + public static function set_hooks_for_changes() |
|
| 120 | + { |
|
| 121 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Loads all the hooks which make requests to old versions of the API |
|
| 127 | + * appear the same as they always did |
|
| 128 | + * |
|
| 129 | + * @throws EE_Error |
|
| 130 | + */ |
|
| 131 | + protected static function _set_hooks_for_changes() |
|
| 132 | + { |
|
| 133 | + $folder_contents = EEH_File::get_contents_of_folders([EE_LIBRARIES . 'rest_api/changes'], false); |
|
| 134 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 135 | + // ignore the base parent class |
|
| 136 | + // and legacy named classes |
|
| 137 | + if ($classname_in_namespace === 'ChangesInBase' |
|
| 138 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 139 | + ) { |
|
| 140 | + continue; |
|
| 141 | + } |
|
| 142 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 143 | + if (class_exists($full_classname)) { |
|
| 144 | + $instance_of_class = new $full_classname; |
|
| 145 | + if ($instance_of_class instanceof ChangesInBase) { |
|
| 146 | + $instance_of_class->setHooks(); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 155 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 156 | + * |
|
| 157 | + * @throws EE_Error |
|
| 158 | + * @throws ReflectionException |
|
| 159 | + */ |
|
| 160 | + public static function register_routes() |
|
| 161 | + { |
|
| 162 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 163 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 164 | + /** |
|
| 165 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 166 | + * but can also contain route options like { |
|
| 167 | + * @type array $schema { |
|
| 168 | + * @type callable $schema_callback |
|
| 169 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 170 | + * WP_REST_Request of course |
|
| 171 | + * } |
|
| 172 | + * } |
|
| 173 | + */ |
|
| 174 | + // when registering routes, register all the endpoints' data at the same time |
|
| 175 | + $multiple_endpoint_args = []; |
|
| 176 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 177 | + /** |
|
| 178 | + * @var array $data_for_single_endpoint { |
|
| 179 | + * @type callable $callback |
|
| 180 | + * @type string methods |
|
| 181 | + * @type array args |
|
| 182 | + * @type array _links |
|
| 183 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 184 | + * WP_REST_Request of course |
|
| 185 | + * } |
|
| 186 | + */ |
|
| 187 | + // skip route options |
|
| 188 | + if (! is_numeric($endpoint_key)) { |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 192 | + throw new EE_Error( |
|
| 193 | + esc_html__( |
|
| 194 | + // @codingStandardsIgnoreStart |
|
| 195 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 196 | + // @codingStandardsIgnoreEnd |
|
| 197 | + 'event_espresso' |
|
| 198 | + ) |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + $callback = $data_for_single_endpoint['callback']; |
|
| 202 | + $single_endpoint_args = [ |
|
| 203 | + 'methods' => $data_for_single_endpoint['methods'], |
|
| 204 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 205 | + : [], |
|
| 206 | + ]; |
|
| 207 | + if (isset($data_for_single_endpoint['_links'])) { |
|
| 208 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 209 | + } |
|
| 210 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 211 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 212 | + $single_endpoint_args['callback'] = static function (WP_REST_Request $request) use ( |
|
| 213 | + $callback, |
|
| 214 | + $callback_args |
|
| 215 | + ) { |
|
| 216 | + array_unshift($callback_args, $request); |
|
| 217 | + return call_user_func_array( |
|
| 218 | + $callback, |
|
| 219 | + $callback_args |
|
| 220 | + ); |
|
| 221 | + }; |
|
| 222 | + } else { |
|
| 223 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 224 | + } |
|
| 225 | + // As of WordPress 5.5, if a permission_callback is not provided, |
|
| 226 | + // the REST API will issue a _doing_it_wrong notice. |
|
| 227 | + // Since the EE REST API defers capabilities to the db model system, |
|
| 228 | + // we will just use the generic WP callback for public endpoints |
|
| 229 | + if (! isset($single_endpoint_args['permission_callback'])) { |
|
| 230 | + $single_endpoint_args['permission_callback'] = '__return_true'; |
|
| 231 | + } |
|
| 232 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 233 | + } |
|
| 234 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 235 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 236 | + $schema_callback = $schema_route_data['schema_callback']; |
|
| 237 | + $callback_args = $schema_route_data['callback_args']; |
|
| 238 | + $multiple_endpoint_args['schema'] = static function () use ($schema_callback, $callback_args) { |
|
| 239 | + return call_user_func_array( |
|
| 240 | + $schema_callback, |
|
| 241 | + $callback_args |
|
| 242 | + ); |
|
| 243 | + }; |
|
| 244 | + } |
|
| 245 | + register_rest_route( |
|
| 246 | + $namespace, |
|
| 247 | + $relative_route, |
|
| 248 | + $multiple_endpoint_args |
|
| 249 | + ); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Checks if there was a version change or something that merits invalidating the cached |
|
| 257 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 258 | + * next time the WP API is used |
|
| 259 | + */ |
|
| 260 | + public static function invalidate_cached_route_data_on_version_change() |
|
| 261 | + { |
|
| 262 | + if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 263 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 264 | + } |
|
| 265 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 266 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 267 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 275 | + */ |
|
| 276 | + public static function invalidate_cached_route_data() |
|
| 277 | + { |
|
| 278 | + // delete the saved EE REST API routes |
|
| 279 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 280 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Gets the EE route data |
|
| 287 | + * |
|
| 288 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 289 | + * @throws EE_Error |
|
| 290 | + * @throws ReflectionException |
|
| 291 | + * @type string|array $callback |
|
| 292 | + * @type string $methods |
|
| 293 | + * @type boolean $hidden_endpoint |
|
| 294 | + * } |
|
| 295 | + */ |
|
| 296 | + public static function get_ee_route_data() |
|
| 297 | + { |
|
| 298 | + $ee_routes = []; |
|
| 299 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoints) { |
|
| 300 | + $ee_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = EED_Core_Rest_Api::_get_ee_route_data_for_version( |
|
| 301 | + $version, |
|
| 302 | + $hidden_endpoints |
|
| 303 | + ); |
|
| 304 | + } |
|
| 305 | + return $ee_routes; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Gets the EE route data from the wp options if it exists already, |
|
| 311 | + * otherwise re-generates it and saves it to the option |
|
| 312 | + * |
|
| 313 | + * @param string $version |
|
| 314 | + * @param boolean $hidden_endpoints |
|
| 315 | + * @return array |
|
| 316 | + * @throws EE_Error |
|
| 317 | + * @throws ReflectionException |
|
| 318 | + */ |
|
| 319 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 320 | + { |
|
| 321 | + $ee_routes = get_option(EED_Core_Rest_Api::saved_routes_option_names . $version, null); |
|
| 322 | + if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 323 | + $ee_routes = EED_Core_Rest_Api::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 324 | + } |
|
| 325 | + return $ee_routes; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Saves the EE REST API route data to a wp option and returns it |
|
| 331 | + * |
|
| 332 | + * @param string $version |
|
| 333 | + * @param boolean $hidden_endpoints |
|
| 334 | + * @return mixed|null |
|
| 335 | + * @throws EE_Error |
|
| 336 | + * @throws ReflectionException |
|
| 337 | + */ |
|
| 338 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 339 | + { |
|
| 340 | + $instance = EED_Core_Rest_Api::instance(); |
|
| 341 | + $routes = apply_filters( |
|
| 342 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 343 | + array_replace_recursive( |
|
| 344 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 345 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 346 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 347 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 348 | + ) |
|
| 349 | + ); |
|
| 350 | + $option_name = EED_Core_Rest_Api::saved_routes_option_names . $version; |
|
| 351 | + if (get_option($option_name)) { |
|
| 352 | + update_option($option_name, $routes, true); |
|
| 353 | + } else { |
|
| 354 | + add_option($option_name, $routes, null, 'no'); |
|
| 355 | + } |
|
| 356 | + return $routes; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 362 | + * need to calculate it on every request |
|
| 363 | + * |
|
| 364 | + * @return void |
|
| 365 | + * @deprecated since version 4.9.1 |
|
| 366 | + */ |
|
| 367 | + public static function save_ee_routes() |
|
| 368 | + { |
|
| 369 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 370 | + $instance = EED_Core_Rest_Api::instance(); |
|
| 371 | + $routes = apply_filters( |
|
| 372 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 373 | + array_replace_recursive( |
|
| 374 | + $instance->_register_config_routes(), |
|
| 375 | + $instance->_register_meta_routes(), |
|
| 376 | + $instance->_register_model_routes(), |
|
| 377 | + $instance->_register_rpc_routes() |
|
| 378 | + ) |
|
| 379 | + ); |
|
| 380 | + update_option(EED_Core_Rest_Api::saved_routes_option_names, $routes, true); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Gets all the route information relating to EE models |
|
| 387 | + * |
|
| 388 | + * @return array @see get_ee_route_data |
|
| 389 | + * @deprecated since version 4.9.1 |
|
| 390 | + */ |
|
| 391 | + protected function _register_model_routes() |
|
| 392 | + { |
|
| 393 | + $model_routes = []; |
|
| 394 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 395 | + $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 396 | + . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 397 | + } |
|
| 398 | + return $model_routes; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Decides whether or not to add write endpoints for this model. |
|
| 404 | + * Currently, this defaults to exclude all global tables and models |
|
| 405 | + * which would allow inserting WP core data (we don't want to duplicate |
|
| 406 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 407 | + * |
|
| 408 | + * @param EEM_Base $model |
|
| 409 | + * @return bool |
|
| 410 | + */ |
|
| 411 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
| 412 | + { |
|
| 413 | + if ($model->is_wp_core_model()) { |
|
| 414 | + return false; |
|
| 415 | + } |
|
| 416 | + foreach ($model->get_tables() as $table) { |
|
| 417 | + if ($table->is_global()) { |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + return true; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 427 | + * in this versioned namespace of EE4 |
|
| 428 | + * |
|
| 429 | + * @param $version |
|
| 430 | + * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 431 | + */ |
|
| 432 | + public static function model_names_with_plural_routes($version) |
|
| 433 | + { |
|
| 434 | + $model_version_info = new ModelVersionInfo($version); |
|
| 435 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 436 | + // let's not bother having endpoints for extra metas |
|
| 437 | + unset( |
|
| 438 | + $models_to_register['Extra_Meta'], |
|
| 439 | + $models_to_register['Extra_Join'], |
|
| 440 | + $models_to_register['Post_Meta'] |
|
| 441 | + ); |
|
| 442 | + return apply_filters( |
|
| 443 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 444 | + $models_to_register |
|
| 445 | + ); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Gets the route data for EE models in the specified version |
|
| 451 | + * |
|
| 452 | + * @param string $version |
|
| 453 | + * @param boolean $hidden_endpoint |
|
| 454 | + * @return array |
|
| 455 | + * @throws EE_Error |
|
| 456 | + * @throws ReflectionException |
|
| 457 | + */ |
|
| 458 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 459 | + { |
|
| 460 | + $model_routes = []; |
|
| 461 | + $model_version_info = new ModelVersionInfo($version); |
|
| 462 | + foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 463 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 464 | + // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 465 | + if (! $model instanceof EEM_Base) { |
|
| 466 | + continue; |
|
| 467 | + } |
|
| 468 | + // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 469 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 470 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 471 | + $model_routes[ $plural_model_route ] = [ |
|
| 472 | + [ |
|
| 473 | + 'callback' => [ |
|
| 474 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 475 | + 'handleRequestGetAll', |
|
| 476 | + ], |
|
| 477 | + 'callback_args' => [$version, $model_name], |
|
| 478 | + 'methods' => WP_REST_Server::READABLE, |
|
| 479 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 480 | + 'args' => $this->_get_read_query_params($model, $version), |
|
| 481 | + '_links' => [ |
|
| 482 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 483 | + ], |
|
| 484 | + ], |
|
| 485 | + 'schema' => [ |
|
| 486 | + 'schema_callback' => [ |
|
| 487 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 488 | + 'handleSchemaRequest', |
|
| 489 | + ], |
|
| 490 | + 'callback_args' => [$version, $model_name], |
|
| 491 | + ], |
|
| 492 | + ]; |
|
| 493 | + $model_routes[ $singular_model_route ] = [ |
|
| 494 | + [ |
|
| 495 | + 'callback' => [ |
|
| 496 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 497 | + 'handleRequestGetOne', |
|
| 498 | + ], |
|
| 499 | + 'callback_args' => [$version, $model_name], |
|
| 500 | + 'methods' => WP_REST_Server::READABLE, |
|
| 501 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 502 | + 'args' => $this->_get_response_selection_query_params($model, $version, true), |
|
| 503 | + ], |
|
| 504 | + ]; |
|
| 505 | + if (apply_filters( |
|
| 506 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 507 | + EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 508 | + $model |
|
| 509 | + )) { |
|
| 510 | + $model_routes[ $plural_model_route ][] = [ |
|
| 511 | + 'callback' => [ |
|
| 512 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 513 | + 'handleRequestInsert', |
|
| 514 | + ], |
|
| 515 | + 'callback_args' => [$version, $model_name], |
|
| 516 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 517 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 518 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 519 | + ]; |
|
| 520 | + $model_routes[ $singular_model_route ] = array_merge( |
|
| 521 | + $model_routes[ $singular_model_route ], |
|
| 522 | + [ |
|
| 523 | + [ |
|
| 524 | + 'callback' => [ |
|
| 525 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 526 | + 'handleRequestUpdate', |
|
| 527 | + ], |
|
| 528 | + 'callback_args' => [$version, $model_name], |
|
| 529 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 530 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 531 | + 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 532 | + ], |
|
| 533 | + [ |
|
| 534 | + 'callback' => [ |
|
| 535 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 536 | + 'handleRequestDelete', |
|
| 537 | + ], |
|
| 538 | + 'callback_args' => [$version, $model_name], |
|
| 539 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 540 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 541 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
| 542 | + ], |
|
| 543 | + ] |
|
| 544 | + ); |
|
| 545 | + } |
|
| 546 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 547 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 548 | + $model, |
|
| 549 | + '(?P<id>[^\/]+)', |
|
| 550 | + $relation_obj |
|
| 551 | + ); |
|
| 552 | + $model_routes[ $related_route ] = [ |
|
| 553 | + [ |
|
| 554 | + 'callback' => [ |
|
| 555 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 556 | + 'handleRequestGetRelated', |
|
| 557 | + ], |
|
| 558 | + 'callback_args' => [$version, $model_name, $relation_name], |
|
| 559 | + 'methods' => WP_REST_Server::READABLE, |
|
| 560 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 561 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 562 | + ], |
|
| 563 | + ]; |
|
| 564 | + |
|
| 565 | + $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 566 | + $model_routes[ $related_write_route ] = [ |
|
| 567 | + [ |
|
| 568 | + 'callback' => [ |
|
| 569 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 570 | + 'handleRequestAddRelation', |
|
| 571 | + ], |
|
| 572 | + 'callback_args' => [$version, $model_name, $relation_name], |
|
| 573 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 574 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 575 | + 'args' => $this->_get_add_relation_query_params( |
|
| 576 | + $model, |
|
| 577 | + $relation_obj->get_other_model(), |
|
| 578 | + $version |
|
| 579 | + ), |
|
| 580 | + ], |
|
| 581 | + [ |
|
| 582 | + 'callback' => [ |
|
| 583 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 584 | + 'handleRequestRemoveRelation', |
|
| 585 | + ], |
|
| 586 | + 'callback_args' => [$version, $model_name, $relation_name], |
|
| 587 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 588 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 589 | + 'args' => [], |
|
| 590 | + ], |
|
| 591 | + ]; |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + return $model_routes; |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 600 | + * excluding the preceding slash. |
|
| 601 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 602 | + * |
|
| 603 | + * @param EEM_Base $model |
|
| 604 | + * @return string |
|
| 605 | + */ |
|
| 606 | + public static function get_collection_route(EEM_Base $model) |
|
| 607 | + { |
|
| 608 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 614 | + * excluding the preceding slash. |
|
| 615 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 616 | + * |
|
| 617 | + * @param EEM_Base $model eg Event or Venue |
|
| 618 | + * @param string $id |
|
| 619 | + * @return string |
|
| 620 | + */ |
|
| 621 | + public static function get_entity_route($model, $id) |
|
| 622 | + { |
|
| 623 | + return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 629 | + * excluding the preceding slash. |
|
| 630 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 631 | + * |
|
| 632 | + * @param EEM_Base $model eg Event or Venue |
|
| 633 | + * @param string $id |
|
| 634 | + * @param EE_Model_Relation_Base $relation_obj |
|
| 635 | + * @return string |
|
| 636 | + */ |
|
| 637 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 638 | + { |
|
| 639 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 640 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
| 641 | + $relation_obj |
|
| 642 | + ); |
|
| 643 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 649 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 650 | + * |
|
| 651 | + * @param string $relative_route |
|
| 652 | + * @param string $version |
|
| 653 | + * @return string |
|
| 654 | + */ |
|
| 655 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 656 | + { |
|
| 657 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 663 | + * routes that don't conform to the traditional REST CRUD-style). |
|
| 664 | + * |
|
| 665 | + * @deprecated since 4.9.1 |
|
| 666 | + */ |
|
| 667 | + protected function _register_rpc_routes() |
|
| 668 | + { |
|
| 669 | + $routes = []; |
|
| 670 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 671 | + $routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 672 | + $version, |
|
| 673 | + $hidden_endpoint |
|
| 674 | + ); |
|
| 675 | + } |
|
| 676 | + return $routes; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * @param string $version |
|
| 682 | + * @param boolean $hidden_endpoint |
|
| 683 | + * @return array |
|
| 684 | + */ |
|
| 685 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 686 | + { |
|
| 687 | + $this_versions_routes = []; |
|
| 688 | + // checkin endpoint |
|
| 689 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = [ |
|
| 690 | + [ |
|
| 691 | + 'callback' => [ |
|
| 692 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 693 | + 'handleRequestToggleCheckin', |
|
| 694 | + ], |
|
| 695 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 696 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 697 | + 'args' => [ |
|
| 698 | + 'force' => [ |
|
| 699 | + 'required' => false, |
|
| 700 | + 'default' => false, |
|
| 701 | + 'description' => __( |
|
| 702 | + // @codingStandardsIgnoreStart |
|
| 703 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 704 | + // @codingStandardsIgnoreEnd |
|
| 705 | + 'event_espresso' |
|
| 706 | + ), |
|
| 707 | + ], |
|
| 708 | + ], |
|
| 709 | + 'callback_args' => [$version], |
|
| 710 | + ], |
|
| 711 | + ]; |
|
| 712 | + return apply_filters( |
|
| 713 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 714 | + $this_versions_routes, |
|
| 715 | + $version, |
|
| 716 | + $hidden_endpoint |
|
| 717 | + ); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + |
|
| 721 | + /** |
|
| 722 | + * Gets the query params that can be used when request one or many |
|
| 723 | + * |
|
| 724 | + * @param EEM_Base $model |
|
| 725 | + * @param string $version |
|
| 726 | + * @return array |
|
| 727 | + */ |
|
| 728 | + protected function _get_response_selection_query_params(EEM_Base $model, $version, $single_only = false) |
|
| 729 | + { |
|
| 730 | + EED_Core_Rest_Api::loadCalculatedModelFields(); |
|
| 731 | + $query_params = [ |
|
| 732 | + 'include' => [ |
|
| 733 | + 'required' => false, |
|
| 734 | + 'default' => '*', |
|
| 735 | + 'type' => 'string', |
|
| 736 | + ], |
|
| 737 | + 'calculate' => [ |
|
| 738 | + 'required' => false, |
|
| 739 | + 'default' => '', |
|
| 740 | + 'enum' => EED_Core_Rest_Api::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 741 | + 'type' => 'string', |
|
| 742 | + // because we accept a CSV list of the enumerated strings, WP core validation and sanitization |
|
| 743 | + // freaks out. We'll just validate this argument while handling the request |
|
| 744 | + 'validate_callback' => null, |
|
| 745 | + 'sanitize_callback' => null, |
|
| 746 | + ], |
|
| 747 | + 'password' => [ |
|
| 748 | + 'required' => false, |
|
| 749 | + 'default' => '', |
|
| 750 | + 'type' => 'string', |
|
| 751 | + ], |
|
| 752 | + ]; |
|
| 753 | + return apply_filters( |
|
| 754 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 755 | + $query_params, |
|
| 756 | + $model, |
|
| 757 | + $version |
|
| 758 | + ); |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + |
|
| 762 | + /** |
|
| 763 | + * Gets the parameters acceptable for delete requests |
|
| 764 | + * |
|
| 765 | + * @param EEM_Base $model |
|
| 766 | + * @param string $version |
|
| 767 | + * @return array |
|
| 768 | + */ |
|
| 769 | + protected function _get_delete_query_params(EEM_Base $model, $version) |
|
| 770 | + { |
|
| 771 | + $params_for_delete = [ |
|
| 772 | + 'allow_blocking' => [ |
|
| 773 | + 'required' => false, |
|
| 774 | + 'default' => true, |
|
| 775 | + 'type' => 'boolean', |
|
| 776 | + ], |
|
| 777 | + ]; |
|
| 778 | + $params_for_delete['force'] = [ |
|
| 779 | + 'required' => false, |
|
| 780 | + 'default' => false, |
|
| 781 | + 'type' => 'boolean', |
|
| 782 | + ]; |
|
| 783 | + return apply_filters( |
|
| 784 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 785 | + $params_for_delete, |
|
| 786 | + $model, |
|
| 787 | + $version |
|
| 788 | + ); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + |
|
| 792 | + /** |
|
| 793 | + * @param EEM_Base $source_model |
|
| 794 | + * @param EEM_Base $related_model |
|
| 795 | + * @param $version |
|
| 796 | + * @return array |
|
| 797 | + * @throws EE_Error |
|
| 798 | + * @since $VID:$ |
|
| 799 | + */ |
|
| 800 | + protected function _get_add_relation_query_params(EEM_Base $source_model, EEM_Base $related_model, $version) |
|
| 801 | + { |
|
| 802 | + // if they're related through a HABTM relation, check for any non-FKs |
|
| 803 | + $all_relation_settings = $source_model->relation_settings(); |
|
| 804 | + $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 805 | + $params = []; |
|
| 806 | + if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
| 807 | + foreach ($relation_settings->getNonKeyFields() as $field) { |
|
| 808 | + /* @var $field EE_Model_Field_Base */ |
|
| 809 | + $params[ $field->get_name() ] = [ |
|
| 810 | + 'required' => ! $field->is_nullable(), |
|
| 811 | + 'default' => ModelDataTranslator::prepareFieldValueForJson( |
|
| 812 | + $field, |
|
| 813 | + $field->get_default_value(), |
|
| 814 | + $version |
|
| 815 | + ), |
|
| 816 | + 'type' => $field->getSchemaType(), |
|
| 817 | + 'validate_callback' => null, |
|
| 818 | + 'sanitize_callback' => null, |
|
| 819 | + ]; |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + return $params; |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * Gets info about reading query params that are acceptable |
|
| 828 | + * |
|
| 829 | + * @param EEM_Base $model eg 'Event' or 'Venue' |
|
| 830 | + * @param string $version |
|
| 831 | + * @return array describing the args acceptable when querying this model |
|
| 832 | + * @throws EE_Error |
|
| 833 | + */ |
|
| 834 | + protected function _get_read_query_params(EEM_Base $model, $version) |
|
| 835 | + { |
|
| 836 | + $default_orderby = []; |
|
| 837 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 838 | + $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 839 | + } |
|
| 840 | + return array_merge( |
|
| 841 | + $this->_get_response_selection_query_params($model, $version), |
|
| 842 | + [ |
|
| 843 | + 'where' => [ |
|
| 844 | + 'required' => false, |
|
| 845 | + 'default' => [], |
|
| 846 | + 'type' => 'object', |
|
| 847 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 848 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 849 | + // while handling the request |
|
| 850 | + 'validate_callback' => null, |
|
| 851 | + 'sanitize_callback' => null, |
|
| 852 | + ], |
|
| 853 | + 'limit' => [ |
|
| 854 | + 'required' => false, |
|
| 855 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 856 | + 'type' => [ |
|
| 857 | + 'array', |
|
| 858 | + 'string', |
|
| 859 | + 'integer', |
|
| 860 | + ], |
|
| 861 | + // because we accept a variety of types, WP core validation and sanitization |
|
| 862 | + // freaks out. We'll just validate this argument while handling the request |
|
| 863 | + 'validate_callback' => null, |
|
| 864 | + 'sanitize_callback' => null, |
|
| 865 | + ], |
|
| 866 | + 'order_by' => [ |
|
| 867 | + 'required' => false, |
|
| 868 | + 'default' => $default_orderby, |
|
| 869 | + 'type' => [ |
|
| 870 | + 'object', |
|
| 871 | + 'string', |
|
| 872 | + ],// because we accept a variety of types, WP core validation and sanitization |
|
| 873 | + // freaks out. We'll just validate this argument while handling the request |
|
| 874 | + 'validate_callback' => null, |
|
| 875 | + 'sanitize_callback' => null, |
|
| 876 | + ], |
|
| 877 | + 'group_by' => [ |
|
| 878 | + 'required' => false, |
|
| 879 | + 'default' => null, |
|
| 880 | + 'type' => [ |
|
| 881 | + 'object', |
|
| 882 | + 'string', |
|
| 883 | + ], |
|
| 884 | + // because we accept an almost infinite list of possible groupings, |
|
| 885 | + // WP core validation and sanitization |
|
| 886 | + // freaks out. We'll just validate this argument while handling the request |
|
| 887 | + 'validate_callback' => null, |
|
| 888 | + 'sanitize_callback' => null, |
|
| 889 | + ], |
|
| 890 | + 'having' => [ |
|
| 891 | + 'required' => false, |
|
| 892 | + 'default' => null, |
|
| 893 | + 'type' => 'object', |
|
| 894 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 895 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 896 | + // while handling the request |
|
| 897 | + 'validate_callback' => null, |
|
| 898 | + 'sanitize_callback' => null, |
|
| 899 | + ], |
|
| 900 | + 'caps' => [ |
|
| 901 | + 'required' => false, |
|
| 902 | + 'default' => EEM_Base::caps_read, |
|
| 903 | + 'type' => 'string', |
|
| 904 | + 'enum' => [ |
|
| 905 | + EEM_Base::caps_read, |
|
| 906 | + EEM_Base::caps_read_admin, |
|
| 907 | + EEM_Base::caps_edit, |
|
| 908 | + EEM_Base::caps_delete, |
|
| 909 | + ], |
|
| 910 | + ], |
|
| 911 | + ] |
|
| 912 | + ); |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * Gets parameter information for a model regarding writing data |
|
| 918 | + * |
|
| 919 | + * @param string $model_name |
|
| 920 | + * @param ModelVersionInfo $model_version_info |
|
| 921 | + * @param boolean $create whether this is for request to create (in |
|
| 922 | + * which case we need all required params) or |
|
| 923 | + * just to update (in which case we don't |
|
| 924 | + * need those on every request) |
|
| 925 | + * @return array |
|
| 926 | + * @throws EE_Error |
|
| 927 | + * @throws ReflectionException |
|
| 928 | + */ |
|
| 929 | + protected function _get_write_params( |
|
| 930 | + $model_name, |
|
| 931 | + ModelVersionInfo $model_version_info, |
|
| 932 | + $create = false |
|
| 933 | + ) { |
|
| 934 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 935 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 936 | + |
|
| 937 | + // we do our own validation and sanitization within the controller |
|
| 938 | + $sanitize_callback = function_exists('rest_validate_value_from_schema') |
|
| 939 | + ? ['EED_Core_Rest_Api', 'default_sanitize_callback'] |
|
| 940 | + : null; |
|
| 941 | + $args_info = []; |
|
| 942 | + foreach ($fields as $field_name => $field_obj) { |
|
| 943 | + if ($field_obj->is_auto_increment()) { |
|
| 944 | + // totally ignore auto increment IDs |
|
| 945 | + continue; |
|
| 946 | + } |
|
| 947 | + $arg_info = $field_obj->getSchema(); |
|
| 948 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 949 | + $arg_info['required'] = $required; |
|
| 950 | + // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 951 | + unset($arg_info['readonly']); |
|
| 952 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
| 953 | + if (isset($schema_properties['raw']) |
|
| 954 | + && $field_obj->getSchemaType() === 'object' |
|
| 955 | + ) { |
|
| 956 | + // if there's a "raw" form of this argument, use those properties instead |
|
| 957 | + $arg_info = array_replace( |
|
| 958 | + $arg_info, |
|
| 959 | + $schema_properties['raw'] |
|
| 960 | + ); |
|
| 961 | + } |
|
| 962 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 963 | + $field_obj, |
|
| 964 | + $field_obj->get_default_value(), |
|
| 965 | + $model_version_info->requestedVersion() |
|
| 966 | + ); |
|
| 967 | + $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 968 | + $args_info[ $field_name ] = $arg_info; |
|
| 969 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 970 | + $gmt_arg_info = $arg_info; |
|
| 971 | + $gmt_arg_info['description'] = sprintf( |
|
| 972 | + esc_html__( |
|
| 973 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 974 | + 'event_espresso' |
|
| 975 | + ), |
|
| 976 | + $field_obj->get_nicename(), |
|
| 977 | + $field_name |
|
| 978 | + ); |
|
| 979 | + $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 980 | + } |
|
| 981 | + } |
|
| 982 | + return $args_info; |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + |
|
| 986 | + /** |
|
| 987 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 988 | + * If the value is blank but not required, don't bother validating it. |
|
| 989 | + * Also, it uses our email validation instead of WP API's default. |
|
| 990 | + * |
|
| 991 | + * @param $value |
|
| 992 | + * @param WP_REST_Request $request |
|
| 993 | + * @param $param |
|
| 994 | + * @return bool|true|WP_Error |
|
| 995 | + * @throws InvalidArgumentException |
|
| 996 | + * @throws InvalidInterfaceException |
|
| 997 | + * @throws InvalidDataTypeException |
|
| 998 | + */ |
|
| 999 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 1000 | + { |
|
| 1001 | + $attributes = $request->get_attributes(); |
|
| 1002 | + if (! isset($attributes['args'][ $param ]) |
|
| 1003 | + || ! is_array($attributes['args'][ $param ])) { |
|
| 1004 | + $validation_result = true; |
|
| 1005 | + } else { |
|
| 1006 | + $args = $attributes['args'][ $param ]; |
|
| 1007 | + if (( |
|
| 1008 | + $value === '' |
|
| 1009 | + || $value === null |
|
| 1010 | + ) |
|
| 1011 | + && (! isset($args['required']) |
|
| 1012 | + || $args['required'] === false |
|
| 1013 | + ) |
|
| 1014 | + ) { |
|
| 1015 | + // not required and not provided? that's cool |
|
| 1016 | + $validation_result = true; |
|
| 1017 | + } elseif (isset($args['format']) |
|
| 1018 | + && $args['format'] === 'email' |
|
| 1019 | + ) { |
|
| 1020 | + $validation_result = true; |
|
| 1021 | + if (! EED_Core_Rest_Api::_validate_email($value)) { |
|
| 1022 | + $validation_result = new WP_Error( |
|
| 1023 | + 'rest_invalid_param', |
|
| 1024 | + esc_html__( |
|
| 1025 | + 'The email address is not valid or does not exist.', |
|
| 1026 | + 'event_espresso' |
|
| 1027 | + ) |
|
| 1028 | + ); |
|
| 1029 | + } |
|
| 1030 | + } else { |
|
| 1031 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 1032 | + } |
|
| 1033 | + } |
|
| 1034 | + if (is_wp_error($validation_result)) { |
|
| 1035 | + return $validation_result; |
|
| 1036 | + } |
|
| 1037 | + return rest_sanitize_request_arg($value, $request, $param); |
|
| 1038 | + } |
|
| 1039 | + |
|
| 1040 | + |
|
| 1041 | + /** |
|
| 1042 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 1043 | + * |
|
| 1044 | + * @param $email |
|
| 1045 | + * @return bool |
|
| 1046 | + * @throws InvalidArgumentException |
|
| 1047 | + * @throws InvalidInterfaceException |
|
| 1048 | + * @throws InvalidDataTypeException |
|
| 1049 | + */ |
|
| 1050 | + protected static function _validate_email($email) |
|
| 1051 | + { |
|
| 1052 | + try { |
|
| 1053 | + EmailAddressFactory::create($email); |
|
| 1054 | + return true; |
|
| 1055 | + } catch (EmailValidationException $e) { |
|
| 1056 | + return false; |
|
| 1057 | + } |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * Gets routes for the config |
|
| 1063 | + * |
|
| 1064 | + * @return array @see _register_model_routes |
|
| 1065 | + * @deprecated since version 4.9.1 |
|
| 1066 | + */ |
|
| 1067 | + protected function _register_config_routes() |
|
| 1068 | + { |
|
| 1069 | + $config_routes = []; |
|
| 1070 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 1071 | + $config_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1072 | + $version, |
|
| 1073 | + $hidden_endpoint |
|
| 1074 | + ); |
|
| 1075 | + } |
|
| 1076 | + return $config_routes; |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + |
|
| 1080 | + /** |
|
| 1081 | + * Gets routes for the config for the specified version |
|
| 1082 | + * |
|
| 1083 | + * @param string $version |
|
| 1084 | + * @param boolean $hidden_endpoint |
|
| 1085 | + * @return array |
|
| 1086 | + */ |
|
| 1087 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1088 | + { |
|
| 1089 | + return [ |
|
| 1090 | + 'config' => [ |
|
| 1091 | + [ |
|
| 1092 | + 'callback' => [ |
|
| 1093 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1094 | + 'handleRequest', |
|
| 1095 | + ], |
|
| 1096 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1097 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1098 | + 'callback_args' => [$version], |
|
| 1099 | + ], |
|
| 1100 | + ], |
|
| 1101 | + 'site_info' => [ |
|
| 1102 | + [ |
|
| 1103 | + 'callback' => [ |
|
| 1104 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1105 | + 'handleRequestSiteInfo', |
|
| 1106 | + ], |
|
| 1107 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1108 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1109 | + 'callback_args' => [$version], |
|
| 1110 | + ], |
|
| 1111 | + ], |
|
| 1112 | + ]; |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + |
|
| 1116 | + /** |
|
| 1117 | + * Gets the meta info routes |
|
| 1118 | + * |
|
| 1119 | + * @return array @see _register_model_routes |
|
| 1120 | + * @deprecated since version 4.9.1 |
|
| 1121 | + */ |
|
| 1122 | + protected function _register_meta_routes() |
|
| 1123 | + { |
|
| 1124 | + $meta_routes = []; |
|
| 1125 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
|
| 1126 | + $meta_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1127 | + $version, |
|
| 1128 | + $hidden_endpoint |
|
| 1129 | + ); |
|
| 1130 | + } |
|
| 1131 | + return $meta_routes; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + |
|
| 1135 | + /** |
|
| 1136 | + * @param string $version |
|
| 1137 | + * @param boolean $hidden_endpoint |
|
| 1138 | + * @return array |
|
| 1139 | + */ |
|
| 1140 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1141 | + { |
|
| 1142 | + return [ |
|
| 1143 | + 'resources' => [ |
|
| 1144 | + [ |
|
| 1145 | + 'callback' => [ |
|
| 1146 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1147 | + 'handleRequestModelsMeta', |
|
| 1148 | + ], |
|
| 1149 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1150 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1151 | + 'callback_args' => [$version], |
|
| 1152 | + ], |
|
| 1153 | + ], |
|
| 1154 | + ]; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + |
|
| 1158 | + /** |
|
| 1159 | + * Tries to hide old 4.6 endpoints from the |
|
| 1160 | + * |
|
| 1161 | + * @param array $route_data |
|
| 1162 | + * @return array |
|
| 1163 | + * @throws EE_Error |
|
| 1164 | + * @throws ReflectionException |
|
| 1165 | + */ |
|
| 1166 | + public static function hide_old_endpoints($route_data) |
|
| 1167 | + { |
|
| 1168 | + // allow API clients to override which endpoints get hidden, in case |
|
| 1169 | + // they want to discover particular endpoints |
|
| 1170 | + // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1171 | + $force_show_ee_namespace = ltrim( |
|
| 1172 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1173 | + '/' |
|
| 1174 | + ); |
|
| 1175 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1176 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1177 | + foreach ($endpoints as $key => $endpoint) { |
|
| 1178 | + // skip schema and other route options |
|
| 1179 | + if (! is_numeric($key)) { |
|
| 1180 | + continue; |
|
| 1181 | + } |
|
| 1182 | + // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1183 | + // to $force_show_ee_namespace, in which case only show that one |
|
| 1184 | + // namespace's endpoints (and hide all others) |
|
| 1185 | + if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1186 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1187 | + ) { |
|
| 1188 | + $full_route = '/' . ltrim($namespace, '/'); |
|
| 1189 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1190 | + unset($route_data[ $full_route ]); |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + } |
|
| 1194 | + } |
|
| 1195 | + return $route_data; |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + |
|
| 1199 | + /** |
|
| 1200 | + * Returns an array describing which versions of core support serving requests for. |
|
| 1201 | + * Keys are core versions' major and minor version, and values are the |
|
| 1202 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1203 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1204 | + * the answers table entirely, in which case it would be very difficult for |
|
| 1205 | + * it to serve 4.6-style responses. |
|
| 1206 | + * Versions of core that are missing from this array are unknowns. |
|
| 1207 | + * previous ver |
|
| 1208 | + * |
|
| 1209 | + * @return array |
|
| 1210 | + */ |
|
| 1211 | + public static function version_compatibilities() |
|
| 1212 | + { |
|
| 1213 | + return apply_filters( |
|
| 1214 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1215 | + [ |
|
| 1216 | + '4.8.29' => '4.8.29', |
|
| 1217 | + '4.8.33' => '4.8.29', |
|
| 1218 | + '4.8.34' => '4.8.29', |
|
| 1219 | + '4.8.36' => '4.8.29', |
|
| 1220 | + ] |
|
| 1221 | + ); |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + |
|
| 1225 | + /** |
|
| 1226 | + * Gets the latest API version served. Eg if there |
|
| 1227 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1228 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1229 | + * |
|
| 1230 | + * @return string |
|
| 1231 | + */ |
|
| 1232 | + public static function latest_rest_api_version() |
|
| 1233 | + { |
|
| 1234 | + $versions_served = EED_Core_Rest_Api::versions_served(); |
|
| 1235 | + $versions_served_keys = array_keys($versions_served); |
|
| 1236 | + return end($versions_served_keys); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + |
|
| 1240 | + /** |
|
| 1241 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1242 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1243 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1244 | + * We also indicate whether or not this version should be put in the index or not |
|
| 1245 | + * |
|
| 1246 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1247 | + * are whether or not they should be hidden |
|
| 1248 | + */ |
|
| 1249 | + public static function versions_served() |
|
| 1250 | + { |
|
| 1251 | + $versions_served = []; |
|
| 1252 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1253 | + $lowest_compatible_version = end($possibly_served_versions); |
|
| 1254 | + reset($possibly_served_versions); |
|
| 1255 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1256 | + $latest_version = end($versions_served_historically); |
|
| 1257 | + reset($versions_served_historically); |
|
| 1258 | + // for each version of core we have ever served: |
|
| 1259 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1260 | + // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1261 | + |
|
| 1262 | + if ($key_versioned_endpoint === $latest_version) { |
|
| 1263 | + // don't hide the latest version in the index |
|
| 1264 | + $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1265 | + } elseif (version_compare($key_versioned_endpoint, $lowest_compatible_version, '>=') |
|
| 1266 | + && version_compare($key_versioned_endpoint, EED_Core_Rest_Api::core_version(), '<') |
|
| 1267 | + ) { |
|
| 1268 | + // include, but hide, previous versions which are still supported |
|
| 1269 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1270 | + } elseif (apply_filters( |
|
| 1271 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1272 | + false, |
|
| 1273 | + $possibly_served_versions |
|
| 1274 | + )) { |
|
| 1275 | + // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1276 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1277 | + } |
|
| 1278 | + } |
|
| 1279 | + return $versions_served; |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + |
|
| 1283 | + /** |
|
| 1284 | + * Gets the major and minor version of EE core's version string |
|
| 1285 | + * |
|
| 1286 | + * @return string |
|
| 1287 | + */ |
|
| 1288 | + public static function core_version() |
|
| 1289 | + { |
|
| 1290 | + return apply_filters( |
|
| 1291 | + 'FHEE__EED_Core_REST_API__core_version', |
|
| 1292 | + implode( |
|
| 1293 | + '.', |
|
| 1294 | + array_slice( |
|
| 1295 | + explode( |
|
| 1296 | + '.', |
|
| 1297 | + espresso_version() |
|
| 1298 | + ), |
|
| 1299 | + 0, |
|
| 1300 | + 3 |
|
| 1301 | + ) |
|
| 1302 | + ) |
|
| 1303 | + ); |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * Gets the default limit that should be used when querying for resources |
|
| 1309 | + * |
|
| 1310 | + * @return int |
|
| 1311 | + */ |
|
| 1312 | + public static function get_default_query_limit() |
|
| 1313 | + { |
|
| 1314 | + // we actually don't use a const because we want folks to always use |
|
| 1315 | + // this method, not the const directly |
|
| 1316 | + return apply_filters( |
|
| 1317 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1318 | + 50 |
|
| 1319 | + ); |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + |
|
| 1323 | + /** |
|
| 1324 | + * @param string $version api version string (i.e. '4.8.36') |
|
| 1325 | + * @return array |
|
| 1326 | + */ |
|
| 1327 | + public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1328 | + { |
|
| 1329 | + $version = empty($version) ? EED_Core_Rest_Api::latest_rest_api_version() : $version; |
|
| 1330 | + $model_names = EED_Core_Rest_Api::model_names_with_plural_routes($version); |
|
| 1331 | + $collection_routes = []; |
|
| 1332 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1333 | + $collection_routes[ strtolower($model_name) ] = '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' |
|
| 1334 | + . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1335 | + } |
|
| 1336 | + return $collection_routes; |
|
| 1337 | + } |
|
| 1338 | + |
|
| 1339 | + |
|
| 1340 | + /** |
|
| 1341 | + * Returns an array of primary key names indexed by model names. |
|
| 1342 | + * |
|
| 1343 | + * @param string $version |
|
| 1344 | + * @return array |
|
| 1345 | + */ |
|
| 1346 | + public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1347 | + { |
|
| 1348 | + $version = empty($version) ? EED_Core_Rest_Api::latest_rest_api_version() : $version; |
|
| 1349 | + $model_names = EED_Core_Rest_Api::model_names_with_plural_routes($version); |
|
| 1350 | + $primary_key_items = []; |
|
| 1351 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1352 | + $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1353 | + foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1354 | + if (count($primary_keys) > 1) { |
|
| 1355 | + $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1356 | + } else { |
|
| 1357 | + $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1358 | + } |
|
| 1359 | + } |
|
| 1360 | + } |
|
| 1361 | + return $primary_key_items; |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * Determines the EE REST API debug mode is activated, or not. |
|
| 1367 | + * |
|
| 1368 | + * @return bool |
|
| 1369 | + * @since 4.9.76.p |
|
| 1370 | + */ |
|
| 1371 | + public static function debugMode() |
|
| 1372 | + { |
|
| 1373 | + static $debug_mode = null; // could be class prop |
|
| 1374 | + if ($debug_mode === null) { |
|
| 1375 | + $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
| 1376 | + } |
|
| 1377 | + return $debug_mode; |
|
| 1378 | + } |
|
| 1379 | + |
|
| 1380 | + |
|
| 1381 | + /** |
|
| 1382 | + * run - initial module setup |
|
| 1383 | + * |
|
| 1384 | + * @access public |
|
| 1385 | + * @param WP $WP |
|
| 1386 | + * @return void |
|
| 1387 | + */ |
|
| 1388 | + public function run($WP) |
|
| 1389 | + { |
|
| 1390 | + } |
|
| 1391 | 1391 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | protected static function _set_hooks_for_changes() |
| 132 | 132 | { |
| 133 | - $folder_contents = EEH_File::get_contents_of_folders([EE_LIBRARIES . 'rest_api/changes'], false); |
|
| 133 | + $folder_contents = EEH_File::get_contents_of_folders([EE_LIBRARIES.'rest_api/changes'], false); |
|
| 134 | 134 | foreach ($folder_contents as $classname_in_namespace => $filepath) { |
| 135 | 135 | // ignore the base parent class |
| 136 | 136 | // and legacy named classes |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | ) { |
| 140 | 140 | continue; |
| 141 | 141 | } |
| 142 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 142 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\'.$classname_in_namespace; |
|
| 143 | 143 | if (class_exists($full_classname)) { |
| 144 | 144 | $instance_of_class = new $full_classname; |
| 145 | 145 | if ($instance_of_class instanceof ChangesInBase) { |
@@ -185,10 +185,10 @@ discard block |
||
| 185 | 185 | * } |
| 186 | 186 | */ |
| 187 | 187 | // skip route options |
| 188 | - if (! is_numeric($endpoint_key)) { |
|
| 188 | + if ( ! is_numeric($endpoint_key)) { |
|
| 189 | 189 | continue; |
| 190 | 190 | } |
| 191 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 191 | + if ( ! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 192 | 192 | throw new EE_Error( |
| 193 | 193 | esc_html__( |
| 194 | 194 | // @codingStandardsIgnoreStart |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | } |
| 210 | 210 | if (isset($data_for_single_endpoint['callback_args'])) { |
| 211 | 211 | $callback_args = $data_for_single_endpoint['callback_args']; |
| 212 | - $single_endpoint_args['callback'] = static function (WP_REST_Request $request) use ( |
|
| 212 | + $single_endpoint_args['callback'] = static function(WP_REST_Request $request) use ( |
|
| 213 | 213 | $callback, |
| 214 | 214 | $callback_args |
| 215 | 215 | ) { |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | // the REST API will issue a _doing_it_wrong notice. |
| 227 | 227 | // Since the EE REST API defers capabilities to the db model system, |
| 228 | 228 | // we will just use the generic WP callback for public endpoints |
| 229 | - if (! isset($single_endpoint_args['permission_callback'])) { |
|
| 229 | + if ( ! isset($single_endpoint_args['permission_callback'])) { |
|
| 230 | 230 | $single_endpoint_args['permission_callback'] = '__return_true'; |
| 231 | 231 | } |
| 232 | 232 | $multiple_endpoint_args[] = $single_endpoint_args; |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | $schema_route_data = $data_for_multiple_endpoints['schema']; |
| 236 | 236 | $schema_callback = $schema_route_data['schema_callback']; |
| 237 | 237 | $callback_args = $schema_route_data['callback_args']; |
| 238 | - $multiple_endpoint_args['schema'] = static function () use ($schema_callback, $callback_args) { |
|
| 238 | + $multiple_endpoint_args['schema'] = static function() use ($schema_callback, $callback_args) { |
|
| 239 | 239 | return call_user_func_array( |
| 240 | 240 | $schema_callback, |
| 241 | 241 | $callback_args |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | { |
| 278 | 278 | // delete the saved EE REST API routes |
| 279 | 279 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
| 280 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 280 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names.$version); |
|
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | { |
| 298 | 298 | $ee_routes = []; |
| 299 | 299 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoints) { |
| 300 | - $ee_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = EED_Core_Rest_Api::_get_ee_route_data_for_version( |
|
| 300 | + $ee_routes[EED_Core_Rest_Api::ee_api_namespace.$version] = EED_Core_Rest_Api::_get_ee_route_data_for_version( |
|
| 301 | 301 | $version, |
| 302 | 302 | $hidden_endpoints |
| 303 | 303 | ); |
@@ -318,8 +318,8 @@ discard block |
||
| 318 | 318 | */ |
| 319 | 319 | protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
| 320 | 320 | { |
| 321 | - $ee_routes = get_option(EED_Core_Rest_Api::saved_routes_option_names . $version, null); |
|
| 322 | - if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 321 | + $ee_routes = get_option(EED_Core_Rest_Api::saved_routes_option_names.$version, null); |
|
| 322 | + if ( ! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 323 | 323 | $ee_routes = EED_Core_Rest_Api::_save_ee_route_data_for_version($version, $hidden_endpoints); |
| 324 | 324 | } |
| 325 | 325 | return $ee_routes; |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
| 348 | 348 | ) |
| 349 | 349 | ); |
| 350 | - $option_name = EED_Core_Rest_Api::saved_routes_option_names . $version; |
|
| 350 | + $option_name = EED_Core_Rest_Api::saved_routes_option_names.$version; |
|
| 351 | 351 | if (get_option($option_name)) { |
| 352 | 352 | update_option($option_name, $routes, true); |
| 353 | 353 | } else { |
@@ -392,8 +392,8 @@ discard block |
||
| 392 | 392 | { |
| 393 | 393 | $model_routes = []; |
| 394 | 394 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
| 395 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 396 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 395 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
| 396 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 397 | 397 | } |
| 398 | 398 | return $model_routes; |
| 399 | 399 | } |
@@ -462,13 +462,13 @@ discard block |
||
| 462 | 462 | foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
| 463 | 463 | $model = EE_Registry::instance()->load_model($model_name); |
| 464 | 464 | // if this isn't a valid model then let's skip iterate to the next item in the loop. |
| 465 | - if (! $model instanceof EEM_Base) { |
|
| 465 | + if ( ! $model instanceof EEM_Base) { |
|
| 466 | 466 | continue; |
| 467 | 467 | } |
| 468 | 468 | // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
| 469 | 469 | $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
| 470 | 470 | $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
| 471 | - $model_routes[ $plural_model_route ] = [ |
|
| 471 | + $model_routes[$plural_model_route] = [ |
|
| 472 | 472 | [ |
| 473 | 473 | 'callback' => [ |
| 474 | 474 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -479,7 +479,7 @@ discard block |
||
| 479 | 479 | 'hidden_endpoint' => $hidden_endpoint, |
| 480 | 480 | 'args' => $this->_get_read_query_params($model, $version), |
| 481 | 481 | '_links' => [ |
| 482 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 482 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace.$version.$singular_model_route), |
|
| 483 | 483 | ], |
| 484 | 484 | ], |
| 485 | 485 | 'schema' => [ |
@@ -490,7 +490,7 @@ discard block |
||
| 490 | 490 | 'callback_args' => [$version, $model_name], |
| 491 | 491 | ], |
| 492 | 492 | ]; |
| 493 | - $model_routes[ $singular_model_route ] = [ |
|
| 493 | + $model_routes[$singular_model_route] = [ |
|
| 494 | 494 | [ |
| 495 | 495 | 'callback' => [ |
| 496 | 496 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | EED_Core_Rest_Api::should_have_write_endpoints($model), |
| 508 | 508 | $model |
| 509 | 509 | )) { |
| 510 | - $model_routes[ $plural_model_route ][] = [ |
|
| 510 | + $model_routes[$plural_model_route][] = [ |
|
| 511 | 511 | 'callback' => [ |
| 512 | 512 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
| 513 | 513 | 'handleRequestInsert', |
@@ -517,8 +517,8 @@ discard block |
||
| 517 | 517 | 'hidden_endpoint' => $hidden_endpoint, |
| 518 | 518 | 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
| 519 | 519 | ]; |
| 520 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 521 | - $model_routes[ $singular_model_route ], |
|
| 520 | + $model_routes[$singular_model_route] = array_merge( |
|
| 521 | + $model_routes[$singular_model_route], |
|
| 522 | 522 | [ |
| 523 | 523 | [ |
| 524 | 524 | 'callback' => [ |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | '(?P<id>[^\/]+)', |
| 550 | 550 | $relation_obj |
| 551 | 551 | ); |
| 552 | - $model_routes[ $related_route ] = [ |
|
| 552 | + $model_routes[$related_route] = [ |
|
| 553 | 553 | [ |
| 554 | 554 | 'callback' => [ |
| 555 | 555 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -562,8 +562,8 @@ discard block |
||
| 562 | 562 | ], |
| 563 | 563 | ]; |
| 564 | 564 | |
| 565 | - $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 566 | - $model_routes[ $related_write_route ] = [ |
|
| 565 | + $related_write_route = $related_route.'/'.'(?P<related_id>[^\/]+)'; |
|
| 566 | + $model_routes[$related_write_route] = [ |
|
| 567 | 567 | [ |
| 568 | 568 | 'callback' => [ |
| 569 | 569 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
@@ -620,7 +620,7 @@ discard block |
||
| 620 | 620 | */ |
| 621 | 621 | public static function get_entity_route($model, $id) |
| 622 | 622 | { |
| 623 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 623 | + return EED_Core_Rest_Api::get_collection_route($model).'/'.$id; |
|
| 624 | 624 | } |
| 625 | 625 | |
| 626 | 626 | |
@@ -640,7 +640,7 @@ discard block |
||
| 640 | 640 | $relation_obj->get_other_model()->get_this_model_name(), |
| 641 | 641 | $relation_obj |
| 642 | 642 | ); |
| 643 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 643 | + return EED_Core_Rest_Api::get_entity_route($model, $id).'/'.$related_model_name_endpoint_part; |
|
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | |
@@ -654,7 +654,7 @@ discard block |
||
| 654 | 654 | */ |
| 655 | 655 | public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
| 656 | 656 | { |
| 657 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 657 | + return '/'.EED_Core_Rest_Api::ee_api_namespace.$version.'/'.$relative_route; |
|
| 658 | 658 | } |
| 659 | 659 | |
| 660 | 660 | |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | { |
| 669 | 669 | $routes = []; |
| 670 | 670 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
| 671 | - $routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 671 | + $routes[EED_Core_Rest_Api::ee_api_namespace.$version] = $this->_get_rpc_route_data_for_version( |
|
| 672 | 672 | $version, |
| 673 | 673 | $hidden_endpoint |
| 674 | 674 | ); |
@@ -801,12 +801,12 @@ discard block |
||
| 801 | 801 | { |
| 802 | 802 | // if they're related through a HABTM relation, check for any non-FKs |
| 803 | 803 | $all_relation_settings = $source_model->relation_settings(); |
| 804 | - $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 804 | + $relation_settings = $all_relation_settings[$related_model->get_this_model_name()]; |
|
| 805 | 805 | $params = []; |
| 806 | 806 | if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
| 807 | 807 | foreach ($relation_settings->getNonKeyFields() as $field) { |
| 808 | 808 | /* @var $field EE_Model_Field_Base */ |
| 809 | - $params[ $field->get_name() ] = [ |
|
| 809 | + $params[$field->get_name()] = [ |
|
| 810 | 810 | 'required' => ! $field->is_nullable(), |
| 811 | 811 | 'default' => ModelDataTranslator::prepareFieldValueForJson( |
| 812 | 812 | $field, |
@@ -835,7 +835,7 @@ discard block |
||
| 835 | 835 | { |
| 836 | 836 | $default_orderby = []; |
| 837 | 837 | foreach ($model->get_combined_primary_key_fields() as $key_field) { |
| 838 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 838 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
| 839 | 839 | } |
| 840 | 840 | return array_merge( |
| 841 | 841 | $this->_get_response_selection_query_params($model, $version), |
@@ -869,7 +869,7 @@ discard block |
||
| 869 | 869 | 'type' => [ |
| 870 | 870 | 'object', |
| 871 | 871 | 'string', |
| 872 | - ],// because we accept a variety of types, WP core validation and sanitization |
|
| 872 | + ], // because we accept a variety of types, WP core validation and sanitization |
|
| 873 | 873 | // freaks out. We'll just validate this argument while handling the request |
| 874 | 874 | 'validate_callback' => null, |
| 875 | 875 | 'sanitize_callback' => null, |
@@ -965,7 +965,7 @@ discard block |
||
| 965 | 965 | $model_version_info->requestedVersion() |
| 966 | 966 | ); |
| 967 | 967 | $arg_info['sanitize_callback'] = $sanitize_callback; |
| 968 | - $args_info[ $field_name ] = $arg_info; |
|
| 968 | + $args_info[$field_name] = $arg_info; |
|
| 969 | 969 | if ($field_obj instanceof EE_Datetime_Field) { |
| 970 | 970 | $gmt_arg_info = $arg_info; |
| 971 | 971 | $gmt_arg_info['description'] = sprintf( |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | $field_obj->get_nicename(), |
| 977 | 977 | $field_name |
| 978 | 978 | ); |
| 979 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 979 | + $args_info[$field_name.'_gmt'] = $gmt_arg_info; |
|
| 980 | 980 | } |
| 981 | 981 | } |
| 982 | 982 | return $args_info; |
@@ -999,16 +999,16 @@ discard block |
||
| 999 | 999 | public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
| 1000 | 1000 | { |
| 1001 | 1001 | $attributes = $request->get_attributes(); |
| 1002 | - if (! isset($attributes['args'][ $param ]) |
|
| 1003 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 1002 | + if ( ! isset($attributes['args'][$param]) |
|
| 1003 | + || ! is_array($attributes['args'][$param])) { |
|
| 1004 | 1004 | $validation_result = true; |
| 1005 | 1005 | } else { |
| 1006 | - $args = $attributes['args'][ $param ]; |
|
| 1006 | + $args = $attributes['args'][$param]; |
|
| 1007 | 1007 | if (( |
| 1008 | 1008 | $value === '' |
| 1009 | 1009 | || $value === null |
| 1010 | 1010 | ) |
| 1011 | - && (! isset($args['required']) |
|
| 1011 | + && ( ! isset($args['required']) |
|
| 1012 | 1012 | || $args['required'] === false |
| 1013 | 1013 | ) |
| 1014 | 1014 | ) { |
@@ -1018,7 +1018,7 @@ discard block |
||
| 1018 | 1018 | && $args['format'] === 'email' |
| 1019 | 1019 | ) { |
| 1020 | 1020 | $validation_result = true; |
| 1021 | - if (! EED_Core_Rest_Api::_validate_email($value)) { |
|
| 1021 | + if ( ! EED_Core_Rest_Api::_validate_email($value)) { |
|
| 1022 | 1022 | $validation_result = new WP_Error( |
| 1023 | 1023 | 'rest_invalid_param', |
| 1024 | 1024 | esc_html__( |
@@ -1068,7 +1068,7 @@ discard block |
||
| 1068 | 1068 | { |
| 1069 | 1069 | $config_routes = []; |
| 1070 | 1070 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
| 1071 | - $config_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1071 | + $config_routes[EED_Core_Rest_Api::ee_api_namespace.$version] = $this->_get_config_route_data_for_version( |
|
| 1072 | 1072 | $version, |
| 1073 | 1073 | $hidden_endpoint |
| 1074 | 1074 | ); |
@@ -1123,7 +1123,7 @@ discard block |
||
| 1123 | 1123 | { |
| 1124 | 1124 | $meta_routes = []; |
| 1125 | 1125 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden_endpoint) { |
| 1126 | - $meta_routes[ EED_Core_Rest_Api::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1126 | + $meta_routes[EED_Core_Rest_Api::ee_api_namespace.$version] = $this->_get_meta_route_data_for_version( |
|
| 1127 | 1127 | $version, |
| 1128 | 1128 | $hidden_endpoint |
| 1129 | 1129 | ); |
@@ -1176,7 +1176,7 @@ discard block |
||
| 1176 | 1176 | foreach ($relative_urls as $resource_name => $endpoints) { |
| 1177 | 1177 | foreach ($endpoints as $key => $endpoint) { |
| 1178 | 1178 | // skip schema and other route options |
| 1179 | - if (! is_numeric($key)) { |
|
| 1179 | + if ( ! is_numeric($key)) { |
|
| 1180 | 1180 | continue; |
| 1181 | 1181 | } |
| 1182 | 1182 | // by default, hide "hidden_endpoint"s, unless the request indicates |
@@ -1185,9 +1185,9 @@ discard block |
||
| 1185 | 1185 | if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
| 1186 | 1186 | || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
| 1187 | 1187 | ) { |
| 1188 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1189 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1190 | - unset($route_data[ $full_route ]); |
|
| 1188 | + $full_route = '/'.ltrim($namespace, '/'); |
|
| 1189 | + $full_route .= '/'.ltrim($resource_name, '/'); |
|
| 1190 | + unset($route_data[$full_route]); |
|
| 1191 | 1191 | } |
| 1192 | 1192 | } |
| 1193 | 1193 | } |
@@ -1261,19 +1261,19 @@ discard block |
||
| 1261 | 1261 | |
| 1262 | 1262 | if ($key_versioned_endpoint === $latest_version) { |
| 1263 | 1263 | // don't hide the latest version in the index |
| 1264 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1264 | + $versions_served[$key_versioned_endpoint] = false; |
|
| 1265 | 1265 | } elseif (version_compare($key_versioned_endpoint, $lowest_compatible_version, '>=') |
| 1266 | 1266 | && version_compare($key_versioned_endpoint, EED_Core_Rest_Api::core_version(), '<') |
| 1267 | 1267 | ) { |
| 1268 | 1268 | // include, but hide, previous versions which are still supported |
| 1269 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1269 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1270 | 1270 | } elseif (apply_filters( |
| 1271 | 1271 | 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
| 1272 | 1272 | false, |
| 1273 | 1273 | $possibly_served_versions |
| 1274 | 1274 | )) { |
| 1275 | 1275 | // if a version is no longer supported, don't include it in index or list of versions served |
| 1276 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1276 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1277 | 1277 | } |
| 1278 | 1278 | } |
| 1279 | 1279 | return $versions_served; |
@@ -1330,7 +1330,7 @@ discard block |
||
| 1330 | 1330 | $model_names = EED_Core_Rest_Api::model_names_with_plural_routes($version); |
| 1331 | 1331 | $collection_routes = []; |
| 1332 | 1332 | foreach ($model_names as $model_name => $model_class_name) { |
| 1333 | - $collection_routes[ strtolower($model_name) ] = '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' |
|
| 1333 | + $collection_routes[strtolower($model_name)] = '/'.EED_Core_Rest_Api::ee_api_namespace.$version.'/' |
|
| 1334 | 1334 | . EEH_Inflector::pluralize_and_lower($model_name); |
| 1335 | 1335 | } |
| 1336 | 1336 | return $collection_routes; |
@@ -1352,9 +1352,9 @@ discard block |
||
| 1352 | 1352 | $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
| 1353 | 1353 | foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
| 1354 | 1354 | if (count($primary_keys) > 1) { |
| 1355 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1355 | + $primary_key_items[strtolower($model_name)][] = $primary_key_name; |
|
| 1356 | 1356 | } else { |
| 1357 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1357 | + $primary_key_items[strtolower($model_name)] = $primary_key_name; |
|
| 1358 | 1358 | } |
| 1359 | 1359 | } |
| 1360 | 1360 | } |
@@ -26,122 +26,122 @@ |
||
| 26 | 26 | { |
| 27 | 27 | |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @param WP_REST_Request $request |
|
| 31 | - * @param string $version |
|
| 32 | - * @return array|WP_REST_Response |
|
| 33 | - */ |
|
| 34 | - public static function handleRequestModelsMeta(WP_REST_Request $request, $version) |
|
| 35 | - { |
|
| 36 | - $controller = new Meta(); |
|
| 37 | - try { |
|
| 38 | - $controller->setRequestedVersion($version); |
|
| 39 | - return $controller->sendResponse($controller->getModelsMetadataEntity()); |
|
| 40 | - } catch (Exception $e) { |
|
| 41 | - return $controller->sendResponse($e); |
|
| 42 | - } |
|
| 43 | - } |
|
| 29 | + /** |
|
| 30 | + * @param WP_REST_Request $request |
|
| 31 | + * @param string $version |
|
| 32 | + * @return array|WP_REST_Response |
|
| 33 | + */ |
|
| 34 | + public static function handleRequestModelsMeta(WP_REST_Request $request, $version) |
|
| 35 | + { |
|
| 36 | + $controller = new Meta(); |
|
| 37 | + try { |
|
| 38 | + $controller->setRequestedVersion($version); |
|
| 39 | + return $controller->sendResponse($controller->getModelsMetadataEntity()); |
|
| 40 | + } catch (Exception $e) { |
|
| 41 | + return $controller->sendResponse($e); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | |
| 46 | - /* |
|
| 46 | + /* |
|
| 47 | 47 | * Gets the model metadata resource entity |
| 48 | 48 | * @return array for JSON response, describing all the models available in teh requested version |
| 49 | 49 | */ |
| 50 | - protected function getModelsMetadataEntity() |
|
| 51 | - { |
|
| 52 | - $response = array(); |
|
| 53 | - foreach ($this->getModelVersionInfo()->modelsForRequestedVersion() as $model_name => $model_classname) { |
|
| 54 | - $model = $this->getModelVersionInfo()->loadModel($model_name); |
|
| 55 | - $fields_json = array(); |
|
| 56 | - foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field_obj) { |
|
| 57 | - if ($this->getModelVersionInfo()->fieldIsIgnored($field_obj)) { |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - if ($field_obj instanceof EE_Boolean_Field) { |
|
| 61 | - $datatype = 'Boolean'; |
|
| 62 | - } elseif ($field_obj->get_wpdb_data_type() == '%d') { |
|
| 63 | - $datatype = 'Number'; |
|
| 64 | - } elseif ($field_name instanceof EE_Serialized_Text_Field) { |
|
| 65 | - $datatype = 'Object'; |
|
| 66 | - } else { |
|
| 67 | - $datatype = 'String'; |
|
| 68 | - } |
|
| 69 | - $default_value = ModelDataTranslator::prepareFieldValueForJson( |
|
| 70 | - $field_obj, |
|
| 71 | - $field_obj->get_default_value(), |
|
| 72 | - $this->getModelVersionInfo()->requestedVersion() |
|
| 73 | - ); |
|
| 74 | - $field_json = array( |
|
| 75 | - 'name' => $field_name, |
|
| 76 | - 'nicename' => wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES), |
|
| 77 | - 'has_rendered_format' => $this->getModelVersionInfo()->fieldHasRenderedFormat($field_obj), |
|
| 78 | - 'has_pretty_format' => $this->getModelVersionInfo()->fieldHasPrettyFormat($field_obj), |
|
| 79 | - 'type' => str_replace('EE_', '', get_class($field_obj)), |
|
| 80 | - 'datatype' => $datatype, |
|
| 81 | - 'nullable' => $field_obj->is_nullable(), |
|
| 82 | - 'default' => $default_value, |
|
| 83 | - 'table_alias' => $field_obj->get_table_alias(), |
|
| 84 | - 'table_column' => $field_obj->get_table_column(), |
|
| 85 | - ); |
|
| 86 | - $fields_json[ $field_json['name'] ] = $field_json; |
|
| 87 | - } |
|
| 88 | - $fields_json = array_merge( |
|
| 89 | - $fields_json, |
|
| 90 | - $this->getModelVersionInfo()->extraResourcePropertiesForModel($model) |
|
| 91 | - ); |
|
| 92 | - $response[ $model_name ]['fields'] = apply_filters( |
|
| 93 | - 'FHEE__Meta__handle_request_models_meta__fields', |
|
| 94 | - $fields_json, |
|
| 95 | - $model |
|
| 96 | - ); |
|
| 97 | - $relations_json = array(); |
|
| 98 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 99 | - $relation_json = array( |
|
| 100 | - 'name' => $relation_name, |
|
| 101 | - 'type' => str_replace('EE_', '', get_class($relation_obj)), |
|
| 102 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation, |
|
| 103 | - ); |
|
| 104 | - $relations_json[ $relation_name ] = $relation_json; |
|
| 105 | - } |
|
| 106 | - $response[ $model_name ]['relations'] = apply_filters( |
|
| 107 | - 'FHEE__Meta__handle_request_models_meta__relations', |
|
| 108 | - $relations_json, |
|
| 109 | - $model |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - return $response; |
|
| 113 | - } |
|
| 50 | + protected function getModelsMetadataEntity() |
|
| 51 | + { |
|
| 52 | + $response = array(); |
|
| 53 | + foreach ($this->getModelVersionInfo()->modelsForRequestedVersion() as $model_name => $model_classname) { |
|
| 54 | + $model = $this->getModelVersionInfo()->loadModel($model_name); |
|
| 55 | + $fields_json = array(); |
|
| 56 | + foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field_obj) { |
|
| 57 | + if ($this->getModelVersionInfo()->fieldIsIgnored($field_obj)) { |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + if ($field_obj instanceof EE_Boolean_Field) { |
|
| 61 | + $datatype = 'Boolean'; |
|
| 62 | + } elseif ($field_obj->get_wpdb_data_type() == '%d') { |
|
| 63 | + $datatype = 'Number'; |
|
| 64 | + } elseif ($field_name instanceof EE_Serialized_Text_Field) { |
|
| 65 | + $datatype = 'Object'; |
|
| 66 | + } else { |
|
| 67 | + $datatype = 'String'; |
|
| 68 | + } |
|
| 69 | + $default_value = ModelDataTranslator::prepareFieldValueForJson( |
|
| 70 | + $field_obj, |
|
| 71 | + $field_obj->get_default_value(), |
|
| 72 | + $this->getModelVersionInfo()->requestedVersion() |
|
| 73 | + ); |
|
| 74 | + $field_json = array( |
|
| 75 | + 'name' => $field_name, |
|
| 76 | + 'nicename' => wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES), |
|
| 77 | + 'has_rendered_format' => $this->getModelVersionInfo()->fieldHasRenderedFormat($field_obj), |
|
| 78 | + 'has_pretty_format' => $this->getModelVersionInfo()->fieldHasPrettyFormat($field_obj), |
|
| 79 | + 'type' => str_replace('EE_', '', get_class($field_obj)), |
|
| 80 | + 'datatype' => $datatype, |
|
| 81 | + 'nullable' => $field_obj->is_nullable(), |
|
| 82 | + 'default' => $default_value, |
|
| 83 | + 'table_alias' => $field_obj->get_table_alias(), |
|
| 84 | + 'table_column' => $field_obj->get_table_column(), |
|
| 85 | + ); |
|
| 86 | + $fields_json[ $field_json['name'] ] = $field_json; |
|
| 87 | + } |
|
| 88 | + $fields_json = array_merge( |
|
| 89 | + $fields_json, |
|
| 90 | + $this->getModelVersionInfo()->extraResourcePropertiesForModel($model) |
|
| 91 | + ); |
|
| 92 | + $response[ $model_name ]['fields'] = apply_filters( |
|
| 93 | + 'FHEE__Meta__handle_request_models_meta__fields', |
|
| 94 | + $fields_json, |
|
| 95 | + $model |
|
| 96 | + ); |
|
| 97 | + $relations_json = array(); |
|
| 98 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 99 | + $relation_json = array( |
|
| 100 | + 'name' => $relation_name, |
|
| 101 | + 'type' => str_replace('EE_', '', get_class($relation_obj)), |
|
| 102 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation, |
|
| 103 | + ); |
|
| 104 | + $relations_json[ $relation_name ] = $relation_json; |
|
| 105 | + } |
|
| 106 | + $response[ $model_name ]['relations'] = apply_filters( |
|
| 107 | + 'FHEE__Meta__handle_request_models_meta__relations', |
|
| 108 | + $relations_json, |
|
| 109 | + $model |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + return $response; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Adds EE metadata to the index |
|
| 118 | - * |
|
| 119 | - * @param WP_REST_Response $rest_response_obj |
|
| 120 | - * @return WP_REST_Response |
|
| 121 | - */ |
|
| 122 | - public static function filterEeMetadataIntoIndex(WP_REST_Response $rest_response_obj) |
|
| 123 | - { |
|
| 124 | - $response_data = $rest_response_obj->get_data(); |
|
| 125 | - $addons = array(); |
|
| 126 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
| 127 | - $addon_json = array( |
|
| 128 | - 'name' => $addon->name(), |
|
| 129 | - 'version' => $addon->version(), |
|
| 130 | - ); |
|
| 131 | - $addons[ $addon_json['name'] ] = $addon_json; |
|
| 132 | - } |
|
| 133 | - $response_data['ee'] = array( |
|
| 134 | - 'version' => EEM_System_Status::instance()->get_ee_version(), |
|
| 135 | - // @codingStandardsIgnoreStart |
|
| 136 | - 'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', |
|
| 137 | - // @codingStandardsIgnoreEnd |
|
| 138 | - 'addons' => $addons, |
|
| 139 | - 'maintenance_mode' => EE_Maintenance_Mode::instance()->real_level(), |
|
| 140 | - 'served_core_versions' => array_keys(EED_Core_Rest_Api::versions_served()), |
|
| 141 | - ); |
|
| 142 | - $rest_response_obj->set_data($response_data); |
|
| 143 | - return $rest_response_obj; |
|
| 144 | - } |
|
| 116 | + /** |
|
| 117 | + * Adds EE metadata to the index |
|
| 118 | + * |
|
| 119 | + * @param WP_REST_Response $rest_response_obj |
|
| 120 | + * @return WP_REST_Response |
|
| 121 | + */ |
|
| 122 | + public static function filterEeMetadataIntoIndex(WP_REST_Response $rest_response_obj) |
|
| 123 | + { |
|
| 124 | + $response_data = $rest_response_obj->get_data(); |
|
| 125 | + $addons = array(); |
|
| 126 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 127 | + $addon_json = array( |
|
| 128 | + 'name' => $addon->name(), |
|
| 129 | + 'version' => $addon->version(), |
|
| 130 | + ); |
|
| 131 | + $addons[ $addon_json['name'] ] = $addon_json; |
|
| 132 | + } |
|
| 133 | + $response_data['ee'] = array( |
|
| 134 | + 'version' => EEM_System_Status::instance()->get_ee_version(), |
|
| 135 | + // @codingStandardsIgnoreStart |
|
| 136 | + 'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', |
|
| 137 | + // @codingStandardsIgnoreEnd |
|
| 138 | + 'addons' => $addons, |
|
| 139 | + 'maintenance_mode' => EE_Maintenance_Mode::instance()->real_level(), |
|
| 140 | + 'served_core_versions' => array_keys(EED_Core_Rest_Api::versions_served()), |
|
| 141 | + ); |
|
| 142 | + $rest_response_obj->set_data($response_data); |
|
| 143 | + return $rest_response_obj; |
|
| 144 | + } |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | |
@@ -83,13 +83,13 @@ discard block |
||
| 83 | 83 | 'table_alias' => $field_obj->get_table_alias(), |
| 84 | 84 | 'table_column' => $field_obj->get_table_column(), |
| 85 | 85 | ); |
| 86 | - $fields_json[ $field_json['name'] ] = $field_json; |
|
| 86 | + $fields_json[$field_json['name']] = $field_json; |
|
| 87 | 87 | } |
| 88 | 88 | $fields_json = array_merge( |
| 89 | 89 | $fields_json, |
| 90 | 90 | $this->getModelVersionInfo()->extraResourcePropertiesForModel($model) |
| 91 | 91 | ); |
| 92 | - $response[ $model_name ]['fields'] = apply_filters( |
|
| 92 | + $response[$model_name]['fields'] = apply_filters( |
|
| 93 | 93 | 'FHEE__Meta__handle_request_models_meta__fields', |
| 94 | 94 | $fields_json, |
| 95 | 95 | $model |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | 'type' => str_replace('EE_', '', get_class($relation_obj)), |
| 102 | 102 | 'single' => $relation_obj instanceof EE_Belongs_To_Relation, |
| 103 | 103 | ); |
| 104 | - $relations_json[ $relation_name ] = $relation_json; |
|
| 104 | + $relations_json[$relation_name] = $relation_json; |
|
| 105 | 105 | } |
| 106 | - $response[ $model_name ]['relations'] = apply_filters( |
|
| 106 | + $response[$model_name]['relations'] = apply_filters( |
|
| 107 | 107 | 'FHEE__Meta__handle_request_models_meta__relations', |
| 108 | 108 | $relations_json, |
| 109 | 109 | $model |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | 'name' => $addon->name(), |
| 129 | 129 | 'version' => $addon->version(), |
| 130 | 130 | ); |
| 131 | - $addons[ $addon_json['name'] ] = $addon_json; |
|
| 131 | + $addons[$addon_json['name']] = $addon_json; |
|
| 132 | 132 | } |
| 133 | 133 | $response_data['ee'] = array( |
| 134 | 134 | 'version' => EEM_System_Status::instance()->get_ee_version(), |
@@ -17,53 +17,53 @@ |
||
| 17 | 17 | class RestApiRequests extends Route |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * returns true if the current request matches this route |
|
| 22 | - * |
|
| 23 | - * @return bool |
|
| 24 | - * @since $VID:$ |
|
| 25 | - */ |
|
| 26 | - public function matchesCurrentRequest(): bool |
|
| 27 | - { |
|
| 28 | - return $this->request->isApi() || $this->request->isWordPressApi(); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * returns true if the current request matches this route |
|
| 22 | + * |
|
| 23 | + * @return bool |
|
| 24 | + * @since $VID:$ |
|
| 25 | + */ |
|
| 26 | + public function matchesCurrentRequest(): bool |
|
| 27 | + { |
|
| 28 | + return $this->request->isApi() || $this->request->isWordPressApi(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @since $VID:$ |
|
| 34 | - */ |
|
| 35 | - protected function registerDependencies() |
|
| 36 | - { |
|
| 37 | - $this->dependency_map->registerDependencies( |
|
| 38 | - 'EventEspresso\core\libraries\rest_api\calculations\Datetime', |
|
| 39 | - [ |
|
| 40 | - 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 41 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 42 | - ] |
|
| 43 | - ); |
|
| 44 | - $this->dependency_map->registerDependencies( |
|
| 45 | - 'EventEspresso\core\libraries\rest_api\calculations\Event', |
|
| 46 | - [ |
|
| 47 | - 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 48 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 49 | - ] |
|
| 50 | - ); |
|
| 51 | - $this->dependency_map->registerDependencies( |
|
| 52 | - 'EventEspresso\core\libraries\rest_api\calculations\Registration', |
|
| 53 | - ['EEM_Registration' => EE_Dependency_Map::load_from_cache] |
|
| 54 | - ); |
|
| 55 | - } |
|
| 32 | + /** |
|
| 33 | + * @since $VID:$ |
|
| 34 | + */ |
|
| 35 | + protected function registerDependencies() |
|
| 36 | + { |
|
| 37 | + $this->dependency_map->registerDependencies( |
|
| 38 | + 'EventEspresso\core\libraries\rest_api\calculations\Datetime', |
|
| 39 | + [ |
|
| 40 | + 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 41 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 42 | + ] |
|
| 43 | + ); |
|
| 44 | + $this->dependency_map->registerDependencies( |
|
| 45 | + 'EventEspresso\core\libraries\rest_api\calculations\Event', |
|
| 46 | + [ |
|
| 47 | + 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 48 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 49 | + ] |
|
| 50 | + ); |
|
| 51 | + $this->dependency_map->registerDependencies( |
|
| 52 | + 'EventEspresso\core\libraries\rest_api\calculations\Registration', |
|
| 53 | + ['EEM_Registration' => EE_Dependency_Map::load_from_cache] |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * implements logic required to run during request |
|
| 60 | - * |
|
| 61 | - * @return bool |
|
| 62 | - * @since $VID:$ |
|
| 63 | - */ |
|
| 64 | - protected function requestHandler(): bool |
|
| 65 | - { |
|
| 66 | - EED_Core_Rest_Api::set_hooks_both(); |
|
| 67 | - return true; |
|
| 68 | - } |
|
| 58 | + /** |
|
| 59 | + * implements logic required to run during request |
|
| 60 | + * |
|
| 61 | + * @return bool |
|
| 62 | + * @since $VID:$ |
|
| 63 | + */ |
|
| 64 | + protected function requestHandler(): bool |
|
| 65 | + { |
|
| 66 | + EED_Core_Rest_Api::set_hooks_both(); |
|
| 67 | + return true; |
|
| 68 | + } |
|
| 69 | 69 | } |