| 1 |
<?php |
| 2 |
/** |
| 3 |
* Scripts |
| 4 |
* |
| 5 |
* @package AutomatorWP\Scripts |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register frontend scripts |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function automatorwp_register_scripts() { |
| 19 |
|
| 20 |
// Use minified libraries if SCRIPT_DEBUG is turned off |
| 21 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 22 |
|
| 23 |
// Stylesheets |
| 24 |
wp_register_style( 'automatorwp-admin-bar-css', AUTOMATORWP_URL . 'assets/css/automatorwp-admin-bar' . $suffix . '.css', array( ), AUTOMATORWP_VER, 'all' ); |
| 25 |
|
| 26 |
// Scripts |
| 27 |
wp_register_script( 'automatorwp-redirect-js', AUTOMATORWP_URL . 'assets/js/automatorwp-redirect' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, true ); |
| 28 |
|
| 29 |
} |
| 30 |
add_action( 'init', 'automatorwp_register_scripts' ); |
| 31 |
|
| 32 |
/** |
| 33 |
* Enqueue frontend scripts |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
function automatorwp_enqueue_scripts( $hook = null ) { |
| 39 |
|
| 40 |
// Redirect script |
| 41 |
if( automatorwp_is_action_in_use( 'automatorwp_redirect_user' ) || automatorwp_is_action_in_use( 'redirect_redirect_user' ) ) { |
| 42 |
|
| 43 |
$excluded_urls = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Filter to let plugins exclude URLs from the redirect check |
| 47 |
* |
| 48 |
* @since 1.6.9 |
| 49 |
* |
| 50 |
* @param array $excluded_urls |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
$excluded_urls = apply_filters( 'automatorwp_redirect_excluded_urls', $excluded_urls ); |
| 55 |
|
| 56 |
$excluded_data = array(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Filter to let plugins exclude request data from the redirect check |
| 60 |
* |
| 61 |
* @since 1.6.9 |
| 62 |
* |
| 63 |
* @param array $excluded_data |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
$excluded_data = apply_filters( 'automatorwp_redirect_excluded_data', $excluded_data ); |
| 68 |
|
| 69 |
$excluded_ajax_actions = array( |
| 70 |
'automatorwp_check_for_redirect', |
| 71 |
// GamiPress Notifications support |
| 72 |
'gamipress_notifications_get_notices', |
| 73 |
'gamipress_notifications_last_check', |
| 74 |
// GamiPress Congratulations Popups support |
| 75 |
'gamipress_congratulations_popups_get_popups', |
| 76 |
'gamipress_congratulations_popups_popup_shown', |
| 77 |
// WooCommerce support |
| 78 |
'woocommerce_load_products', |
| 79 |
// BuddyBoss support |
| 80 |
'buddyboss_load_user_profile', |
| 81 |
'buddyboss_theme_get_header_unread_messages', |
| 82 |
'buddyboss_theme_get_header_notifications', |
| 83 |
// WordPress support |
| 84 |
'heartbeat', |
| 85 |
); |
| 86 |
|
| 87 |
/** |
| 88 |
* Filter to let plugins exclude ajax actions from the redirect check |
| 89 |
* |
| 90 |
* @since 1.6.9 |
| 91 |
* |
| 92 |
* @param array $excluded_ajax_actions |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
$excluded_ajax_actions = apply_filters( 'automatorwp_redirect_excluded_ajax_actions', $excluded_ajax_actions ); |
| 97 |
|
| 98 |
wp_localize_script( 'automatorwp-redirect-js', 'automatorwp_redirect', array( |
| 99 |
'ajaxurl' => esc_url( admin_url( 'admin-ajax.php', 'relative' ) ), |
| 100 |
'nonce' => automatorwp_get_nonce(), |
| 101 |
'user_id' => get_current_user_id(), |
| 102 |
'excluded_urls' => $excluded_urls, |
| 103 |
'excluded_data' => $excluded_data, |
| 104 |
'excluded_ajax_actions' => $excluded_ajax_actions, |
| 105 |
) ); |
| 106 |
|
| 107 |
wp_enqueue_script( 'automatorwp-redirect-js' ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
add_action( 'wp_enqueue_scripts', 'automatorwp_enqueue_scripts' ); |
| 113 |
|
| 114 |
/** |
| 115 |
* Register admin scripts |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
function automatorwp_admin_register_scripts() { |
| 121 |
|
| 122 |
// Use minified libraries if SCRIPT_DEBUG is turned off |
| 123 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 124 |
|
| 125 |
// Libraries |
| 126 |
wp_register_style( 'automatorwp-select2-css', AUTOMATORWP_URL . 'assets/libs/select2/css/select2' . $suffix . '.css', array( ), AUTOMATORWP_VER, 'all' ); |
| 127 |
wp_register_script( 'automatorwp-select2-js', AUTOMATORWP_URL . 'assets/js/automatorwp-select2' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, true ); |
| 128 |
|
| 129 |
wp_register_script( 'automatorwp-select2-dropdown-position-js', AUTOMATORWP_URL . 'assets/libs/select2-dropdownPosition/select2-dropdownPosition' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, true ); |
| 130 |
|
| 131 |
// Stylesheets |
| 132 |
wp_register_style( 'automatorwp-admin-css', AUTOMATORWP_URL . 'assets/css/automatorwp-admin' . $suffix . '.css', array( ), AUTOMATORWP_VER, 'all' ); |
| 133 |
wp_register_style( 'automatorwp-admin-rtl-css', AUTOMATORWP_URL . 'assets/css/automatorwp-admin-rtl' . $suffix . '.css', array( ), AUTOMATORWP_VER, 'all' ); |
| 134 |
|
| 135 |
// Scripts |
| 136 |
wp_register_script( 'automatorwp-admin-functions-js', AUTOMATORWP_URL . 'assets/js/automatorwp-admin-functions' . $suffix . '.js', array( 'jquery', 'jquery-ui-dialog' ), AUTOMATORWP_VER, true ); |
| 137 |
wp_register_script( 'automatorwp-admin-js', AUTOMATORWP_URL . 'assets/js/automatorwp-admin' . $suffix . '.js', array( 'jquery', 'jquery-ui-sortable', 'automatorwp-admin-functions-js', 'automatorwp-select2-js' ), AUTOMATORWP_VER, true ); |
| 138 |
wp_register_script( 'automatorwp-admin-notices-js', AUTOMATORWP_URL . 'assets/js/automatorwp-admin-notices' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, false ); |
| 139 |
wp_register_script( 'automatorwp-admin-add-ons-js', AUTOMATORWP_URL . 'assets/js/automatorwp-admin-add-ons' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, true ); |
| 140 |
wp_register_script( 'automatorwp-import-automation-js', AUTOMATORWP_URL . 'assets/js/automatorwp-import-automation' . $suffix . '.js', array( 'jquery' ), AUTOMATORWP_VER, true ); |
| 141 |
|
| 142 |
} |
| 143 |
add_action( 'admin_init', 'automatorwp_admin_register_scripts' ); |
| 144 |
|
| 145 |
/** |
| 146 |
* Enqueue admin scripts |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
* |
| 150 |
* @param string $hook |
| 151 |
* |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
function automatorwp_admin_enqueue_scripts( $hook ) { |
| 155 |
|
| 156 |
// Stylesheets |
| 157 |
wp_enqueue_style( 'automatorwp-admin-css' ); |
| 158 |
wp_enqueue_style( 'automatorwp-admin-rtl-css' ); |
| 159 |
|
| 160 |
// Localize admin script |
| 161 |
wp_localize_script( 'automatorwp-admin-notices-js', 'automatorwp_admin_notices', array( |
| 162 |
'nonce' => automatorwp_get_admin_nonce(), |
| 163 |
) ); |
| 164 |
|
| 165 |
// Scripts |
| 166 |
wp_enqueue_script( 'automatorwp-admin-notices-js' ); |
| 167 |
|
| 168 |
// Import automation scripts |
| 169 |
if( $hook === 'automatorwp_page_automatorwp_import_automation' ) { |
| 170 |
wp_localize_script( 'automatorwp-import-automation-js', 'automatorwp_import_automation', array( |
| 171 |
'nonce' => automatorwp_get_admin_nonce(), |
| 172 |
) ); |
| 173 |
|
| 174 |
wp_enqueue_script( 'automatorwp-import-automation-js' ); |
| 175 |
} |
| 176 |
|
| 177 |
// Add-ons scripts |
| 178 |
if( $hook === 'automatorwp_page_automatorwp_add_ons' ) { |
| 179 |
wp_enqueue_script( 'automatorwp-admin-add-ons-js' ); |
| 180 |
} |
| 181 |
|
| 182 |
$allow_enqueue = true; |
| 183 |
|
| 184 |
|
| 185 |
$allowed_hooks = array( |
| 186 |
'automatorwp_page_automatorwp_automations', // Automations list |
| 187 |
'admin_page_edit_automatorwp_automations', // Automation edit |
| 188 |
'automatorwp_page_automatorwp_logs', // Logs list |
| 189 |
'admin_page_edit_automatorwp_logs', // Log edit |
| 190 |
'automatorwp_page_automatorwp_add_ons', // Add-ons page |
| 191 |
'automatorwp_page_automatorwp_licenses', // Licenses page |
| 192 |
); |
| 193 |
|
| 194 |
// Prevent to enqueue scripts outside our pages |
| 195 |
if( ! in_array( $hook, $allowed_hooks ) ) { |
| 196 |
$allow_enqueue = false; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Filter to enqueue admin scripts |
| 201 |
* |
| 202 |
* @since 1.0.0 |
| 203 |
* |
| 204 |
* @param bool $allow_enqueue |
| 205 |
* @param string $hook |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
$allow_enqueue = apply_filters( 'automatorwp_allow_enqueue_admin_scripts', $allow_enqueue, $hook ); |
| 210 |
|
| 211 |
if( ! $allow_enqueue ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
automatorwp_enqueue_admin_functions_script(); |
| 216 |
|
| 217 |
// Enqueue editor assets |
| 218 |
wp_enqueue_editor(); |
| 219 |
wp_enqueue_media(); |
| 220 |
|
| 221 |
// Localize admin script |
| 222 |
wp_localize_script( 'automatorwp-admin-js', 'automatorwp_admin', array( |
| 223 |
'nonce' => automatorwp_get_admin_nonce(), |
| 224 |
'save_text' => __( 'Save', 'automatorwp' ), |
| 225 |
'saving_text' => __( 'Saving', 'automatorwp' ), |
| 226 |
'never_expires_text' => __( 'Never Expires', 'automatorwp' ), |
| 227 |
) ); |
| 228 |
|
| 229 |
// Scripts |
| 230 |
wp_enqueue_script( 'automatorwp-admin-js' ); |
| 231 |
|
| 232 |
} |
| 233 |
add_action( 'admin_enqueue_scripts', 'automatorwp_admin_enqueue_scripts' ); |
| 234 |
|
| 235 |
/** |
| 236 |
* Enqueue the admin functions script with all required components |
| 237 |
* |
| 238 |
* @since 1.0.0 |
| 239 |
* @return void |
| 240 |
*/ |
| 241 |
function automatorwp_enqueue_admin_functions_script() { |
| 242 |
|
| 243 |
// Enqueue Select2 library |
| 244 |
wp_enqueue_script( 'automatorwp-select2-js' ); |
| 245 |
wp_enqueue_style( 'automatorwp-select2-css' ); |
| 246 |
|
| 247 |
// Enqueue Select2 dropdown position |
| 248 |
wp_enqueue_script( 'automatorwp-select2-dropdown-position-js' ); |
| 249 |
|
| 250 |
// Setup an array of post type labels to use on post selector field |
| 251 |
$post_types = get_post_types( array(), 'objects' ); |
| 252 |
$post_type_labels = array(); |
| 253 |
|
| 254 |
foreach( $post_types as $key => $obj ) { |
| 255 |
$post_type_labels[$key] = $obj->labels->singular_name; |
| 256 |
} |
| 257 |
|
| 258 |
// Setup an array of taxonomy labels to use on the taxonomy selector field |
| 259 |
$taxonomies = get_taxonomies( array(), 'objects' ); |
| 260 |
$taxonomy_labels = array(); |
| 261 |
|
| 262 |
foreach( $taxonomies as $key => $obj ) { |
| 263 |
$taxonomy_labels[$key] = $obj->labels->singular_name; |
| 264 |
} |
| 265 |
|
| 266 |
// Localize admin functions script |
| 267 |
wp_localize_script( 'automatorwp-admin-functions-js', 'automatorwp_admin_functions', array( |
| 268 |
'nonce' => automatorwp_get_admin_nonce(), |
| 269 |
'post_type_labels' => $post_type_labels, |
| 270 |
'taxonomy_labels' => $taxonomy_labels, |
| 271 |
// Selector placeholders |
| 272 |
'selector_placeholder' => __( 'Select an option', 'automatorwp' ), |
| 273 |
'post_selector_placeholder' => __( 'Select a post', 'automatorwp' ), |
| 274 |
'term_selector_placeholder' => __( 'Select a term', 'automatorwp' ), |
| 275 |
/* translators: %s: Taxonomy title (category, tag, etc). */ |
| 276 |
'taxonomy_selector_placeholder_pattern' => __( 'Select a %s', 'automatorwp' ), |
| 277 |
'user_selector_placeholder' => __( 'Select a user', 'automatorwp' ), |
| 278 |
'object_selector_placeholder' => __( 'Select an item', 'automatorwp' ), |
| 279 |
) ); |
| 280 |
|
| 281 |
wp_enqueue_script( 'automatorwp-admin-functions-js' ); |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Register and enqueue admin bar scripts |
| 287 |
* |
| 288 |
* @since 1.3.2 |
| 289 |
* @return void |
| 290 |
*/ |
| 291 |
function automatorwp_enqueue_admin_bar_scripts() { |
| 292 |
|
| 293 |
wp_enqueue_style( 'automatorwp-admin-bar-css' ); |
| 294 |
|
| 295 |
} |
| 296 |
add_action( 'admin_bar_init', 'automatorwp_enqueue_admin_bar_scripts' ); |
| 297 |
|
| 298 |
/** |
| 299 |
* CMB2 script dependencies |
| 300 |
* |
| 301 |
* @since 1.0.0 |
| 302 |
* |
| 303 |
* @param string $hook |
| 304 |
* |
| 305 |
* @return void |
| 306 |
*/ |
| 307 |
function automatorwp_cmb2_script_dependencies( $dependencies ) { |
| 308 |
|
| 309 |
// Bail if not is a custom page |
| 310 |
if( ! isset( $_GET['page'] ) ) { |
| 311 |
return $dependencies; |
| 312 |
} |
| 313 |
|
| 314 |
// Bail if not is the edit automation page |
| 315 |
if( $_GET['page'] !== 'edit_automatorwp_automations' ) { |
| 316 |
return $dependencies; |
| 317 |
} |
| 318 |
|
| 319 |
// Force to enqueue datetimepicker lib since triggers and actions forms are loaded through ajax |
| 320 |
if ( ! isset( $dependencies['jquery-ui-datetimepicker'] ) ) { |
| 321 |
$dependencies['jquery-ui-datetimepicker'] = 'jquery-ui-datetimepicker'; |
| 322 |
} |
| 323 |
|
| 324 |
// Force to enqueue media-editor actions forms are loaded through ajax |
| 325 |
if ( ! isset( $dependencies['media-editor'] ) ) { |
| 326 |
$dependencies['media-editor'] = 'media-editor'; |
| 327 |
} |
| 328 |
|
| 329 |
return $dependencies; |
| 330 |
|
| 331 |
} |
| 332 |
add_filter( 'cmb2_script_dependencies', 'automatorwp_cmb2_script_dependencies' ); |
| 333 |
|
| 334 |
/** |
| 335 |
* Setup a global nonce for all frontend scripts |
| 336 |
* |
| 337 |
* @since 1.0.0 |
| 338 |
* |
| 339 |
* @return string |
| 340 |
*/ |
| 341 |
function automatorwp_get_nonce() { |
| 342 |
|
| 343 |
if( ! defined( 'AUTOMATORWP_NONCE' ) ) |
| 344 |
define( 'AUTOMATORWP_NONCE', wp_create_nonce( 'automatorwp' ) ); |
| 345 |
|
| 346 |
return AUTOMATORWP_NONCE; |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Setup a global nonce for all admin scripts |
| 352 |
* |
| 353 |
* @since 1.0.0 |
| 354 |
* |
| 355 |
* @return string |
| 356 |
*/ |
| 357 |
function automatorwp_get_admin_nonce() { |
| 358 |
|
| 359 |
if( ! defined( 'AUTOMATORWP_ADMIN_NONCE' ) ) |
| 360 |
define( 'AUTOMATORWP_ADMIN_NONCE', wp_create_nonce( 'automatorwp_admin' ) ); |
| 361 |
|
| 362 |
return AUTOMATORWP_ADMIN_NONCE; |
| 363 |
|
| 364 |
} |
| 365 |
|