Passed
Push — develop ( 725b0b...952465 )
by Aristeides
09:10 queued 04:22
created

Kirki_Modules_PostMessage::script_var_typography()   F

Complexity

Conditions 41
Paths 804

Size

Total Lines 63
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 41
eloc 46
nc 804
nop 2
dl 0
loc 63
rs 0.2722
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Automatic postMessage scripts calculation for Kirki controls.
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
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Adds styles to the customizer.
20
 */
21
class Kirki_Modules_PostMessage {
22
23
	/**
24
	 * The object instance.
25
	 *
26
	 * @static
27
	 * @access private
28
	 * @since 3.0.0
29
	 * @var object
30
	 */
31
	private static $instance;
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * @access protected
37
	 * @since 3.0.0
38
	 */
39
	protected function __construct() {
40
		add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
41
	}
42
43
	/**
44
	 * Gets an instance of this object.
45
	 * Prevents duplicate instances which avoid artefacts and improves performance.
46
	 *
47
	 * @static
48
	 * @access public
49
	 * @since 3.0.0
50
	 * @return object
51
	 */
52
	public static function get_instance() {
53
		if ( ! self::$instance ) {
54
			self::$instance = new self();
55
		}
56
		return self::$instance;
57
	}
58
59
	/**
60
	 * Enqueues the postMessage script
61
	 * and adds variables to it using the wp_localize_script function.
62
	 * The rest is handled via JS.
63
	 */
64
	public function postmessage() {
65
		wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
66
		$fields = Kirki::$fields;
67
		$data   = array();
68
		foreach ( $fields as $field ) {
69
			if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) {
70
				$data[] = $field;
71
			}
72
		}
73
		wp_localize_script( 'kirki_auto_postmessage', 'kirkiPostMessageFields', $data );
74
	}
75
}
76