'content/create-cpt-item', 'args' => array( 'label' => __( 'Create CPT Item', 'acrossai-abilities-manager' ), 'description' => __( 'Create a custom post type record. post_type is required and must be registered.', 'acrossai-abilities-manager' ), 'category' => 'acrossai-content', 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => static function (): bool { return current_user_can( 'manage_options' ); }, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'post_type' => array( 'type' => 'string' ), 'allow_protected_post_type' => array( 'type' => 'boolean', 'default' => false, 'description' => __( 'Write anyway when the post type belongs to a plugin that keeps derived data elsewhere. The response states what was bypassed.', 'acrossai-abilities-manager' ), ), 'title' => array( 'type' => 'string' ), 'content' => array( 'type' => 'string' ), 'excerpt' => array( 'type' => 'string' ), 'status' => array( 'type' => 'string', 'default' => 'draft', ), 'slug' => array( 'type' => 'string' ), 'author' => array( 'type' => 'integer' ), 'meta' => array( 'type' => 'object' ), 'return_content' => array( 'type' => 'boolean', 'default' => false, 'description' => __( 'When true, the response includes the saved post_content / post_excerpt / post_content_filtered fields. Default false: those large fields are stripped and content_bytes is returned instead.', 'acrossai-abilities-manager' ), ), 'apply_wp_slash' => Slash_Input::schema_fragment()['apply_wp_slash'], ), 'required' => array( 'post_type', 'title' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'blocked_reason' => array( 'type' => 'string' ), 'post_type' => array( 'type' => 'string' ), 'use_instead' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ), 'writes' => array( 'type' => 'string' ), 'warnings' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ), 'success' => array( 'type' => 'boolean' ), 'id' => array( 'type' => 'integer' ), 'item' => array( 'type' => 'object' ), 'content_bytes' => array( 'type' => 'integer' ), 'message' => array( 'type' => 'string' ), ), 'required' => array( 'success' ), 'additionalProperties' => false, ), 'meta' => array( 'acrossai' => array( 'tab_group' => 'content', 'sub_group' => 'cpt', 'sub_group_label' => __( 'Custom Post Types', 'acrossai-abilities-manager' ), ), 'show_in_rest' => true, 'mcp' => array( 'public' => false, 'type' => 'tool', ), 'annotations' => array( 'readonly' => false, 'destructive' => false, 'idempotent' => false, ), 'input_flags' => Slash_Input::meta_flags(), ), ), ); } /** * Execute the ability. * * @param array $input Ability input payload. * @return array */ public function execute( array $input = array() ): array { $post_type = sanitize_key( (string) ( $input['post_type'] ?? '' ) ); /* * Feature 120 — refuse before the post lookup, deliberately. For an HPOS order `get_post()` * misses and the caller is told "Item not found for the given post_type", which is both * wrong and unhelpful: the order exists, just not here. */ $assessment = Protected_Post_Types::assess( $post_type, isset( $input['meta'] ) && array() !== (array) $input['meta'] ); if ( $assessment['blocked'] && empty( $input['allow_protected_post_type'] ) ) { return Protected_Post_Types::refusal( $assessment['verdict'] ); } $warnings = Protected_Post_Types::warnings_for( $assessment['verdict'] ); if ( '' === $post_type || ! post_type_exists( $post_type ) ) { return array( 'success' => false, /* translators: %s: post type */ 'message' => sprintf( __( 'Unknown post type "%s".', 'acrossai-abilities-manager' ), $post_type ), ); } $cap = get_post_type_object( $post_type )->cap->edit_posts ?? 'edit_posts'; if ( ! current_user_can( $cap ) ) { return array( 'success' => false, 'message' => __( 'You do not have permission to create items of this type.', 'acrossai-abilities-manager' ), ); } $args = array( 'post_type' => $post_type, 'post_title' => sanitize_text_field( (string) ( $input['title'] ?? '' ) ), 'post_content' => (string) ( $input['content'] ?? '' ), 'post_excerpt' => (string) ( $input['excerpt'] ?? '' ), 'post_status' => sanitize_key( (string) ( $input['status'] ?? 'draft' ) ), ); if ( ! empty( $input['slug'] ) ) { $args['post_name'] = sanitize_title( (string) $input['slug'] ); } if ( ! empty( $input['author'] ) ) { $args['post_author'] = (int) $input['author']; } if ( ! empty( $input['meta'] ) && is_array( $input['meta'] ) ) { $args['meta_input'] = $input['meta']; } $id = wp_insert_post( Slash_Input::slash( $args, $input ), true ); if ( is_wp_error( $id ) ) { return array( 'success' => false, 'message' => $id->get_error_message(), ); } $fetched = (array) get_post( (int) $id, ARRAY_A ); $content_bytes = strlen( (string) ( $fetched['post_content'] ?? '' ) ); $return_content = ! empty( $input['return_content'] ); if ( ! $return_content ) { unset( $fetched['post_content'], $fetched['post_content_filtered'], $fetched['post_excerpt'] ); } return array( 'success' => true, 'warnings' => $warnings, 'id' => (int) $id, 'item' => $fetched, 'content_bytes' => $content_bytes, /* translators: 1: post type, 2: ID */ 'message' => sprintf( __( 'Created %1$s #%2$d.', 'acrossai-abilities-manager' ), $post_type, $id ), ); } }