1 | <?php |
||
2 | /** |
||
3 | * Handles sections created via the Kirki API. |
||
4 | * |
||
5 | * @package Kirki |
||
6 | * @category Modules |
||
7 | * @author Aristeides Stathopoulos |
||
8 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||
9 | * @license https://opensource.org/licenses/MIT |
||
10 | * @since 3.0.0 |
||
11 | */ |
||
12 | |||
13 | /** |
||
14 | * Handle selective refreshes introduced in WordPress 4.5. |
||
15 | */ |
||
16 | class Kirki_Modules_Selective_Refresh { |
||
17 | |||
18 | /** |
||
19 | * The object instance. |
||
20 | * |
||
21 | * @static |
||
22 | * @access private |
||
23 | * @since 3.0.0 |
||
24 | * @var object |
||
25 | */ |
||
26 | private static $instance; |
||
27 | |||
28 | /** |
||
29 | * Adds any necessary actions & filters. |
||
30 | * |
||
31 | * @access protected |
||
32 | */ |
||
33 | protected function __construct() { |
||
34 | add_action( 'customize_register', array( $this, 'register_partials' ), 99 ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Gets an instance of this object. |
||
39 | * Prevents duplicate instances which avoid artefacts and improves performance. |
||
40 | * |
||
41 | * @static |
||
42 | * @access public |
||
43 | * @since 3.0.0 |
||
44 | * @return object |
||
45 | */ |
||
46 | public static function get_instance() { |
||
47 | if ( ! self::$instance ) { |
||
48 | self::$instance = new self(); |
||
49 | } |
||
50 | return self::$instance; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Parses all fields and searches for the "partial_refresh" argument inside them. |
||
55 | * If that argument is found, then it starts parsing the array of arguments. |
||
56 | * Registers a selective_refresh in the customizer for each one of them. |
||
57 | * |
||
58 | * @param object $wp_customize WP_Customize_Manager. |
||
59 | */ |
||
60 | public function register_partials( $wp_customize ) { |
||
61 | |||
62 | // Abort if selective refresh is not available. |
||
63 | if ( ! isset( $wp_customize->selective_refresh ) ) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | // Get an array of all fields. |
||
68 | $fields = Kirki::$fields; |
||
69 | |||
70 | // Start parsing the fields. |
||
71 | foreach ( $fields as $field ) { |
||
72 | if ( isset( $field['partial_refresh'] ) && ! empty( $field['partial_refresh'] ) ) { |
||
73 | // Start going through each item in the array of partial refreshes. |
||
74 | foreach ( $field['partial_refresh'] as $partial_refresh => $partial_refresh_args ) { |
||
75 | // If we have all we need, create the selective refresh call. |
||
76 | if ( isset( $partial_refresh_args['render_callback'] ) && isset( $partial_refresh_args['selector'] ) ) { |
||
77 | $partial_refresh_args = wp_parse_args( |
||
78 | $partial_refresh_args, array( |
||
0 ignored issues
–
show
|
|||
79 | 'settings' => $field['settings'], |
||
80 | ) |
||
81 | ); |
||
82 | $wp_customize->selective_refresh->add_partial( $partial_refresh, $partial_refresh_args ); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 |
For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this: