'object', 'additionalProperties' => false, 'required' => array( 'title' ), 'properties' => array( 'title' => array( 'type' => 'string', 'description' => __( 'The post title.', 'gripmcp' ), 'minLength' => 1, 'maxLength' => 500, ), 'content' => array( 'type' => 'string', 'description' => __( 'The post body (HTML or block markup).', 'gripmcp' ), 'default' => '', ), 'excerpt' => array( 'type' => 'string', 'description' => __( 'Optional manual excerpt.', 'gripmcp' ), 'default' => '', ), 'status' => array( 'type' => 'string', 'description' => __( 'Post status.', 'gripmcp' ), 'enum' => array( 'draft', 'pending', 'publish', 'private', 'future' ), 'default' => 'draft', ), 'post_type' => array( 'type' => 'string', 'description' => __( 'Post type. Defaults to post. Media, menu items and WooCommerce products, orders and coupons have their own tools.', 'gripmcp' ), 'default' => 'post', ), 'author' => array( 'type' => 'string', 'description' => __( 'Assign the post to a user: numeric ID, login, or email address. Defaults to you. Assigning to someone else requires the "edit_others_posts" capability.', 'gripmcp' ), ), 'slug' => array( 'type' => 'string', 'description' => __( 'Optional URL slug.', 'gripmcp' ), ), 'date_gmt' => array( 'type' => 'string', 'description' => __( 'Publish date in GMT/UTC (Y-m-d H:i:s). Required with status "future", and must be in the future. A future date with status "publish" schedules the post, as the block editor does.', 'gripmcp' ), ), ), ); } protected function required_capability(): string { return 'edit_posts'; } /** * The code this call would add (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 { if ( Custom_Css::TYPE === sanitize_key( (string) ( $arguments['post_type'] ?? 'post' ) ) ) { return array(); } return Added_Code::in_post( null, array( 'post_title' => (string) ( $arguments['title'] ?? '' ), 'post_content' => (string) ( $arguments['content'] ?? '' ), 'post_excerpt' => (string) ( $arguments['excerpt'] ?? '' ), ) ); } /** * A post that adds code, on a plan that allows it, is held for approval * like a delete, so the person sees the code before it goes on the site. * * @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 ) ); } protected function run( array $arguments, Request_Context $context ): Tool_Result { $post_type = sanitize_key( $arguments['post_type'] ?? 'post' ); $type_object = get_post_type_object( $post_type ); if ( ! $type_object ) { return $this->fail( sprintf( /* translators: %s: post type. */ __( 'Unknown post type: %s', 'gripmcp' ), $post_type ) ); } // Additional CSS is one post per theme, which WordPress must be told // about or it never shows. if ( Custom_Css::TYPE === $post_type ) { return $this->create_css( $arguments, $context ); } // WordPress's own records, media, menu items and WooCommerce data are // not made as plain posts. $type_error = Post_Access::create_error( $type_object ); if ( '' !== $type_error ) { return $this->fail( $type_error ); } // Fine-grained capability: creating requires the type's create/edit cap. if ( ! $context->user_can( $type_object->cap->create_posts ) ) { return $this->fail( __( 'You do not have permission to create this content type.', 'gripmcp' ) ); } $status = $arguments['status'] ?? 'draft'; $status_error = Post_Access::status_error( (string) $status, $type_object, $context ); if ( '' !== $status_error ) { return $this->fail( $status_error ); } // The date is GMT already: read it as such, not as site time. $date_gmt = ''; if ( isset( $arguments['date_gmt'] ) && '' !== 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' ) ); } } if ( 'future' === $status ) { $future_error = Post_Schedule::future_error( $date_gmt ); if ( '' !== $future_error ) { return $this->fail( $future_error ); } } // Resolve the author, defaulting to the calling user. $author_id = $context->user_id(); if ( isset( $arguments['author'] ) && '' !== 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 !== $context->user_id() && ! $context->user_can( 'edit_others_posts' ) ) { return $this->fail( __( 'You do not have permission to assign posts to other users (requires the "edit_others_posts" capability).', 'gripmcp' ) ); } $author_id = $resolved; } $postarr = array( 'post_type' => $post_type, 'post_title' => (string) $arguments['title'], 'post_content' => (string) ( $arguments['content'] ?? '' ), 'post_excerpt' => (string) ( $arguments['excerpt'] ?? '' ), 'post_status' => $status, 'post_author' => $author_id, ); if ( ! empty( $arguments['slug'] ) ) { $postarr['post_name'] = sanitize_title( (string) $arguments['slug'] ); } if ( '' !== $date_gmt ) { $postarr['post_date_gmt'] = $date_gmt; $postarr['post_date'] = get_date_from_gmt( $date_gmt ); } // Scripts, embedded frames, event handlers and a block's own CSS are // left to the person, unless the plan adds code with approval. $code = Added_Code::refusal( Added_Code::in_post( null, $postarr ) ); if ( '' !== $code ) { return $this->fail( $code ); } $result = wp_insert_post( wp_slash( $postarr ), true ); if ( is_wp_error( $result ) ) { return $this->fail( sprintf( /* translators: %s: error message. */ __( 'Failed to create post: %s', 'gripmcp' ), $result->get_error_message() ) ); } // WordPress keeps no revision of a post as it was created, so its // first edit could not be rolled back with wp_revisions_restore. // The block editor saves one; so does this. It does nothing where // revisions are off. wp_save_post_revision( (int) $result ); $post = get_post( (int) $result ); $summary = sprintf( /* translators: 1: title, 2: id. */ __( 'Created "%1$s" (id %2$d).', 'gripmcp' ), Post_Presenter::title( $post ), (int) $result ); $note = $post instanceof \WP_Post ?Post_Schedule::status_note( (string) $status, $post ) : ''; if ( '' !== $note ) { $summary .= ' ' . $note; } // WordPress gives a taken slug a number ("-2") without saying so. $slug_note = $post instanceof \WP_Post && isset( $postarr['post_name'] ) ? Post_Presenter::slug_note( $post, (string) $postarr['post_name'] ) : ''; if ( '' !== $slug_note ) { $summary .= ' ' . $slug_note; } return $this->data( Post_Presenter::to_array( $post, false ), $summary ); } /** * Create a theme's Additional CSS. The slug names the theme (its folder), * the active one when left out; the title is always the theme's folder * name, as WordPress requires. * * @param array $arguments Arguments. * @param Request_Context $context Context. */ private function create_css( array $arguments, Request_Context $context ): Tool_Result { if ( ! Custom_Css::writable() ) { return $this->fail( Custom_Css::read_only() ); } if ( ! Custom_Css::can_edit( $context ) ) { return $this->fail( Custom_Css::forbidden() ); } $stylesheet = isset( $arguments['slug'] ) && '' !== trim( (string) $arguments['slug'] ) ? trim( (string) $arguments['slug'] ) : get_stylesheet(); $post = Custom_Css::create( (string) ( $arguments['content'] ?? '' ), $stylesheet ); if ( is_wp_error( $post ) ) { return $this->fail( $post->get_error_message() ); } $after = Custom_Css::after_save( $post ); $payload = Post_Presenter::to_array( $post, false ); $notes = $after['notes']; $payload['refreshed'] = $after['refreshed']; $summary = sprintf( /* translators: 1: theme directory name, 2: post id. */ __( 'Created the Additional CSS for the theme "%1$s" (id %2$d).', 'gripmcp' ), $stylesheet, (int) $post->ID ); if ( '' !== $after['summary'] ) { $summary .= ' ' . $after['summary']; } // WordPress keeps a theme's CSS published and named after the theme. $ignored = array(); if ( isset( $arguments['status'] ) && 'publish' !== $arguments['status'] ) { $ignored[] = 'status'; } if ( isset( $arguments['title'] ) && '' !== trim( (string) $arguments['title'] ) && $stylesheet !== trim( (string) $arguments['title'] ) ) { $ignored[] = 'title'; } if ( ! empty( $ignored ) ) { $notes[] = sprintf( /* translators: %s: argument names, e.g. "status and title". */ __( 'Additional CSS is always published and titled with the theme\'s folder name, as WordPress requires, so %s did not apply.', 'gripmcp' ), wp_sprintf( '%l', $ignored ) ); } if ( ! empty( $notes ) ) { $payload['notes'] = $notes; $summary .= ' ' . implode( ' ', $notes ); } return $this->data( $payload, $summary ); } /** * 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; } }