array( 'live' => array( 'class' => Logs_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( 'get_last_item' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'extract_last_item', ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'severity' => array( 'class' => Logs_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'set_severity_status', ), 'args' => array( 'severity_name' => array( 'required' => true, 'type' => 'string', 'description' => 'Severity name', ), 'status' => array( 'required' => true, 'type' => 'string', 'description' => 'Severity status', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'single_severity' => array( 'class' => Logs_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'set_single_severity', ), 'args' => array( 'severity_name' => array( 'required' => true, 'type' => 'string', 'description' => 'Severity name', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'requests' => array( 'class' => Requests_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'set_request_status', ), 'args' => array( 'request_type' => array( 'required' => true, 'type' => 'string', 'description' => 'Request type', ), 'status' => array( 'required' => true, 'type' => 'string', 'description' => 'Severity status', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'cron_run' => array( 'class' => Crons_Helper::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'run_cron_api', ), 'args' => array( 'cron_hash' => array( 'required' => true, 'type' => 'string', 'description' => 'Cron has to execute', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'drop_table' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::DELETABLE, 'callback' => 'drop_table', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'truncate_table' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::DELETABLE, 'callback' => 'truncate_table', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'get_table_record' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P.+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'extract_row_data', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), 'id' => array( 'required' => true, 'type' => 'string', 'pattern' => '.+', 'description' => 'ID of the table record which row needs to be shown', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'get_fatals_record' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P.+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'extract_fatals_row_data', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), 'id' => array( 'required' => true, 'type' => 'string', 'pattern' => '.+', 'description' => 'ID of the table record which row needs to be shown', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'get_hook_capture_record' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P.+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'extract_hook_capture_row_data', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), 'id' => array( 'required' => true, 'type' => 'string', 'pattern' => '.+', 'description' => 'ID of the hook capture record which row needs to be shown', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'get_url_record' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\w+)/(?P.+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'extract_url_record_data', ), 'args' => array( 'table_name' => array( 'required' => true, 'type' => 'string', 'pattern' => '\w+', 'description' => 'Table name', ), 'id' => array( 'required' => true, 'type' => 'string', 'pattern' => '.+', 'description' => 'ID of the URL record which row needs to be shown', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'collect_assets' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\d+)' => array( 'methods' => array( 'method' => \WP_REST_Server::CREATABLE, 'callback' => 'collect_assets', ), 'args' => array( 'id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', 'description' => 'URL record ID', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'get_transient_record' => array( 'class' => Transients_Helper::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\d+)' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'get_transient_by_id', ), 'args' => array( 'transient_id' => array( 'required' => true, 'type' => 'string', 'pattern' => '\d+', 'description' => 'Transient hash', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'mail_body' => array( 'class' => WP_Mail_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\d+)/' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'get_mail_body_api', ), 'args' => array( 'id' => array( 'required' => true, 'type' => 'integer', 'pattern' => '\d+', 'description' => 'ID of the mail record which body needs to be extracted', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'system-status' => array( 'class' => System_Analytics::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( 'get-status' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'rest_get_system_status', ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'pagespeed_api_data' => array( 'class' => PageSpeed_List::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( 'pagespeed_api_data' => array( 'methods' => array( 'method' => \WP_REST_Server::READABLE, 'callback' => 'get_pagespeed_api_data', ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), 'collect_pagespeed' => array( 'class' => Common_Table::class, 'namespace' => self::ENDPOINT_ROOT_NAME . '/v1', 'endpoints' => array( array( '(?P\d+)' => array( 'methods' => array( 'method' => \WP_REST_Server::CREATABLE, 'callback' => 'collect_pagespeed', ), 'args' => array( 'id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', 'description' => 'URL record ID', ), ), 'checkPermissions' => array( __CLASS__, 'check_permissions' ), 'showInIndex' => false, ), ), ), ), ), ); /** * Inits the class * * @return void * * @since 1.9.2 */ public static function init() { \add_action( 'rest_api_init', array( __CLASS__, 'init_endpoints' ) ); } /** * Inits all the endpoints from given structure * * @return void * * @since 1.9.2 */ public static function init_endpoints() { foreach ( self::$endpoints as $endpoint_provider ) { foreach ( $endpoint_provider as $root_endpoint => $settings ) { $class = $settings['class']; $namespace = $settings['namespace']; foreach ( $settings['endpoints'] as $routes ) { foreach ( $routes as $route => $endpoint ) { $args = array(); if ( isset( $endpoint['args'] ) ) { $args = $endpoint['args']; } $check_permissions = array(); if ( isset( $endpoint['checkPermissions'] ) ) { $check_permissions = $endpoint['checkPermissions']; } $show_in_index = $endpoint['showInIndex']; \register_rest_route( $namespace, '/' . $root_endpoint . '/' . $route, array( array( 'methods' => $endpoint['methods']['method'], 'callback' => array( $class, $endpoint['methods']['callback'] ), 'args' => $args, 'permission_callback' => $check_permissions, 'show_in_index' => $show_in_index, ), ), false ); } } } } } /** * Global method to check permissions for API endpoints - this one checks if the user has admin capability. * * @return bool * * @since 1.9.2 */ public static function check_permissions() { return \current_user_can( 'manage_options' ); } } }