Created
March 19, 2018 20:37
-
-
Save CNDLS/8e9c626b51b84dde6436d9f5b15555ec to your computer and use it in GitHub Desktop.
Filter the Backbone.js template for Elementor Section and Column elements, so that editing the custom spacing controls shows up in the page editor preview pane.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Change the Editor Preview Template for the section and column elements so that each | |
* element's mld spacing settings are reflected in the preview window whenever the user makes a | |
* change. | |
*/ | |
add_action( 'elementor/element/print_template', 'add_spacing_to_section_and_column_preview_template', 10, 2); | |
/** | |
* | |
* @param $template string The JavaScript template output | |
* @param $widget Widget_Base The widget instance | |
*/ | |
function add_spacing_to_section_and_column_preview_template( $template, $widget ) { | |
$class_name = get_class( $widget ); | |
// By default, new template is the same as the old template | |
$new_template = $template; | |
/* | |
* Create the Javascript data processing part of the Backbone preview template which | |
* tells Backbone to construct a string of mld spacing classes out of a repeater control | |
* whenever the controls are updated. | |
*/ | |
ob_start(); | |
?> | |
<# | |
if ( settings.mld_spacing_items && (settings.spacing_items_override !== 'yes') ) { | |
var spacing_classes_array = []; | |
_.each( settings.mld_spacing_items, function( item ) { | |
var spacing_class = item.property + "-" + item.direction + "-" + item.heading_level; | |
spacing_classes_array.push(spacing_class); | |
if (item.zero_xs === 'yes') { | |
spacing_classes_array.push(item.property + "-" + item.direction + "-none" + "-xs"); | |
} | |
if (item.zero_sm === 'yes') { | |
spacing_classes_array.push(item.property + "-" + item.direction + "-none" + "-sm"); | |
} | |
if (item.zero_md === 'yes') { | |
spacing_classes_array.push(item.property + "-" + item.direction + "-none" + "-md"); | |
} | |
if (item.zero_lg === 'yes') { | |
spacing_classes_array.push(item.property + "-" + item.direction + "-none" + "-lg"); | |
} | |
if (item.zero_xl === 'yes') { | |
spacing_classes_array.push(item.property + "-" + item.direction + "-none" + "-xl"); | |
} | |
}); | |
var spacing_classes_string = spacing_classes_array.join(' '); | |
} | |
#> | |
<?php | |
// Get the output buffer contents as a string and turn off the output buffer | |
// We will prepend the processing script to the template further down in this function | |
$mld_spacing_processing_script = ob_get_clean(); | |
if ( $class_name === 'Elementor\\Element_Section' ) { | |
/* | |
* Replace the original elementor-row with a row that has our spacing class variable | |
* which Backbone will dynamically update. | |
*/ | |
$row_string = '<div class="elementor-row">'; | |
$modified_row_string = '<div class="elementor-row {{ spacing_classes_string }}">'; | |
// mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) | |
$new_template = str_replace( $row_string, $modified_row_string, $template ); | |
} elseif ( $class_name === 'Elementor\\Element_Column' ) { | |
/* | |
* Replace the original elementor-column with a column that has our spacing class variable | |
* which Backbone will dynamically update. | |
*/ | |
$column_string = '<div class="elementor-column-wrap">'; | |
$modified_column_string = '<div class="elementor-column-wrap {{ spacing_classes_string }}">'; | |
// mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) | |
$new_template = str_replace( $column_string, $modified_column_string, $template ); | |
} | |
// Add the processing script to the beginning of the template | |
$new_template = $mld_spacing_processing_script . "\n" .$new_template; | |
return $new_template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment