| @@ -15,8 +15,15 @@ | ||
| 15 | 15 | generate_access_token |
| 16 | 16 | }; |
| 17 | 17 | use function WPE\FaustWP\Settings\get_secret_key; |
| 18 | 18 | |
| 19 | +use function WPE\FaustWP\Telemetry\{ | |
| 20 | + get_wp_version, | |
| 21 | + is_wpe, | |
| 22 | + get_anonymous_faustwp_data, | |
| 23 | + get_anonymous_wpgraphql_content_blocks_data, | |
| 24 | +}; | |
| 25 | + | |
| 19 | 26 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | 27 | exit; |
| 21 | 28 | } |
| 22 | 29 | |
| @@ -58,8 +65,9 @@ | ||
| 58 | 65 | add_action( 'rest_api_init', __NAMESPACE__ . '\\register_rest_routes' ); |
| 59 | 66 | /** |
| 60 | 67 | * Callback for WordPress 'rest_api_init' action. |
| 61 | 68 | * |
| 69 | + * Register the GET /faustwp/v1/telemetry endpoint. | |
| 62 | 70 | * Register the POST /faustwp/v1/authorize endpoint. |
| 63 | 71 | * |
| 64 | 72 | * @link https://developer.wordpress.org/reference/functions/register_rest_route/ |
| 65 | 73 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/ |
| @@ -68,8 +76,18 @@ | ||
| 68 | 76 | */ |
| 69 | 77 | function register_rest_routes() { |
| 70 | 78 | register_rest_route( |
| 71 | 79 | 'faustwp/v1', |
| 80 | + '/telemetry', | |
| 81 | + array( | |
| 82 | + 'methods' => 'POST', | |
| 83 | + 'callback' => __NAMESPACE__ . '\\handle_rest_telemetry_callback', | |
| 84 | + 'permission_callback' => __NAMESPACE__ . '\\rest_telemetry_permission_callback', | |
| 85 | + ) | |
| 86 | + ); | |
| 87 | + | |
| 88 | + register_rest_route( | |
| 89 | + 'faustwp/v1', | |
| 72 | 90 | '/authorize', |
| 73 | 91 | array( |
| 74 | 92 | 'methods' => 'POST', |
| 75 | 93 | 'callback' => __NAMESPACE__ . '\\handle_rest_authorize_callback', |
| @@ -90,8 +108,46 @@ | ||
| 90 | 108 | 'callback' => __NAMESPACE__ . '\\handle_rest_authorize_callback', |
| 91 | 109 | 'permission_callback' => __NAMESPACE__ . '\\wpac_authorize_permission_callback', |
| 92 | 110 | ) |
| 93 | 111 | ); |
| 112 | +} | |
| 113 | + | |
| 114 | +/** | |
| 115 | + * Callback for WordPress register_rest_route() 'callback' parameter. | |
| 116 | + * | |
| 117 | + * Handle GET /faustwp/v1/telemetry response. | |
| 118 | + * | |
| 119 | + * @link https://developer.wordpress.org/reference/functions/register_rest_route/ | |
| 120 | + * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/#endpoint-callback | |
| 121 | + * | |
| 122 | + * @param \WP_REST_Request $request Current WP_REST_Request object. | |
| 123 | + * | |
| 124 | + * @return mixed A WP_REST_Response, array, or WP_Error. | |
| 125 | + */ | |
| 126 | +function handle_rest_telemetry_callback( \WP_REST_Request $request ) { | |
| 127 | + $data = array( | |
| 128 | + 'faustwp' => get_anonymous_faustwp_data(), | |
| 129 | + 'wpgraphql_content_blocks' => get_anonymous_wpgraphql_content_blocks_data(), | |
| 130 | + 'is_wpe' => is_wpe(), | |
| 131 | + 'multisite' => is_multisite(), | |
| 132 | + 'php_version' => PHP_VERSION, | |
| 133 | + 'wp_version' => get_wp_version(), | |
| 134 | + ); | |
| 135 | + | |
| 136 | + return new \WP_REST_Response( $data ); | |
| 137 | +} | |
| 138 | + | |
| 139 | +/** | |
| 140 | + * Callback to check permissions for requests to `faustwp/v1/telemetry`. | |
| 141 | + * | |
| 142 | + * Authorized if the 'secret_key' settings value and http header 'x-faustwp-secret' match. | |
| 143 | + * | |
| 144 | + * @param \WP_REST_Request $request The current WP_REST_Request object. | |
| 145 | + * | |
| 146 | + * @return bool True if current user can, false if else. | |
| 147 | + */ | |
| 148 | +function rest_telemetry_permission_callback( \WP_REST_Request $request ) { | |
| 149 | + return rest_authorize_permission_callback( $request ); | |
| 94 | 150 | } |
| 95 | 151 | |
| 96 | 152 | /** |
| 97 | 153 | * Callback for WordPress register_rest_route() 'callback' parameter. |