id = 'thinkrank/update-schema-settings'; $this->label = __( 'Update ThinkRank Schema Settings', 'thinkrank' ); $this->description = __( 'Update ThinkRank structured-data (schema) settings. When auto_deploy is enabled, saving automatically regenerates and redeploys the site-wide schema markup. Read the current values with get-schema-settings first; only the keys you pass are changed.', 'thinkrank' ); } /** * {@inheritDoc} * * @return array */ public function get_annotations() { return [ 'readonly' => false, 'destructive' => true, 'idempotent' => true, 'priority' => 2.0, 'openWorldHint' => false, ]; } /** * Build the JSON schema properties for schema settings. * * @return array */ private static function schema_properties() { $props = []; foreach ( self::BOOL_KEYS as $key ) { $props[ $key ] = [ 'type' => 'boolean' ]; } foreach ( self::INT_KEYS as $key ) { $props[ $key ] = [ 'type' => 'integer' ]; } foreach ( self::ARRAY_KEYS as $key ) { $props[ $key ] = [ 'type' => 'array' ]; } foreach ( self::STRING_KEYS as $key ) { $props[ $key ] = [ 'type' => 'string' ]; } return $props; } /** * {@inheritDoc} * * @return array */ public function get_input_schema() { return [ 'type' => 'object', 'additionalProperties' => false, 'properties' => [ 'settings' => [ 'type' => 'object', 'description' => __( 'Schema settings to update.', 'thinkrank' ), 'properties' => self::schema_properties(), ], ], '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_schema_settings_payload', __( 'A schema settings payload is required.', 'thinkrank' ), [ 'status' => 400 ] ); } $mgr = new Schema_Management_System(); $merged = $mgr->get_settings( 'site', null ); $found = false; foreach ( self::BOOL_KEYS as $key ) { if ( array_key_exists( $key, $settings ) ) { $merged[ $key ] = (bool) $settings[ $key ]; $found = true; } } foreach ( self::INT_KEYS as $key ) { if ( array_key_exists( $key, $settings ) ) { $merged[ $key ] = (int) $settings[ $key ]; $found = true; } } foreach ( self::ARRAY_KEYS as $key ) { if ( array_key_exists( $key, $settings ) && is_array( $settings[ $key ] ) ) { $merged[ $key ] = array_values( $settings[ $key ] ); $found = true; } } foreach ( self::STRING_KEYS as $key ) { if ( array_key_exists( $key, $settings ) ) { $value = sanitize_text_field( (string) $settings[ $key ] ); // validation_level is an enum. An out-of-range value would make // the downstream all-or-nothing validator reject the entire // save, silently dropping the other valid fields — so skip an // invalid value here and let the rest of the payload save. if ( 'validation_level' === $key && ! in_array( $value, [ 'strict', 'moderate', 'lenient' ], true ) ) { continue; } $merged[ $key ] = $value; $found = true; } } if ( ! $found ) { return new \WP_Error( 'thinkrank_no_valid_schema_setting_keys', __( 'No valid schema setting keys were provided.', 'thinkrank' ), [ 'status' => 400 ] ); } $result = $mgr->save_settings( 'site', null, $merged ); return [ 'success' => (bool) $result, 'message' => (bool) $result ? __( 'Schema settings updated.', 'thinkrank' ) : __( 'Failed to update schema settings.', 'thinkrank' ), ]; } }