Selective_Refresh.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles selective refreshes for Kirki fields. |
| 4 | * |
| 5 | * @package kirki-framework/module-selective-refresh |
| 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 | /** |
| 19 | * Handle selective refreshes introduced in WordPress 4.5. |
| 20 | */ |
| 21 | class Selective_Refresh { |
| 22 | |
| 23 | /** |
| 24 | * The class instance. |
| 25 | * |
| 26 | * @static |
| 27 | * @access private |
| 28 | * @since 1.0.0 |
| 29 | * @var object |
| 30 | */ |
| 31 | private static $instance; |
| 32 | |
| 33 | /** |
| 34 | * An array of fields with selective refreshes. |
| 35 | * |
| 36 | * @static |
| 37 | * @access private |
| 38 | * @since 1.0.0 |
| 39 | * @var array |
| 40 | */ |
| 41 | private static $fields = []; |
| 42 | |
| 43 | /** |
| 44 | * Get the one, true instance of this class. |
| 45 | * |
| 46 | * @static |
| 47 | * @access public |
| 48 | * @since 1.0.0 |
| 49 | * @return object |
| 50 | */ |
| 51 | public static function get_instance() { |
| 52 | if ( null === self::$instance ) { |
| 53 | self::$instance = new self(); |
| 54 | } |
| 55 | return self::$instance; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Adds any necessary actions & filters. |
| 60 | * |
| 61 | * @access public |
| 62 | */ |
| 63 | public function __construct() { |
| 64 | add_filter( 'kirki_field_add_setting_args', [ $this, 'filter_setting_args' ], 10, 2 ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Filter setting args. |
| 69 | * |
| 70 | * @access public |
| 71 | * @since 1.0.0 |
| 72 | * @param array $field The field arguments. |
| 73 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 74 | * @return array |
| 75 | */ |
| 76 | public function filter_setting_args( $field, $wp_customize ) { |
| 77 | |
| 78 | // Abort if selective refresh is not available. |
| 79 | if ( ! isset( $wp_customize->selective_refresh ) ) { |
| 80 | return $field; |
| 81 | } |
| 82 | |
| 83 | if ( isset( $field['partial_refresh'] ) && ! empty( $field['partial_refresh'] ) ) { |
| 84 | |
| 85 | // Start going through each item in the array of partial refreshes. |
| 86 | foreach ( $field['partial_refresh'] as $partial_refresh => $partial_refresh_args ) { |
| 87 | |
| 88 | // If we have all we need, create the selective refresh call. |
| 89 | if ( isset( $partial_refresh_args['render_callback'] ) && isset( $partial_refresh_args['selector'] ) ) { |
| 90 | $partial_refresh_args = wp_parse_args( |
| 91 | $partial_refresh_args, |
| 92 | [ |
| 93 | 'settings' => $field['settings'], |
| 94 | ] |
| 95 | ); |
| 96 | $wp_customize->selective_refresh->add_partial( $partial_refresh, $partial_refresh_args ); |
| 97 | |
| 98 | // If partial refresh is set, change the transport to auto. |
| 99 | $field['transport'] = 'postMessage'; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return $field; |
| 105 | } |
| 106 | } |