Нет описания

Assets.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. /**
  6. * Class PUM_Admin_Assets
  7. *
  8. * @since 1.7.0
  9. */
  10. class PUM_Admin_Assets {
  11. /**
  12. * @var
  13. */
  14. public static $suffix;
  15. /**
  16. * @var
  17. */
  18. public static $js_url;
  19. /**
  20. * @var
  21. */
  22. public static $css_url;
  23. /**
  24. * @var bool Use minified libraries if SCRIPT_DEBUG is turned off.
  25. */
  26. public static $debug;
  27. /**
  28. * Initialize
  29. */
  30. public static function init() {
  31. self::$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
  32. self::$suffix = self::$debug ? '' : '.min';
  33. self::$js_url = Popup_Maker::$URL . 'assets/js/';
  34. self::$css_url = Popup_Maker::$URL . 'assets/css/';
  35. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_admin_scripts' ) );
  36. add_action( 'admin_print_footer_scripts', array( __CLASS__, 'maybe_localize_and_templates' ), - 1 );
  37. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_admin_styles' ), 100 );
  38. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'fix_broken_extension_scripts' ), 100 );
  39. }
  40. public static function fix_broken_extension_scripts() {
  41. if ( wp_script_is( 'pum-mci-admin' ) && class_exists( 'PUM_MCI' ) && version_compare( PUM_MCI::$VER, '1.3.0', '<' ) && ! pum_is_settings_page() ) {
  42. wp_dequeue_script( 'pum-mci-admin' );
  43. }
  44. }
  45. /**
  46. * Load Admin Scripts
  47. */
  48. public static function register_admin_scripts() {
  49. $admin_vars = apply_filters( 'pum_admin_vars', apply_filters( 'pum_admin_var', array(
  50. 'post_id' => ! empty( $_GET['post'] ) ? intval( $_GET['post'] ) : null,
  51. 'pm_dir_url' => Popup_Maker::$URL,
  52. 'default_provider' => pum_get_option( 'newsletter_default_provider', 'none' ),
  53. 'homeurl' => home_url(),
  54. 'I10n' => array(
  55. 'preview_popup' => __( 'Preview', 'popup-maker' ),
  56. 'add' => __( 'Add', 'popup-maker' ),
  57. 'save' => __( 'Save', 'popup-maker' ),
  58. 'update' => __( 'Update', 'popup-maker' ),
  59. 'insert' => __( 'Insert', 'popup-maker' ),
  60. 'cancel' => __( 'Cancel', 'popup-maker' ),
  61. 'confirm_delete_trigger' => __( "Are you sure you want to delete this trigger?", 'popup-maker' ),
  62. 'confirm_delete_cookie' => __( "Are you sure you want to delete this cookie?", 'popup-maker' ),
  63. 'no_cookie' => __( 'None', 'popup-maker' ),
  64. 'confirm_count_reset' => __( 'Are you sure you want to reset the open count?', 'popup-maker' ),
  65. 'shortcode_ui_button_tooltip' => __( 'Popup Maker Shortcodes', 'popup-maker' ),
  66. 'error_loading_shortcode_preview' => __( 'There was an error in generating the preview', 'popup-maker' ),
  67. ),
  68. ) ) );
  69. wp_register_script( 'pum-admin-general', self::$js_url . 'admin-general' . self::$suffix . '.js', array( 'jquery', 'wp-color-picker', 'jquery-ui-slider', 'wp-util' ), Popup_Maker::$VER, true );
  70. wp_localize_script( 'pum-admin-general', 'pum_admin_vars', $admin_vars );
  71. wp_register_script( 'pum-admin-batch', self::$js_url . 'admin-batch' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  72. wp_register_script( 'pum-admin-marketing', self::$js_url . 'admin-marketing' . self::$suffix . '.js', null, Popup_Maker::$VER, true );
  73. wp_register_script( 'pum-admin-popup-editor', self::$js_url . 'admin-popup-editor' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  74. wp_register_script( 'pum-admin-theme-editor', self::$js_url . 'admin-theme-editor' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  75. wp_register_script( 'pum-admin-settings-page', self::$js_url . 'admin-settings-page' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  76. wp_register_script( 'pum-admin-shortcode-ui', self::$js_url . 'admin-shortcode-ui' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  77. wp_register_script( 'iframe-resizer', self::$js_url . 'vendor/iframeResizer.min.js', array( 'jquery' ) );
  78. // @deprecated handle. Currently loads empty file and admin-general as dependency.
  79. wp_register_script( 'popup-maker-admin', self::$js_url . 'pum-admin-deprecated' . self::$suffix . '.js', array( 'pum-admin-general' ), Popup_Maker::$VER, true );
  80. wp_localize_script( 'pum-admin-general', 'pum_admin', $admin_vars );
  81. wp_enqueue_script( 'pum-admin-marketing' );
  82. if ( PUM_Utils_Upgrades::instance()->has_uncomplete_upgrades() ) {
  83. wp_enqueue_script( 'pum-admin-batch' );
  84. }
  85. if ( pum_is_all_popups_page() ) {
  86. wp_enqueue_script( 'pum-admin-general' );
  87. }
  88. if ( pum_is_popup_editor() ) {
  89. wp_enqueue_script( 'pum-admin-popup-editor' );
  90. }
  91. if ( pum_is_popup_theme_editor() ) {
  92. wp_enqueue_script( 'pum-admin-theme-editor' );
  93. wp_localize_script( 'pum-admin-theme-editor', 'pum_google_fonts', PUM_Integration_GoogleFonts::fetch_fonts() );
  94. }
  95. if ( pum_is_settings_page() ) {
  96. wp_enqueue_script( 'pum-admin-settings-page' );
  97. }
  98. if ( pum_is_support_page() ) {
  99. wp_enqueue_script( 'iframe-resizer' );
  100. }
  101. }
  102. /**
  103. *
  104. */
  105. public static function maybe_localize_and_templates() {
  106. if ( wp_script_is( 'pum-admin-general' ) || wp_script_is( 'popup-maker-admin' ) ) {
  107. // Register Templates.
  108. PUM_Admin_Templates::init();
  109. }
  110. if ( wp_script_is( 'pum-admin-batch' ) ) {
  111. wp_localize_script( 'pum-admin-batch', 'pum_batch_vars', array(
  112. 'complete' => __( 'Your all set, the upgrades completed successfully!', 'popup-maker' ),
  113. 'unsupported_browser' => __( 'We are sorry but your browser is not compatible with this kind of file upload. Please upgrade your browser.', 'popup-maker' ),
  114. 'import_field_required' => 'This field must be mapped for the import to proceed.',
  115. ) );
  116. }
  117. }
  118. /**
  119. * Load Admin Styles
  120. */
  121. public static function register_admin_styles() {
  122. $suffix = ( is_rtl() ? '-rtl' : '' ) . self::$suffix;
  123. wp_register_style( 'pum-admin-general', self::$css_url . 'pum-admin-general' . $suffix . '.css', array( 'dashicons', 'wp-color-picker' ), Popup_Maker::$VER );
  124. wp_register_style( 'pum-admin-batch', self::$css_url . 'pum-admin-batch' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  125. wp_register_style( 'pum-admin-popup-editor', self::$css_url . 'pum-admin-popup-editor' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  126. wp_register_style( 'pum-admin-theme-editor', self::$css_url . 'pum-admin-theme-editor' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  127. wp_register_style( 'pum-admin-extensions-page', self::$css_url . 'pum-admin-extensions-page' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  128. wp_register_style( 'pum-admin-settings-page', self::$css_url . 'pum-admin-settings-page' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  129. wp_register_style( 'pum-admin-support-page', self::$css_url . 'pum-admin-support-page' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  130. wp_register_style( 'pum-admin-shortcode-ui', self::$css_url . 'pum-admin-shortcode-ui' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  131. // @deprecated handle. Currently loads empty file and admin-general as dependency.
  132. wp_register_style( 'popup-maker-admin', self::$css_url . 'pum-admin-deprecated' . $suffix . '.css', array( 'pum-admin-general' ), Popup_Maker::$VER );
  133. if ( PUM_Utils_Upgrades::instance()->has_uncomplete_upgrades() ) {
  134. wp_enqueue_style( 'pum-admin-batch' );
  135. }
  136. if ( pum_is_popup_editor() ) {
  137. wp_enqueue_style( 'pum-admin-popup-editor' );
  138. }
  139. if ( pum_is_popup_theme_editor() ) {
  140. PUM_Site_Assets::register_styles();
  141. wp_enqueue_style( 'pum-admin-theme-editor' );
  142. }
  143. if ( pum_is_extensions_page() ) {
  144. wp_enqueue_style( 'pum-admin-extensions-page' );
  145. }
  146. if ( pum_is_settings_page() ) {
  147. wp_enqueue_style( 'pum-admin-settings-page' );
  148. }
  149. if ( pum_is_support_page() ) {
  150. wp_enqueue_style( 'pum-admin-support-page' );
  151. }
  152. }
  153. /**
  154. * @return bool
  155. */
  156. public static function should_load() {
  157. if ( defined( "PUM_FORCE_ADMIN_SCRIPTS_LOAD" ) && PUM_FORCE_ADMIN_SCRIPTS_LOAD ) {
  158. return true;
  159. }
  160. if ( ! is_admin() ) {
  161. return false;
  162. }
  163. return pum_is_admin_page();
  164. }
  165. }