| 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 |
/** |
| 77 |
* Enqueue editor-only js and css (Enqueue scripts (only on Edit Post Page)) |
| 78 |
*/ |
| 79 |
public function enqueueEditorAssets() { |
| 80 |
|
| 81 |
global $pagenow; |
| 82 |
|
| 83 |
$dependencies = array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api', 'wp-api-fetch', 'lodash' ); |
| 84 |
|
| 85 |
if ( $pagenow && $pagenow === 'widgets.php' ) { |
| 86 |
array_push( $dependencies, 'wp-edit-widgets' ); |
| 87 |
} else { |
| 88 |
array_push( $dependencies, 'wp-editor' ); |
| 89 |
} |
| 90 |
|
| 91 |
// Enqueue the bundled block JS file |
| 92 |
wp_enqueue_script( |
| 93 |
"{$this->prefix}-blocks-editor-js", |
| 94 |
getwid_get_plugin_url( 'assets/js/editor.blocks.js' ), |
| 95 |
apply_filters( |
| 96 |
'getwid/editor_blocks_js/dependencies', |
| 97 |
$dependencies |
| 98 |
), |
| 99 |
$this->version, |
| 100 |
true |
| 101 |
); |
| 102 |
|
| 103 |
//disabled blocks |
| 104 |
$disabledBlocks = []; |
| 105 |
$disabledBlocksData = []; |
| 106 |
if ( getwid()->blocksManager()->hasDisabledBlocks() ) { |
| 107 |
$disabledBlocks = getwid()->blocksManager()->getDisabledBlocks(); |
| 108 |
foreach ( $disabledBlocks as $block ) { |
| 109 |
$disabledBlocksData[] = $block->getBlockName(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
wp_localize_script( |
| 114 |
"{$this->prefix}-blocks-editor-js", |
| 115 |
'Getwid', |
| 116 |
apply_filters( |
| 117 |
'getwid/editor_blocks_js/localize_data', |
| 118 |
[ |
| 119 |
'disabled_blocks' => $disabledBlocksData, |
| 120 |
'settings' => [ |
| 121 |
'wide_support' => get_theme_support( 'align-wide' ), |
| 122 |
'date_time_utc' => current_time('Y-m-d H:i:s'), |
| 123 |
'post_type' => get_post_type(), |
| 124 |
'google_api_key' => get_option( 'getwid_google_api_key', '' ), |
| 125 |
'instagram_token_isset' => (bool) get_option( 'getwid_instagram_token', '' ), |
| 126 |
|
| 127 |
'assets_path' => getwid_get_plugin_url( '/assets' ), |
| 128 |
'image_sizes' => $this->get_image_sizes(), |
| 129 |
|
| 130 |
'excerpt_length' => apply_filters( 'excerpt_length', 55 ), |
| 131 |
'recaptcha_site_key' => get_option( 'getwid_recaptcha_v2_site_key' , '' ), |
| 132 |
'recaptcha_secret_key' => get_option( 'getwid_recaptcha_v2_secret_key', '' ), |
| 133 |
'mailchimp_api_key' => get_option( 'getwid_mailchimp_api_key' , '' ), |
| 134 |
'debug' => ( defined( 'WP_DEBUG' ) ? WP_DEBUG : false ) |
| 135 |
], |
| 136 |
'templates' => [ |
| 137 |
'name' => getwid()->postTemplatePart()->postType, |
| 138 |
'new' => admin_url( 'post-new.php?post_type=' . getwid()->postTemplatePart()->postType ), |
| 139 |
'view' => admin_url( 'edit.php?post_type=' . getwid()->postTemplatePart()->postType ), |
| 140 |
'edit' => admin_url( 'post.php?post=' ) |
| 141 |
], |
| 142 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 143 |
'options_general_url' => admin_url( 'options-general.php' ), |
| 144 |
'get_instagram_token_url' => add_query_arg( |
| 145 |
['nonce' => wp_create_nonce('getwid_nonce_save_instagram_token') ], |
| 146 |
admin_url( 'options-general.php' ) |
| 147 |
), |
| 148 |
'options_url' => [ |
| 149 |
'general' => getwid()->settingsPage()->getTabUrl('general'), |
| 150 |
'appearance' => getwid()->settingsPage()->getTabUrl('appearance'), |
| 151 |
'blocks' => getwid()->settingsPage()->getTabUrl('blocks'), |
| 152 |
], |
| 153 |
'nonces' => array( |
| 154 |
'google_api_key' => wp_create_nonce( 'getwid_nonce_google_api_key' ), |
| 155 |
'recaptcha_v2' => wp_create_nonce( 'getwid_nonce_recaptcha_v2' ), |
| 156 |
'mailchimp_api_key' => wp_create_nonce( 'getwid_nonce_mailchimp_api_key' ), |
| 157 |
'check_instagram_token' => wp_create_nonce( 'getwid_nonce_check_instagram_token' ) |
| 158 |
), |
| 159 |
'acf_exist' => getwid_acf_is_active(), |
| 160 |
'editor_scrollable_element_class' => $this->getWPEditorScrollableElementClassName() |
| 161 |
] |
| 162 |
) |
| 163 |
); |
| 164 |
|
| 165 |
wp_set_script_translations( "{$this->prefix}-blocks-editor-js", "getwid" ); |
| 166 |
|
| 167 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 168 |
// Enqueue optional editor only styles |
| 169 |
wp_enqueue_style( |
| 170 |
"{$this->prefix}-blocks-editor", |
| 171 |
getwid_get_plugin_url( 'assets/css/blocks.editor' . $rtl . '.css' ), |
| 172 |
apply_filters( |
| 173 |
'getwid/editor_blocks_css/dependencies', |
| 174 |
[] |
| 175 |
), |
| 176 |
$this->version |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Enqueue js and css for both editor and front-end |
| 182 |
* |
| 183 |
*/ |
| 184 |
public function enqueueFrontBlockAssets() { |
| 185 |
|
| 186 |
// *** Start of Backend & Frontend *** |
| 187 |
|
| 188 |
/** |
| 189 |
* Assets optimization. Currently in Beta. |
| 190 |
* @since 1.5.3 |
| 191 |
*/ |
| 192 |
$_has_enabled_blocks = getwid()->blocksManager()->hasEnabledBlocks(); |
| 193 |
|
| 194 |
if ( $_has_enabled_blocks ) { |
| 195 |
|
| 196 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() || is_admin() ) { |
| 197 |
|
| 198 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 199 |
|
| 200 |
wp_enqueue_style( |
| 201 |
"{$this->prefix}-blocks", |
| 202 |
getwid_get_plugin_url( 'assets/css/blocks.style' . $rtl . '.css' ), |
| 203 |
|
| 204 |
// section, banner, icon-box, icon, image-box, image-hotspot, media-text-slider, video-popup, post-carousel, post-slider, images-slider |
| 205 |
/** |
| 206 |
* Filters frontend style dependencies. |
| 207 |
*/ |
| 208 |
apply_filters( |
| 209 |
'getwid/blocks_style_css/dependencies', |
| 210 |
[] |
| 211 |
), |
| 212 |
$this->version |
| 213 |
); |
| 214 |
|
| 215 |
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_section_content_width_css() ); |
| 216 |
wp_add_inline_style( "{$this->prefix}-blocks", getwid_generate_smooth_animation_css() ); |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
// *** End of Backend & Frontend *** |
| 222 |
|
| 223 |
/** |
| 224 |
* Assets optimization. Currently in Beta. |
| 225 |
* @since 1.5.3 |
| 226 |
*/ |
| 227 |
if ( is_admin() || ! $_has_enabled_blocks || ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() ) ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Filters frontend script dependencies. |
| 233 |
*/ |
| 234 |
wp_enqueue_script( |
| 235 |
"{$this->prefix}-blocks-frontend-js", |
| 236 |
getwid_get_plugin_url( 'assets/js/frontend.blocks.js' ), |
| 237 |
apply_filters( |
| 238 |
'getwid/frontend_blocks_js/dependencies', |
| 239 |
[ 'jquery' ] |
| 240 |
), |
| 241 |
$this->version, |
| 242 |
true |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
public function localizeFrontend() { |
| 247 |
wp_localize_script( |
| 248 |
"{$this->prefix}-blocks-frontend-js", |
| 249 |
'Getwid', |
| 250 |
apply_filters( |
| 251 |
'getwid/frontend_blocks_js/localize_data', |
| 252 |
[ |
| 253 |
'settings' => [], |
| 254 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 255 |
'isRTL' => is_rtl(), |
| 256 |
'nonces' => array( |
| 257 |
'contact_form' => wp_create_nonce( 'getwid_nonce_send_contact_form' ) |
| 258 |
), |
| 259 |
] |
| 260 |
) |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
public function wp_late_enqueue_scripts() { |
| 265 |
|
| 266 |
$should_enqueue_common_style = apply_filters('getwid/optimize/should_load_common_css', false); |
| 267 |
|
| 268 |
if ( TRUE == getwid()->assetsOptimization()->load_assets_on_demand() && $should_enqueue_common_style ) { |
| 269 |
|
| 270 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 271 |
|
| 272 |
wp_enqueue_style( |
| 273 |
"{$this->prefix}-blocks-common", |
| 274 |
getwid_get_plugin_url( 'assets/blocks/common.style' . $rtl . '.css' ), |
| 275 |
[], |
| 276 |
$this->version |
| 277 |
); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
public function enqueue_editor_section_css() { |
| 282 |
add_editor_style( getwid_generate_section_content_width_css() ); |
| 283 |
} |
| 284 |
|
| 285 |
private function getWPEditorScrollableElementClassName() { |
| 286 |
|
| 287 |
global $wp_version; |
| 288 |
|
| 289 |
if ( version_compare( $wp_version, '6.8', '>=' ) ) { |
| 290 |
|
| 291 |
return 'block-editor-block-canvas'; |
| 292 |
} |
| 293 |
|
| 294 |
if ( version_compare( $wp_version, '5.5', '>=' ) ) { |
| 295 |
|
| 296 |
return 'interface-interface-skeleton__content'; |
| 297 |
} |
| 298 |
|
| 299 |
if ( version_compare( $wp_version, '5.4', '>=' ) ) { |
| 300 |
|
| 301 |
return 'block-editor-editor-skeleton__content'; |
| 302 |
} |
| 303 |
|
| 304 |
if ( version_compare( $wp_version, '5.1', '>=' ) ) { |
| 305 |
|
| 306 |
return 'edit-post-layout__content'; |
| 307 |
} |
| 308 |
|
| 309 |
return ''; |
| 310 |
} |
| 311 |
|
| 312 |
} |
| 313 |
|