scripts-manager.php
9 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
<?php
namespace Getwid;
/**
* Class ScriptsManager
* @package Getwid
*/
class ScriptsManager {
private $version;
private $prefix;
/**
* ScriptsManager constructor.
*/
public function __construct() {
$settings = getwid()->settings();
$this->version = $settings->getVersion();
$this->prefix = $settings->getPrefix();
// Fires after block assets have been enqueued for the editing interface.
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueueEditorAssets'] );
// Fires after enqueuing block assets for both editor and front-end.
add_action( 'enqueue_block_assets', [ $this, 'enqueueFrontBlockAssets' ] );
// section_content_width inline styles
add_action( 'after_theme_setup', [ $this, 'enqueue_editor_section_css' ] );
add_action( 'wp_footer', [ $this, 'localizeFrontend' ] );
// Register frontend styles
add_action( 'wp_footer', [ $this, 'wp_late_enqueue_scripts' ] );
}
public function get_image_sizes() {
global $_wp_additional_image_sizes;
$intermediate_image_sizes = get_intermediate_image_sizes();
$image_sizes = array();
foreach ( $intermediate_image_sizes as $size ) {
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$image_sizes[ $size ] = array(
'width' => $_wp_additional_image_sizes[ $size ][ 'width' ],
'height' => $_wp_additional_image_sizes[ $size ][ 'height' ]
);
} else {
$image_sizes[ $size ] = array(
'width' => intval( get_option( "{$size}_size_w" ) ),
'height' => intval( get_option( "{$size}_size_h" ) )
);
}
}
$sizes_arr = [];
foreach ( $image_sizes as $key => $value ) {
$temp_arr = [];
$temp_arr[ 'value' ] = $key;
$temp_arr[ 'label' ] = ucwords( strtolower( preg_replace( '/[-_]/', ' ', $key ) ) ) . " - {$value['width']} x {$value['height']}";
$sizes_arr[] = $temp_arr;
}
$sizes_arr[] = array(
'value' => 'full',
'label' => __( 'Full Size', 'getwid' )
);
return $sizes_arr;
}
public function load_locale_data() {
$locale_data = $this->get_locale_data( 'getwid' );
wp_add_inline_script(
'wp-i18n',
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ', "'. $this->prefix .'" );'
);
}
public function get_locale_data( $domain ) {
$translations = get_translations_for_domain( $domain );
$locale = array(
'' => array(
'domain' => $domain,
'lang' => is_admin() ? get_user_locale() : get_locale(),
),
);
if ( ! empty( $translations->headers[ 'Plural-Forms' ] ) ) {
$locale[ '' ][ 'plural_forms' ] = $translations->headers[ 'Plural-Forms' ];
}
foreach ( $translations->entries as $msgid => $entry ) {
$locale[ $msgid ] = $entry->translations;
}
return $locale;
}
/**
* Enqueue editor-only js and css (Enqueue scripts (only on Edit Post Page))
*/
public function enqueueEditorAssets() {
global $pagenow;
$dependencies = array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api', 'wp-api-fetch' );
if ( $pagenow && $pagenow === 'widgets.php' ) {
array_push( $dependencies, 'wp-edit-widgets' );
} else {
array_push( $dependencies, 'wp-editor' );
}
// Enqueue the bundled block JS file
wp_enqueue_script(
"{$this->prefix}-blocks-editor-js",
getwid_get_plugin_url( 'assets/js/editor.blocks.js' ),
apply_filters(
'getwid/editor_blocks_js/dependencies',
$dependencies
),
$this->version,
true
);
//disabled blocks
$disabledBlocks = [];
$disabledBlocksData = [];
if ( getwid()->blocksManager()->hasDisabledBlocks() ) {
$disabledBlocks = getwid()->blocksManager()->getDisabledBlocks();
foreach ( $disabledBlocks as $block ) {
$disabledBlocksData[] = $block->getBlockName();
}
}
$this->load_locale_data();
wp_localize_script(
"{$this->prefix}-blocks-editor-js",
'Getwid',
apply_filters(
'getwid/editor_blocks_js/localize_data',
[
'localeData' => $this->get_locale_data( 'getwid' ),
'disabled_blocks' => $disabledBlocksData,
'settings' => [
'wide_support' => get_theme_support( 'align-wide' ),
'date_time_utc' => current_time('Y-m-d H:i:s'),
'post_type' => get_post_type(),
'google_api_key' => get_option( 'getwid_google_api_key', '' ),
'instagram_token' => get_option( 'getwid_instagram_token', '' ),
'assets_path' => getwid_get_plugin_url( '/assets' ),
'image_sizes' => $this->get_image_sizes(),
'excerpt_length' => apply_filters( 'excerpt_length', 55 ),
'recaptcha_site_key' => get_option( 'getwid_recaptcha_v2_site_key' , '' ),
'recaptcha_secret_key' => get_option( 'getwid_recaptcha_v2_secret_key', '' ),
'mailchimp_api_key' => get_option( 'getwid_mailchimp_api_key' , '' ),
'debug' => ( defined( 'WP_DEBUG' ) ? WP_DEBUG : false )
],
'templates' => [
'name' => getwid()->postTemplatePart()->postType,
'new' => admin_url( 'post-new.php?post_type=' . getwid()->postTemplatePart()->postType ),
'view' => admin_url( 'edit.php?post_type=' . getwid()->postTemplatePart()->postType ),
'edit' => admin_url( 'post.php?post=' )
],
'ajax_url' => admin_url( 'admin-ajax.php' ),
'options_general_url' => admin_url( 'options-general.php' ),
'get_instagram_token_url' => add_query_arg(
['nonce' => wp_create_nonce('getwid_nonce_save_instagram_token') ],
admin_url( 'options-general.php' )
),
'options_url' => [
'general' => getwid()->settingsPage()->getTabUrl('general'),
'appearance' => getwid()->settingsPage()->getTabUrl('appearance'),
'blocks' => getwid()->settingsPage()->getTabUrl('blocks'),
],
'nonces' => array(
'google_api_key' => wp_create_nonce( 'getwid_nonce_google_api_key' ),
'recaptcha_v2_contact_form' => wp_create_nonce( 'getwid_nonce_contact_form' ),
'mailchimp_api_key' => wp_create_nonce( 'getwid_nonce_mailchimp_api_key' ),
'get_instagram_token' => wp_create_nonce( 'getwid_nonce_get_instagram_token' )
),
'acf_exist' => getwid_acf_is_active(),
]
)
);
$rtl = is_rtl() ? '.rtl' : '';
// Enqueue optional editor only styles
wp_enqueue_style(
"{$this->prefix}-blocks-editor",
getwid_get_plugin_url( 'assets/css/blocks.editor' . $rtl . '.css' ),
apply_filters(
'getwid/editor_blocks_css/dependencies',
[]
),
$this->version
);
}
/**
* Enqueue js and css for both editor and front-end
*
*/
public function enqueueFrontBlockAssets() {
// *** Start of Backend & Frontend ***
/**
* Assets optimization. Currently in Beta.
* @since 1.5.3
*/
$_has_enabled_blocks = getwid()->blocksManager()->hasEnabledBlocks();
if ( $_has_enabled_blocks ) {
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() || is_admin() ) {
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
"{$this->prefix}-blocks",
getwid_get_plugin_url( 'assets/css/blocks.style' . $rtl . '.css' ),
// section, banner, icon-box, icon, image-box, image-hotspot, media-text-slider, video-popup, post-carousel, post-slider, images-slider
apply_filters(
'getwid/blocks_style_css/dependencies',
[]
),
$this->version
);
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_section_content_width_css() );
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_smooth_animation_css() );
}
}
// *** End of Backend & Frontend ***
/**
* Assets optimization. Currently in Beta.
* @since 1.5.3
*/
if ( is_admin() || ! $_has_enabled_blocks || ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() ) ) {
return;
}
wp_enqueue_script(
"{$this->prefix}-blocks-frontend-js",
getwid_get_plugin_url( 'assets/js/frontend.blocks.js' ),
apply_filters(
'getwid/frontend_blocks_js/dependencies',
[ 'jquery' ]
),
$this->version,
true
);
}
public function localizeFrontend() {
wp_localize_script(
"{$this->prefix}-blocks-frontend-js",
'Getwid',
apply_filters(
'getwid/frontend_blocks_js/localize_data',
[
'settings' => [],
'ajax_url' => admin_url( 'admin-ajax.php' ),
'isRTL' => is_rtl(),
'nonces' => array(
'recaptcha_v2_contact_form' => wp_create_nonce( 'getwid_nonce_contact_form' )
),
]
)
);
}
public function wp_late_enqueue_scripts() {
$should_enqueue_common_style = apply_filters('getwid/optimize/should_load_common_css', false);
if ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() && $should_enqueue_common_style ) {
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
"{$this->prefix}-blocks-common",
getwid_get_plugin_url( 'assets/blocks/common.style' . $rtl . '.css' ),
[],
$this->version
);
}
}
function enqueue_editor_section_css() {
add_editor_style( getwid_generate_section_content_width_css() );
}
}