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