PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.5
Elementor Website Builder – more than just a page builder v3.27.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 / announcements / module.php

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

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