array( 'type' => 'string', 'description' => __( 'Keyword or phrase to research.', 'acrossai-abilities-manager' ), ), 'country' => array( 'type' => 'string', 'default' => 'all', 'description' => __( 'Country code to target, or "all".', 'acrossai-abilities-manager' ), ), ); } protected function output_properties(): array { return array( 'keyword' => array( 'type' => 'string' ), 'country' => array( 'type' => 'string' ), 'research' => array( 'type' => array( 'object', 'array', 'null' ) ), 'credits_before' => array( 'type' => array( 'integer', 'null' ) ), 'credits_after' => array( 'type' => array( 'integer', 'null' ) ), ); } /** * 'confirm' is intentionally absent — see Base_Rank_Math_Ability::ability(). */ protected function required_input(): array { return array( 'keyword' ); } protected function annotations(): array { return array( 'readonly' => false, 'destructive' => true, 'idempotent' => false ); } /** * @param array $input * @return array|WP_Error */ protected function run( array $input ) { $keyword = isset( $input['keyword'] ) ? sanitize_text_field( (string) $input['keyword'] ) : ''; if ( '' === $keyword ) { return new WP_Error( 'invalid_input', __( 'keyword is required.', 'acrossai-abilities-manager' ) ); } $result = Entitlement_Repository::research_keyword( $keyword, isset( $input['country'] ) ? sanitize_text_field( (string) $input['country'] ) : 'all' ); if ( is_wp_error( $result ) ) { return $result; } $spent = ( null !== $result['credits_before'] && null !== $result['credits_after'] ) ? max( 0, $result['credits_before'] - $result['credits_after'] ) : null; $result['message'] = null !== $spent ? sprintf( /* translators: 1: researched keyword, 2: credits spent, 3: credits remaining */ __( 'Researched "%1$s". %2$d credits spent, %3$d remaining.', 'acrossai-abilities-manager' ), $keyword, $spent, $result['credits_after'] ) : sprintf( /* translators: %s: researched keyword */ __( 'Researched "%s".', 'acrossai-abilities-manager' ), $keyword ); return $result; } }