| 1 |
<?php |
| 2 |
/** |
| 3 |
* FrontendScripts class file. |
| 4 |
* |
| 5 |
* @package MooWoodle |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MooWoodle; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* MooWoodle FrontendScripts class |
| 14 |
* |
| 15 |
* @class FrontendScripts class |
| 16 |
* @version 3.3.0 |
| 17 |
* @author MooWoodle |
| 18 |
*/ |
| 19 |
class FrontendScripts { |
| 20 |
|
| 21 |
/** |
| 22 |
* Cached admin settings. |
| 23 |
* |
| 24 |
* @var array|null |
| 25 |
*/ |
| 26 |
private static $settings_cache = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Cached WooCommerce account menu. |
| 30 |
* |
| 31 |
* @var array|null |
| 32 |
*/ |
| 33 |
private static $account_menu_cache = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* FrontendScripts constructor. |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) ); |
| 40 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the build path for assets based on environment. |
| 45 |
* |
| 46 |
* @return string Relative path to the build directory. |
| 47 |
*/ |
| 48 |
public static function get_asset_path( $path_type = 'url', $plugin_path = '', $plugin_url = '' ) { |
| 49 |
$build_path = 'assets/'; |
| 50 |
if ( $plugin_path === '' ) { |
| 51 |
$plugin_path = MooWoodle()->plugin_path; |
| 52 |
} |
| 53 |
if ( $plugin_url === '' ) { |
| 54 |
$plugin_url = MooWoodle()->plugin_url; |
| 55 |
} |
| 56 |
|
| 57 |
return 'file' === $path_type |
| 58 |
? $plugin_path . $build_path |
| 59 |
: $plugin_url . $build_path; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register and store a script for later use. |
| 64 |
* |
| 65 |
* @param string $handle Unique script handle. |
| 66 |
* @param string $path URL to the script file. |
| 67 |
* @param array $deps Optional. Script dependencies. Default empty array. |
| 68 |
* @param string $version Optional. Script version. Default empty string. |
| 69 |
*/ |
| 70 |
public static function register_script( $handle, $path, $deps = array(), $version = '' ) { |
| 71 |
if ( wp_script_is( $handle, 'registered' ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
wp_register_script( $handle, $path, $deps, $version, true ); |
| 75 |
wp_set_script_translations( $handle, 'moowoodle' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register and store a style for later use. |
| 80 |
* |
| 81 |
* @param string $handle Unique style handle. |
| 82 |
* @param string $path URL to the style file. |
| 83 |
* @param array $deps Optional. Style dependencies. Default empty array. |
| 84 |
* @param string $version Optional. Style version. Default empty string. |
| 85 |
*/ |
| 86 |
public static function register_style( $handle, $path, $deps = array(), $version = '' ) { |
| 87 |
if ( wp_style_is( $handle, 'registered' ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
wp_register_style( $handle, $path, $deps, $version ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register frontend scripts using filters and enqueue required external scripts. |
| 95 |
* |
| 96 |
* Loads block assets and additional scripts defined through the `moowoodle_frontend_scripts` filter. |
| 97 |
*/ |
| 98 |
public static function register_frontend_scripts() { |
| 99 |
$version = MooWoodle()->version; |
| 100 |
$vendor_asset = include self::get_asset_path( 'file' ) . 'js/vendors.asset.php'; |
| 101 |
|
| 102 |
$block_scripts = array( |
| 103 |
'my-courses', |
| 104 |
'moodle-enrollment-mapping', |
| 105 |
); |
| 106 |
|
| 107 |
$registered_scripts = apply_filters( |
| 108 |
'moowoodle_frontend_scripts', |
| 109 |
array( |
| 110 |
'moowoodle-vendor' => array( |
| 111 |
'src' => self::get_asset_path() . 'js/vendors.js', |
| 112 |
'deps' => $vendor_asset['dependencies'], |
| 113 |
), |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
foreach ( $block_scripts as $handle ) { |
| 118 |
$asset_path = self::get_asset_path( 'file' ) . "js/block/{$handle}/index.asset.php"; |
| 119 |
$asset = file_exists( $asset_path ) |
| 120 |
? include $asset_path |
| 121 |
: array( |
| 122 |
'dependencies' => array(), |
| 123 |
'version' => $version, |
| 124 |
); |
| 125 |
|
| 126 |
$registered_scripts[ "moowoodle-{$handle}" ] = array( |
| 127 |
'src' => self::get_asset_path() . "js/block/{$handle}/index.js", |
| 128 |
'deps' => $asset['dependencies'], |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
foreach ( $registered_scripts as $name => $script_config ) { |
| 133 |
self::register_script( $name, $script_config['src'], $script_config['deps'], $script_config['version'] ?? $version ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Register frontend styles using filters. |
| 139 |
* |
| 140 |
* Allows style registration through `moowoodle_frontend_styles` filter. |
| 141 |
*/ |
| 142 |
public static function register_frontend_styles() { |
| 143 |
$version = MooWoodle()->version; |
| 144 |
$registered_styles = apply_filters( |
| 145 |
'moowoodle_frontend_styles', |
| 146 |
array( |
| 147 |
'moowoodle-my-courses' => array( |
| 148 |
'src' => self::get_asset_path() . "styles/block/my-courses/index.css", |
| 149 |
), |
| 150 |
) |
| 151 |
); |
| 152 |
foreach ( $registered_styles as $name => $style_config ) { |
| 153 |
self::register_style( $name, $style_config['src'], array(), $style_config['version'] ?? $version ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Register/queue frontend scripts. |
| 159 |
*/ |
| 160 |
public static function enqueue_frontend_assets() { |
| 161 |
self::register_frontend_scripts(); |
| 162 |
self::register_frontend_styles(); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Register/queue admin scripts. |
| 167 |
*/ |
| 168 |
public static function enqueue_admin_assets() { |
| 169 |
self::register_admin_scripts(); |
| 170 |
self::register_admin_styles(); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Register admin scripts using filters. |
| 175 |
* |
| 176 |
* Loads admin-specific JavaScript assets and chunked dependencies. |
| 177 |
*/ |
| 178 |
public static function register_admin_scripts() { |
| 179 |
$version = MooWoodle()->version; |
| 180 |
// Enqueue all chunk files (External dependencies). |
| 181 |
$index_asset_path = self::get_asset_path( 'file' ) . 'js/index.asset.php'; |
| 182 |
$index_asset = file_exists( $index_asset_path ) |
| 183 |
? include $index_asset_path |
| 184 |
: array( |
| 185 |
'dependencies' => array(), |
| 186 |
'version' => $version, |
| 187 |
); |
| 188 |
|
| 189 |
$vendor_asset_path = self::get_asset_path( 'file' ) . 'js/vendors.asset.php'; |
| 190 |
$vendor_asset = file_exists( $vendor_asset_path ) |
| 191 |
? include $vendor_asset_path |
| 192 |
: array( |
| 193 |
'dependencies' => array(), |
| 194 |
'version' => $version, |
| 195 |
); |
| 196 |
|
| 197 |
$registered_scripts = apply_filters( |
| 198 |
'moowoodle_admin_scripts', |
| 199 |
array( |
| 200 |
'moowoodle-vendor' => array( |
| 201 |
'src' => self::get_asset_path() . 'js/vendors.js', |
| 202 |
'deps' => $vendor_asset['dependencies'], |
| 203 |
), |
| 204 |
'moowoodle-admin' => array( |
| 205 |
'src' => self::get_asset_path() . 'js/index.js', |
| 206 |
'deps' => $index_asset['dependencies'], |
| 207 |
), |
| 208 |
) |
| 209 |
); |
| 210 |
foreach ( $registered_scripts as $name => $script_config ) { |
| 211 |
self::register_script( $name, $script_config['src'], $script_config['deps'], $script_config['version'] ?? $version ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Register admin styles using filters. |
| 217 |
* |
| 218 |
* Allows style registration through `moowoodle_admin_styles` filter. |
| 219 |
*/ |
| 220 |
public static function register_admin_styles() { |
| 221 |
$version = MooWoodle()->version; |
| 222 |
$registered_styles = apply_filters( |
| 223 |
'moowoodle_admin_styles', |
| 224 |
array( |
| 225 |
'moowoodle-index' => array( |
| 226 |
'src' => self::get_asset_path() . 'styles/index.css', |
| 227 |
), |
| 228 |
'moowoodle-moodle-enrollment-mapping' => array( |
| 229 |
'src' => self::get_asset_path() . 'styles/block/moodle-enrollment-mapping/index.css', |
| 230 |
), |
| 231 |
), |
| 232 |
); |
| 233 |
|
| 234 |
foreach ( $registered_styles as $name => $style_config ) { |
| 235 |
self::register_style( $name, $style_config['src'], array(), $style_config['version'] ?? $version ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
public static function get_admin_settings() { |
| 240 |
if ( null !== self::$settings_cache ) { |
| 241 |
return self::$settings_cache; |
| 242 |
} |
| 243 |
|
| 244 |
$settings = array(); |
| 245 |
|
| 246 |
$tabs_names = apply_filters( |
| 247 |
'moowoodle_additional_tabs_names', |
| 248 |
array_keys( Util::MOOWOODLE_SETTINGS ) |
| 249 |
); |
| 250 |
|
| 251 |
foreach ( $tabs_names as $tab_name ) { |
| 252 |
$option_name = str_replace( '-', '_', 'moowoodle_' . $tab_name . '_settings' ); |
| 253 |
$settings[ $tab_name ] = MooWoodle()->setting->get_option( $option_name ); |
| 254 |
} |
| 255 |
|
| 256 |
self::$settings_cache = $settings; |
| 257 |
return self::$settings_cache; |
| 258 |
} |
| 259 |
|
| 260 |
public static function get_account_menu() { |
| 261 |
if ( null !== self::$account_menu_cache ) { |
| 262 |
return self::$account_menu_cache; |
| 263 |
} |
| 264 |
|
| 265 |
$account_menu_items = wc_get_account_menu_items(); |
| 266 |
unset( $account_menu_items['my-courses'] ); |
| 267 |
|
| 268 |
self::$account_menu_cache = $account_menu_items; |
| 269 |
return self::$account_menu_cache; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Localize all scripts. |
| 274 |
* |
| 275 |
* @param string $handle Script handle the data will be attached to. |
| 276 |
*/ |
| 277 |
public static function localize_scripts( $handle ) { |
| 278 |
global $post; |
| 279 |
if ( $post ) { |
| 280 |
$wordpress_course_id = get_post_meta( $post->ID, Util::MOOWOODLE_PRODUCT_META['wordpress_course_id'], true ); |
| 281 |
$wordpress_cohort_id = apply_filters( 'moowoodle_get_wordpress_cohort_id', null, $post->ID ); |
| 282 |
$default_type = $wordpress_course_id ? 'course' : ( $wordpress_cohort_id ? 'cohort' : '' ); |
| 283 |
$selected_id = $wordpress_course_id ? $wordpress_course_id : $wordpress_cohort_id; |
| 284 |
} |
| 285 |
|
| 286 |
$base_rest = array( |
| 287 |
'apiUrl' => untrailingslashit( get_rest_url() ), |
| 288 |
'restUrl' => MooWoodle()->rest_namespace, |
| 289 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 290 |
); |
| 291 |
|
| 292 |
$settings_data = array( |
| 293 |
'admin_settings' => self::get_admin_settings(), |
| 294 |
); |
| 295 |
|
| 296 |
$localize_scripts = |
| 297 |
array( |
| 298 |
'moowoodle-admin' => array( |
| 299 |
'object_name' => 'appLocalizer', |
| 300 |
'use_rest' => true, |
| 301 |
'use_settings' => true, |
| 302 |
'data' => array( |
| 303 |
'khali_dabba' => Util::is_khali_dabba(), |
| 304 |
'shop_url' => MOOWOODLE_PRO_SHOP_URL, |
| 305 |
'video_url' => MOOWOODLE_YOUTUBE_VIDEO_URL, |
| 306 |
'chat_url' => MOOWOODLE_CHAT_URL, |
| 307 |
'account_menu' => self::get_account_menu(), |
| 308 |
'tab_name' => __( 'MooWoodle', 'moowoodle' ), |
| 309 |
'wc_email_url' => admin_url( '/admin.php?page=wc-settings&tab=email§ion=enrollmentemail' ), |
| 310 |
'moodle_site_url' => MooWoodle()->setting->get_setting( 'moodle_url' ), |
| 311 |
'wp_user_roles' => wp_roles()->get_names(), |
| 312 |
'free_version' => MooWoodle()->version, |
| 313 |
'pro_data' => apply_filters( |
| 314 |
'moowoodle_update_pro_data', |
| 315 |
array( |
| 316 |
'version' => false, |
| 317 |
'manage_plan_url' => MOOWOODLE_PRO_SHOP_URL, |
| 318 |
) |
| 319 |
), |
| 320 |
'md_user_roles' => array( |
| 321 |
1 => __( 'Manager', 'moowoodle' ), |
| 322 |
2 => __( 'Course creator', 'moowoodle' ), |
| 323 |
3 => __( 'Teacher', 'moowoodle' ), |
| 324 |
4 => __( 'Non-editing teacher', 'moowoodle' ), |
| 325 |
5 => __( 'Student', 'moowoodle' ), |
| 326 |
7 => __( 'Authenticated user', 'moowoodle' ), |
| 327 |
), |
| 328 |
'date_format' => Util::wp_to_react_date_format( get_option( 'date_format' ) ), |
| 329 |
), |
| 330 |
), |
| 331 |
'moowoodle-my-courses' => array( |
| 332 |
'object_name' => 'courseMyAcc', |
| 333 |
'use_rest' => true, |
| 334 |
), |
| 335 |
'moowoodle-moodle-enrollment-mapping' => array( |
| 336 |
'object_name' => 'moowoodleProduct', |
| 337 |
'use_rest' => true, |
| 338 |
'data' => apply_filters( |
| 339 |
'moowoodle_moodle_enrollment_mapping_data', |
| 340 |
array( |
| 341 |
'khali_dabba' => MooWoodle()->util->is_khali_dabba(), |
| 342 |
'postId' => $post ? $post->ID : '', |
| 343 |
'linkType' => $default_type ?? '', |
| 344 |
'linkedItemId' => $selected_id ?? '', |
| 345 |
'productMetaNonce' => wp_create_nonce( 'product_meta_nonce' ), |
| 346 |
'syncUrl' => esc_url( admin_url( 'admin.php?page=moowoodle#&tab=synchronization&subtab=synchronize-course' ) ), |
| 347 |
), |
| 348 |
$post |
| 349 |
), |
| 350 |
), |
| 351 |
); |
| 352 |
|
| 353 |
$localize_scripts = apply_filters( 'moowoodle_localize_scripts', $localize_scripts ); |
| 354 |
$config = $localize_scripts[ $handle ] ?? array(); |
| 355 |
|
| 356 |
$localized_data = array(); |
| 357 |
$localized_data = array_merge( |
| 358 |
! empty( $config['use_rest'] ) ? $base_rest : array(), |
| 359 |
! empty( $config['use_settings'] ) ? $settings_data : array(), |
| 360 |
$config['data'] ?? array() |
| 361 |
); |
| 362 |
|
| 363 |
if ( ! empty( $config['object_name'] ) ) { |
| 364 |
self::localize_script( $handle, $config['object_name'], $localized_data ); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Localizes a registered script with data for use in JavaScript. |
| 370 |
* |
| 371 |
* @param string $handle Script handle the data will be attached to. |
| 372 |
* @param string $name JavaScript object name. |
| 373 |
* @param array $localized_data Data to be made available in JavaScript. |
| 374 |
*/ |
| 375 |
public static function localize_script( $handle, $name, $localized_data = array() ) { |
| 376 |
wp_localize_script( $handle, $name, $localized_data ); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Enqueues a registered script. |
| 381 |
* |
| 382 |
* @param string $handle Handle of the registered script to enqueue. |
| 383 |
*/ |
| 384 |
public static function enqueue_script( $handle ) { |
| 385 |
wp_enqueue_script( $handle ); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Enqueues a registered style. |
| 390 |
* |
| 391 |
* @param string $handle Handle of the registered style to enqueue. |
| 392 |
*/ |
| 393 |
public static function enqueue_style( $handle ) { |
| 394 |
wp_enqueue_style( $handle ); |
| 395 |
} |
| 396 |
} |
| 397 |
|