Completed
Push — develop ( 2d809e...4b580b )
by Aristeides
03:12
created

Kirki_Modules_Loading::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Adds a custom loading icon when the previewer refreshes.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Modules
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       3.0.0
10
 */
11
12
/**
13
 * Modifies the loading overlay.
14
 */
15
class Kirki_Modules_Loading {
16
17
	/**
18
	 * The object instance.
19
	 *
20
	 * @static
21
	 * @access private
22
	 * @since 3.0.0
23
	 * @var object
24
	 */
25
	private static $instance;
26
27
	/**
28
	 * Constructor.
29
	 *
30
	 * @access protected
31
	 */
32
	protected function __construct() {
33
		add_action( 'init', array( $this, 'init' ) );
34
	}
35
36
	/**
37
	 * Gets an instance of this object.
38
	 * Prevents duplicate instances which avoid artefacts and improves performance.
39
	 *
40
	 * @static
41
	 * @access public
42
	 * @since 3.0.0
43
	 * @return object
44
	 */
45
	public static function get_instance() {
46
		if ( ! self::$instance ) {
47
			self::$instance = new self();
48
		}
49
		return self::$instance;
50
	}
51
52
	/**
53
	 * Runs on init.
54
	 *
55
	 * @access public
56
	 * @since 3.0.0
57
	 */
58
	public function init() {
59
60
		global $wp_customize;
61
		if ( ! $wp_customize ) {
62
			return;
63
		}
64
		// Allow disabling the custom loader using the kirki/config filter.
65
		$config = apply_filters( 'kirki/config', array() );
66
		if ( isset( $config['disable_loader'] ) && true === $config['disable_loader'] ) {
67
			return;
68
		}
69
		// Add the "loading" icon.
70
		add_action( 'wp_footer', array( $this, 'add_loader_to_footer' ) );
71
		add_action( 'wp_head', array( $this, 'add_loader_styles_to_header' ), 99 );
72
		$this->remove_default_loading_styles();
73
74
	}
75
76
	/**
77
	 * Adds a custom "loading" div $ its styles when changes are made to the customizer.
78
	 *
79
	 * @access public
80
	 */
81
	public function add_loader_to_footer() {
82
		?>
83
		<div class="kirki-customizer-loading-wrapper">
84
			<span class="kirki-customizer-loading"></span>
85
		</div>
86
		<?php
87
	}
88
89
	/**
90
	 * Adds the loader CSS to our `<head>`.
91
	 *
92
	 * @access public
93
	 */
94
	public function add_loader_styles_to_header() {
95
		?>
96
		<style>
97
			body.wp-customizer-unloading {
98
				opacity: 1;
99
				cursor: progress !important;
100
				-webkit-transition: none;
101
				transition: none;
102
			}
103
			body.wp-customizer-unloading * {
104
				pointer-events: none !important;
105
			}
106
			.kirki-customizer-loading-wrapper {
107
				width: 100%;
108
				height: 100%;
109
				position: fixed;
110
				top: 0;
111
				left: 0;
112
				background: rgba(255,255,255,0.83);
113
				z-index: 999999;
114
				display: none;
115
				opacity: 0;
116
				-webkit-transition: opacity 0.5s;
117
				transition: opacity 0.5s;
118
				background-image: url("<?php echo esc_url_raw( Kirki::$url ); ?>/assets/images/kirki-logo.svg");
119
				background-repeat: no-repeat;
120
				background-position: center center;
121
			}
122
			body.wp-customizer-unloading .kirki-customizer-loading-wrapper {
123
				display: block;
124
				opacity: 1;
125
			}
126
			.kirki-customizer-loading-wrapper .kirki-customizer-loading {
127
				position: absolute;
128
				width: 60px;
129
				height: 60px;
130
				top: 50%;
131
				left: 50%;
132
				margin: -30px;
133
				background-color: rgba(0,0,0,.83);
134
				border-radius: 100%;
135
				-webkit-animation: sk-scaleout 1.0s infinite ease-in-out;
136
				animation: sk-scaleout 1.0s infinite ease-in-out;
137
			}
138
			@-webkit-keyframes sk-scaleout {
139
				0% { -webkit-transform: scale(0) }
140
				100% {
141
					-webkit-transform: scale(1.0);
142
					opacity: 0;
143
				}
144
			}
145
			@keyframes sk-scaleout {
146
				0% {
147
					-webkit-transform: scale(0);
148
					transform: scale(0);
149
				}
150
				100% {
151
					-webkit-transform: scale(1.0);
152
					transform: scale(1.0);
153
					opacity: 0;
154
				}
155
			}
156
		</style>
157
		<?php
158
	}
159
160
	/**
161
	 * Removes the default loader styles from WP Core.
162
	 *
163
	 * @access public
164
	 */
165
	public function remove_default_loading_styles() {
166
		global $wp_customize;
167
		remove_action( 'wp_head', array( $wp_customize, 'customize_preview_loading_style' ) );
168
	}
169
}
170