diff --git a/composer.json b/composer.json index 3b3c35d5ef..e673ef664b 100755 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "knplabs/gaufrette": "0.7", "bagrinsergiu/brizy-migration-utils": "^1.4", "bagrinsergiu/brizy-merge-page-assets": "dev-master", - "bagrinsergiu/content-placeholder": "^2.0.8", + "bagrinsergiu/content-placeholder": "^2.0.10", "enshrined/svg-sanitize": "^0.13", "select2/select2": "^4.0", "symfony/dotenv": "^3", diff --git a/composer.lock b/composer.lock index 34195312b2..15cd8afd80 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3304adb878f0b8cdc35383de6c064419", + "content-hash": "dc00b28f001f3e324eb20682f67acc47", "packages": [ { "name": "bagrinsergiu/brizy-merge-page-assets", @@ -81,16 +81,16 @@ }, { "name": "bagrinsergiu/content-placeholder", - "version": "2.0.9", + "version": "v2.0.10", "source": { "type": "git", "url": "https://github.com/bagrinsergiu/brizy-content-placeholder.git", - "reference": "b83a386223b21c2e38c4f38798c6426e45fdd3c0" + "reference": "f27284044391dec9e9b69dc0867d0a54ca6d484c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bagrinsergiu/brizy-content-placeholder/zipball/b83a386223b21c2e38c4f38798c6426e45fdd3c0", - "reference": "b83a386223b21c2e38c4f38798c6426e45fdd3c0", + "url": "https://api.github.com/repos/bagrinsergiu/brizy-content-placeholder/zipball/f27284044391dec9e9b69dc0867d0a54ca6d484c", + "reference": "f27284044391dec9e9b69dc0867d0a54ca6d484c", "shasum": "" }, "require": { @@ -115,10 +115,10 @@ "placeholders" ], "support": { - "source": "https://github.com/bagrinsergiu/brizy-content-placeholder/tree/2.0.9", + "source": "https://github.com/bagrinsergiu/brizy-content-placeholder/tree/v2.0.10", "issues": "https://github.com/bagrinsergiu/brizy-content-placeholder/issues" }, - "time": "2023-07-05T10:53:28+00:00" + "time": "2023-08-09T11:49:48+00:00" }, { "name": "enshrined/svg-sanitize", diff --git a/content/placeholders/abstract.php b/content/placeholders/abstract.php index 31ba30bc0c..9387b01297 100644 --- a/content/placeholders/abstract.php +++ b/content/placeholders/abstract.php @@ -4,229 +4,218 @@ use BrizyPlaceholders\ContextInterface; use BrizyPlaceholders\PlaceholderInterface; -abstract class Brizy_Content_Placeholders_Abstract extends \BrizyPlaceholders\AbstractPlaceholder implements PlaceholderInterface -{ - - const DISPLAY_INLINE = 'inline'; - const DISPLAY_BLOCK = 'block'; - - /** - * @return string - */ - protected $label; - - /** - * @return string - */ - protected $placeholder; - - /** - * @var array - */ - protected $attributes = []; - - /** - * @var string - */ - protected $display = self::DISPLAY_INLINE; - - protected $group = ''; - - /** - * It should return a unique identifier of the placeholder used for replacer. - * Do not use this where you need a constant value. - * - * @return mixed - */ - public function getUid() - { - return md5(microtime()); - } - - public function getUniqueId() - { - return md5(get_class($this)); - } - - public function getConfigStructure() - { - return [ - 'id' => $this->getUniqueId(), - 'label' => $this->getLabel(), - 'name' => $this->getPlaceholder(), - 'placeholder' => $this->buildPlaceholder(), - 'display' => $this->getDisplay(), - 'attr' => (object)$this->getAttributes() - ]; - } - - public function support($placeholderName) - { - return $this->getPlaceholder() == $placeholderName; - } - - public function shouldFallbackValue($value, ContextInterface $context, ContentPlaceholder $placeholder) - { - return empty($value); - } - - public function getFallbackValue(ContextInterface $context, ContentPlaceholder $placeholder) - { - $attributes = $placeholder->getAttributes(); - - return isset($attributes[PlaceholderInterface::FALLBACK_KEY]) ? $attributes[PlaceholderInterface::FALLBACK_KEY] : ''; - } - - /** - * @return mixed - */ - public function getLabel() - { - return $this->label; - } - - /** - * @param mixed $label - * - * @return Brizy_Content_Placeholders_Abstract - */ - public function setLabel($label) - { - $this->label = $label; - - return $this; - } - - /** - * @return string - */ - public function getDisplay() - { - return $this->display; - } - - /** - * @param string $display - * - * @return Brizy_Content_Placeholders_Abstract - */ - public function setDisplay($display) - { - $this->display = $display; - - return $this; - } - - /** - * @return string - */ - public function getGroup() - { - return $this->group; - } - - /** - * @param string $group - * - * @return Brizy_Content_Placeholders_Abstract - */ - public function setGroup($group) - { - $this->group = $group; - - return $this; - } - - /** - * @return mixed - */ - public function getPlaceholder() - { - return $this->placeholder; - } - - /** - * @param mixed $placeholder - * - * @return Brizy_Content_Placeholders_Abstract - */ - public function setPlaceholder($placeholder) - { - $this->placeholder = $placeholder; - - return $this; - } - - public function getAttributes() - { - return $this->attributes; - } - - public function setAttributes($attributes) - { - return $this->attributes = (array)$attributes; - } - - public function getEntity(ContentPlaceholder $placeholder) - { - if (($entityType = $placeholder->getAttribute('type')) && ($entityId = $placeholder->getAttribute('id'))) { - - if ($this->is_post_type($entityType)) { - return get_post((int)$entityId); - } - - if ($entityType == "WP_User") { - return get_user_by('ID', $entityId); - } - - if ($entityType == "WP_Term") { - return get_term((int)$entityId); - } - } - - return null; - } - - /** - * @return array - */ - public function convertToOptionValue() - { - return array( - 'label' => $this->getLabel(), - 'placeholder' => $this->getPlaceholder(), - 'display' => $this->getDisplay(), - ); - } - - public function jsonSerialize() - { - return array( - 'label' => $this->getLabel(), - 'placeholder' => $this->getReplacePlaceholder(), - 'display' => $this->getDisplay(), - ); - } - - /** - * @return string - */ - public function getReplacePlaceholder($attributes = []) - { - return $this->buildPlaceholder($attributes); - } - - private function is_post_type($post = null) - { - $all_custom_post_types = get_post_types(); - - // there are no custom post types - if (empty ($all_custom_post_types)) { - return false; - } - - $custom_types = array_keys($all_custom_post_types); - - return in_array($post, $custom_types); - } +abstract class Brizy_Content_Placeholders_Abstract extends \BrizyPlaceholders\AbstractPlaceholder implements PlaceholderInterface { + + const DISPLAY_INLINE = 'inline'; + const DISPLAY_BLOCK = 'block'; + + /** + * @return string + */ + protected $label; + + /** + * @return string + */ + protected $placeholder; + + /** + * @var array + */ + protected $attributes = []; + + protected $varyAttributes = ['type','id']; + + /** + * @var string + */ + protected $display = self::DISPLAY_INLINE; + + protected $group = ''; + + /** + * It should return a unique identifier of the placeholder used for replacer. + * Do not use this where you need a constant value. + * + * @return mixed + */ + public function getUid() { + return md5( microtime() ); + } + + public function getUniqueId() { + return md5( get_class( $this ) ); + } + + public function getConfigStructure() { + return [ + 'id' => $this->getUniqueId(), + 'label' => $this->getLabel(), + 'name' => $this->getPlaceholder(), + 'placeholder' => $this->buildPlaceholder(), + 'display' => $this->getDisplay(), + 'attr' => (object) $this->getAttributes(), + 'varyAttr' => (object) $this->getVaryAttributes() + ]; + } + + public function support( $placeholderName ) { + return $this->getPlaceholder() == $placeholderName; + } + + public function shouldFallbackValue( $value, ContextInterface $context, ContentPlaceholder $placeholder ) { + return empty( $value ); + } + + public function getFallbackValue( ContextInterface $context, ContentPlaceholder $placeholder ) { + $attributes = $placeholder->getAttributes(); + + return isset( $attributes[ PlaceholderInterface::FALLBACK_KEY ] ) ? $attributes[ PlaceholderInterface::FALLBACK_KEY ] : ''; + } + + /** + * @return mixed + */ + public function getLabel() { + return $this->label; + } + + /** + * @param mixed $label + * + * @return Brizy_Content_Placeholders_Abstract + */ + public function setLabel( $label ) { + $this->label = $label; + + return $this; + } + + /** + * @return string + */ + public function getDisplay() { + return $this->display; + } + + /** + * @param string $display + * + * @return Brizy_Content_Placeholders_Abstract + */ + public function setDisplay( $display ) { + $this->display = $display; + + return $this; + } + + /** + * @return string + */ + public function getGroup() { + return $this->group; + } + + /** + * @param string $group + * + * @return Brizy_Content_Placeholders_Abstract + */ + public function setGroup( $group ) { + $this->group = $group; + + return $this; + } + + /** + * @return mixed + */ + public function getPlaceholder() { + return $this->placeholder; + } + + /** + * @param mixed $placeholder + * + * @return Brizy_Content_Placeholders_Abstract + */ + public function setPlaceholder( $placeholder ) { + $this->placeholder = $placeholder; + + return $this; + } + + public function getAttributes() { + return $this->attributes; + } + + public function setAttributes( $attributes ) { + return $this->attributes = (array) $attributes; + } + + public function getVaryAttributes() { + return $this->varyAttributes; + } + + public function setVaryAttributes( $attributes ) { + return $this->varyAttributes = (array) $attributes; + } + + public function getEntity( ContentPlaceholder $placeholder ) { + if ( ( $entityType = $placeholder->getAttribute( 'type' ) ) && ( $entityId = $placeholder->getAttribute( 'id' ) ) ) { + + if ( $this->is_post_type( $entityType ) ) { + return get_post( (int) $entityId ); + } + + if ( $entityType == "WP_User" ) { + return get_user_by( 'ID', $entityId ); + } + + if ( $entityType == "WP_Term" ) { + return get_term( (int) $entityId ); + } + } + + return null; + } + + /** + * @return array + */ + public function convertToOptionValue() { + return array( + 'label' => $this->getLabel(), + 'placeholder' => $this->getPlaceholder(), + 'display' => $this->getDisplay(), + ); + } + + public function jsonSerialize() { + return array( + 'label' => $this->getLabel(), + 'placeholder' => $this->getReplacePlaceholder(), + 'display' => $this->getDisplay(), + ); + } + + /** + * @return string + */ + public function getReplacePlaceholder( $attributes = [] ) { + return $this->buildPlaceholder( $attributes ); + } + + private function is_post_type( $post = null ) { + $all_custom_post_types = get_post_types(); + + // there are no custom post types + if ( empty ( $all_custom_post_types ) ) { + return false; + } + + $custom_types = array_keys( $all_custom_post_types ); + + return in_array( $post, $custom_types ); + } } diff --git a/content/placeholders/simple.php b/content/placeholders/simple.php index 3a00fbb529..034b4c3128 100644 --- a/content/placeholders/simple.php +++ b/content/placeholders/simple.php @@ -18,13 +18,17 @@ class Brizy_Content_Placeholders_Simple extends Brizy_Content_Placeholders_Abstr * @param $value * @param string $display */ - public function __construct($label, $placeholder, $value, $group = null, $display = Brizy_Content_Placeholders_Abstract::DISPLAY_INLINE, $attrs = []) + public function __construct($label, $placeholder, $value, $group = null, $display = Brizy_Content_Placeholders_Abstract::DISPLAY_INLINE, $attrs = [], $varyAttrs = null) { $this->setLabel($label); $this->setPlaceholder($placeholder); $this->setDisplay($display); $this->setGroup($group); $this->setAttributes($attrs); + if(is_array($varyAttrs)) + { + $this->setVaryAttributes($varyAttrs); + } $this->value = $value; }