PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0-beta1
Elementor Website Builder – more than just a page builder v4.2.0-beta1
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.2.0-beta1, at modules/promotions/module.php

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