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