compatibility
2 years ago
controls
2 years ago
pa-display-conditions
2 years ago
templates
2 years ago
acf-helper.php
2 years ago
addons-cross-cp.php
2 years ago
addons-integration.php
2 years ago
assets-manager.php
2 years ago
class-pa-core.php
2 years ago
class-premium-template-tags.php
2 years ago
helper-functions.php
2 years ago
live-editor-modal.php
2 years ago
module-base.php
2 years ago
pa-nav-menu-walker.php
2 years ago
assets-manager.php
708 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PA Assets Manager. |
| 4 | */ |
| 5 | |
| 6 | namespace PremiumAddons\Includes; |
| 7 | |
| 8 | use Elementor\Plugin; |
| 9 | use PremiumAddons\Includes\Helper_Functions; |
| 10 | use PremiumAddons\Admin\Includes\Admin_Helper; |
| 11 | |
| 12 | require_once PREMIUM_ADDONS_PATH . 'widgets/dep/urlopen.php'; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * PA Assets Manager Class. |
| 20 | */ |
| 21 | class Assets_Manager { |
| 22 | |
| 23 | /** |
| 24 | * Class Instance. |
| 25 | * |
| 26 | * @var object|null instance. |
| 27 | */ |
| 28 | private static $instance = null; |
| 29 | |
| 30 | /** |
| 31 | * Post Id. |
| 32 | * Option Id. |
| 33 | * |
| 34 | * @var string|null post_id. |
| 35 | */ |
| 36 | public static $post_id = null; |
| 37 | |
| 38 | /** |
| 39 | * Templates ids loaded in a post. |
| 40 | * |
| 41 | * @var array temp_ids. |
| 42 | */ |
| 43 | public static $temp_ids = array(); |
| 44 | |
| 45 | /** |
| 46 | * All elements loaded in a post. |
| 47 | * |
| 48 | * @var array temp_elements. |
| 49 | */ |
| 50 | public static $temp_elements = array(); |
| 51 | |
| 52 | /** |
| 53 | * Is page assets updated. |
| 54 | * |
| 55 | * @var boolean is_updated. |
| 56 | */ |
| 57 | public static $is_updated = null; |
| 58 | |
| 59 | /** |
| 60 | * Class Constructor. |
| 61 | */ |
| 62 | public function __construct() { |
| 63 | |
| 64 | add_action( 'elementor/editor/after_save', array( $this, 'handle_post_save' ), 10, 2 ); |
| 65 | |
| 66 | // Check if the elments are cached. |
| 67 | add_action( 'wp', array( $this, 'set_assets_vars' ) ); |
| 68 | |
| 69 | // Save the elements on the current page. |
| 70 | add_filter( 'elementor/frontend/builder_content_data', array( $this, 'manage_post_data' ), 10, 2 ); |
| 71 | |
| 72 | add_action( 'wp_footer', array( $this, 'cache_post_assets' ) ); |
| 73 | |
| 74 | add_action( 'wp_trash_post', array( $this, 'delete_cached_options' ) ); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets Edit Time upon editor save. |
| 80 | * |
| 81 | * @access public |
| 82 | * @since 4.6.1 |
| 83 | */ |
| 84 | public function handle_post_save( $post_id ) { |
| 85 | |
| 86 | if ( wp_doing_cron() ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // The post is saved, then we need to remove the assets related to it. |
| 91 | $this->set_post_id( $post_id ); |
| 92 | self::remove_files(); |
| 93 | |
| 94 | update_option( 'pa_edit_time', strtotime( 'now' ) ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Mange Post Data. |
| 99 | * |
| 100 | * @access public |
| 101 | * @since 4.6.1 |
| 102 | * |
| 103 | * @param array $data post data. |
| 104 | * @param int|string $post_id post id. |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | public function manage_post_data( $data, $post_id ) { |
| 109 | |
| 110 | if ( ! self::$is_updated ) { |
| 111 | $pa_elems = $this->extract_pa_elements( $data ); |
| 112 | |
| 113 | self::$temp_ids[] = $post_id; |
| 114 | self::$temp_elements = array_unique( array_merge( self::$temp_elements, $pa_elems ) ); |
| 115 | } |
| 116 | |
| 117 | return $data; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set post unique id. |
| 122 | * |
| 123 | * @access public |
| 124 | * @since 4.6.1 |
| 125 | * |
| 126 | * @param int|string $id post id. |
| 127 | */ |
| 128 | public function set_post_id( $id = 'default' ) { |
| 129 | |
| 130 | $post_id = 'default' === $id ? 'pa_assets_' . get_queried_object_id() : 'pa_assets_' . $id; |
| 131 | |
| 132 | if ( null === self::$post_id ) { |
| 133 | self::$post_id = Helper_Functions::generate_unique_id( $post_id ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Extracts PA Elements. |
| 139 | * |
| 140 | * @access public |
| 141 | * @since 4.6.1 |
| 142 | * |
| 143 | * @param array $data post data. |
| 144 | * |
| 145 | * @return array |
| 146 | */ |
| 147 | public function extract_pa_elements( $data ) { |
| 148 | |
| 149 | if ( empty( $data ) ) { |
| 150 | return array(); |
| 151 | } |
| 152 | |
| 153 | $pa_names = Admin_Helper::get_pa_elements_names(); |
| 154 | |
| 155 | $social_revs = array( |
| 156 | 'premium-yelp-reviews', |
| 157 | 'premium-google-reviews', |
| 158 | 'premium-facebook-reviews', |
| 159 | ); |
| 160 | |
| 161 | $pa_elems = array(); |
| 162 | |
| 163 | Plugin::$instance->db->iterate_data( |
| 164 | $data, |
| 165 | function ( $element ) use ( &$pa_elems, $pa_names, $social_revs ) { |
| 166 | |
| 167 | if ( isset( $element['elType'] ) ) { |
| 168 | |
| 169 | if ( 'widget' === $element['elType'] && isset( $element['widgetType'] ) ) { |
| 170 | |
| 171 | $widget_type = ( 'global' === $element['widgetType'] && ! empty( $element['templateID'] ) ) ? $this->get_global_widget_type( $element['templateID'] ) : $element['widgetType']; |
| 172 | |
| 173 | if ( in_array( $widget_type, $pa_names, true ) && ! in_array( $widget_type, $pa_elems, true ) ) { |
| 174 | |
| 175 | $widget_type = in_array( $widget_type, $social_revs, true ) ? 'premium-reviews' : $widget_type; |
| 176 | |
| 177 | if ( in_array( $widget_type, array( 'premium-twitter-feed', 'premium-facebook-feed' ), true ) && ! in_array( 'social-common', $pa_elems, true ) ) { |
| 178 | array_push( $pa_elems, 'social-common' ); |
| 179 | } |
| 180 | |
| 181 | array_push( $pa_elems, $widget_type ); |
| 182 | |
| 183 | if ( 'premium-woo-products' === $widget_type ) { |
| 184 | $papro_activated = apply_filters( 'papro_activated', false ); |
| 185 | |
| 186 | if ( $papro_activated ) { |
| 187 | array_push( $pa_elems, 'premium-woo-products-pro' ); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | ); |
| 195 | |
| 196 | return $pa_elems; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get Global Wiget Type. |
| 201 | * |
| 202 | * @access public |
| 203 | * @since 4.6.1 |
| 204 | * @link https://code.elementor.com/methods/elementor-templatelibrary-manager-get_template_data/ |
| 205 | * @param int $temp_id template it. |
| 206 | * |
| 207 | * @return string|void |
| 208 | */ |
| 209 | public function get_global_widget_type( $temp_id ) { |
| 210 | |
| 211 | $temp_data = Plugin::$instance->templates_manager->get_template_data( |
| 212 | array( |
| 213 | 'source' => 'local', |
| 214 | 'template_id' => $temp_id, |
| 215 | ) |
| 216 | ); |
| 217 | |
| 218 | if ( is_wp_error( $temp_data ) || ! $temp_data || empty( $temp_data ) ) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | if ( ! isset( $temp_data['content'] ) || empty( $temp_data['content'] ) ) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | return $temp_data['content'][0]['widgetType']; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets Assets Variables. |
| 231 | * Sets Post ID & Is_updated Flag. |
| 232 | * |
| 233 | * @access public |
| 234 | * @since 4.6.1 |
| 235 | */ |
| 236 | public function set_assets_vars() { |
| 237 | |
| 238 | $is_edit_mode = Helper_Functions::is_edit_mode(); |
| 239 | |
| 240 | if ( ! $this->is_built_with_elementor() || $is_edit_mode ) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | $this->set_post_id(); |
| 245 | |
| 246 | self::$is_updated = self::is_ready_for_generate(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Is Built With Elementor. |
| 251 | * |
| 252 | * @access public |
| 253 | * @since 4.6.1 |
| 254 | * |
| 255 | * @return boolean |
| 256 | */ |
| 257 | public function is_built_with_elementor() { |
| 258 | |
| 259 | if ( ! class_exists( 'Elementor\Plugin' ) ) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | $type = get_post_type(); |
| 264 | |
| 265 | if ( 'page' !== $type && 'post' !== $type ) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | $current_id = get_the_ID(); |
| 270 | |
| 271 | if ( ! $current_id || $current_id < 0 ) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | return Plugin::$instance->documents->get( get_the_ID() )->is_built_with_elementor(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Check if assets is updated. |
| 280 | * |
| 281 | * @access public |
| 282 | * @since 4.6.1 |
| 283 | * |
| 284 | * @return boolean |
| 285 | */ |
| 286 | public static function is_ready_for_generate() { |
| 287 | |
| 288 | $editor_time = get_option( 'pa_edit_time', false ); |
| 289 | |
| 290 | // If no post/page was saved after the feature is enabled. |
| 291 | if ( ! $editor_time ) { |
| 292 | update_option( 'pa_edit_time', strtotime( 'now' ) ); |
| 293 | } |
| 294 | |
| 295 | $post_edit_time = get_option( 'pa_edit_' . self::$post_id, false ); |
| 296 | |
| 297 | // If the time of the last update is not equal to the time the current post was last changed. This means another post was saved, then load the default assets. |
| 298 | // In this case, we need to load the default assets until the elements in the page needs to be cached first. |
| 299 | if ( ! $post_edit_time || (int) $editor_time !== (int) $post_edit_time ) { |
| 300 | // A change was made in the page elements, then we need to force the assets to be regenerated |
| 301 | self::remove_files(); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Cached post assets. |
| 310 | * |
| 311 | * Update post options in db on page load. |
| 312 | * |
| 313 | * @access public |
| 314 | * @since 4.6.1 |
| 315 | */ |
| 316 | public function cache_post_assets() { |
| 317 | |
| 318 | $is_edit_mode = Helper_Functions::is_edit_mode(); |
| 319 | $cond = $this->is_built_with_elementor() && ! $is_edit_mode; |
| 320 | |
| 321 | if ( ! self::$is_updated && $cond ) { |
| 322 | update_option( 'pa_elements_' . self::$post_id, self::$temp_elements, false ); |
| 323 | update_option( 'pa_edit_' . self::$post_id, get_option( 'pa_edit_time' ), false ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Delete Cached Options. |
| 329 | * Delete post options from db on post delete. |
| 330 | * |
| 331 | * @access public |
| 332 | * @since 4.6.1 |
| 333 | * |
| 334 | * @param int $post_id post id. |
| 335 | */ |
| 336 | public function delete_cached_options( $post_id ) { |
| 337 | |
| 338 | $id = substr( md5( 'pa_assets_' . $post_id ), 0, 9 ); |
| 339 | |
| 340 | delete_option( 'pa_elements_' . $id ); |
| 341 | delete_option( 'pa_edit_' . $id ); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Generate Assets files. |
| 347 | * Adds assets into pa-frontend(|-rtl).min.(js|css). |
| 348 | * |
| 349 | * @access public |
| 350 | * @since 4.6.1 |
| 351 | * |
| 352 | * @param string $ext assets extensions (js|css). |
| 353 | */ |
| 354 | public static function generate_asset_file( $ext ) { |
| 355 | |
| 356 | $direction = is_rtl() && 'css' === $ext ? 'rtl-' : ''; |
| 357 | $main_file_name = Helper_Functions::get_safe_path( PREMIUM_ASSETS_PATH . '/pa-frontend-' . $direction . self::$post_id . '.min.' . $ext ); |
| 358 | |
| 359 | // If the file already exists, then there is no need to regenerate a new one. |
| 360 | if ( file_exists( $main_file_name ) ) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | $content = self::get_asset_file_content( $ext ); |
| 365 | |
| 366 | // If no premium elements exist on the page, then don't generate files |
| 367 | if ( empty( $content ) ) { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if ( 'css' === $ext && is_rtl() ) { |
| 372 | $rtl_file_name = Helper_Functions::get_safe_path( PREMIUM_ASSETS_PATH . '/pa-frontend-rtl-' . self::$post_id . '.min.css' ); |
| 373 | } |
| 374 | |
| 375 | if ( ! file_exists( PREMIUM_ASSETS_PATH ) ) { |
| 376 | wp_mkdir_p( PREMIUM_ASSETS_PATH ); |
| 377 | } |
| 378 | |
| 379 | if ( 'css' === $ext ) { |
| 380 | |
| 381 | if ( is_rtl() ) { |
| 382 | // Make sure to delete the file before creating the new one. |
| 383 | file_put_contents( $rtl_file_name, '@charset "UTF-8";' . $content['rtl'] ); // phpcs:ignore |
| 384 | } else { |
| 385 | file_put_contents( $main_file_name, '@charset "UTF-8";' . $content['main'] ); // phpcs:ignore |
| 386 | } |
| 387 | } else { |
| 388 | file_put_contents( $main_file_name, $content ); // phpcs:ignore |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /** |
| 394 | * Clear cached file. |
| 395 | * Delete file if it exists. |
| 396 | * |
| 397 | * @access public |
| 398 | * @since 4.6.1 |
| 399 | * |
| 400 | * @param string $file_name file name. |
| 401 | */ |
| 402 | public static function clear_cached_file( $file_name ) { |
| 403 | |
| 404 | if ( file_exists( $file_name ) ) { |
| 405 | unlink( $file_name ); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Remove files |
| 411 | * |
| 412 | * @since 4.6.1 |
| 413 | */ |
| 414 | public static function remove_files() { |
| 415 | |
| 416 | $ext = array( 'css', 'js' ); |
| 417 | |
| 418 | foreach ( $ext as $e ) { |
| 419 | |
| 420 | $path = PREMIUM_ASSETS_PATH . '/pa-frontend-' . self::$post_id . '.min.' . $e; |
| 421 | |
| 422 | if ( 'css' === $e ) { |
| 423 | $rtl_path = PREMIUM_ASSETS_PATH . '/pa-frontend-rtl-' . self::$post_id . '.min.' . $e; |
| 424 | self::clear_cached_file( $rtl_path ); |
| 425 | } |
| 426 | |
| 427 | self::clear_cached_file( $path ); |
| 428 | } |
| 429 | |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Get Asset File Content. |
| 434 | * |
| 435 | * Collects pa/papro widgets assets. |
| 436 | * |
| 437 | * @access public |
| 438 | * @since 4.6.1 |
| 439 | * |
| 440 | * @param string $ext js|css. |
| 441 | * |
| 442 | * @return string|array $content |
| 443 | */ |
| 444 | public static function get_asset_file_content( $ext ) { |
| 445 | |
| 446 | // Get the cached elements of the current post/page. |
| 447 | $pa_elements = get_option( 'pa_elements_' . self::$post_id, array() ); |
| 448 | |
| 449 | if ( empty( $pa_elements ) ) { |
| 450 | return ''; |
| 451 | } |
| 452 | |
| 453 | $content = ''; |
| 454 | |
| 455 | if ( 'css' === $ext ) { |
| 456 | $rtl_content = ''; |
| 457 | } |
| 458 | |
| 459 | $pa_elements = self::prepare_pa_elements( $pa_elements, $ext ); |
| 460 | |
| 461 | foreach ( $pa_elements as $element ) { |
| 462 | |
| 463 | $path = self::get_file_path( $element, $ext ); |
| 464 | |
| 465 | if ( ! $path ) { |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | $content .= self::get_file_content( $path ); |
| 470 | |
| 471 | if ( 'css' === $ext && is_rtl() ) { |
| 472 | $rtl_path = self::get_file_path( $element, $ext, '-rtl' ); |
| 473 | $rtl_content .= self::get_file_content( $rtl_path ); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if ( 'css' === $ext ) { |
| 478 | |
| 479 | $content = array( |
| 480 | 'main' => $content, |
| 481 | 'rtl' => $rtl_content, |
| 482 | ); |
| 483 | |
| 484 | // Fix: at-rule or selector expected css error. |
| 485 | $content = str_replace( '@charset "UTF-8";', '', $content ); |
| 486 | } |
| 487 | |
| 488 | return $content; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Prepare PA Elements. |
| 493 | * |
| 494 | * @access public |
| 495 | * @since 4.6.1 |
| 496 | * |
| 497 | * @param array $elements post elements. |
| 498 | * @param string $ext js|css. |
| 499 | * |
| 500 | * @return array |
| 501 | */ |
| 502 | public static function prepare_pa_elements( $elements, $ext ) { |
| 503 | |
| 504 | if ( 'css' === $ext ) { |
| 505 | $common_assets = self::has_free_elements( $elements ) ? array( 'common' ) : array(); |
| 506 | $common_assets = self::has_pro_elements( $elements ) ? array_merge( $common_assets, array( 'common-pro' ) ) : $common_assets; |
| 507 | |
| 508 | $elements = array_merge( $elements, $common_assets ); |
| 509 | $indep_elements = array( |
| 510 | 'premium-world-clock' |
| 511 | ); |
| 512 | |
| 513 | } else { |
| 514 | $indep_elements = array( |
| 515 | 'social-common', |
| 516 | 'premium-hscroll', |
| 517 | 'premium-facebook-feed', |
| 518 | 'premium-behance-feed', |
| 519 | 'premium-lottie', |
| 520 | 'premium-vscroll', |
| 521 | 'premium-hscroll', |
| 522 | 'premium-nav-menu', |
| 523 | 'premium-addon-maps', |
| 524 | 'premium-woo-products-pro', |
| 525 | 'premium-addon-testimonials', |
| 526 | 'premium-smart-post-listing', |
| 527 | 'premium-addon-pricing-table', |
| 528 | 'premium-addon-image-separator', |
| 529 | 'premium-notifications', |
| 530 | ); |
| 531 | |
| 532 | } |
| 533 | |
| 534 | $elements = array_diff( $elements, $indep_elements ); |
| 535 | |
| 536 | return $elements; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Get File Content. |
| 541 | * |
| 542 | * @param string $path file path. |
| 543 | * |
| 544 | * @return string |
| 545 | */ |
| 546 | public static function get_file_content( $path ) { |
| 547 | |
| 548 | $file_content = rplg_urlopen( $path ); |
| 549 | |
| 550 | if ( isset( $file_content['code'] ) ) { |
| 551 | if ( in_array( $file_content['code'], array( 404, 401 ), true ) ) { |
| 552 | return ''; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return self::clean_content( $file_content['data'] ); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Clean Content |
| 561 | * Removes Page Html if it's returned as result. |
| 562 | * |
| 563 | * @param string $content file content. |
| 564 | * |
| 565 | * @return string |
| 566 | */ |
| 567 | public static function clean_content( $content ) { |
| 568 | |
| 569 | if ( strpos( $content, '<!DOCTYPE html>' ) ) { |
| 570 | $content = explode( '<!DOCTYPE html>', $content )[0]; |
| 571 | } |
| 572 | |
| 573 | if ( strpos( $content, '<!doctype html>' ) ) { |
| 574 | $content = explode( '<!doctype html>', $content )[0]; |
| 575 | } |
| 576 | |
| 577 | return $content; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Get File Path. |
| 582 | * Construct file path. |
| 583 | * |
| 584 | * @param string $element pa element name. |
| 585 | * @param string $ext file extension ( js|css). |
| 586 | * @param string $dir post dir (-rtl|''). |
| 587 | * |
| 588 | * @return string file path. |
| 589 | */ |
| 590 | public static function get_file_path( $element, $ext, $dir = '' ) { |
| 591 | |
| 592 | $is_pro = self::is_pro_widget( $element ); |
| 593 | |
| 594 | $papro_activated = apply_filters( 'papro_activated', false ) && version_compare( PREMIUM_PRO_ADDONS_VERSION, '2.7.1', '>' ); |
| 595 | |
| 596 | if ( ! $papro_activated && $is_pro ) { |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | $element = str_replace( '-addon', '', $element ); |
| 601 | |
| 602 | $path = $is_pro ? PREMIUM_PRO_ADDONS_URL : PREMIUM_ADDONS_URL; |
| 603 | |
| 604 | return $path . 'assets/frontend/min-' . $ext . '/' . $element . $dir . '.min.' . $ext; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Is Pro Widget. |
| 609 | * Checks if the widget is pro. |
| 610 | * |
| 611 | * @access public |
| 612 | * @since 4.6.1 |
| 613 | * |
| 614 | * @param string $widget widget name. |
| 615 | * |
| 616 | * @return bool |
| 617 | */ |
| 618 | public static function is_pro_widget( $widget ) { |
| 619 | |
| 620 | $pro_names = array_merge( array( 'common-pro', 'premium-reviews', 'premium-woo-products-pro', 'social-common' ), self::get_pro_widgets_names() ); |
| 621 | |
| 622 | return in_array( $widget, $pro_names, true ); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Has Pro Elements. |
| 627 | * Check if the post has pa pro elements. |
| 628 | * |
| 629 | * @access public |
| 630 | * @since 4.6.1 |
| 631 | * |
| 632 | * @param array $post_elems post elements. |
| 633 | * |
| 634 | * @return boolean |
| 635 | */ |
| 636 | public static function has_pro_elements( $post_elems ) { |
| 637 | |
| 638 | $papro_elems = self::get_pro_widgets_names(); |
| 639 | $has_pro = array_intersect( $post_elems, $papro_elems ) ? true : false; |
| 640 | |
| 641 | return $has_pro; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Has Free Elements. |
| 646 | * Check if the post has pa elements. |
| 647 | * |
| 648 | * @access public |
| 649 | * @since 4.6.1 |
| 650 | * |
| 651 | * @param array $post_elems post elements. |
| 652 | * |
| 653 | * @return boolean |
| 654 | */ |
| 655 | public static function has_free_elements( $post_elems ) { |
| 656 | |
| 657 | $pa_elems = Admin_Helper::get_free_widgets_names(); |
| 658 | |
| 659 | // add smart post listing |
| 660 | $pa_elems[] = 'premium-smart-post-listing'; |
| 661 | $pa_elems[] = 'premium-addon-instagram-feed'; |
| 662 | |
| 663 | $has_free = array_intersect( $post_elems, $pa_elems ) ? true : false; |
| 664 | |
| 665 | return $has_free; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Get Pro Widgets Names. |
| 670 | * |
| 671 | * @access public |
| 672 | * @since 4.6.1 |
| 673 | * |
| 674 | * @return array |
| 675 | */ |
| 676 | public static function get_pro_widgets_names() { |
| 677 | |
| 678 | $pro_elems = Admin_Helper::get_pro_elements(); |
| 679 | $pro_names = array(); |
| 680 | |
| 681 | foreach ( $pro_elems as $element ) { |
| 682 | if ( isset( $element['name'] ) ) { |
| 683 | array_push( $pro_names, $element['name'] ); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return $pro_names; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Creates and returns an instance of the class. |
| 692 | * |
| 693 | * @since 4.6.1 |
| 694 | * @access public |
| 695 | * |
| 696 | * @return object |
| 697 | */ |
| 698 | public static function get_instance() { |
| 699 | |
| 700 | if ( ! isset( self::$instance ) ) { |
| 701 | |
| 702 | self::$instance = new self(); |
| 703 | |
| 704 | } |
| 705 | |
| 706 | return self::$instance; |
| 707 | } |
| 708 | } |