Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.29% covered (danger)
35.29%
6 / 17
33.33% covered (danger)
33.33%
1 / 3
CRAP
n/a
0 / 0
wpcom_rest_api_v2_load_plugin_files
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
wpcom_rest_api_v2_load_plugin
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
load_wpcom_rest_api_v2_plugin_files
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Loader for WP REST API endpoints that are synced with WP.com.
4 *
5 * On WP.com see:
6 *  - wp-content/mu-plugins/rest-api.php
7 *  - wp-content/rest-api-plugins/jetpack-endpoints/
8 *
9 * @package automattic/jetpack
10 */
11
12/**
13 * Disable direct access.
14 */
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Loop through endpoint files and load them.
21 *
22 * @param string $file_pattern Path pattern to the endpoints (pattern must be supported by glob()).
23 */
24function wpcom_rest_api_v2_load_plugin_files( $file_pattern ) {
25    $plugins = glob( __DIR__ . '/' . $file_pattern );
26
27    if ( ! is_array( $plugins ) ) {
28        return;
29    }
30
31    foreach ( array_filter( $plugins, 'is_file' ) as $plugin ) {
32        require_once $plugin;
33    }
34}
35
36/**
37 * API v2 plugins: define a class, then call this function.
38 *
39 * @param string $class_name The name of the class to load.
40 */
41function wpcom_rest_api_v2_load_plugin( $class_name ) {
42    global $wpcom_rest_api_v2_plugins;
43
44    if ( ! isset( $wpcom_rest_api_v2_plugins ) ) {
45        $wpcom_rest_api_v2_plugins = array();
46    }
47
48    if ( ! isset( $wpcom_rest_api_v2_plugins[ $class_name ] ) ) {
49        $wpcom_rest_api_v2_plugins[ $class_name ] = new $class_name();
50    }
51}
52
53require __DIR__ . '/class-wpcom-rest-field-controller.php';
54
55/**
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.
62 */
63function load_wpcom_rest_api_v2_plugin_files() {
64    wpcom_rest_api_v2_load_plugin_files( 'wpcom-endpoints/*.php' );
65    wpcom_rest_api_v2_load_plugin_files( 'wpcom-fields/*.php' );
66}
67add_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.
73if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) && PHPUNIT_JETPACK_TESTSUITE ) {
74    add_action( 'plugins_loaded', 'load_wpcom_rest_api_v2_plugin_files' );
75}