PluginProbe
Brizy – Page Builder / trunk
Brizy – Page Builder vtrunk
2.8.23 2.8.22 2.8.21 2.8.20 2.8.19 2.8.18 2.8.17 2.8.16 2.8.15 2.8.14 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.15 2.7.16 All 188 releases
brizy / admin / settings.php

settings.php in Brizy – Page Builder trunk, at admin/settings.php

693 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (!defined('ABSPATH')) {
2 die('Direct access forbidden.');
3 }
4
5
6 class Brizy_Admin_Settings
7 {
8
9 private $selected_post_types;
10
11 private $role_list;
12
13 private $capability_options;
14
15 /**
16 * @var string
17 */
18 private $screenName;
19
20
21 public static function menu_slug()
22 {
23 return Brizy_Editor::prefix('-settings');
24 }
25
26 /**
27 * @return Brizy_Admin_Settings
28 */
29 public static function _init()
30 {
31
32 static $instance;
33
34 return $instance ? $instance : $instance = new self();
35 }
36
37 /**
38 * Brizy_Admin_Settings constructor.
39 */
40 private function __construct()
41 {
42
43 add_action('admin_menu', array($this, 'actionRegisterSettingsPage'));
44 if (!is_network_admin()) {
45 add_action('admin_menu', array($this, 'actionRegisterSubMenuSettingsPage'), 11);
46 add_action('admin_menu', array($this, 'actionRegisterSubMenuGetHelpLink'), 20);
47 add_action('admin_menu', array($this, 'actionRegisterSubMenuGoProPage'), 20);
48 add_action('wp_ajax_brizy_replace_url', [$this, 'replaceUrl']);
49 }
50 add_action('submenu_file', array($this, 'submenu_file'), 10, 2);
51 add_action('current_screen', array($this, 'action_validate_form_submit'));
52 add_action('brizy_settings_role_capability_row', array($this, 'role_capability_select_row'));
53 add_action('brizy_settings_post_type_row', array($this, 'post_type_row'));
54 add_action('brizy_settings_submit', array($this, 'settings_submit'));
55 add_action('brizy_settings_render_tabs', array($this, 'render_tabs'));
56 add_action('brizy_settings_render_content', array($this, 'render_tab_content'));
57 $this->role_list = self::get_role_list();
58 $this->capability_options = $this->get_capability_options();
59 try {
60 $this->selected_post_types = Brizy_Editor_Storage_Common::instance()->get('post-types');
61 } catch (Exception $e) {
62 $this->selected_post_types = array('post', 'page');
63 Brizy_Editor_Storage_Common::instance()->set('post-types', $this->selected_post_types);
64 }
65 }
66
67 /**
68 * Keep the tempate menu selected
69 *
70 * @param $submenu_file
71 * @param $parent_file
72 */
73 function submenu_file($submenu_file, $parent_file)
74 {
75 global $post_type, $pagenow;
76 if ($parent_file !== Brizy_Admin_Settings::menu_slug() || $pagenow !== 'post-new.php') {
77 return $submenu_file;
78 }
79
80 return "edit.php?post_type={$post_type}";
81 }
82
83 /**
84 * @internal
85 */
86 function actionRegisterSettingsPage()
87 {
88
89 if (!Brizy_Editor_User::is_user_allowed() || is_network_admin()) {
90 return;
91 }
92 $this->screenName = add_menu_page(Brizy_Editor::get()->get_name(), Brizy_Editor::get()->get_name(), 'read', self::menu_slug(), array(
93 $this,
94 'render'
95 ), '', '58');
96 }
97
98 /**
99 * @internal
100 */
101 function actionRegisterSubMenuSettingsPage()
102 {
103 add_submenu_page(self::menu_slug(), __('Settings', 'brizy'), __('Settings', 'brizy'), 'manage_options', self::menu_slug(), [
104 $this,
105 'render'
106 ], 0);
107 add_submenu_page(self::menu_slug(), __('Tools', 'brizy'), __('Tools', 'brizy'), 'manage_options', Brizy_Editor::prefix('-tools'), [
108 $this,
109 'renderPageTools'
110 ], 6);
111 }
112
113 /**
114 * @internal
115 */
116 function actionRegisterSubMenuGetHelpLink()
117 {
118
119 if (apply_filters('brizy_wl_enabled', false)) {
120 return;
121 }
122 if (!class_exists('BrizyPro_Main')) {
123 return;
124 }
125 global $submenu;
126 $submenu[self::menu_slug()][] = array(
127 '<span id="get-help"></span>' . __('Get Help', 'brizy'),
128 'manage_options',
129 __bt('support-url', apply_filters('brizy_support_url', Brizy_Config::SUPPORT_URL))
130 );
131 }
132
133 /**
134 * @internal
135 */
136 public function actionRegisterSubMenuGoProPage()
137 {
138
139 global $submenu;
140 if (class_exists('BrizyPro_Main')) {
141 return;
142 }
143 $submenu[self::menu_slug()][] = array(
144 '<span style="display:flex;color:#00b9eb;" id="go-pro">
145 <svg height="20" width="20">
146 <path d="M13,7 L12,7 L12,4.73333333 C12,2.6744 10.206,1 8,1 C5.794,1 4,2.6744 4,4.73333333 L4,7 L3,7 C2.448,7 2,7.41813333 2,7.93333333 L2,14.0666667 C2,14.5818667 2.448,15 3,15 L13,15 C13.552,15 14,14.5818667 14,14.0666667 L14,7.93333333 C14,7.41813333 13.552,7 13,7 Z M10,5 L12,5 L12,7 L10,7 L6,7 L6,5 C6,3.897 6.897,3 8,3 C9.103,3 10,3.897 10,5 Z" fill="#00b9eb" fill-rule="nonzero"/>
147 </svg>' . __('Go Pro', 'brizy') . "</span>",
148 'manage_options',
149 Brizy_Config::getUpgradeUrl()
150 );
151 }
152
153 private function get_selected_tab()
154 {
155 return (!empty($_REQUEST['tab'])) ? esc_attr($_REQUEST['tab']) : null;
156 }
157
158 private function get_tabs()
159 {
160 $selected_tab = $this->get_selected_tab();
161 $tabs = [
162 [
163 'id' => 'general',
164 'label' => __('General', 'brizy'),
165 'is_selected' => is_null($selected_tab) || $selected_tab == 'general',
166 'href' => menu_page_url(self::menu_slug(), false) . '&tab=general'
167 ],
168 [
169 'id' => 'roles',
170 'label' => __('Role Manager', 'brizy'),
171 'is_selected' => $selected_tab == 'roles',
172 'href' => menu_page_url(self::menu_slug(), false) . '&tab=roles'
173 ],
174 [
175 'id' => 'maintenance',
176 'label' => __('Maintenance Mode', 'brizy'),
177 'is_selected' => $selected_tab == 'maintenance',
178 'href' => menu_page_url(self::menu_slug(), false) . '&tab=maintenance'
179 ]
180 ];
181
182 return apply_filters('brizy_settings_tabs', $tabs, $selected_tab);
183 }
184
185 private function get_tab_content($tab)
186 {
187 switch ($tab) {
188 default:
189 case 'general':
190 return $this->get_general_tab();
191 break;
192 case 'roles':
193 return $this->get_roles_tab();
194 break;
195 case 'maintenance':
196 return $this->get_maintenance_tab();
197 break;
198
199 }
200 }
201
202 private function get_general_tab()
203 {
204 $list_post_types = $this->list_post_types();
205 $prepared_types = array_map(array($this, 'is_selected'), $list_post_types);
206 $svgEnabled = Brizy_Admin_Svg_Main::isSvgEnabled();
207 $jsonEnabled = Brizy_Admin_Json_Main::isJsonEnabled();
208 $gettingStartedVideoEnabled = Brizy_Admin_GettingStarted::isVideoEnabled();
209
210 return Brizy_Admin_View::render('settings/general', [
211 'types' => $prepared_types,
212 'svgUploadEnabled' => $svgEnabled,
213 'jsonUploadEnabled' => $jsonEnabled,
214 'gettingStartedVideoEnabled' => $gettingStartedVideoEnabled
215 ] );
216 }
217
218 private function get_roles_tab()
219 {
220 return Brizy_Admin_View::render('settings/roles', array(
221 'roles' => array_map(array(
222 $this,
223 'is_role_selected'
224 ), $this->list_wp_roles()),
225 ));
226 }
227
228 private function get_maintenance_tab()
229 {
230
231 $args = Brizy_MaintenanceMode::get_settings();
232 $args['pages'] = wp_list_pluck(get_pages(), 'post_title', 'ID');
233 $args['available_roles'] = $this->list_wp_roles();
234
235 return Brizy_Admin_View::render('settings/maintenance', $args);
236 }
237
238 public function settings_submit()
239 {
240
241 $screen = get_current_screen();
242 if ($this->screenName != $screen->id) {
243 return;
244 }
245 switch ($_POST['tab']) {
246 case 'general':
247 $this->general_settings_submit();
248 break;
249 case 'roles':
250 $this->roles_settings_submit();
251 break;
252 case 'maintenance':
253 $this->maintenance_settings_submit();
254 break;
255 }
256 }
257
258 public function general_settings_submit()
259 {
260 $error_count = 0;
261 $allowed_post_types = array_map(array($this, 'to_type'), $this->post_types());
262 $post_types = isset($_POST['post-types']) ? (array)$_POST['post-types'] : array();
263 $array_diff = array_diff($post_types, $allowed_post_types);
264 $svgEnabled = isset($_POST['svg-upload-enabled']) ? (bool)$_POST['svg-upload-enabled'] : false;
265 $jsonEnabled = isset($_POST['json-upload-enabled']) ? (bool)$_POST['json-upload-enabled'] : false;
266 $gettingStartedVideoEnabled = isset( $_POST['getting-started-video-enabled'] );
267 if (count($array_diff) > 0) {
268 //error
269 Brizy_Admin_Flash::instance()->add_error('Invalid post type selected');
270 $error_count++;
271 }
272 Brizy_Editor_Storage_Common::instance()->set('svg-upload', $svgEnabled);
273 Brizy_Editor_Storage_Common::instance()->set('json-upload', $jsonEnabled);
274 Brizy_Editor_Storage_Common::instance()->set( 'getting-started-video-enabled', $gettingStartedVideoEnabled );
275 if ($error_count == 0) {
276 $this->selected_post_types = $post_types;
277 Brizy_Editor_Storage_Common::instance()->set('post-types', $post_types);
278 }
279
280 }
281
282 public function maintenance_settings_submit()
283 {
284 Brizy_Editor_Storage_Common::instance()->set('maintenance', $_POST['brizy-maintenance']);
285 }
286
287 /**
288 * Return the list of allowed capabilities Ids
289 * @return array
290 */
291 public function get_capabilities()
292 {
293 $capabilities = $this->get_capability_options();
294 $caps = array();
295 foreach ($capabilities as $capability) {
296 $caps[] = $capability['capability'];
297 }
298
299 return $caps;
300 }
301
302 /**
303 * Return the list of capabilities including the label
304 *
305 * @return mixed|void
306 */
307 public function get_capability_options()
308 {
309 return apply_filters('brizy_settings_capability_options', array(
310 array('capability' => '', 'label' => __('No Access')),
311 array(
312 'capability' => Brizy_Admin_Capabilities::CAP_EDIT_WHOLE_PAGE,
313 'label' => __('Full Access', 'brizy')
314 )
315 ));
316 }
317
318 public function roles_settings_submit()
319 {
320 $allowed_roles = array_map(array($this, 'role_to_id'), $this->list_wp_roles());
321 $allowed_capabilities = $this->get_capabilities();
322 $roles = isset($_POST['role-capability']) ? (array)$_POST['role-capability'] : array();
323 if (count($roles) != 0) {
324 foreach ($roles as $role_id => $capability) {
325
326 if (!in_array($role_id, $allowed_roles)) {
327 continue;
328 }
329 // validate capability
330 if ($capability != "" && !in_array($capability, $allowed_capabilities)) {
331 continue;
332 }
333 // remove all brizy capabilities from this role
334 foreach ($allowed_capabilities as $cap) {
335 wp_roles()->remove_cap($role_id, $cap);
336 }
337 if ($capability != "") {
338 wp_roles()->add_cap($role_id, $capability);
339 }
340 }
341 }
342 }
343
344 /**
345 * @internal
346 */
347 public function render()
348 {
349
350 if (is_network_admin()) {
351 return;
352 }
353 try {
354 echo Brizy_Admin_View::render('settings/view', array());
355
356 } catch (Exception $e) {
357
358 }
359 }
360
361 public function renderPageTools()
362 {
363
364 if (is_network_admin()) {
365 return;
366 }
367 try {
368 echo Brizy_Admin_View::render('settings/tools', []);
369 } catch (Exception $e) {
370 }
371 }
372
373 public function render_tabs()
374 {
375 $tabs = $this->get_tabs();
376 foreach ($tabs as $tab) {
377 $is_active_class = $tab['is_selected'] ? 'nav-tab-active' : '';
378 ?>
379 <a href="<?php echo $tab['href'] ?>"
380 class="nav-tab <?php echo $is_active_class ?>"><?php echo __($tab['label']) ?></a>
381 <?php
382 }
383 }
384
385 public function render_tab_content()
386 {
387 $tab = $this->get_selected_tab();
388 echo apply_filters('brizy_settings_render_tab', $this->get_tab_content($tab), $tab);
389 }
390
391
392 /**
393 * @internal
394 **/
395 public function action_validate_form_submit()
396 {
397
398 if (count($_POST) == 0) {
399 return;
400 }
401 if (!isset($_POST['tab'])) {
402 return;
403 }
404 if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'])) {
405 return;
406 }
407 do_action('brizy_settings_submit');
408 if (!Brizy_Admin_Flash::instance()->has_notice_type(Brizy_Admin_Flash::ERROR)) {
409 Brizy_Admin_Flash::instance()->add_success('Settings saved.');
410 }
411 $tab = $this->get_selected_tab();
412 wp_redirect(menu_page_url($this->menu_slug(), false) . ($tab ? '&tab=' . $tab : ''));
413 exit;
414
415 }
416
417 protected function list_post_types()
418 {
419 $types = $this->post_types();
420
421 return array_map(array($this, 'post_type_to_choice'), $types);
422 }
423
424 static public function get_role_list()
425 {
426 $roles = wp_roles()->roles;
427 unset($roles['administrator']);
428 foreach ($roles as $key => $role) {
429 $roles[$key]['id'] = $key;
430 }
431 $roles = apply_filters('brizy_settings_roles', $roles);
432
433 return $roles;
434 }
435
436 /**
437 * @return array
438 */
439 public function list_wp_roles()
440 {
441 return $this->role_list;
442 }
443
444 /**
445 * @return array
446 */
447 protected function post_types()
448 {
449
450 $types = get_post_types(array('public' => true), 'objects');
451 $types = array_filter($types, array($this, 'filter_types'));
452
453 return apply_filters('brizy_settings_post_types', $types);
454 }
455
456
457 private function post_type_to_choice(WP_Post_Type $type)
458 {
459 return array(
460 'type' => $type->name,
461 'name' => $type->labels->name,
462 );
463 }
464
465 private function to_type(WP_Post_Type $type)
466 {
467 return $type->name;
468 }
469
470 private function role_to_id($value)
471 {
472 return $value['id'];
473 }
474
475
476 private function filter_types(WP_Post_Type $type)
477 {
478 return !in_array($type->name, array(
479 'attachment',
480 'elementor_library',
481 Brizy_Admin_Stories_Main::CP_STORY
482 ));
483 }
484
485 private function is_selected($type)
486 {
487 $type['selected'] = in_array($type['type'], $this->selected_post_types);
488
489 return $type;
490 }
491
492 private function is_role_selected($role)
493 {
494 $role['selected'] = !isset($role['capabilities'][Brizy_Admin_Capabilities::CAP_EDIT_WHOLE_PAGE]);
495
496 return $role;
497 }
498
499 public function role_capability_select_row($role)
500 {
501 ?>
502 <tr class="user-display-name-wrap">
503 <th><label for="display_name"><?php echo $role['name'] ?></label></th>
504 <td>
505 <select name="role-capability[<?php echo $role['id'] ?>]">
506 <?php
507 foreach ($this->capability_options as $option) {
508 ?>
509 <option value="<?php echo $option['capability'] ?>"
510 <?php echo isset($role['capabilities'][$option['capability']]) ? 'selected' : '' ?>>
511 <?php echo $option['label'] ?>
512 </option>
513 <?php
514 }
515 ?>
516 </select>
517 </td>
518 </tr>
519 <?php
520 }
521
522 public function post_type_row($type)
523 {
524 ?>
525 <label>
526 <input type="checkbox"
527 name="post-types[]"
528 value="<?php echo $type['type']; ?>"
529 <?php echo $type['selected'] ? 'checked' : ''; ?>
530 >
531 <?php echo $type['name']; ?>
532 </label>
533 <?php
534 }
535
536 public function replaceUrl()
537 {
538 check_ajax_referer('brizy-admin-nonce', 'nonce');
539 if (!current_user_can('manage_options')) {
540 wp_send_json_error(['message' => __('You must be an administrator running a replace URL session', 'brizy')]);
541 }
542 $from = trim($_POST['from']);
543 $to = trim($_POST['to']);
544 $fromEncoded = urlencode($from);
545 $toEncoded = urlencode($to);
546 $fromJsonEscaped = strtr($from, ['/' => '\/']);
547 $toJsonEscaped = strtr($to, ['/' => '\/']);
548 if ($from === $to) {
549 wp_send_json_error(['message' => __("The old and new URLs must be different", 'brizy')]);
550 }
551 if (!filter_var($from, FILTER_VALIDATE_URL) || !filter_var($to, FILTER_VALIDATE_URL)) {
552 wp_send_json_error(['message' => esc_html__("The old and new URLs must be valid URLs", 'brizy')]);
553 }
554 wp_raise_memory_limit('admin');
555 global $wpdb;
556 $offset = 0;
557 while (true) {
558
559 $rows = $wpdb->get_results("SELECT pm.meta_value, pm.post_id FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = 'brizy' AND p.post_status != 'inherit' ORDER BY pm.meta_id ASC LIMIT {$offset}, 100", ARRAY_A);
560 if (empty($rows)) {
561 break;
562 }
563 foreach ($rows as $row) {
564
565 if (($data = maybe_unserialize($row['meta_value'])) === false) {
566 continue;
567 }
568
569 if (!empty($data['brizy-post']['editor_data'])) {
570 $json = base64_decode($data['brizy-post']['editor_data']);
571 if ($json) {
572 $inEditorPlain = strpos($json, $from) !== false;
573 $inEditorEncoded = strpos($json, $fromEncoded) !== false;
574 $inEditorEscaped = strpos($json, $fromJsonEscaped) !== false;
575 if ($inEditorPlain || $inEditorEncoded || $inEditorEscaped) {
576 $json = strtr($json, [
577 $from => $to,
578 $fromEncoded => $toEncoded,
579 $fromJsonEscaped => $toJsonEscaped,
580 ]);
581 $data['brizy-post']['editor_data'] = base64_encode($json);
582 $data['brizy-post']['compiled_html'] = '';
583 update_post_meta($row['post_id'], 'brizy', $data);
584 }
585 }
586 }
587
588 $compiledRaw = get_post_meta($row['post_id'], Brizy_Editor_Post::BRIZY_POST_COMPILED_SECTIONS, true);
589 if ($compiledRaw) {
590 $compiledJson = base64_decode($compiledRaw);
591 if ($compiledJson) {
592 $newCompiledJson = strtr($compiledJson, [
593 $from => $to,
594 $fromJsonEscaped => $toJsonEscaped,
595 $fromEncoded => $toEncoded,
596 ]);
597 if ($newCompiledJson !== $compiledJson) {
598 update_post_meta($row['post_id'], Brizy_Editor_Post::BRIZY_POST_COMPILED_SECTIONS, base64_encode($newCompiledJson));
599 }
600 }
601 }
602
603 $wpPost = get_post($row['post_id']);
604 if ($wpPost && $wpPost->post_content) {
605 $newContent = strtr($wpPost->post_content, [
606 $from => $to,
607 $fromJsonEscaped => $toJsonEscaped,
608 $fromEncoded => $toEncoded,
609 ]);
610 if ($newContent !== $wpPost->post_content) {
611 $wpdb->update($wpdb->posts, ['post_content' => $newContent], ['ID' => $row['post_id']]);
612 clean_post_cache($row['post_id']);
613 }
614 }
615 }
616 $offset += 100;
617 }
618 Brizy_Editor_Post::markAllForCompilation();
619 wp_send_json_success(['message' => __('The replacement was successful', 'brizy')]);
620 }
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693