Repository navigation
Expand file tree
/
Copy pathscripts.php
More file actions
324 lines (269 loc) · 7.65 KB
/
Copy pathscripts.php
File metadata and controls
324 lines (269 loc) · 7.65 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Drop-in Name: Scripts Manager
* Description: Add custom scripts to the head and footer of your pages. Offers sitewide scripts and the ability to add scripts to specific posts and pages from the post editor.
* Author: Alex, Kolakube
* Author URI: https://marketersdelight.com/
* Drop-in URI: https://marketersdelight.com/dropins/scripts/
* Drop-in Slug: scripts
* Text Domain: md-scripts
* Version: 2.0
* Requires at least: 6.2
* Requires PHP: 7.4
* @since MD4.4.2
*/
class md_scripts extends md_api {
/**
* Run actions and filters.
*
* @since 4.4.2
*/
public function actions() {
add_action( 'wp_head', array( $this, 'wp_head' ) );
add_action( 'wp_footer', array( $this, 'wp_footer' ), 100 );
add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ) );
add_filter( 'body_class', array( $this, 'body_class' ) );
if ( is_admin() )
add_action( 'md_hook_admin_after_tools', array( $this, 'admin_fields' ) );
}
/**
* Register various admin settings.
*
* @since 5.0
*/
public function register() {
$this->name = __( 'Scripts', 'md-scripts' );
return array(
'admin_page' => array(
'name' => $this->name,
'group' => 'page_settings',
'position' => 30,
'fields' => $this->fields()
),
'meta_box' => array(
'name' => $this->name,
'child_of' => 'page_settings',
'fields' => $this->fields()
),
'term' => array(
'name' => $this->name,
'group' => 'page_settings',
'fields' => $this->fields()
)
);
}
/**
* Register settings to save.
*
* @since 4.4.2
*/
public function fields() {
$scripts = $this->dequeue_data( 'ids' );
$save = array(
'body_class' => array( 'type' => 'text' ),
'header_scripts' => array( 'type' => 'code' ),
'footer_scripts' => array( 'type' => 'code' )
);
if ( ! empty( $scripts ) )
$save['scripts_manager'] = array(
'type' => 'checkbox',
'options' => $scripts
);
return $save;
}
/**
* Filter body classes.
*
* @since 4.1
* @moved 5.6
*/
public function body_class( $classes ) {
$string = md_setting( array( 'scripts', 'body_class' ) );
$single = md_module( array( 'scripts', 'body_class' ) );
if ( $single )
$string .= " $single";
if ( $string ) {
$string = explode( ' ' , $string );
foreach ( $string as $class )
$classes[] = esc_attr( $class );
}
return $classes;
}
/**
* Add settings template and script to Page Settings sections.
*
* @since 5.6
*/
public function admin_fields() { ?>
<div class="md-widget md-toggle md-sep-small">
<h3 class="md-widget-title"><?php echo esc_html( $this->name ); ?></h3>
<div class="md-widget-item">
<?php $this->admin_template(); ?>
</div>
</div>
<?php }
/**
* Meta box fields.
*
* @since 5.0
*/
public function meta_box() {
echo "<div class=\"md-$this->_clean_id md-tab-content\">";
$this->admin_template();
echo '</div>';
}
/**
* Admin settings controls.
*
* @since 4.4.2
*/
public function admin_template() {
$scripts = $this->dequeue_data( 'labels' );
$screen = get_current_screen();
?>
<div class="md-sep-small">
<?php $this->fields->field( 'body_class', array(
'type' => 'text',
'label' => __( 'Body classes', 'md-scripts' ),
'description' => __( 'Add custom CSS classes to the <code>body</code> tag.', 'md-scripts' )
) ); ?>
</div>
<div class="md-sep-small">
<?php $this->fields->field( 'header_scripts', array(
'type' => 'code',
'label' => __( 'Header Scripts', 'md-scripts' ),
'description' => __( 'Print scripts to the <code><head></code> section (before opening <code><body></code> tag).', 'md-scripts' )
) ); ?>
</div>
<div class="md-sep-small">
<?php $this->fields->field( 'footer_scripts', array(
'type' => 'code',
'label' => __( 'Footer Scripts', 'md-scripts' ),
'description' => __( 'Print scripts after the <code><footer></code> section (before closing <code></body></code> tag).', 'md-scripts' )
) ); ?>
</div>
<?php if ( $screen->base !== 'toplevel_page_md_settings' && ! empty( $scripts ) ) : ?>
<div class="md-sep-small">
<?php $this->fields->field( 'scripts_manager', array(
'type' => 'checkbox',
'label' => __( 'Scripts Optimization', 'md-scripts' ),
'description' => __( '<b>Note:</b> Only remove the scripts and styles you know this page does not use, otherwise you may break some plugin functionality.', 'md-scripts' ),
'options' => $scripts
) ); ?>
</div>
<?php endif; ?>
<?php }
/**
* Run dequeue actions on specified pages.
*
* @since 4.9.4
*/
public function dequeue_scripts() {
$scripts = $this->dequeue_data();
if ( ! empty( $scripts ) ) {
if ( is_singular() ) {
$meta = md_post_meta( array( 'scripts', 'scripts_manager' ) );
if ( ! empty( $meta ) )
$this->dequeue_action( $meta );
}
if ( is_category() || is_tax() ) {
$tax = md_term_meta( array( 'scripts', 'scripts_manager' ) );
if ( ! empty( $tax ) )
$this->dequeue_action( $tax );
}
}
}
/**
* Call this action to dequeue specified scripts and styles.
*
* @since 4.9.4
*/
public function dequeue_action( $source ) {
foreach ( $this->dequeue_data() as $plugin => $assets )
if ( ! empty( $source[$plugin] ) ) {
if ( isset( $assets['styles'] ) )
foreach ( $assets['styles'] as $style )
wp_dequeue_style( $style );
if ( isset( $assets['scripts'] ) )
foreach ( $assets['scripts'] as $script )
wp_dequeue_script( $script );
}
}
/**
* Compile scripts data into different formats from filter.
*
* @since 4.4.2
*/
public function dequeue_data( $sort = null ) {
$scripts = apply_filters( 'md_filter_dequeue_scripts', array() );
if ( ! empty( $scripts ) )
if ( $sort == 'ids' ) {
$ids = array();
foreach ( $scripts as $plugin => $assets )
$ids[] = $plugin;
return $ids;
}
elseif ( $sort == 'labels' ) {
$labels = array();
foreach ( $scripts as $plugin => $assets )
$labels[$plugin] = $assets['label'];
return $labels;
}
return $scripts;
}
/**
* Add scripts to header.
*
* @since 4.4.2
*/
public function wp_head() {
$scripts = '';
$key = array( 'scripts', 'header_scripts' );
$scripts .= md_setting( array( 'scripts', 'header_scripts' ) );
if ( is_singular() )
$scripts .= md_post_meta( $key );
elseif ( is_post_type_archive() || is_home() )
$scripts .= md_post_type_field( $key );
elseif ( is_category() || is_tax() )
$scripts .= md_term_meta( $key );
if ( $scripts )
echo html_entity_decode( $scripts );
}
/**
* Add scripts to footer.
*
* @since 4.4.2
*/
public function wp_footer() {
$scripts = '';
$key = array( 'scripts', 'footer_scripts' );
$google = md_setting( array( 'integrations', 'api_keys', 'google_analytics', 'key' ) );
if ( $google )
$this->google_analytics( $google );
$scripts .= md_setting( array( 'scripts', 'footer_scripts' ) );
if ( is_singular() )
$scripts .= md_post_meta( $key );
elseif ( is_post_type_archive() || is_home() )
$scripts .= md_post_type_field( $key );
elseif ( is_category() || is_tax() )
$scripts .= md_term_meta( $key );
if ( $scripts )
echo html_entity_decode( $scripts );
}
/**
* Google Analytics Tracking Code.
*
* @since 4.9
*/
public function google_analytics( $key ) { ?>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $key ); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo esc_attr( $key ); ?>');
</script>
<?php }
}
new md_scripts;