PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.5
Elementor Website Builder – more than just a page builder v4.1.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / promotions / module.php

module.php in Elementor Website Builder – more than just a page builder 4.1.5, at modules/promotions/module.php

345 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Promotions;
4
5 use Elementor\Api;
6 use Elementor\Controls_Manager;
7 use Elementor\Core\Base\Module as Base_Module;
8 use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager;
9 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Custom_Code_Menu;
10 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Custom_Elements_Menu;
11 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Fonts_Menu;
12 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Icons_Menu;
13 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Popups_Menu;
14 use Elementor\Modules\Promotions\AdminMenuItems\Editor_One_Submissions_Menu;
15 use Elementor\Modules\Promotions\AdminMenuItems\Go_Pro_Promotion_Item;
16 use Elementor\Modules\Promotions\Controls\Atomic_Promotion_Control;
17 use Elementor\Modules\Promotions\Conversion_Banner;
18 use Elementor\Modules\Promotions\Pointers\Birthday;
19 use Elementor\Modules\Promotions\Pointers\Black_Friday;
20 use Elementor\Modules\Promotions\PropTypes\Promotion_Prop_Type;
21 use Elementor\Modules\Promotions\Widgets\Ally_Dashboard_Widget;
22 use Elementor\Modules\Promotions\Widgets\Atomic_Form_Widget_Promotion;
23 use Elementor\Widgets_Manager;
24 use Elementor\Utils;
25 use Elementor\Includes\EditorAssetsAPI;
26 use Elementor\Plugin;
27 use Elementor\Modules\EditorOne\Classes\Menu_Config;
28 use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider;
29
30 if ( ! defined( 'ABSPATH' ) ) {
31 exit; // Exit if accessed directly.
32 }
33
34 class Module extends Base_Module {
35
36 const ADMIN_MENU_PRIORITY = 100;
37
38 const ADMIN_MENU_PROMOTIONS_PRIORITY = 120;
39
40 public static function is_active() {
41 return ! Utils::has_pro() || ! Utils::is_license_active();
42 }
43
44 public function get_name() {
45 return 'promotions';
46 }
47
48 public function __construct() {
49 parent::__construct();
50
51 add_filter( 'elementor/editor/localize_settings', [ $this, 'add_v4_promotions_data' ] );
52
53 if ( Utils::has_pro() ) {
54 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_react_data' ] );
55 $this->register_atomic_promotions();
56
57 return;
58 }
59
60 add_action( 'admin_init', function () {
61 $this->handle_external_redirects();
62 } );
63
64 add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
65 $this->register_editor_one_menu_items( $menu_data_provider );
66 } );
67
68 if ( Utils::is_sale_time() ) {
69 add_filter( 'add_menu_classes', [ $this, 'override_one_menu_upgrade_label_during_sale' ] );
70 }
71
72 add_action( 'elementor/widgets/register', function( Widgets_Manager $manager ) {
73 foreach ( Api::get_promotion_widgets() as $widget_data ) {
74 $manager->register( new Widgets\Pro_Widget_Promotion( [], [
75 'widget_name' => $widget_data['name'],
76 'widget_title' => $widget_data['title'],
77 ] ) );
78 }
79 } );
80
81 if ( Birthday::should_display_notice() ) {
82 new Birthday();
83 }
84
85 if ( Black_Friday::should_display_notice() ) {
86 new Black_Friday();
87 }
88
89 if ( Conversion_Banner::should_display_banner() ) {
90 new Conversion_Banner();
91 }
92
93 add_filter( 'elementor/editor/localize_settings', [ $this, 'add_editing_panel_sticky_promotion' ] );
94
95 add_action( 'elementor/controls/register', function ( Controls_Manager $controls_manager ) {
96 $controls_manager->register( new Controls\Promotion_Control() );
97 } );
98
99 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_react_data' ] );
100 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_editor_v4_alphachip' ] );
101
102 // Add Ally promo
103 Ally_Dashboard_Widget::init();
104
105 $this->register_atomic_promotions();
106 }
107
108 /**
109 * Get Ally Scanner URL
110 *
111 * @return string
112 */
113 public static function get_ally_external_scanner_url(): string {
114 return apply_filters( 'elementor/ally_external_scanner_url', 'https://go.elementor.com/acc-checker-dashboard-plg' );
115 }
116
117 private function handle_external_redirects() {
118 $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
119 if ( empty( $page ) ) {
120 return;
121 }
122
123 if ( 'go_elementor_pro' === $page ) {
124 wp_redirect( Go_Pro_Promotion_Item::get_url() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
125 die;
126 }
127 }
128
129 public function override_one_menu_upgrade_label_during_sale( $menu ) {
130 global $submenu;
131
132 $parent_slug = Menu_Config::ELEMENTOR_HOME_MENU_SLUG;
133 $upgrade_slug = 'elementor-one-upgrade';
134
135 if ( empty( $submenu[ $parent_slug ] ) ) {
136 return $menu;
137 }
138
139 foreach ( $submenu[ $parent_slug ] as &$item ) {
140 if ( isset( $item[2] ) && $upgrade_slug === $item[2] ) {
141 $item[0] = esc_html__( 'Sale!', 'elementor' ) . '<br />' . esc_html__( 'Upgrade Now', 'elementor' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
142 break;
143 }
144 }
145
146 return $menu;
147 }
148
149 private function register_editor_one_menu_items( Menu_Data_Provider $menu_data_provider ) {
150 $menu_data_provider->register_menu( new Editor_One_Custom_Elements_Menu() );
151 $menu_data_provider->register_menu( new Editor_One_Submissions_Menu() );
152 $menu_data_provider->register_menu( new Editor_One_Fonts_Menu() );
153 $menu_data_provider->register_menu( new Editor_One_Icons_Menu() );
154 $menu_data_provider->register_menu( new Editor_One_Custom_Code_Menu() );
155 $menu_data_provider->register_menu( new Editor_One_Popups_Menu() );
156 }
157
158 public function enqueue_react_data(): void {
159 if ( ! current_user_can( 'manage_options' ) ) {
160 return;
161 }
162
163 $min_suffix = Utils::is_script_debug() ? '' : '.min';
164
165 wp_enqueue_script(
166 'e-react-promotions',
167 ELEMENTOR_ASSETS_URL . 'js/e-react-promotions' . $min_suffix . '.js',
168 [
169 'react',
170 'react-dom',
171 'backbone-marionette',
172 'elementor-editor-modules',
173 'elementor-v2-ui',
174 'elementor-v2-icons',
175 ],
176 ELEMENTOR_VERSION,
177 true
178 );
179
180 wp_set_script_translations( 'e-react-promotions', 'elementor' );
181
182 wp_localize_script(
183 'e-react-promotions',
184 'elementorPromotionsData',
185 $this->get_app_js_config()
186 );
187 }
188
189 public function enqueue_editor_v4_alphachip(): void {
190 if ( ! current_user_can( 'manage_options' ) ) {
191 return;
192 }
193
194 $min_suffix = Utils::is_script_debug() ? '' : '.min';
195
196 wp_enqueue_script(
197 'editor-v4-opt-in-alphachip',
198 ELEMENTOR_ASSETS_URL . 'js/editor-v4-opt-in-alphachip' . $min_suffix . '.js',
199 [
200 'react',
201 'react-dom',
202 'elementor-common',
203 'elementor-v2-ui',
204 ],
205 ELEMENTOR_VERSION,
206 true
207 );
208 }
209
210 private function get_app_js_config(): array {
211 $editor_assets_api = new EditorAssetsAPI( $this->get_api_config() );
212 $promotion_data = new PromotionData( $editor_assets_api );
213
214 return $promotion_data->get_promotion_data();
215 }
216
217 private function get_api_config(): array {
218 return [
219 EditorAssetsAPI::ASSETS_DATA_URL => 'https://assets.elementor.com/free-to-pro-upsell/v1/free-to-pro-upsell.json',
220 EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_free_to_pro_upsell',
221 EditorAssetsAPI::ASSETS_DATA_KEY => 'free-to-pro-upsell',
222 ];
223 }
224
225 public function add_editing_panel_sticky_promotion( array $settings ): array {
226 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_panel_promotions' ) ) {
227 return $settings;
228 }
229
230 $settings['editingPanelStickyPromotion'] = Filtered_Promotions_Manager::get_editor_panel_sticky_promotion();
231
232 return $settings;
233 }
234
235 public function add_v4_promotions_data( array $settings ): array {
236 if ( ! current_user_can( 'manage_options' ) ) {
237 return $settings;
238 }
239
240 $editor_assets_api = new EditorAssetsAPI( $this->get_v4_promotions_api_config() );
241 $promotion_data = new PromotionData( $editor_assets_api );
242
243 $settings['v4Promotions'] = $promotion_data->get_v4_promotions_data();
244
245 return $settings;
246 }
247
248 private function get_v4_promotions_api_config(): array {
249 return [
250 EditorAssetsAPI::ASSETS_DATA_URL => 'https://assets.elementor.com/packages/v1/promotions.json',
251 EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_v4_promotions',
252 EditorAssetsAPI::ASSETS_DATA_KEY => 'promotions',
253 ];
254 }
255
256 private function is_atomic_widgets_active(): bool {
257 return Plugin::$instance->experiments->is_feature_active( 'e_atomic_elements' );
258 }
259
260 private function get_atomic_promotion_configs(): array {
261 return [
262 [
263 'key' => 'attributes',
264 'label' => __( 'Attributes', 'elementor' ),
265 'section' => 'settings',
266 'priority' => 40,
267 ],
268 [
269 'key' => 'display-conditions',
270 'label' => __( 'Display Conditions', 'elementor' ),
271 'section' => 'settings',
272 'priority' => 50,
273 ],
274 ];
275 }
276
277 private function register_atomic_promotions(): void {
278 add_action( 'elementor/init', function() {
279 if ( ! $this->is_atomic_widgets_active() ) {
280 return;
281 }
282
283 add_filter(
284 'elementor/atomic-widgets/props-schema',
285 [ $this, 'inject_atomic_promotion_props' ]
286 );
287
288 foreach ( $this->get_atomic_promotion_configs() as $config ) {
289 add_filter(
290 'elementor/atomic-widgets/controls',
291 fn( array $controls, $element ) => $this->inject_atomic_promotion_control( $controls, $element, $config ),
292 $config['priority'],
293 2
294 );
295 }
296 } );
297
298 ( new Atomic_Form_Widget_Promotion() )->register();
299 }
300
301 public function inject_atomic_promotion_props( array $schema ): array {
302 foreach ( $this->get_atomic_promotion_configs() as $config ) {
303 $key = $config['key'];
304
305 if ( isset( $schema[ $key ] ) ) {
306 continue;
307 }
308
309 $schema[ $key ] = Promotion_Prop_Type::make( $key );
310 }
311
312 return $schema;
313 }
314
315 protected function inject_atomic_promotion_control( array $element_controls, $atomic_element, array $config ): array {
316 $key = $config['key'];
317 $schema = $atomic_element::get_props_schema();
318
319 if ( ! array_key_exists( $key, $schema ) ) {
320 return $element_controls;
321 }
322
323 foreach ( $element_controls as $item ) {
324 if ( ! ( $item instanceof \Elementor\Modules\AtomicWidgets\Controls\Section ) ) {
325 continue;
326 }
327
328 if ( $item->get_id() !== $config['section'] ) {
329 continue;
330 }
331
332 $control = Atomic_Promotion_Control::make( $key )
333 ->set_label( $config['label'] )
334 ->set_meta( [
335 'topDivider' => true,
336 ] );
337
338 $item->add_item( $control );
339 break;
340 }
341
342 return $element_controls;
343 }
344 }
345