Postmessage.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Automatic postMessage scripts calculation for Kirki controls. |
| 4 | * |
| 5 | * @package kirki-framework/module-postmessage |
| 6 | * @author Themeum |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | use Kirki\URL; |
| 19 | |
| 20 | /** |
| 21 | * Adds styles to the customizer. |
| 22 | */ |
| 23 | class Postmessage |
| 24 | { |
| 25 | |
| 26 | /** |
| 27 | * The class instance. |
| 28 | * |
| 29 | * @static |
| 30 | * @access private |
| 31 | * @since 1.0.0 |
| 32 | * @var object |
| 33 | */ |
| 34 | private static $instance; |
| 35 | |
| 36 | /** |
| 37 | * An array of fields to be processed. |
| 38 | * |
| 39 | * @access protected |
| 40 | * @since 1.0.0 |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $fields = []; |
| 44 | |
| 45 | /** |
| 46 | * Get the one, true instance of this class. |
| 47 | * |
| 48 | * @static |
| 49 | * @access public |
| 50 | * @since 1.0.0 |
| 51 | * @return object |
| 52 | */ |
| 53 | public static function get_instance() |
| 54 | { |
| 55 | if ( null === self::$instance ) { |
| 56 | self::$instance = new self(); |
| 57 | } |
| 58 | return self::$instance; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructor. |
| 63 | * |
| 64 | * @access public |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public function __construct() |
| 68 | { |
| 69 | add_action('customize_preview_init', [$this, 'postmessage'], 20); |
| 70 | add_action('kirki_field_add_setting_args', [$this, 'field_add_setting_args']); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Filter setting args before adding the setting to the customizer. |
| 75 | * |
| 76 | * @access public |
| 77 | * @since 1.0.0 |
| 78 | * @param array $args The field arguments. |
| 79 | * @return array |
| 80 | */ |
| 81 | public function field_add_setting_args($args) |
| 82 | { |
| 83 | |
| 84 | if (!isset($args['transport'])) { |
| 85 | return $args; |
| 86 | } |
| 87 | |
| 88 | $args['transport'] = 'auto' === $args['transport'] ? 'postMessage' : $args['transport']; |
| 89 | |
| 90 | if ('postMessage' === $args['transport']) { |
| 91 | $args['js_vars'] = isset($args['js_vars']) ? (array) $args['js_vars'] : []; |
| 92 | $args['output'] = isset($args['output']) ? (array) $args['output'] : []; |
| 93 | $js_vars = $args['js_vars']; |
| 94 | |
| 95 | // Try to auto-generate js_vars. |
| 96 | // First we need to check if js_vars are empty, and that output is not empty. |
| 97 | if (empty($args['js_vars']) && !empty($args['output'])) { |
| 98 | |
| 99 | // Convert to array of arrays if needed. |
| 100 | if (isset($args['output']['element'])) { |
| 101 | /* translators: The field ID where the error occurs. */ |
| 102 | _doing_it_wrong(__METHOD__, sprintf(esc_html__('"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki'), esc_html($args['settings'])), '3.0.10'); |
| 103 | |
| 104 | $args['output'] = array($args['output']); |
| 105 | } |
| 106 | |
| 107 | foreach ($args['output'] as $output) { |
| 108 | $output['element'] = isset($output['element']) ? $output['element'] : ':root'; |
| 109 | $output['element'] = is_array($output['element']) ? implode(',', $output['element']) : $output['element']; |
| 110 | $output['function'] = isset($output['function']) ? $output['function'] : 'style'; |
| 111 | $js_vars[] = $output; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $args['js_vars'] = $js_vars; |
| 116 | } |
| 117 | |
| 118 | $this->fields[] = $args; |
| 119 | |
| 120 | return $args; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Enqueues the postMessage script |
| 126 | * and adds variables to it using the wp_localize_script function. |
| 127 | * The rest is handled via JS. |
| 128 | */ |
| 129 | public function postmessage() |
| 130 | { |
| 131 | |
| 132 | |
| 133 | $fields = $this->fields; |
| 134 | |
| 135 | // Compatibility with v3 API. |
| 136 | if (class_exists('\Kirki\Compatibility\Kirki')) { |
| 137 | $fields = array_merge(\Kirki\Compatibility\Kirki::$fields, $fields); |
| 138 | } |
| 139 | |
| 140 | $data = []; |
| 141 | |
| 142 | foreach ($fields as $field) { |
| 143 | if (isset($field['transport']) && 'postMessage' === $field['transport'] && isset($field['js_vars']) && !empty($field['js_vars']) && is_array($field['js_vars']) && isset($field['settings'])) { |
| 144 | $data[] = $field; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | wp_localize_script('kirki-customizer', 'kirkiPostMessageFields', $data); |
| 149 | |
| 150 | $extras = apply_filters('kirki_postmessage_script', false); |
| 151 | |
| 152 | if ($extras) { |
| 153 | wp_add_inline_script('kirki-customizer', $extras, 'after'); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | } |