\WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create' ), 'permission_callback' => array( $this, 'can_create' ), 'args' => array( 'post_type' => array( 'type' => 'string', 'default' => 'page', ), 'title' => array( 'type' => 'string', 'required' => true, ), 'blocks' => array( 'type' => array( 'string', 'array' ), 'required' => true, 'items' => array( 'type' => 'string' ), ), 'slug' => array( 'type' => 'string' ), 'seo' => Fields::schema(), 'op_key' => OpKeys::schema( true ), ), ) ); register_rest_route( Plugin::REST_NAMESPACE, '/content/(?P\d+)/status', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'status' ), 'permission_callback' => array( $this, 'can_change_status' ), 'args' => array( 'id' => array( 'type' => 'integer' ), 'status' => array( 'type' => 'string', 'required' => true, 'enum' => StatusChanger::STATUSES, ), 'expected_modified_gmt' => array( 'type' => 'string', 'required' => true, ), 'op_key' => OpKeys::schema( true ), ), ) ); } /** * Whether the caller may create posts of the requested type. An unknown * or unmanaged type is left to the handler, which answers 400. * * @param \WP_REST_Request $request Request. * @return bool */ public function can_create( \WP_REST_Request $request ) { $type = get_post_type_object( (string) $request['post_type'] ); return $type ? current_user_can( $type->cap->create_posts ) : current_user_can( 'edit_posts' ); } /** * Whether the caller may edit the post and, to publish it, publish posts * of its type. * * @param \WP_REST_Request $request Request. * @return bool */ public function can_change_status( \WP_REST_Request $request ) { if ( ! Posts::can_edit( $request ) ) { return false; } $post = get_post( (int) $request['id'] ); if ( $post && 'publish' === $request['status'] ) { $type = get_post_type_object( $post->post_type ); return $type && current_user_can( $type->cap->publish_posts ); } return true; } /** * Creates a draft. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function create( \WP_REST_Request $request ) { $replay = OpKeys::replay( $request['op_key'], 'create', 0 ); if ( null !== $replay ) { return Posts::respond( $replay ); } $type = get_post_type_object( (string) $request['post_type'] ); if ( ! $type || 'attachment' === $type->name || ! $type->public || ! $type->show_in_rest ) { return new \WP_Error( 'searchsmith_unsupported_type', __( 'This kind of post is not managed by Blaze.', 'searchsmith' ), array( 'status' => 400 ) ); } return Posts::respond( PostCreator::create( array( 'post_type' => $type->name, 'title' => (string) $request['title'], 'blocks' => $request['blocks'], 'slug' => (string) $request['slug'], 'seo' => $request['seo'], 'op_key' => (string) $request['op_key'], ) ) ); } /** * Publishes a post or moves it to draft. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function status( \WP_REST_Request $request ) { $post = Posts::supported( (int) $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $replay = OpKeys::replay( $request['op_key'], 'status', $post->ID ); if ( null !== $replay ) { return Posts::respond( $replay ); } return Posts::respond( StatusChanger::change( $post, array( 'status' => (string) $request['status'], 'expected_modified_gmt' => (string) $request['expected_modified_gmt'], 'op_key' => (string) $request['op_key'], ) ) ); } }