Admin
3 years ago
Api
3 years ago
Blocks
3 years ago
Hooks
3 years ago
AjaxController.php
3 years ago
BlocksController.php
3 years ago
ElementorController.php
3 years ago
GutenBergController.php
3 years ago
PageTemplateController.php
3 years ago
ScriptController.php
3 years ago
ShortcodeController.php
3 years ago
WidgetController.php
3 years ago
ScriptController.php
319 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Script Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers; |
| 9 | |
| 10 | // Do not allow directly accessing this file. |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit( 'This script cannot be accessed directly.' ); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Script Controller class. |
| 17 | */ |
| 18 | class ScriptController { |
| 19 | /** |
| 20 | * Version |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private $version; |
| 25 | |
| 26 | /** |
| 27 | * Settings |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | private $settings; |
| 32 | |
| 33 | /** |
| 34 | * Class construct |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | $this->version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : RT_THE_POST_GRID_VERSION; |
| 38 | add_action( 'wp_head', [ $this, 'header_scripts' ] ); |
| 39 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); |
| 40 | add_action( 'init', [ $this, 'init' ] ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Init |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function init() { |
| 49 | $this->settings = get_option( rtTPG()->options['settings'] ); |
| 50 | $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 51 | |
| 52 | if ( 'rttpg_settings' === $current_page ) { |
| 53 | wp_enqueue_style( 'wp-color-picker' ); |
| 54 | wp_enqueue_script( 'wp-color-picker' ); |
| 55 | } |
| 56 | |
| 57 | // register scripts. |
| 58 | $scripts = []; |
| 59 | $styles = []; |
| 60 | |
| 61 | $scripts[] = [ |
| 62 | 'handle' => 'rt-isotope-js', |
| 63 | 'src' => rtTPG()->get_assets_uri( 'vendor/isotope/isotope.pkgd.min.js' ), |
| 64 | 'deps' => [ 'jquery' ], |
| 65 | 'footer' => true, |
| 66 | ]; |
| 67 | |
| 68 | $scripts[] = [ |
| 69 | 'handle' => 'rt-tpg', |
| 70 | 'src' => rtTPG()->get_assets_uri( 'js/rttpg.js' ), |
| 71 | 'deps' => [ 'jquery' ], |
| 72 | 'footer' => true, |
| 73 | ]; |
| 74 | |
| 75 | // register acf styles. |
| 76 | $styles['rt-fontawsome'] = rtTPG()->get_assets_uri( 'vendor/font-awesome/css/font-awesome.min.css' ); |
| 77 | |
| 78 | // Plugin specific css. |
| 79 | $styles['rt-tpg'] = rtTPG()->tpg_can_be_rtl( 'css/thepostgrid' ); |
| 80 | $styles['rt-tpg-block'] = rtTPG()->tpg_can_be_rtl( 'css/tpg-block' ); |
| 81 | $styles['rt-tpg-shortcode'] = rtTPG()->tpg_can_be_rtl( 'css/tpg-shortcode' ); |
| 82 | |
| 83 | if ( is_admin() ) { |
| 84 | $scripts[] = [ |
| 85 | 'handle' => 'rt-select2', |
| 86 | 'src' => rtTPG()->get_assets_uri( 'vendor/select2/select2.min.js' ), |
| 87 | 'deps' => [ 'jquery' ], |
| 88 | 'footer' => false, |
| 89 | ]; |
| 90 | $scripts[] = [ |
| 91 | 'handle' => 'rt-tpg-admin', |
| 92 | 'src' => rtTPG()->get_assets_uri( 'js/admin.js' ), |
| 93 | 'deps' => [ 'jquery', 'wp-color-picker' ], |
| 94 | 'footer' => true, |
| 95 | ]; |
| 96 | $scripts[] = [ |
| 97 | 'handle' => 'rt-tpg-admin-preview', |
| 98 | 'src' => rtTPG()->get_assets_uri( 'js/admin-preview.js' ), |
| 99 | 'deps' => [ 'jquery' ], |
| 100 | 'footer' => true, |
| 101 | ]; |
| 102 | |
| 103 | $styles['rt-select2'] = rtTPG()->get_assets_uri( 'vendor/select2/select2.min.css' ); |
| 104 | $styles['rt-tpg-admin'] = rtTPG()->get_assets_uri( 'css/admin/admin.css' ); |
| 105 | $styles['rt-tpg-admin-preview'] = rtTPG()->get_assets_uri( 'css/admin/admin-preview.css' ); |
| 106 | } |
| 107 | |
| 108 | foreach ( $scripts as $script ) { |
| 109 | wp_register_script( $script['handle'], $script['src'], $script['deps'], isset( $script['version'] ) ? $script['version'] : $this->version, $script['footer'] ); |
| 110 | } |
| 111 | |
| 112 | foreach ( $styles as $k => $v ) { |
| 113 | wp_register_style( $k, $v, false, isset( $script['version'] ) ? $script['version'] : $this->version ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Enqueue scripts. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function enqueue() { |
| 123 | $block_type = isset( $this->settings['tpg_block_type'] ) ? $this->settings['tpg_block_type'] : 'default'; |
| 124 | wp_enqueue_script( 'jquery' ); |
| 125 | |
| 126 | if ( ! isset( $this->settings['tpg_load_script'] ) ) { |
| 127 | wp_enqueue_style( 'rt-fontawsome' ); |
| 128 | |
| 129 | if ( 'default' === $block_type ) { |
| 130 | wp_enqueue_style( 'rt-tpg' ); |
| 131 | } |
| 132 | |
| 133 | if ( 'elementor' === $block_type ) { |
| 134 | wp_enqueue_style( 'rt-tpg-block' ); |
| 135 | } |
| 136 | |
| 137 | if ( 'shortcode' === $block_type ) { |
| 138 | wp_enqueue_style( 'rt-tpg-shortcode' ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $scriptBefore = isset( $this->settings['script_before_item_load'] ) ? stripslashes( $this->settings['script_before_item_load'] ) : null; |
| 143 | $scriptAfter = isset( $this->settings['script_after_item_load'] ) ? stripslashes( $this->settings['script_after_item_load'] ) : null; |
| 144 | $scriptLoaded = isset( $this->settings['script_loaded'] ) ? stripslashes( $this->settings['script_loaded'] ) : null; |
| 145 | |
| 146 | $script = "(function($){ |
| 147 | $('.rt-tpg-container').on('tpg_item_before_load', function(){{$scriptBefore}}); |
| 148 | $('.rt-tpg-container').on('tpg_item_after_load', function(){{$scriptAfter}}); |
| 149 | $('.rt-tpg-container').on('tpg_loaded', function(){{$scriptLoaded}}); |
| 150 | })(jQuery);"; |
| 151 | wp_add_inline_script( 'rt-tpg', $script ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Header Scripts |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public function header_scripts() { |
| 160 | ?> |
| 161 | <style> |
| 162 | :root { |
| 163 | --tpg-primary-color: <?php echo isset( $this->settings['tpg_primary_color_main'] ) ? sanitize_hex_color( $this->settings['tpg_primary_color_main'] ) : '#0d6efd'; ?>; |
| 164 | --tpg-secondary-color: <?php echo isset( $this->settings['tpg_secondary_color_main'] ) ? sanitize_hex_color( $this->settings['tpg_secondary_color_main'] ) : '#0654c4'; ?>; |
| 165 | --tpg-primary-light: #c4d0ff |
| 166 | } |
| 167 | |
| 168 | <?php if ( isset( $this->settings['tpg_loader_color'] ) ) : ?> |
| 169 | body .rt-tpg-container .rt-loading, |
| 170 | body #bottom-script-loader .rt-ball-clip-rotate { |
| 171 | color: <?php echo sanitize_hex_color( $this->settings['tpg_loader_color'] ); ?> !important; |
| 172 | } |
| 173 | |
| 174 | <?php endif; ?> |
| 175 | </style> |
| 176 | |
| 177 | <?php |
| 178 | if ( isset( $this->settings['tpg_load_script'] ) ) : |
| 179 | ?> |
| 180 | <style> |
| 181 | .rt-container-fluid { |
| 182 | position: relative; |
| 183 | } |
| 184 | |
| 185 | .rt-tpg-container .tpg-pre-loader { |
| 186 | position: relative; |
| 187 | overflow: hidden; |
| 188 | } |
| 189 | |
| 190 | .rt-tpg-container .rt-loading-overlay { |
| 191 | opacity: 0; |
| 192 | visibility: hidden; |
| 193 | position: absolute; |
| 194 | top: 0; |
| 195 | left: 0; |
| 196 | width: 100%; |
| 197 | height: 100%; |
| 198 | z-index: 1; |
| 199 | background-color: #fff; |
| 200 | } |
| 201 | |
| 202 | .rt-tpg-container .rt-loading { |
| 203 | color: var(--tpg-primary-color); |
| 204 | position: absolute; |
| 205 | top: 40%; |
| 206 | left: 50%; |
| 207 | margin-left: -16px; |
| 208 | z-index: 2; |
| 209 | opacity: 0; |
| 210 | visibility: hidden; |
| 211 | } |
| 212 | |
| 213 | .rt-tpg-container .tpg-pre-loader .rt-loading-overlay { |
| 214 | opacity: 0.8; |
| 215 | visibility: visible; |
| 216 | } |
| 217 | |
| 218 | .tpg-carousel-main .tpg-pre-loader .rt-loading-overlay { |
| 219 | opacity: 1; |
| 220 | } |
| 221 | |
| 222 | .rt-tpg-container .tpg-pre-loader .rt-loading { |
| 223 | opacity: 1; |
| 224 | visibility: visible; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #bottom-script-loader { |
| 229 | position: absolute; |
| 230 | width: calc(100% + 60px); |
| 231 | height: calc(100% + 60px); |
| 232 | z-index: 999; |
| 233 | background: rgba(255, 255, 255, 0.95); |
| 234 | margin: -30px; |
| 235 | } |
| 236 | |
| 237 | #bottom-script-loader .rt-ball-clip-rotate { |
| 238 | color: var(--tpg-primary-color); |
| 239 | position: absolute; |
| 240 | top: 80px; |
| 241 | left: 50%; |
| 242 | margin-left: -16px; |
| 243 | z-index: 2; |
| 244 | } |
| 245 | |
| 246 | .tpg-el-main-wrapper.loading { |
| 247 | min-height: 300px; |
| 248 | transition: 0.4s; |
| 249 | } |
| 250 | |
| 251 | .tpg-el-main-wrapper.loading::before { |
| 252 | width: 32px; |
| 253 | height: 32px; |
| 254 | display: inline-block; |
| 255 | float: none; |
| 256 | border: 2px solid currentColor; |
| 257 | background: transparent; |
| 258 | border-bottom-color: transparent; |
| 259 | border-radius: 100%; |
| 260 | -webkit-animation: ball-clip-rotate 0.75s linear infinite; |
| 261 | -moz-animation: ball-clip-rotate 0.75s linear infinite; |
| 262 | -o-animation: ball-clip-rotate 0.75s linear infinite; |
| 263 | animation: ball-clip-rotate 0.75s linear infinite; |
| 264 | left: 50%; |
| 265 | top: 50%; |
| 266 | position: absolute; |
| 267 | z-index: 9999999999; |
| 268 | color: red; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | .rt-tpg-container .slider-main-wrapper, |
| 273 | .tpg-el-main-wrapper .slider-main-wrapper { |
| 274 | opacity: 0; |
| 275 | } |
| 276 | |
| 277 | .md-modal { |
| 278 | visibility: hidden; |
| 279 | } |
| 280 | .md-modal.md-show { |
| 281 | visibility: visible; |
| 282 | } |
| 283 | |
| 284 | .builder-content.content-invisible { |
| 285 | visibility: hidden; |
| 286 | } |
| 287 | |
| 288 | .rt-tpg-container > *:not(.bottom-script-loader, .slider-main-wrapper) { |
| 289 | opacity: 0; |
| 290 | } |
| 291 | |
| 292 | .rt-popup-content .rt-tpg-container > *:not(.bottom-script-loader, .slider-main-wrapper) { |
| 293 | opacity: 1; |
| 294 | } |
| 295 | |
| 296 | </style> |
| 297 | |
| 298 | <script> |
| 299 | jQuery( document ).ready( function () { |
| 300 | setTimeout( function () { |
| 301 | jQuery( '.rt-tpg-container > *:not(.bottom-script-loader, .slider-main-wrapper)' ).animate( { "opacity": 1 } ); |
| 302 | }, 100 ); |
| 303 | } ); |
| 304 | |
| 305 | jQuery( window ).on( 'elementor/frontend/init', function () { |
| 306 | if ( elementorFrontend.isEditMode() ) { |
| 307 | elementorFrontend.hooks.addAction( 'frontend/element_ready/widget', function () { |
| 308 | jQuery( '.rt-tpg-container > *:not(.bottom-script-loader, .slider-main-wrapper)' ).animate( { "opacity": 1 } ); |
| 309 | } ); |
| 310 | } |
| 311 | } ); |
| 312 | </script> |
| 313 | <?php |
| 314 | endif; |
| 315 | |
| 316 | } |
| 317 | |
| 318 | } |
| 319 |