'object', 'additionalProperties' => false, 'required' => array( 'id' ), 'properties' => array( 'id' => array( 'type' => 'integer', 'description' => __( 'The post id to update.', 'gripmcp' ), 'minimum' => 1, ), 'title' => array( 'type' => 'string', 'description' => __( 'New title.', 'gripmcp' ), 'maxLength' => 500, ), 'content' => array( 'type' => 'string', 'description' => __( 'New body content.', 'gripmcp' ), ), 'excerpt' => array( 'type' => 'string', 'description' => __( 'New excerpt.', 'gripmcp' ), ), 'status' => array( 'type' => 'string', 'description' => __( 'New status.', 'gripmcp' ), 'enum' => array( 'draft', 'pending', 'publish', 'private', 'future' ), ), 'author' => array( 'type' => 'string', 'description' => __( 'Reassign the post to a user: numeric ID, login, or email address. Requires the "edit_others_posts" capability.', 'gripmcp' ), ), 'slug' => array( 'type' => 'string', 'description' => __( 'New URL slug.', 'gripmcp' ), ), 'date_gmt' => array( 'type' => 'string', 'description' => __( 'New publish date in GMT/UTC (Y-m-d H:i:s). To schedule a post, pass status "future" with a date in the future.', 'gripmcp' ), ), ), ); } protected function required_capability(): string { return 'edit_posts'; } protected function is_idempotent(): bool { return true; } /** * The code this call would add to the post as it is now * (Support\Added_Code). A theme's Additional CSS has its own rules. * * @param array $arguments Validated arguments. * @return array */ public function added_code( array $arguments ): array { $post = get_post( (int) ( $arguments['id'] ?? 0 ) ); if ( ! $post instanceof \WP_Post || Custom_Css::is_css( $post ) ) { return array(); } $fields = array(); foreach ( array( 'title' => 'post_title', 'content' => 'post_content', 'excerpt' => 'post_excerpt' ) as $argument => $column ) { if ( array_key_exists( $argument, $arguments ) ) { $fields[ $column ] = (string) $arguments[ $argument ]; } } return Added_Code::in_post( $post, $fields ); } /** * A change that adds code, on a plan that allows it, is held for * approval like a delete, so the person sees the code first. * * @param array $arguments Validated arguments. */ public function is_destructive_call( array $arguments ): bool { return Added_Code::allowed() && ! empty( $this->added_code( $arguments ) ); } /** * What "Destructive actions: Off" says about it. * * @param array $arguments Validated arguments. */ public function destructive_refusal( array $arguments ): string { return Added_Code::off_message( $this->added_code( $arguments ) ); } /** * The post as it is when the change is held: changed before anyone * approves, the code on the approval page may no longer be all it adds. * * @param array $arguments Validated arguments. */ public function approval_state( array $arguments ): string { return empty( $this->added_code( $arguments ) ) ? '' : Added_Code::post_state( (int) ( $arguments['id'] ?? 0 ) ); } protected function run( array $arguments, Request_Context $context ): Tool_Result { $id = (int) $arguments['id']; $post = get_post( $id ); if ( ! $post instanceof \WP_Post ) { return $this->fail( sprintf( /* translators: %d: post id. */ __( 'No post found with id %d.', 'gripmcp' ), $id ) ); } // Additional CSS answers to edit_css: its post type maps edit_post to // a capability no role holds. $is_css = Custom_Css::is_css( $post ); if ( $is_css && ! Custom_Css::writable() ) { return $this->fail( Custom_Css::read_only() ); } if ( $is_css && ! Custom_Css::can_edit( $context ) ) { return $this->fail( Custom_Css::forbidden() ); } if ( ! $is_css && ! $context->user_can( 'edit_post', $id ) ) { return $this->fail( __( 'You do not have permission to edit this post.', 'gripmcp' ) ); } // Orders and fonts are not changed as plain posts at all: refused // before any field is looked at. $locked = Post_Access::change_error( $post, Post_Access::CHANGE_UPDATE ); if ( '' !== $locked ) { return $this->fail( $locked ); } if ( $is_css && array_key_exists( 'slug', $arguments ) ) { return $this->fail( __( 'WordPress finds a theme\'s Additional CSS by the theme\'s folder name, which is its slug, so the slug cannot change. Nothing was changed.', 'gripmcp' ) ); } if ( $is_css && array_key_exists( 'content', $arguments ) ) { $markup = Custom_Css::markup_error( (string) $arguments['content'] ); if ( null !== $markup ) { return $this->fail( $markup ); } } $postarr = array( 'ID' => $id ); if ( array_key_exists( 'title', $arguments ) ) { $postarr['post_title'] = (string) $arguments['title']; } if ( array_key_exists( 'content', $arguments ) ) { // The update would succeed and change nothing visitors see. if ( Elementor_Support::builds( $id ) ) { return $this->fail( Elementor_Support::body_note( $id ) ); } $postarr['post_content'] = (string) $arguments['content']; } if ( array_key_exists( 'excerpt', $arguments ) ) { $postarr['post_excerpt'] = (string) $arguments['excerpt']; } if ( array_key_exists( 'slug', $arguments ) ) { $postarr['post_name'] = sanitize_title( (string) $arguments['slug'] ); } if ( array_key_exists( 'status', $arguments ) ) { $status = (string) $arguments['status']; $type_object = get_post_type_object( $post->post_type ); if ( $type_object && $status !== $post->post_status ) { $status_error = Post_Access::status_error( $status, $type_object, $context ); if ( '' !== $status_error ) { return $this->fail( $status_error ); } } $postarr['post_status'] = $status; } // The date is GMT already. Both columns are set, and edit_date, or // WordPress keeps a draft's old date or recomputes it from site time. $date_gmt = ''; if ( array_key_exists( 'date_gmt', $arguments ) && '' !== trim( (string) $arguments['date_gmt'] ) ) { $date_gmt = Post_Schedule::parse_gmt( (string) $arguments['date_gmt'] ); if ( null === $date_gmt ) { return $this->fail( __( 'date_gmt is not a date WordPress can use. Give it as Y-m-d H:i:s in GMT, for example 2026-11-01 09:00:00.', 'gripmcp' ) ); } $postarr['post_date_gmt'] = $date_gmt; $postarr['post_date'] = get_date_from_gmt( $date_gmt ); $postarr['edit_date'] = true; } if ( isset( $postarr['post_status'] ) && 'future' === $postarr['post_status'] ) { $future_error = Post_Schedule::future_error( '' !== $date_gmt ? $date_gmt : (string) $post->post_date_gmt ); if ( '' !== $future_error ) { return $this->fail( $future_error ); } } if ( array_key_exists( 'author', $arguments ) && '' !== trim( (string) $arguments['author'] ) ) { $resolved = $this->resolve_user( (string) $arguments['author'] ); if ( is_wp_error( $resolved ) ) { return $this->fail( $resolved->get_error_message() ); } if ( $resolved !== (int) $post->post_author && ! $context->user_can( 'edit_others_posts' ) ) { return $this->fail( __( 'You do not have permission to reassign posts to other users (requires the "edit_others_posts" capability).', 'gripmcp' ) ); } $postarr['post_author'] = $resolved; } if ( count( $postarr ) === 1 ) { return $this->fail( __( 'No fields provided to update.', 'gripmcp' ) ); } // Templates keep their slug and stay published, and the site's styles // stay a document WordPress reads. Checked before anything is written, // the restore from the trash below included. $change = Post_Access::change_error( $post, Post_Access::CHANGE_UPDATE, $postarr ); if ( '' !== $change ) { return $this->fail( $change ); } // Code already in the post stays; code the change adds is left to // the person, unless the plan adds code with approval. $code = $is_css ? '' : Added_Code::refusal( Added_Code::in_post( $post, $postarr ) ); if ( '' !== $code ) { return $this->fail( $code ); } // Out of the trash the way wp-admin takes a post out: wp_untrash_post() // clears the record of the status it had and brings back the comments // trashed with it. The status asked for is then applied below. if ( 'trash' === $post->post_status && isset( $postarr['post_status'] ) && 'trash' !== $postarr['post_status'] ) { $untrashed = self::untrash( $post, $context ); if ( '' !== $untrashed ) { return $this->fail( $untrashed ); } } $result = wp_update_post( wp_slash( $postarr ), true ); if ( is_wp_error( $result ) ) { return $this->fail( sprintf( /* translators: %s: error message. */ __( 'Failed to update post: %s', 'gripmcp' ), $result->get_error_message() ) ); } $fresh = get_post( $id ); $payload = Post_Presenter::to_array( $fresh, false ); $summary = sprintf( /* translators: %d: post id. */ __( 'Updated post %d.', 'gripmcp' ), $id ); if ( $fresh instanceof \WP_Post ) { $asked = isset( $postarr['post_status'] ) ? (string) $postarr['post_status'] : ( '' !== $date_gmt ? $post->post_status : '' ); $note = Post_Schedule::status_note( $asked, $fresh ); if ( '' !== $note ) { $summary .= ' ' . $note; } // WordPress gives a taken slug a number ("-2") without saying so. $slug_note = isset( $postarr['post_name'] ) ? Post_Presenter::slug_note( $fresh, (string) $postarr['post_name'] ) : ''; if ( '' !== $slug_note ) { $summary .= ' ' . $slug_note; } } if ( $is_css && $fresh instanceof \WP_Post ) { $after = Custom_Css::after_save( $fresh ); $payload['refreshed'] = $after['refreshed']; if ( '' !== $after['summary'] ) { $summary .= ' ' . $after['summary']; } if ( ! empty( $after['notes'] ) ) { $payload['notes'] = $after['notes']; $summary .= ' ' . implode( ' ', $after['notes'] ); } } return $this->data( $payload, $summary ); } /** * Take a trashed post out of the trash, as wp-admin's Restore does, or * say why not. Shared with wp_cli post update. * * @param \WP_Post $post A post in the trash. * @param Request_Context $context Context. * @return string Why it could not be restored, or ''. */ public static function untrash( \WP_Post $post, Request_Context $context ): string { // wp-admin asks for the delete capability to restore from the trash. if ( ! $context->user_can( 'delete_post', (int) $post->ID ) ) { return __( 'Your WordPress account cannot restore this from the trash, so nothing was changed.', 'gripmcp' ); } if ( ! wp_untrash_post( (int) $post->ID ) ) { return __( 'WordPress could not take this out of the trash, so nothing was changed.', 'gripmcp' ); } return ''; } /** * Resolve a user reference (ID, login, or email) to a user ID. * * @param string $reference User reference. * @return int|\WP_Error */ private function resolve_user( string $reference ) { $reference = trim( $reference ); if ( ctype_digit( $reference ) ) { $user = get_userdata( (int) $reference ); } elseif ( is_email( $reference ) ) { $user = get_user_by( 'email', $reference ); } else { $user = get_user_by( 'login', $reference ); if ( ! $user ) { $user = get_user_by( 'slug', $reference ); } } if ( ! $user ) { return new \WP_Error( 'gripmcp_user_not_found', sprintf( /* translators: %s: user reference. */ __( 'No user matches "%s". Use wp_users_list to find the correct ID or login.', 'gripmcp' ), $reference ) ); } return (int) $user->ID; } }