original script, to * avoid losing inline scripts which may have been attached. * * Adapted from `gutenberg_override_script()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L56-L105 * * @param WP_Scripts $scripts WP_Scripts instance. * @param string $handle Name of the script. Should be unique. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param bool $in_footer Optional. Whether to enqueue the script before instead of in the . * Default `false`. * * @return bool Whether or not the script was overridden. */ public function override_script( $scripts, $handle, $src, $deps = [], $ver = false, $in_footer = false ) { $script = $scripts->query( $handle, 'registered' ); if ( $script ) { /* * In many ways, this is a reimplementation of `wp_register_script` but * bypassing consideration of whether a script by the given handle had * already been registered. */ // See: `_WP_Dependency::__construct()`. $script->src = $src; $script->deps = $deps; $script->ver = $ver; $script->args = $in_footer; /* * The script's `group` designation is an indication of whether it is * to be printed in the header or footer. The behavior here defers to * the arguments as passed. Specifically, group data is not assigned * for a script unless it is designated to be printed in the footer. */ // See: `wp_register_script()` . unset( $script->extra['group'] ); if ( $in_footer ) { $script->add_data( 'group', 1 ); } } else { $scripts->add( $handle, $src, $deps, $ver, $in_footer ); } return (bool) $script; } /** * Registers a style according to `wp_register_style`. Honors this request by deregistering any style by the same * handler before registration. * * Adapted from `gutenberg_override_style()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L177-L182 * * @param WP_Styles $styles WP_Styles instance. * @param string $handle Name of the stylesheet. Should be unique. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param string $media Optional. The media for which this stylesheet has been defined. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like * '(orientation: portrait)' and '(max-width: 640px)'. */ public function override_style( $styles, $handle, $src, $deps = [], $ver = false, $media = 'all' ) { $style = $styles->query( $handle, 'registered' ); if ( $style ) { $styles->remove( $handle ); } $styles->add( $handle, $src, $deps, $ver, $media ); } } original script, to * avoid losing inline scripts which may have been attached. * * Adapted from `gutenberg_override_script()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L56-L105 * * @param WP_Scripts $scripts WP_Scripts instance. * @param string $handle Name of the script. Should be unique. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param bool $in_footer Optional. Whether to enqueue the script before instead of in the . * Default `false`. * * @return bool Whether or not the script was overridden. */ public function override_script( $scripts, $handle, $src, $deps = [], $ver = false, $in_footer = false ) { $script = $scripts->query( $handle, 'registered' ); if ( $script ) { /* * In many ways, this is a reimplementation of `wp_register_script` but * bypassing consideration of whether a script by the given handle had * already been registered. */ // See: `_WP_Dependency::__construct()`. $script->src = $src; $script->deps = $deps; $script->ver = $ver; $script->args = $in_footer; /* * The script's `group` designation is an indication of whether it is * to be printed in the header or footer. The behavior here defers to * the arguments as passed. Specifically, group data is not assigned * for a script unless it is designated to be printed in the footer. */ // See: `wp_register_script()` . unset( $script->extra['group'] ); if ( $in_footer ) { $script->add_data( 'group', 1 ); } } else { $scripts->add( $handle, $src, $deps, $ver, $in_footer ); } return (bool) $script; } /** * Registers a style according to `wp_register_style`. Honors this request by deregistering any style by the same * handler before registration. * * Adapted from `gutenberg_override_style()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L177-L182 * * @param WP_Styles $styles WP_Styles instance. * @param string $handle Name of the stylesheet. Should be unique. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param string $media Optional. The media for which this stylesheet has been defined. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like * '(orientation: portrait)' and '(max-width: 640px)'. */ public function override_style( $styles, $handle, $src, $deps = [], $ver = false, $media = 'all' ) { $style = $styles->query( $handle, 'registered' ); if ( $style ) { $styles->remove( $handle ); } $styles->add( $handle, $src, $deps, $ver, $media ); } } original script, to * avoid losing inline scripts which may have been attached. * * Adapted from `gutenberg_override_script()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L56-L105 * * @param WP_Scripts $scripts WP_Scripts instance. * @param string $handle Name of the script. Should be unique. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param bool $in_footer Optional. Whether to enqueue the script before instead of in the . * Default `false`. * * @return bool Whether or not the script was overridden. */ public function override_script( $scripts, $handle, $src, $deps = [], $ver = false, $in_footer = false ) { $script = $scripts->query( $handle, 'registered' ); if ( $script ) { /* * In many ways, this is a reimplementation of `wp_register_script` but * bypassing consideration of whether a script by the given handle had * already been registered. */ // See: `_WP_Dependency::__construct()`. $script->src = $src; $script->deps = $deps; $script->ver = $ver; $script->args = $in_footer; /* * The script's `group` designation is an indication of whether it is * to be printed in the header or footer. The behavior here defers to * the arguments as passed. Specifically, group data is not assigned * for a script unless it is designated to be printed in the footer. */ // See: `wp_register_script()` . unset( $script->extra['group'] ); if ( $in_footer ) { $script->add_data( 'group', 1 ); } } else { $scripts->add( $handle, $src, $deps, $ver, $in_footer ); } return (bool) $script; } /** * Registers a style according to `wp_register_style`. Honors this request by deregistering any style by the same * handler before registration. * * Adapted from `gutenberg_override_style()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L177-L182 * * @param WP_Styles $styles WP_Styles instance. * @param string $handle Name of the stylesheet. Should be unique. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param string $media Optional. The media for which this stylesheet has been defined. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like * '(orientation: portrait)' and '(max-width: 640px)'. */ public function override_style( $styles, $handle, $src, $deps = [], $ver = false, $media = 'all' ) { $style = $styles->query( $handle, 'registered' ); if ( $style ) { $styles->remove( $handle ); } $styles->add( $handle, $src, $deps, $ver, $media ); } } original script, to * avoid losing inline scripts which may have been attached. * * Adapted from `gutenberg_override_script()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L56-L105 * * @param WP_Scripts $scripts WP_Scripts instance. * @param string $handle Name of the script. Should be unique. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param bool $in_footer Optional. Whether to enqueue the script before instead of in the . * Default `false`. * * @return bool Whether or not the script was overridden. */ public function override_script( $scripts, $handle, $src, $deps = [], $ver = false, $in_footer = false ) { $script = $scripts->query( $handle, 'registered' ); if ( $script ) { /* * In many ways, this is a reimplementation of `wp_register_script` but * bypassing consideration of whether a script by the given handle had * already been registered. */ // See: `_WP_Dependency::__construct()`. $script->src = $src; $script->deps = $deps; $script->ver = $ver; $script->args = $in_footer; /* * The script's `group` designation is an indication of whether it is * to be printed in the header or footer. The behavior here defers to * the arguments as passed. Specifically, group data is not assigned * for a script unless it is designated to be printed in the footer. */ // See: `wp_register_script()` . unset( $script->extra['group'] ); if ( $in_footer ) { $script->add_data( 'group', 1 ); } } else { $scripts->add( $handle, $src, $deps, $ver, $in_footer ); } return (bool) $script; } /** * Registers a style according to `wp_register_style`. Honors this request by deregistering any style by the same * handler before registration. * * Adapted from `gutenberg_override_style()` in the Gutenberg plugin. * * @link https://github.com/WordPress/gutenberg/blob/132fec1fb5b4ab6af1d7696cbfe0574597644f18/lib/client-assets.php#L177-L182 * * @param WP_Styles $styles WP_Styles instance. * @param string $handle Name of the stylesheet. Should be unique. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param string $media Optional. The media for which this stylesheet has been defined. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like * '(orientation: portrait)' and '(max-width: 640px)'. */ public function override_style( $styles, $handle, $src, $deps = [], $ver = false, $media = 'all' ) { $style = $styles->query( $handle, 'registered' ); if ( $style ) { $styles->remove( $handle ); } $styles->add( $handle, $src, $deps, $ver, $media ); } } ormer to display the configuration for. * * [--=] * : Only list the config entries where equals the requested . * * [--fields=] * : Limit the output to specific fields. Defaults to all fields. * * [--field=] * : Prints the value of a single field for each config entry. * * [--format=] * : Render output in a particular format. * --- * default: table * options: * - count * - csv * - json * - table * - yaml * --- * * ## EXAMPLES * * # Check the current configuration of the RewriteAmpUrls transformer. * $ wp amp optimizer transformer config RewriteAmpUrls * +-------------------+----------------------------+ * | key | value | * +-------------------+----------------------------+ * | ampRuntimeVersion | | * | ampUrlPrefix | https://cdn.ampproject.org | * | esmModulesEnabled | true | * | geoApiUrl | | * | lts | false | * | rtv | false | * +-------------------+----------------------------+ * * # Fetch the attribute that is added to store a backup of inlined styles. * $ wp amp optimizer transformer config OptimizeHeroImages --key=inlineStyleBackupAttribute --field=value * data-amp-original-style * * # Render the configuration of the AmpRuntimeCss transformer as a JSON array. * $ wp amp optimizer transformer config AmpRuntimeCss --format=json * {"canary":false,"styles":"","version":""} * * @param array $args Array of positional arguments. * @param array $assoc_args Associative array of associative arguments. * @throws WP_CLI\ExitException If the requested file could not be read. */ public function config( $args, $assoc_args ) { $transformer = array_shift( $args ); $transformer_class = $this->deduce_transformer_class( $transformer ); if ( false === $transformer_class ) { WP_CLI::error( "Unknown transformer: {$transformer}." ); } $default_fields = [ 'key', 'value', ]; $defaults = [ 'fields' => implode( ',', $default_fields ), 'format' => 'table', ]; $assoc_args = array_merge( $defaults, $assoc_args ); try { $config_array = $this->configuration->getTransformerConfiguration( $transformer_class )->toArray(); } catch ( UnknownConfigurationClass $exception ) { WP_CLI::error( $exception->getMessage() ); return; } $config_entries = []; foreach ( $config_array as $key => $value ) { if ( is_bool( $value ) && in_array( $assoc_args['format'], [ 'table', 'csv' ], true ) ) { $value = $value ? 'true' : 'false'; } $config_entries[] = compact( 'key', 'value' ); } $config_entries = $this->filter_entries( $config_entries, $default_fields, $assoc_args ); if ( 'count' === $assoc_args['format'] ) { WP_CLI::log( (string) count( $config_entries ) ); return; } if ( empty( $config_entries ) ) { WP_CLI::error( 'No matching config entries found.' ); } if ( 'json' === $assoc_args['format'] ) { // Flatten the entries again for producing the JSON output that the spec tests understand. $json_array = []; foreach ( $config_entries as $config_entry ) { $json_array[ $config_entry['key'] ] = $config_entry['value']; } WP_CLI::log( wp_json_encode( $json_array ) ); return; } $formatter = new WP_CLI\Formatter( $assoc_args, $default_fields ); $formatter->display_items( $config_entries ); } /** * Filters the entries of an associative array based on a provided filter key. * * @param array $entries Associative array to filter. * @param array $fields Array of known fields. * @param array $assoc_args Filters to apply. * * @return array */ private function filter_entries( $entries, $fields, $assoc_args ) { $result = []; foreach ( $entries as $entry ) { foreach ( $fields as $field ) { if ( array_key_exists( $field, $assoc_args ) && $entry[ $field ] !== $assoc_args[ $field ] ) { continue 2; } } $result[] = $entry; } return $result; } /** * Deduce the transformer class from a transformer name. * * @param string $transformer Transformer name to get the class for. * @return string|false Class of the transformer, or false if none found. */ private function deduce_transformer_class( $transformer ) { $transformer_classes = $this->configuration->get( Configuration::KEY_TRANSFORMERS ); foreach ( $transformer_classes as $transformer_class ) { if ( $transformer === $this->get_transformer_name( $transformer_class ) ) { return (string) $transformer_class; } } return false; } /** * Get the name of a transformer from its class. * * @param string $transformer_class Transformer class to get the name for. * @return string Name of the transformer. */ private function get_transformer_name( $transformer_class ) { $name_parts = explode( '\\', $transformer_class ); return (string) array_pop( $name_parts ); } /** * Get the source of a transformer. * * @param string $transformer_class Class of the transformer to get the source for. * @return string Source of the transformer. Will be one of 'toolbox', 'plugin', 'third-party'. */ private function get_transformer_source( $transformer_class ) { if ( 0 === strpos( $transformer_class, 'AmpProject\\Optimizer\\Transformer\\' ) ) { return 'toolbox'; } if ( 0 === strpos( $transformer_class, 'AmpProject\\AmpWP\\Optimizer\\Transformer\\' ) ) { return 'plugin'; } return 'third-party'; } } ormer to display the configuration for. * * [--=] * : Only list the config entries where equals the requested . * * [--fields=] * : Limit the output to specific fields. Defaults to all fields. * * [--field=] * : Prints the value of a single field for each config entry. * * [--format=] * : Render output in a particular format. * --- * default: table * options: * - count * - csv * - json * - table * - yaml * --- * * ## EXAMPLES * * # Check the current configuration of the RewriteAmpUrls transformer. * $ wp amp optimizer transformer config RewriteAmpUrls * +-------------------+----------------------------+ * | key | value | * +-------------------+----------------------------+ * | ampRuntimeVersion | | * | ampUrlPrefix | https://cdn.ampproject.org | * | esmModulesEnabled | true | * | geoApiUrl | | * | lts | false | * | rtv | false | * +-------------------+----------------------------+ * * # Fetch the attribute that is added to store a backup of inlined styles. * $ wp amp optimizer transformer config OptimizeHeroImages --key=inlineStyleBackupAttribute --field=value * data-amp-original-style * * # Render the configuration of the AmpRuntimeCss transformer as a JSON array. * $ wp amp optimizer transformer config AmpRuntimeCss --format=json * {"canary":false,"styles":"","version":""} * * @param array $args Array of positional arguments. * @param array $assoc_args Associative array of associative arguments. * @throws WP_CLI\ExitException If the requested file could not be read. */ public function config( $args, $assoc_args ) { $transformer = array_shift( $args ); $transformer_class = $this->deduce_transformer_class( $transformer ); if ( false === $transformer_class ) { WP_CLI::error( "Unknown transformer: {$transformer}." ); } $default_fields = [ 'key', 'value', ]; $defaults = [ 'fields' => implode( ',', $default_fields ), 'format' => 'table', ]; $assoc_args = array_merge( $defaults, $assoc_args ); try { $config_array = $this->configuration->getTransformerConfiguration( $transformer_class )->toArray(); } catch ( UnknownConfigurationClass $exception ) { WP_CLI::error( $exception->getMessage() ); return; } $config_entries = []; foreach ( $config_array as $key => $value ) { if ( is_bool( $value ) && in_array( $assoc_args['format'], [ 'table', 'csv' ], true ) ) { $value = $value ? 'true' : 'false'; } $config_entries[] = compact( 'key', 'value' ); } $config_entries = $this->filter_entries( $config_entries, $default_fields, $assoc_args ); if ( 'count' === $assoc_args['format'] ) { WP_CLI::log( (string) count( $config_entries ) ); return; } if ( empty( $config_entries ) ) { WP_CLI::error( 'No matching config entries found.' ); } if ( 'json' === $assoc_args['format'] ) { // Flatten the entries again for producing the JSON output that the spec tests understand. $json_array = []; foreach ( $config_entries as $config_entry ) { $json_array[ $config_entry['key'] ] = $config_entry['value']; } WP_CLI::log( wp_json_encode( $json_array ) ); return; } $formatter = new WP_CLI\Formatter( $assoc_args, $default_fields ); $formatter->display_items( $config_entries ); } /** * Filters the entries of an associative array based on a provided filter key. * * @param array $entries Associative array to filter. * @param array $fields Array of known fields. * @param array $assoc_args Filters to apply. * * @return array */ private function filter_entries( $entries, $fields, $assoc_args ) { $result = []; foreach ( $entries as $entry ) { foreach ( $fields as $field ) { if ( array_key_exists( $field, $assoc_args ) && $entry[ $field ] !== $assoc_args[ $field ] ) { continue 2; } } $result[] = $entry; } return $result; } /** * Deduce the transformer class from a transformer name. * * @param string $transformer Transformer name to get the class for. * @return string|false Class of the transformer, or false if none found. */ private function deduce_transformer_class( $transformer ) { $transformer_classes = $this->configuration->get( Configuration::KEY_TRANSFORMERS ); foreach ( $transformer_classes as $transformer_class ) { if ( $transformer === $this->get_transformer_name( $transformer_class ) ) { return (string) $transformer_class; } } return false; } /** * Get the name of a transformer from its class. * * @param string $transformer_class Transformer class to get the name for. * @return string Name of the transformer. */ private function get_transformer_name( $transformer_class ) { $name_parts = explode( '\\', $transformer_class ); return (string) array_pop( $name_parts ); } /** * Get the source of a transformer. * * @param string $transformer_class Class of the transformer to get the source for. * @return string Source of the transformer. Will be one of 'toolbox', 'plugin', 'third-party'. */ private function get_transformer_source( $transformer_class ) { if ( 0 === strpos( $transformer_class, 'AmpProject\\Optimizer\\Transformer\\' ) ) { return 'toolbox'; } if ( 0 === strpos( $transformer_class, 'AmpProject\\AmpWP\\Optimizer\\Transformer\\' ) ) { return 'plugin'; } return 'third-party'; } } ormer to display the configuration for. * * [--=] * : Only list the config entries where equals the requested . * * [--fields=] * : Limit the output to specific fields. Defaults to all fields. * * [--field=] * : Prints the value of a single field for each config entry. * * [--format=] * : Render output in a particular format. * --- * default: table * options: * - count * - csv * - json * - table * - yaml * --- * * ## EXAMPLES * * # Check the current configuration of the RewriteAmpUrls transformer. * $ wp amp optimizer transformer config RewriteAmpUrls * +-------------------+----------------------------+ * | key | value | * +-------------------+----------------------------+ * | ampRuntimeVersion | | * | ampUrlPrefix | https://cdn.ampproject.org | * | esmModulesEnabled | true | * | geoApiUrl | | * | lts | false | * | rtv | false | * +-------------------+----------------------------+ * * # Fetch the attribute that is added to store a backup of inlined styles. * $ wp amp optimizer transformer config OptimizeHeroImages --key=inlineStyleBackupAttribute --field=value * data-amp-original-style * * # Render the configuration of the AmpRuntimeCss transformer as a JSON array. * $ wp amp optimizer transformer config AmpRuntimeCss --format=json * {"canary":false,"styles":"","version":""} * * @param array $args Array of positional arguments. * @param array $assoc_args Associative array of associative arguments. * @throws WP_CLI\ExitException If the requested file could not be read. */ public function config( $args, $assoc_args ) { $transformer = array_shift( $args ); $transformer_class = $this->deduce_transformer_class( $transformer ); if ( false === $transformer_class ) { WP_CLI::error( "Unknown transformer: {$transformer}." ); } $default_fields = [ 'key', 'value', ]; $defaults = [ 'fields' => implode( ',', $default_fields ), 'format' => 'table', ]; $assoc_args = array_merge( $defaults, $assoc_args ); try { $config_array = $this->configuration->getTransformerConfiguration( $transformer_class )->toArray(); } catch ( UnknownConfigurationClass $exception ) { WP_CLI::error( $exception->getMessage() ); return; } $config_entries = []; foreach ( $config_array as $key => $value ) { if ( is_bool( $value ) && in_array( $assoc_args['format'], [ 'table', 'csv' ], true ) ) { $value = $value ? 'true' : 'false'; } $config_entries[] = compact( 'key', 'value' ); } $config_entries = $this->filter_entries( $config_entries, $default_fields, $assoc_args ); if ( 'count' === $assoc_args['format'] ) { WP_CLI::log( (string) count( $config_entries ) ); return; } if ( empty( $config_entries ) ) { WP_CLI::error( 'No matching config entries found.' ); } if ( 'json' === $assoc_args['format'] ) { // Flatten the entries again for producing the JSON output that the spec tests understand. $json_array = []; foreach ( $config_entries as $config_entry ) { $json_array[ $config_entry['key'] ] = $config_entry['value']; } WP_CLI::log( wp_json_encode( $json_array ) ); return; } $formatter = new WP_CLI\Formatter( $assoc_args, $default_fields ); $formatter->display_items( $config_entries ); } /** * Filters the entries of an associative array based on a provided filter key. * * @param array $entries Associative array to filter. * @param array $fields Array of known fields. * @param array $assoc_args Filters to apply. * * @return array */ private function filter_entries( $entries, $fields, $assoc_args ) { $result = []; foreach ( $entries as $entry ) { foreach ( $fields as $field ) { if ( array_key_exists( $field, $assoc_args ) && $entry[ $field ] !== $assoc_args[ $field ] ) { continue 2; } } $result[] = $entry; } return $result; } /** * Deduce the transformer class from a transformer name. * * @param string $transformer Transformer name to get the class for. * @return string|false Class of the transformer, or false if none found. */ private function deduce_transformer_class( $transformer ) { $transformer_classes = $this->configuration->get( Configuration::KEY_TRANSFORMERS ); foreach ( $transformer_classes as $transformer_class ) { if ( $transformer === $this->get_transformer_name( $transformer_class ) ) { return (string) $transformer_class; } } return false; } /** * Get the name of a transformer from its class. * * @param string $transformer_class Transformer class to get the name for. * @return string Name of the transformer. */ private function get_transformer_name( $transformer_class ) { $name_parts = explode( '\\', $transformer_class ); return (string) array_pop( $name_parts ); } /** * Get the source of a transformer. * * @param string $transformer_class Class of the transformer to get the source for. * @return string Source of the transformer. Will be one of 'toolbox', 'plugin', 'third-party'. */ private function get_transformer_source( $transformer_class ) { if ( 0 === strpos( $transformer_class, 'AmpProject\\Optimizer\\Transformer\\' ) ) { return 'toolbox'; } if ( 0 === strpos( $transformer_class, 'AmpProject\\AmpWP\\Optimizer\\Transformer\\' ) ) { return 'plugin'; } return 'third-party'; } } ormer to display the configuration for. * * [--=] * : Only list the config entries where equals the requested . * * [--fields=] * : Limit the output to specific fields. Defaults to all fields. * * [--field=] * : Prints the value of a single field for each config entry. * * [--format=] * : Render output in a particular format. * --- * default: table * options: * - count * - csv * - json * - table * - yaml * --- * * ## EXAMPLES * * # Check the current configuration of the RewriteAmpUrls transformer. * $ wp amp optimizer transformer config RewriteAmpUrls * +-------------------+----------------------------+ * | key | value | * +-------------------+----------------------------+ * | ampRuntimeVersion | | * | ampUrlPrefix | https://cdn.ampproject.org | * | esmModulesEnabled | true | * | geoApiUrl | | * | lts | false | * | rtv | false | * +-------------------+----------------------------+ * * # Fetch the attribute that is added to store a backup of inlined styles. * $ wp amp optimizer transformer config OptimizeHeroImages --key=inlineStyleBackupAttribute --field=value * data-amp-original-style * * # Render the configuration of the AmpRuntimeCss transformer as a JSON array. * $ wp amp optimizer transformer config AmpRuntimeCss --format=json * {"canary":false,"styles":"","version":""} * * @param array $args Array of positional arguments. * @param array $assoc_args Associative array of associative arguments. * @throws WP_CLI\ExitException If the requested file could not be read. */ public function config( $args, $assoc_args ) { $transformer = array_shift( $args ); $transformer_class = $this->deduce_transformer_class( $transformer ); if ( false === $transformer_class ) { WP_CLI::error( "Unknown transformer: {$transformer}." ); } $default_fields = [ 'key', 'value', ]; $defaults = [ 'fields' => implode( ',', $default_fields ), 'format' => 'table', ]; $assoc_args = array_merge( $defaults, $assoc_args ); try { $config_array = $this->configuration->getTransformerConfiguration( $transformer_class )->toArray(); } catch ( UnknownConfigurationClass $exception ) { WP_CLI::error( $exception->getMessage() ); return; } $config_entries = []; foreach ( $config_array as $key => $value ) { if ( is_bool( $value ) && in_array( $assoc_args['format'], [ 'table', 'csv' ], true ) ) { $value = $value ? 'true' : 'false'; } $config_entries[] = compact( 'key', 'value' ); } $config_entries = $this->filter_entries( $config_entries, $default_fields, $assoc_args ); if ( 'count' === $assoc_args['format'] ) { WP_CLI::log( (string) count( $config_entries ) ); return; } if ( empty( $config_entries ) ) { WP_CLI::error( 'No matching config entries found.' ); } if ( 'json' === $assoc_args['format'] ) { // Flatten the entries again for producing the JSON output that the spec tests understand. $json_array = []; foreach ( $config_entries as $config_entry ) { $json_array[ $config_entry['key'] ] = $config_entry['value']; } WP_CLI::log( wp_json_encode( $json_array ) ); return; } $formatter = new WP_CLI\Formatter( $assoc_args, $default_fields ); $formatter->display_items( $config_entries ); } /** * Filters the entries of an associative array based on a provided filter key. * * @param array $entries Associative array to filter. * @param array $fields Array of known fields. * @param array $assoc_args Filters to apply. * * @return array */ private function filter_entries( $entries, $fields, $assoc_args ) { $result = []; foreach ( $entries as $entry ) { foreach ( $fields as $field ) { if ( array_key_exists( $field, $assoc_args ) && $entry[ $field ] !== $assoc_args[ $field ] ) { continue 2; } } $result[] = $entry; } return $result; } /** * Deduce the transformer class from a transformer name. * * @param string $transformer Transformer name to get the class for. * @return string|false Class of the transformer, or false if none found. */ private function deduce_transformer_class( $transformer ) { $transformer_classes = $this->configuration->get( Configuration::KEY_TRANSFORMERS ); foreach ( $transformer_classes as $transformer_class ) { if ( $transformer === $this->get_transformer_name( $transformer_class ) ) { return (string) $transformer_class; } } return false; } /** * Get the name of a transformer from its class. * * @param string $transformer_class Transformer class to get the name for. * @return string Name of the transformer. */ private function get_transformer_name( $transformer_class ) { $name_parts = explode( '\\', $transformer_class ); return (string) array_pop( $name_parts ); } /** * Get the source of a transformer. * * @param string $transformer_class Class of the transformer to get the source for. * @return string Source of the transformer. Will be one of 'toolbox', 'plugin', 'third-party'. */ private function get_transformer_source( $transformer_class ) { if ( 0 === strpos( $transformer_class, 'AmpProject\\Optimizer\\Transformer\\' ) ) { return 'toolbox'; } if ( 0 === strpos( $transformer_class, 'AmpProject\\AmpWP\\Optimizer\\Transformer\\' ) ) { return 'plugin'; } return 'third-party'; } } Mon ami n’aime pas la pluie Archives - Infos Culture du Faso
dim 23 février 2025

Suivez-nous sur les réseaux sociaux

spot_img
AccueilTagsMon ami n’aime pas la pluie

Tag: Mon ami n’aime pas la pluie

Aucun article à afficher

- Advertisement -spot_img

Latest Articles

Vous ne pouvez pas copier le contenu de cette page