updates
1 month ago
Admin.php
1 month ago
Ajax.php
1 month ago
Customizer.php
1 month ago
Elementor.php
1 month ago
Enqueue.php
1 month ago
Hooks.php
1 month ago
Install.php
1 month ago
Main.php
1 month ago
Update.php
1 month ago
functions.php
1 month ago
Enqueue.php
251 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AI1WPSA; |
| 4 | |
| 5 | if (! defined('ABSPATH')) exit; |
| 6 | |
| 7 | class Enqueue { |
| 8 | |
| 9 | private static $instance = null; |
| 10 | |
| 11 | public function __construct() { |
| 12 | // Frontend scripts |
| 13 | add_action('wp_enqueue_scripts', array($this, 'frontend_scripts')); |
| 14 | |
| 15 | // Customizer scripts |
| 16 | add_action('customize_preview_init', array($this, 'customizer_scripts')); |
| 17 | |
| 18 | // Admin scripts |
| 19 | add_action('admin_enqueue_scripts', array($this, 'admin_scripts')); |
| 20 | |
| 21 | // Block editor scripts |
| 22 | add_action('enqueue_block_editor_assets', array($this, 'block_editor_scripts')); |
| 23 | } |
| 24 | |
| 25 | public function frontend_scripts() { |
| 26 | // frontend styles |
| 27 | wp_register_style('ai1wpsa-fontawesome', AI1WPSA_ASSETS . '/vendor/fontawesome/fontawesome.min.css', [], '7.1.0'); |
| 28 | wp_register_style('ai1wpsa-fontawesome-brands', AI1WPSA_ASSETS . '/vendor/fontawesome/brands.min.css', [], '7.1.0'); |
| 29 | wp_register_style('ai1wpsa-bootstrap-icons', AI1WPSA_ASSETS . '/vendor/bootstrap-icons/bootstrap-icons.min.css', [], '1.13.1'); |
| 30 | wp_register_style('ai1wpsa-remixicon', AI1WPSA_ASSETS . '/vendor/remixicon/remixicon.min.css', [], '4.7.0'); |
| 31 | wp_register_style('ai1wpsa-lineicon', AI1WPSA_ASSETS . '/vendor/lineicon/lineicons.min.css', [], '5.0.0'); |
| 32 | wp_register_style('ai1wpsa-click-to-call', AI1WPSA_ASSETS . '/vendor/click-to-call/click-to-call.min.css', [], '1.0.0'); |
| 33 | wp_register_style('ai1wpsa-toc', AI1WPSA_ASSETS . '/vendor/toc/toc.min.css', [], '1.0.0'); |
| 34 | wp_register_style('ai1wpsa-frontend', AI1WPSA_ASSETS . '/css/frontend.min.css', [], AI1WPSA_VERSION); |
| 35 | |
| 36 | // fix default sticky |
| 37 | $fix_default_sticky = ai1wpsa_get_settings('fixDefaultSticky') || get_option('ai1wpsa_sticky_header') || ai1wpsa_get_settings('stickySocialIcon') || ai1wpsa_get_settings('stickyCookieConsent'); |
| 38 | |
| 39 | // convert to boolean |
| 40 | $fix_default_sticky = filter_var($fix_default_sticky, FILTER_VALIDATE_BOOLEAN); |
| 41 | |
| 42 | // icon type |
| 43 | $icon_type = ai1wpsa_get_settings('stickySocialIconType', 'dashicons'); |
| 44 | |
| 45 | if ($icon_type === 'fontawesome') { |
| 46 | wp_enqueue_style('ai1wpsa-fontawesome'); |
| 47 | wp_enqueue_style('ai1wpsa-fontawesome-brands'); |
| 48 | } else if ($icon_type === 'bootstrap') { |
| 49 | wp_enqueue_style('ai1wpsa-bootstrap-icons'); |
| 50 | } else if ($icon_type === 'remixicon') { |
| 51 | wp_enqueue_style('ai1wpsa-remixicon'); |
| 52 | } else if ($icon_type === 'lineicon') { |
| 53 | wp_enqueue_style('ai1wpsa-lineicon'); |
| 54 | } else { |
| 55 | wp_enqueue_style('dashicons'); |
| 56 | } |
| 57 | |
| 58 | if ($fix_default_sticky === true || is_plugin_active('elementor/elementor.php')) { |
| 59 | wp_enqueue_style('ai1wpsa-frontend'); |
| 60 | } |
| 61 | |
| 62 | // Scripts |
| 63 | wp_register_script('ai1wpsa-stickr', AI1WPSA_ASSETS . '/vendor/stickr.min.js', [ |
| 64 | 'jquery', |
| 65 | 'wp-plupload', |
| 66 | 'wp-util', |
| 67 | ], AI1WPSA_VERSION, true); |
| 68 | |
| 69 | wp_register_script('ai1wpsa-theia', AI1WPSA_ASSETS . '/vendor/theia-sticky-sidebar.js', [ |
| 70 | 'jquery', |
| 71 | ], AI1WPSA_VERSION, true); |
| 72 | |
| 73 | wp_register_script('ai1wpsa-click-to-call', AI1WPSA_ASSETS . '/vendor/click-to-call/click-to-call.min.js', [ |
| 74 | 'jquery', |
| 75 | ], '1.0', true); |
| 76 | |
| 77 | wp_register_script('ai1wpsa-toc', AI1WPSA_ASSETS . '/vendor/toc/toc.min.js', [], '1.0', true); |
| 78 | |
| 79 | wp_register_script('ai1wpsa-frontend', AI1WPSA_ASSETS . '/js/frontend.min.js', [ |
| 80 | 'jquery', |
| 81 | 'wp-plupload', |
| 82 | 'wp-util', |
| 83 | ], AI1WPSA_VERSION, true); |
| 84 | |
| 85 | $fix_default_sticky = ai1wpsa_get_settings('fixDefaultSticky', false); |
| 86 | $fix_default_sticky = filter_var($fix_default_sticky, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 87 | |
| 88 | if (empty($fix_default_sticky) || $fix_default_sticky === false || $fix_default_sticky === '0') { |
| 89 | wp_enqueue_script('ai1wpsa-stickr'); |
| 90 | } |
| 91 | |
| 92 | $sticky_sidebar = ai1wpsa_get_settings('stickySidebar'); |
| 93 | |
| 94 | if (filter_var($sticky_sidebar, FILTER_VALIDATE_BOOLEAN)) { |
| 95 | wp_enqueue_script('ai1wpsa-theia'); |
| 96 | } |
| 97 | |
| 98 | // click to call |
| 99 | $click_to_call = ai1wpsa_get_settings('clicktoCall', false); |
| 100 | |
| 101 | if (filter_var($click_to_call, FILTER_VALIDATE_BOOLEAN)) { |
| 102 | wp_enqueue_style('ai1wpsa-click-to-call'); |
| 103 | wp_enqueue_script('ai1wpsa-click-to-call'); |
| 104 | } |
| 105 | |
| 106 | // table of contents |
| 107 | $sticky_toc = ai1wpsa_get_settings('stickyToc', false); |
| 108 | $sticky_toc_post_types = (array) ai1wpsa_get_settings('stickyTocPostTypes', ['post']); |
| 109 | |
| 110 | if (filter_var($sticky_toc, FILTER_VALIDATE_BOOLEAN) && is_singular($sticky_toc_post_types)) { |
| 111 | wp_enqueue_style('ai1wpsa-toc'); |
| 112 | wp_enqueue_script('ai1wpsa-toc'); |
| 113 | } |
| 114 | |
| 115 | wp_enqueue_script('ai1wpsa-frontend'); |
| 116 | |
| 117 | wp_localize_script('ai1wpsa-frontend', 'ai1wpsa', $this->get_localize_data()); |
| 118 | } |
| 119 | |
| 120 | public function customizer_scripts() { |
| 121 | wp_enqueue_script('ai1wpsa-customizer', AI1WPSA_ASSETS . '/js/customizer.min.js', ['customize-preview', 'jquery'], AI1WPSA_VERSION, true); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Admin Assets |
| 126 | * |
| 127 | * @param [type] $hook |
| 128 | * @return void |
| 129 | */ |
| 130 | public function admin_scripts($hook) { |
| 131 | // Only load on our plugin settings page |
| 132 | // if ('toplevel_page_all-in-one-wp-sticky-anything' !== $hook && 'sticky-anything_page_sticky-anything-getting-started' !== $hook) { |
| 133 | // return; |
| 134 | // } |
| 135 | |
| 136 | // Styles |
| 137 | $style_deps = array('wp-components',); |
| 138 | |
| 139 | if ('toplevel_page_all-in-one-wp-sticky-anything' == $hook) { |
| 140 | $style_deps[] = 'wp-codemirror'; |
| 141 | } |
| 142 | |
| 143 | wp_enqueue_style('ai1wpsa-swal', AI1WPSA_URL . '/assets/vendor/sweetalert2/sweetalert2.min.css', $style_deps, '11.12.4'); |
| 144 | |
| 145 | wp_enqueue_style('ai1wpsa-admin', AI1WPSA_URL . '/assets/css/admin.min.css', $style_deps, AI1WPSA_VERSION); |
| 146 | |
| 147 | // default dependencies |
| 148 | $deps = array( |
| 149 | 'jquery', |
| 150 | 'wp-util', |
| 151 | 'wp-i18n', |
| 152 | 'wp-element', |
| 153 | 'wp-components', |
| 154 | 'wp-blocks', |
| 155 | 'wp-hooks', |
| 156 | 'wp-block-editor', |
| 157 | ); |
| 158 | |
| 159 | if ('toplevel_page_all-in-one-wp-sticky-anything' == $hook) { |
| 160 | wp_enqueue_script('wp-theme-plugin-editor'); |
| 161 | |
| 162 | wp_enqueue_code_editor(array( |
| 163 | 'type' => 'text/css', |
| 164 | )); |
| 165 | } |
| 166 | |
| 167 | wp_enqueue_script('ai1wpsa-swal', AI1WPSA_URL . '/assets/vendor/sweetalert2/sweetalert2.min.js', $deps, '11.12.4', true); |
| 168 | |
| 169 | wp_enqueue_script('ai1wpsa-admin', AI1WPSA_URL . '/assets/js/admin.min.js', $deps, AI1WPSA_VERSION, true); |
| 170 | wp_localize_script('ai1wpsa-admin', 'ai1wpsa', $this->get_localize_data($hook)); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Block Editor Assets |
| 175 | * |
| 176 | * @return void |
| 177 | */ |
| 178 | public function block_editor_scripts() { |
| 179 | if (! function_exists('get_current_screen')) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | $screen = get_current_screen(); |
| 184 | |
| 185 | if (empty($screen) || 'widgets' !== $screen->id) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // Scripts |
| 190 | $deps = array( |
| 191 | 'jquery', |
| 192 | 'wp-i18n', |
| 193 | 'wp-element', |
| 194 | 'wp-components', |
| 195 | 'wp-blocks', |
| 196 | 'wp-block-editor', |
| 197 | ); |
| 198 | |
| 199 | wp_enqueue_script('ai1wpsa-block-editor', AI1WPSA_URL . '/assets/js/block-editor.min.js', $deps, AI1WPSA_VERSION, true); |
| 200 | wp_localize_script('ai1wpsa-block-editor', 'ai1wpsa', $this->get_localize_data('block_editor')); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | public function get_localize_data($hook = null) { |
| 205 | |
| 206 | // Fetch the saved settings |
| 207 | $settings = get_option('ai1wpsa_settings'); |
| 208 | |
| 209 | // if not an array make it one |
| 210 | if (!is_array($settings)) { |
| 211 | $settings = []; |
| 212 | } |
| 213 | |
| 214 | $ai1wpsa_sticky_header = get_option('ai1wpsa_sticky_header'); |
| 215 | $ai1wpsa_z_index = get_option('ai1wpsa_z_index', 9999); |
| 216 | |
| 217 | // customizer settings |
| 218 | if ($ai1wpsa_sticky_header) { |
| 219 | $settings['stickyHeader'] = $ai1wpsa_sticky_header; |
| 220 | $settings['stickyCIndex'] = $ai1wpsa_z_index; |
| 221 | } |
| 222 | |
| 223 | $data = [ |
| 224 | 'nonce' => wp_create_nonce('ai1wpsa'), |
| 225 | 'isPro' => false, |
| 226 | 'isLoggedIn' => is_user_logged_in(), |
| 227 | 'settings' => $settings, |
| 228 | ]; |
| 229 | |
| 230 | if (is_admin()) { |
| 231 | $data['homeUrl'] = home_url(); |
| 232 | $data['ajaxUrl'] = admin_url('admin-ajax.php'); |
| 233 | $data['pluginUrl'] = AI1WPSA_URL; |
| 234 | $data['adminUrl'] = admin_url(); |
| 235 | $data['postTypes'] = ai1wpsa_get_public_post_types(); |
| 236 | } |
| 237 | |
| 238 | return apply_filters('ai1wpsa_localize_data', $data, $hook); |
| 239 | } |
| 240 | |
| 241 | public static function instance() { |
| 242 | if (null === self::$instance) { |
| 243 | self::$instance = new self; |
| 244 | } |
| 245 | |
| 246 | return self::$instance; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | Enqueue::instance(); |
| 251 |