: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Option Templates helper methods.
* Class ET_Builder_Module_Helper_OptionTemplate
class ET_Builder_Module_Helper_OptionTemplate {
private $templates = array();
private $cache = array();
private $tab_slug_map = array();
public $template_prefix = '%t';
protected static $_ = null;
public static function instance() {
return $instance ? $instance : $instance = new self();
private function __construct() {
self::$_ = ET_Core_Data_Utils::instance();
private function uniq( $prefix, $content ) {
$key = md5( $prefix . serialize( $content ) );
if ( isset( $this->map[ $key ] ) ) {
return $this->map[ $key ];
return ( $this->map[ $key ] = $this->template_prefix . $key );
* Determine whether option template is enabled on current request or not
public function is_enabled() {
// Option template tends to be enabled on most request to speed up performance
// Option template is disabled on:
// 1. AJAX request for fetching classic builder (BB)'s module data. BB data is shipped as
// optimized template markup which is rendered on server then sent as string. Hence
// Option Template's sent-config-rebuild-on-js won't be usable for BB
// 2. BB's editing page. BB edit page scans for field dependency and generates visibility
// setting on `window.et_pb_module_field_dependencies` variable for field depency thus
// actual field should be rendered here instead of templateId
if ( et_builder_is_loading_bb_data() || et_builder_is_bb_page() ) {
* Filters option template status
return apply_filters( 'et_builder_option_template_is_active', $status );
* Determine whether given field name is option template field based on its first two characters
public function is_option_template_field( $field_name = '' ) {
return $this->template_prefix === substr( $field_name, 0, 2 );
public function has( $key ) {
return isset( $this->templates[ $key ] );
public function add( $key, $template ) {
$fields_template = array_merge( $template, et_pb_responsive_options()->create( $template ) );
// Populate tab_slug of given template because advance fields can be rendered on any tab
foreach( $fields_template as $field_name => $field ) {
if ( isset( $field['tab_slug'] ) ) {
$tab_slug = '' === $field['tab_slug'] ? 'advanced' : $field['tab_slug'];
if ( ! isset( $this->tab_slug_map[ $tab_slug ] ) ) {
$this->tab_slug_map[ $tab_slug ] = array( $key );
if ( ! in_array( $key, $this->tab_slug_map[ $tab_slug ] ) ) {
$this->tab_slug_map[ $tab_slug ][] = $key;
$this->templates[ $key ] = $fields_template;
public function create( $key, $config, $return_template_id = false ) {
$data = array( $key, $config );
$id = $this->uniq( $key, $data );
// Alternative, this will save the values directly in the Module $this->unprocessed_fields
// instead of this Calls $this->data and hence require a simpler logic.
// Theoretically it should require more memory but blackfire begs to differ.
$this->data[ $id ] = $data;
// Return as template id instead of id => key if needed
if ( $return_template_id ) {
return array( $id => $key );
* Create placeholders for template's params
public function placeholders( $config, $idx = 1, $path = array() ) {
foreach ( $config as $key => $value ) {
if ( is_array( $value ) ) {
// Prepend current key as path so placeholder later can correctly fetch correct
// value from template data using dot notation path (both lodash get() or utils's
// array_get() support this).
$value = $this->placeholders( $value, $idx, $path );
// Prepend dot notation path as prefix if needed
$prefix = empty( $path ) ? '' : implode( '.', $path ) . '.';
$value = "%%{$prefix}{$key}%%";
$placeholders[ $key ] = $value;
public function templates() {
* Set `$this->data` property from external source (ie: static field definition cache).
* @param array $cached_data
public function set_data( $cached_data = array() ) {
$this->data = wp_parse_args(
* Set `$this->templates` property from external source (ie: static field definition cache).
* @param array $cached_template
public function set_templates( $cached_templates = array() ) {
$this->templates = wp_parse_args(
* Set `$this->tab_slug_map` from external source (ie: static field definition cache).
* @param array $cached_tab_slug_map
public function set_tab_slug_map( $cached_tab_slug_map = array() ) {
$this->tab_slug_map = wp_parse_args(
* Get template data based on given template id
* @param string $template_id
public function get_data( $template_id = '' ) {
return self::$_->array_get( $this->data, $template_id, array() );
* Get template based on given template type
public function get_template( $type = '' ) {
return self::$_->array_get( $this->templates, $type, array() );
* Get hashed cache key based on params given
public function get_cache_key( $params ) {
$params = is_string( $params ) ? $params : serialize( $params );
* Return null if no cached value found
public function get_cache( $name, $key ) {
return self::$_->array_get( $this->cache, "{$name}.{$key}", null );
public function set_cache( $name, $key, $value ) {
self::$_->array_set( $this->cache, "{$name}.{$key}", $value );
* Get placeholder of given template
* @param string $template
public function get_template_placeholder( $template ) {
// Check for cached result first for faster performance
$cache_name = 'template_placeholder';
$cache_key = $this->get_cache_key( $template );
$cache = $this->get_cache( $cache_name, $cache_key );
if ( ! is_null( $cache ) ) {
preg_match( '/(?<=%%).*(?=%%)/', $template, $placeholder );
$this->set_cache( $cache_name, $cache_key, $placeholder );
public function get_tab_slug_map() {
return $this->tab_slug_map;
public function is_template_inside_tab( $tab_name, $template_type ) {
// Template which has `%%tab_slug%%` tab_slug can exist on any tab
if ( in_array( $template_type, self::$_->array_get( $this->tab_slug_map, '%%tab_slug%%', array() ) ) ) {
if ( in_array( $template_type, self::$_->array_get( $this->tab_slug_map, $tab_name, array() ) ) ) {
public function rebuild_string_placeholder( $template, $data = array(), $settings = array() ) {
$default_settings = array(
'remove_suffix_if_empty' => false,
$placeholder_settings = wp_parse_args( $settings, $default_settings );
// Check for cached result first for faster performance
$cache_name = 'string_placeholder';
$cache_key = $this->get_cache_key( array( $template, $data, $placeholder_settings ) );
$cache = $this->get_cache( $cache_name, $cache_key );
if ( ! is_null( $cache ) ) {
$placeholder = is_string( $template ) ? $this->get_template_placeholder( $template ) : false;
// If found, replace placeholder with correct value from data
if ( is_array( $placeholder ) && isset( $placeholder[0] ) ) {
// Get placeholder replacement
$replacement = self::$_->array_get( $data, "1.{$placeholder[0]}" );
// Pass null as empty string; null as attribute affect builder differently.
// Attribute with empty string will be omitted later.
if ( is_null( $replacement ) ) {
// If placeholder is identical to template, return replacement early. This also
// handles the case where replacement as array type
if ( "%%{$placeholder[0]}%%" === $template ) {
$this->set_cache( $cache_name, $cache_key, $replacement );
// Get placeholder suffix
$has_suffix = '' === $replacement && $placeholder_settings['remove_suffix_if_empty'];
$suffix = $has_suffix ? $placeholder_settings['suffix'] : '';
// Make sure replacement is string before proceed;
if ( is_string( $replacement ) ) {
$rebuilt_string = str_replace( "%%{$placeholder[0]}%%{$suffix}", $replacement, $template );
$this->set_cache( $cache_name, $cache_key, $rebuilt_string );
$this->set_cache( $cache_name, $cache_key, $template );
public function rebuild_preset_placeholder( $template, $data = array(), $settings = array() ) {
// Check for cached result first for faster performance
$cache_name = 'preset_placeholder';
$cache_key = $this->get_cache_key( array( $template, $data, $settings ) );
$cache = $this->get_cache( $cache_name, $cache_key );
if ( ! is_null( $cache ) ) {
foreach ( $template as $preset_attr_key => $preset_attr_value ) {
// Object inside preset array mostly contains fields attribute which its object key
// contains placeholder while its object value contains actual value without placeholder.
if ( is_array( $preset_attr_value ) ) {
$rebuilt_preset_attr_object = array();
foreach( $preset_attr_value as $name => $value ) {
$object_item_name = $this->rebuild_string_placeholder( $name, $data, $settings );
$rebuilt_preset_attr_object[ $object_item_name ] = $value;
$rebuild_attr[ $preset_attr_key ] = $rebuilt_preset_attr_object;
$rebuild_attr[ $preset_attr_key ] = $preset_attr_value;
$this->set_cache( $cache_name, $cache_key, $rebuild_attr );
public function rebuild_composite_structure_placeholder( $template_type, $template, $data = array() ) {
// Check for cached result first for faster performance
$cache_name = 'composite_structure_placeholder';
$cache_key = $this->get_cache_key( array( $template_type, $template, $data ) );
$cache = $this->get_cache( $cache_name, $cache_key );
if ( ! is_null( $cache ) ) {
$rebuilt_composite_structure_field = $template;
// Replaces placeholder with actual value on border's nested composite structure fields
if ( 'border' === $template_type ) {
// Reset `controls` attribute output
$rebuilt_composite_structure_field['controls'] = array();
// Loop composite structure's original `controls` from template
foreach ( $template['controls'] as $field_name => $field ) {
$rebuilt_field_name = $this->rebuild_string_placeholder( $field_name, $data );
// Loop field on composite structure controls
foreach ( $rebuilt_field as $attr_name => $attr_value ) {
$rebuilt_field[ $attr_name ] = $this->rebuild_string_placeholder( $attr_value, $data, array(
'suffix' => 'label' === $attr_name ? ' ' : '',
'remove_suffix_if_empty' => 'label' === $attr_name,
$rebuilt_composite_structure_field['controls'][ $rebuilt_field_name ] = $rebuilt_field;
$this->set_cache( $cache_name, $cache_key, $rebuilt_composite_structure_field );
return $rebuilt_composite_structure_field;
public function rebuild_field_attr_value( $attr_name, $attr_value, $template_data ) {
// Check for cached result first for faster performance
$cache_name = 'field_attr_value';
$cache_key = $this->get_cache_key( array( $attr_name, $attr_value, $template_data ) );
$cache = $this->get_cache( $cache_name, $cache_key );
if ( ! is_null( $cache ) ) {
$template_type = self::$_->array_get( $template_data, '0', '' );
$prefix = self::$_->array_get( $template_data, '1.prefix', '' );
// Certain advanced field (ie. Text Shadow) automatically adds underscore
$auto_add_prefix_underscore = isset( $template_data[0] ) &&'text_shadow' === $template_data[0] && '' === $prefix;
// 1. Field attribute value's type is string
if ( is_string( $attr_value ) ) {
$placeholder_has_space_suffix = 'label' === $attr_name && in_array( $template_type, array( 'border', 'text_shadow' ) );
$rebuilt_placeholder = $this->rebuild_string_placeholder( $attr_value, $template_data, array(
'suffix' => $placeholder_has_space_suffix ? ' ' : '',