1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
<?php
namespace Elementor\Modules\Announcements;
use Elementor\Core\Base\App as BaseApp; use Elementor\Modules\Ai\Preferences; use Elementor\Modules\Announcements\Classes\Announcement; use Elementor\Settings as ElementorSettings;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
class Module extends BaseApp {
/** * @return bool */ public static function is_active(): bool { return is_admin(); }
/** * @return string */ public function get_name(): string { return 'announcements'; }
/** * Render wrapper for the app to load. */ private function render_app_wrapper() { ?> <div id="e-announcements-root"></div> <?php }
/** * Enqueue app scripts. */ private function enqueue_scripts() { wp_enqueue_script( 'announcements-app', $this->get_js_assets_url( 'announcements-app' ), [ 'wp-i18n', ], ELEMENTOR_VERSION, true );
wp_set_script_translations( 'announcements-app', 'elementor' );
$this->print_config( 'announcements-app' ); }
/** * Get initialization settings to use in frontend. * * @return array[] */ protected function get_init_settings(): array { $active_announcements = $this->get_active_announcements(); $additional_settings = [];
foreach ( $active_announcements as $announcement ) { $additional_settings[] = $announcement->get_prepared_data(); //@TODO - replace with ajax request from the front after actually triggered $announcement->after_triggered(); }
return [ 'announcements' => $additional_settings, ]; }
/** * Enqueue the module styles. */ public function enqueue_styles() { wp_enqueue_style( 'announcements-app', $this->get_css_assets_url( 'modules/announcements/announcements' ), [], ELEMENTOR_VERSION ); }
/** * Retrieve all announcement in raw format ( array ). * * @return array[] */ private function get_raw_announcements(): array { $raw_announcements = [];
if ( Preferences::is_ai_enabled( get_current_user_id() ) ) { $raw_announcements[] = $this->get_ai_announcement_data(); }
// DO NOT USE THIS FILTER return apply_filters( 'elementor/announcements/raw_announcements', $raw_announcements ); }
private function get_ai_announcement_data(): array { return [ 'title' => __( 'Discover your new superpowers ', 'elementor' ), 'description' => __( '<p>With AI for text, code, image generation and editing, you can bring your vision to life faster than ever. Start your free trial now - <b>no credit card required!</b></p>', 'elementor' ), 'media' => [ 'type' => 'image', 'src' => ELEMENTOR_ASSETS_URL . 'images/announcement.png?' . ELEMENTOR_VERSION, ], 'cta' => [ [ 'label' => __( 'Let\'s do it', 'elementor' ), 'variant' => 'primary', 'target' => '_top', 'url' => '#welcome-ai', ], [ 'label' => __( 'Skip', 'elementor' ), 'variant' => 'secondary', ], ], 'triggers' => [ [ 'action' => 'aiStarted', ], ], ]; }
/** * Retrieve all announcement objects. * * @return array */ private function get_announcements(): array { $announcements = []; foreach ( $this->get_raw_announcements() as $announcement_data ) { $announcements[] = new Announcement( $announcement_data ); }
return $announcements; }
/** * Retrieve all active announcement objects. * * @return array */ private function get_active_announcements(): array { $active_announcements = []; foreach ( $this->get_announcements() as $announcement ) { if ( $announcement->is_active() ) { $active_announcements[] = $announcement; } }
return $active_announcements; }
public function __construct() { parent::__construct();
add_action( 'elementor/init', [ $this, 'on_elementor_init' ] ); }
public function on_elementor_init() { if ( empty( $this->get_active_announcements() ) ) { return; }
add_action( 'elementor/editor/footer', function () { $this->render_app_wrapper(); } );
add_action( 'elementor/editor/after_enqueue_scripts', function () { $this->enqueue_scripts(); $this->enqueue_styles(); } ); } }
|