ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-kirki-scripts-tooltips.php (3086B)


      1 <?php
      2 /**
      3  * Injects tooltips to controls when the 'tooltip' argument is used.
      4  *
      5  * @package     Kirki
      6  * @category    Core
      7  * @author      Aristeides Stathopoulos
      8  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
      9  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     10  * @since       2.0
     11  */
     12 
     13 // Exit if accessed directly.
     14 if ( ! defined( 'ABSPATH' ) ) {
     15 	exit;
     16 }
     17 
     18 if ( ! class_exists( 'Kirki_Scripts_Tooltips' ) ) {
     19 
     20 	/**
     21 	 * Adds script for tooltips.
     22 	 */
     23 	class Kirki_Scripts_Tooltips {
     24 
     25 		/**
     26 		 * The script generated for ALL fields
     27 		 *
     28 		 * @static
     29 		 * @access public
     30 		 * @var string
     31 		 */
     32 		public static $tooltip_script = '';
     33 
     34 		/**
     35 		 * Whether the script has already been added to the customizer or not.
     36 		 *
     37 		 * @static
     38 		 * @access public
     39 		 * @var bool
     40 		 */
     41 		public static $script_added = false;
     42 
     43 		/**
     44 		 * The class constructor
     45 		 */
     46 		public function __construct() {
     47 			add_action( 'customize_controls_print_footer_scripts', array( $this, 'enqueue_script' ) );
     48 		}
     49 
     50 		/**
     51 		 * Generates the scripts needed for tooltips.
     52 		 * This works on a per-field basis.
     53 		 * Once created, the script is added to the $tooltip_script property.
     54 		 *
     55 		 * @param array $args The field definition.
     56 		 * @return void
     57 		 */
     58 		public static function generate_script( $args = array() ) {
     59 
     60 			/**
     61 			 * The following control types already have the "tooltip" argument in them
     62 			 * and they don't need an extra implementation in order to be rendered.
     63 			 * We're going to ignore these control-types and only process the rest.
     64 			 */
     65 			$ready_controls = array(
     66 				'checkbox',
     67 				'code',
     68 				'color-alpha',
     69 				'custom',
     70 				'dimension',
     71 				'editor',
     72 				'multicheck',
     73 				'number',
     74 				'palette',
     75 				'radio-buttonset',
     76 				'radio-image',
     77 				'radio',
     78 				'kirki-radio',
     79 				'repeater',
     80 				'select',
     81 				'kirki-select',
     82 				'select2',
     83 				'select2-multiple',
     84 				'slider',
     85 				'sortable',
     86 				'spacing',
     87 				'switch',
     88 				'textarea',
     89 				'toggle',
     90 				'typography',
     91 			);
     92 
     93 			/**
     94 			 * Make sure the field-type has been defined.
     95 			 * If it has not been defined the we don't know what to do with it and should exit.
     96 			 * No error is displayed, we just won't do anything.
     97 			 */
     98 			if ( isset( $args['type'] ) && in_array( $args['type'], $ready_controls ) ) {
     99 				return;
    100 			}
    101 
    102 			$script = '';
    103 			if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) {
    104 				$content = "<a href='#' class='tooltip hint--left' data-hint='" . wp_strip_all_tags( $args['tooltip'] ) . "'><span class='dashicons dashicons-info'></span></a>";
    105 				$script  = '$( "' . $content . '" ).prependTo( "#customize-control-' . $args['settings'] . '" );';
    106 			}
    107 
    108 			self::$tooltip_script .= $script;
    109 
    110 		}
    111 
    112 		/**
    113 		 * Format the script in a way that will be compatible with WordPress.
    114 		 */
    115 		public function enqueue_script() {
    116 			if ( ! self::$script_added && '' != self::$tooltip_script ) {
    117 				self::$script_added = true;
    118 				echo '<script>jQuery(document).ready(function($) { "use strict"; ' . wp_kses_post( self::$tooltip_script ) . '});</script>';
    119 			}
    120 		}
    121 	}
    122 }