public function generate_script() { global $wp_customize; // Early exit if we're not in the customizer if (!isset($wp_customize)) { return; } // Get an array of all the fields $fields = Kirki::$fields; $script = ''; // Parse the fields and create the script. foreach ($fields as $field) { $field['transport'] = Kirki_Field::sanitize_transport($field); $field['js_vars'] = Kirki_Field::sanitize_js_vars($field); if (!is_null($field['js_vars']) && 'postMessage' == $field['transport']) { foreach ($field['js_vars'] as $js_vars) { $units = !empty($js_vars['units']) ? " + '" . $js_vars['units'] . "'" : ''; $prefix = !empty($js_vars['prefix']) ? "'" . $js_vars['prefix'] . "' + " : ''; $script .= 'wp.customize( \'' . Kirki_Field::sanitize_settings($field) . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; if ('html' == $js_vars['function']) { $script .= '$(\'' . $js_vars['element'] . '\').html( newval );'; } else { $script .= '$(\'' . $js_vars['element'] . '\').' . $js_vars['function'] . '(\'' . $js_vars['property'] . '\', ' . $prefix . 'newval' . $units . ' );'; } $script .= '}); });'; } } } return $script; }
public function generate_script() { global $wp_customize; // Early exit if we're not in the customizer if (!isset($wp_customize)) { return; } // Get an array of all the fields $fields = Kirki::$fields; $script = 'jQuery( "#kirki-styles-inline-css" ).remove();'; $styles = array(); // Parse the fields and create the script. foreach ($fields as $field) { $field['transport'] = Kirki_Field::sanitize_transport($field); $field['js_vars'] = Kirki_Field::sanitize_js_vars($field); if (!empty($field['setting'])) { $inline_id = 'kirki-' . $field['setting']; $styles[] = '<style id="' . esc_attr($inline_id) . '">' . Kirki_Output::generate_css_by_fields(array($field)) . '</style>'; } if ('postMessage' == $field['transport']) { $script .= 'wp.customize( \'' . Kirki_Field::sanitize_settings($field) . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; if (!is_null($field['js_vars'])) { foreach ($field['js_vars'] as $js_vars) { 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' . (!empty($js_vars['units']) ? ' + \'' . $js_vars['units'] . "'" : '') . ' );'; } } } else { $output_unit = ''; if (!empty($field['output']) && !empty($field['output']['units'])) { $output_unit = $field['output']['units']; } $inline_id = 'kirki-' . $field['setting']; $placeholder_inline_css = Kirki_Output::generate_css_by_fields(array($field), true); $script .= 'jQuery( "#' . $inline_id . '" )[0].innerHTML = "' . str_replace(array('{value}', '>'), array('" + newval + "' . $output_unit, '>'), esc_js($placeholder_inline_css)) . '";'; } $script .= '}); });'; } } return array($script, $styles); }
public function test_sanitize_js_vars() { $this->assertEquals(null, Kirki_Field::sanitize_js_vars(array())); $this->assertEquals(null, Kirki_Field::sanitize_js_vars(array('js_vars' => 'foo'))); $this->assertEquals(array(array('element' => '#main', 'function' => 'css', 'property' => 'color', 'units' => '', 'prefix' => '')), Kirki_Field::sanitize_js_vars(array('js_vars' => array('element' => '#main', 'function' => 'css', 'property' => 'color')))); $this->assertEquals(array(array('element' => 'body > #main', 'function' => 'css', 'property' => 'font-size', 'units' => 'px', 'prefix' => '')), Kirki_Field::sanitize_js_vars(array('js_vars' => array(array('element' => 'body > #main', 'function' => 'css', 'property' => 'font-size', 'units' => 'px'))))); }