| @@ -12,9 +12,9 @@ | ||
| 12 | 12 | /** |
| 13 | 13 | * Disable direct access. |
| 14 | 14 | */ |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; | |
| 16 | + exit( 0 ); | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Loop through endpoint files and load them. |
| @@ -41,10 +41,9 @@ | ||
| 41 | 41 | function wpcom_rest_api_v2_load_plugin( $class_name ) { |
| 42 | 42 | global $wpcom_rest_api_v2_plugins; |
| 43 | 43 | |
| 44 | 44 | if ( ! isset( $wpcom_rest_api_v2_plugins ) ) { |
| 45 | - $wpcom_rest_api_v2_plugins = array(); | |
| 46 | - $_GLOBALS['wpcom_rest_api_v2_plugins'] = array(); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase,VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable | |
| 45 | + $wpcom_rest_api_v2_plugins = array(); | |
| 47 | 46 | } |
| 48 | 47 | |
| 49 | 48 | if ( ! isset( $wpcom_rest_api_v2_plugins[ $class_name ] ) ) { |
| 50 | 49 | $wpcom_rest_api_v2_plugins[ $class_name ] = new $class_name(); |
| @@ -53,11 +52,24 @@ | ||
| 53 | 52 | |
| 54 | 53 | require __DIR__ . '/class-wpcom-rest-field-controller.php'; |
| 55 | 54 | |
| 56 | 55 | /** |
| 57 | - * Load the REST API v2 plugin files during the plugins_loaded action. | |
| 56 | + * Load the REST API v2 plugin files. | |
| 57 | + * | |
| 58 | + * Hooked to `rest_api_init` (priority 5) so the ~37 endpoint class files only | |
| 59 | + * load on REST requests, not on every front-end page. Priority 5 ensures the | |
| 60 | + * loader runs before constructors register their own `register_routes` callbacks | |
| 61 | + * at the default priority 10 within the same `rest_api_init` action. | |
| 58 | 62 | */ |
| 59 | 63 | function load_wpcom_rest_api_v2_plugin_files() { |
| 60 | 64 | wpcom_rest_api_v2_load_plugin_files( 'wpcom-endpoints/*.php' ); |
| 61 | 65 | wpcom_rest_api_v2_load_plugin_files( 'wpcom-fields/*.php' ); |
| 62 | 66 | } |
| 63 | -add_action( 'plugins_loaded', 'load_wpcom_rest_api_v2_plugin_files' ); | |
| 67 | +add_action( 'rest_api_init', 'load_wpcom_rest_api_v2_plugin_files', 5 ); | |
| 68 | + | |
| 69 | +// In the PHPUnit test suite, fire the loader during `plugins_loaded` so all 37 endpoint | |
| 70 | +// classes are defined before PHPUnit performs test discovery. Test files use | |
| 71 | +// `#[CoversClass( WPCOM_REST_API_V2_Endpoint_*::class )]` attributes that PHPUnit eagerly | |
| 72 | +// reflects, requiring the classes to be loadable at that moment. | |
| 73 | +if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) && PHPUNIT_JETPACK_TESTSUITE ) { | |
| 74 | + add_action( 'plugins_loaded', 'load_wpcom_rest_api_v2_plugin_files' ); | |
| 75 | +} | |