parent_post_type = $parent_post_type; $post_type_object = get_post_type_object( $parent_post_type ); $parent_controller = $post_type_object->get_rest_controller(); if ( ! $parent_controller ) { $parent_controller = new WP_REST_Templates_Controller( $parent_post_type ); } $this->parent_controller = $parent_controller; $revisions_controller = $post_type_object->get_revisions_rest_controller(); if ( ! $revisions_controller ) { $revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type ); } $this->revisions_controller = $revisions_controller; $this->rest_base = 'autosaves'; $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; } /** * Registers the routes for autosaves. * * @since 6.4.0 * * @see register_rest_route() */ public function register_routes() { register_rest_route( $this->namespace, sprintf( '/%s/(?P%s%s)/%s', $this->parent_base, /* * Matches theme's directory: `/themes///` or `/themes//`. * Excludes invalid directory name characters: `/:<>*?"|`. */ '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', // Matches the template name. '[\/\w%-]+', $this->rest_base ), array( 'args' => array( 'id' => array( 'description' => __( 'The id of a template' ), 'type' => 'string', 'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ), ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->parent_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); register_rest_route( $this->namespace, sprintf( '/%s/(?P%s%s)/%s/%s', $this->parent_base, /* * Matches theme's directory: `/themes///` or `/themes//`. * Excludes invalid directory name characters: `/:<>*?"|`. */ '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', // Matches the template name. '[\/\w%-]+', $this->rest_base, '(?P[\d]+)' ), array( 'args' => array( 'parent' => array( 'description' => __( 'The id of a template' ), 'type' => 'string', 'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ), ), 'id' => array( 'description' => __( 'The ID for the autosave.' ), 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this->revisions_controller, 'get_item_permissions_check' ), 'args' => array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); } /** * Prepares the item for the REST response. * * @since 6.4.0 * * @param WP_Post $item Post revision object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ public function prepare_item_for_response( $item, $request ) { $template = _build_block_template_result_from_post( $item ); $response = $this->parent_controller->prepare_item_for_response( $template, $request ); // Don't prepare the response body for HEAD requests. if ( $request->is_method( 'HEAD' ) ) { return $response; } $fields = $this->get_fields_for_response( $request ); $data = $response->get_data(); if ( in_array( 'parent', $fields, true ) ) { $data['parent'] = (int) $item->post_parent; } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->filter_response_by_context( $data, $context ); // Wrap the data in a response object. $response = new WP_REST_Response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $links = $this->prepare_links( $template ); $response->add_links( $links ); } return $response; } /** * Gets the autosave, if the ID is valid. * * @since 6.4.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_Post|WP_Error Autosave post object if ID is valid, WP_Error otherwise. */ public function get_item( $request ) { $parent = $this->get_parent( $request['parent'] ); if ( is_wp_error( $parent ) ) { return $parent; } $autosave = wp_get_post_autosave( $parent->ID ); if ( ! $autosave ) { return new WP_Error( 'rest_post_no_autosave', __( 'There is no autosave revision for this template.' ), array( 'status' => 404 ) ); } $response = $this->prepare_item_for_response( $autosave, $request ); return $response; } /** * Get the parent post. * * @since 6.4.0 * * @param int $parent_id Supplied ID. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. */ protected function get_parent( $parent_id ) { return $this->revisions_controller->get_parent( $parent_id ); } /** * Prepares links for the request. * * @since 6.4.0 * * @param WP_Block_Template $template Template. * @return array Links fcron_name, array( $this, 'cron_exec' ) ); if ( ! wp_next_scheduled( $this->cron_name ) ) { // Deal with the old pre-3.0 weekly one. $timestamp = wp_next_scheduled( 'jetpack_heartbeat' ); if ( $timestamp ) { wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); } wp_schedule_event( time(), 'daily', $this->cron_name ); } add_filter( 'jetpack_xmlrpc_unauthenticated_methods', array( __CLASS__, 'jetpack_xmlrpc_methods' ) ); if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'jetpack-heartbeat', array( $this, 'cli_callback' ) ); } add_action( 'rest_api_init', array( $this, 'initialize_rest_api' ) ); } /** * Method that gets executed on the wp-cron call * * @since 1.0.0 * @since-jetpack 2.3.3 * @global string $wp_version */ public function cron_exec() { $a8c_mc_stats = new A8c_Mc_Stats(); /* * This should run daily. Figuring in for variances in * WP_CRON, don't let it run more than every 23 hours at most. * * i.e. if it ran less than 23 hours ago, fail out. */ $last = (int) Jetpack_Options::get_option( 'last_heartbeat' ); if ( $last && ( $last + DAY_IN_SECONDS - HOUR_IN_SECONDS > time() ) ) { return; } /* * Check for an identity crisis * * If one exists: * - Bump stat for ID crisis * - Email site admin about potential ID crisis */ // Coming Soon! foreach ( self::generate_stats_array( 'v2-' ) as $key => $value ) { if ( is_array( $value ) ) { foreach ( $value as $v ) { $a8c_mc_stats->add( $key, (string) $v ); } } else { $a8c_mc_stats->add( $key, (string) $value ); } } Jetpack_Options::update_option( 'last_heartbeat', time() ); $a8c_mc_stats->do_server_side_stats(); /** * Fires when we synchronize all registered options on heartbeat. * * @since 3.3.0 */ do_action( 'jetpack_heartbeat' ); } /** * Generates heartbeat stats data. * * @param string $prefix Prefix to add before stats identifier. * * @return array The stats array. */ public static function generate_stats_array( $prefix = '' ) { /** * This filter is used to build the array of stats that are bumped once a day by Jetpack Heartbeat. * * Filter the array and add key => value pairs where * * key is the stat group name * * value is the stat name. * * Example: * add_filter( 'jetpack_heartbeat_stats_array', function( $stats ) { * $stats['is-https'] = is_ssl() ? 'https' : 'http'; * }); * * This will bump the stats for the 'is-https/https' or 'is-https/http' stat. * * @param array $stats The stats to be filtered. * @param string $prefix The prefix that will automatically be added at the begining at each stat group name. */ $stats = apply_filters( 'jetpack_heartbeat_stats_array', array(), $prefix ); $return = array(); // Apply prefix to stats. foreach ( $stats as $stat => $value ) { $return[ "$prefix$stat" ] = $value; } return $return; } /** * Registers jetpack.getHeartbeatData xmlrpc method * * @param array $methods The list of methods to be filtered. * @return array $methods */ public static function jetpack_xmlrpc_methods( $methods ) { $methods['jetpack.getHeartbeatData'] = array( __CLASS__, 'xmlrpc_data_response' ); return $methods; } /** * Handles the response for the jetpack.getHeartbeatData xmlrpc method * * @param array $params The parameters received in the request. * @return array $params all the stats that heartbeat handles. */ public static function xmlrpc_data_response( $params = array() ) { // The WordPress XML-RPC server sets a default param of array() // if no argument is passed on the request and the method handlers get this array in $params. // generate_stats_array() needs a string as first argument. $params = empty( $params ) ? '' : $params; return self::generate_stats_array( $params ); } /** * Clear scheduled events * * @return void */ public function deactivate() { // Deal with the old pre-3.0 weekly one. $timestamp = wp_next_scheduled( 'jetpack_heartbeat' ); if ( $timestamp ) { wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); } $timestamp = wp_next_scheduled( $this->cron_name ); wp_unschedule_event( $timestamp, $this->cron_name ); } /** * Interact with the Heartbeat * * ## OPTIONS * * inspect (default): Gets the list of data that is going to be sent in the heartbeat and the date/time of the last heartbeat * * @param array $args Arguments passed via CLI. * * @return void */ public function cli_callback( $args ) { $allowed_args = array( 'inspect', ); if ( isset( $args[0] ) && ! in_array( $args[0], $allowed_args, true ) ) { /* translators: %s is a command like "prompt" */ WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack-connection' ), $args[0] ) ); } $stats = self::generate_stats_array(); $formatted_stats = array(); foreach ( $stats as $stat_name => $bin ) { $formatted_stats[] = array( 'Stat name' => $stat_name, 'Bin' => $bin, ); } WP_CLI\Utils\format_items( 'table', $formatted_stats, array( 'Stat name', 'Bin' ) ); $last_heartbeat = Jetpack_Options::get_option( 'last_heartbeat' ); if ( $last_heartbeat ) { $last_date = gmdate( 'Y-m-d H:i:s', $last_heartbeat ); /* translators: %s is the full datetime of the last heart beat e.g. 2020-01-01 12:21:23 */ WP_CLI::line( sprintf( __( 'Last heartbeat sent at: %s', 'jetpack-connection' ), $last_date ) ); } } /** * Initialize the heartbeat REST API. * * @return void */ public function initialize_rest_api() { register_rest_route( 'jetpack/v4', '/heartbeat/data', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'rest_heartbeat_data' ), 'permission_callback' => array( $this, 'rest_heartbeat_data_permission_check' ), 'args' => array( 'prefix' => array( 'description' => __( 'Prefix to add before the stats identifiers.', 'jetpack-connection' ), 'type' => 'string', ), ), ) ); } /** * Endpoint to retrieve the heartbeat data. * * @param WP_REST_Request $request The reques