public function __construct($args) { /** * Run the parent class constructor */ parent::__construct($args); /** * Set the field arguments */ $this->args = $args; /** * Create the settings. */ new Kirki_Settings($this->args); /** * Check if we're on the customizer. * If we are, then we will create the controls, * add the scripts needed for the customizer * and any other tweaks that this field may require. */ if ($this->wp_customize) { /** * Create the control */ new Kirki_Control($this->args); /** * Create the scripts for postMessage to properly work */ Kirki_Customizer_Scripts_PostMessage::generate_script($this->args); /** * Create the scripts for tooltips. */ Kirki_Customizer_Scripts_Tooltips::generate_script($this->args); } }
public function test_generate_script_valid() { $postmessage = new Kirki_Customizer_Scripts_PostMessage(); /** * No script should be generated here */ Kirki_Customizer_Scripts_PostMessage::generate_script(array('settings' => 'my_setting', 'transport' => 'refresh')); $this->assertEquals(Kirki_Customizer_Scripts_PostMessage::$postmessage_script, null); /** * Generate the script normally with valid values * Use the "css" function. */ Kirki_Customizer_Scripts_PostMessage::$postmessage_script = ''; Kirki_Customizer_Scripts_PostMessage::generate_script(array('settings' => 'my_setting', 'transport' => 'postMessage', 'js_vars' => array(array('element' => 'body', 'function' => 'css', 'property' => 'font-size', 'units' => 'px')))); $this->assertEquals(Kirki_Customizer_Scripts_PostMessage::$postmessage_script, 'wp.customize( \'my_setting\', function( value ) {value.bind( function( newval ) {$(\'body\').css(\'font-size\', newval + \'px\' );}); });'); /** * Generate the script normally with valid values * Use the "html" function. */ Kirki_Customizer_Scripts_PostMessage::$postmessage_script = ''; Kirki_Customizer_Scripts_PostMessage::generate_script(array('settings' => 'my_setting', 'transport' => 'postMessage', 'js_vars' => array(array('element' => 'body', 'function' => 'html')))); $this->assertEquals(Kirki_Customizer_Scripts_PostMessage::$postmessage_script, 'wp.customize( \'my_setting\', function( value ) {value.bind( function( newval ) {$(\'body\').html( newval );}); });'); /** * Test that the final script is properly wrapped and echoed */ $this->expectOutputString('<script>jQuery(document).ready(function($) { "use strict"; wp.customize( \'my_setting\', function( value ) {value.bind( function( newval ) {$(\'body\').html( newval );}); });});</script>'); $postmessage->enqueue_script(); }