PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.0-dev2
Elementor Website Builder – more than just a page builder v3.20.0-dev2
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 / core / utils / hints.php

hints.php in Elementor Website Builder – more than just a page builder 3.20.0-dev2, at core/utils/hints.php

347 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace elementor\core\utils;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use Elementor\User;
9
10 class Hints {
11 const INFO = 'info';
12 const SUCCESS = 'success';
13 const WARNING = 'warning';
14 const DANGER = 'danger';
15
16 const DEFINED = 'defined';
17 const DISMISSED = 'dismissed';
18 const CAPABILITY = 'capability';
19 const PLUGIN_INSTALLED = 'plugin_installed';
20 const PLUGIN_ACTIVE = 'plugin_active';
21
22 /**
23 * get_notice_types
24 * @return string[]
25 */
26 public static function get_notice_types(): array {
27 return [
28 self::INFO,
29 self::SUCCESS,
30 self::WARNING,
31 self::DANGER,
32 ];
33 }
34
35 /**
36 * get_hints
37 *
38 * @param $hint_key
39 *
40 * @return array|string[]|\string[][]
41 */
42 public static function get_hints( $hint_key = null ): array {
43 $hints = [
44 'image-optimization-once' => [
45 self::DISMISSED => 'image-optimization-once',
46 self::CAPABILITY => 'install_plugins',
47 self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
48 ],
49 'image-optimization' => [
50 self::DISMISSED => 'image_optimizer_hint',
51 self::CAPABILITY => 'install_plugins',
52 self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
53 ],
54 ];
55 if ( ! $hint_key ) {
56 return $hints;
57 }
58
59 return $hints[ $hint_key ] ?? [];
60 }
61
62 /**
63 * get_notice_icon
64 * @return string
65 */
66 public static function get_notice_icon(): string {
67 return '<div class="elementor-control-notice-icon">
68 <svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
69 <path d="M2.25 9H3M9 2.25V3M15 9H15.75M4.2 4.2L4.725 4.725M13.8 4.2L13.275 4.725M7.27496 12.75H10.725M6.75 12C6.12035 11.5278 5.65525 10.8694 5.42057 10.1181C5.1859 9.36687 5.19355 8.56082 5.44244 7.81415C5.69133 7.06748 6.16884 6.41804 6.80734 5.95784C7.44583 5.49764 8.21294 5.25 9 5.25C9.78706 5.25 10.5542 5.49764 11.1927 5.95784C11.8312 6.41804 12.3087 7.06748 12.5576 7.81415C12.8065 8.56082 12.8141 9.36687 12.5794 10.1181C12.3448 10.8694 11.8796 11.5278 11.25 12C10.9572 12.2899 10.7367 12.6446 10.6064 13.0355C10.4761 13.4264 10.4397 13.8424 10.5 14.25C10.5 14.6478 10.342 15.0294 10.0607 15.3107C9.77936 15.592 9.39782 15.75 9 15.75C8.60218 15.75 8.22064 15.592 7.93934 15.3107C7.65804 15.0294 7.5 14.6478 7.5 14.25C7.56034 13.8424 7.52389 13.4264 7.3936 13.0355C7.2633 12.6446 7.04282 12.2899 6.75 12Z" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
70 </svg>
71 </div>';
72 }
73
74 /**
75 * get_notice_template
76 *
77 * Print or Retrieve the notice template.
78 * @param array $notice
79 * @param bool $return
80 *
81 * @return string|void
82 */
83 public static function get_notice_template( array $notice, bool $return = false ) {
84 $default_settings = [
85 'type' => 'info',
86 'icon' => false,
87 'heading' => '',
88 'content' => '',
89 'dismissible' => false,
90 'button_text' => '',
91 'button_event' => '',
92 'button_data' => [],
93 'display' => false,
94 ];
95 $notice_settings = array_merge( $default_settings, $notice );
96
97 if ( empty( $notice_settings['heading'] ) && empty( $notice_settings['content'] ) ) {
98 return '';
99 }
100
101 if ( ! in_array( $notice_settings['type'], self::get_notice_types(), true ) ) {
102 $notice_settings['type'] = 'info';
103 }
104
105 $icon = '';
106 $heading = '';
107 $content = '';
108 $dismissible = '';
109 $button = '';
110
111 if ( $notice_settings['icon'] ) {
112 $icon = self::get_notice_icon();
113 }
114
115 if ( ! empty( $notice_settings['heading'] ) ) {
116 $heading = '<div class="elementor-control-notice-main-heading">' . $notice_settings['heading'] . '</div>';
117 }
118
119 if ( ! empty( $notice_settings['content'] ) ) {
120 $content = '<div class="elementor-control-notice-main-content">' . $notice_settings['content'] . '</div>';
121 }
122
123 if ( ! empty( $notice_settings['button_text'] ) ) {
124 $button_settings = ( ! empty( $notice_settings['button_data'] ) ) ? ' data-settings="' . esc_attr( json_encode( $notice_settings['button_data'] ) ) . '"' : '';
125 $button = '<div class="elementor-control-notice-main-actions">
126 <button type="button" class="e-btn e-' . $notice_settings['type'] . ' e-btn-1" data-event="' . $notice_settings['button_event'] . '"' . $button_settings . '>
127 ' . $notice_settings['button_text'] . '
128 </button>
129 </div>';
130 }
131
132 if ( $notice_settings['dismissible'] ) {
133 $dismissible = '<button class="elementor-control-notice-dismiss tooltip-target" data-event="' . $notice_settings['dismissible'] . '" data-tooltip="' . esc_attr__( 'Don’t show again.', 'elementor' ) . '">
134 <i class="eicon eicon-close" aria-hidden="true"></i>
135 <span class="elementor-screen-only">' . esc_html__( 'Don’t show again.', 'elementor' ) . '</span>
136 </button>';
137 }
138
139 $notice_template = sprintf( '<div class="elementor-control-notice elementor-control-notice-type-%1$s" data-display="%7$s">
140 %2$s
141 <div class="elementor-control-notice-main">
142 %3$s
143 %4$s
144 %5$s
145 </div>
146 %6$s
147 </div>',
148 $notice_settings['type'],
149 $icon,
150 $heading,
151 $content,
152 $button,
153 $dismissible,
154 $notice_settings['display']
155 );
156
157 if ( $return ) {
158 return $notice_template;
159 }
160 echo wp_kses( $notice_template, self::get_notice_allowed_html() );
161 }
162
163 /**
164 * get_plugin_install_url
165 * @param $plugin_slug
166 *
167 * @return string
168 */
169 public static function get_plugin_install_url( $plugin_slug ): string {
170 $action = 'install-plugin';
171 return wp_nonce_url(
172 add_query_arg(
173 [
174 'action' => $action,
175 'plugin' => $plugin_slug,
176 ],
177 admin_url( 'update.php' )
178 ),
179 $action . '_' . $plugin_slug
180 );
181 }
182
183 /**
184 * get_plugin_activate_url
185 * @param $plugin_slug
186 *
187 * @return string
188 */
189 public static function get_plugin_activate_url( $plugin_slug ): string {
190 return admin_url( 'plugins.php' );
191 }
192
193 /**
194 * is_dismissed
195 * @param $key
196 *
197 * @return bool
198 */
199 public static function is_dismissed( $key ): bool {
200 $dismissed = User::get_dismissed_editor_notices();
201 return in_array( $key, $dismissed, true );
202 }
203
204 /**
205 * should_display_hint
206 * @param $hint_key
207 *
208 * @return bool
209 */
210 public static function should_display_hint( $hint_key ): bool {
211 $hint = self::get_hints( $hint_key );
212 if ( empty( $hint ) ) {
213 return false;
214 }
215
216 foreach ( $hint as $key => $value ) {
217 switch ( $key ) {
218 case self::DISMISSED:
219 if ( self::is_dismissed( $value ) ) {
220 return false;
221 }
222 break;
223 case self::CAPABILITY:
224 if ( ! current_user_can( $value ) ) {
225 return false;
226 }
227 break;
228 case self::DEFINED:
229 if ( defined( $value ) ) {
230 return false;
231 }
232 break;
233 case self::PLUGIN_INSTALLED:
234 if ( ! self::is_plugin_installed( $value ) ) {
235 return false;
236 }
237 break;
238 case self::PLUGIN_ACTIVE:
239 if ( ! self::is_plugin_active( $value ) ) {
240 return false;
241 }
242 break;
243 }
244 }
245 return true;
246 }
247
248 /**
249 * is_plugin_installed
250 * @param $plugin
251 *
252 * @return bool
253 */
254 public static function is_plugin_installed( $plugin ) : bool {
255 $plugins = get_plugins();
256 $plugin = self::ensure_plugin_folder( $plugin );
257 return ! empty( $plugins[ $plugin ] );
258 }
259
260 /**
261 * is_plugin_active
262 * @param $plugin
263 *
264 * @return bool
265 */
266 public static function is_plugin_active( $plugin ): bool {
267 $plugin = self::ensure_plugin_folder( $plugin );
268 return is_plugin_active( $plugin );
269 }
270
271 /**
272 * get_plugin_action_url
273 * @param $plugin
274 *
275 * @return string
276 */
277 public static function get_plugin_action_url( $plugin ): string {
278 if ( ! self::is_plugin_installed( $plugin ) ) {
279 return self::get_plugin_install_url( $plugin );
280 }
281
282 if ( ! self::is_plugin_active( $plugin ) ) {
283 return self::get_plugin_activate_url( $plugin );
284 }
285
286 return '';
287 }
288
289 /**
290 * ensure_plugin_folder
291 * @param $plugin
292 *
293 * @return string
294 */
295 private static function ensure_plugin_folder( $plugin ): string {
296 if ( false === strpos( $plugin, '/' ) ) {
297 $plugin = $plugin . '/' . $plugin . '.php';
298 }
299 return $plugin;
300 }
301
302 /**
303 * get_notice_allowed_html
304 * @return array[]
305 */
306 public static function get_notice_allowed_html(): array {
307 return [
308 'div' => [
309 'class' => [],
310 'data-display' => [],
311 ],
312 'svg' => [
313 'width' => [],
314 'height' => [],
315 'viewbox' => [],
316 'fill' => [],
317 'xmlns' => [],
318 ],
319 'path' => [
320 'd' => [],
321 'stroke' => [],
322 'stroke-width' => [],
323 'stroke-linecap' => [],
324 'stroke-linejoin' => [],
325 ],
326 'button' => [
327 'class' => [],
328 'data-event' => [],
329 'data-settings' => [],
330 'data-tooltip' => [],
331 ],
332 'i' => [
333 'class' => [],
334 'aria-hidden' => [],
335 ],
336 'span' => [
337 'class' => [],
338 ],
339 'a' => [
340 'href' => [],
341 'style' => [],
342 'target' => [],
343 ],
344 ];
345 }
346 }
347