@@ -8,25 +8,25 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class SharedType extends AbstractDependencyType { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Holds a cached instance of the value stored (or returned) internally. |
|
| 13 | - * |
|
| 14 | - * @var mixed |
|
| 15 | - */ |
|
| 16 | - private $shared_instance; |
|
| 11 | + /** |
|
| 12 | + * Holds a cached instance of the value stored (or returned) internally. |
|
| 13 | + * |
|
| 14 | + * @var mixed |
|
| 15 | + */ |
|
| 16 | + private $shared_instance; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Returns the internal stored and shared value after initial generation. |
|
| 20 | - * |
|
| 21 | - * @param Container $container An instance of the dependency injection |
|
| 22 | - * container. |
|
| 23 | - * |
|
| 24 | - * @return mixed |
|
| 25 | - */ |
|
| 26 | - public function get( Container $container ) { |
|
| 27 | - if ( empty( $this->shared_instance ) ) { |
|
| 28 | - $this->shared_instance = $this->resolve_value( $container ); |
|
| 29 | - } |
|
| 30 | - return $this->shared_instance; |
|
| 31 | - } |
|
| 18 | + /** |
|
| 19 | + * Returns the internal stored and shared value after initial generation. |
|
| 20 | + * |
|
| 21 | + * @param Container $container An instance of the dependency injection |
|
| 22 | + * container. |
|
| 23 | + * |
|
| 24 | + * @return mixed |
|
| 25 | + */ |
|
| 26 | + public function get( Container $container ) { |
|
| 27 | + if ( empty( $this->shared_instance ) ) { |
|
| 28 | + $this->shared_instance = $this->resolve_value( $container ); |
|
| 29 | + } |
|
| 30 | + return $this->shared_instance; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -23,9 +23,9 @@ |
||
| 23 | 23 | * |
| 24 | 24 | * @return mixed |
| 25 | 25 | */ |
| 26 | - public function get( Container $container ) { |
|
| 27 | - if ( empty( $this->shared_instance ) ) { |
|
| 28 | - $this->shared_instance = $this->resolve_value( $container ); |
|
| 26 | + public function get(Container $container) { |
|
| 27 | + if (empty($this->shared_instance)) { |
|
| 28 | + $this->shared_instance = $this->resolve_value($container); |
|
| 29 | 29 | } |
| 30 | 30 | return $this->shared_instance; |
| 31 | 31 | } |
@@ -13,86 +13,86 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Container { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A map of Dependency Type objects used to resolve dependencies. |
|
| 18 | - * |
|
| 19 | - * @var AbstractDependencyType[] |
|
| 20 | - */ |
|
| 21 | - private $registry = []; |
|
| 16 | + /** |
|
| 17 | + * A map of Dependency Type objects used to resolve dependencies. |
|
| 18 | + * |
|
| 19 | + * @var AbstractDependencyType[] |
|
| 20 | + */ |
|
| 21 | + private $registry = []; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Public api for adding a factory to the container. |
|
| 25 | - * |
|
| 26 | - * Factory dependencies will have the instantiation callback invoked |
|
| 27 | - * every time the dependency is requested. |
|
| 28 | - * |
|
| 29 | - * Typical Usage: |
|
| 30 | - * |
|
| 31 | - * ``` |
|
| 32 | - * $container->register( MyClass::class, $container->factory( $mycallback ) ); |
|
| 33 | - * ``` |
|
| 34 | - * |
|
| 35 | - * @param Closure $instantiation_callback This will be invoked when the |
|
| 36 | - * dependency is required. It will |
|
| 37 | - * receive an instance of this |
|
| 38 | - * container so the callback can |
|
| 39 | - * retrieve dependencies from the |
|
| 40 | - * container. |
|
| 41 | - * |
|
| 42 | - * @return FactoryType An instance of the FactoryType dependency. |
|
| 43 | - */ |
|
| 44 | - public function factory( Closure $instantiation_callback ) { |
|
| 45 | - return new FactoryType( $instantiation_callback ); |
|
| 46 | - } |
|
| 23 | + /** |
|
| 24 | + * Public api for adding a factory to the container. |
|
| 25 | + * |
|
| 26 | + * Factory dependencies will have the instantiation callback invoked |
|
| 27 | + * every time the dependency is requested. |
|
| 28 | + * |
|
| 29 | + * Typical Usage: |
|
| 30 | + * |
|
| 31 | + * ``` |
|
| 32 | + * $container->register( MyClass::class, $container->factory( $mycallback ) ); |
|
| 33 | + * ``` |
|
| 34 | + * |
|
| 35 | + * @param Closure $instantiation_callback This will be invoked when the |
|
| 36 | + * dependency is required. It will |
|
| 37 | + * receive an instance of this |
|
| 38 | + * container so the callback can |
|
| 39 | + * retrieve dependencies from the |
|
| 40 | + * container. |
|
| 41 | + * |
|
| 42 | + * @return FactoryType An instance of the FactoryType dependency. |
|
| 43 | + */ |
|
| 44 | + public function factory( Closure $instantiation_callback ) { |
|
| 45 | + return new FactoryType( $instantiation_callback ); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Interface for registering a new dependency with the container. |
|
| 50 | - * |
|
| 51 | - * By default, the $value will be added as a shared dependency. This means |
|
| 52 | - * that it will be a single instance shared among any other classes having |
|
| 53 | - * that dependency. |
|
| 54 | - * |
|
| 55 | - * If you want a new instance every time it's required, then wrap the value |
|
| 56 | - * in a call to the factory method (@see Container::factory for example) |
|
| 57 | - * |
|
| 58 | - * Note: Currently if the provided id already is registered in the container, |
|
| 59 | - * the provided value is ignored. |
|
| 60 | - * |
|
| 61 | - * @param string $id A unique string identifier for the provided value. |
|
| 62 | - * Typically it's the fully qualified name for the |
|
| 63 | - * dependency. |
|
| 64 | - * @param mixed $value The value for the dependency. Typically, this is a |
|
| 65 | - * closure that will create the class instance needed. |
|
| 66 | - */ |
|
| 67 | - public function register( $id, $value ) { |
|
| 68 | - if ( empty( $this->registry[ $id ] ) ) { |
|
| 69 | - if ( ! $value instanceof FactoryType ) { |
|
| 70 | - $value = new SharedType( $value ); |
|
| 71 | - } |
|
| 72 | - $this->registry[ $id ] = $value; |
|
| 73 | - } |
|
| 74 | - } |
|
| 48 | + /** |
|
| 49 | + * Interface for registering a new dependency with the container. |
|
| 50 | + * |
|
| 51 | + * By default, the $value will be added as a shared dependency. This means |
|
| 52 | + * that it will be a single instance shared among any other classes having |
|
| 53 | + * that dependency. |
|
| 54 | + * |
|
| 55 | + * If you want a new instance every time it's required, then wrap the value |
|
| 56 | + * in a call to the factory method (@see Container::factory for example) |
|
| 57 | + * |
|
| 58 | + * Note: Currently if the provided id already is registered in the container, |
|
| 59 | + * the provided value is ignored. |
|
| 60 | + * |
|
| 61 | + * @param string $id A unique string identifier for the provided value. |
|
| 62 | + * Typically it's the fully qualified name for the |
|
| 63 | + * dependency. |
|
| 64 | + * @param mixed $value The value for the dependency. Typically, this is a |
|
| 65 | + * closure that will create the class instance needed. |
|
| 66 | + */ |
|
| 67 | + public function register( $id, $value ) { |
|
| 68 | + if ( empty( $this->registry[ $id ] ) ) { |
|
| 69 | + if ( ! $value instanceof FactoryType ) { |
|
| 70 | + $value = new SharedType( $value ); |
|
| 71 | + } |
|
| 72 | + $this->registry[ $id ] = $value; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Interface for retrieving the dependency stored in the container for the |
|
| 78 | - * given identifier. |
|
| 79 | - * |
|
| 80 | - * @param string $id The identifier for the dependency being retrieved. |
|
| 81 | - * @throws Exception If there is no dependency for the given identifier in |
|
| 82 | - * the container. |
|
| 83 | - * |
|
| 84 | - * @return mixed Typically a class instance. |
|
| 85 | - */ |
|
| 86 | - public function get( $id ) { |
|
| 87 | - if ( ! isset( $this->registry[ $id ] ) ) { |
|
| 88 | - // this is a developer facing exception, hence it is not localized. |
|
| 89 | - throw new Exception( |
|
| 90 | - sprintf( |
|
| 91 | - 'Cannot construct an instance of %s because it has not been registered.', |
|
| 92 | - $id |
|
| 93 | - ) |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - return $this->registry[ $id ]->get( $this ); |
|
| 97 | - } |
|
| 76 | + /** |
|
| 77 | + * Interface for retrieving the dependency stored in the container for the |
|
| 78 | + * given identifier. |
|
| 79 | + * |
|
| 80 | + * @param string $id The identifier for the dependency being retrieved. |
|
| 81 | + * @throws Exception If there is no dependency for the given identifier in |
|
| 82 | + * the container. |
|
| 83 | + * |
|
| 84 | + * @return mixed Typically a class instance. |
|
| 85 | + */ |
|
| 86 | + public function get( $id ) { |
|
| 87 | + if ( ! isset( $this->registry[ $id ] ) ) { |
|
| 88 | + // this is a developer facing exception, hence it is not localized. |
|
| 89 | + throw new Exception( |
|
| 90 | + sprintf( |
|
| 91 | + 'Cannot construct an instance of %s because it has not been registered.', |
|
| 92 | + $id |
|
| 93 | + ) |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + return $this->registry[ $id ]->get( $this ); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return FactoryType An instance of the FactoryType dependency. |
| 43 | 43 | */ |
| 44 | - public function factory( Closure $instantiation_callback ) { |
|
| 45 | - return new FactoryType( $instantiation_callback ); |
|
| 44 | + public function factory(Closure $instantiation_callback) { |
|
| 45 | + return new FactoryType($instantiation_callback); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -64,12 +64,12 @@ discard block |
||
| 64 | 64 | * @param mixed $value The value for the dependency. Typically, this is a |
| 65 | 65 | * closure that will create the class instance needed. |
| 66 | 66 | */ |
| 67 | - public function register( $id, $value ) { |
|
| 68 | - if ( empty( $this->registry[ $id ] ) ) { |
|
| 69 | - if ( ! $value instanceof FactoryType ) { |
|
| 70 | - $value = new SharedType( $value ); |
|
| 67 | + public function register($id, $value) { |
|
| 68 | + if (empty($this->registry[$id])) { |
|
| 69 | + if (!$value instanceof FactoryType) { |
|
| 70 | + $value = new SharedType($value); |
|
| 71 | 71 | } |
| 72 | - $this->registry[ $id ] = $value; |
|
| 72 | + $this->registry[$id] = $value; |
|
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return mixed Typically a class instance. |
| 85 | 85 | */ |
| 86 | - public function get( $id ) { |
|
| 87 | - if ( ! isset( $this->registry[ $id ] ) ) { |
|
| 86 | + public function get($id) { |
|
| 87 | + if (!isset($this->registry[$id])) { |
|
| 88 | 88 | // this is a developer facing exception, hence it is not localized. |
| 89 | 89 | throw new Exception( |
| 90 | 90 | sprintf( |
@@ -93,6 +93,6 @@ discard block |
||
| 93 | 93 | ) |
| 94 | 94 | ); |
| 95 | 95 | } |
| 96 | - return $this->registry[ $id ]->get( $this ); |
|
| 96 | + return $this->registry[$id]->get($this); |
|
| 97 | 97 | } |
| 98 | 98 | } |
@@ -12,43 +12,43 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class AbstractDependencyType { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Holds a callable or value provided for this type. |
|
| 17 | - * |
|
| 18 | - * @var mixed |
|
| 19 | - */ |
|
| 20 | - private $callable_or_value; |
|
| 15 | + /** |
|
| 16 | + * Holds a callable or value provided for this type. |
|
| 17 | + * |
|
| 18 | + * @var mixed |
|
| 19 | + */ |
|
| 20 | + private $callable_or_value; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Constructor |
|
| 24 | - * |
|
| 25 | - * @param mixed $callable_or_value A callable or value for the dependency |
|
| 26 | - * type instance. |
|
| 27 | - */ |
|
| 28 | - public function __construct( $callable_or_value ) { |
|
| 29 | - $this->callable_or_value = $callable_or_value; |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * Constructor |
|
| 24 | + * |
|
| 25 | + * @param mixed $callable_or_value A callable or value for the dependency |
|
| 26 | + * type instance. |
|
| 27 | + */ |
|
| 28 | + public function __construct( $callable_or_value ) { |
|
| 29 | + $this->callable_or_value = $callable_or_value; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Resolver for the internal dependency value. |
|
| 34 | - * |
|
| 35 | - * @param Container $container The Dependency Injection Container. |
|
| 36 | - * |
|
| 37 | - * @return mixed |
|
| 38 | - */ |
|
| 39 | - protected function resolve_value( Container $container ) { |
|
| 40 | - $callback = $this->callable_or_value; |
|
| 41 | - return \is_callable( $callback ) |
|
| 42 | - ? $callback( $container ) |
|
| 43 | - : $callback; |
|
| 44 | - } |
|
| 32 | + /** |
|
| 33 | + * Resolver for the internal dependency value. |
|
| 34 | + * |
|
| 35 | + * @param Container $container The Dependency Injection Container. |
|
| 36 | + * |
|
| 37 | + * @return mixed |
|
| 38 | + */ |
|
| 39 | + protected function resolve_value( Container $container ) { |
|
| 40 | + $callback = $this->callable_or_value; |
|
| 41 | + return \is_callable( $callback ) |
|
| 42 | + ? $callback( $container ) |
|
| 43 | + : $callback; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Retrieves the value stored internally for this DependencyType |
|
| 48 | - * |
|
| 49 | - * @param Container $container The Dependency Injection Container. |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - abstract public function get( Container $container ); |
|
| 46 | + /** |
|
| 47 | + * Retrieves the value stored internally for this DependencyType |
|
| 48 | + * |
|
| 49 | + * @param Container $container The Dependency Injection Container. |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + abstract public function get( Container $container ); |
|
| 54 | 54 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * @param mixed $callable_or_value A callable or value for the dependency |
| 26 | 26 | * type instance. |
| 27 | 27 | */ |
| 28 | - public function __construct( $callable_or_value ) { |
|
| 28 | + public function __construct($callable_or_value) { |
|
| 29 | 29 | $this->callable_or_value = $callable_or_value; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -36,10 +36,10 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @return mixed |
| 38 | 38 | */ |
| 39 | - protected function resolve_value( Container $container ) { |
|
| 39 | + protected function resolve_value(Container $container) { |
|
| 40 | 40 | $callback = $this->callable_or_value; |
| 41 | - return \is_callable( $callback ) |
|
| 42 | - ? $callback( $container ) |
|
| 41 | + return \is_callable($callback) |
|
| 42 | + ? $callback($container) |
|
| 43 | 43 | : $callback; |
| 44 | 44 | } |
| 45 | 45 | |
@@ -50,5 +50,5 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @return void |
| 52 | 52 | */ |
| 53 | - abstract public function get( Container $container ); |
|
| 53 | + abstract public function get(Container $container); |
|
| 54 | 54 | } |
@@ -7,15 +7,15 @@ |
||
| 7 | 7 | * @since 2.5.0 |
| 8 | 8 | */ |
| 9 | 9 | class FactoryType extends AbstractDependencyType { |
| 10 | - /** |
|
| 11 | - * Invokes and returns the value from the stored internal callback. |
|
| 12 | - * |
|
| 13 | - * @param Container $container An instance of the dependency injection |
|
| 14 | - * container. |
|
| 15 | - * |
|
| 16 | - * @return mixed |
|
| 17 | - */ |
|
| 18 | - public function get( Container $container ) { |
|
| 19 | - return $this->resolve_value( $container ); |
|
| 20 | - } |
|
| 10 | + /** |
|
| 11 | + * Invokes and returns the value from the stored internal callback. |
|
| 12 | + * |
|
| 13 | + * @param Container $container An instance of the dependency injection |
|
| 14 | + * container. |
|
| 15 | + * |
|
| 16 | + * @return mixed |
|
| 17 | + */ |
|
| 18 | + public function get( Container $container ) { |
|
| 19 | + return $this->resolve_value( $container ); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | * |
| 16 | 16 | * @return mixed |
| 17 | 17 | */ |
| 18 | - public function get( Container $container ) { |
|
| 19 | - return $this->resolve_value( $container ); |
|
| 18 | + public function get(Container $container) { |
|
| 19 | + return $this->resolve_value($container); |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -12,79 +12,79 @@ |
||
| 12 | 12 | * @deprecated 2.5.0 |
| 13 | 13 | */ |
| 14 | 14 | class BlocksWpQuery extends WP_Query { |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * Sets up the WordPress query, if parameter is not empty. |
|
| 19 | - * |
|
| 20 | - * Unlike the constructor in WP_Query, this does not RUN the query. |
|
| 21 | - * |
|
| 22 | - * @param string|array $query URL query string or array of vars. |
|
| 23 | - */ |
|
| 24 | - public function __construct( $query = '' ) { |
|
| 25 | - if ( ! empty( $query ) ) { |
|
| 26 | - $this->init(); |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * Sets up the WordPress query, if parameter is not empty. |
|
| 19 | + * |
|
| 20 | + * Unlike the constructor in WP_Query, this does not RUN the query. |
|
| 21 | + * |
|
| 22 | + * @param string|array $query URL query string or array of vars. |
|
| 23 | + */ |
|
| 24 | + public function __construct( $query = '' ) { |
|
| 25 | + if ( ! empty( $query ) ) { |
|
| 26 | + $this->init(); |
|
| 27 | 27 | |
| 28 | - // Remove current product from query to avoid infinite loops and out of memory issues. |
|
| 29 | - // That might happen if the store is using Gutenberg for product descriptions. |
|
| 30 | - // See https://github.com/woocommerce/woocommerce-blocks/issues/6416. |
|
| 31 | - global $post; |
|
| 32 | - if ( $post && array_key_exists( 'post__in', $query ) && is_array( $query['post__in'] ) ) { |
|
| 33 | - $global_post_id = $post->ID; |
|
| 34 | - $filtered_post__in = array_filter( |
|
| 35 | - $query['post__in'], |
|
| 36 | - static function ( $post_id ) use ( $global_post_id ) { |
|
| 37 | - if ( $global_post_id === $post_id ) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 42 | - ); |
|
| 43 | - $query['post__in'] = $filtered_post__in; |
|
| 44 | - } |
|
| 28 | + // Remove current product from query to avoid infinite loops and out of memory issues. |
|
| 29 | + // That might happen if the store is using Gutenberg for product descriptions. |
|
| 30 | + // See https://github.com/woocommerce/woocommerce-blocks/issues/6416. |
|
| 31 | + global $post; |
|
| 32 | + if ( $post && array_key_exists( 'post__in', $query ) && is_array( $query['post__in'] ) ) { |
|
| 33 | + $global_post_id = $post->ID; |
|
| 34 | + $filtered_post__in = array_filter( |
|
| 35 | + $query['post__in'], |
|
| 36 | + static function ( $post_id ) use ( $global_post_id ) { |
|
| 37 | + if ( $global_post_id === $post_id ) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | + ); |
|
| 43 | + $query['post__in'] = $filtered_post__in; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - $this->query = wp_parse_args( $query ); |
|
| 47 | - $this->query_vars = $this->query; |
|
| 48 | - $this->parse_query_vars(); |
|
| 49 | - } |
|
| 50 | - } |
|
| 46 | + $this->query = wp_parse_args( $query ); |
|
| 47 | + $this->query_vars = $this->query; |
|
| 48 | + $this->parse_query_vars(); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get cached posts, if a cache exists. |
|
| 54 | - * |
|
| 55 | - * A hash is generated using the array of query_vars. If doing custom queries via filters such as posts_where |
|
| 56 | - * (where the SQL query is manipulated directly) you can still ensure there is a unique hash by injecting custom |
|
| 57 | - * query vars via the parse_query filter. For example: |
|
| 58 | - * |
|
| 59 | - * add_filter( 'parse_query', function( $wp_query ) { |
|
| 60 | - * $wp_query->query_vars['my_custom_query_var'] = true; |
|
| 61 | - * } ); |
|
| 62 | - * |
|
| 63 | - * Doing so won't have any negative effect on the query itself, and it will cause the hash to change. |
|
| 64 | - * |
|
| 65 | - * @param string $transient_version Transient version to allow for invalidation. |
|
| 66 | - * @return WP_Post[]|int[] Array of post objects or post IDs. |
|
| 67 | - */ |
|
| 68 | - public function get_cached_posts( $transient_version = '' ) { |
|
| 69 | - $hash = md5( wp_json_encode( $this->query_vars ) ); |
|
| 70 | - $transient_name = 'wc_blocks_query_' . $hash; |
|
| 71 | - $transient_value = get_transient( $transient_name ); |
|
| 52 | + /** |
|
| 53 | + * Get cached posts, if a cache exists. |
|
| 54 | + * |
|
| 55 | + * A hash is generated using the array of query_vars. If doing custom queries via filters such as posts_where |
|
| 56 | + * (where the SQL query is manipulated directly) you can still ensure there is a unique hash by injecting custom |
|
| 57 | + * query vars via the parse_query filter. For example: |
|
| 58 | + * |
|
| 59 | + * add_filter( 'parse_query', function( $wp_query ) { |
|
| 60 | + * $wp_query->query_vars['my_custom_query_var'] = true; |
|
| 61 | + * } ); |
|
| 62 | + * |
|
| 63 | + * Doing so won't have any negative effect on the query itself, and it will cause the hash to change. |
|
| 64 | + * |
|
| 65 | + * @param string $transient_version Transient version to allow for invalidation. |
|
| 66 | + * @return WP_Post[]|int[] Array of post objects or post IDs. |
|
| 67 | + */ |
|
| 68 | + public function get_cached_posts( $transient_version = '' ) { |
|
| 69 | + $hash = md5( wp_json_encode( $this->query_vars ) ); |
|
| 70 | + $transient_name = 'wc_blocks_query_' . $hash; |
|
| 71 | + $transient_value = get_transient( $transient_name ); |
|
| 72 | 72 | |
| 73 | - if ( isset( $transient_value, $transient_value['version'], $transient_value['value'] ) && $transient_value['version'] === $transient_version ) { |
|
| 74 | - return $transient_value['value']; |
|
| 75 | - } |
|
| 73 | + if ( isset( $transient_value, $transient_value['version'], $transient_value['value'] ) && $transient_value['version'] === $transient_version ) { |
|
| 74 | + return $transient_value['value']; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - $results = $this->get_posts(); |
|
| 77 | + $results = $this->get_posts(); |
|
| 78 | 78 | |
| 79 | - set_transient( |
|
| 80 | - $transient_name, |
|
| 81 | - array( |
|
| 82 | - 'version' => $transient_version, |
|
| 83 | - 'value' => $results, |
|
| 84 | - ), |
|
| 85 | - DAY_IN_SECONDS * 30 |
|
| 86 | - ); |
|
| 79 | + set_transient( |
|
| 80 | + $transient_name, |
|
| 81 | + array( |
|
| 82 | + 'version' => $transient_version, |
|
| 83 | + 'value' => $results, |
|
| 84 | + ), |
|
| 85 | + DAY_IN_SECONDS * 30 |
|
| 86 | + ); |
|
| 87 | 87 | |
| 88 | - return $results; |
|
| 89 | - } |
|
| 88 | + return $results; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -21,20 +21,20 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param string|array $query URL query string or array of vars. |
| 23 | 23 | */ |
| 24 | - public function __construct( $query = '' ) { |
|
| 25 | - if ( ! empty( $query ) ) { |
|
| 24 | + public function __construct($query = '') { |
|
| 25 | + if (!empty($query)) { |
|
| 26 | 26 | $this->init(); |
| 27 | 27 | |
| 28 | 28 | // Remove current product from query to avoid infinite loops and out of memory issues. |
| 29 | 29 | // That might happen if the store is using Gutenberg for product descriptions. |
| 30 | 30 | // See https://github.com/woocommerce/woocommerce-blocks/issues/6416. |
| 31 | 31 | global $post; |
| 32 | - if ( $post && array_key_exists( 'post__in', $query ) && is_array( $query['post__in'] ) ) { |
|
| 32 | + if ($post && array_key_exists('post__in', $query) && is_array($query['post__in'])) { |
|
| 33 | 33 | $global_post_id = $post->ID; |
| 34 | 34 | $filtered_post__in = array_filter( |
| 35 | 35 | $query['post__in'], |
| 36 | - static function ( $post_id ) use ( $global_post_id ) { |
|
| 37 | - if ( $global_post_id === $post_id ) { |
|
| 36 | + static function($post_id) use ($global_post_id) { |
|
| 37 | + if ($global_post_id === $post_id) { |
|
| 38 | 38 | return false; |
| 39 | 39 | } |
| 40 | 40 | return true; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | $query['post__in'] = $filtered_post__in; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - $this->query = wp_parse_args( $query ); |
|
| 46 | + $this->query = wp_parse_args($query); |
|
| 47 | 47 | $this->query_vars = $this->query; |
| 48 | 48 | $this->parse_query_vars(); |
| 49 | 49 | } |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | * @param string $transient_version Transient version to allow for invalidation. |
| 66 | 66 | * @return WP_Post[]|int[] Array of post objects or post IDs. |
| 67 | 67 | */ |
| 68 | - public function get_cached_posts( $transient_version = '' ) { |
|
| 69 | - $hash = md5( wp_json_encode( $this->query_vars ) ); |
|
| 68 | + public function get_cached_posts($transient_version = '') { |
|
| 69 | + $hash = md5(wp_json_encode($this->query_vars)); |
|
| 70 | 70 | $transient_name = 'wc_blocks_query_' . $hash; |
| 71 | - $transient_value = get_transient( $transient_name ); |
|
| 71 | + $transient_value = get_transient($transient_name); |
|
| 72 | 72 | |
| 73 | - if ( isset( $transient_value, $transient_value['version'], $transient_value['value'] ) && $transient_value['version'] === $transient_version ) { |
|
| 73 | + if (isset($transient_value, $transient_value['version'], $transient_value['value']) && $transient_value['version'] === $transient_version) { |
|
| 74 | 74 | return $transient_value['value']; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -6,417 +6,417 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class StyleAttributesUtils { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Get class and style for font-size from attributes. |
|
| 11 | - * |
|
| 12 | - * @param array $attributes Block attributes. |
|
| 13 | - * |
|
| 14 | - * @return (array | null) |
|
| 15 | - */ |
|
| 16 | - public static function get_font_size_class_and_style( $attributes ) { |
|
| 17 | - |
|
| 18 | - $font_size = $attributes['fontSize'] ?? ''; |
|
| 19 | - |
|
| 20 | - $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; |
|
| 21 | - |
|
| 22 | - if ( ! $font_size && '' === $custom_font_size ) { |
|
| 23 | - return null; |
|
| 24 | - }; |
|
| 25 | - |
|
| 26 | - if ( $font_size ) { |
|
| 27 | - return array( |
|
| 28 | - 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), |
|
| 29 | - 'style' => null, |
|
| 30 | - ); |
|
| 31 | - } elseif ( '' !== $custom_font_size ) { |
|
| 32 | - return array( |
|
| 33 | - 'class' => null, |
|
| 34 | - 'style' => sprintf( 'font-size: %s;', $custom_font_size ), |
|
| 35 | - ); |
|
| 36 | - } |
|
| 37 | - return null; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get class and style for font-family from attributes. |
|
| 42 | - * |
|
| 43 | - * @param array $attributes Block attributes. |
|
| 44 | - * |
|
| 45 | - * @return (array | null) |
|
| 46 | - */ |
|
| 47 | - public static function get_font_family_class_and_style( $attributes ) { |
|
| 48 | - |
|
| 49 | - $font_family = $attributes['fontFamily'] ?? ''; |
|
| 50 | - |
|
| 51 | - if ( $font_family ) { |
|
| 52 | - return array( |
|
| 53 | - 'class' => sprintf( 'has-%s-font-family', $font_family ), |
|
| 54 | - 'style' => null, |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - return null; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Get class and style for text-color from attributes. |
|
| 62 | - * |
|
| 63 | - * @param array $attributes Block attributes. |
|
| 64 | - * |
|
| 65 | - * @return (array | null) |
|
| 66 | - */ |
|
| 67 | - public static function get_text_color_class_and_style( $attributes ) { |
|
| 68 | - |
|
| 69 | - $text_color = $attributes['textColor'] ?? ''; |
|
| 70 | - |
|
| 71 | - $custom_text_color = $attributes['style']['color']['text'] ?? ''; |
|
| 72 | - |
|
| 73 | - if ( ! $text_color && ! $custom_text_color ) { |
|
| 74 | - return null; |
|
| 75 | - }; |
|
| 76 | - |
|
| 77 | - if ( $text_color ) { |
|
| 78 | - return array( |
|
| 79 | - 'class' => sprintf( 'has-text-color has-%s-color', $text_color ), |
|
| 80 | - 'style' => null, |
|
| 81 | - 'value' => self::get_preset_value( $text_color ), |
|
| 82 | - ); |
|
| 83 | - } elseif ( $custom_text_color ) { |
|
| 84 | - return array( |
|
| 85 | - 'class' => null, |
|
| 86 | - 'style' => sprintf( 'color: %s;', $custom_text_color ), |
|
| 87 | - 'value' => $custom_text_color, |
|
| 88 | - ); |
|
| 89 | - } |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get class and style for link-color from attributes. |
|
| 95 | - * |
|
| 96 | - * @param array $attributes Block attributes. |
|
| 97 | - * |
|
| 98 | - * @return (array | null) |
|
| 99 | - */ |
|
| 100 | - public static function get_link_color_class_and_style( $attributes ) { |
|
| 101 | - |
|
| 102 | - if ( ! isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
|
| 103 | - return null; |
|
| 104 | - }; |
|
| 105 | - |
|
| 106 | - $link_color = $attributes['style']['elements']['link']['color']['text']; |
|
| 107 | - |
|
| 108 | - // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. |
|
| 109 | - // If the link color is selected from the core color picker, the value of $link_color is an hex value. |
|
| 110 | - // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. |
|
| 111 | - $index_named_link_color = strrpos( $link_color, '|' ); |
|
| 112 | - |
|
| 113 | - if ( ! empty( $index_named_link_color ) ) { |
|
| 114 | - $parsed_named_link_color = substr( $link_color, $index_named_link_color + 1 ); |
|
| 115 | - return array( |
|
| 116 | - 'class' => null, |
|
| 117 | - 'style' => sprintf( 'color: %s;', self::get_preset_value( $parsed_named_link_color ) ), |
|
| 118 | - 'value' => self::get_preset_value( $parsed_named_link_color ), |
|
| 119 | - ); |
|
| 120 | - } else { |
|
| 121 | - return array( |
|
| 122 | - 'class' => null, |
|
| 123 | - 'style' => sprintf( 'color: %s;', $link_color ), |
|
| 124 | - 'value' => $link_color, |
|
| 125 | - ); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get class and style for line height from attributes. |
|
| 131 | - * |
|
| 132 | - * @param array $attributes Block attributes. |
|
| 133 | - * |
|
| 134 | - * @return (array | null) |
|
| 135 | - */ |
|
| 136 | - public static function get_line_height_class_and_style( $attributes ) { |
|
| 137 | - |
|
| 138 | - $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; |
|
| 139 | - |
|
| 140 | - if ( ! $line_height ) { |
|
| 141 | - return null; |
|
| 142 | - }; |
|
| 143 | - |
|
| 144 | - return array( |
|
| 145 | - 'class' => null, |
|
| 146 | - 'style' => sprintf( 'line-height: %s;', $line_height ), |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get class and style for background-color from attributes. |
|
| 152 | - * |
|
| 153 | - * @param array $attributes Block attributes. |
|
| 154 | - * |
|
| 155 | - * @return (array | null) |
|
| 156 | - */ |
|
| 157 | - public static function get_background_color_class_and_style( $attributes ) { |
|
| 158 | - |
|
| 159 | - $background_color = $attributes['backgroundColor'] ?? ''; |
|
| 160 | - |
|
| 161 | - $custom_background_color = $attributes['style']['color']['background'] ?? ''; |
|
| 162 | - |
|
| 163 | - if ( ! $background_color && '' === $custom_background_color ) { |
|
| 164 | - return null; |
|
| 165 | - }; |
|
| 166 | - |
|
| 167 | - if ( $background_color ) { |
|
| 168 | - return array( |
|
| 169 | - 'class' => sprintf( 'has-background has-%s-background-color', $background_color ), |
|
| 170 | - 'style' => null, |
|
| 171 | - 'value' => self::get_preset_value( $background_color ), |
|
| 172 | - ); |
|
| 173 | - } elseif ( '' !== $custom_background_color ) { |
|
| 174 | - return array( |
|
| 175 | - 'class' => null, |
|
| 176 | - 'style' => sprintf( 'background-color: %s;', $custom_background_color ), |
|
| 177 | - 'value' => $custom_background_color, |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Get class and style for border-color from attributes. |
|
| 185 | - * |
|
| 186 | - * @param array $attributes Block attributes. |
|
| 187 | - * |
|
| 188 | - * @return (array | null) |
|
| 189 | - */ |
|
| 190 | - public static function get_border_color_class_and_style( $attributes ) { |
|
| 191 | - |
|
| 192 | - $border_color = $attributes['borderColor'] ?? ''; |
|
| 193 | - |
|
| 194 | - $custom_border_color = $attributes['style']['border']['color'] ?? ''; |
|
| 195 | - |
|
| 196 | - if ( ! $border_color && '' === $custom_border_color ) { |
|
| 197 | - return null; |
|
| 198 | - }; |
|
| 199 | - |
|
| 200 | - if ( $border_color ) { |
|
| 201 | - return array( |
|
| 202 | - 'class' => sprintf( 'has-border-color has-%s-border-color', $border_color ), |
|
| 203 | - 'style' => null, |
|
| 204 | - ); |
|
| 205 | - } elseif ( '' !== $custom_border_color ) { |
|
| 206 | - return array( |
|
| 207 | - 'class' => null, |
|
| 208 | - 'style' => sprintf( 'border-color: %s;', $custom_border_color ), |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - return null; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get class and style for border-radius from attributes. |
|
| 216 | - * |
|
| 217 | - * @param array $attributes Block attributes. |
|
| 218 | - * |
|
| 219 | - * @return (array | null) |
|
| 220 | - */ |
|
| 221 | - public static function get_border_radius_class_and_style( $attributes ) { |
|
| 222 | - |
|
| 223 | - $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; |
|
| 224 | - |
|
| 225 | - if ( '' === $custom_border_radius ) { |
|
| 226 | - return null; |
|
| 227 | - }; |
|
| 228 | - |
|
| 229 | - return array( |
|
| 230 | - 'class' => null, |
|
| 231 | - 'style' => sprintf( 'border-radius: %s;', $custom_border_radius ), |
|
| 232 | - ); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Get class and style for border width from attributes. |
|
| 237 | - * |
|
| 238 | - * @param array $attributes Block attributes. |
|
| 239 | - * |
|
| 240 | - * @return (array | null) |
|
| 241 | - */ |
|
| 242 | - public static function get_border_width_class_and_style( $attributes ) { |
|
| 243 | - |
|
| 244 | - $custom_border_width = $attributes['style']['border']['width'] ?? ''; |
|
| 245 | - |
|
| 246 | - if ( '' === $custom_border_width ) { |
|
| 247 | - return null; |
|
| 248 | - }; |
|
| 249 | - |
|
| 250 | - return array( |
|
| 251 | - 'class' => null, |
|
| 252 | - 'style' => sprintf( 'border-width: %s;', $custom_border_width ), |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Get class and style for align from attributes. |
|
| 258 | - * |
|
| 259 | - * @param array $attributes Block attributes. |
|
| 260 | - * |
|
| 261 | - * @return (array | null) |
|
| 262 | - */ |
|
| 263 | - public static function get_align_class_and_style( $attributes ) { |
|
| 264 | - |
|
| 265 | - $align_attribute = isset( $attributes['align'] ) ? $attributes['align'] : null; |
|
| 266 | - |
|
| 267 | - if ( ! $align_attribute ) { |
|
| 268 | - return null; |
|
| 269 | - }; |
|
| 270 | - |
|
| 271 | - if ( 'wide' === $align_attribute ) { |
|
| 272 | - return array( |
|
| 273 | - 'class' => 'alignwide', |
|
| 274 | - 'style' => null, |
|
| 275 | - ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - if ( 'full' === $align_attribute ) { |
|
| 279 | - return array( |
|
| 280 | - 'class' => 'alignfull', |
|
| 281 | - 'style' => null, |
|
| 282 | - ); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if ( 'left' === $align_attribute ) { |
|
| 286 | - return array( |
|
| 287 | - 'class' => 'alignleft', |
|
| 288 | - 'style' => null, |
|
| 289 | - ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if ( 'right' === $align_attribute ) { |
|
| 293 | - return array( |
|
| 294 | - 'class' => 'alignright', |
|
| 295 | - 'style' => null, |
|
| 296 | - ); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ( 'center' === $align_attribute ) { |
|
| 300 | - return array( |
|
| 301 | - 'class' => 'aligncenter', |
|
| 302 | - 'style' => null, |
|
| 303 | - ); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return null; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get class and style for padding from attributes. |
|
| 311 | - * |
|
| 312 | - * @param array $attributes Block attributes. |
|
| 313 | - * |
|
| 314 | - * @return (array | null) |
|
| 315 | - */ |
|
| 316 | - public static function get_padding_class_and_style( $attributes ) { |
|
| 317 | - $padding = isset( $attributes['style']['spacing']['padding'] ) ? $attributes['style']['spacing']['padding'] : null; |
|
| 318 | - |
|
| 319 | - if ( ! $padding ) { |
|
| 320 | - return null; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return array( |
|
| 324 | - 'class' => null, |
|
| 325 | - 'style' => sprintf( 'padding: %s;', implode( ' ', $padding ) ), |
|
| 326 | - ); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get classes and styles from attributes. |
|
| 331 | - * |
|
| 332 | - * @param array $attributes Block attributes. |
|
| 333 | - * @param array $properties Properties to get classes/styles from. |
|
| 334 | - * |
|
| 335 | - * @return array |
|
| 336 | - */ |
|
| 337 | - public static function get_classes_and_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 338 | - $classes_and_styles = array( |
|
| 339 | - 'line_height' => self::get_line_height_class_and_style( $attributes ), |
|
| 340 | - 'text_color' => self::get_text_color_class_and_style( $attributes ), |
|
| 341 | - 'font_size' => self::get_font_size_class_and_style( $attributes ), |
|
| 342 | - 'font_family' => self::get_font_family_class_and_style( $attributes ), |
|
| 343 | - 'link_color' => self::get_link_color_class_and_style( $attributes ), |
|
| 344 | - 'background_color' => self::get_background_color_class_and_style( $attributes ), |
|
| 345 | - 'border_color' => self::get_border_color_class_and_style( $attributes ), |
|
| 346 | - 'border_radius' => self::get_border_radius_class_and_style( $attributes ), |
|
| 347 | - 'border_width' => self::get_border_width_class_and_style( $attributes ), |
|
| 348 | - 'padding' => self::get_padding_class_and_style( $attributes ), |
|
| 349 | - ); |
|
| 350 | - |
|
| 351 | - if ( ! empty( $properties ) ) { |
|
| 352 | - foreach ( $classes_and_styles as $key => $value ) { |
|
| 353 | - if ( ! in_array( $key, $properties, true ) ) { |
|
| 354 | - unset( $classes_and_styles[ $key ] ); |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $classes_and_styles = array_filter( $classes_and_styles ); |
|
| 360 | - |
|
| 361 | - $classes = array_map( |
|
| 362 | - function( $item ) { |
|
| 363 | - return $item['class']; |
|
| 364 | - }, |
|
| 365 | - $classes_and_styles |
|
| 366 | - ); |
|
| 367 | - |
|
| 368 | - $styles = array_map( |
|
| 369 | - function( $item ) { |
|
| 370 | - return $item['style']; |
|
| 371 | - }, |
|
| 372 | - $classes_and_styles |
|
| 373 | - ); |
|
| 374 | - |
|
| 375 | - $classes = array_filter( $classes ); |
|
| 376 | - $styles = array_filter( $styles ); |
|
| 377 | - |
|
| 378 | - return array( |
|
| 379 | - 'classes' => implode( ' ', $classes ), |
|
| 380 | - 'styles' => implode( ' ', $styles ), |
|
| 381 | - ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Get space-separated classes from block attributes. |
|
| 386 | - * |
|
| 387 | - * @param array $attributes Block attributes. |
|
| 388 | - * @param array $properties Properties to get classes from. |
|
| 389 | - * |
|
| 390 | - * @return string Space-separated classes. |
|
| 391 | - */ |
|
| 392 | - public static function get_classes_by_attributes( $attributes, $properties = array() ) { |
|
| 393 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 394 | - |
|
| 395 | - return $classes_and_styles['classes']; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get space-separated style rules from block attributes. |
|
| 400 | - * |
|
| 401 | - * @param array $attributes Block attributes. |
|
| 402 | - * @param array $properties Properties to get styles from. |
|
| 403 | - * |
|
| 404 | - * @return string Space-separated style rules. |
|
| 405 | - */ |
|
| 406 | - public static function get_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 407 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 408 | - |
|
| 409 | - return $classes_and_styles['styles']; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get CSS value for color preset. |
|
| 414 | - * |
|
| 415 | - * @param string $preset_name Preset name. |
|
| 416 | - * |
|
| 417 | - * @return string CSS value for color preset. |
|
| 418 | - */ |
|
| 419 | - public static function get_preset_value( $preset_name ) { |
|
| 420 | - return "var(--wp--preset--color--$preset_name)"; |
|
| 421 | - } |
|
| 9 | + /** |
|
| 10 | + * Get class and style for font-size from attributes. |
|
| 11 | + * |
|
| 12 | + * @param array $attributes Block attributes. |
|
| 13 | + * |
|
| 14 | + * @return (array | null) |
|
| 15 | + */ |
|
| 16 | + public static function get_font_size_class_and_style( $attributes ) { |
|
| 17 | + |
|
| 18 | + $font_size = $attributes['fontSize'] ?? ''; |
|
| 19 | + |
|
| 20 | + $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; |
|
| 21 | + |
|
| 22 | + if ( ! $font_size && '' === $custom_font_size ) { |
|
| 23 | + return null; |
|
| 24 | + }; |
|
| 25 | + |
|
| 26 | + if ( $font_size ) { |
|
| 27 | + return array( |
|
| 28 | + 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), |
|
| 29 | + 'style' => null, |
|
| 30 | + ); |
|
| 31 | + } elseif ( '' !== $custom_font_size ) { |
|
| 32 | + return array( |
|
| 33 | + 'class' => null, |
|
| 34 | + 'style' => sprintf( 'font-size: %s;', $custom_font_size ), |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | + return null; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get class and style for font-family from attributes. |
|
| 42 | + * |
|
| 43 | + * @param array $attributes Block attributes. |
|
| 44 | + * |
|
| 45 | + * @return (array | null) |
|
| 46 | + */ |
|
| 47 | + public static function get_font_family_class_and_style( $attributes ) { |
|
| 48 | + |
|
| 49 | + $font_family = $attributes['fontFamily'] ?? ''; |
|
| 50 | + |
|
| 51 | + if ( $font_family ) { |
|
| 52 | + return array( |
|
| 53 | + 'class' => sprintf( 'has-%s-font-family', $font_family ), |
|
| 54 | + 'style' => null, |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + return null; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Get class and style for text-color from attributes. |
|
| 62 | + * |
|
| 63 | + * @param array $attributes Block attributes. |
|
| 64 | + * |
|
| 65 | + * @return (array | null) |
|
| 66 | + */ |
|
| 67 | + public static function get_text_color_class_and_style( $attributes ) { |
|
| 68 | + |
|
| 69 | + $text_color = $attributes['textColor'] ?? ''; |
|
| 70 | + |
|
| 71 | + $custom_text_color = $attributes['style']['color']['text'] ?? ''; |
|
| 72 | + |
|
| 73 | + if ( ! $text_color && ! $custom_text_color ) { |
|
| 74 | + return null; |
|
| 75 | + }; |
|
| 76 | + |
|
| 77 | + if ( $text_color ) { |
|
| 78 | + return array( |
|
| 79 | + 'class' => sprintf( 'has-text-color has-%s-color', $text_color ), |
|
| 80 | + 'style' => null, |
|
| 81 | + 'value' => self::get_preset_value( $text_color ), |
|
| 82 | + ); |
|
| 83 | + } elseif ( $custom_text_color ) { |
|
| 84 | + return array( |
|
| 85 | + 'class' => null, |
|
| 86 | + 'style' => sprintf( 'color: %s;', $custom_text_color ), |
|
| 87 | + 'value' => $custom_text_color, |
|
| 88 | + ); |
|
| 89 | + } |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get class and style for link-color from attributes. |
|
| 95 | + * |
|
| 96 | + * @param array $attributes Block attributes. |
|
| 97 | + * |
|
| 98 | + * @return (array | null) |
|
| 99 | + */ |
|
| 100 | + public static function get_link_color_class_and_style( $attributes ) { |
|
| 101 | + |
|
| 102 | + if ( ! isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
|
| 103 | + return null; |
|
| 104 | + }; |
|
| 105 | + |
|
| 106 | + $link_color = $attributes['style']['elements']['link']['color']['text']; |
|
| 107 | + |
|
| 108 | + // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. |
|
| 109 | + // If the link color is selected from the core color picker, the value of $link_color is an hex value. |
|
| 110 | + // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. |
|
| 111 | + $index_named_link_color = strrpos( $link_color, '|' ); |
|
| 112 | + |
|
| 113 | + if ( ! empty( $index_named_link_color ) ) { |
|
| 114 | + $parsed_named_link_color = substr( $link_color, $index_named_link_color + 1 ); |
|
| 115 | + return array( |
|
| 116 | + 'class' => null, |
|
| 117 | + 'style' => sprintf( 'color: %s;', self::get_preset_value( $parsed_named_link_color ) ), |
|
| 118 | + 'value' => self::get_preset_value( $parsed_named_link_color ), |
|
| 119 | + ); |
|
| 120 | + } else { |
|
| 121 | + return array( |
|
| 122 | + 'class' => null, |
|
| 123 | + 'style' => sprintf( 'color: %s;', $link_color ), |
|
| 124 | + 'value' => $link_color, |
|
| 125 | + ); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get class and style for line height from attributes. |
|
| 131 | + * |
|
| 132 | + * @param array $attributes Block attributes. |
|
| 133 | + * |
|
| 134 | + * @return (array | null) |
|
| 135 | + */ |
|
| 136 | + public static function get_line_height_class_and_style( $attributes ) { |
|
| 137 | + |
|
| 138 | + $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; |
|
| 139 | + |
|
| 140 | + if ( ! $line_height ) { |
|
| 141 | + return null; |
|
| 142 | + }; |
|
| 143 | + |
|
| 144 | + return array( |
|
| 145 | + 'class' => null, |
|
| 146 | + 'style' => sprintf( 'line-height: %s;', $line_height ), |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get class and style for background-color from attributes. |
|
| 152 | + * |
|
| 153 | + * @param array $attributes Block attributes. |
|
| 154 | + * |
|
| 155 | + * @return (array | null) |
|
| 156 | + */ |
|
| 157 | + public static function get_background_color_class_and_style( $attributes ) { |
|
| 158 | + |
|
| 159 | + $background_color = $attributes['backgroundColor'] ?? ''; |
|
| 160 | + |
|
| 161 | + $custom_background_color = $attributes['style']['color']['background'] ?? ''; |
|
| 162 | + |
|
| 163 | + if ( ! $background_color && '' === $custom_background_color ) { |
|
| 164 | + return null; |
|
| 165 | + }; |
|
| 166 | + |
|
| 167 | + if ( $background_color ) { |
|
| 168 | + return array( |
|
| 169 | + 'class' => sprintf( 'has-background has-%s-background-color', $background_color ), |
|
| 170 | + 'style' => null, |
|
| 171 | + 'value' => self::get_preset_value( $background_color ), |
|
| 172 | + ); |
|
| 173 | + } elseif ( '' !== $custom_background_color ) { |
|
| 174 | + return array( |
|
| 175 | + 'class' => null, |
|
| 176 | + 'style' => sprintf( 'background-color: %s;', $custom_background_color ), |
|
| 177 | + 'value' => $custom_background_color, |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Get class and style for border-color from attributes. |
|
| 185 | + * |
|
| 186 | + * @param array $attributes Block attributes. |
|
| 187 | + * |
|
| 188 | + * @return (array | null) |
|
| 189 | + */ |
|
| 190 | + public static function get_border_color_class_and_style( $attributes ) { |
|
| 191 | + |
|
| 192 | + $border_color = $attributes['borderColor'] ?? ''; |
|
| 193 | + |
|
| 194 | + $custom_border_color = $attributes['style']['border']['color'] ?? ''; |
|
| 195 | + |
|
| 196 | + if ( ! $border_color && '' === $custom_border_color ) { |
|
| 197 | + return null; |
|
| 198 | + }; |
|
| 199 | + |
|
| 200 | + if ( $border_color ) { |
|
| 201 | + return array( |
|
| 202 | + 'class' => sprintf( 'has-border-color has-%s-border-color', $border_color ), |
|
| 203 | + 'style' => null, |
|
| 204 | + ); |
|
| 205 | + } elseif ( '' !== $custom_border_color ) { |
|
| 206 | + return array( |
|
| 207 | + 'class' => null, |
|
| 208 | + 'style' => sprintf( 'border-color: %s;', $custom_border_color ), |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + return null; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get class and style for border-radius from attributes. |
|
| 216 | + * |
|
| 217 | + * @param array $attributes Block attributes. |
|
| 218 | + * |
|
| 219 | + * @return (array | null) |
|
| 220 | + */ |
|
| 221 | + public static function get_border_radius_class_and_style( $attributes ) { |
|
| 222 | + |
|
| 223 | + $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; |
|
| 224 | + |
|
| 225 | + if ( '' === $custom_border_radius ) { |
|
| 226 | + return null; |
|
| 227 | + }; |
|
| 228 | + |
|
| 229 | + return array( |
|
| 230 | + 'class' => null, |
|
| 231 | + 'style' => sprintf( 'border-radius: %s;', $custom_border_radius ), |
|
| 232 | + ); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Get class and style for border width from attributes. |
|
| 237 | + * |
|
| 238 | + * @param array $attributes Block attributes. |
|
| 239 | + * |
|
| 240 | + * @return (array | null) |
|
| 241 | + */ |
|
| 242 | + public static function get_border_width_class_and_style( $attributes ) { |
|
| 243 | + |
|
| 244 | + $custom_border_width = $attributes['style']['border']['width'] ?? ''; |
|
| 245 | + |
|
| 246 | + if ( '' === $custom_border_width ) { |
|
| 247 | + return null; |
|
| 248 | + }; |
|
| 249 | + |
|
| 250 | + return array( |
|
| 251 | + 'class' => null, |
|
| 252 | + 'style' => sprintf( 'border-width: %s;', $custom_border_width ), |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Get class and style for align from attributes. |
|
| 258 | + * |
|
| 259 | + * @param array $attributes Block attributes. |
|
| 260 | + * |
|
| 261 | + * @return (array | null) |
|
| 262 | + */ |
|
| 263 | + public static function get_align_class_and_style( $attributes ) { |
|
| 264 | + |
|
| 265 | + $align_attribute = isset( $attributes['align'] ) ? $attributes['align'] : null; |
|
| 266 | + |
|
| 267 | + if ( ! $align_attribute ) { |
|
| 268 | + return null; |
|
| 269 | + }; |
|
| 270 | + |
|
| 271 | + if ( 'wide' === $align_attribute ) { |
|
| 272 | + return array( |
|
| 273 | + 'class' => 'alignwide', |
|
| 274 | + 'style' => null, |
|
| 275 | + ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + if ( 'full' === $align_attribute ) { |
|
| 279 | + return array( |
|
| 280 | + 'class' => 'alignfull', |
|
| 281 | + 'style' => null, |
|
| 282 | + ); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if ( 'left' === $align_attribute ) { |
|
| 286 | + return array( |
|
| 287 | + 'class' => 'alignleft', |
|
| 288 | + 'style' => null, |
|
| 289 | + ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if ( 'right' === $align_attribute ) { |
|
| 293 | + return array( |
|
| 294 | + 'class' => 'alignright', |
|
| 295 | + 'style' => null, |
|
| 296 | + ); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ( 'center' === $align_attribute ) { |
|
| 300 | + return array( |
|
| 301 | + 'class' => 'aligncenter', |
|
| 302 | + 'style' => null, |
|
| 303 | + ); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return null; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get class and style for padding from attributes. |
|
| 311 | + * |
|
| 312 | + * @param array $attributes Block attributes. |
|
| 313 | + * |
|
| 314 | + * @return (array | null) |
|
| 315 | + */ |
|
| 316 | + public static function get_padding_class_and_style( $attributes ) { |
|
| 317 | + $padding = isset( $attributes['style']['spacing']['padding'] ) ? $attributes['style']['spacing']['padding'] : null; |
|
| 318 | + |
|
| 319 | + if ( ! $padding ) { |
|
| 320 | + return null; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return array( |
|
| 324 | + 'class' => null, |
|
| 325 | + 'style' => sprintf( 'padding: %s;', implode( ' ', $padding ) ), |
|
| 326 | + ); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get classes and styles from attributes. |
|
| 331 | + * |
|
| 332 | + * @param array $attributes Block attributes. |
|
| 333 | + * @param array $properties Properties to get classes/styles from. |
|
| 334 | + * |
|
| 335 | + * @return array |
|
| 336 | + */ |
|
| 337 | + public static function get_classes_and_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 338 | + $classes_and_styles = array( |
|
| 339 | + 'line_height' => self::get_line_height_class_and_style( $attributes ), |
|
| 340 | + 'text_color' => self::get_text_color_class_and_style( $attributes ), |
|
| 341 | + 'font_size' => self::get_font_size_class_and_style( $attributes ), |
|
| 342 | + 'font_family' => self::get_font_family_class_and_style( $attributes ), |
|
| 343 | + 'link_color' => self::get_link_color_class_and_style( $attributes ), |
|
| 344 | + 'background_color' => self::get_background_color_class_and_style( $attributes ), |
|
| 345 | + 'border_color' => self::get_border_color_class_and_style( $attributes ), |
|
| 346 | + 'border_radius' => self::get_border_radius_class_and_style( $attributes ), |
|
| 347 | + 'border_width' => self::get_border_width_class_and_style( $attributes ), |
|
| 348 | + 'padding' => self::get_padding_class_and_style( $attributes ), |
|
| 349 | + ); |
|
| 350 | + |
|
| 351 | + if ( ! empty( $properties ) ) { |
|
| 352 | + foreach ( $classes_and_styles as $key => $value ) { |
|
| 353 | + if ( ! in_array( $key, $properties, true ) ) { |
|
| 354 | + unset( $classes_and_styles[ $key ] ); |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $classes_and_styles = array_filter( $classes_and_styles ); |
|
| 360 | + |
|
| 361 | + $classes = array_map( |
|
| 362 | + function( $item ) { |
|
| 363 | + return $item['class']; |
|
| 364 | + }, |
|
| 365 | + $classes_and_styles |
|
| 366 | + ); |
|
| 367 | + |
|
| 368 | + $styles = array_map( |
|
| 369 | + function( $item ) { |
|
| 370 | + return $item['style']; |
|
| 371 | + }, |
|
| 372 | + $classes_and_styles |
|
| 373 | + ); |
|
| 374 | + |
|
| 375 | + $classes = array_filter( $classes ); |
|
| 376 | + $styles = array_filter( $styles ); |
|
| 377 | + |
|
| 378 | + return array( |
|
| 379 | + 'classes' => implode( ' ', $classes ), |
|
| 380 | + 'styles' => implode( ' ', $styles ), |
|
| 381 | + ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Get space-separated classes from block attributes. |
|
| 386 | + * |
|
| 387 | + * @param array $attributes Block attributes. |
|
| 388 | + * @param array $properties Properties to get classes from. |
|
| 389 | + * |
|
| 390 | + * @return string Space-separated classes. |
|
| 391 | + */ |
|
| 392 | + public static function get_classes_by_attributes( $attributes, $properties = array() ) { |
|
| 393 | + $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 394 | + |
|
| 395 | + return $classes_and_styles['classes']; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get space-separated style rules from block attributes. |
|
| 400 | + * |
|
| 401 | + * @param array $attributes Block attributes. |
|
| 402 | + * @param array $properties Properties to get styles from. |
|
| 403 | + * |
|
| 404 | + * @return string Space-separated style rules. |
|
| 405 | + */ |
|
| 406 | + public static function get_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 407 | + $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 408 | + |
|
| 409 | + return $classes_and_styles['styles']; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get CSS value for color preset. |
|
| 414 | + * |
|
| 415 | + * @param string $preset_name Preset name. |
|
| 416 | + * |
|
| 417 | + * @return string CSS value for color preset. |
|
| 418 | + */ |
|
| 419 | + public static function get_preset_value( $preset_name ) { |
|
| 420 | + return "var(--wp--preset--color--$preset_name)"; |
|
| 421 | + } |
|
| 422 | 422 | } |
@@ -13,25 +13,25 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @return (array | null) |
| 15 | 15 | */ |
| 16 | - public static function get_font_size_class_and_style( $attributes ) { |
|
| 16 | + public static function get_font_size_class_and_style($attributes) { |
|
| 17 | 17 | |
| 18 | 18 | $font_size = $attributes['fontSize'] ?? ''; |
| 19 | 19 | |
| 20 | 20 | $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; |
| 21 | 21 | |
| 22 | - if ( ! $font_size && '' === $custom_font_size ) { |
|
| 22 | + if (!$font_size && '' === $custom_font_size) { |
|
| 23 | 23 | return null; |
| 24 | 24 | }; |
| 25 | 25 | |
| 26 | - if ( $font_size ) { |
|
| 26 | + if ($font_size) { |
|
| 27 | 27 | return array( |
| 28 | - 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), |
|
| 28 | + 'class' => sprintf('has-font-size has-%s-font-size', $font_size), |
|
| 29 | 29 | 'style' => null, |
| 30 | 30 | ); |
| 31 | - } elseif ( '' !== $custom_font_size ) { |
|
| 31 | + } elseif ('' !== $custom_font_size) { |
|
| 32 | 32 | return array( |
| 33 | 33 | 'class' => null, |
| 34 | - 'style' => sprintf( 'font-size: %s;', $custom_font_size ), |
|
| 34 | + 'style' => sprintf('font-size: %s;', $custom_font_size), |
|
| 35 | 35 | ); |
| 36 | 36 | } |
| 37 | 37 | return null; |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @return (array | null) |
| 46 | 46 | */ |
| 47 | - public static function get_font_family_class_and_style( $attributes ) { |
|
| 47 | + public static function get_font_family_class_and_style($attributes) { |
|
| 48 | 48 | |
| 49 | 49 | $font_family = $attributes['fontFamily'] ?? ''; |
| 50 | 50 | |
| 51 | - if ( $font_family ) { |
|
| 51 | + if ($font_family) { |
|
| 52 | 52 | return array( |
| 53 | - 'class' => sprintf( 'has-%s-font-family', $font_family ), |
|
| 53 | + 'class' => sprintf('has-%s-font-family', $font_family), |
|
| 54 | 54 | 'style' => null, |
| 55 | 55 | ); |
| 56 | 56 | } |
@@ -64,26 +64,26 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return (array | null) |
| 66 | 66 | */ |
| 67 | - public static function get_text_color_class_and_style( $attributes ) { |
|
| 67 | + public static function get_text_color_class_and_style($attributes) { |
|
| 68 | 68 | |
| 69 | 69 | $text_color = $attributes['textColor'] ?? ''; |
| 70 | 70 | |
| 71 | 71 | $custom_text_color = $attributes['style']['color']['text'] ?? ''; |
| 72 | 72 | |
| 73 | - if ( ! $text_color && ! $custom_text_color ) { |
|
| 73 | + if (!$text_color && !$custom_text_color) { |
|
| 74 | 74 | return null; |
| 75 | 75 | }; |
| 76 | 76 | |
| 77 | - if ( $text_color ) { |
|
| 77 | + if ($text_color) { |
|
| 78 | 78 | return array( |
| 79 | - 'class' => sprintf( 'has-text-color has-%s-color', $text_color ), |
|
| 79 | + 'class' => sprintf('has-text-color has-%s-color', $text_color), |
|
| 80 | 80 | 'style' => null, |
| 81 | - 'value' => self::get_preset_value( $text_color ), |
|
| 81 | + 'value' => self::get_preset_value($text_color), |
|
| 82 | 82 | ); |
| 83 | - } elseif ( $custom_text_color ) { |
|
| 83 | + } elseif ($custom_text_color) { |
|
| 84 | 84 | return array( |
| 85 | 85 | 'class' => null, |
| 86 | - 'style' => sprintf( 'color: %s;', $custom_text_color ), |
|
| 86 | + 'style' => sprintf('color: %s;', $custom_text_color), |
|
| 87 | 87 | 'value' => $custom_text_color, |
| 88 | 88 | ); |
| 89 | 89 | } |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return (array | null) |
| 99 | 99 | */ |
| 100 | - public static function get_link_color_class_and_style( $attributes ) { |
|
| 100 | + public static function get_link_color_class_and_style($attributes) { |
|
| 101 | 101 | |
| 102 | - if ( ! isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
|
| 102 | + if (!isset($attributes['style']['elements']['link']['color']['text'])) { |
|
| 103 | 103 | return null; |
| 104 | 104 | }; |
| 105 | 105 | |
@@ -108,19 +108,19 @@ discard block |
||
| 108 | 108 | // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. |
| 109 | 109 | // If the link color is selected from the core color picker, the value of $link_color is an hex value. |
| 110 | 110 | // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. |
| 111 | - $index_named_link_color = strrpos( $link_color, '|' ); |
|
| 111 | + $index_named_link_color = strrpos($link_color, '|'); |
|
| 112 | 112 | |
| 113 | - if ( ! empty( $index_named_link_color ) ) { |
|
| 114 | - $parsed_named_link_color = substr( $link_color, $index_named_link_color + 1 ); |
|
| 113 | + if (!empty($index_named_link_color)) { |
|
| 114 | + $parsed_named_link_color = substr($link_color, $index_named_link_color + 1); |
|
| 115 | 115 | return array( |
| 116 | 116 | 'class' => null, |
| 117 | - 'style' => sprintf( 'color: %s;', self::get_preset_value( $parsed_named_link_color ) ), |
|
| 118 | - 'value' => self::get_preset_value( $parsed_named_link_color ), |
|
| 117 | + 'style' => sprintf('color: %s;', self::get_preset_value($parsed_named_link_color)), |
|
| 118 | + 'value' => self::get_preset_value($parsed_named_link_color), |
|
| 119 | 119 | ); |
| 120 | 120 | } else { |
| 121 | 121 | return array( |
| 122 | 122 | 'class' => null, |
| 123 | - 'style' => sprintf( 'color: %s;', $link_color ), |
|
| 123 | + 'style' => sprintf('color: %s;', $link_color), |
|
| 124 | 124 | 'value' => $link_color, |
| 125 | 125 | ); |
| 126 | 126 | } |
@@ -133,17 +133,17 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return (array | null) |
| 135 | 135 | */ |
| 136 | - public static function get_line_height_class_and_style( $attributes ) { |
|
| 136 | + public static function get_line_height_class_and_style($attributes) { |
|
| 137 | 137 | |
| 138 | 138 | $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; |
| 139 | 139 | |
| 140 | - if ( ! $line_height ) { |
|
| 140 | + if (!$line_height) { |
|
| 141 | 141 | return null; |
| 142 | 142 | }; |
| 143 | 143 | |
| 144 | 144 | return array( |
| 145 | 145 | 'class' => null, |
| 146 | - 'style' => sprintf( 'line-height: %s;', $line_height ), |
|
| 146 | + 'style' => sprintf('line-height: %s;', $line_height), |
|
| 147 | 147 | ); |
| 148 | 148 | } |
| 149 | 149 | |
@@ -154,26 +154,26 @@ discard block |
||
| 154 | 154 | * |
| 155 | 155 | * @return (array | null) |
| 156 | 156 | */ |
| 157 | - public static function get_background_color_class_and_style( $attributes ) { |
|
| 157 | + public static function get_background_color_class_and_style($attributes) { |
|
| 158 | 158 | |
| 159 | 159 | $background_color = $attributes['backgroundColor'] ?? ''; |
| 160 | 160 | |
| 161 | 161 | $custom_background_color = $attributes['style']['color']['background'] ?? ''; |
| 162 | 162 | |
| 163 | - if ( ! $background_color && '' === $custom_background_color ) { |
|
| 163 | + if (!$background_color && '' === $custom_background_color) { |
|
| 164 | 164 | return null; |
| 165 | 165 | }; |
| 166 | 166 | |
| 167 | - if ( $background_color ) { |
|
| 167 | + if ($background_color) { |
|
| 168 | 168 | return array( |
| 169 | - 'class' => sprintf( 'has-background has-%s-background-color', $background_color ), |
|
| 169 | + 'class' => sprintf('has-background has-%s-background-color', $background_color), |
|
| 170 | 170 | 'style' => null, |
| 171 | - 'value' => self::get_preset_value( $background_color ), |
|
| 171 | + 'value' => self::get_preset_value($background_color), |
|
| 172 | 172 | ); |
| 173 | - } elseif ( '' !== $custom_background_color ) { |
|
| 173 | + } elseif ('' !== $custom_background_color) { |
|
| 174 | 174 | return array( |
| 175 | 175 | 'class' => null, |
| 176 | - 'style' => sprintf( 'background-color: %s;', $custom_background_color ), |
|
| 176 | + 'style' => sprintf('background-color: %s;', $custom_background_color), |
|
| 177 | 177 | 'value' => $custom_background_color, |
| 178 | 178 | ); |
| 179 | 179 | } |
@@ -187,25 +187,25 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return (array | null) |
| 189 | 189 | */ |
| 190 | - public static function get_border_color_class_and_style( $attributes ) { |
|
| 190 | + public static function get_border_color_class_and_style($attributes) { |
|
| 191 | 191 | |
| 192 | 192 | $border_color = $attributes['borderColor'] ?? ''; |
| 193 | 193 | |
| 194 | 194 | $custom_border_color = $attributes['style']['border']['color'] ?? ''; |
| 195 | 195 | |
| 196 | - if ( ! $border_color && '' === $custom_border_color ) { |
|
| 196 | + if (!$border_color && '' === $custom_border_color) { |
|
| 197 | 197 | return null; |
| 198 | 198 | }; |
| 199 | 199 | |
| 200 | - if ( $border_color ) { |
|
| 200 | + if ($border_color) { |
|
| 201 | 201 | return array( |
| 202 | - 'class' => sprintf( 'has-border-color has-%s-border-color', $border_color ), |
|
| 202 | + 'class' => sprintf('has-border-color has-%s-border-color', $border_color), |
|
| 203 | 203 | 'style' => null, |
| 204 | 204 | ); |
| 205 | - } elseif ( '' !== $custom_border_color ) { |
|
| 205 | + } elseif ('' !== $custom_border_color) { |
|
| 206 | 206 | return array( |
| 207 | 207 | 'class' => null, |
| 208 | - 'style' => sprintf( 'border-color: %s;', $custom_border_color ), |
|
| 208 | + 'style' => sprintf('border-color: %s;', $custom_border_color), |
|
| 209 | 209 | ); |
| 210 | 210 | } |
| 211 | 211 | return null; |
@@ -218,17 +218,17 @@ discard block |
||
| 218 | 218 | * |
| 219 | 219 | * @return (array | null) |
| 220 | 220 | */ |
| 221 | - public static function get_border_radius_class_and_style( $attributes ) { |
|
| 221 | + public static function get_border_radius_class_and_style($attributes) { |
|
| 222 | 222 | |
| 223 | 223 | $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; |
| 224 | 224 | |
| 225 | - if ( '' === $custom_border_radius ) { |
|
| 225 | + if ('' === $custom_border_radius) { |
|
| 226 | 226 | return null; |
| 227 | 227 | }; |
| 228 | 228 | |
| 229 | 229 | return array( |
| 230 | 230 | 'class' => null, |
| 231 | - 'style' => sprintf( 'border-radius: %s;', $custom_border_radius ), |
|
| 231 | + 'style' => sprintf('border-radius: %s;', $custom_border_radius), |
|
| 232 | 232 | ); |
| 233 | 233 | } |
| 234 | 234 | |
@@ -239,17 +239,17 @@ discard block |
||
| 239 | 239 | * |
| 240 | 240 | * @return (array | null) |
| 241 | 241 | */ |
| 242 | - public static function get_border_width_class_and_style( $attributes ) { |
|
| 242 | + public static function get_border_width_class_and_style($attributes) { |
|
| 243 | 243 | |
| 244 | 244 | $custom_border_width = $attributes['style']['border']['width'] ?? ''; |
| 245 | 245 | |
| 246 | - if ( '' === $custom_border_width ) { |
|
| 246 | + if ('' === $custom_border_width) { |
|
| 247 | 247 | return null; |
| 248 | 248 | }; |
| 249 | 249 | |
| 250 | 250 | return array( |
| 251 | 251 | 'class' => null, |
| 252 | - 'style' => sprintf( 'border-width: %s;', $custom_border_width ), |
|
| 252 | + 'style' => sprintf('border-width: %s;', $custom_border_width), |
|
| 253 | 253 | ); |
| 254 | 254 | } |
| 255 | 255 | |
@@ -260,43 +260,43 @@ discard block |
||
| 260 | 260 | * |
| 261 | 261 | * @return (array | null) |
| 262 | 262 | */ |
| 263 | - public static function get_align_class_and_style( $attributes ) { |
|
| 263 | + public static function get_align_class_and_style($attributes) { |
|
| 264 | 264 | |
| 265 | - $align_attribute = isset( $attributes['align'] ) ? $attributes['align'] : null; |
|
| 265 | + $align_attribute = isset($attributes['align']) ? $attributes['align'] : null; |
|
| 266 | 266 | |
| 267 | - if ( ! $align_attribute ) { |
|
| 267 | + if (!$align_attribute) { |
|
| 268 | 268 | return null; |
| 269 | 269 | }; |
| 270 | 270 | |
| 271 | - if ( 'wide' === $align_attribute ) { |
|
| 271 | + if ('wide' === $align_attribute) { |
|
| 272 | 272 | return array( |
| 273 | 273 | 'class' => 'alignwide', |
| 274 | 274 | 'style' => null, |
| 275 | 275 | ); |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | - if ( 'full' === $align_attribute ) { |
|
| 278 | + if ('full' === $align_attribute) { |
|
| 279 | 279 | return array( |
| 280 | 280 | 'class' => 'alignfull', |
| 281 | 281 | 'style' => null, |
| 282 | 282 | ); |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - if ( 'left' === $align_attribute ) { |
|
| 285 | + if ('left' === $align_attribute) { |
|
| 286 | 286 | return array( |
| 287 | 287 | 'class' => 'alignleft', |
| 288 | 288 | 'style' => null, |
| 289 | 289 | ); |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( 'right' === $align_attribute ) { |
|
| 292 | + if ('right' === $align_attribute) { |
|
| 293 | 293 | return array( |
| 294 | 294 | 'class' => 'alignright', |
| 295 | 295 | 'style' => null, |
| 296 | 296 | ); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - if ( 'center' === $align_attribute ) { |
|
| 299 | + if ('center' === $align_attribute) { |
|
| 300 | 300 | return array( |
| 301 | 301 | 'class' => 'aligncenter', |
| 302 | 302 | 'style' => null, |
@@ -313,16 +313,16 @@ discard block |
||
| 313 | 313 | * |
| 314 | 314 | * @return (array | null) |
| 315 | 315 | */ |
| 316 | - public static function get_padding_class_and_style( $attributes ) { |
|
| 317 | - $padding = isset( $attributes['style']['spacing']['padding'] ) ? $attributes['style']['spacing']['padding'] : null; |
|
| 316 | + public static function get_padding_class_and_style($attributes) { |
|
| 317 | + $padding = isset($attributes['style']['spacing']['padding']) ? $attributes['style']['spacing']['padding'] : null; |
|
| 318 | 318 | |
| 319 | - if ( ! $padding ) { |
|
| 319 | + if (!$padding) { |
|
| 320 | 320 | return null; |
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | return array( |
| 324 | 324 | 'class' => null, |
| 325 | - 'style' => sprintf( 'padding: %s;', implode( ' ', $padding ) ), |
|
| 325 | + 'style' => sprintf('padding: %s;', implode(' ', $padding)), |
|
| 326 | 326 | ); |
| 327 | 327 | } |
| 328 | 328 | |
@@ -334,50 +334,50 @@ discard block |
||
| 334 | 334 | * |
| 335 | 335 | * @return array |
| 336 | 336 | */ |
| 337 | - public static function get_classes_and_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 337 | + public static function get_classes_and_styles_by_attributes($attributes, $properties = array()) { |
|
| 338 | 338 | $classes_and_styles = array( |
| 339 | - 'line_height' => self::get_line_height_class_and_style( $attributes ), |
|
| 340 | - 'text_color' => self::get_text_color_class_and_style( $attributes ), |
|
| 341 | - 'font_size' => self::get_font_size_class_and_style( $attributes ), |
|
| 342 | - 'font_family' => self::get_font_family_class_and_style( $attributes ), |
|
| 343 | - 'link_color' => self::get_link_color_class_and_style( $attributes ), |
|
| 344 | - 'background_color' => self::get_background_color_class_and_style( $attributes ), |
|
| 345 | - 'border_color' => self::get_border_color_class_and_style( $attributes ), |
|
| 346 | - 'border_radius' => self::get_border_radius_class_and_style( $attributes ), |
|
| 347 | - 'border_width' => self::get_border_width_class_and_style( $attributes ), |
|
| 348 | - 'padding' => self::get_padding_class_and_style( $attributes ), |
|
| 339 | + 'line_height' => self::get_line_height_class_and_style($attributes), |
|
| 340 | + 'text_color' => self::get_text_color_class_and_style($attributes), |
|
| 341 | + 'font_size' => self::get_font_size_class_and_style($attributes), |
|
| 342 | + 'font_family' => self::get_font_family_class_and_style($attributes), |
|
| 343 | + 'link_color' => self::get_link_color_class_and_style($attributes), |
|
| 344 | + 'background_color' => self::get_background_color_class_and_style($attributes), |
|
| 345 | + 'border_color' => self::get_border_color_class_and_style($attributes), |
|
| 346 | + 'border_radius' => self::get_border_radius_class_and_style($attributes), |
|
| 347 | + 'border_width' => self::get_border_width_class_and_style($attributes), |
|
| 348 | + 'padding' => self::get_padding_class_and_style($attributes), |
|
| 349 | 349 | ); |
| 350 | 350 | |
| 351 | - if ( ! empty( $properties ) ) { |
|
| 352 | - foreach ( $classes_and_styles as $key => $value ) { |
|
| 353 | - if ( ! in_array( $key, $properties, true ) ) { |
|
| 354 | - unset( $classes_and_styles[ $key ] ); |
|
| 351 | + if (!empty($properties)) { |
|
| 352 | + foreach ($classes_and_styles as $key => $value) { |
|
| 353 | + if (!in_array($key, $properties, true)) { |
|
| 354 | + unset($classes_and_styles[$key]); |
|
| 355 | 355 | } |
| 356 | 356 | } |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - $classes_and_styles = array_filter( $classes_and_styles ); |
|
| 359 | + $classes_and_styles = array_filter($classes_and_styles); |
|
| 360 | 360 | |
| 361 | 361 | $classes = array_map( |
| 362 | - function( $item ) { |
|
| 362 | + function($item) { |
|
| 363 | 363 | return $item['class']; |
| 364 | 364 | }, |
| 365 | 365 | $classes_and_styles |
| 366 | 366 | ); |
| 367 | 367 | |
| 368 | 368 | $styles = array_map( |
| 369 | - function( $item ) { |
|
| 369 | + function($item) { |
|
| 370 | 370 | return $item['style']; |
| 371 | 371 | }, |
| 372 | 372 | $classes_and_styles |
| 373 | 373 | ); |
| 374 | 374 | |
| 375 | - $classes = array_filter( $classes ); |
|
| 376 | - $styles = array_filter( $styles ); |
|
| 375 | + $classes = array_filter($classes); |
|
| 376 | + $styles = array_filter($styles); |
|
| 377 | 377 | |
| 378 | 378 | return array( |
| 379 | - 'classes' => implode( ' ', $classes ), |
|
| 380 | - 'styles' => implode( ' ', $styles ), |
|
| 379 | + 'classes' => implode(' ', $classes), |
|
| 380 | + 'styles' => implode(' ', $styles), |
|
| 381 | 381 | ); |
| 382 | 382 | } |
| 383 | 383 | |
@@ -389,8 +389,8 @@ discard block |
||
| 389 | 389 | * |
| 390 | 390 | * @return string Space-separated classes. |
| 391 | 391 | */ |
| 392 | - public static function get_classes_by_attributes( $attributes, $properties = array() ) { |
|
| 393 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 392 | + public static function get_classes_by_attributes($attributes, $properties = array()) { |
|
| 393 | + $classes_and_styles = self::get_classes_and_styles_by_attributes($attributes, $properties); |
|
| 394 | 394 | |
| 395 | 395 | return $classes_and_styles['classes']; |
| 396 | 396 | } |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | * |
| 404 | 404 | * @return string Space-separated style rules. |
| 405 | 405 | */ |
| 406 | - public static function get_styles_by_attributes( $attributes, $properties = array() ) { |
|
| 407 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
|
| 406 | + public static function get_styles_by_attributes($attributes, $properties = array()) { |
|
| 407 | + $classes_and_styles = self::get_classes_and_styles_by_attributes($attributes, $properties); |
|
| 408 | 408 | |
| 409 | 409 | return $classes_and_styles['styles']; |
| 410 | 410 | } |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | * |
| 417 | 417 | * @return string CSS value for color preset. |
| 418 | 418 | */ |
| 419 | - public static function get_preset_value( $preset_name ) { |
|
| 419 | + public static function get_preset_value($preset_name) { |
|
| 420 | 420 | return "var(--wp--preset--color--$preset_name)"; |
| 421 | 421 | } |
| 422 | 422 | } |
@@ -10,558 +10,558 @@ |
||
| 10 | 10 | * {@internal This class and its methods should only be used within the BlockTemplateController.php and is not intended for public use.} |
| 11 | 11 | */ |
| 12 | 12 | class BlockTemplateUtils { |
| 13 | - /** |
|
| 14 | - * Directory names for block templates |
|
| 15 | - * |
|
| 16 | - * Directory names conventions for block templates have changed with Gutenberg 12.1.0, |
|
| 17 | - * however, for backwards-compatibility, we also keep the older conventions, prefixed |
|
| 18 | - * with `DEPRECATED_`. |
|
| 19 | - * |
|
| 20 | - * @var array { |
|
| 21 | - * @var string DEPRECATED_TEMPLATES Old directory name of the block templates directory. |
|
| 22 | - * @var string DEPRECATED_TEMPLATE_PARTS Old directory name of the block template parts directory. |
|
| 23 | - * @var string TEMPLATES_DIR_NAME Directory name of the block templates directory. |
|
| 24 | - * @var string TEMPLATE_PARTS_DIR_NAME Directory name of the block template parts directory. |
|
| 25 | - * } |
|
| 26 | - */ |
|
| 27 | - const DIRECTORY_NAMES = array( |
|
| 28 | - 'DEPRECATED_TEMPLATES' => 'block-templates', |
|
| 29 | - 'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts', |
|
| 30 | - 'TEMPLATES' => 'templates', |
|
| 31 | - 'TEMPLATE_PARTS' => 'parts', |
|
| 32 | - ); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * WooCommerce plugin slug |
|
| 36 | - * |
|
| 37 | - * This is used to save templates to the DB which are stored against this value in the wp_terms table. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - const PLUGIN_SLUG = 'woocommerce/woocommerce'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Deprecated WooCommerce plugin slug |
|
| 45 | - * |
|
| 46 | - * For supporting users who have customized templates under the incorrect plugin slug during the first release. |
|
| 47 | - * More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - const DEPRECATED_PLUGIN_SLUG = 'woocommerce'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Returns an array containing the references of |
|
| 55 | - * the passed blocks and their inner blocks. |
|
| 56 | - * |
|
| 57 | - * @param array $blocks array of blocks. |
|
| 58 | - * |
|
| 59 | - * @return array block references to the passed blocks and their inner blocks. |
|
| 60 | - */ |
|
| 61 | - public static function flatten_blocks( &$blocks ) { |
|
| 62 | - $all_blocks = array(); |
|
| 63 | - $queue = array(); |
|
| 64 | - foreach ( $blocks as &$block ) { |
|
| 65 | - $queue[] = &$block; |
|
| 66 | - } |
|
| 67 | - $queue_count = count( $queue ); |
|
| 68 | - |
|
| 69 | - while ( $queue_count > 0 ) { |
|
| 70 | - $block = &$queue[0]; |
|
| 71 | - array_shift( $queue ); |
|
| 72 | - $all_blocks[] = &$block; |
|
| 73 | - |
|
| 74 | - if ( ! empty( $block['innerBlocks'] ) ) { |
|
| 75 | - foreach ( $block['innerBlocks'] as &$inner_block ) { |
|
| 76 | - $queue[] = &$inner_block; |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $queue_count = count( $queue ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $all_blocks; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Parses wp_template content and injects the current theme's |
|
| 88 | - * stylesheet as a theme attribute into each wp_template_part |
|
| 89 | - * |
|
| 90 | - * @param string $template_content serialized wp_template content. |
|
| 91 | - * |
|
| 92 | - * @return string Updated wp_template content. |
|
| 93 | - */ |
|
| 94 | - public static function inject_theme_attribute_in_content( $template_content ) { |
|
| 95 | - $has_updated_content = false; |
|
| 96 | - $new_content = ''; |
|
| 97 | - $template_blocks = parse_blocks( $template_content ); |
|
| 98 | - |
|
| 99 | - $blocks = self::flatten_blocks( $template_blocks ); |
|
| 100 | - foreach ( $blocks as &$block ) { |
|
| 101 | - if ( |
|
| 102 | - 'core/template-part' === $block['blockName'] && |
|
| 103 | - ! isset( $block['attrs']['theme'] ) |
|
| 104 | - ) { |
|
| 105 | - $block['attrs']['theme'] = wp_get_theme()->get_stylesheet(); |
|
| 106 | - $has_updated_content = true; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ( $has_updated_content ) { |
|
| 111 | - foreach ( $template_blocks as &$block ) { |
|
| 112 | - $new_content .= serialize_block( $block ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $new_content; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return $template_content; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Build a unified template object based a post Object. |
|
| 123 | - * Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes. |
|
| 124 | - * |
|
| 125 | - * @param \WP_Post $post Template post. |
|
| 126 | - * |
|
| 127 | - * @return \WP_Block_Template|\WP_Error Template. |
|
| 128 | - */ |
|
| 129 | - public static function build_template_result_from_post( $post ) { |
|
| 130 | - $terms = get_the_terms( $post, 'wp_theme' ); |
|
| 131 | - |
|
| 132 | - if ( is_wp_error( $terms ) ) { |
|
| 133 | - return $terms; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if ( ! $terms ) { |
|
| 137 | - return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $theme = $terms[0]->name; |
|
| 141 | - $has_theme_file = true; |
|
| 142 | - |
|
| 143 | - $template = new \WP_Block_Template(); |
|
| 144 | - $template->wp_id = $post->ID; |
|
| 145 | - $template->id = $theme . '//' . $post->post_name; |
|
| 146 | - $template->theme = $theme; |
|
| 147 | - $template->content = $post->post_content; |
|
| 148 | - $template->slug = $post->post_name; |
|
| 149 | - $template->source = 'custom'; |
|
| 150 | - $template->type = $post->post_type; |
|
| 151 | - $template->description = $post->post_excerpt; |
|
| 152 | - $template->title = $post->post_title; |
|
| 153 | - $template->status = $post->post_status; |
|
| 154 | - $template->has_theme_file = $has_theme_file; |
|
| 155 | - $template->is_custom = false; |
|
| 156 | - $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. |
|
| 157 | - |
|
| 158 | - if ( 'wp_template_part' === $post->post_type ) { |
|
| 159 | - $type_terms = get_the_terms( $post, 'wp_template_part_area' ); |
|
| 160 | - if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { |
|
| 161 | - $template->area = $type_terms[0]->name; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // We are checking 'woocommerce' to maintain classic templates which are saved to the DB, |
|
| 166 | - // prior to updating to use the correct slug. |
|
| 167 | - // More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. |
|
| 168 | - if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) { |
|
| 169 | - $template->origin = 'plugin'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $template; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Build a unified template object based on a theme file. |
|
| 177 | - * Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes. |
|
| 178 | - * |
|
| 179 | - * @param array|object $template_file Theme file. |
|
| 180 | - * @param string $template_type wp_template or wp_template_part. |
|
| 181 | - * |
|
| 182 | - * @return \WP_Block_Template Template. |
|
| 183 | - */ |
|
| 184 | - public static function build_template_result_from_file( $template_file, $template_type ) { |
|
| 185 | - $template_file = (object) $template_file; |
|
| 186 | - |
|
| 187 | - // If the theme has an archive-products.html template but does not have product taxonomy templates |
|
| 188 | - // then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend. |
|
| 189 | - $template_is_from_theme = 'theme' === $template_file->source; |
|
| 190 | - $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 191 | - |
|
| 192 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 193 | - $template_content = file_get_contents( $template_file->path ); |
|
| 194 | - $template = new \WP_Block_Template(); |
|
| 195 | - $template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug; |
|
| 196 | - $template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG; |
|
| 197 | - $template->content = self::inject_theme_attribute_in_content( $template_content ); |
|
| 198 | - // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
|
| 199 | - $template->source = $template_file->source ? $template_file->source : 'plugin'; |
|
| 200 | - $template->slug = $template_file->slug; |
|
| 201 | - $template->type = $template_type; |
|
| 202 | - $template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug ); |
|
| 203 | - $template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug ); |
|
| 204 | - $template->status = 'publish'; |
|
| 205 | - $template->has_theme_file = true; |
|
| 206 | - $template->origin = $template_file->source; |
|
| 207 | - $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. |
|
| 208 | - $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. |
|
| 209 | - $template->area = 'uncategorized'; |
|
| 210 | - return $template; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any. |
|
| 215 | - * |
|
| 216 | - * @param string $template_file Block template file path. |
|
| 217 | - * @param string $template_type wp_template or wp_template_part. |
|
| 218 | - * @param string $template_slug Block template slug e.g. single-product. |
|
| 219 | - * @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks. |
|
| 220 | - * |
|
| 221 | - * @return object Block template object. |
|
| 222 | - */ |
|
| 223 | - public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) { |
|
| 224 | - $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 225 | - |
|
| 226 | - $new_template_item = array( |
|
| 227 | - 'slug' => $template_slug, |
|
| 228 | - 'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug, |
|
| 229 | - 'path' => $template_file, |
|
| 230 | - 'type' => $template_type, |
|
| 231 | - 'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG, |
|
| 232 | - // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
|
| 233 | - 'source' => $template_is_from_theme ? 'theme' : 'plugin', |
|
| 234 | - 'title' => self::get_block_template_title( $template_slug ), |
|
| 235 | - 'description' => self::get_block_template_description( $template_slug ), |
|
| 236 | - 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. |
|
| 237 | - ); |
|
| 238 | - |
|
| 239 | - return (object) $new_template_item; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Finds all nested template part file paths in a theme's directory. |
|
| 244 | - * |
|
| 245 | - * @param string $base_directory The theme's file path. |
|
| 246 | - * @return array $path_list A list of paths to all template part files. |
|
| 247 | - */ |
|
| 248 | - public static function get_template_paths( $base_directory ) { |
|
| 249 | - $path_list = array(); |
|
| 250 | - if ( file_exists( $base_directory ) ) { |
|
| 251 | - $nested_files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $base_directory ) ); |
|
| 252 | - $nested_html_files = new \RegexIterator( $nested_files, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH ); |
|
| 253 | - foreach ( $nested_html_files as $path => $file ) { |
|
| 254 | - $path_list[] = $path; |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - return $path_list; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Returns template titles. |
|
| 262 | - * |
|
| 263 | - * @param string $template_slug The templates slug (e.g. single-product). |
|
| 264 | - * @return string Human friendly title. |
|
| 265 | - */ |
|
| 266 | - public static function get_block_template_title( $template_slug ) { |
|
| 267 | - $plugin_template_types = self::get_plugin_block_template_types(); |
|
| 268 | - if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 269 | - return $plugin_template_types[ $template_slug ]['title']; |
|
| 270 | - } else { |
|
| 271 | - // Human friendly title converted from the slug. |
|
| 272 | - return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) ); |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Returns template descriptions. |
|
| 278 | - * |
|
| 279 | - * @param string $template_slug The templates slug (e.g. single-product). |
|
| 280 | - * @return string Template description. |
|
| 281 | - */ |
|
| 282 | - public static function get_block_template_description( $template_slug ) { |
|
| 283 | - $plugin_template_types = self::get_plugin_block_template_types(); |
|
| 284 | - if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 285 | - return $plugin_template_types[ $template_slug ]['description']; |
|
| 286 | - } |
|
| 287 | - return ''; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Returns a filtered list of plugin template types, containing their |
|
| 292 | - * localized titles and descriptions. |
|
| 293 | - * |
|
| 294 | - * @return array The plugin template types. |
|
| 295 | - */ |
|
| 296 | - public static function get_plugin_block_template_types() { |
|
| 297 | - $plugin_template_types = array( |
|
| 298 | - 'single-product' => array( |
|
| 299 | - 'title' => _x( 'Single Product', 'Template name', 'woocommerce' ), |
|
| 300 | - 'description' => __( 'Template used to display the single product.', 'woocommerce' ), |
|
| 301 | - ), |
|
| 302 | - 'archive-product' => array( |
|
| 303 | - 'title' => _x( 'Product Catalog', 'Template name', 'woocommerce' ), |
|
| 304 | - 'description' => __( 'Template used to display products.', 'woocommerce' ), |
|
| 305 | - ), |
|
| 306 | - 'taxonomy-product_cat' => array( |
|
| 307 | - 'title' => _x( 'Products by Category', 'Template name', 'woocommerce' ), |
|
| 308 | - 'description' => __( 'Template used to display products by category.', 'woocommerce' ), |
|
| 309 | - ), |
|
| 310 | - 'taxonomy-product_tag' => array( |
|
| 311 | - 'title' => _x( 'Products by Tag', 'Template name', 'woocommerce' ), |
|
| 312 | - 'description' => __( 'Template used to display products by tag.', 'woocommerce' ), |
|
| 313 | - ), |
|
| 314 | - ProductSearchResultsTemplate::SLUG => array( |
|
| 315 | - 'title' => _x( 'Product Search Results', 'Template name', 'woocommerce' ), |
|
| 316 | - 'description' => __( 'Template used to display search results for products.', 'woocommerce' ), |
|
| 317 | - ), |
|
| 318 | - MiniCartTemplate::SLUG => array( |
|
| 319 | - 'title' => _x( 'Mini Cart', 'Template name', 'woocommerce' ), |
|
| 320 | - 'description' => __( 'Template used to display the Mini Cart drawer.', 'woocommerce' ), |
|
| 321 | - ), |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - return $plugin_template_types; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Converts template paths into a slug |
|
| 329 | - * |
|
| 330 | - * @param string $path The template's path. |
|
| 331 | - * @return string slug |
|
| 332 | - */ |
|
| 333 | - public static function generate_template_slug_from_path( $path ) { |
|
| 334 | - $template_extension = '.html'; |
|
| 335 | - |
|
| 336 | - return basename( $path, $template_extension ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Gets the first matching template part within themes directories |
|
| 341 | - * |
|
| 342 | - * Since [Gutenberg 12.1.0](https://github.com/WordPress/gutenberg/releases/tag/v12.1.0), the conventions for |
|
| 343 | - * block templates and parts directory has changed from `block-templates` and `block-templates-parts` |
|
| 344 | - * to `templates` and `parts` respectively. |
|
| 345 | - * |
|
| 346 | - * This function traverses all possible combinations of directory paths where a template or part |
|
| 347 | - * could be located and returns the first one which is readable, prioritizing the new convention |
|
| 348 | - * over the deprecated one, but maintaining that one for backwards compatibility. |
|
| 349 | - * |
|
| 350 | - * @param string $template_slug The slug of the template (i.e. without the file extension). |
|
| 351 | - * @param string $template_type Either `wp_template` or `wp_template_part`. |
|
| 352 | - * |
|
| 353 | - * @return string|null The matched path or `null` if no match was found. |
|
| 354 | - */ |
|
| 355 | - public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) { |
|
| 356 | - $template_filename = $template_slug . '.html'; |
|
| 357 | - $possible_templates_dir = 'wp_template' === $template_type ? array( |
|
| 358 | - self::DIRECTORY_NAMES['TEMPLATES'], |
|
| 359 | - self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'], |
|
| 360 | - ) : array( |
|
| 361 | - self::DIRECTORY_NAMES['TEMPLATE_PARTS'], |
|
| 362 | - self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'], |
|
| 363 | - ); |
|
| 364 | - |
|
| 365 | - // Combine the possible root directory names with either the template directory |
|
| 366 | - // or the stylesheet directory for child themes. |
|
| 367 | - $possible_paths = array_reduce( |
|
| 368 | - $possible_templates_dir, |
|
| 369 | - function( $carry, $item ) use ( $template_filename ) { |
|
| 370 | - $filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename; |
|
| 371 | - |
|
| 372 | - $carry[] = get_stylesheet_directory() . $filepath; |
|
| 373 | - $carry[] = get_template_directory() . $filepath; |
|
| 374 | - |
|
| 375 | - return $carry; |
|
| 376 | - }, |
|
| 377 | - array() |
|
| 378 | - ); |
|
| 379 | - |
|
| 380 | - // Return the first matching. |
|
| 381 | - foreach ( $possible_paths as $path ) { |
|
| 382 | - if ( is_readable( $path ) ) { |
|
| 383 | - return $path; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - return null; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Check if the theme has a template. So we know if to load our own in or not. |
|
| 392 | - * |
|
| 393 | - * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
|
| 394 | - * @return boolean |
|
| 395 | - */ |
|
| 396 | - public static function theme_has_template( $template_name ) { |
|
| 397 | - return ! ! self::get_theme_template_path( $template_name, 'wp_template' ); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Check if the theme has a template. So we know if to load our own in or not. |
|
| 402 | - * |
|
| 403 | - * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
|
| 404 | - * @return boolean |
|
| 405 | - */ |
|
| 406 | - public static function theme_has_template_part( $template_name ) { |
|
| 407 | - return ! ! self::get_theme_template_path( $template_name, 'wp_template_part' ); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed. |
|
| 412 | - * |
|
| 413 | - * @return boolean |
|
| 414 | - */ |
|
| 415 | - public static function supports_block_templates() { |
|
| 416 | - if ( |
|
| 417 | - ! wc_current_theme_is_fse_theme() && |
|
| 418 | - ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) |
|
| 419 | - ) { |
|
| 420 | - return false; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - return true; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Retrieves a single unified template object using its id. |
|
| 428 | - * |
|
| 429 | - * @param string $id Template unique identifier (example: theme_slug//template_slug). |
|
| 430 | - * @param string $template_type Optional. Template type: `'wp_template'` or '`wp_template_part'`. |
|
| 431 | - * Default `'wp_template'`. |
|
| 432 | - * |
|
| 433 | - * @return WP_Block_Template|null Template. |
|
| 434 | - */ |
|
| 435 | - public static function get_block_template( $id, $template_type ) { |
|
| 436 | - if ( function_exists( 'get_block_template' ) ) { |
|
| 437 | - return get_block_template( $id, $template_type ); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - if ( function_exists( 'gutenberg_get_block_template' ) ) { |
|
| 441 | - return gutenberg_get_block_template( $id, $template_type ); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - return null; |
|
| 445 | - |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Checks if we can fallback to the `archive-product` template for a given slug |
|
| 450 | - * |
|
| 451 | - * `taxonomy-product_cat` and `taxonomy-product_tag` templates can generally use the |
|
| 452 | - * `archive-product` as a fallback if there are no specific overrides. |
|
| 453 | - * |
|
| 454 | - * @param string $template_slug Slug to check for fallbacks. |
|
| 455 | - * @return boolean |
|
| 456 | - */ |
|
| 457 | - public static function template_is_eligible_for_product_archive_fallback( $template_slug ) { |
|
| 458 | - $eligible_for_fallbacks = array( 'taxonomy-product_cat', 'taxonomy-product_tag' ); |
|
| 459 | - |
|
| 460 | - return in_array( $template_slug, $eligible_for_fallbacks, true ) |
|
| 461 | - && ! self::theme_has_template( $template_slug ) |
|
| 462 | - && self::theme_has_template( 'archive-product' ); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Sets the `has_theme_file` to `true` for templates with fallbacks |
|
| 467 | - * |
|
| 468 | - * There are cases (such as tags and categories) in which fallback templates |
|
| 469 | - * can be used; so, while *technically* the theme doesn't have a specific file |
|
| 470 | - * for them, it is important that we tell Gutenberg that we do, in fact, |
|
| 471 | - * have a theme file (i.e. the fallback one). |
|
| 472 | - * |
|
| 473 | - * **Note:** this function changes the array that has been passed. |
|
| 474 | - * |
|
| 475 | - * It returns `true` if anything was changed, `false` otherwise. |
|
| 476 | - * |
|
| 477 | - * @param array $query_result Array of template objects. |
|
| 478 | - * @param object $template A specific template object which could have a fallback. |
|
| 479 | - * |
|
| 480 | - * @return boolean |
|
| 481 | - */ |
|
| 482 | - public static function set_has_theme_file_if_fallback_is_available( $query_result, $template ) { |
|
| 483 | - foreach ( $query_result as &$query_result_template ) { |
|
| 484 | - if ( |
|
| 485 | - $query_result_template->slug === $template->slug |
|
| 486 | - && $query_result_template->theme === $template->theme |
|
| 487 | - ) { |
|
| 488 | - if ( self::template_is_eligible_for_product_archive_fallback( $template->slug ) ) { |
|
| 489 | - $query_result_template->has_theme_file = true; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - return true; |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - return false; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Filter block templates by feature flag. |
|
| 501 | - * |
|
| 502 | - * @param WP_Block_Template[] $block_templates An array of block template objects. |
|
| 503 | - * |
|
| 504 | - * @return WP_Block_Template[] An array of block template objects. |
|
| 505 | - */ |
|
| 506 | - public static function filter_block_templates_by_feature_flag( $block_templates ) { |
|
| 507 | - $feature_gating = new FeatureGating(); |
|
| 508 | - $flag = $feature_gating->get_flag(); |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * An array of block templates with slug as key and flag as value. |
|
| 512 | - * |
|
| 513 | - * @var array |
|
| 514 | - */ |
|
| 515 | - $block_templates_with_feature_gate = array(); |
|
| 516 | - |
|
| 517 | - return array_filter( |
|
| 518 | - $block_templates, |
|
| 519 | - function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) { |
|
| 520 | - if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) { |
|
| 521 | - return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag; |
|
| 522 | - } |
|
| 523 | - return true; |
|
| 524 | - } |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Removes templates that were added to a theme's block-templates directory, but already had a customised version saved in the database. |
|
| 530 | - * |
|
| 531 | - * @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on. |
|
| 532 | - * |
|
| 533 | - * @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default. |
|
| 534 | - */ |
|
| 535 | - public static function remove_theme_templates_with_custom_alternative( $templates ) { |
|
| 536 | - |
|
| 537 | - // Get the slugs of all templates that have been customised and saved in the database. |
|
| 538 | - $customised_template_slugs = array_map( |
|
| 539 | - function( $template ) { |
|
| 540 | - return $template->slug; |
|
| 541 | - }, |
|
| 542 | - array_values( |
|
| 543 | - array_filter( |
|
| 544 | - $templates, |
|
| 545 | - function( $template ) { |
|
| 546 | - // This template has been customised and saved as a post. |
|
| 547 | - return 'custom' === $template->source; |
|
| 548 | - } |
|
| 549 | - ) |
|
| 550 | - ) |
|
| 551 | - ); |
|
| 552 | - |
|
| 553 | - // Remove theme (i.e. filesystem) templates that have the same slug as a customised one. We don't need to check |
|
| 554 | - // for `woocommerce` in $template->source here because woocommerce templates won't have been added to $templates |
|
| 555 | - // if a saved version was found in the db. This only affects saved templates that were saved BEFORE a theme |
|
| 556 | - // template with the same slug was added. |
|
| 557 | - return array_values( |
|
| 558 | - array_filter( |
|
| 559 | - $templates, |
|
| 560 | - function( $template ) use ( $customised_template_slugs ) { |
|
| 561 | - // This template has been customised and saved as a post, so return it. |
|
| 562 | - return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) ); |
|
| 563 | - } |
|
| 564 | - ) |
|
| 565 | - ); |
|
| 566 | - } |
|
| 13 | + /** |
|
| 14 | + * Directory names for block templates |
|
| 15 | + * |
|
| 16 | + * Directory names conventions for block templates have changed with Gutenberg 12.1.0, |
|
| 17 | + * however, for backwards-compatibility, we also keep the older conventions, prefixed |
|
| 18 | + * with `DEPRECATED_`. |
|
| 19 | + * |
|
| 20 | + * @var array { |
|
| 21 | + * @var string DEPRECATED_TEMPLATES Old directory name of the block templates directory. |
|
| 22 | + * @var string DEPRECATED_TEMPLATE_PARTS Old directory name of the block template parts directory. |
|
| 23 | + * @var string TEMPLATES_DIR_NAME Directory name of the block templates directory. |
|
| 24 | + * @var string TEMPLATE_PARTS_DIR_NAME Directory name of the block template parts directory. |
|
| 25 | + * } |
|
| 26 | + */ |
|
| 27 | + const DIRECTORY_NAMES = array( |
|
| 28 | + 'DEPRECATED_TEMPLATES' => 'block-templates', |
|
| 29 | + 'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts', |
|
| 30 | + 'TEMPLATES' => 'templates', |
|
| 31 | + 'TEMPLATE_PARTS' => 'parts', |
|
| 32 | + ); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * WooCommerce plugin slug |
|
| 36 | + * |
|
| 37 | + * This is used to save templates to the DB which are stored against this value in the wp_terms table. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + const PLUGIN_SLUG = 'woocommerce/woocommerce'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Deprecated WooCommerce plugin slug |
|
| 45 | + * |
|
| 46 | + * For supporting users who have customized templates under the incorrect plugin slug during the first release. |
|
| 47 | + * More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + const DEPRECATED_PLUGIN_SLUG = 'woocommerce'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Returns an array containing the references of |
|
| 55 | + * the passed blocks and their inner blocks. |
|
| 56 | + * |
|
| 57 | + * @param array $blocks array of blocks. |
|
| 58 | + * |
|
| 59 | + * @return array block references to the passed blocks and their inner blocks. |
|
| 60 | + */ |
|
| 61 | + public static function flatten_blocks( &$blocks ) { |
|
| 62 | + $all_blocks = array(); |
|
| 63 | + $queue = array(); |
|
| 64 | + foreach ( $blocks as &$block ) { |
|
| 65 | + $queue[] = &$block; |
|
| 66 | + } |
|
| 67 | + $queue_count = count( $queue ); |
|
| 68 | + |
|
| 69 | + while ( $queue_count > 0 ) { |
|
| 70 | + $block = &$queue[0]; |
|
| 71 | + array_shift( $queue ); |
|
| 72 | + $all_blocks[] = &$block; |
|
| 73 | + |
|
| 74 | + if ( ! empty( $block['innerBlocks'] ) ) { |
|
| 75 | + foreach ( $block['innerBlocks'] as &$inner_block ) { |
|
| 76 | + $queue[] = &$inner_block; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $queue_count = count( $queue ); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $all_blocks; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Parses wp_template content and injects the current theme's |
|
| 88 | + * stylesheet as a theme attribute into each wp_template_part |
|
| 89 | + * |
|
| 90 | + * @param string $template_content serialized wp_template content. |
|
| 91 | + * |
|
| 92 | + * @return string Updated wp_template content. |
|
| 93 | + */ |
|
| 94 | + public static function inject_theme_attribute_in_content( $template_content ) { |
|
| 95 | + $has_updated_content = false; |
|
| 96 | + $new_content = ''; |
|
| 97 | + $template_blocks = parse_blocks( $template_content ); |
|
| 98 | + |
|
| 99 | + $blocks = self::flatten_blocks( $template_blocks ); |
|
| 100 | + foreach ( $blocks as &$block ) { |
|
| 101 | + if ( |
|
| 102 | + 'core/template-part' === $block['blockName'] && |
|
| 103 | + ! isset( $block['attrs']['theme'] ) |
|
| 104 | + ) { |
|
| 105 | + $block['attrs']['theme'] = wp_get_theme()->get_stylesheet(); |
|
| 106 | + $has_updated_content = true; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ( $has_updated_content ) { |
|
| 111 | + foreach ( $template_blocks as &$block ) { |
|
| 112 | + $new_content .= serialize_block( $block ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $new_content; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return $template_content; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Build a unified template object based a post Object. |
|
| 123 | + * Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes. |
|
| 124 | + * |
|
| 125 | + * @param \WP_Post $post Template post. |
|
| 126 | + * |
|
| 127 | + * @return \WP_Block_Template|\WP_Error Template. |
|
| 128 | + */ |
|
| 129 | + public static function build_template_result_from_post( $post ) { |
|
| 130 | + $terms = get_the_terms( $post, 'wp_theme' ); |
|
| 131 | + |
|
| 132 | + if ( is_wp_error( $terms ) ) { |
|
| 133 | + return $terms; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if ( ! $terms ) { |
|
| 137 | + return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $theme = $terms[0]->name; |
|
| 141 | + $has_theme_file = true; |
|
| 142 | + |
|
| 143 | + $template = new \WP_Block_Template(); |
|
| 144 | + $template->wp_id = $post->ID; |
|
| 145 | + $template->id = $theme . '//' . $post->post_name; |
|
| 146 | + $template->theme = $theme; |
|
| 147 | + $template->content = $post->post_content; |
|
| 148 | + $template->slug = $post->post_name; |
|
| 149 | + $template->source = 'custom'; |
|
| 150 | + $template->type = $post->post_type; |
|
| 151 | + $template->description = $post->post_excerpt; |
|
| 152 | + $template->title = $post->post_title; |
|
| 153 | + $template->status = $post->post_status; |
|
| 154 | + $template->has_theme_file = $has_theme_file; |
|
| 155 | + $template->is_custom = false; |
|
| 156 | + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. |
|
| 157 | + |
|
| 158 | + if ( 'wp_template_part' === $post->post_type ) { |
|
| 159 | + $type_terms = get_the_terms( $post, 'wp_template_part_area' ); |
|
| 160 | + if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { |
|
| 161 | + $template->area = $type_terms[0]->name; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // We are checking 'woocommerce' to maintain classic templates which are saved to the DB, |
|
| 166 | + // prior to updating to use the correct slug. |
|
| 167 | + // More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. |
|
| 168 | + if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) { |
|
| 169 | + $template->origin = 'plugin'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $template; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Build a unified template object based on a theme file. |
|
| 177 | + * Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes. |
|
| 178 | + * |
|
| 179 | + * @param array|object $template_file Theme file. |
|
| 180 | + * @param string $template_type wp_template or wp_template_part. |
|
| 181 | + * |
|
| 182 | + * @return \WP_Block_Template Template. |
|
| 183 | + */ |
|
| 184 | + public static function build_template_result_from_file( $template_file, $template_type ) { |
|
| 185 | + $template_file = (object) $template_file; |
|
| 186 | + |
|
| 187 | + // If the theme has an archive-products.html template but does not have product taxonomy templates |
|
| 188 | + // then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend. |
|
| 189 | + $template_is_from_theme = 'theme' === $template_file->source; |
|
| 190 | + $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 191 | + |
|
| 192 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 193 | + $template_content = file_get_contents( $template_file->path ); |
|
| 194 | + $template = new \WP_Block_Template(); |
|
| 195 | + $template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug; |
|
| 196 | + $template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG; |
|
| 197 | + $template->content = self::inject_theme_attribute_in_content( $template_content ); |
|
| 198 | + // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
|
| 199 | + $template->source = $template_file->source ? $template_file->source : 'plugin'; |
|
| 200 | + $template->slug = $template_file->slug; |
|
| 201 | + $template->type = $template_type; |
|
| 202 | + $template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug ); |
|
| 203 | + $template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug ); |
|
| 204 | + $template->status = 'publish'; |
|
| 205 | + $template->has_theme_file = true; |
|
| 206 | + $template->origin = $template_file->source; |
|
| 207 | + $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. |
|
| 208 | + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. |
|
| 209 | + $template->area = 'uncategorized'; |
|
| 210 | + return $template; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any. |
|
| 215 | + * |
|
| 216 | + * @param string $template_file Block template file path. |
|
| 217 | + * @param string $template_type wp_template or wp_template_part. |
|
| 218 | + * @param string $template_slug Block template slug e.g. single-product. |
|
| 219 | + * @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks. |
|
| 220 | + * |
|
| 221 | + * @return object Block template object. |
|
| 222 | + */ |
|
| 223 | + public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) { |
|
| 224 | + $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 225 | + |
|
| 226 | + $new_template_item = array( |
|
| 227 | + 'slug' => $template_slug, |
|
| 228 | + 'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug, |
|
| 229 | + 'path' => $template_file, |
|
| 230 | + 'type' => $template_type, |
|
| 231 | + 'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG, |
|
| 232 | + // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
|
| 233 | + 'source' => $template_is_from_theme ? 'theme' : 'plugin', |
|
| 234 | + 'title' => self::get_block_template_title( $template_slug ), |
|
| 235 | + 'description' => self::get_block_template_description( $template_slug ), |
|
| 236 | + 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. |
|
| 237 | + ); |
|
| 238 | + |
|
| 239 | + return (object) $new_template_item; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Finds all nested template part file paths in a theme's directory. |
|
| 244 | + * |
|
| 245 | + * @param string $base_directory The theme's file path. |
|
| 246 | + * @return array $path_list A list of paths to all template part files. |
|
| 247 | + */ |
|
| 248 | + public static function get_template_paths( $base_directory ) { |
|
| 249 | + $path_list = array(); |
|
| 250 | + if ( file_exists( $base_directory ) ) { |
|
| 251 | + $nested_files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $base_directory ) ); |
|
| 252 | + $nested_html_files = new \RegexIterator( $nested_files, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH ); |
|
| 253 | + foreach ( $nested_html_files as $path => $file ) { |
|
| 254 | + $path_list[] = $path; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + return $path_list; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Returns template titles. |
|
| 262 | + * |
|
| 263 | + * @param string $template_slug The templates slug (e.g. single-product). |
|
| 264 | + * @return string Human friendly title. |
|
| 265 | + */ |
|
| 266 | + public static function get_block_template_title( $template_slug ) { |
|
| 267 | + $plugin_template_types = self::get_plugin_block_template_types(); |
|
| 268 | + if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 269 | + return $plugin_template_types[ $template_slug ]['title']; |
|
| 270 | + } else { |
|
| 271 | + // Human friendly title converted from the slug. |
|
| 272 | + return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) ); |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Returns template descriptions. |
|
| 278 | + * |
|
| 279 | + * @param string $template_slug The templates slug (e.g. single-product). |
|
| 280 | + * @return string Template description. |
|
| 281 | + */ |
|
| 282 | + public static function get_block_template_description( $template_slug ) { |
|
| 283 | + $plugin_template_types = self::get_plugin_block_template_types(); |
|
| 284 | + if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 285 | + return $plugin_template_types[ $template_slug ]['description']; |
|
| 286 | + } |
|
| 287 | + return ''; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Returns a filtered list of plugin template types, containing their |
|
| 292 | + * localized titles and descriptions. |
|
| 293 | + * |
|
| 294 | + * @return array The plugin template types. |
|
| 295 | + */ |
|
| 296 | + public static function get_plugin_block_template_types() { |
|
| 297 | + $plugin_template_types = array( |
|
| 298 | + 'single-product' => array( |
|
| 299 | + 'title' => _x( 'Single Product', 'Template name', 'woocommerce' ), |
|
| 300 | + 'description' => __( 'Template used to display the single product.', 'woocommerce' ), |
|
| 301 | + ), |
|
| 302 | + 'archive-product' => array( |
|
| 303 | + 'title' => _x( 'Product Catalog', 'Template name', 'woocommerce' ), |
|
| 304 | + 'description' => __( 'Template used to display products.', 'woocommerce' ), |
|
| 305 | + ), |
|
| 306 | + 'taxonomy-product_cat' => array( |
|
| 307 | + 'title' => _x( 'Products by Category', 'Template name', 'woocommerce' ), |
|
| 308 | + 'description' => __( 'Template used to display products by category.', 'woocommerce' ), |
|
| 309 | + ), |
|
| 310 | + 'taxonomy-product_tag' => array( |
|
| 311 | + 'title' => _x( 'Products by Tag', 'Template name', 'woocommerce' ), |
|
| 312 | + 'description' => __( 'Template used to display products by tag.', 'woocommerce' ), |
|
| 313 | + ), |
|
| 314 | + ProductSearchResultsTemplate::SLUG => array( |
|
| 315 | + 'title' => _x( 'Product Search Results', 'Template name', 'woocommerce' ), |
|
| 316 | + 'description' => __( 'Template used to display search results for products.', 'woocommerce' ), |
|
| 317 | + ), |
|
| 318 | + MiniCartTemplate::SLUG => array( |
|
| 319 | + 'title' => _x( 'Mini Cart', 'Template name', 'woocommerce' ), |
|
| 320 | + 'description' => __( 'Template used to display the Mini Cart drawer.', 'woocommerce' ), |
|
| 321 | + ), |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + return $plugin_template_types; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Converts template paths into a slug |
|
| 329 | + * |
|
| 330 | + * @param string $path The template's path. |
|
| 331 | + * @return string slug |
|
| 332 | + */ |
|
| 333 | + public static function generate_template_slug_from_path( $path ) { |
|
| 334 | + $template_extension = '.html'; |
|
| 335 | + |
|
| 336 | + return basename( $path, $template_extension ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Gets the first matching template part within themes directories |
|
| 341 | + * |
|
| 342 | + * Since [Gutenberg 12.1.0](https://github.com/WordPress/gutenberg/releases/tag/v12.1.0), the conventions for |
|
| 343 | + * block templates and parts directory has changed from `block-templates` and `block-templates-parts` |
|
| 344 | + * to `templates` and `parts` respectively. |
|
| 345 | + * |
|
| 346 | + * This function traverses all possible combinations of directory paths where a template or part |
|
| 347 | + * could be located and returns the first one which is readable, prioritizing the new convention |
|
| 348 | + * over the deprecated one, but maintaining that one for backwards compatibility. |
|
| 349 | + * |
|
| 350 | + * @param string $template_slug The slug of the template (i.e. without the file extension). |
|
| 351 | + * @param string $template_type Either `wp_template` or `wp_template_part`. |
|
| 352 | + * |
|
| 353 | + * @return string|null The matched path or `null` if no match was found. |
|
| 354 | + */ |
|
| 355 | + public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) { |
|
| 356 | + $template_filename = $template_slug . '.html'; |
|
| 357 | + $possible_templates_dir = 'wp_template' === $template_type ? array( |
|
| 358 | + self::DIRECTORY_NAMES['TEMPLATES'], |
|
| 359 | + self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'], |
|
| 360 | + ) : array( |
|
| 361 | + self::DIRECTORY_NAMES['TEMPLATE_PARTS'], |
|
| 362 | + self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'], |
|
| 363 | + ); |
|
| 364 | + |
|
| 365 | + // Combine the possible root directory names with either the template directory |
|
| 366 | + // or the stylesheet directory for child themes. |
|
| 367 | + $possible_paths = array_reduce( |
|
| 368 | + $possible_templates_dir, |
|
| 369 | + function( $carry, $item ) use ( $template_filename ) { |
|
| 370 | + $filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename; |
|
| 371 | + |
|
| 372 | + $carry[] = get_stylesheet_directory() . $filepath; |
|
| 373 | + $carry[] = get_template_directory() . $filepath; |
|
| 374 | + |
|
| 375 | + return $carry; |
|
| 376 | + }, |
|
| 377 | + array() |
|
| 378 | + ); |
|
| 379 | + |
|
| 380 | + // Return the first matching. |
|
| 381 | + foreach ( $possible_paths as $path ) { |
|
| 382 | + if ( is_readable( $path ) ) { |
|
| 383 | + return $path; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + return null; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Check if the theme has a template. So we know if to load our own in or not. |
|
| 392 | + * |
|
| 393 | + * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
|
| 394 | + * @return boolean |
|
| 395 | + */ |
|
| 396 | + public static function theme_has_template( $template_name ) { |
|
| 397 | + return ! ! self::get_theme_template_path( $template_name, 'wp_template' ); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Check if the theme has a template. So we know if to load our own in or not. |
|
| 402 | + * |
|
| 403 | + * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
|
| 404 | + * @return boolean |
|
| 405 | + */ |
|
| 406 | + public static function theme_has_template_part( $template_name ) { |
|
| 407 | + return ! ! self::get_theme_template_path( $template_name, 'wp_template_part' ); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed. |
|
| 412 | + * |
|
| 413 | + * @return boolean |
|
| 414 | + */ |
|
| 415 | + public static function supports_block_templates() { |
|
| 416 | + if ( |
|
| 417 | + ! wc_current_theme_is_fse_theme() && |
|
| 418 | + ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) |
|
| 419 | + ) { |
|
| 420 | + return false; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + return true; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Retrieves a single unified template object using its id. |
|
| 428 | + * |
|
| 429 | + * @param string $id Template unique identifier (example: theme_slug//template_slug). |
|
| 430 | + * @param string $template_type Optional. Template type: `'wp_template'` or '`wp_template_part'`. |
|
| 431 | + * Default `'wp_template'`. |
|
| 432 | + * |
|
| 433 | + * @return WP_Block_Template|null Template. |
|
| 434 | + */ |
|
| 435 | + public static function get_block_template( $id, $template_type ) { |
|
| 436 | + if ( function_exists( 'get_block_template' ) ) { |
|
| 437 | + return get_block_template( $id, $template_type ); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + if ( function_exists( 'gutenberg_get_block_template' ) ) { |
|
| 441 | + return gutenberg_get_block_template( $id, $template_type ); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + return null; |
|
| 445 | + |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Checks if we can fallback to the `archive-product` template for a given slug |
|
| 450 | + * |
|
| 451 | + * `taxonomy-product_cat` and `taxonomy-product_tag` templates can generally use the |
|
| 452 | + * `archive-product` as a fallback if there are no specific overrides. |
|
| 453 | + * |
|
| 454 | + * @param string $template_slug Slug to check for fallbacks. |
|
| 455 | + * @return boolean |
|
| 456 | + */ |
|
| 457 | + public static function template_is_eligible_for_product_archive_fallback( $template_slug ) { |
|
| 458 | + $eligible_for_fallbacks = array( 'taxonomy-product_cat', 'taxonomy-product_tag' ); |
|
| 459 | + |
|
| 460 | + return in_array( $template_slug, $eligible_for_fallbacks, true ) |
|
| 461 | + && ! self::theme_has_template( $template_slug ) |
|
| 462 | + && self::theme_has_template( 'archive-product' ); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Sets the `has_theme_file` to `true` for templates with fallbacks |
|
| 467 | + * |
|
| 468 | + * There are cases (such as tags and categories) in which fallback templates |
|
| 469 | + * can be used; so, while *technically* the theme doesn't have a specific file |
|
| 470 | + * for them, it is important that we tell Gutenberg that we do, in fact, |
|
| 471 | + * have a theme file (i.e. the fallback one). |
|
| 472 | + * |
|
| 473 | + * **Note:** this function changes the array that has been passed. |
|
| 474 | + * |
|
| 475 | + * It returns `true` if anything was changed, `false` otherwise. |
|
| 476 | + * |
|
| 477 | + * @param array $query_result Array of template objects. |
|
| 478 | + * @param object $template A specific template object which could have a fallback. |
|
| 479 | + * |
|
| 480 | + * @return boolean |
|
| 481 | + */ |
|
| 482 | + public static function set_has_theme_file_if_fallback_is_available( $query_result, $template ) { |
|
| 483 | + foreach ( $query_result as &$query_result_template ) { |
|
| 484 | + if ( |
|
| 485 | + $query_result_template->slug === $template->slug |
|
| 486 | + && $query_result_template->theme === $template->theme |
|
| 487 | + ) { |
|
| 488 | + if ( self::template_is_eligible_for_product_archive_fallback( $template->slug ) ) { |
|
| 489 | + $query_result_template->has_theme_file = true; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + return true; |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + return false; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Filter block templates by feature flag. |
|
| 501 | + * |
|
| 502 | + * @param WP_Block_Template[] $block_templates An array of block template objects. |
|
| 503 | + * |
|
| 504 | + * @return WP_Block_Template[] An array of block template objects. |
|
| 505 | + */ |
|
| 506 | + public static function filter_block_templates_by_feature_flag( $block_templates ) { |
|
| 507 | + $feature_gating = new FeatureGating(); |
|
| 508 | + $flag = $feature_gating->get_flag(); |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * An array of block templates with slug as key and flag as value. |
|
| 512 | + * |
|
| 513 | + * @var array |
|
| 514 | + */ |
|
| 515 | + $block_templates_with_feature_gate = array(); |
|
| 516 | + |
|
| 517 | + return array_filter( |
|
| 518 | + $block_templates, |
|
| 519 | + function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) { |
|
| 520 | + if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) { |
|
| 521 | + return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag; |
|
| 522 | + } |
|
| 523 | + return true; |
|
| 524 | + } |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Removes templates that were added to a theme's block-templates directory, but already had a customised version saved in the database. |
|
| 530 | + * |
|
| 531 | + * @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on. |
|
| 532 | + * |
|
| 533 | + * @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default. |
|
| 534 | + */ |
|
| 535 | + public static function remove_theme_templates_with_custom_alternative( $templates ) { |
|
| 536 | + |
|
| 537 | + // Get the slugs of all templates that have been customised and saved in the database. |
|
| 538 | + $customised_template_slugs = array_map( |
|
| 539 | + function( $template ) { |
|
| 540 | + return $template->slug; |
|
| 541 | + }, |
|
| 542 | + array_values( |
|
| 543 | + array_filter( |
|
| 544 | + $templates, |
|
| 545 | + function( $template ) { |
|
| 546 | + // This template has been customised and saved as a post. |
|
| 547 | + return 'custom' === $template->source; |
|
| 548 | + } |
|
| 549 | + ) |
|
| 550 | + ) |
|
| 551 | + ); |
|
| 552 | + |
|
| 553 | + // Remove theme (i.e. filesystem) templates that have the same slug as a customised one. We don't need to check |
|
| 554 | + // for `woocommerce` in $template->source here because woocommerce templates won't have been added to $templates |
|
| 555 | + // if a saved version was found in the db. This only affects saved templates that were saved BEFORE a theme |
|
| 556 | + // template with the same slug was added. |
|
| 557 | + return array_values( |
|
| 558 | + array_filter( |
|
| 559 | + $templates, |
|
| 560 | + function( $template ) use ( $customised_template_slugs ) { |
|
| 561 | + // This template has been customised and saved as a post, so return it. |
|
| 562 | + return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) ); |
|
| 563 | + } |
|
| 564 | + ) |
|
| 565 | + ); |
|
| 566 | + } |
|
| 567 | 567 | } |
@@ -58,26 +58,26 @@ discard block |
||
| 58 | 58 | * |
| 59 | 59 | * @return array block references to the passed blocks and their inner blocks. |
| 60 | 60 | */ |
| 61 | - public static function flatten_blocks( &$blocks ) { |
|
| 61 | + public static function flatten_blocks(&$blocks) { |
|
| 62 | 62 | $all_blocks = array(); |
| 63 | 63 | $queue = array(); |
| 64 | - foreach ( $blocks as &$block ) { |
|
| 64 | + foreach ($blocks as &$block) { |
|
| 65 | 65 | $queue[] = &$block; |
| 66 | 66 | } |
| 67 | - $queue_count = count( $queue ); |
|
| 67 | + $queue_count = count($queue); |
|
| 68 | 68 | |
| 69 | - while ( $queue_count > 0 ) { |
|
| 69 | + while ($queue_count > 0) { |
|
| 70 | 70 | $block = &$queue[0]; |
| 71 | - array_shift( $queue ); |
|
| 71 | + array_shift($queue); |
|
| 72 | 72 | $all_blocks[] = &$block; |
| 73 | 73 | |
| 74 | - if ( ! empty( $block['innerBlocks'] ) ) { |
|
| 75 | - foreach ( $block['innerBlocks'] as &$inner_block ) { |
|
| 74 | + if (!empty($block['innerBlocks'])) { |
|
| 75 | + foreach ($block['innerBlocks'] as &$inner_block) { |
|
| 76 | 76 | $queue[] = &$inner_block; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $queue_count = count( $queue ); |
|
| 80 | + $queue_count = count($queue); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | return $all_blocks; |
@@ -91,25 +91,25 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return string Updated wp_template content. |
| 93 | 93 | */ |
| 94 | - public static function inject_theme_attribute_in_content( $template_content ) { |
|
| 94 | + public static function inject_theme_attribute_in_content($template_content) { |
|
| 95 | 95 | $has_updated_content = false; |
| 96 | 96 | $new_content = ''; |
| 97 | - $template_blocks = parse_blocks( $template_content ); |
|
| 97 | + $template_blocks = parse_blocks($template_content); |
|
| 98 | 98 | |
| 99 | - $blocks = self::flatten_blocks( $template_blocks ); |
|
| 100 | - foreach ( $blocks as &$block ) { |
|
| 99 | + $blocks = self::flatten_blocks($template_blocks); |
|
| 100 | + foreach ($blocks as &$block) { |
|
| 101 | 101 | if ( |
| 102 | 102 | 'core/template-part' === $block['blockName'] && |
| 103 | - ! isset( $block['attrs']['theme'] ) |
|
| 103 | + !isset($block['attrs']['theme']) |
|
| 104 | 104 | ) { |
| 105 | 105 | $block['attrs']['theme'] = wp_get_theme()->get_stylesheet(); |
| 106 | 106 | $has_updated_content = true; |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if ( $has_updated_content ) { |
|
| 111 | - foreach ( $template_blocks as &$block ) { |
|
| 112 | - $new_content .= serialize_block( $block ); |
|
| 110 | + if ($has_updated_content) { |
|
| 111 | + foreach ($template_blocks as &$block) { |
|
| 112 | + $new_content .= serialize_block($block); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | return $new_content; |
@@ -126,15 +126,15 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @return \WP_Block_Template|\WP_Error Template. |
| 128 | 128 | */ |
| 129 | - public static function build_template_result_from_post( $post ) { |
|
| 130 | - $terms = get_the_terms( $post, 'wp_theme' ); |
|
| 129 | + public static function build_template_result_from_post($post) { |
|
| 130 | + $terms = get_the_terms($post, 'wp_theme'); |
|
| 131 | 131 | |
| 132 | - if ( is_wp_error( $terms ) ) { |
|
| 132 | + if (is_wp_error($terms)) { |
|
| 133 | 133 | return $terms; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - if ( ! $terms ) { |
|
| 137 | - return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) ); |
|
| 136 | + if (!$terms) { |
|
| 137 | + return new \WP_Error('template_missing_theme', __('No theme is defined for this template.', 'woocommerce')); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | $theme = $terms[0]->name; |
@@ -155,9 +155,9 @@ discard block |
||
| 155 | 155 | $template->is_custom = false; |
| 156 | 156 | $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. |
| 157 | 157 | |
| 158 | - if ( 'wp_template_part' === $post->post_type ) { |
|
| 159 | - $type_terms = get_the_terms( $post, 'wp_template_part_area' ); |
|
| 160 | - if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { |
|
| 158 | + if ('wp_template_part' === $post->post_type) { |
|
| 159 | + $type_terms = get_the_terms($post, 'wp_template_part_area'); |
|
| 160 | + if (!is_wp_error($type_terms) && false !== $type_terms) { |
|
| 161 | 161 | $template->area = $type_terms[0]->name; |
| 162 | 162 | } |
| 163 | 163 | } |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | // We are checking 'woocommerce' to maintain classic templates which are saved to the DB, |
| 166 | 166 | // prior to updating to use the correct slug. |
| 167 | 167 | // More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. |
| 168 | - if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) { |
|
| 168 | + if (self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower($theme)) { |
|
| 169 | 169 | $template->origin = 'plugin'; |
| 170 | 170 | } |
| 171 | 171 | |
@@ -181,26 +181,26 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @return \WP_Block_Template Template. |
| 183 | 183 | */ |
| 184 | - public static function build_template_result_from_file( $template_file, $template_type ) { |
|
| 184 | + public static function build_template_result_from_file($template_file, $template_type) { |
|
| 185 | 185 | $template_file = (object) $template_file; |
| 186 | 186 | |
| 187 | 187 | // If the theme has an archive-products.html template but does not have product taxonomy templates |
| 188 | 188 | // then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend. |
| 189 | 189 | $template_is_from_theme = 'theme' === $template_file->source; |
| 190 | - $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 190 | + $theme_name = wp_get_theme()->get('TextDomain'); |
|
| 191 | 191 | |
| 192 | 192 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 193 | - $template_content = file_get_contents( $template_file->path ); |
|
| 193 | + $template_content = file_get_contents($template_file->path); |
|
| 194 | 194 | $template = new \WP_Block_Template(); |
| 195 | 195 | $template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug; |
| 196 | 196 | $template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG; |
| 197 | - $template->content = self::inject_theme_attribute_in_content( $template_content ); |
|
| 197 | + $template->content = self::inject_theme_attribute_in_content($template_content); |
|
| 198 | 198 | // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
| 199 | 199 | $template->source = $template_file->source ? $template_file->source : 'plugin'; |
| 200 | 200 | $template->slug = $template_file->slug; |
| 201 | 201 | $template->type = $template_type; |
| 202 | - $template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug ); |
|
| 203 | - $template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug ); |
|
| 202 | + $template->title = !empty($template_file->title) ? $template_file->title : self::get_block_template_title($template_file->slug); |
|
| 203 | + $template->description = !empty($template_file->description) ? $template_file->description : self::get_block_template_description($template_file->slug); |
|
| 204 | 204 | $template->status = 'publish'; |
| 205 | 205 | $template->has_theme_file = true; |
| 206 | 206 | $template->origin = $template_file->source; |
@@ -220,8 +220,8 @@ discard block |
||
| 220 | 220 | * |
| 221 | 221 | * @return object Block template object. |
| 222 | 222 | */ |
| 223 | - public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) { |
|
| 224 | - $theme_name = wp_get_theme()->get( 'TextDomain' ); |
|
| 223 | + public static function create_new_block_template_object($template_file, $template_type, $template_slug, $template_is_from_theme = false) { |
|
| 224 | + $theme_name = wp_get_theme()->get('TextDomain'); |
|
| 225 | 225 | |
| 226 | 226 | $new_template_item = array( |
| 227 | 227 | 'slug' => $template_slug, |
@@ -231,8 +231,8 @@ discard block |
||
| 231 | 231 | 'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG, |
| 232 | 232 | // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. |
| 233 | 233 | 'source' => $template_is_from_theme ? 'theme' : 'plugin', |
| 234 | - 'title' => self::get_block_template_title( $template_slug ), |
|
| 235 | - 'description' => self::get_block_template_description( $template_slug ), |
|
| 234 | + 'title' => self::get_block_template_title($template_slug), |
|
| 235 | + 'description' => self::get_block_template_description($template_slug), |
|
| 236 | 236 | 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. |
| 237 | 237 | ); |
| 238 | 238 | |
@@ -245,12 +245,12 @@ discard block |
||
| 245 | 245 | * @param string $base_directory The theme's file path. |
| 246 | 246 | * @return array $path_list A list of paths to all template part files. |
| 247 | 247 | */ |
| 248 | - public static function get_template_paths( $base_directory ) { |
|
| 248 | + public static function get_template_paths($base_directory) { |
|
| 249 | 249 | $path_list = array(); |
| 250 | - if ( file_exists( $base_directory ) ) { |
|
| 251 | - $nested_files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $base_directory ) ); |
|
| 252 | - $nested_html_files = new \RegexIterator( $nested_files, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH ); |
|
| 253 | - foreach ( $nested_html_files as $path => $file ) { |
|
| 250 | + if (file_exists($base_directory)) { |
|
| 251 | + $nested_files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($base_directory)); |
|
| 252 | + $nested_html_files = new \RegexIterator($nested_files, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH); |
|
| 253 | + foreach ($nested_html_files as $path => $file) { |
|
| 254 | 254 | $path_list[] = $path; |
| 255 | 255 | } |
| 256 | 256 | } |
@@ -263,13 +263,13 @@ discard block |
||
| 263 | 263 | * @param string $template_slug The templates slug (e.g. single-product). |
| 264 | 264 | * @return string Human friendly title. |
| 265 | 265 | */ |
| 266 | - public static function get_block_template_title( $template_slug ) { |
|
| 266 | + public static function get_block_template_title($template_slug) { |
|
| 267 | 267 | $plugin_template_types = self::get_plugin_block_template_types(); |
| 268 | - if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 269 | - return $plugin_template_types[ $template_slug ]['title']; |
|
| 268 | + if (isset($plugin_template_types[$template_slug])) { |
|
| 269 | + return $plugin_template_types[$template_slug]['title']; |
|
| 270 | 270 | } else { |
| 271 | 271 | // Human friendly title converted from the slug. |
| 272 | - return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) ); |
|
| 272 | + return ucwords(preg_replace('/[\-_]/', ' ', $template_slug)); |
|
| 273 | 273 | } |
| 274 | 274 | } |
| 275 | 275 | |
@@ -279,10 +279,10 @@ discard block |
||
| 279 | 279 | * @param string $template_slug The templates slug (e.g. single-product). |
| 280 | 280 | * @return string Template description. |
| 281 | 281 | */ |
| 282 | - public static function get_block_template_description( $template_slug ) { |
|
| 282 | + public static function get_block_template_description($template_slug) { |
|
| 283 | 283 | $plugin_template_types = self::get_plugin_block_template_types(); |
| 284 | - if ( isset( $plugin_template_types[ $template_slug ] ) ) { |
|
| 285 | - return $plugin_template_types[ $template_slug ]['description']; |
|
| 284 | + if (isset($plugin_template_types[$template_slug])) { |
|
| 285 | + return $plugin_template_types[$template_slug]['description']; |
|
| 286 | 286 | } |
| 287 | 287 | return ''; |
| 288 | 288 | } |
@@ -296,28 +296,28 @@ discard block |
||
| 296 | 296 | public static function get_plugin_block_template_types() { |
| 297 | 297 | $plugin_template_types = array( |
| 298 | 298 | 'single-product' => array( |
| 299 | - 'title' => _x( 'Single Product', 'Template name', 'woocommerce' ), |
|
| 300 | - 'description' => __( 'Template used to display the single product.', 'woocommerce' ), |
|
| 299 | + 'title' => _x('Single Product', 'Template name', 'woocommerce'), |
|
| 300 | + 'description' => __('Template used to display the single product.', 'woocommerce'), |
|
| 301 | 301 | ), |
| 302 | 302 | 'archive-product' => array( |
| 303 | - 'title' => _x( 'Product Catalog', 'Template name', 'woocommerce' ), |
|
| 304 | - 'description' => __( 'Template used to display products.', 'woocommerce' ), |
|
| 303 | + 'title' => _x('Product Catalog', 'Template name', 'woocommerce'), |
|
| 304 | + 'description' => __('Template used to display products.', 'woocommerce'), |
|
| 305 | 305 | ), |
| 306 | 306 | 'taxonomy-product_cat' => array( |
| 307 | - 'title' => _x( 'Products by Category', 'Template name', 'woocommerce' ), |
|
| 308 | - 'description' => __( 'Template used to display products by category.', 'woocommerce' ), |
|
| 307 | + 'title' => _x('Products by Category', 'Template name', 'woocommerce'), |
|
| 308 | + 'description' => __('Template used to display products by category.', 'woocommerce'), |
|
| 309 | 309 | ), |
| 310 | 310 | 'taxonomy-product_tag' => array( |
| 311 | - 'title' => _x( 'Products by Tag', 'Template name', 'woocommerce' ), |
|
| 312 | - 'description' => __( 'Template used to display products by tag.', 'woocommerce' ), |
|
| 311 | + 'title' => _x('Products by Tag', 'Template name', 'woocommerce'), |
|
| 312 | + 'description' => __('Template used to display products by tag.', 'woocommerce'), |
|
| 313 | 313 | ), |
| 314 | 314 | ProductSearchResultsTemplate::SLUG => array( |
| 315 | - 'title' => _x( 'Product Search Results', 'Template name', 'woocommerce' ), |
|
| 316 | - 'description' => __( 'Template used to display search results for products.', 'woocommerce' ), |
|
| 315 | + 'title' => _x('Product Search Results', 'Template name', 'woocommerce'), |
|
| 316 | + 'description' => __('Template used to display search results for products.', 'woocommerce'), |
|
| 317 | 317 | ), |
| 318 | 318 | MiniCartTemplate::SLUG => array( |
| 319 | - 'title' => _x( 'Mini Cart', 'Template name', 'woocommerce' ), |
|
| 320 | - 'description' => __( 'Template used to display the Mini Cart drawer.', 'woocommerce' ), |
|
| 319 | + 'title' => _x('Mini Cart', 'Template name', 'woocommerce'), |
|
| 320 | + 'description' => __('Template used to display the Mini Cart drawer.', 'woocommerce'), |
|
| 321 | 321 | ), |
| 322 | 322 | ); |
| 323 | 323 | |
@@ -330,10 +330,10 @@ discard block |
||
| 330 | 330 | * @param string $path The template's path. |
| 331 | 331 | * @return string slug |
| 332 | 332 | */ |
| 333 | - public static function generate_template_slug_from_path( $path ) { |
|
| 333 | + public static function generate_template_slug_from_path($path) { |
|
| 334 | 334 | $template_extension = '.html'; |
| 335 | 335 | |
| 336 | - return basename( $path, $template_extension ); |
|
| 336 | + return basename($path, $template_extension); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * |
| 353 | 353 | * @return string|null The matched path or `null` if no match was found. |
| 354 | 354 | */ |
| 355 | - public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) { |
|
| 355 | + public static function get_theme_template_path($template_slug, $template_type = 'wp_template') { |
|
| 356 | 356 | $template_filename = $template_slug . '.html'; |
| 357 | 357 | $possible_templates_dir = 'wp_template' === $template_type ? array( |
| 358 | 358 | self::DIRECTORY_NAMES['TEMPLATES'], |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | // or the stylesheet directory for child themes. |
| 367 | 367 | $possible_paths = array_reduce( |
| 368 | 368 | $possible_templates_dir, |
| 369 | - function( $carry, $item ) use ( $template_filename ) { |
|
| 369 | + function($carry, $item) use ($template_filename) { |
|
| 370 | 370 | $filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename; |
| 371 | 371 | |
| 372 | 372 | $carry[] = get_stylesheet_directory() . $filepath; |
@@ -378,8 +378,8 @@ discard block |
||
| 378 | 378 | ); |
| 379 | 379 | |
| 380 | 380 | // Return the first matching. |
| 381 | - foreach ( $possible_paths as $path ) { |
|
| 382 | - if ( is_readable( $path ) ) { |
|
| 381 | + foreach ($possible_paths as $path) { |
|
| 382 | + if (is_readable($path)) { |
|
| 383 | 383 | return $path; |
| 384 | 384 | } |
| 385 | 385 | } |
@@ -393,8 +393,8 @@ discard block |
||
| 393 | 393 | * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
| 394 | 394 | * @return boolean |
| 395 | 395 | */ |
| 396 | - public static function theme_has_template( $template_name ) { |
|
| 397 | - return ! ! self::get_theme_template_path( $template_name, 'wp_template' ); |
|
| 396 | + public static function theme_has_template($template_name) { |
|
| 397 | + return !!self::get_theme_template_path($template_name, 'wp_template'); |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | /** |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | * @param string $template_name name of the template file without .html extension e.g. 'single-product'. |
| 404 | 404 | * @return boolean |
| 405 | 405 | */ |
| 406 | - public static function theme_has_template_part( $template_name ) { |
|
| 407 | - return ! ! self::get_theme_template_path( $template_name, 'wp_template_part' ); |
|
| 406 | + public static function theme_has_template_part($template_name) { |
|
| 407 | + return !!self::get_theme_template_path($template_name, 'wp_template_part'); |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -414,8 +414,8 @@ discard block |
||
| 414 | 414 | */ |
| 415 | 415 | public static function supports_block_templates() { |
| 416 | 416 | if ( |
| 417 | - ! wc_current_theme_is_fse_theme() && |
|
| 418 | - ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) |
|
| 417 | + !wc_current_theme_is_fse_theme() && |
|
| 418 | + (!function_exists('gutenberg_supports_block_templates') || !gutenberg_supports_block_templates()) |
|
| 419 | 419 | ) { |
| 420 | 420 | return false; |
| 421 | 421 | } |
@@ -432,13 +432,13 @@ discard block |
||
| 432 | 432 | * |
| 433 | 433 | * @return WP_Block_Template|null Template. |
| 434 | 434 | */ |
| 435 | - public static function get_block_template( $id, $template_type ) { |
|
| 436 | - if ( function_exists( 'get_block_template' ) ) { |
|
| 437 | - return get_block_template( $id, $template_type ); |
|
| 435 | + public static function get_block_template($id, $template_type) { |
|
| 436 | + if (function_exists('get_block_template')) { |
|
| 437 | + return get_block_template($id, $template_type); |
|
| 438 | 438 | } |
| 439 | 439 | |
| 440 | - if ( function_exists( 'gutenberg_get_block_template' ) ) { |
|
| 441 | - return gutenberg_get_block_template( $id, $template_type ); |
|
| 440 | + if (function_exists('gutenberg_get_block_template')) { |
|
| 441 | + return gutenberg_get_block_template($id, $template_type); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | return null; |
@@ -454,12 +454,12 @@ discard block |
||
| 454 | 454 | * @param string $template_slug Slug to check for fallbacks. |
| 455 | 455 | * @return boolean |
| 456 | 456 | */ |
| 457 | - public static function template_is_eligible_for_product_archive_fallback( $template_slug ) { |
|
| 458 | - $eligible_for_fallbacks = array( 'taxonomy-product_cat', 'taxonomy-product_tag' ); |
|
| 457 | + public static function template_is_eligible_for_product_archive_fallback($template_slug) { |
|
| 458 | + $eligible_for_fallbacks = array('taxonomy-product_cat', 'taxonomy-product_tag'); |
|
| 459 | 459 | |
| 460 | - return in_array( $template_slug, $eligible_for_fallbacks, true ) |
|
| 461 | - && ! self::theme_has_template( $template_slug ) |
|
| 462 | - && self::theme_has_template( 'archive-product' ); |
|
| 460 | + return in_array($template_slug, $eligible_for_fallbacks, true) |
|
| 461 | + && !self::theme_has_template($template_slug) |
|
| 462 | + && self::theme_has_template('archive-product'); |
|
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | /** |
@@ -479,13 +479,13 @@ discard block |
||
| 479 | 479 | * |
| 480 | 480 | * @return boolean |
| 481 | 481 | */ |
| 482 | - public static function set_has_theme_file_if_fallback_is_available( $query_result, $template ) { |
|
| 483 | - foreach ( $query_result as &$query_result_template ) { |
|
| 482 | + public static function set_has_theme_file_if_fallback_is_available($query_result, $template) { |
|
| 483 | + foreach ($query_result as &$query_result_template) { |
|
| 484 | 484 | if ( |
| 485 | 485 | $query_result_template->slug === $template->slug |
| 486 | 486 | && $query_result_template->theme === $template->theme |
| 487 | 487 | ) { |
| 488 | - if ( self::template_is_eligible_for_product_archive_fallback( $template->slug ) ) { |
|
| 488 | + if (self::template_is_eligible_for_product_archive_fallback($template->slug)) { |
|
| 489 | 489 | $query_result_template->has_theme_file = true; |
| 490 | 490 | } |
| 491 | 491 | |
@@ -503,7 +503,7 @@ discard block |
||
| 503 | 503 | * |
| 504 | 504 | * @return WP_Block_Template[] An array of block template objects. |
| 505 | 505 | */ |
| 506 | - public static function filter_block_templates_by_feature_flag( $block_templates ) { |
|
| 506 | + public static function filter_block_templates_by_feature_flag($block_templates) { |
|
| 507 | 507 | $feature_gating = new FeatureGating(); |
| 508 | 508 | $flag = $feature_gating->get_flag(); |
| 509 | 509 | |
@@ -516,9 +516,9 @@ discard block |
||
| 516 | 516 | |
| 517 | 517 | return array_filter( |
| 518 | 518 | $block_templates, |
| 519 | - function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) { |
|
| 520 | - if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) { |
|
| 521 | - return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag; |
|
| 519 | + function($block_template) use ($flag, $block_templates_with_feature_gate) { |
|
| 520 | + if (isset($block_templates_with_feature_gate[$block_template->slug])) { |
|
| 521 | + return $block_templates_with_feature_gate[$block_template->slug] <= $flag; |
|
| 522 | 522 | } |
| 523 | 523 | return true; |
| 524 | 524 | } |
@@ -532,17 +532,17 @@ discard block |
||
| 532 | 532 | * |
| 533 | 533 | * @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default. |
| 534 | 534 | */ |
| 535 | - public static function remove_theme_templates_with_custom_alternative( $templates ) { |
|
| 535 | + public static function remove_theme_templates_with_custom_alternative($templates) { |
|
| 536 | 536 | |
| 537 | 537 | // Get the slugs of all templates that have been customised and saved in the database. |
| 538 | 538 | $customised_template_slugs = array_map( |
| 539 | - function( $template ) { |
|
| 539 | + function($template) { |
|
| 540 | 540 | return $template->slug; |
| 541 | 541 | }, |
| 542 | 542 | array_values( |
| 543 | 543 | array_filter( |
| 544 | 544 | $templates, |
| 545 | - function( $template ) { |
|
| 545 | + function($template) { |
|
| 546 | 546 | // This template has been customised and saved as a post. |
| 547 | 547 | return 'custom' === $template->source; |
| 548 | 548 | } |
@@ -557,9 +557,9 @@ discard block |
||
| 557 | 557 | return array_values( |
| 558 | 558 | array_filter( |
| 559 | 559 | $templates, |
| 560 | - function( $template ) use ( $customised_template_slugs ) { |
|
| 560 | + function($template) use ($customised_template_slugs) { |
|
| 561 | 561 | // This template has been customised and saved as a post, so return it. |
| 562 | - return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) ); |
|
| 562 | + return !('theme' === $template->source && in_array($template->slug, $customised_template_slugs, true)); |
|
| 563 | 563 | } |
| 564 | 564 | ) |
| 565 | 565 | ); |
@@ -21,109 +21,109 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Package { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * For back compat this is provided. Ideally, you should register your |
|
| 26 | - * class with Automattic\Woocommerce\Blocks\Container and make Package a |
|
| 27 | - * dependency. |
|
| 28 | - * |
|
| 29 | - * @since 2.5.0 |
|
| 30 | - * @return Package The Package instance class |
|
| 31 | - */ |
|
| 32 | - protected static function get_package() { |
|
| 33 | - return self::container()->get( NewPackage::class ); |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * For back compat this is provided. Ideally, you should register your |
|
| 26 | + * class with Automattic\Woocommerce\Blocks\Container and make Package a |
|
| 27 | + * dependency. |
|
| 28 | + * |
|
| 29 | + * @since 2.5.0 |
|
| 30 | + * @return Package The Package instance class |
|
| 31 | + */ |
|
| 32 | + protected static function get_package() { |
|
| 33 | + return self::container()->get( NewPackage::class ); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Init the package - load the blocks library and define constants. |
|
| 38 | - * |
|
| 39 | - * @since 2.5.0 Handled by new NewPackage. |
|
| 40 | - */ |
|
| 41 | - public static function init() { |
|
| 42 | - self::container()->get( Bootstrap::class ); |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * Init the package - load the blocks library and define constants. |
|
| 38 | + * |
|
| 39 | + * @since 2.5.0 Handled by new NewPackage. |
|
| 40 | + */ |
|
| 41 | + public static function init() { |
|
| 42 | + self::container()->get( Bootstrap::class ); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Return the version of the package. |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public static function get_version() { |
|
| 51 | - return self::get_package()->get_version(); |
|
| 52 | - } |
|
| 45 | + /** |
|
| 46 | + * Return the version of the package. |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public static function get_version() { |
|
| 51 | + return self::get_package()->get_version(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Return the path to the package. |
|
| 56 | - * |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - public static function get_path() { |
|
| 60 | - return self::get_package()->get_path(); |
|
| 61 | - } |
|
| 54 | + /** |
|
| 55 | + * Return the path to the package. |
|
| 56 | + * |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + public static function get_path() { |
|
| 60 | + return self::get_package()->get_path(); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Returns an instance of the the FeatureGating class. |
|
| 65 | - * |
|
| 66 | - * @return FeatureGating |
|
| 67 | - */ |
|
| 68 | - public static function feature() { |
|
| 69 | - return self::get_package()->feature(); |
|
| 70 | - } |
|
| 63 | + /** |
|
| 64 | + * Returns an instance of the the FeatureGating class. |
|
| 65 | + * |
|
| 66 | + * @return FeatureGating |
|
| 67 | + */ |
|
| 68 | + public static function feature() { |
|
| 69 | + return self::get_package()->feature(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Checks if we're executing the code in an experimental build mode. |
|
| 74 | - * |
|
| 75 | - * @return boolean |
|
| 76 | - */ |
|
| 77 | - public static function is_experimental_build() { |
|
| 78 | - return self::get_package()->is_experimental_build(); |
|
| 79 | - } |
|
| 72 | + /** |
|
| 73 | + * Checks if we're executing the code in an experimental build mode. |
|
| 74 | + * |
|
| 75 | + * @return boolean |
|
| 76 | + */ |
|
| 77 | + public static function is_experimental_build() { |
|
| 78 | + return self::get_package()->is_experimental_build(); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Checks if we're executing the code in an feature plugin or experimental build mode. |
|
| 83 | - * |
|
| 84 | - * @return boolean |
|
| 85 | - */ |
|
| 86 | - public static function is_feature_plugin_build() { |
|
| 87 | - return self::get_package()->is_feature_plugin_build(); |
|
| 88 | - } |
|
| 81 | + /** |
|
| 82 | + * Checks if we're executing the code in an feature plugin or experimental build mode. |
|
| 83 | + * |
|
| 84 | + * @return boolean |
|
| 85 | + */ |
|
| 86 | + public static function is_feature_plugin_build() { |
|
| 87 | + return self::get_package()->is_feature_plugin_build(); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Loads the dependency injection container for woocommerce blocks. |
|
| 92 | - * |
|
| 93 | - * @param boolean $reset Used to reset the container to a fresh instance. |
|
| 94 | - * Note: this means all dependencies will be |
|
| 95 | - * reconstructed. |
|
| 96 | - */ |
|
| 97 | - public static function container( $reset = false ) { |
|
| 98 | - static $container; |
|
| 99 | - if ( |
|
| 100 | - ! $container instanceof Container |
|
| 101 | - || $reset |
|
| 102 | - ) { |
|
| 103 | - $container = new Container(); |
|
| 104 | - // register Package. |
|
| 105 | - $container->register( |
|
| 106 | - NewPackage::class, |
|
| 107 | - function ( $container ) { |
|
| 108 | - // leave for automated version bumping. |
|
| 109 | - $version = '8.0.0'; |
|
| 110 | - return new NewPackage( |
|
| 111 | - $version, |
|
| 112 | - dirname( __DIR__ ), |
|
| 113 | - new FeatureGating() |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - ); |
|
| 117 | - // register Bootstrap. |
|
| 118 | - $container->register( |
|
| 119 | - Bootstrap::class, |
|
| 120 | - function ( $container ) { |
|
| 121 | - return new Bootstrap( |
|
| 122 | - $container |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - ); |
|
| 126 | - } |
|
| 127 | - return $container; |
|
| 128 | - } |
|
| 90 | + /** |
|
| 91 | + * Loads the dependency injection container for woocommerce blocks. |
|
| 92 | + * |
|
| 93 | + * @param boolean $reset Used to reset the container to a fresh instance. |
|
| 94 | + * Note: this means all dependencies will be |
|
| 95 | + * reconstructed. |
|
| 96 | + */ |
|
| 97 | + public static function container( $reset = false ) { |
|
| 98 | + static $container; |
|
| 99 | + if ( |
|
| 100 | + ! $container instanceof Container |
|
| 101 | + || $reset |
|
| 102 | + ) { |
|
| 103 | + $container = new Container(); |
|
| 104 | + // register Package. |
|
| 105 | + $container->register( |
|
| 106 | + NewPackage::class, |
|
| 107 | + function ( $container ) { |
|
| 108 | + // leave for automated version bumping. |
|
| 109 | + $version = '8.0.0'; |
|
| 110 | + return new NewPackage( |
|
| 111 | + $version, |
|
| 112 | + dirname( __DIR__ ), |
|
| 113 | + new FeatureGating() |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + ); |
|
| 117 | + // register Bootstrap. |
|
| 118 | + $container->register( |
|
| 119 | + Bootstrap::class, |
|
| 120 | + function ( $container ) { |
|
| 121 | + return new Bootstrap( |
|
| 122 | + $container |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + ); |
|
| 126 | + } |
|
| 127 | + return $container; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @return Package The Package instance class |
| 31 | 31 | */ |
| 32 | 32 | protected static function get_package() { |
| 33 | - return self::container()->get( NewPackage::class ); |
|
| 33 | + return self::container()->get(NewPackage::class); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @since 2.5.0 Handled by new NewPackage. |
| 40 | 40 | */ |
| 41 | 41 | public static function init() { |
| 42 | - self::container()->get( Bootstrap::class ); |
|
| 42 | + self::container()->get(Bootstrap::class); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -94,22 +94,22 @@ discard block |
||
| 94 | 94 | * Note: this means all dependencies will be |
| 95 | 95 | * reconstructed. |
| 96 | 96 | */ |
| 97 | - public static function container( $reset = false ) { |
|
| 97 | + public static function container($reset = false) { |
|
| 98 | 98 | static $container; |
| 99 | 99 | if ( |
| 100 | - ! $container instanceof Container |
|
| 100 | + !$container instanceof Container |
|
| 101 | 101 | || $reset |
| 102 | 102 | ) { |
| 103 | 103 | $container = new Container(); |
| 104 | 104 | // register Package. |
| 105 | 105 | $container->register( |
| 106 | 106 | NewPackage::class, |
| 107 | - function ( $container ) { |
|
| 107 | + function($container) { |
|
| 108 | 108 | // leave for automated version bumping. |
| 109 | 109 | $version = '8.0.0'; |
| 110 | 110 | return new NewPackage( |
| 111 | 111 | $version, |
| 112 | - dirname( __DIR__ ), |
|
| 112 | + dirname(__DIR__), |
|
| 113 | 113 | new FeatureGating() |
| 114 | 114 | ); |
| 115 | 115 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | // register Bootstrap. |
| 118 | 118 | $container->register( |
| 119 | 119 | Bootstrap::class, |
| 120 | - function ( $container ) { |
|
| 120 | + function($container) { |
|
| 121 | 121 | return new Bootstrap( |
| 122 | 122 | $container |
| 123 | 123 | ); |
@@ -13,275 +13,275 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | final class AssetsController { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Asset API interface for various asset registration. |
|
| 18 | - * |
|
| 19 | - * @var AssetApi |
|
| 20 | - */ |
|
| 21 | - private $api; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - * |
|
| 26 | - * @param AssetApi $asset_api Asset API interface for various asset registration. |
|
| 27 | - */ |
|
| 28 | - public function __construct( AssetApi $asset_api ) { |
|
| 29 | - $this->api = $asset_api; |
|
| 30 | - $this->init(); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Initialize class features. |
|
| 35 | - */ |
|
| 36 | - protected function init() { |
|
| 37 | - add_action( 'init', array( $this, 'register_assets' ) ); |
|
| 38 | - add_filter( 'wp_resource_hints', array( $this, 'add_resource_hints' ), 10, 2 ); |
|
| 39 | - add_action( 'body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 40 | - add_action( 'admin_body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 41 | - add_action( 'admin_enqueue_scripts', array( $this, 'update_block_style_dependencies' ), 20 ); |
|
| 42 | - add_action( 'wp_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 43 | - add_action( 'admin_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Register block scripts & styles. |
|
| 48 | - */ |
|
| 49 | - public function register_assets() { |
|
| 50 | - $this->register_style( 'wc-blocks-vendors-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-vendors-style', 'css' ), __DIR__ ) ); |
|
| 51 | - $this->register_style( 'wc-blocks-editor-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-editor-style', 'css' ), __DIR__ ), [ 'wp-edit-blocks' ], 'all', true ); |
|
| 52 | - $this->register_style( 'wc-blocks-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-style', 'css' ), __DIR__ ), [ 'wc-blocks-vendors-style' ], 'all', true ); |
|
| 53 | - |
|
| 54 | - $this->api->register_script( 'wc-blocks-middleware', 'build/wc-blocks-middleware.js', [], false ); |
|
| 55 | - $this->api->register_script( 'wc-blocks-data-store', 'build/wc-blocks-data.js', [ 'wc-blocks-middleware' ] ); |
|
| 56 | - $this->api->register_script( 'wc-blocks-vendors', $this->api->get_block_asset_build_path( 'wc-blocks-vendors' ), [], false ); |
|
| 57 | - $this->api->register_script( 'wc-blocks-registry', 'build/wc-blocks-registry.js', [], false ); |
|
| 58 | - $this->api->register_script( 'wc-blocks', $this->api->get_block_asset_build_path( 'wc-blocks' ), [ 'wc-blocks-vendors' ], false ); |
|
| 59 | - $this->api->register_script( 'wc-blocks-shared-context', 'build/wc-blocks-shared-context.js', [] ); |
|
| 60 | - $this->api->register_script( 'wc-blocks-shared-hocs', 'build/wc-blocks-shared-hocs.js', [], false ); |
|
| 61 | - |
|
| 62 | - // The price package is shared externally so has no blocks prefix. |
|
| 63 | - $this->api->register_script( 'wc-price-format', 'build/price-format.js', [], false ); |
|
| 64 | - |
|
| 65 | - $this->api->register_script( 'wc-blocks-checkout', 'build/blocks-checkout.js', [] ); |
|
| 66 | - |
|
| 67 | - wp_add_inline_script( |
|
| 68 | - 'wc-blocks-middleware', |
|
| 69 | - " |
|
| 16 | + /** |
|
| 17 | + * Asset API interface for various asset registration. |
|
| 18 | + * |
|
| 19 | + * @var AssetApi |
|
| 20 | + */ |
|
| 21 | + private $api; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + * |
|
| 26 | + * @param AssetApi $asset_api Asset API interface for various asset registration. |
|
| 27 | + */ |
|
| 28 | + public function __construct( AssetApi $asset_api ) { |
|
| 29 | + $this->api = $asset_api; |
|
| 30 | + $this->init(); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Initialize class features. |
|
| 35 | + */ |
|
| 36 | + protected function init() { |
|
| 37 | + add_action( 'init', array( $this, 'register_assets' ) ); |
|
| 38 | + add_filter( 'wp_resource_hints', array( $this, 'add_resource_hints' ), 10, 2 ); |
|
| 39 | + add_action( 'body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 40 | + add_action( 'admin_body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 41 | + add_action( 'admin_enqueue_scripts', array( $this, 'update_block_style_dependencies' ), 20 ); |
|
| 42 | + add_action( 'wp_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 43 | + add_action( 'admin_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Register block scripts & styles. |
|
| 48 | + */ |
|
| 49 | + public function register_assets() { |
|
| 50 | + $this->register_style( 'wc-blocks-vendors-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-vendors-style', 'css' ), __DIR__ ) ); |
|
| 51 | + $this->register_style( 'wc-blocks-editor-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-editor-style', 'css' ), __DIR__ ), [ 'wp-edit-blocks' ], 'all', true ); |
|
| 52 | + $this->register_style( 'wc-blocks-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-style', 'css' ), __DIR__ ), [ 'wc-blocks-vendors-style' ], 'all', true ); |
|
| 53 | + |
|
| 54 | + $this->api->register_script( 'wc-blocks-middleware', 'build/wc-blocks-middleware.js', [], false ); |
|
| 55 | + $this->api->register_script( 'wc-blocks-data-store', 'build/wc-blocks-data.js', [ 'wc-blocks-middleware' ] ); |
|
| 56 | + $this->api->register_script( 'wc-blocks-vendors', $this->api->get_block_asset_build_path( 'wc-blocks-vendors' ), [], false ); |
|
| 57 | + $this->api->register_script( 'wc-blocks-registry', 'build/wc-blocks-registry.js', [], false ); |
|
| 58 | + $this->api->register_script( 'wc-blocks', $this->api->get_block_asset_build_path( 'wc-blocks' ), [ 'wc-blocks-vendors' ], false ); |
|
| 59 | + $this->api->register_script( 'wc-blocks-shared-context', 'build/wc-blocks-shared-context.js', [] ); |
|
| 60 | + $this->api->register_script( 'wc-blocks-shared-hocs', 'build/wc-blocks-shared-hocs.js', [], false ); |
|
| 61 | + |
|
| 62 | + // The price package is shared externally so has no blocks prefix. |
|
| 63 | + $this->api->register_script( 'wc-price-format', 'build/price-format.js', [], false ); |
|
| 64 | + |
|
| 65 | + $this->api->register_script( 'wc-blocks-checkout', 'build/blocks-checkout.js', [] ); |
|
| 66 | + |
|
| 67 | + wp_add_inline_script( |
|
| 68 | + 'wc-blocks-middleware', |
|
| 69 | + " |
|
| 70 | 70 | var wcBlocksMiddlewareConfig = { |
| 71 | 71 | storeApiNonce: '" . esc_js( wp_create_nonce( 'wc_store_api' ) ) . "', |
| 72 | 72 | wcStoreApiNonceTimestamp: '" . esc_js( time() ) . "' |
| 73 | 73 | }; |
| 74 | 74 | ", |
| 75 | - 'before' |
|
| 76 | - ); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Defines resource hints to help speed up the loading of some critical blocks. |
|
| 81 | - * |
|
| 82 | - * These will not impact page loading times negatively because they are loaded once the current page is idle. |
|
| 83 | - * |
|
| 84 | - * @param array $urls URLs to print for resource hints. Each URL is an array of resource attributes, or a URL string. |
|
| 85 | - * @param string $relation_type The relation type the URLs are printed. Possible values: preconnect, dns-prefetch, prefetch, prerender. |
|
| 86 | - * @return array URLs to print for resource hints. |
|
| 87 | - */ |
|
| 88 | - public function add_resource_hints( $urls, $relation_type ) { |
|
| 89 | - if ( ! Package::feature()->is_feature_plugin_build() || ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) ) { |
|
| 90 | - return $urls; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // We only need to prefetch when the cart has contents. |
|
| 94 | - $cart = wc()->cart; |
|
| 95 | - |
|
| 96 | - if ( ! $cart || ! $cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count() ) { |
|
| 97 | - return $urls; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $resources = []; |
|
| 101 | - $is_block_cart = has_block( 'woocommerce/cart' ); |
|
| 102 | - $is_block_checkout = has_block( 'woocommerce/checkout' ); |
|
| 103 | - |
|
| 104 | - if ( 'prefetch' === $relation_type ) { |
|
| 105 | - if ( ! $is_block_cart ) { |
|
| 106 | - $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'cart-frontend' ) ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if ( ! $is_block_checkout ) { |
|
| 110 | - $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'checkout-frontend' ) ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $urls = array_merge( |
|
| 114 | - $urls, |
|
| 115 | - array_map( |
|
| 116 | - function( $src ) { |
|
| 117 | - return array( |
|
| 118 | - 'href' => $src, |
|
| 119 | - 'as' => 'script', |
|
| 120 | - ); |
|
| 121 | - }, |
|
| 122 | - array_unique( array_filter( $resources ) ) |
|
| 123 | - ) |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ( 'prerender' === $relation_type && $is_block_cart ) { |
|
| 128 | - $checkout_page_id = wc_get_page_id( 'checkout' ); |
|
| 129 | - $checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : ''; |
|
| 130 | - if ( $checkout_page_url ) { |
|
| 131 | - $urls[] = $checkout_page_url; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return $urls; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get resource hint for a block by name. |
|
| 140 | - * |
|
| 141 | - * @param string $filename Block filename. |
|
| 142 | - * @return array |
|
| 143 | - */ |
|
| 144 | - private function get_block_asset_resource_hints( $filename = '' ) { |
|
| 145 | - if ( ! $filename ) { |
|
| 146 | - return []; |
|
| 147 | - } |
|
| 148 | - $script_data = $this->api->get_script_data( |
|
| 149 | - $this->api->get_block_asset_build_path( $filename ) |
|
| 150 | - ); |
|
| 151 | - return array_merge( [ add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ], $this->get_script_dependency_src_array( $script_data['dependencies'] ) ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the src of all script dependencies (handles). |
|
| 156 | - * |
|
| 157 | - * @param array $dependencies Array of dependency handles. |
|
| 158 | - * @return string[] Array of src strings. |
|
| 159 | - */ |
|
| 160 | - private function get_script_dependency_src_array( array $dependencies ) { |
|
| 161 | - $wp_scripts = wp_scripts(); |
|
| 162 | - return array_reduce( |
|
| 163 | - $dependencies, |
|
| 164 | - function( $src, $handle ) use ( $wp_scripts ) { |
|
| 165 | - if ( isset( $wp_scripts->registered[ $handle ] ) ) { |
|
| 166 | - $src[] = add_query_arg( 'ver', $wp_scripts->registered[ $handle ]->ver, $wp_scripts->registered[ $handle ]->src ); |
|
| 167 | - $src = array_merge( $src, $this->get_script_dependency_src_array( $wp_scripts->registered[ $handle ]->deps ) ); |
|
| 168 | - } |
|
| 169 | - return $src; |
|
| 170 | - }, |
|
| 171 | - [] |
|
| 172 | - ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Add body classes to the frontend and within admin. |
|
| 177 | - * |
|
| 178 | - * @param string|array $classes Array or string of CSS classnames. |
|
| 179 | - * @return string|array Modified classnames. |
|
| 180 | - */ |
|
| 181 | - public function add_theme_body_class( $classes ) { |
|
| 182 | - $class = 'theme-' . get_template(); |
|
| 183 | - |
|
| 184 | - if ( is_array( $classes ) ) { |
|
| 185 | - $classes[] = $class; |
|
| 186 | - } else { |
|
| 187 | - $classes .= ' ' . $class . ' '; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $classes; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get the file modified time as a cache buster if we're in dev mode. |
|
| 195 | - * |
|
| 196 | - * @param string $file Local path to the file. |
|
| 197 | - * @return string The cache buster value to use for the given file. |
|
| 198 | - */ |
|
| 199 | - protected function get_file_version( $file ) { |
|
| 200 | - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) { |
|
| 201 | - return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ); |
|
| 202 | - } |
|
| 203 | - return \Automattic\WooCommerce\Blocks\Package::get_version(); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Registers a style according to `wp_register_style`. |
|
| 208 | - * |
|
| 209 | - * @param string $handle Name of the stylesheet. Should be unique. |
|
| 210 | - * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
|
| 211 | - * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|
| 212 | - * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
|
| 213 | - * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
|
| 214 | - * @param boolean $rtl Optional. Whether or not to register RTL styles. |
|
| 215 | - */ |
|
| 216 | - protected function register_style( $handle, $src, $deps = [], $media = 'all', $rtl = false ) { |
|
| 217 | - $filename = str_replace( plugins_url( '/', __DIR__ ), '', $src ); |
|
| 218 | - $ver = self::get_file_version( $filename ); |
|
| 219 | - |
|
| 220 | - wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 221 | - |
|
| 222 | - if ( $rtl ) { |
|
| 223 | - wp_style_add_data( $handle, 'rtl', 'replace' ); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Update block style dependencies after they have been registered. |
|
| 229 | - */ |
|
| 230 | - public function update_block_style_dependencies() { |
|
| 231 | - $wp_styles = wp_styles(); |
|
| 232 | - $style = $wp_styles->query( 'wc-blocks-style', 'registered' ); |
|
| 233 | - |
|
| 234 | - if ( ! $style ) { |
|
| 235 | - return; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // In WC < 5.5, `woocommerce-general` is not registered in block editor |
|
| 239 | - // screens, so we don't add it as a dependency if it's not registered. |
|
| 240 | - // In WC >= 5.5, `woocommerce-general` is registered on `admin_enqueue_scripts`, |
|
| 241 | - // so we need to check if it's registered here instead of on `init`. |
|
| 242 | - if ( |
|
| 243 | - wp_style_is( 'woocommerce-general', 'registered' ) && |
|
| 244 | - ! in_array( 'woocommerce-general', $style->deps, true ) |
|
| 245 | - ) { |
|
| 246 | - $style->deps[] = 'woocommerce-general'; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Fix scripts with wc-settings dependency. |
|
| 252 | - * |
|
| 253 | - * The wc-settings script only works correctly when enqueued in the footer. This is to give blocks etc time to |
|
| 254 | - * register their settings data before it's printed. |
|
| 255 | - * |
|
| 256 | - * This code will look at registered scripts, and if they have a wc-settings dependency, force them to print in the |
|
| 257 | - * footer instead of the header. |
|
| 258 | - * |
|
| 259 | - * This only supports packages known to require wc-settings! |
|
| 260 | - * |
|
| 261 | - * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5052 |
|
| 262 | - */ |
|
| 263 | - public function update_block_settings_dependencies() { |
|
| 264 | - $wp_scripts = wp_scripts(); |
|
| 265 | - $known_packages = [ 'wc-settings', 'wc-blocks-checkout', 'wc-price-format' ]; |
|
| 266 | - |
|
| 267 | - foreach ( $wp_scripts->registered as $handle => $script ) { |
|
| 268 | - // scripts that are loaded in the footer has extra->group = 1. |
|
| 269 | - if ( array_intersect( $known_packages, $script->deps ) && ! isset( $script->extra['group'] ) ) { |
|
| 270 | - // Append the script to footer. |
|
| 271 | - $wp_scripts->add_data( $handle, 'group', 1 ); |
|
| 272 | - // Show a warning. |
|
| 273 | - $error_handle = 'wc-settings-dep-in-header'; |
|
| 274 | - $used_deps = implode( ', ', array_intersect( $known_packages, $script->deps ) ); |
|
| 275 | - $error_message = "Scripts that have a dependency on [$used_deps] must be loaded in the footer, {$handle} was registered to load in the header, but has been switched to load in the footer instead. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5059"; |
|
| 276 | - // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
|
| 277 | - wp_register_script( $error_handle, '' ); |
|
| 278 | - wp_enqueue_script( $error_handle ); |
|
| 279 | - wp_add_inline_script( |
|
| 280 | - $error_handle, |
|
| 281 | - sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 282 | - ); |
|
| 283 | - |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - } |
|
| 75 | + 'before' |
|
| 76 | + ); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Defines resource hints to help speed up the loading of some critical blocks. |
|
| 81 | + * |
|
| 82 | + * These will not impact page loading times negatively because they are loaded once the current page is idle. |
|
| 83 | + * |
|
| 84 | + * @param array $urls URLs to print for resource hints. Each URL is an array of resource attributes, or a URL string. |
|
| 85 | + * @param string $relation_type The relation type the URLs are printed. Possible values: preconnect, dns-prefetch, prefetch, prerender. |
|
| 86 | + * @return array URLs to print for resource hints. |
|
| 87 | + */ |
|
| 88 | + public function add_resource_hints( $urls, $relation_type ) { |
|
| 89 | + if ( ! Package::feature()->is_feature_plugin_build() || ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) ) { |
|
| 90 | + return $urls; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // We only need to prefetch when the cart has contents. |
|
| 94 | + $cart = wc()->cart; |
|
| 95 | + |
|
| 96 | + if ( ! $cart || ! $cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count() ) { |
|
| 97 | + return $urls; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $resources = []; |
|
| 101 | + $is_block_cart = has_block( 'woocommerce/cart' ); |
|
| 102 | + $is_block_checkout = has_block( 'woocommerce/checkout' ); |
|
| 103 | + |
|
| 104 | + if ( 'prefetch' === $relation_type ) { |
|
| 105 | + if ( ! $is_block_cart ) { |
|
| 106 | + $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'cart-frontend' ) ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if ( ! $is_block_checkout ) { |
|
| 110 | + $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'checkout-frontend' ) ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $urls = array_merge( |
|
| 114 | + $urls, |
|
| 115 | + array_map( |
|
| 116 | + function( $src ) { |
|
| 117 | + return array( |
|
| 118 | + 'href' => $src, |
|
| 119 | + 'as' => 'script', |
|
| 120 | + ); |
|
| 121 | + }, |
|
| 122 | + array_unique( array_filter( $resources ) ) |
|
| 123 | + ) |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ( 'prerender' === $relation_type && $is_block_cart ) { |
|
| 128 | + $checkout_page_id = wc_get_page_id( 'checkout' ); |
|
| 129 | + $checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : ''; |
|
| 130 | + if ( $checkout_page_url ) { |
|
| 131 | + $urls[] = $checkout_page_url; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return $urls; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get resource hint for a block by name. |
|
| 140 | + * |
|
| 141 | + * @param string $filename Block filename. |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + private function get_block_asset_resource_hints( $filename = '' ) { |
|
| 145 | + if ( ! $filename ) { |
|
| 146 | + return []; |
|
| 147 | + } |
|
| 148 | + $script_data = $this->api->get_script_data( |
|
| 149 | + $this->api->get_block_asset_build_path( $filename ) |
|
| 150 | + ); |
|
| 151 | + return array_merge( [ add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ], $this->get_script_dependency_src_array( $script_data['dependencies'] ) ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the src of all script dependencies (handles). |
|
| 156 | + * |
|
| 157 | + * @param array $dependencies Array of dependency handles. |
|
| 158 | + * @return string[] Array of src strings. |
|
| 159 | + */ |
|
| 160 | + private function get_script_dependency_src_array( array $dependencies ) { |
|
| 161 | + $wp_scripts = wp_scripts(); |
|
| 162 | + return array_reduce( |
|
| 163 | + $dependencies, |
|
| 164 | + function( $src, $handle ) use ( $wp_scripts ) { |
|
| 165 | + if ( isset( $wp_scripts->registered[ $handle ] ) ) { |
|
| 166 | + $src[] = add_query_arg( 'ver', $wp_scripts->registered[ $handle ]->ver, $wp_scripts->registered[ $handle ]->src ); |
|
| 167 | + $src = array_merge( $src, $this->get_script_dependency_src_array( $wp_scripts->registered[ $handle ]->deps ) ); |
|
| 168 | + } |
|
| 169 | + return $src; |
|
| 170 | + }, |
|
| 171 | + [] |
|
| 172 | + ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Add body classes to the frontend and within admin. |
|
| 177 | + * |
|
| 178 | + * @param string|array $classes Array or string of CSS classnames. |
|
| 179 | + * @return string|array Modified classnames. |
|
| 180 | + */ |
|
| 181 | + public function add_theme_body_class( $classes ) { |
|
| 182 | + $class = 'theme-' . get_template(); |
|
| 183 | + |
|
| 184 | + if ( is_array( $classes ) ) { |
|
| 185 | + $classes[] = $class; |
|
| 186 | + } else { |
|
| 187 | + $classes .= ' ' . $class . ' '; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $classes; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get the file modified time as a cache buster if we're in dev mode. |
|
| 195 | + * |
|
| 196 | + * @param string $file Local path to the file. |
|
| 197 | + * @return string The cache buster value to use for the given file. |
|
| 198 | + */ |
|
| 199 | + protected function get_file_version( $file ) { |
|
| 200 | + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) { |
|
| 201 | + return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ); |
|
| 202 | + } |
|
| 203 | + return \Automattic\WooCommerce\Blocks\Package::get_version(); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Registers a style according to `wp_register_style`. |
|
| 208 | + * |
|
| 209 | + * @param string $handle Name of the stylesheet. Should be unique. |
|
| 210 | + * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
|
| 211 | + * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|
| 212 | + * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
|
| 213 | + * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
|
| 214 | + * @param boolean $rtl Optional. Whether or not to register RTL styles. |
|
| 215 | + */ |
|
| 216 | + protected function register_style( $handle, $src, $deps = [], $media = 'all', $rtl = false ) { |
|
| 217 | + $filename = str_replace( plugins_url( '/', __DIR__ ), '', $src ); |
|
| 218 | + $ver = self::get_file_version( $filename ); |
|
| 219 | + |
|
| 220 | + wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 221 | + |
|
| 222 | + if ( $rtl ) { |
|
| 223 | + wp_style_add_data( $handle, 'rtl', 'replace' ); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Update block style dependencies after they have been registered. |
|
| 229 | + */ |
|
| 230 | + public function update_block_style_dependencies() { |
|
| 231 | + $wp_styles = wp_styles(); |
|
| 232 | + $style = $wp_styles->query( 'wc-blocks-style', 'registered' ); |
|
| 233 | + |
|
| 234 | + if ( ! $style ) { |
|
| 235 | + return; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // In WC < 5.5, `woocommerce-general` is not registered in block editor |
|
| 239 | + // screens, so we don't add it as a dependency if it's not registered. |
|
| 240 | + // In WC >= 5.5, `woocommerce-general` is registered on `admin_enqueue_scripts`, |
|
| 241 | + // so we need to check if it's registered here instead of on `init`. |
|
| 242 | + if ( |
|
| 243 | + wp_style_is( 'woocommerce-general', 'registered' ) && |
|
| 244 | + ! in_array( 'woocommerce-general', $style->deps, true ) |
|
| 245 | + ) { |
|
| 246 | + $style->deps[] = 'woocommerce-general'; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Fix scripts with wc-settings dependency. |
|
| 252 | + * |
|
| 253 | + * The wc-settings script only works correctly when enqueued in the footer. This is to give blocks etc time to |
|
| 254 | + * register their settings data before it's printed. |
|
| 255 | + * |
|
| 256 | + * This code will look at registered scripts, and if they have a wc-settings dependency, force them to print in the |
|
| 257 | + * footer instead of the header. |
|
| 258 | + * |
|
| 259 | + * This only supports packages known to require wc-settings! |
|
| 260 | + * |
|
| 261 | + * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5052 |
|
| 262 | + */ |
|
| 263 | + public function update_block_settings_dependencies() { |
|
| 264 | + $wp_scripts = wp_scripts(); |
|
| 265 | + $known_packages = [ 'wc-settings', 'wc-blocks-checkout', 'wc-price-format' ]; |
|
| 266 | + |
|
| 267 | + foreach ( $wp_scripts->registered as $handle => $script ) { |
|
| 268 | + // scripts that are loaded in the footer has extra->group = 1. |
|
| 269 | + if ( array_intersect( $known_packages, $script->deps ) && ! isset( $script->extra['group'] ) ) { |
|
| 270 | + // Append the script to footer. |
|
| 271 | + $wp_scripts->add_data( $handle, 'group', 1 ); |
|
| 272 | + // Show a warning. |
|
| 273 | + $error_handle = 'wc-settings-dep-in-header'; |
|
| 274 | + $used_deps = implode( ', ', array_intersect( $known_packages, $script->deps ) ); |
|
| 275 | + $error_message = "Scripts that have a dependency on [$used_deps] must be loaded in the footer, {$handle} was registered to load in the header, but has been switched to load in the footer instead. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5059"; |
|
| 276 | + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
|
| 277 | + wp_register_script( $error_handle, '' ); |
|
| 278 | + wp_enqueue_script( $error_handle ); |
|
| 279 | + wp_add_inline_script( |
|
| 280 | + $error_handle, |
|
| 281 | + sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 282 | + ); |
|
| 283 | + |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @param AssetApi $asset_api Asset API interface for various asset registration. |
| 27 | 27 | */ |
| 28 | - public function __construct( AssetApi $asset_api ) { |
|
| 28 | + public function __construct(AssetApi $asset_api) { |
|
| 29 | 29 | $this->api = $asset_api; |
| 30 | 30 | $this->init(); |
| 31 | 31 | } |
@@ -34,42 +34,42 @@ discard block |
||
| 34 | 34 | * Initialize class features. |
| 35 | 35 | */ |
| 36 | 36 | protected function init() { |
| 37 | - add_action( 'init', array( $this, 'register_assets' ) ); |
|
| 38 | - add_filter( 'wp_resource_hints', array( $this, 'add_resource_hints' ), 10, 2 ); |
|
| 39 | - add_action( 'body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 40 | - add_action( 'admin_body_class', array( $this, 'add_theme_body_class' ), 1 ); |
|
| 41 | - add_action( 'admin_enqueue_scripts', array( $this, 'update_block_style_dependencies' ), 20 ); |
|
| 42 | - add_action( 'wp_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 43 | - add_action( 'admin_enqueue_scripts', array( $this, 'update_block_settings_dependencies' ), 100 ); |
|
| 37 | + add_action('init', array($this, 'register_assets')); |
|
| 38 | + add_filter('wp_resource_hints', array($this, 'add_resource_hints'), 10, 2); |
|
| 39 | + add_action('body_class', array($this, 'add_theme_body_class'), 1); |
|
| 40 | + add_action('admin_body_class', array($this, 'add_theme_body_class'), 1); |
|
| 41 | + add_action('admin_enqueue_scripts', array($this, 'update_block_style_dependencies'), 20); |
|
| 42 | + add_action('wp_enqueue_scripts', array($this, 'update_block_settings_dependencies'), 100); |
|
| 43 | + add_action('admin_enqueue_scripts', array($this, 'update_block_settings_dependencies'), 100); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * Register block scripts & styles. |
| 48 | 48 | */ |
| 49 | 49 | public function register_assets() { |
| 50 | - $this->register_style( 'wc-blocks-vendors-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-vendors-style', 'css' ), __DIR__ ) ); |
|
| 51 | - $this->register_style( 'wc-blocks-editor-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-editor-style', 'css' ), __DIR__ ), [ 'wp-edit-blocks' ], 'all', true ); |
|
| 52 | - $this->register_style( 'wc-blocks-style', plugins_url( $this->api->get_block_asset_build_path( 'wc-blocks-style', 'css' ), __DIR__ ), [ 'wc-blocks-vendors-style' ], 'all', true ); |
|
| 53 | - |
|
| 54 | - $this->api->register_script( 'wc-blocks-middleware', 'build/wc-blocks-middleware.js', [], false ); |
|
| 55 | - $this->api->register_script( 'wc-blocks-data-store', 'build/wc-blocks-data.js', [ 'wc-blocks-middleware' ] ); |
|
| 56 | - $this->api->register_script( 'wc-blocks-vendors', $this->api->get_block_asset_build_path( 'wc-blocks-vendors' ), [], false ); |
|
| 57 | - $this->api->register_script( 'wc-blocks-registry', 'build/wc-blocks-registry.js', [], false ); |
|
| 58 | - $this->api->register_script( 'wc-blocks', $this->api->get_block_asset_build_path( 'wc-blocks' ), [ 'wc-blocks-vendors' ], false ); |
|
| 59 | - $this->api->register_script( 'wc-blocks-shared-context', 'build/wc-blocks-shared-context.js', [] ); |
|
| 60 | - $this->api->register_script( 'wc-blocks-shared-hocs', 'build/wc-blocks-shared-hocs.js', [], false ); |
|
| 50 | + $this->register_style('wc-blocks-vendors-style', plugins_url($this->api->get_block_asset_build_path('wc-blocks-vendors-style', 'css'), __DIR__)); |
|
| 51 | + $this->register_style('wc-blocks-editor-style', plugins_url($this->api->get_block_asset_build_path('wc-blocks-editor-style', 'css'), __DIR__), ['wp-edit-blocks'], 'all', true); |
|
| 52 | + $this->register_style('wc-blocks-style', plugins_url($this->api->get_block_asset_build_path('wc-blocks-style', 'css'), __DIR__), ['wc-blocks-vendors-style'], 'all', true); |
|
| 53 | + |
|
| 54 | + $this->api->register_script('wc-blocks-middleware', 'build/wc-blocks-middleware.js', [], false); |
|
| 55 | + $this->api->register_script('wc-blocks-data-store', 'build/wc-blocks-data.js', ['wc-blocks-middleware']); |
|
| 56 | + $this->api->register_script('wc-blocks-vendors', $this->api->get_block_asset_build_path('wc-blocks-vendors'), [], false); |
|
| 57 | + $this->api->register_script('wc-blocks-registry', 'build/wc-blocks-registry.js', [], false); |
|
| 58 | + $this->api->register_script('wc-blocks', $this->api->get_block_asset_build_path('wc-blocks'), ['wc-blocks-vendors'], false); |
|
| 59 | + $this->api->register_script('wc-blocks-shared-context', 'build/wc-blocks-shared-context.js', []); |
|
| 60 | + $this->api->register_script('wc-blocks-shared-hocs', 'build/wc-blocks-shared-hocs.js', [], false); |
|
| 61 | 61 | |
| 62 | 62 | // The price package is shared externally so has no blocks prefix. |
| 63 | - $this->api->register_script( 'wc-price-format', 'build/price-format.js', [], false ); |
|
| 63 | + $this->api->register_script('wc-price-format', 'build/price-format.js', [], false); |
|
| 64 | 64 | |
| 65 | - $this->api->register_script( 'wc-blocks-checkout', 'build/blocks-checkout.js', [] ); |
|
| 65 | + $this->api->register_script('wc-blocks-checkout', 'build/blocks-checkout.js', []); |
|
| 66 | 66 | |
| 67 | 67 | wp_add_inline_script( |
| 68 | 68 | 'wc-blocks-middleware', |
| 69 | 69 | " |
| 70 | 70 | var wcBlocksMiddlewareConfig = { |
| 71 | - storeApiNonce: '" . esc_js( wp_create_nonce( 'wc_store_api' ) ) . "', |
|
| 72 | - wcStoreApiNonceTimestamp: '" . esc_js( time() ) . "' |
|
| 71 | + storeApiNonce: '" . esc_js(wp_create_nonce('wc_store_api')) . "', |
|
| 72 | + wcStoreApiNonceTimestamp: '" . esc_js(time()) . "' |
|
| 73 | 73 | }; |
| 74 | 74 | ", |
| 75 | 75 | 'before' |
@@ -85,49 +85,49 @@ discard block |
||
| 85 | 85 | * @param string $relation_type The relation type the URLs are printed. Possible values: preconnect, dns-prefetch, prefetch, prerender. |
| 86 | 86 | * @return array URLs to print for resource hints. |
| 87 | 87 | */ |
| 88 | - public function add_resource_hints( $urls, $relation_type ) { |
|
| 89 | - if ( ! Package::feature()->is_feature_plugin_build() || ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) ) { |
|
| 88 | + public function add_resource_hints($urls, $relation_type) { |
|
| 89 | + if (!Package::feature()->is_feature_plugin_build() || !in_array($relation_type, ['prefetch', 'prerender'], true)) { |
|
| 90 | 90 | return $urls; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // We only need to prefetch when the cart has contents. |
| 94 | 94 | $cart = wc()->cart; |
| 95 | 95 | |
| 96 | - if ( ! $cart || ! $cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count() ) { |
|
| 96 | + if (!$cart || !$cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count()) { |
|
| 97 | 97 | return $urls; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | $resources = []; |
| 101 | - $is_block_cart = has_block( 'woocommerce/cart' ); |
|
| 102 | - $is_block_checkout = has_block( 'woocommerce/checkout' ); |
|
| 101 | + $is_block_cart = has_block('woocommerce/cart'); |
|
| 102 | + $is_block_checkout = has_block('woocommerce/checkout'); |
|
| 103 | 103 | |
| 104 | - if ( 'prefetch' === $relation_type ) { |
|
| 105 | - if ( ! $is_block_cart ) { |
|
| 106 | - $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'cart-frontend' ) ); |
|
| 104 | + if ('prefetch' === $relation_type) { |
|
| 105 | + if (!$is_block_cart) { |
|
| 106 | + $resources = array_merge($resources, $this->get_block_asset_resource_hints('cart-frontend')); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if ( ! $is_block_checkout ) { |
|
| 110 | - $resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'checkout-frontend' ) ); |
|
| 109 | + if (!$is_block_checkout) { |
|
| 110 | + $resources = array_merge($resources, $this->get_block_asset_resource_hints('checkout-frontend')); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | $urls = array_merge( |
| 114 | 114 | $urls, |
| 115 | 115 | array_map( |
| 116 | - function( $src ) { |
|
| 116 | + function($src) { |
|
| 117 | 117 | return array( |
| 118 | 118 | 'href' => $src, |
| 119 | 119 | 'as' => 'script', |
| 120 | 120 | ); |
| 121 | 121 | }, |
| 122 | - array_unique( array_filter( $resources ) ) |
|
| 122 | + array_unique(array_filter($resources)) |
|
| 123 | 123 | ) |
| 124 | 124 | ); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - if ( 'prerender' === $relation_type && $is_block_cart ) { |
|
| 128 | - $checkout_page_id = wc_get_page_id( 'checkout' ); |
|
| 129 | - $checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : ''; |
|
| 130 | - if ( $checkout_page_url ) { |
|
| 127 | + if ('prerender' === $relation_type && $is_block_cart) { |
|
| 128 | + $checkout_page_id = wc_get_page_id('checkout'); |
|
| 129 | + $checkout_page_url = $checkout_page_id ? get_permalink($checkout_page_id) : ''; |
|
| 130 | + if ($checkout_page_url) { |
|
| 131 | 131 | $urls[] = $checkout_page_url; |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -141,14 +141,14 @@ discard block |
||
| 141 | 141 | * @param string $filename Block filename. |
| 142 | 142 | * @return array |
| 143 | 143 | */ |
| 144 | - private function get_block_asset_resource_hints( $filename = '' ) { |
|
| 145 | - if ( ! $filename ) { |
|
| 144 | + private function get_block_asset_resource_hints($filename = '') { |
|
| 145 | + if (!$filename) { |
|
| 146 | 146 | return []; |
| 147 | 147 | } |
| 148 | 148 | $script_data = $this->api->get_script_data( |
| 149 | - $this->api->get_block_asset_build_path( $filename ) |
|
| 149 | + $this->api->get_block_asset_build_path($filename) |
|
| 150 | 150 | ); |
| 151 | - return array_merge( [ add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ], $this->get_script_dependency_src_array( $script_data['dependencies'] ) ); |
|
| 151 | + return array_merge([add_query_arg('ver', $script_data['version'], $script_data['src'])], $this->get_script_dependency_src_array($script_data['dependencies'])); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -157,14 +157,14 @@ discard block |
||
| 157 | 157 | * @param array $dependencies Array of dependency handles. |
| 158 | 158 | * @return string[] Array of src strings. |
| 159 | 159 | */ |
| 160 | - private function get_script_dependency_src_array( array $dependencies ) { |
|
| 160 | + private function get_script_dependency_src_array(array $dependencies) { |
|
| 161 | 161 | $wp_scripts = wp_scripts(); |
| 162 | 162 | return array_reduce( |
| 163 | 163 | $dependencies, |
| 164 | - function( $src, $handle ) use ( $wp_scripts ) { |
|
| 165 | - if ( isset( $wp_scripts->registered[ $handle ] ) ) { |
|
| 166 | - $src[] = add_query_arg( 'ver', $wp_scripts->registered[ $handle ]->ver, $wp_scripts->registered[ $handle ]->src ); |
|
| 167 | - $src = array_merge( $src, $this->get_script_dependency_src_array( $wp_scripts->registered[ $handle ]->deps ) ); |
|
| 164 | + function($src, $handle) use ($wp_scripts) { |
|
| 165 | + if (isset($wp_scripts->registered[$handle])) { |
|
| 166 | + $src[] = add_query_arg('ver', $wp_scripts->registered[$handle]->ver, $wp_scripts->registered[$handle]->src); |
|
| 167 | + $src = array_merge($src, $this->get_script_dependency_src_array($wp_scripts->registered[$handle]->deps)); |
|
| 168 | 168 | } |
| 169 | 169 | return $src; |
| 170 | 170 | }, |
@@ -178,10 +178,10 @@ discard block |
||
| 178 | 178 | * @param string|array $classes Array or string of CSS classnames. |
| 179 | 179 | * @return string|array Modified classnames. |
| 180 | 180 | */ |
| 181 | - public function add_theme_body_class( $classes ) { |
|
| 181 | + public function add_theme_body_class($classes) { |
|
| 182 | 182 | $class = 'theme-' . get_template(); |
| 183 | 183 | |
| 184 | - if ( is_array( $classes ) ) { |
|
| 184 | + if (is_array($classes)) { |
|
| 185 | 185 | $classes[] = $class; |
| 186 | 186 | } else { |
| 187 | 187 | $classes .= ' ' . $class . ' '; |
@@ -196,9 +196,9 @@ discard block |
||
| 196 | 196 | * @param string $file Local path to the file. |
| 197 | 197 | * @return string The cache buster value to use for the given file. |
| 198 | 198 | */ |
| 199 | - protected function get_file_version( $file ) { |
|
| 200 | - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) { |
|
| 201 | - return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ); |
|
| 199 | + protected function get_file_version($file) { |
|
| 200 | + if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && file_exists(\Automattic\WooCommerce\Blocks\Package::get_path() . $file)) { |
|
| 201 | + return filemtime(\Automattic\WooCommerce\Blocks\Package::get_path() . $file); |
|
| 202 | 202 | } |
| 203 | 203 | return \Automattic\WooCommerce\Blocks\Package::get_version(); |
| 204 | 204 | } |
@@ -213,14 +213,14 @@ discard block |
||
| 213 | 213 | * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 214 | 214 | * @param boolean $rtl Optional. Whether or not to register RTL styles. |
| 215 | 215 | */ |
| 216 | - protected function register_style( $handle, $src, $deps = [], $media = 'all', $rtl = false ) { |
|
| 217 | - $filename = str_replace( plugins_url( '/', __DIR__ ), '', $src ); |
|
| 218 | - $ver = self::get_file_version( $filename ); |
|
| 216 | + protected function register_style($handle, $src, $deps = [], $media = 'all', $rtl = false) { |
|
| 217 | + $filename = str_replace(plugins_url('/', __DIR__), '', $src); |
|
| 218 | + $ver = self::get_file_version($filename); |
|
| 219 | 219 | |
| 220 | - wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 220 | + wp_register_style($handle, $src, $deps, $ver, $media); |
|
| 221 | 221 | |
| 222 | - if ( $rtl ) { |
|
| 223 | - wp_style_add_data( $handle, 'rtl', 'replace' ); |
|
| 222 | + if ($rtl) { |
|
| 223 | + wp_style_add_data($handle, 'rtl', 'replace'); |
|
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | public function update_block_style_dependencies() { |
| 231 | 231 | $wp_styles = wp_styles(); |
| 232 | - $style = $wp_styles->query( 'wc-blocks-style', 'registered' ); |
|
| 232 | + $style = $wp_styles->query('wc-blocks-style', 'registered'); |
|
| 233 | 233 | |
| 234 | - if ( ! $style ) { |
|
| 234 | + if (!$style) { |
|
| 235 | 235 | return; |
| 236 | 236 | } |
| 237 | 237 | |
@@ -240,8 +240,8 @@ discard block |
||
| 240 | 240 | // In WC >= 5.5, `woocommerce-general` is registered on `admin_enqueue_scripts`, |
| 241 | 241 | // so we need to check if it's registered here instead of on `init`. |
| 242 | 242 | if ( |
| 243 | - wp_style_is( 'woocommerce-general', 'registered' ) && |
|
| 244 | - ! in_array( 'woocommerce-general', $style->deps, true ) |
|
| 243 | + wp_style_is('woocommerce-general', 'registered') && |
|
| 244 | + !in_array('woocommerce-general', $style->deps, true) |
|
| 245 | 245 | ) { |
| 246 | 246 | $style->deps[] = 'woocommerce-general'; |
| 247 | 247 | } |
@@ -262,23 +262,23 @@ discard block |
||
| 262 | 262 | */ |
| 263 | 263 | public function update_block_settings_dependencies() { |
| 264 | 264 | $wp_scripts = wp_scripts(); |
| 265 | - $known_packages = [ 'wc-settings', 'wc-blocks-checkout', 'wc-price-format' ]; |
|
| 265 | + $known_packages = ['wc-settings', 'wc-blocks-checkout', 'wc-price-format']; |
|
| 266 | 266 | |
| 267 | - foreach ( $wp_scripts->registered as $handle => $script ) { |
|
| 267 | + foreach ($wp_scripts->registered as $handle => $script) { |
|
| 268 | 268 | // scripts that are loaded in the footer has extra->group = 1. |
| 269 | - if ( array_intersect( $known_packages, $script->deps ) && ! isset( $script->extra['group'] ) ) { |
|
| 269 | + if (array_intersect($known_packages, $script->deps) && !isset($script->extra['group'])) { |
|
| 270 | 270 | // Append the script to footer. |
| 271 | - $wp_scripts->add_data( $handle, 'group', 1 ); |
|
| 271 | + $wp_scripts->add_data($handle, 'group', 1); |
|
| 272 | 272 | // Show a warning. |
| 273 | 273 | $error_handle = 'wc-settings-dep-in-header'; |
| 274 | - $used_deps = implode( ', ', array_intersect( $known_packages, $script->deps ) ); |
|
| 274 | + $used_deps = implode(', ', array_intersect($known_packages, $script->deps)); |
|
| 275 | 275 | $error_message = "Scripts that have a dependency on [$used_deps] must be loaded in the footer, {$handle} was registered to load in the header, but has been switched to load in the footer instead. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5059"; |
| 276 | 276 | // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 277 | - wp_register_script( $error_handle, '' ); |
|
| 278 | - wp_enqueue_script( $error_handle ); |
|
| 277 | + wp_register_script($error_handle, ''); |
|
| 278 | + wp_enqueue_script($error_handle); |
|
| 279 | 279 | wp_add_inline_script( |
| 280 | 280 | $error_handle, |
| 281 | - sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 281 | + sprintf('console.warn( "%s" );', $error_message) |
|
| 282 | 282 | ); |
| 283 | 283 | |
| 284 | 284 | } |