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 / editor.php

editor.php in Brizy – Page Builder trunk, at editor.php

619 lines 20.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Brizy_Editor
5 */
6 class Brizy_Editor
7 {
8
9 private static $settings_key = 'post-types';
10
11 private static $instance;
12
13 private static $is_allowed_for_current_user = null;
14 /**
15 * All plugin ajax actions and enpoints are going to be prefixed with this string.
16 * This will not affect the database prefix tables or option keys and post meta keys *
17 *
18 * @var string
19 */
20 private static $prefix = null;
21 /**
22 * @var Brizy_Content_MainProcessor
23 */
24 private $mainProcessor = null;
25
26 public static function get()
27 {
28
29 if (self::$instance) {
30 return self::$instance;
31 }
32 self::$instance = new self();
33
34 return self::$instance;
35 }
36
37 /**
38 * Return the prefix
39 *
40 * @param string $string
41 *
42 * @return string
43 */
44 public static function prefix($string = null)
45 {
46
47 $string = $string ? trim($string) : '';
48 if (!self::$prefix) {
49
50 $prefix = 'brizy';
51 if (apply_filters('brizy_wl_enabled', false)) {
52 $prefix = method_exists('BrizyPro_Admin_WhiteLabel', 'getPrefix') ? BrizyPro_Admin_WhiteLabel::_init()->getPrefix() : get_option('brizy_prefix', 'brizy');
53 }
54 self::$prefix = $prefix;
55 }
56
57 return self::$prefix . $string;
58 }
59
60 /**
61 * @param string $string
62 *
63 * @return string
64 * @deprecated
65 * Return the prefix
66 *
67 */
68 public static function setPrefix($string)
69 {
70
71 if ($string == '') {
72 throw new Exception('The prefix cannot be empty');
73 }
74 update_option('brizy_prefix', $string);
75
76 return self::$prefix = $string;
77 }
78
79
80 /**
81 * Brizy_Editor constructor.
82 */
83 private function __construct()
84 {
85
86 if (is_admin()) {
87 Brizy_SystemChecks::run();
88 }
89 // make sure the project is created
90 // do not remove this! we force the project creation here.
91 $project = Brizy_Editor_Project::get();
92 Brizy_Editor_Compiler::checkRecompileTag();
93 Brizy_Admin_Flash::instance()->initialize(); // initialize flash
94 add_action('init', array($this, 'registerCustomPostTemplates'), -4000);
95 add_action('init', array($this, 'runMigrations'), -3000);
96 add_action('init', array('Brizy_MaintenanceMode', 'init'), -4000);
97 add_action('init', array($this, 'resetPermalinks'), -2000);
98 add_action('init', array($this, 'initialize'), -2000);
99 add_action('init', array($this, 'initializeThirdParty'), -2000);
100 add_action('init', [$this, 'handleEditorEditMode'], 0);
101 add_action('wp', [$this, 'handleEditorPreviewMode']);
102 }
103
104 public function initialize()
105 {
106
107 add_action('init', array($this, 'wordpressInit'), 1000);
108 add_action('wp_loaded', array($this, 'wordpressLoaded'));
109 add_action('wp_print_scripts', array($this, 'forceJqueryQueue'), 99999);
110 add_action('pre_get_posts', array($this, 'filterSearchPostTypes'));
111 if (current_user_can(Brizy_Admin_Capabilities::CAP_EDIT_WHOLE_PAGE) || Brizy_Editor_User::is_administrator()) {
112 Brizy_Admin_Rules_Api::_init();
113 }
114 if (!defined('WP_POST_REVISIONS') || (defined('WP_POST_REVISIONS') && WP_POST_REVISIONS !== false)) {
115 add_filter("wp_revisions_to_keep", array($this, 'revisionsToKeep'), 10, 2);
116 }
117 }
118
119 public function filterSearchPostTypes($query)
120 {
121 if (!empty($_REQUEST['brz_post_type']) && is_array($_REQUEST['brz_post_type']) && $query->is_search() && !is_admin()) {
122 $allowed = array_values(get_post_types(['public' => true]));
123 $requested = array_map('sanitize_key', $_REQUEST['brz_post_type']);
124 $filtered = array_intersect($requested, $allowed);
125
126 if (!empty($filtered)) {
127 $query->set('post_type', array_values($filtered));
128 }
129 }
130 }
131
132 public function initializeThirdParty()
133 {
134 Brizy_Editor_ThirdParty_Main::_init();
135 }
136
137 public function runMigrations()
138 {
139
140 try {
141 if (!Brizy_Editor_Project::get()->getWpPost()) {
142 return;
143 }
144 } catch (Exception $e) {
145 Brizy_Admin_Flash::instance()->add_error('On run migrations, get project throw the message: ' . $e->getMessage());
146 }
147 try {
148 $migrationManager = new Brizy_Admin_Migrations();
149 $migrationManager->runMigrations(BRIZY_VERSION);
150 } catch (Brizy_Admin_Migrations_UpgradeRequiredException $e) {
151 Brizy_Admin_Flash::instance()->add_error('Please upgrade Brizy to the latest version.');
152 Brizy_Logger::instance()->critical('Unknown migration found. The plugin must be downgraded to the previous version');
153 //throw new Exception( 'Halt plugin execution!' );
154 }
155 }
156
157 public function wordpressInit()
158 {
159
160 // watch all supported posts and create meta revisions
161 $metaManager = new Brizy_Admin_Post_RevisionManager();
162 $metaManager->addMonitor(new Brizy_Admin_Post_BrizyPostsMonitor());
163 $metaManager->addMonitor(new Brizy_Admin_Post_ProjectPostMonitor());
164 //Brizy_Editor_Asset_Cleaner::_init();
165 Brizy_Admin_PanelPostContent::_init();
166 Brizy_Admin_Templates::instance();
167 Brizy_Admin_Popups_Main::_init();
168 Brizy_Admin_FormEntries::_init();
169 Brizy_Admin_Fonts_Main::_init();
170 Brizy_Admin_Blocks_Main::_init();
171 Brizy_Admin_Stories_Main::_init();
172 Brizy_Admin_Symbols_Main::_init();
173 if (Brizy_Editor_User::is_user_allowed()) {
174 Brizy_Admin_Svg_Main::_init();
175 Brizy_Admin_Json_Main::_init();
176 Brizy_Admin_Layouts_Main::_init();
177 Brizy_Admin_Membership_Membership::_init();
178 // the cloud will be always initialized with the exception when the white label is enabled
179 if (!(apply_filters('brizy_wl_enabled', false))) {
180 Brizy_Admin_Cloud::_init();
181 }
182 }
183 $this->initializeAssetLoaders();
184 $supported_post_types = $this->supported_post_types();
185 $supported_post_types[] = Brizy_Admin_Templates::CP_TEMPLATE;
186 foreach ($supported_post_types as $type) {
187 add_filter("theme_{$type}_templates", array($this, 'registerPageTemplates'));
188 }
189 }
190
191 public function wordpressLoaded()
192 {
193
194 $pid = Brizy_Editor::get()->currentPostId();
195 $post = null;
196 try {
197 // do not delete this line
198 $user = Brizy_Editor_User::get();
199 if ($pid) {
200 $post = Brizy_Editor_Entity::get($pid);
201 }
202 } catch (Exception $e) {
203
204 }
205
206 $this->loadEditorApi($post, $user);
207
208 $this->loadEditorAdminSettings();
209 if (!apply_filters('brizy_wl_enabled', false)) {
210 if (current_user_can('manage_options')) {
211 add_action('wp_dashboard_setup', 'Brizy_Admin_DashboardWidget::_init');
212 }
213 }
214 if ((current_user_can('manage_options') && is_admin()) || (defined('WP_CLI') && WP_CLI)) {
215 new Brizy_Import_Main();
216
217 }
218 add_filter('brizy_content', array($this, 'brizy_content'), 10, 3);
219 }
220
221 public function handleEditorEditMode()
222 {
223 $pid = Brizy_Editor::get()->currentPostId();
224 if (!$pid || !Brizy_Editor_Entity::isBrizyEnabled($pid) || !Brizy_Editor_User::is_user_allowed($pid)) {
225 return;
226 }
227 try {
228 $post = Brizy_Editor_Entity::get($pid);
229 } catch (Exception $e) {
230 return;
231 }
232 try {
233 $main = Brizy_Public_Main::get($post);
234 $main->editMode();
235 } catch (Exception $e) {
236 Brizy_Logger::instance()->exception($e);
237 }
238 }
239
240 public function handleEditorPreviewMode()
241 {
242 $pid = Brizy_Editor::get()->currentPostId();
243 if (!$pid || !Brizy_Editor_Entity::isBrizyEnabled($pid)) {
244 return;
245
246 }
247 try {
248 $post = Brizy_Editor_Entity::get($pid);
249 } catch (Exception $e) {
250 return;
251
252 }
253 try {
254 $context = Brizy_Content_ContextFactory::createContext(Brizy_Editor_Project::get(), $post->getWpPost());
255 $this->mainProcessor = new Brizy_Content_MainProcessor($context);
256 $main = Brizy_Public_Main::get($post);
257 $main->previewMode();
258 } catch (Exception $e) {
259 Brizy_Logger::instance()->exception($e);
260 }
261 }
262
263 public function revisionsToKeep($num, $post)
264 {
265 try {
266 $revisionCount = apply_filters('brizy_revisions_max_count', BRIZY_MAX_REVISIONS_TO_KEEP);
267 // $num can be -1
268 if ($revisionCount > $num && $num >= 0) {
269 return $num;
270 }
271 if (in_array($post->post_type, array(Brizy_Editor_Project::BRIZY_PROJECT))) {
272 return $revisionCount;
273
274 }
275 if (Brizy_Editor_Entity::isBrizyEnabled($post->ID)) {
276 $num = $revisionCount;
277 }
278 } catch (Exception $e) {
279 Brizy_Logger::instance()->debug($e->getMessage(), array($e));
280 }
281
282 return $num;
283 }
284
285 /**
286 * Reset permalinks after plugin upgrade or enable
287 */
288 public function resetPermalinks()
289 {
290
291 $this->registerCustomPostTemplates();
292 if (defined('BRIZY_PRO_VERSION') && class_exists('BrizyPro_Main')) {
293 $mainInstance = new BrizyPro_Main();
294 $mainInstance->registerCustomPosts();
295
296 }
297 if (get_option('brizy-regenerate-permalinks', false)) {
298 flush_rewrite_rules();
299 delete_option('brizy-regenerate-permalinks');
300 }
301 }
302
303 /**
304 * @param $templates
305 *
306 * @return array
307 */
308 function registerPageTemplates($templates)
309 {
310 return array_merge($templates, array(
311 Brizy_Config::BRIZY_BLANK_TEMPLATE_FILE_NAME => __bt('brizy', 'Brizy') . __(' Template', 'brizy'),
312 ));
313 }
314
315 public function registerCustomPostTemplates()
316 {
317 Brizy_Editor_Project::registerCustomPostType();
318 Brizy_Admin_Layouts_Main::registerCustomPosts();
319 Brizy_Admin_Fonts_Main::registerCustomPosts();
320 Brizy_Admin_FormEntries::registerCustomPost();
321 Brizy_Admin_Stories_Main::registerCustomPosts();
322 Brizy_Admin_Popups_Main::registerCustomPosts();
323 Brizy_Admin_Blocks_Main::registerCustomPosts();
324 Brizy_Admin_Templates::registerCustomPostTemplate();
325 }
326
327 /**
328 * @param $project
329 * @param $post
330 * @param $user
331 */
332 private function loadEditorApi($post, $user)
333 {
334 try {
335 new Brizy_Editor_RestExtend();
336 new Brizy_Editor_API($post);
337 new Brizy_Editor_BlockScreenshotApi($post);
338 Brizy_Editor_Accounts_Api::_init();
339 new Brizy_Editor_Forms_Api($post);
340 new Brizy_Editor_AuthModal();
341 // for other apis
342 do_action('brizy_register_api_methods', $user, $post);
343 } catch (Exception $e) {
344 Brizy_Logger::instance()->exception($e);
345 }
346 }
347
348 private function loadEditorAdminSettings()
349 {
350 try {
351 new Brizy_Admin_Capabilities(Brizy_Editor_Storage_Common::instance());
352 if (is_admin()) {
353 Brizy_Admin_Main::instance();
354 new Brizy_Admin_GettingStarted();
355 $this->initFeedback();
356
357 }
358 if (is_network_admin()) {
359 Brizy_Admin_NetworkSettings::_init();
360 } elseif (is_admin()) {
361 Brizy_Admin_Settings::_init();
362 }
363 } catch (Exception $exception) {
364 Brizy_Admin_Flash::instance()->add_error('Unable to empty the trash. Please try again later.');
365 wp_safe_redirect($_SERVER['HTTP_REFERER']);
366 exit;
367 }
368 }
369
370 public function brizy_content($content, $project, $wpPost, $contentType = 'document')
371 {
372
373 $context = Brizy_Content_ContextFactory::createContext($project, $wpPost);
374 if (!$this->mainProcessor) {
375 $this->mainProcessor = new Brizy_Content_MainProcessor($context);
376 } else {
377 $this->mainProcessor->setContext($context);
378 }
379
380 return $this->mainProcessor->process($content);
381 }
382
383 public function forceJqueryQueue()
384 {
385 if (!wp_script_is('jquery', 'enqueued')) {
386 wp_enqueue_script('jquery');
387 }
388 }
389
390 private function initializeAssetLoaders()
391 {
392 try {
393 $url_builder = new Brizy_Editor_UrlBuilder(null);
394 $config = null;
395 $proxy = new Brizy_Public_AssetProxy($url_builder, $config);
396 $crop_proxy = new Brizy_Public_CropProxy($url_builder, $config);
397 $attachment_proxy = new Brizy_Public_AttachmentProxy($url_builder, $config);
398 $screenshot_roxy = new Brizy_Public_BlockScreenshotProxy(new Brizy_Editor_UrlBuilder(null), $config);
399 } catch (Exception $e) {
400 Brizy_Logger::instance()->exception($e);
401 }
402 }
403
404 /*
405 * ====================================================================================================
406 * =====================================================================================================
407 * =====================================================================================================
408 * =====================================================================================================
409 * =====================================================================================================
410 */
411 function currentPostId()
412 {
413 $pid = null;
414 global $wp_query;
415 if ($wp_query && $wp_query->is_posts_page) {
416 $pid = absint(get_queried_object_id());
417 } elseif ($wp_query && ($apid = get_queried_object_id()) && (is_single() || is_page()) && $wp_query->queried_object instanceof WP_Post) {
418 $pid = absint($apid);
419 } elseif (function_exists('is_shop') && is_shop()) {
420 $pid = wc_get_page_id('shop');
421 } elseif (isset($_REQUEST[Brizy_Editor::prefix('_post')])) {
422 $pid = absint($_REQUEST[Brizy_Editor::prefix('_post')]);
423 } elseif (isset($_REQUEST['post'])) {
424 $pid = absint($_REQUEST['post']);
425 } elseif (isset($_REQUEST['page_id'])) {
426 $pid = absint($_REQUEST['page_id']);
427 } elseif (isset($_REQUEST['post_ID'])) {
428 $pid = absint($_REQUEST['post_ID']);
429 } elseif (isset($_REQUEST['id'])) {
430 $pid = absint($_REQUEST['id']);
431 }
432
433 return $pid;
434 }
435
436 static public function get_slug()
437 {
438 return apply_filters('brizy-slug', 'brizy');
439 }
440
441 public function get_path($rel = '/')
442 {
443 return BRIZY_PLUGIN_PATH . DIRECTORY_SEPARATOR . ltrim($rel, DIRECTORY_SEPARATOR);
444 }
445
446 public function get_url($rel = '')
447 {
448 return BRIZY_PLUGIN_URL . "/" . ltrim($rel, "/");
449 }
450
451 public function get_version()
452 {
453 return BRIZY_VERSION;
454 }
455
456 /**
457 * @param $wp_post_id
458 * @param bool $throw
459 *
460 * @return bool
461 * @throws Brizy_Editor_Exceptions_UnsupportedPostType
462 */
463 public static function checkIfPostTypeIsSupported($wp_post_id, $throw = true)
464 {
465 $type = get_post_type($wp_post_id);
466 $supported_post_types = self::get()->supported_post_types();
467 $supported_post_types[] = 'revision';
468 if (!in_array($type, $supported_post_types)) {
469
470 if ($throw) {
471 throw new Brizy_Editor_Exceptions_UnsupportedPostType("Brizy editor doesn't support '{$type}' post type");
472 } else {
473 return false;
474 }
475 }
476
477 return true;
478 }
479
480 public function supported_post_types()
481 {
482 $types = $this->get_post_types();
483
484 return apply_filters('brizy_supported_post_types', apply_filters('brizy:post_types', $types));
485 }
486
487 public function default_supported_post_types()
488 {
489 return array('page', 'post');
490 }
491
492 public function get_name()
493 {
494 return __bt('brizy', 'Brizy');
495 }
496
497 protected function get_post_types()
498 {
499 try {
500 return Brizy_Editor_Storage_Common::instance()->get(self::$settings_key);
501 } catch (Brizy_Editor_Exceptions_NotFound $exception) {
502 Brizy_Editor_Storage_Common::instance()->set(self::$settings_key, $this->default_supported_post_types());
503
504 return $this->default_supported_post_types();
505 }
506 }
507
508 public function lockProject()
509 {
510 if (!function_exists('wp_set_post_lock')) {
511 require_once ABSPATH . 'wp-admin/includes/post.php';
512 }
513 wp_set_post_lock(Brizy_Editor_Project::get()->getWpPostId());
514 }
515
516 public function removeProjectLock()
517 {
518 delete_post_meta(Brizy_Editor_Project::get()->getWpPostId(), '_edit_lock');
519 }
520
521 public function checkIfProjectIsLocked()
522 {
523 if (!function_exists('wp_check_post_lock')) {
524 require_once ABSPATH . 'wp-admin/includes/post.php';
525 }
526
527 return wp_check_post_lock(Brizy_Editor_Project::get()->getWpPostId());
528 }
529
530
531 private function initFeedback()
532 {
533
534 $feedback = true;
535 if (class_exists('BrizyPro_Admin_WhiteLabel')) {
536
537 $whiteLabel = BrizyPro_Admin_WhiteLabel::_init();
538 $callable = is_callable([$whiteLabel, 'getEnabled']);
539 if (($callable && $whiteLabel->getEnabled()) || !$callable) {
540 $feedback = false;
541
542 }
543 }
544 if ($feedback && current_user_can('manage_options')) {
545 new Brizy_Admin_Feedback();
546 }
547 }
548
549 /**
550 * @deprecated Use Brizy_Editor_User::is_user_allowed()
551 */
552 public static function is_user_allowed()
553 {
554
555 if (!is_user_logged_in()) {
556 return false;
557 }
558 if (current_user_can('manage_options')) {
559 return true;
560 }
561 if (is_null(self::$is_allowed_for_current_user)) {
562 self::$is_allowed_for_current_user = (current_user_can(Brizy_Admin_Capabilities::CAP_EDIT_WHOLE_PAGE) || current_user_can(Brizy_Admin_Capabilities::CAP_EDIT_CONTENT_ONLY));
563
564 }
565
566 return self::$is_allowed_for_current_user;
567 }
568
569 /**
570 * Get all image sizes.
571 *
572 * Retrieve available image sizes with data like `width`, `height` and `crop`.
573 *
574 * @return array An array of available image sizes.
575 */
576 static public function get_all_image_sizes()
577 {
578 global $_wp_additional_image_sizes;
579 static $image_sizes = [];
580 if ($image_sizes) {
581 return $image_sizes;
582
583 }
584 foreach (['thumbnail', 'medium', 'medium_large', 'large'] as $size) {
585 $image_sizes[$size] = [
586 'width' => (int)get_option($size . '_size_w'),
587 'height' => (int)get_option($size . '_size_h'),
588 'crop' => (bool)get_option($size . '_crop'),
589 ];
590 }
591 if ($_wp_additional_image_sizes) {
592 $image_sizes = array_merge($image_sizes, $_wp_additional_image_sizes);
593 }
594 /** The filter image_size_names_choose is documented in wp-admin/includes/media.php */
595 $image_sizes = array_filter(apply_filters('image_size_names_choose', $image_sizes), function ($size) {
596 return !empty($size['width']) && !empty($size['height']);
597 });
598 $translations = [
599 'thumbnail' => __('Thumbnail'),
600 'medium' => __('Medium'),
601 'large' => __('Large'),
602 'full' => __('Full Size'),
603 ];
604 foreach ($image_sizes as $sizeName => $sizeAttrs) {
605 $label = isset($translations[$sizeName]) ? $translations[$sizeName] : ucwords(str_replace('_', ' ', $sizeName));
606 if (is_array($sizeAttrs)) {
607 $label .= sprintf(' - %d x %d', $sizeAttrs['width'], $sizeAttrs['height']);
608 }
609 $image_sizes[$sizeName]['label'] = $label;
610
611 }
612 if (!array_key_exists('original', $image_sizes)) {
613 $image_sizes = ['original' => ['label' => __('Original', 'brizy')]] + $image_sizes;
614 }
615
616 return $image_sizes;
617 }
618 }
619