示例#1
0
 /**
 * Try to automatically generate the script necessary for postMessage to work.
 * Something like this will have to be added to the control arguments:
 *
 
 	'transport' => 'postMessage',
 	'js_vars'   => array(
 		array(
 			'element'  => 'body',
 			'function' => 'css',
 			'property' => 'color',
 		),
 		array(
 			'element'  => '#content',
 			'function' => 'css',
 			'property' => 'background-color',
 		),
 		array(
 		'element'  => 'body',
 		'function' => 'html',
 		)
 	)
 *
 */
 public function wp_footer()
 {
     global $wp_customize;
     // Early exit if we're not in the customizer
     if (!isset($wp_customize)) {
         return;
     }
     $fields = Kirki::fields()->get_all();
     $script = '';
     foreach ($fields as $field) {
         if (isset($field['transport']) && !is_null($field['js_vars']) && 'postMessage' == $field['transport']) {
             foreach ($field['js_vars'] as $js_vars) {
                 $script .= 'wp.customize( \'' . $field['settings'] . '\', function( value ) {';
                 $script .= 'value.bind( function( newval ) {';
                 if ('html' == $js_vars['function']) {
                     $script .= '$( \'' . esc_js($js_vars["element"]) . '\' ).html( newval );';
                 } elseif ('css' == $js_vars['function']) {
                     $script .= '$(\'' . esc_js($js_vars["element"]) . '\').css(\'' . esc_js($js_vars["property"]) . '\', newval );';
                 }
                 $script .= '}); });';
             }
         }
     }
     if ('' != $script) {
         echo ScriptRegistry::prepare($script);
     }
 }
示例#2
0
 /**
  * If we've specified an image to be used as logo,
  * replace the default theme description with a div that will include our logo.
  */
 public function customize_controls_print_scripts()
 {
     $options = Kirki::config()->get_all();
     $script = '';
     if ('' != $options['logo_image'] || '' != $options['description']) {
         if ('' != $options['logo_image']) {
             $script .= '$( \'div#customize-info .preview-notice\' ).replaceWith( \'<img src="' . $options['logo_image'] . '">\' );';
         }
         if ('' != $options['description']) {
             $script .= '$( \'div#customize-info .accordion-section-content\' ).replaceWith( \'<div class="accordion-section-content"><div class="theme-description">' . $options['description'] . '</div></div>\' );';
         }
     }
     if ('' != $script) {
         echo ScriptRegistry::prepare($script);
     }
 }
示例#3
0
 /**
  * Add the help bubble
  */
 function customize_controls_print_footer_scripts()
 {
     $fields = Kirki::fields()->get_all();
     $scripts = array();
     foreach ($fields as $field) {
         if ('number' == $field['type']) {
             $scripts[] = '$( "#customize-control-' . $field['settings'] . ' input[type=\'number\']").stepper();';
         }
     }
     // No need to echo anything if the script is empty
     if (empty($scripts)) {
         return;
     }
     // Make sure we don't add any duplicates
     $scripts = array_unique($scripts);
     // Convert array to string
     $script = implode('', $scripts);
     echo ScriptRegistry::prepare($script);
 }
示例#4
0
 /**
  * Add the help bubble
  */
 function customize_controls_print_footer_scripts()
 {
     $fields = Kirki::fields()->get_all();
     $scripts = array();
     $script = '';
     foreach ($fields as $field) {
         if (!empty($field['help'])) {
             $bubble_content = $field['help'];
             $content = "<a href='#' class='tooltip hint--left' data-hint='" . strip_tags(esc_html($bubble_content)) . "'><span class='dashicons dashicons-info'></span></a>";
             $scripts[] = '$( "' . $content . '" ).prependTo( "#customize-control-' . $field['settings'] . '" );';
         }
     }
     // No need to echo anything if the script is empty
     if (empty($scripts)) {
         return;
     }
     // Make sure we don't add any duplicates
     $scripts = array_unique($scripts);
     // Convert array to string
     $script = implode('', $scripts);
     echo ScriptRegistry::prepare($script);
 }
示例#5
0
 /**
  * Add the required script.
  */
 function customize_controls_print_footer_scripts()
 {
     $fields = Kirki::fields()->get_all();
     // Early exit if no controls are defined
     if (empty($fields)) {
         return;
     }
     $script = '';
     foreach ($fields as $field) {
         $required = isset($field['required']) ? $field['required'] : false;
         if ($required) {
             $show = false;
             foreach ($required as $dependency) {
                 // Find the type of the dependency control
                 $type = $fields[$dependency['setting']]['type'];
                 // If "operator" is not set then set it to "=="
                 if (!isset($dependency['operator'])) {
                     $dependency['operator'] = '==';
                 }
                 $dependency['operator'] = esc_js($dependency['operator']);
                 // Set the control type
                 $type = 'dropdown-pages' == $type ? 'select' : $type;
                 $type = 'radio-image' == $type ? 'radio' : $type;
                 $type = 'radio-buttonset' == $type ? 'radio' : $type;
                 $type = 'toggle' == $type ? 'checkbox' : $type;
                 $type = 'switch' == $type ? 'checkbox' : $type;
                 // Set the controller used in the script
                 $controller = '#customize-control-' . $dependency['setting'] . ' input';
                 if ('select' == $type) {
                     $controller = '#customize-control-' . $dependency['setting'] . ' select';
                 } elseif ('radio' == $type) {
                     $controller = '#customize-control-' . $dependency['setting'] . ' input[value="' . $dependency['value'] . '"]';
                 }
                 // The target element
                 $target = '#customize-control-' . $field['settings'];
                 // if this is a background control then make sure we target all sub-controls
                 if ('background' == $field['type']) {
                     $target = '#customize-control-' . $control['settings'] . '_color, ';
                     $target .= '#customize-control-' . $control['settings'] . '_image, ';
                     $target .= '#customize-control-' . $control['settings'] . '_repeat, ';
                     $target .= '#customize-control-' . $control['settings'] . '_size, ';
                     $target .= '#customize-control-' . $control['settings'] . '_position, ';
                     $target .= '#customize-control-' . $control['settings'] . '_attach';
                 }
                 if (!isset($dependency['operator'])) {
                     $dependency['operator'] = '==';
                 }
                 $action_1 = '.show()';
                 $action_2 = '.hide()';
                 // Allow checking both checked and unchecked checkboxes
                 if ('checkbox' == $type) {
                     if (0 == $dependency['value'] && '==' == $dependency['operator']) {
                         $action_1 = '.hide()';
                         $action_2 = '.show()';
                         $show = true;
                     }
                     if (1 == $dependency['value'] && '!=' == $dependency['operator']) {
                         $action_1 = '.hide()';
                         $action_2 = '.show()';
                     }
                 }
                 // Get the initial status
                 $value = kirki_get_option($field['settings']);
                 if ('==' == $dependency['operator']) {
                     $show = $show && $dependency['value'] == $value ? true : $show;
                 } elseif ('!=' == $dependency['operator']) {
                     $show = $show && $dependency['value'] != $value ? true : $show;
                 } elseif ('>=' == $dependency['operator']) {
                     $show = $show && $dependency['value'] >= $value ? true : $show;
                 } elseif ('<=' == $dependency['operator']) {
                     $show = $show && $dependency['value'] <= $value ? true : $show;
                 } elseif ('>' == $dependency['operator']) {
                     $show = $show && $dependency['value'] > $value ? true : $show;
                 } elseif ('<' == $dependency['operator']) {
                     $show = $show && $dependency['value'] < $value ? true : $show;
                 }
                 // if initial status is hidden then hide the control
                 if (false == $show) {
                     $script .= '$("' . $target . '").hide();';
                 }
                 $script .= '$("' . $controller . '").';
                 $script .= 'checkbox' == $type ? 'click' : 'change';
                 $script .= '(function(){';
                 $script .= 'if ($("' . $controller . '").';
                 $script .= 'checkbox' == $type ? 'is(":checked") ) {' : 'val() ' . $dependency['operator'] . ' "' . $dependency['value'] . '") {';
                 $script .= '$("' . $target . '")' . $action_1 . ';';
                 $script .= '} else {';
                 $script .= '$("' . $target . '")' . $action_2 . ';';
                 $script .= '}});';
                 $script .= 'checkbox' != $type ? '$("' . $controller . '").trigger("change");' : '';
             }
         }
     }
     // If there's a script then echo it wrapped.
     if (!empty($script)) {
         echo ScriptRegistry::prepare($script);
     }
 }