storage_key. */ private const BOOL_MAP = [ 'enabled' => 'author_archives_enabled', 'show_in_search_results' => 'author_archives_index', 'show_empty_archives' => 'author_archives_show_empty', ]; /** * String settings: API alias => storage_key. */ private const STRING_MAP = [ 'title' => 'author_archives_title', 'meta_description' => 'author_archives_meta_desc', ]; /** * Constructor. */ public function __construct() { $this->id = 'thinkrank/update-author-archives-settings'; $this->label = __( 'Update ThinkRank Author Archives Settings', 'thinkrank' ); $this->description = __( 'Update ThinkRank author archives settings: enable/index toggles, empty-archive visibility, and the SEO title/meta description templates. Read the current values with get-author-archives-settings first; only the keys you pass are changed.', 'thinkrank' ); } /** * {@inheritDoc} * * @return array */ public function get_annotations() { return [ 'readonly' => false, // Settings writes are recoverable: the matching get-* ability reads // the previous value, so nothing is lost that cannot be put back. // `destructive` is reserved for calls that lose data or reach // outside the site, and marking routine configuration with it made // MCP clients demand a human approval for every save — which users // reported as a permission bug, because the client's refusal reads // as "No approval received" (#675). 'destructive' => false, 'idempotent' => true, 'priority' => 2.0, 'openWorldHint' => false, ]; } /** * {@inheritDoc} * * @return array */ public function get_input_schema() { return [ 'type' => 'object', 'additionalProperties' => false, 'properties' => [ 'settings' => [ 'type' => 'object', 'description' => __( 'Author archives settings to update.', 'thinkrank' ), 'properties' => [ 'enabled' => [ 'type' => 'boolean' ], 'show_in_search_results' => [ 'type' => 'boolean' ], 'show_empty_archives' => [ 'type' => 'boolean' ], 'title' => [ 'type' => 'string' ], 'meta_description' => [ 'type' => 'string' ], ], ], ], 'required' => [ 'settings' ], ]; } /** * {@inheritDoc} * * @return array */ public function get_output_schema() { return [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'message' => [ 'type' => 'string' ], ], ]; } /** * Execute ability. * * @param array $input Ability input payload. * @return array|\WP_Error */ public function execute( $input ) { $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; if ( empty( $settings ) ) { return new \WP_Error( 'thinkrank_missing_author_archives_settings_payload', __( 'An author archives settings payload is required.', 'thinkrank' ), [ 'status' => 400 ] ); } $store = Settings::instance(); $found = false; $failed = []; foreach ( self::BOOL_MAP as $alias => $storage_key ) { if ( array_key_exists( $alias, $settings ) ) { $found = true; if ( ! $store->set( $storage_key, (bool) $settings[ $alias ] ) ) { $failed[] = $alias; } } } foreach ( self::STRING_MAP as $alias => $storage_key ) { if ( array_key_exists( $alias, $settings ) ) { $found = true; if ( ! $store->set( $storage_key, sanitize_text_field( (string) $settings[ $alias ] ) ) ) { $failed[] = $alias; } } } if ( ! $found ) { return new \WP_Error( 'thinkrank_no_valid_author_archives_setting_keys', __( 'No valid author archives setting keys were provided.', 'thinkrank' ), [ 'status' => 400 ] ); } // A discarded set() return meant a failed write still answered // "settings updated"; surface it instead. if ( ! empty( $failed ) ) { return new \WP_Error( 'thinkrank_author_archives_settings_not_saved', sprintf( /* translators: %s is a comma-separated list of setting names that could not be saved. */ __( 'Some author archives settings could not be saved: %s', 'thinkrank' ), implode( ', ', $failed ) ), [ 'status' => 500 ] ); } return [ 'success' => true, 'message' => __( 'Author archives settings updated.', 'thinkrank' ), ]; } }