-
Notifications
You must be signed in to change notification settings - Fork 84
/
Main.php
308 lines (276 loc) · 6.72 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* Main entry point for Header Footer Grid
*
* Name: Header Footer Grid
* Author: Bogdan Preda <[email protected]>
*
* @version 1.0.0
* @package HFG
*/
namespace HFG;
use HFG\Core\Builder\Abstract_Builder;
use HFG\Core\Css_Generator;
use HFG\Core\Customizer;
use HFG\Core\Settings\Config;
use HFG\Core\Settings\Manager;
use HFG\Traits\Core;
use PHP_CodeSniffer\Tokenizers\CSS;
/**
* Class Main
*
* @package HFG
*/
class Main {
use Core;
/**
* Template relative directory.
*/
const TEMPLATES_DIRECTORY = 'header-footer-grid/templates/';
/**
* Define version constant used for assets.
*/
const VERSION = '2.0.0';
/**
* Holds the instance of this class.
*
* @since 1.0.0
* @access private
* @var Main $_instance
*/
private static $_instance = null;
/**
* Holds a reference to Settings class
*
* @since 1.0.0
* @access private
* @var Manager $settings
*/
private $settings;
/**
* Cache is active filter result.
*
* @var bool Is active?
*/
private static $is_active = true;
/**
* Teamplates locations.
*
* @var array Header templates locations.
*/
private static $templates_location = [];
/**
* Holds a reference to Customizer class
*
* @since 1.0.0
* @access private
* @var Customizer $settings
*/
private $customizer;
/**
* Dynamic CSS generator.
*
* @since 2.7.0
* @var Css_Generator Dynamic CSS generator.
*/
private $css_generator;
/**
* Main Instance
* Ensures only one instance of class is loaded or can be loaded.
*
* @return \HFG\Main|null Instance.
* @since 1.0.0
* @access public
*/
public static function get_instance() {
if ( null === self::$_instance && self::$is_active ) {
if ( ! apply_filters( 'hfg_active', true ) ) {
self::$is_active = false;
return null;
}
self::$_instance = new self();
self::$_instance->init();
self::$_instance->settings = new Manager();
self::$_instance->customizer = new Customizer();
self::$_instance->css_generator = new Css_Generator();
$default_directories = [];
if ( is_child_theme() ) {
$default_directories[] = get_stylesheet_directory() . '/' . self::TEMPLATES_DIRECTORY;
}
$default_directories[] = get_template_directory() . '/' . self::TEMPLATES_DIRECTORY;
self::$templates_location = apply_filters( 'hfg_template_locations', $default_directories );
}
return self::$_instance;
}
/**
* Initializes the class
*
* @since 1.0.0
* @access public
*/
public function init() {
add_filter( 'neve_style_subscribers', array( $this, 'inline_styles' ) );
if ( ! neve_is_new_builder() ) {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_utils_scripts' ) );
}
}
/**
* JS utility scripts for admin
*
* @since 1.0.1
* @access public
*/
public function admin_utils_scripts() {
$layout_data = self::get_instance()->get_builder( 'footer' )->get_layout_data();
$footer_top = ( isset( $layout_data['desktop']['top'] ) ) ? $layout_data['desktop']['top'] : array();
$footer_bottom = ( isset( $layout_data['desktop']['bottom'] ) ) ? $layout_data['desktop']['bottom'] : array();
$footer_rows = array_merge( wp_list_pluck( $footer_top, 'id' ), wp_list_pluck( $footer_bottom, 'id' ) );
$sidebars_to_search = array(
'footer-one-widgets',
'footer-two-widgets',
'footer-three-widgets',
'footer-four-widgets',
);
$hide = '';
foreach ( $sidebars_to_search as $id ) {
if ( ! in_array( $id, $footer_rows, true ) ) {
$hide .= '$("#' . $id . '").parent().hide();';
}
}
$hide_widgets = '
( function( $ ) {
setTimeout(function() {
' . $hide . '
}, 300);
})(jQuery)
';
wp_add_inline_script( 'jquery', $hide_widgets );
}
/**
* Load template.
*
* @param string $slug Template slug.
* @param string $name Template variation.
* @param array $args Component arguments.
*/
public function load( $slug, $name = '', $args = [] ) {
$templates = array();
$name = (string) $name;
if ( $name !== '' ) {
$templates[] = "{$slug}-{$name}.php";
}
$templates[] = "{$slug}.php";
$located = '';
if ( count( self::$templates_location ) === 1 && count( $templates ) === 1 ) {
load_template( self::$templates_location[0] . $templates[0], false, $args );
return;
}
foreach ( self::$templates_location as $location ) {
foreach ( $templates as $template ) {
if ( is_file( $location . $template ) ) {
$located = $location . $template;
break;
}
}
if ( '' !== $located ) {
break;
}
}
if ( '' !== $located ) {
load_template( $located, false, $args );
}
}
/**
* Generate inline style CSS from array.
*
* @param array $subscribers Subscribers list.
*
* @return array
* @since 1.0.0
* @access public
*/
public function inline_styles( $subscribers ) {
if ( is_customize_preview() ) {
return $subscribers;
}
$css_array = [];
/**
* An instance of Abstract_Builder.
*
* @var Abstract_Builder $builder
*/
foreach ( $this->get_builders() as $builder ) {
$css_array = $builder->add_style( $css_array );
}
return array_merge( $subscribers, $css_array );
}
/**
* Return builders list, or builder details.
*
* @return Abstract_Builder[] Builder array(s).
*/
public function get_builders() {
return $this->customizer->get_builders();
}
/**
* Return builders list, or builder details.
*
* @param string $builder Builder name, if required.
*
* @return Abstract_Builder Builder array(s).
*/
public function get_builder( $builder = '' ) {
if ( empty( $builder ) ) {
$builder = Abstract_Builder::get_current_builder();
}
return $this->customizer->get_builders( $builder );
}
/**
* Cloning is forbidden.
*
* @access public
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, '', '1.0.0' );
}
/**
* Un-serializing instances of this class is forbidden.
*
* @access public
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, '', '1.0.0' );
}
/**
* Check if assets should be enqueued.
*
* @return bool
*/
private function should_enqueue_assets() {
$disabled_templates = array( 'elementor_canvas' );
$current_template = get_page_template_slug();
if ( in_array( $current_template, $disabled_templates, true ) ) {
return false;
}
if ( is_singular() ) {
$id = get_the_ID();
$header = get_post_meta( $id, 'neve_meta_disable_header', true );
$footer = get_post_meta( $id, 'neve_meta_disable_footer', true );
if ( $header === 'on' && $footer === 'on' ) {
return false;
}
}
return true;
}
}
/**
* Function to return Main instance.
*
* @return Main
* @since 1.0.0
*/
function hfg() {
return Main::get_instance();
}