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