id = 'thinkrank/update-social-platforms-settings'; $this->label = __( 'Update ThinkRank Social Platform Settings', 'thinkrank' ); $this->description = __( 'Update ThinkRank social platform verification settings: public platform IDs (Facebook, YouTube, WhatsApp) and sensitive site-verification codes (Pinterest, Instagram, TikTok). Only non-empty provided keys are saved; sensitive codes are stored encrypted. Read the current values with get-social-platforms-settings first.', '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' => __( 'Social platform settings to update (any subset of the fields below).', 'thinkrank' ), 'additionalProperties' => false, 'properties' => [ 'facebook_app_id' => [ 'type' => 'string' ], 'facebook_admins' => [ 'type' => 'string' ], 'youtube_channel_id' => [ 'type' => 'string' ], 'whatsapp_business_id' => [ 'type' => 'string' ], 'pinterest_site_verification' => [ 'type' => 'string' ], 'instagram_verification' => [ 'type' => 'string' ], 'tiktok_verification' => [ 'type' => 'string' ], ], ], ], 'required' => [ 'settings' ], ]; } /** * {@inheritDoc} * * @return array */ public function get_output_schema() { return [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'message' => [ 'type' => 'string' ], 'settings' => [ 'type' => 'object', 'description' => __( 'The resulting settings, with sensitive codes masked.', 'thinkrank' ), 'additionalProperties' => true, ], ], ]; } /** * 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_social_platforms_settings_payload', __( 'A social platform settings payload is required.', 'thinkrank' ), [ 'status' => 400 ] ); } $store = Settings::instance(); $allowed = array_merge( self::PUBLIC_KEYS, self::SENSITIVE_KEYS ); $found = false; foreach ( $allowed as $key ) { if ( ! array_key_exists( $key, $settings ) ) { continue; } $value = sanitize_text_field( (string) $settings[ $key ] ); // Mirror the REST endpoint: only persist non-empty values so a // masked/blank round-trip never wipes an existing code. if ( '' === $value ) { continue; } $store->set( $key, $value ); $found = true; } if ( ! $found ) { return new \WP_Error( 'thinkrank_no_valid_social_platforms_setting_keys', __( 'No valid, non-empty social platform setting keys were provided.', 'thinkrank' ), [ 'status' => 400 ] ); } return [ 'success' => true, 'message' => __( 'Social platform settings updated.', 'thinkrank' ), 'settings' => $this->read_masked( $store ), ]; } /** * Read current settings back with sensitive codes masked. * * @param Settings $store Settings store. * @return array */ private function read_masked( Settings $store ): array { $out = []; foreach ( self::PUBLIC_KEYS as $key ) { $out[ $key ] = (string) $store->get( $key, '' ); } foreach ( self::SENSITIVE_KEYS as $key ) { $code = (string) $store->get( $key, '' ); if ( '' === $code ) { $out[ $key ] = ''; } elseif ( strlen( $code ) > 8 ) { $out[ $key ] = substr( $code, 0, 4 ) . 'XXXX'; } else { $out[ $key ] = 'XXXX'; } } return $out; } }