class-elementor-controller.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Elementor\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Data\Controllers\RestController; |
| 8 | use SuperbAddons\Library\Controllers\FavoritesController; |
| 9 | use SuperbAddons\Library\Controllers\LibraryController; |
| 10 | use SuperbAddons\Library\Controllers\LibraryRequestController; |
| 11 | use SuperbAddons\Elementor\Utils\ElementorSourceExtension; |
| 12 | use Exception; |
| 13 | use SuperbAddons\Admin\Utils\AdminLinkSource; |
| 14 | use SuperbAddons\Admin\Utils\AdminLinkUtil; |
| 15 | |
| 16 | class ElementorController |
| 17 | { |
| 18 | const MINIMUM_ELEMENTOR_VERSION = '3.0'; |
| 19 | const MINIMUM_PHP_VERSION = '5.6'; |
| 20 | |
| 21 | const IMPORT_COMPLETION_ACTION = 'superbaddons_elementorimport_completed_'; |
| 22 | |
| 23 | public function __construct() |
| 24 | { |
| 25 | if (!self::is_compatible()) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | add_action('elementor/editor/before_enqueue_scripts', array($this, 'elementor_editor_scripts')); |
| 30 | add_action('elementor/editor/footer', array($this, 'elementor_editor_footer_scripts')); |
| 31 | add_action('elementor/editor/after_enqueue_styles', array($this, 'elementor_editor_enqueue_styles')); |
| 32 | add_action('elementor/preview/enqueue_styles', array($this, 'elementor_preview_enqueue_styles')); |
| 33 | } |
| 34 | |
| 35 | public static function is_installed_and_activated() |
| 36 | { |
| 37 | return did_action('elementor/loaded'); |
| 38 | } |
| 39 | |
| 40 | public static function is_compatible() |
| 41 | { |
| 42 | // Check if Elementor installed and activated |
| 43 | if (!self::is_installed_and_activated()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // Check for required Elementor version |
| 48 | if (!defined("ELEMENTOR_VERSION") || version_compare(ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '<')) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // Check for required PHP version |
| 53 | if (version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '<')) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | public static function GetElementorLibraryMenuItems() |
| 61 | { |
| 62 | return array( |
| 63 | array( |
| 64 | "id" => "sections", |
| 65 | "premium_url" => AdminLinkUtil::GetLink(AdminLinkSource::LIBRARY_ITEM), |
| 66 | "title" => esc_html__('Sections', "superb-blocks"), |
| 67 | "routes" => array( |
| 68 | "list" => LibraryRequestController::ELEMENTOR_LIST_ROUTE, |
| 69 | "insert" => LibraryRequestController::ELEMENTOR_INSERT_ROUTE |
| 70 | ), |
| 71 | "hidden" => true |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function elementor_editor_scripts() |
| 77 | { |
| 78 | wp_enqueue_script('superb-elementor-layout-library', SUPERBADDONS_ASSETS_PATH . '/js/elementor/layout-library.js', array('jquery', 'wp-element'), SUPERBADDONS_VERSION, true); |
| 79 | wp_localize_script('superb-elementor-layout-library', 'superblayoutlibrary_g', array( |
| 80 | "title" => esc_html__('Add Superb Template', "superb-blocks"), |
| 81 | "logoUrl" => esc_url(SUPERBADDONS_ASSETS_PATH . '/img/icon-superb.svg'), |
| 82 | "style_placeholder" => esc_html__('All themes', "superb-blocks"), |
| 83 | "category_placeholder" => esc_html__('All categories', "superb-blocks"), |
| 84 | "snacks" => array( |
| 85 | "insert_error" => esc_html__('Something went wrong while attempting to insert this element. Please try again or contact support if the problem persists.', "superb-blocks"), |
| 86 | "list_error" => esc_html__('Something went wrong while attempting to list elements. Please try again or contact support if the problem persists.', "superb-blocks") |
| 87 | ), |
| 88 | "menu_items" => self::GetElementorLibraryMenuItems(), |
| 89 | "favorites" => FavoritesController::GetFavorites(get_current_user_id()), |
| 90 | "rest" => array( |
| 91 | "base" => \get_rest_url(), |
| 92 | "namespace" => RestController::NAMESPACE, |
| 93 | "nonce" => wp_create_nonce("wp_rest") |
| 94 | ) |
| 95 | )); |
| 96 | } |
| 97 | |
| 98 | public function elementor_editor_footer_scripts() |
| 99 | { |
| 100 | LibraryController::InsertTemplatesWithWrapper(); |
| 101 | } |
| 102 | |
| 103 | public function elementor_editor_enqueue_styles() |
| 104 | { |
| 105 | wp_enqueue_style( |
| 106 | 'superb-addons-elements', |
| 107 | SUPERBADDONS_ASSETS_PATH . '/css/framework.min.css', |
| 108 | array(), |
| 109 | SUPERBADDONS_VERSION |
| 110 | ); |
| 111 | wp_enqueue_style( |
| 112 | 'superb-elementor-editor-layout-library', |
| 113 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-editor.min.css', |
| 114 | array(), |
| 115 | SUPERBADDONS_VERSION |
| 116 | ); |
| 117 | wp_enqueue_style( |
| 118 | 'superbaddons-toast', |
| 119 | SUPERBADDONS_ASSETS_PATH . '/css/toast.min.css', |
| 120 | array(), |
| 121 | SUPERBADDONS_VERSION |
| 122 | ); |
| 123 | wp_enqueue_style( |
| 124 | 'superb-addons-font-manrope', |
| 125 | SUPERBADDONS_ASSETS_PATH . '/fonts/manrope/manrope.css', |
| 126 | array(), |
| 127 | SUPERBADDONS_VERSION |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | public function elementor_preview_enqueue_styles() |
| 132 | { |
| 133 | wp_enqueue_style( |
| 134 | 'superb-elementor-layout-library', |
| 135 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css', |
| 136 | array(), |
| 137 | SUPERBADDONS_VERSION |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | public static function ElementorDataImportAction($data) |
| 142 | { |
| 143 | $dynamic_action = time(); |
| 144 | self::MaybeHandleBadFunctions($dynamic_action); |
| 145 | $source = new ElementorSourceExtension(); |
| 146 | $source->HandleImport($data['content']); |
| 147 | try { |
| 148 | // constant contains plugin prefix |
| 149 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound |
| 150 | do_action(self::IMPORT_COMPLETION_ACTION . $dynamic_action); |
| 151 | return $data; |
| 152 | } catch (Exception $ex) { |
| 153 | LogController::HandleException($ex); |
| 154 | //Completion action failed |
| 155 | //Catch exception to ensure content response |
| 156 | return $data; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private static function MaybeHandleBadFunctions($action_affix = "") |
| 161 | { |
| 162 | // This option from Royal Addons (royal-elementor-addons), when enabled, creates and uploads multiple identical placeholder images in the user's media library on every insert unless disabled. |
| 163 | // They even disable it and re-enable it themselves during their own insert function. |
| 164 | if (class_exists("WprAddons\\Plugin")) { |
| 165 | $wpr_args = array( |
| 166 | 'wpr-parallax-background' => get_option('wpr-parallax-background'), |
| 167 | 'wpr-parallax-multi-layer' => get_option('wpr-parallax-multi-layer') |
| 168 | ); |
| 169 | update_option('wpr-parallax-background', ''); |
| 170 | update_option('wpr-parallax-multi-layer', ''); |
| 171 | |
| 172 | // constant contains plugin prefix |
| 173 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound |
| 174 | add_action(self::IMPORT_COMPLETION_ACTION . $action_affix, function () use ($wpr_args) { |
| 175 | update_option('wpr-parallax-background', $wpr_args['wpr-parallax-background']); |
| 176 | update_option('wpr-parallax-multi-layer', $wpr_args['wpr-parallax-multi-layer']); |
| 177 | }); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 |