\d+)/seo', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'read_seo' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => array( 'type' => 'integer' ) ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/edit', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'edit' ), 'permission_callback' => array( Posts::class, 'can_edit' ), 'args' => array( 'id' => array( 'type' => 'integer' ), 'expected_modified_gmt' => array( 'type' => 'string', 'required' => true, ), 'change_id' => array( 'type' => 'string', 'required' => true, 'pattern' => '^[A-Za-z0-9_-]{1,64}$', ), 'title' => array( 'type' => 'string' ), 'content' => array( 'type' => 'string' ), 'excerpt' => array( 'type' => 'string' ), 'seo' => Fields::schema(), 'op_key' => OpKeys::schema( false ), ), ) ); } /** * Returns a post's SEO fields and what the connector knows about it. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function read_seo( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $store = Stores::current(); return rest_ensure_response( array( 'id' => $post->ID, 'post_type' => $post->post_type, 'modified_gmt' => ContentEditor::modified( $post ), 'seo_plugin' => Environment::seo_plugin(), 'supported' => null !== $store, 'builder' => Environment::builder_for_post( $post->ID ), 'seo' => $store ? $store->read( $post->ID ) : null, ) ); } /** * Applies a guarded edit. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function edit( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $replay = OpKeys::replay( $request['op_key'], 'edit', $post->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } $params = array_intersect_key( $request->get_params(), array_flip( array( 'expected_modified_gmt', 'change_id', 'title', 'content', 'excerpt', 'seo' ) ) ); $result = ContentEditor::edit( $post, $params ); if ( is_wp_error( $result ) ) { return $result; } if ( null !== $request['op_key'] ) { $result['op_key'] = (string) $request['op_key']; OpKeys::record( $result['op_key'], 'edit', $post->ID, $result['revision_ids'] ? $result['revision_ids'][0] : $post->ID, $result ); } return rest_ensure_response( $result ); } }