| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class ScriptsManager |
| 7 |
* @package Getwid |
| 8 |
*/ |
| 9 |
class ScriptsManager { |
| 10 |
|
| 11 |
private $version; |
| 12 |
private $prefix; |
| 13 |
|
| 14 |
/** |
| 15 |
* ScriptsManager constructor. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
|
| 19 |
$settings = getwid()->settings(); |
| 20 |
|
| 21 |
$this->version = $settings->getVersion(); |
| 22 |
$this->prefix = $settings->getPrefix(); |
| 23 |
|
| 24 |
// Fires after block assets have been enqueued for the editing interface. |
| 25 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueueEditorAssets'] ); |
| 26 |
|
| 27 |
// Fires after enqueuing block assets for both editor and front-end. |
| 28 |
add_action( 'enqueue_block_assets', [ $this, 'enqueueFrontBlockAssets' ] ); |
| 29 |
|
| 30 |
// section_content_width inline styles |
| 31 |
add_action( 'after_theme_setup', [ $this, 'enqueue_editor_section_css' ] ); |
| 32 |
|
| 33 |
add_action( 'wp_footer', [ $this, 'localizeFrontend' ] ); |
| 34 |
|
| 35 |
// Register frontend styles |
| 36 |
add_action( 'wp_footer', [ $this, 'wp_late_enqueue_scripts' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
public function get_image_sizes() { |
| 40 |
|
| 41 |
global $_wp_additional_image_sizes; |
| 42 |
|
| 43 |
$intermediate_image_sizes = get_intermediate_image_sizes(); |
| 44 |
|
| 45 |
$image_sizes = array(); |
| 46 |
foreach ( $intermediate_image_sizes as $size ) { |
| 47 |
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { |
| 48 |
$image_sizes[ $size ] = array( |
| 49 |
'width' => $_wp_additional_image_sizes[ $size ][ 'width' ], |
| 50 |
'height' => $_wp_additional_image_sizes[ $size ][ 'height' ] |
| 51 |
); |
| 52 |
} else { |
| 53 |
$image_sizes[ $size ] = array( |
| 54 |
'width' => intval( get_option( "{$size}_size_w" ) ), |
| 55 |
'height' => intval( get_option( "{$size}_size_h" ) ) |
| 56 |
); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$sizes_arr = []; |
| 61 |
foreach ( $image_sizes as $key => $value ) { |
| 62 |
$temp_arr = []; |
| 63 |
$temp_arr[ 'value' ] = $key; |
| 64 |
$temp_arr[ 'label' ] = ucwords( strtolower( preg_replace( '/[-_]/', ' ', $key ) ) ) . " - {$value['width']} x {$value['height']}"; |
| 65 |
$sizes_arr[] = $temp_arr; |
| 66 |
} |
| 67 |
|
| 68 |
$sizes_arr[] = array( |
| 69 |
'value' => 'full', |
| 70 |
'label' => __( 'Full Size', 'getwid' ) |
| 71 |
); |
| 72 |
|
| 73 |
return $sizes_arr; |
| 74 |
} |
| 75 |
|
| 76 |
public function load_locale_data() { |
| 77 |
$locale_data = $this->get_locale_data( 'getwid' ); |
| 78 |
wp_add_inline_script( |
| 79 |
'wp-i18n', |
| 80 |
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ', "'. $this->prefix .'" );' |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
public function get_locale_data( $domain ) { |
| 85 |
$translations = get_translations_for_domain( $domain ); |
| 86 |
|
| 87 |
$locale = array( |
| 88 |
'' => array( |
| 89 |
'domain' => $domain, |
| 90 |
'lang' => is_admin() ? get_user_locale() : get_locale(), |
| 91 |
), |
| 92 |
); |
| 93 |
|
| 94 |
if ( ! empty( $translations->headers[ 'Plural-Forms' ] ) ) { |
| 95 |
$locale[ '' ][ 'plural_forms' ] = $translations->headers[ 'Plural-Forms' ]; |
| 96 |
} |
| 97 |
|
| 98 |
foreach ( $translations->entries as $msgid => $entry ) { |
| 99 |
$locale[ $msgid ] = $entry->translations; |
| 100 |
} |
| 101 |
|
| 102 |
return $locale; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Enqueue editor-only js and css (Enqueue scripts (only on Edit Post Page)) |
| 107 |
*/ |
| 108 |
public function enqueueEditorAssets() { |
| 109 |
|
| 110 |
global $pagenow; |
| 111 |
|
| 112 |
$dependencies = array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api', 'wp-api-fetch', 'lodash' ); |
| 113 |
|
| 114 |
if ( $pagenow && $pagenow === 'widgets.php' ) { |
| 115 |
array_push( $dependencies, 'wp-edit-widgets' ); |
| 116 |
} else { |
| 117 |
array_push( $dependencies, 'wp-editor' ); |
| 118 |
} |
| 119 |
|
| 120 |
// Enqueue the bundled block JS file |
| 121 |
wp_enqueue_script( |
| 122 |
"{$this->prefix}-blocks-editor-js", |
| 123 |
getwid_get_plugin_url( 'assets/js/editor.blocks.js' ), |
| 124 |
apply_filters( |
| 125 |
'getwid/editor_blocks_js/dependencies', |
| 126 |
$dependencies |
| 127 |
), |
| 128 |
$this->version, |
| 129 |
true |
| 130 |
); |
| 131 |
|
| 132 |
//disabled blocks |
| 133 |
$disabledBlocks = []; |
| 134 |
$disabledBlocksData = []; |
| 135 |
if ( getwid()->blocksManager()->hasDisabledBlocks() ) { |
| 136 |
$disabledBlocks = getwid()->blocksManager()->getDisabledBlocks(); |
| 137 |
foreach ( $disabledBlocks as $block ) { |
| 138 |
$disabledBlocksData[] = $block->getBlockName(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$this->load_locale_data(); |
| 143 |
|
| 144 |
wp_localize_script( |
| 145 |
"{$this->prefix}-blocks-editor-js", |
| 146 |
'Getwid', |
| 147 |
apply_filters( |
| 148 |
'getwid/editor_blocks_js/localize_data', |
| 149 |
[ |
| 150 |
'localeData' => $this->get_locale_data( 'getwid' ), |
| 151 |
'disabled_blocks' => $disabledBlocksData, |
| 152 |
'settings' => [ |
| 153 |
'wide_support' => get_theme_support( 'align-wide' ), |
| 154 |
'date_time_utc' => current_time('Y-m-d H:i:s'), |
| 155 |
'post_type' => get_post_type(), |
| 156 |
'google_api_key' => get_option( 'getwid_google_api_key', '' ), |
| 157 |
'instagram_token_isset' => (bool) get_option( 'getwid_instagram_token', '' ), |
| 158 |
|
| 159 |
'assets_path' => getwid_get_plugin_url( '/assets' ), |
| 160 |
'image_sizes' => $this->get_image_sizes(), |
| 161 |
|
| 162 |
'excerpt_length' => apply_filters( 'excerpt_length', 55 ), |
| 163 |
'recaptcha_site_key' => get_option( 'getwid_recaptcha_v2_site_key' , '' ), |
| 164 |
'recaptcha_secret_key' => get_option( 'getwid_recaptcha_v2_secret_key', '' ), |
| 165 |
'mailchimp_api_key' => get_option( 'getwid_mailchimp_api_key' , '' ), |
| 166 |
'debug' => ( defined( 'WP_DEBUG' ) ? WP_DEBUG : false ) |
| 167 |
], |
| 168 |
'templates' => [ |
| 169 |
'name' => getwid()->postTemplatePart()->postType, |
| 170 |
'new' => admin_url( 'post-new.php?post_type=' . getwid()->postTemplatePart()->postType ), |
| 171 |
'view' => admin_url( 'edit.php?post_type=' . getwid()->postTemplatePart()->postType ), |
| 172 |
'edit' => admin_url( 'post.php?post=' ) |
| 173 |
], |
| 174 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 175 |
'options_general_url' => admin_url( 'options-general.php' ), |
| 176 |
'get_instagram_token_url' => add_query_arg( |
| 177 |
['nonce' => wp_create_nonce('getwid_nonce_save_instagram_token') ], |
| 178 |
admin_url( 'options-general.php' ) |
| 179 |
), |
| 180 |
'options_url' => [ |
| 181 |
'general' => getwid()->settingsPage()->getTabUrl('general'), |
| 182 |
'appearance' => getwid()->settingsPage()->getTabUrl('appearance'), |
| 183 |
'blocks' => getwid()->settingsPage()->getTabUrl('blocks'), |
| 184 |
], |
| 185 |
'nonces' => array( |
| 186 |
'google_api_key' => wp_create_nonce( 'getwid_nonce_google_api_key' ), |
| 187 |
'recaptcha_v2' => wp_create_nonce( 'getwid_nonce_recaptcha_v2' ), |
| 188 |
'mailchimp_api_key' => wp_create_nonce( 'getwid_nonce_mailchimp_api_key' ), |
| 189 |
'check_instagram_token' => wp_create_nonce( 'getwid_nonce_check_instagram_token' ) |
| 190 |
), |
| 191 |
'acf_exist' => getwid_acf_is_active(), |
| 192 |
] |
| 193 |
) |
| 194 |
); |
| 195 |
|
| 196 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 197 |
// Enqueue optional editor only styles |
| 198 |
wp_enqueue_style( |
| 199 |
"{$this->prefix}-blocks-editor", |
| 200 |
getwid_get_plugin_url( 'assets/css/blocks.editor' . $rtl . '.css' ), |
| 201 |
apply_filters( |
| 202 |
'getwid/editor_blocks_css/dependencies', |
| 203 |
[] |
| 204 |
), |
| 205 |
$this->version |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Enqueue js and css for both editor and front-end |
| 211 |
* |
| 212 |
*/ |
| 213 |
public function enqueueFrontBlockAssets() { |
| 214 |
|
| 215 |
// *** Start of Backend & Frontend *** |
| 216 |
|
| 217 |
/** |
| 218 |
* Assets optimization. Currently in Beta. |
| 219 |
* @since 1.5.3 |
| 220 |
*/ |
| 221 |
$_has_enabled_blocks = getwid()->blocksManager()->hasEnabledBlocks(); |
| 222 |
|
| 223 |
if ( $_has_enabled_blocks ) { |
| 224 |
|
| 225 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() || is_admin() ) { |
| 226 |
|
| 227 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 228 |
|
| 229 |
wp_enqueue_style( |
| 230 |
"{$this->prefix}-blocks", |
| 231 |
getwid_get_plugin_url( 'assets/css/blocks.style' . $rtl . '.css' ), |
| 232 |
|
| 233 |
// section, banner, icon-box, icon, image-box, image-hotspot, media-text-slider, video-popup, post-carousel, post-slider, images-slider |
| 234 |
/** |
| 235 |
* Filters frontend style dependencies. |
| 236 |
*/ |
| 237 |
apply_filters( |
| 238 |
'getwid/blocks_style_css/dependencies', |
| 239 |
[] |
| 240 |
), |
| 241 |
$this->version |
| 242 |
); |
| 243 |
|
| 244 |
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_section_content_width_css() ); |
| 245 |
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_smooth_animation_css() ); |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
// *** End of Backend & Frontend *** |
| 251 |
|
| 252 |
/** |
| 253 |
* Assets optimization. Currently in Beta. |
| 254 |
* @since 1.5.3 |
| 255 |
*/ |
| 256 |
if ( is_admin() || ! $_has_enabled_blocks || ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() ) ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Filters frontend script dependencies. |
| 262 |
*/ |
| 263 |
wp_enqueue_script( |
| 264 |
"{$this->prefix}-blocks-frontend-js", |
| 265 |
getwid_get_plugin_url( 'assets/js/frontend.blocks.js' ), |
| 266 |
apply_filters( |
| 267 |
'getwid/frontend_blocks_js/dependencies', |
| 268 |
[ 'jquery' ] |
| 269 |
), |
| 270 |
$this->version, |
| 271 |
true |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
public function localizeFrontend() { |
| 276 |
wp_localize_script( |
| 277 |
"{$this->prefix}-blocks-frontend-js", |
| 278 |
'Getwid', |
| 279 |
apply_filters( |
| 280 |
'getwid/frontend_blocks_js/localize_data', |
| 281 |
[ |
| 282 |
'settings' => [], |
| 283 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 284 |
'isRTL' => is_rtl(), |
| 285 |
'nonces' => array( |
| 286 |
'contact_form' => wp_create_nonce( 'getwid_nonce_send_contact_form' ) |
| 287 |
), |
| 288 |
] |
| 289 |
) |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
public function wp_late_enqueue_scripts() { |
| 294 |
|
| 295 |
$should_enqueue_common_style = apply_filters('getwid/optimize/should_load_common_css', false); |
| 296 |
|
| 297 |
if ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() && $should_enqueue_common_style ) { |
| 298 |
|
| 299 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 300 |
|
| 301 |
wp_enqueue_style( |
| 302 |
"{$this->prefix}-blocks-common", |
| 303 |
getwid_get_plugin_url( 'assets/blocks/common.style' . $rtl . '.css' ), |
| 304 |
[], |
| 305 |
$this->version |
| 306 |
); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
function enqueue_editor_section_css() { |
| 311 |
add_editor_style( getwid_generate_section_content_width_css() ); |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|