PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.9.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.9.1
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Hooks / Handlers / FluentBlockEditorHandler.php

FluentBlockEditorHandler.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.9.1, at app/Hooks/Handlers/FluentBlockEditorHandler.php

1,164 lines 51.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Hooks\Handlers;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\BaseSpace;
7 use FluentCommunity\App\Services\Helper;
8 use FluentCommunity\Framework\Support\Arr;
9 use FluentCommunity\Modules\Course\Model\CourseLesson;
10
11 class FluentBlockEditorHandler
12 {
13 public function register()
14 {
15 add_action('init', function () {
16
17 register_post_type('fcom-dummy', [
18 'label' => 'Lesson',
19 'public' => false,
20 'show_in_rest' => true,
21 'supports' => ['title', 'editor', 'thumbnail'],
22 ]);
23
24 register_post_type('fcom-lockscreen', [
25 'label' => 'Lockscreen',
26 'public' => false,
27 'show_in_rest' => true,
28 'supports' => ['editor'],
29 ]);
30
31 if (!isset($_REQUEST['fluent_community_block_editor'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
32 return;
33 }
34
35 if (!defined('IFRAME_REQUEST')) {
36 define('IFRAME_REQUEST', true); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
37 }
38
39 remove_action('enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets');
40 add_action('fluent_community/block_editor_head', function () {
41 $url = FLUENT_COMMUNITY_PLUGIN_URL . 'Modules/Gutenberg/editor/index.css';
42 $contentStylingUrl = FLUENT_COMMUNITY_PLUGIN_URL . 'Modules/Gutenberg/editor/content_styling.css';
43 // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- emitted into a custom non-wp_head action hook, wp_enqueue_style not applicable
44 ?>
45 <link rel="stylesheet"
46 href="<?php echo esc_url($url); ?>?version=<?php echo esc_attr(FLUENT_COMMUNITY_PLUGIN_VERSION); ?>"
47 media="screen"/>
48 <link rel="stylesheet"
49 href="<?php echo esc_url($contentStylingUrl); ?>?version=<?php echo esc_attr(FLUENT_COMMUNITY_PLUGIN_VERSION); ?>"
50 media="screen"/>
51 <?php // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet ?>
52
53 <style>
54 /* No admin bar here; WP 7.1 offsets the editor by this var, leaving a gap. */
55 html { --wp-admin--admin-bar--height: 0; }
56 <?php echo wp_strip_all_tags($this->getColorSchemaCss()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- generated CSS, no user input ?>
57 </style>
58
59 <?php
60 });
61 add_filter('should_load_separate_core_block_assets', '__return_false', 20);
62 $this->renderCustomEditor($_REQUEST); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
63
64 $actionHook = 'template_redirect';
65 if(is_admin()) {
66 $actionHook = 'admin_init';
67 }
68
69 add_action($actionHook, function () {
70 $this->renderPage();
71 exit(200);
72 }, -1000);
73 }, 2);
74 }
75
76 public function renderCustomEditor($data = [])
77 {
78 do_action('litespeed_control_set_nocache', 'fluentcommunity api request'); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
79 // set no cache headers
80 nocache_headers();
81
82 $hasAccess = false;
83 $postType = 'fcom-dummy';
84 $context = Arr::get($data, 'context');
85
86 $lesson = null;
87
88 if ($context === 'course_lesson') {
89 $lessonId = Arr::get($data, 'lesson_id');
90 if ($lessonId) {
91 $lesson = CourseLesson::find($lessonId);
92 $hasAccess = $lesson && $lesson->course && $lesson->course->isCourseAdmin();
93 }
94 }
95
96 if ($context === 'lockscreen') {
97 $postType = 'fcom-lockscreen';
98 $spaceId = Arr::get($data, 'space_id');
99 if ($spaceId) {
100 $space = BaseSpace::query()->onlyMain()->find($spaceId);
101 $hasAccess = $space && $space->isAdmin(get_current_user_id(), true);
102 }
103 }
104
105 if (!$hasAccess) {
106 echo '<h3 style="padding: 100px; text-align: center;">' . esc_html__('Sorry, you do not have access to this page.', 'fluent-community') . '</h3>';
107 exit(200);
108 }
109
110 add_filter('should_load_separate_core_block_assets', '__return_false', 20);
111 show_admin_bar(false);
112
113 $firstPost = Utility::getApp('db')->table('posts')
114 ->where('post_type', $postType)
115 ->first();
116
117 if ($firstPost) {
118 $simulatedPost = get_post($firstPost->ID);
119 $simulatedPost->post_content = '<!-- wp:paragraph --><p> </p><!-- /wp:paragraph -->';
120 } else {
121 $newPostId = wp_insert_post(array(
122 'post_title' => $context === 'course_lesson' ? 'Demo Lesson Title' : '',
123 'post_content' => '<!-- wp:paragraph --><p> </p><!-- /wp:paragraph -->',
124 'post_type' => $postType,
125 'post_status' => 'draft',
126 ));
127
128 $simulatedPost = get_post($newPostId);
129 }
130
131 global $post;
132 $post = $simulatedPost;
133
134 if ($lesson) {
135 $post->post_title = $lesson->title;
136 $post->post_content = $lesson->message ?: '<!-- wp:paragraph --><p> </p><!-- /wp:paragraph -->';
137 }
138
139 // renderPage() exits during admin_init and manually fires wp_enqueue_scripts in
140 // both the admin and frontend branches. admin_enqueue_scripts never runs here, so
141 // the apiFetch preload must ride wp_enqueue_scripts or the editor falls back to a
142 // live GET /wp/v2/fcom-dummy/{id}?context=edit — which 403s for users without
143 // edit_others_posts (e.g. contributors) on the shared, admin-authored dummy post.
144 add_action('wp_enqueue_scripts', function () use ($post) {
145 wp_enqueue_script('postbox', admin_url('js/postbox.min.js'), array('jquery-ui-sortable'), FLUENT_COMMUNITY_PLUGIN_VERSION, true);
146 wp_enqueue_style('dashicons');
147 wp_enqueue_style('media');
148 wp_enqueue_style('admin-menu');
149 wp_enqueue_style('admin-bar');
150 wp_enqueue_style('l10n');
151
152 wp_add_inline_script(
153 'wp-api-fetch',
154 \sprintf(
155 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
156 wp_json_encode(
157 array(
158 '/wp/v2/' . $post->post_type . '/' . $post->ID . '?context=edit' => array(
159 'body' => array(
160 'id' => $post->ID,
161 'title' => array('raw' => $post->post_title),
162 'content' => array(
163 'block_format' => 1,
164 'raw' => $post->post_content,
165 ),
166 'excerpt' => array('raw' => ''),
167 'date' => '',
168 'date_gmt' => '',
169 'modified' => '',
170 'modified_gmt' => '',
171 'link' => home_url('/'),
172 'guid' => array(),
173 'parent' => 0,
174 'menu_order' => 0,
175 'author' => 0,
176 'featured_media' => 0,
177 'comment_status' => 'closed',
178 'ping_status' => 'closed',
179 'template' => '',
180 'meta' => array(),
181 '_links' => array(),
182 'type' => $post->post_type,
183 'status' => 'pending', // pending is the best state to remove draft saving possibilities.
184 'slug' => '',
185 'generated_slug' => '',
186 'permalink_template' => home_url('/'),
187 ),
188 ),
189 )
190 )
191 ),
192 'after'
193 );
194 }, 11);
195
196 add_action('wp_enqueue_scripts', function ($hook) use ($post) {
197 // Gutenberg requires the post-locking functions defined within:
198 // See `show_post_locked_dialog` and `get_post_metadata` filters below.
199 include_once ABSPATH . 'wp-admin/includes/post.php';
200 $this->gutenberg_editor_scripts_and_styles($hook, $post);
201 });
202
203 // Disable post locking dialogue.
204 add_filter('show_post_locked_dialog', '__return_false');
205
206 // Everyone can richedit! This avoids a case where a page can be cached where a user can't richedit.
207 $GLOBALS['wp_rich_edit'] = true;
208 add_filter('user_can_richedit', '__return_true', 1000);
209
210 // Homepage is always locked by @wordpressdotorg
211 // This prevents other logged-in users taking a lock of the post on the front-end.
212 add_filter('get_post_metadata', function ($value, $post_id, $meta_key) {
213 if ($meta_key !== '_edit_lock') {
214 return $value;
215 }
216 return time() . ':' . get_current_user_id(); // WordPressdotorg user ID
217 }, 10, 3);
218
219 // Disable Jetpack Blocks for now.
220 add_filter('jetpack_gutenberg', '__return_false');
221 }
222
223 private function gutenberg_editor_scripts_and_styles($hook, $post)
224 {
225 $initial_edits = array(
226 'title' => $post->post_title,
227 'content' => $post->post_content,
228 'excerpt' => $post->post_excerpt,
229 );
230
231 $editor_settings = $this->getEditorSettings($post);
232
233 $init_script =
234 "(function() {
235 window._wpLoadBlockEditor = new Promise(function(resolve) {
236 wp.domReady(function() {
237 resolve(wp.editPost.initializeEditor('editor', \"%s\", %d, %s, %s));
238 });
239 });
240 })();";
241
242 $script = sprintf(
243 $init_script,
244 $post->post_type,
245 $post->ID,
246 wp_json_encode($editor_settings),
247 wp_json_encode($initial_edits)
248 );
249 wp_add_inline_script('wp-edit-post', $script);
250
251 /**
252 * Scripts
253 */
254 wp_enqueue_media();
255
256 add_filter('user_can_richedit', '__return_true');
257 wp_tinymce_inline_scripts();
258 wp_enqueue_editor();
259
260 /**
261 * Styles
262 */
263 wp_enqueue_style('wp-edit-post');
264
265 // Include block styles needed when user is not signed in. See: https://github.com/WordPress/wporg-gutenberg/issues/26
266 wp_enqueue_style('global-styles');
267 wp_enqueue_style('wp-block-library');
268 wp_enqueue_style('wp-block-image');
269 wp_enqueue_style('wp-block-group');
270 wp_enqueue_style('wp-block-heading');
271 wp_enqueue_style('wp-block-button');
272 wp_enqueue_style('wp-block-paragraph');
273 wp_enqueue_style('wp-block-separator');
274 wp_enqueue_style('wp-block-columns');
275 wp_enqueue_style('wp-block-cover');
276 wp_enqueue_style('global-styles-css-custom-properties');
277 wp_enqueue_style('wp-block-spacer');
278
279 wp_register_style('fluent_com_editor_styles', FLUENT_COMMUNITY_PLUGIN_URL . 'Modules/Gutenberg/editor/style.css', [], FLUENT_COMMUNITY_PLUGIN_VERSION, 'all');
280
281 add_action('fluent_enqueue_block_editor_assets', 'wp_enqueue_editor_format_library_assets');
282
283 // WP 6.9+ loads the LaTeX-to-MathML converter as a script module; core wires this on
284 // 'enqueue_block_editor_assets', which this standalone editor page never fires.
285 if (function_exists('wp_enqueue_block_editor_script_modules')) {
286 add_action('fluent_enqueue_block_editor_assets', 'wp_enqueue_block_editor_script_modules');
287 }
288
289 /**
290 * Fires after block assets have been enqueued for the editing interface.
291 *
292 * Call `add_action` on any hook before 'admin_enqueue_scripts'.
293 *
294 * In the function call you supply, simply use `wp_enqueue_script` and
295 * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
296 *
297 * @since 0.4.0
298 */
299 do_action('fluent_enqueue_block_editor_assets');
300
301 wp_enqueue_script('fcom_editor_custom', FLUENT_COMMUNITY_PLUGIN_URL . 'Modules/Gutenberg/editor/index.js', ['react', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-plugins'], FLUENT_COMMUNITY_PLUGIN_VERSION . time(), true);
302 wp_localize_script('fcom_editor_custom', 'fcomEditorI18n', $this->getEditorI18nStrings());
303 wp_localize_script('fcom_editor_custom', 'fcomEditorVars', [
304 'video_gate_default_threshold' => \FluentCommunity\Modules\Course\Services\LessonVideoGateService::getDefaultThreshold(),
305 'can_unfiltered_html' => current_user_can('unfiltered_html') ? 'yes' : 'no',
306 ]);
307 }
308
309 private function getEditorI18nStrings()
310 {
311 $strings = [
312 'Enable Video Embed' => __('Enable Video Embed', 'fluent-community'),
313 'Enable Comments' => __('Enable Comments', 'fluent-community'),
314 'Free Preview Lesson' => __('Free Preview Lesson', 'fluent-community'),
315 'If enabled, public users can view this lesson without enrolling the course.' => __('If enabled, public users can view this lesson without enrolling the course.', 'fluent-community'),
316 'Media Embed' => __('Media Embed', 'fluent-community'),
317 'Documents & Files' => __('Documents & Files', 'fluent-community'),
318 'Smartcodes' => __('Smartcodes', 'fluent-community'),
319 'You may use following smartcode in your lesson:' => __('You may use following smartcode in your lesson:', 'fluent-community'),
320 "User's Name:" => __("User's Name:", 'fluent-community'),
321 "User's Email:" => __("User's Email:", 'fluent-community'),
322 "User's photo HTML:" => __("User's photo HTML:", 'fluent-community'),
323 "User's Profile Link:" => __("User's Profile Link:", 'fluent-community'),
324 'Failed to fetch embed. Please check the URL.' => __('Failed to fetch embed. Please check the URL.', 'fluent-community'),
325 'Video thumbnail' => __('Video thumbnail', 'fluent-community'),
326 'Media embedded successfully' => __('Media embedded successfully', 'fluent-community'),
327 'Edit media' => __('Edit media', 'fluent-community'),
328 'Oembed' => __('Oembed', 'fluent-community'),
329 'Custom HTML' => __('Custom HTML', 'fluent-community'),
330 'Custom HTML Code' => __('Custom HTML Code', 'fluent-community'),
331 'Your account cannot save custom HTML. Scripts, iframes and similar tags are removed on save.' => __('Your account cannot save custom HTML. Scripts, iframes and similar tags are removed on save.', 'fluent-community'),
332 'Paste an iframe code' => __('Paste an iframe code', 'fluent-community'),
333 'Embed' => __('Embed', 'fluent-community'),
334 'Paste a URL to embed' => __('Paste a URL to embed', 'fluent-community'),
335 'Embed from Vimeo, YouTube, Wistia and more' => __('Embed from Vimeo, YouTube, Wistia and more', 'fluent-community'),
336 'FluentPlayer' => __('FluentPlayer', 'fluent-community'),
337 'Select Player Block' => __('Select Player Block', 'fluent-community'),
338 'Use FluentPlayer Block' => __('Use FluentPlayer Block', 'fluent-community'),
339 'The feature video is managed by the FluentPlayer block in the editor.' => __('The feature video is managed by the FluentPlayer block in the editor.', 'fluent-community'),
340 'A FluentPlayer block will be added at the top of the lesson content. Add or edit the video directly from that block in the editor.' => __('A FluentPlayer block will be added at the top of the lesson content. Add or edit the video directly from that block in the editor.', 'fluent-community'),
341 'Video Completion' => __('Video Completion', 'fluent-community'),
342 'Require video watch to complete' => __('Require video watch to complete', 'fluent-community'),
343 'Students must watch the video before they can mark this lesson as completed.' => __('Students must watch the video before they can mark this lesson as completed.', 'fluent-community'),
344 'Auto-complete lesson when the video ends' => __('Auto-complete lesson when the video ends', 'fluent-community'),
345 'Marks the lesson complete a few seconds after the student watches the video to the end.' => __('Marks the lesson complete a few seconds after the student watches the video to the end.', 'fluent-community'),
346 'Required watch percentage' => __('Required watch percentage', 'fluent-community'),
347 '100% means the video must be watched to the end.' => __('100% means the video must be watched to the end.', 'fluent-community'),
348 'Lesson Duration' => __('Lesson Duration', 'fluent-community'),
349 'Minutes' => __('Minutes', 'fluent-community'),
350 'Seconds' => __('Seconds', 'fluent-community'),
351 'View' => __('View', 'fluent-community'),
352 'No documents attached yet.' => __('No documents attached yet.', 'fluent-community'),
353 'Manage Documents & Files' => __('Manage Documents & Files', 'fluent-community'),
354 "User's Name" => __("User's Name", 'fluent-community'),
355 "User's Email" => __("User's Email", 'fluent-community'),
356 "User's photo HTML" => __("User's photo HTML", 'fluent-community'),
357 'Profile Link' => __('Profile Link', 'fluent-community'),
358 ];
359
360 return apply_filters('fluent_community/editor_i18n_strings', $strings);
361 }
362
363 private function gutenberg_get_available_image_sizes()
364 {
365 $size_names = apply_filters(
366 'fluent_community/image_size_names_choose',
367 array(
368 'thumbnail' => __('Thumbnail', 'fluent-community'),
369 'medium' => __('Medium', 'fluent-community'),
370 'large' => __('Large', 'fluent-community'),
371 'full' => __('Full Size', 'fluent-community'),
372 )
373 );
374 $all_sizes = array();
375 foreach ($size_names as $size_slug => $size_name) {
376 $all_sizes[] = array(
377 'slug' => $size_slug,
378 'name' => $size_name,
379 );
380 }
381 return $all_sizes;
382 }
383
384 protected function renderPage()
385 {
386
387 remove_action( 'wp_print_styles', 'print_emoji_styles' );
388
389 add_action('fluent_community/block_editor_footer', function () {
390 wp_underscore_playlist_templates();
391 wp_print_footer_scripts();
392 $this->printScriptModules();
393 wp_print_media_templates();
394 wp_enqueue_global_styles();
395 if (function_exists('wp_enqueue_stored_styles')) {
396 wp_enqueue_stored_styles();
397 }
398 wp_maybe_inline_styles();
399 });
400
401 add_action('fluent_block_editor/head', 'wp_enqueue_scripts', 1);
402 add_action('fluent_block_editor/head', 'wp_resource_hints', 2);
403 add_action('fluent_block_editor/head', 'wp_preload_resources', 1);
404 add_action('fluent_block_editor/head', 'wp_print_styles', 8);
405 add_action('fluent_block_editor/head', 'wp_print_head_scripts', 9);
406 add_action('fluent_block_editor/head', 'wp_custom_css_cb', 101);
407
408 $this->unloadOtherScripts();
409 ?>
410 <!DOCTYPE html>
411 <html <?php language_attributes(); ?>>
412 <head>
413 <title>FluentCommunity Block Editor</title>
414 <meta charset='utf-8'>
415 <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0,viewport-fit=cover"/>
416 <meta name="mobile-web-app-capable" content="yes">
417 <meta name="robots" content="noindex">
418 <?php do_action('fluent_block_editor/head'); ?>
419 <?php do_action('fluent_community/block_editor_head'); ?>
420 </head>
421 <body class="fcom_custom_editor">
422 <div class="wp-site-blocks">
423 <div id="editor" class="gutenberg__editor"></div>
424 </div>
425 <?php
426 do_action('fluent_community/block_editor_footer');
427 ?>
428 </body>
429 </html>
430 <?php
431 }
432
433 /**
434 * Core prints script modules on 'wp_footer' / 'admin_print_footer_scripts'. This editor page
435 * renders its own document and fires neither, so blocks relying on script modules (core/math
436 * and its LaTeX-to-MathML converter on WP 6.9+) would never load.
437 */
438 protected function printScriptModules()
439 {
440 if (!function_exists('wp_script_modules')) {
441 return;
442 }
443
444 $scriptModules = wp_script_modules();
445
446 $scriptModules->print_import_map();
447 $scriptModules->print_enqueued_script_modules();
448 $scriptModules->print_script_module_preloads();
449
450 if (method_exists($scriptModules, 'print_script_module_translations')) {
451 $scriptModules->print_script_module_translations();
452 }
453 }
454
455 private function shouldBlockAsset(string $src, string $pluginUrl, string $themesUrl, string $approvedPattern): bool
456 {
457 $isPlugin = strpos($src, $pluginUrl) !== false;
458 $isTheme = strpos($src, $themesUrl) !== false;
459
460 if (!$isPlugin && !$isTheme) {
461 return false;
462 }
463
464 return !preg_match('#' . $approvedPattern . '#', $src);
465 }
466
467 private function unloadOtherScripts()
468 {
469 // skips scripts and styles both; fluent_community/skip_no_conflict is styles only
470 $isSkip = apply_filters('fluent_com_editor/skip_no_conflict', false);
471 if ($isSkip) {
472 return;
473 }
474
475 // scripts only; styles use fluent_community/asset_listed_slugs
476 $approvedSlugs = apply_filters('fluent_com_editor/asset_listed_slugs', [
477 '\/gutenberg\/'
478 ]);
479 $approvedSlugs[] = '\/fluent-community(-pro)?\/';
480 $approvedSlugs = array_unique($approvedSlugs);
481 $approvedSlugs = implode('|', $approvedSlugs);
482
483 $pluginUrl = str_replace(['http:', 'https:'], '', plugins_url());
484
485 $themesUrl = str_replace(['http:', 'https:'], '', get_theme_root_uri());
486
487 add_filter('script_loader_src', function ($src, $handle) use ($approvedSlugs, $pluginUrl, $themesUrl) {
488 if (!$src) {
489 return $src;
490 }
491
492 if ($this->shouldBlockAsset($src, $pluginUrl, $themesUrl, $approvedSlugs)) {
493 return false;
494 }
495
496 return $src;
497 }, 1, 2);
498
499 add_action('wp_print_scripts', function () use ($approvedSlugs, $pluginUrl, $themesUrl) {
500 global $wp_scripts;
501 if (!$wp_scripts) {
502 return;
503 }
504
505 foreach ($wp_scripts->queue as $script) {
506 if (empty($wp_scripts->registered[$script]) || empty($wp_scripts->registered[$script]->src)) {
507 continue;
508 }
509
510 $src = $wp_scripts->registered[$script]->src;
511 if ($this->shouldBlockAsset($src, $pluginUrl, $themesUrl, $approvedSlugs)) {
512 wp_dequeue_script($wp_scripts->registered[$script]->handle);
513 }
514 }
515 }, 1);
516
517 add_action('wp_print_styles', function () {
518 $isSkip = apply_filters('fluent_community/skip_no_conflict', false, 'styles');
519
520 if ($isSkip) {
521 return;
522 }
523
524 global $wp_styles;
525 if (!$wp_styles) {
526 return;
527 }
528
529 // styles only; scripts use fluent_com_editor/asset_listed_slugs
530 $approvedSlugs = apply_filters('fluent_community/asset_listed_slugs', [
531 '\/gutenberg\/',
532 ]);
533
534 $approvedSlugs[] = '\/fluent-community(-pro)?\/';
535
536 $approvedSlugs = array_unique($approvedSlugs);
537 $approvedSlugs = implode('|', $approvedSlugs);
538
539 $pluginUrl = plugins_url();
540 $themeUrl = get_theme_root_uri();
541
542 $pluginUrl = str_replace(['http:', 'https:'], '', $pluginUrl);
543 $themeUrl = str_replace(['http:', 'https:'], '', $themeUrl);
544
545 foreach ($wp_styles->queue as $script) {
546
547 if (empty($wp_styles->registered[$script]) || empty($wp_styles->registered[$script]->src)) {
548 continue;
549 }
550
551 $src = $wp_styles->registered[$script]->src;
552 $pluginMatched = (strpos($src, $pluginUrl) !== false) && !preg_match('#' . $approvedSlugs . '#', $src);
553 $themeMatched = (strpos($src, $themeUrl) !== false) && !preg_match('#' . $approvedSlugs . '#', $src);
554
555 if (!$pluginMatched && !$themeMatched) {
556 continue;
557 }
558
559 wp_dequeue_style($wp_styles->registered[$script]->handle);
560 }
561 }, 999999);
562 }
563
564 private function getEditorSettings($post)
565 {
566 // Media settings.
567 $max_upload_size = wp_max_upload_size();
568 if (!$max_upload_size) {
569 $max_upload_size = 0;
570 }
571
572 $lock_details = array(
573 'isLocked' => false,
574 'user' => '',
575 );
576
577 $editor_settings = array(
578 'maxUploadFileSize' => $max_upload_size,
579 'allowedMimeTypes' => get_allowed_mime_types(),
580 'postLock' => $lock_details,
581 'postLockUtils' => array(
582 'nonce' => wp_create_nonce('lock-post_' . $post->ID),
583 'unlockNonce' => wp_create_nonce('update-post_' . $post->ID),
584 'ajaxUrl' => admin_url('admin-ajax.php'),
585 ),
586 '__experimentalFeatures' => $this->getExperimentalFeatures(),
587 'colors' => $this->getColorPalette(),
588 '__experimentalDiscussionSettings' => [
589 'avatarURL' => 'https://secure.gravatar.com/avatar/?s=96&f=y&r=g',
590 'commentOrder' => 'asc',
591 'commentsPerPage' => '50',
592 'defaultCommentsPage' => 'newest',
593 'defaultCommentStatus' => 'open',
594 'pageComments' => '',
595 'threadComments' => '1',
596 'threadCommentsDepth' => '5'
597 ],
598 '__unstableGalleryWithImageBlocks' => false,
599 '__unstableIsBlockBasedTheme' => false,
600 'enableCustomUnits' => [
601 'px',
602 'em',
603 'rem',
604 '%',
605 'vh',
606 'vw'
607 ],
608 'fontSizes' => [
609 [
610 'name' => 'Small',
611 'size' => 'var(--fcom-font-size-small)',
612 'slug' => 'small'
613 ],
614 [
615 'name' => 'Medium',
616 'size' => 'var(--fcom-font-size-medium)',
617 'slug' => 'medium'
618 ],
619 [
620 'name' => 'Large',
621 'size' => 'var(--fcom-font-size-large)',
622 'slug' => 'large'
623 ],
624 [
625 'name' => 'Larger',
626 'size' => 'var(--fcom-font-size-larger)',
627 'slug' => 'larger'
628 ],
629 [
630 'name' => 'XX-Large',
631 'size' => 'var(--fcom-font-size-xxlarge)',
632 'slug' => 'xxlarge'
633 ]
634 ],
635 'fullscreenMode' => 1,
636 'enableCustomSpacing' => 1,
637 'enableCustomLineHeight' => 1,
638 'enableCustomFields' => false,
639 'disablePostFormats' => true,
640 'disableLayoutStyles' => false,
641 'disableCustomSpacingSizes' => false,
642 'disableCustomGradients' => 1,
643 'alignWide' => true,
644 'disableCustomFontSizes' => false,
645 'disableCustomColors' => false,
646 'canUpdateBlockBindings' => false,
647 'bodyPlaceholder' => __('Start writing or type / to choose a block for your lesson content', 'fluent-community'),
648 'allowedBlockTypes' => apply_filters('fluent_community/allowed_block_types', [
649 'core/audio',
650 'core/block',
651 'core/buttons',
652 'core/button',
653 'core/code',
654 'core/columns',
655 'core/column',
656 'core/cover',
657 'core/details',
658 'core/embed',
659 'core/footnotes',
660 'core/freeform',
661 'core/gallery',
662 'core/group',
663 'core/heading',
664 'core/html',
665 'core/image',
666 'core/list',
667 'core/list-item',
668 'core/math',
669 'core/media-text',
670 'core/missing',
671 'core/paragraph',
672 'core/preformatted',
673 'core/pullquote',
674 'core/quote',
675 'core/separator',
676 'core/social-link',
677 'core/social-links',
678 'core/spacer',
679 'core/table',
680 'core/verse'
681 ]),
682 'gradients' => [],
683 'imageDefaultSize' => 'large',
684 'imageEditing' => true,
685 'isRTL' => Helper::isRtl(),
686 'autosaveInterval' => 999,
687 'localAutosaveInterval' => 999,
688 'richEditingEnabled' => true,
689 'spacingSizes' => [
690 [
691 'name' => '2X-Small',
692 'size' => '0.44rem',
693 'slug' => '20'
694 ],
695 [
696 'name' => 'X-Small',
697 'size' => '0.67rem',
698 'slug' => '30'
699 ],
700 [
701 'name' => 'Small',
702 'size' => '1rem',
703 'slug' => '40'
704 ],
705 [
706 'name' => 'Medium',
707 'size' => '1.5rem',
708 'slug' => '50'
709 ],
710 [
711 'name' => 'Large',
712 'size' => '2.25rem',
713 'slug' => '60'
714 ],
715 [
716 'name' => 'X-Large',
717 'size' => '3.38rem',
718 'slug' => '70'
719 ],
720 [
721 'name' => '2X-Large',
722 'size' => '5.06rem',
723 'slug' => '80'
724 ]
725 ],
726 'titlePlaceholder' => __('Add Lesson title', 'fluent-community')
727 );
728
729 $editor_settings['styles'] = $this->getEditorStyles();
730 $editor_settings['__unstableResolvedAssets'] = $this->getResolvedAssets();
731 $editor_settings['defaultEditorStyles'] = $this->getDefaultEditorStyles();
732 $editor_settings['imageSizes'] = $this->gutenberg_get_available_image_sizes();
733
734 $editor_settings = apply_filters('fluent_community/block_editor_settings', $editor_settings);
735 return $editor_settings;
736 }
737
738 private function getExperimentalFeatures()
739 {
740 return array(
741 'appearanceTools' => true,
742 'useRootPaddingAwareAlignments' => false,
743 'border' => [
744 'color' => 1,
745 'radius' => 1,
746 'style' => 1,
747 'width' => 1,
748 ],
749 'color' => [
750 'background' => true,
751 'button' => 1,
752 'caption' => 1,
753 'customDuotone' => 0,
754 'defaultDuotone' => 0,
755 'defaultGradients' => 0,
756 'defaultPalette' => [],
757 'duotone' => [],
758 'gradients' => [],
759 'heading' => 1,
760 'link' => 1,
761 'palette' => [
762 'default' => [],
763 'theme' => [
764 [
765 'name' => 'Accent',
766 'slug' => 'theme-palette-color-1',
767 'color' => 'var(--theme-palette-color-1)',
768 ],
769 [
770 'name' => 'Accent - alt',
771 'slug' => 'theme-palette-color-2',
772 'color' => 'var(--theme-palette-color-2)',
773 ],
774 [
775 'name' => 'Strongest text',
776 'slug' => 'theme-palette-color-3',
777 'color' => 'var(--theme-palette-color-3)',
778 ],
779 [
780 'name' => 'Strong Text',
781 'slug' => 'theme-palette-color-4',
782 'color' => 'var(--theme-palette-color-4)',
783 ],
784 [
785 'name' => 'Medium text',
786 'slug' => 'theme-palette-color-5',
787 'color' => 'var(--theme-palette-color-5)',
788 ],
789 [
790 'name' => 'Subtle Text',
791 'slug' => 'theme-palette-color-6',
792 'color' => 'var(--theme-palette-color-6)',
793 ],
794 [
795 'name' => 'Subtle Background',
796 'slug' => 'theme-palette-color-7',
797 'color' => 'var(--theme-palette-color-7)',
798 ],
799 [
800 'name' => 'Lighter Background',
801 'slug' => 'theme-palette-color-8',
802 'color' => 'var(--theme-palette-color-8)',
803 ]
804 ]
805 ],
806 'text' => true,
807 ],
808 'dimensions' => [
809 'defaultAspectRatios' => true,
810 'aspectRatios' => [
811 'default' => [
812 [
813 'name' => 'Square - 1:1',
814 'slug' => 'square',
815 'ratio' => '1',
816 ],
817 [
818 'name' => 'Standard - 4:3',
819 'slug' => '4-3',
820 'ratio' => '4/3',
821 ],
822 [
823 'name' => 'Portrait - 3:4',
824 'slug' => '3-4',
825 'ratio' => '3/4',
826 ],
827 [
828 'name' => 'Classic - 3:2',
829 'slug' => '3-2',
830 'ratio' => '3/2',
831 ],
832 [
833 'name' => 'Classic Portrait - 2:3',
834 'slug' => '2-3',
835 'ratio' => '2/3',
836 ],
837 [
838 'name' => 'Wide - 16:9',
839 'slug' => '16-9',
840 'ratio' => '16/9',
841 ],
842 [
843 'name' => 'Tall - 9:16',
844 'slug' => '9-16',
845 'ratio' => '9/16',
846 ],
847 ]
848 ],
849 'aspectRatio' => 1,
850 'minHeight' => 1,
851 ],
852 'shadow' => [
853 'defaultPresets' => true,
854 'presets' => [
855 'default' => [
856 [
857 'name' => 'Natural',
858 'slug' => 'natural',
859 'shadow' => '6px 6px 9px rgba(0, 0, 0, 0.2)',
860 ],
861 [
862 'name' => 'Deep',
863 'slug' => 'deep',
864 'shadow' => '12px 12px 50px rgba(0, 0, 0, 0.4)',
865 ],
866 [
867 'name' => 'Sharp',
868 'slug' => 'sharp',
869 'shadow' => '6px 6px 0px rgba(0, 0, 0, 0.2)',
870 ],
871 [
872 'name' => 'Outlined',
873 'slug' => 'outlined',
874 'shadow' => '6px 6px 0px -3px rgba(255, 255, 255, 1), 6px 6px rgba(0, 0, 0, 1)',
875 ],
876 [
877 'name' => 'Crisp',
878 'slug' => 'crisp',
879 'shadow' => '6px 6px 0px rgba(0, 0, 0, 1)',
880 ],
881 ],
882 ],
883 ],
884 'spacing' => [
885 'blockGap' => 1,
886 'margin' => 1,
887 'padding' => 1,
888 'defaultSpacingSizes' => true,
889 'spacingScale' => [
890 'default' => [
891 'operator' => '*',
892 'increment' => 1.5,
893 'steps' => 7,
894 'mediumStep' => 1.5,
895 'unit' => 'rem',
896 ],
897 ],
898 'spacingSizes' => [
899 'default' => [
900 [
901 'name' => '2X-Small',
902 'slug' => '20',
903 'size' => '0.44rem',
904 ],
905 [
906 'name' => 'X-Small',
907 'slug' => '30',
908 'size' => '0.67rem',
909 ],
910 [
911 'name' => 'Small',
912 'slug' => '40',
913 'size' => '1rem',
914 ],
915 [
916 'name' => 'Medium',
917 'slug' => '50',
918 'size' => '1.5rem',
919 ],
920 [
921 'name' => 'Large',
922 'slug' => '60',
923 'size' => '2.25rem',
924 ],
925 [
926 'name' => 'X-Large',
927 'slug' => '70',
928 'size' => '3.38rem',
929 ],
930 [
931 'name' => '2X-Large',
932 'slug' => '80',
933 'size' => '5.06rem',
934 ],
935 ],
936 ],
937 ],
938 'typography' => [
939 'defaultFontSizes' => NULL,
940 'dropCap' => true,
941 'fontSizes' => [
942 'default' => [
943 [
944 'name' => 'Small',
945 'slug' => 'small',
946 'size' => '13px',
947 ],
948 [
949 'name' => 'Medium',
950 'slug' => 'medium',
951 'size' => '20px',
952 ],
953 [
954 'name' => 'Large',
955 'slug' => 'large',
956 'size' => '36px',
957 ],
958 [
959 'name' => 'Extra Large',
960 'slug' => 'x-large',
961 'size' => '42px',
962 ],
963 ],
964 'theme' => [
965 [
966 'name' => 'Small',
967 'slug' => 'small',
968 'size' => 'var(--fcom-font-size-small)',
969 ],
970 [
971 'name' => 'Medium',
972 'slug' => 'medium',
973 'size' => 'var(--fcom-font-size-medium)',
974 ],
975 [
976 'name' => 'Large',
977 'slug' => 'large',
978 'size' => 'var(--fcom-font-size-large)',
979 ],
980 [
981 'name' => 'Larger',
982 'slug' => 'larger',
983 'size' => 'var(--fcom-font-size-larger)',
984 ],
985 [
986 'name' => 'XX-Large',
987 'slug' => 'xxlarge',
988 'size' => 'var(--fcom-font-size-xxlarge)',
989 ],
990 ],
991 ],
992 'fontStyle' => true,
993 'fontWeight' => true,
994 'letterSpacing' => true,
995 'textAlign' => true,
996 'textDecoration' => true,
997 'textTransform' => true,
998 'writingMode' => false,
999 'fluid' => 0,
1000 ],
1001 'blocks' => [
1002 'core/button' => [
1003 'border' => [
1004 'radius' => true,
1005 ]
1006 ],
1007 'core/image' => [
1008 'lightbox' => [
1009 'allowEditing' => true,
1010 ]
1011 ],
1012 'core/pullquote' => [
1013 'border' => [
1014 'color' => true,
1015 'radius' => true,
1016 'style' => true,
1017 'width' => true,
1018 ]
1019 ],
1020 'core/paragraph' => [
1021 'spacing' => [
1022 'margin' => 1,
1023 'padding' => 1,
1024 ]
1025 ]
1026 ],
1027 'layout' => [
1028 'contentSize' => 'var(--theme-block-max-width)',
1029 'wideSize' => 'var(--theme-block-wide-max-width)',
1030 ],
1031 'background' => [
1032 'backgroundImage' => 1,
1033 'backgroundSize' => 1,
1034 ],
1035 'position' => [
1036 'sticky' => 0,
1037 ]
1038 );
1039 }
1040
1041 private function getColorPalette()
1042 {
1043 return [
1044 [
1045 'color' => 'var(--fcom-primary-bg, #ffffff)',
1046 'name' => 'Accent',
1047 'slug' => 'theme-palette-color-1'
1048 ],
1049 [
1050 'color' => 'var(--fcom-secondary-bg, #f0f2f5)',
1051 'name' => 'Accent - alt',
1052 'slug' => 'theme-palette-color-2'
1053 ],
1054 [
1055 'color' => 'var(--fcom-secondary-text, #525866)',
1056 'name' => 'Strongest text',
1057 'slug' => 'theme-palette-color-3'
1058 ],
1059 [
1060 'color' => 'var(--fcom-secondary-content-bg, #f0f3f5)',
1061 'name' => 'Strong Text',
1062 'slug' => 'theme-palette-color-4'
1063 ],
1064 [
1065 'color' => 'var(--fcom-active-bg, #f0f3f5)',
1066 'name' => 'Medium text',
1067 'slug' => 'theme-palette-color-5'
1068 ],
1069 [
1070 'color' => 'var(--fcom-light-bg, #E1E4EA)',
1071 'name' => 'Subtle Text',
1072 'slug' => 'theme-palette-color-6'
1073 ],
1074 [
1075 'color' => 'var(--fcom-deep-bg, #E1E4EA)',
1076 'name' => 'Subtle Background',
1077 'slug' => 'theme-palette-color-7'
1078 ],
1079 [
1080 'color' => 'var(--fcom-primary-text, #19283a)',
1081 'name' => 'Lighter Background',
1082 'slug' => 'theme-palette-color-8'
1083 ]
1084 ];
1085 }
1086
1087 private function getEditorStyles()
1088 {
1089 $editorDir = FLUENT_COMMUNITY_PLUGIN_DIR . 'Modules/Gutenberg/editor/';
1090
1091 return [
1092 [
1093 '__unstableType' => 'colorSchema',
1094 'css' => $this->getColorSchemaCss(),
1095 'isGlobalStyles' => true
1096 ],
1097 [
1098 '__unstableType' => 'theme',
1099 'css' => file_get_contents($editorDir . 'editor-iframe-styles.css') ?: '',
1100 'isGlobalStyles' => true
1101 ],
1102 [
1103 'css' => file_get_contents($editorDir . 'editor.css') ?: '',
1104 '__unstableType' => 'user'
1105 ]
1106 ];
1107 }
1108
1109 private function getResolvedAssets()
1110 {
1111 $resolvedStyles = [
1112 'wp-components-css' => includes_url('/css/dist/components/style.min.css'),
1113 'wp-preferences-css' => includes_url('/css/dist/preferences/style.min.css'),
1114 'wp-block-editor-css' => includes_url('/css/dist/block-editor/style.min.css'),
1115 'wp-reusable-blocks-css' => includes_url('/css/dist/reusable-blocks/style.min.css'),
1116 'wp-patterns-css' => includes_url('/css/dist/patterns/style.min.css'),
1117 'wp-editor-css' => includes_url('/css/dist/editor/style.min.css'),
1118 'wp-block-library-css' => includes_url('/css/dist/block-library/style.min.css'),
1119 'wp-block-editor-content-css' => includes_url('/css/dist/block-editor/content.min.css'),
1120 'wp-edit-blocks-css' => includes_url('/css/dist/block-library/editor.min.css'),
1121 'fcom-content-styling' => FLUENT_COMMUNITY_PLUGIN_URL . 'Modules/Gutenberg/editor/content_styling.css'
1122 ];
1123
1124 global $wp_version;
1125 $cssFiles = '';
1126 foreach ($resolvedStyles as $name => $file) {
1127 $cssFiles .= "<link rel='stylesheet' id='{$name}' href='{$file}?ver={$wp_version}' media='all' />\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
1128 }
1129
1130 return [
1131 'scripts' => '<script src="' . includes_url('/js/dist/vendor/wp-polyfill.min.js?ver=3.15.0') . '" id="wp-polyfill-js"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
1132 'styles' => $cssFiles
1133 ];
1134 }
1135
1136 private function getDefaultEditorStyles()
1137 {
1138 return [
1139 [
1140 '__unstableType' => 'user',
1141 'css' => ':root{--wp-admin-theme-color:#007cba;--wp-admin-theme-color--rgb:0, 124, 186;--wp-admin-theme-color-darker-10:#006ba1;--wp-admin-theme-color-darker-10--rgb:0, 107, 161;--wp-admin-theme-color-darker-20:#005a87;--wp-admin-theme-color-darker-20--rgb:0, 90, 135;--wp-admin-border-width-focus:2px;--wp-block-synced-color:#7a00df;--wp-block-synced-color--rgb:122, 0, 223;--wp-bound-block-color:var(--wp-block-synced-color);}@media (min-resolution:192dpi){:root{--wp-admin-border-width-focus:1.5px;}}body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif;font-size:18px;line-height:1.5;--wp--style--block-gap:2em;}p{line-height:1.8;}.editor-post-title__block{font-size:2.5em;font-weight:800;margin-bottom:1em;margin-top:2em;}'
1142 ]
1143 ];
1144 }
1145
1146 private function getColorSchemaCss()
1147 {
1148 $colorSchema = Utility::getColorSchemaConfig();
1149
1150 $darkSchemaConfig = Arr::get($colorSchema, 'dark');
1151
1152 $colorSchemaCss = ':root {';
1153 foreach (Arr::get($darkSchemaConfig, 'body', []) as $colorKey => $value) {
1154 if ($value) {
1155 $cssVar = ' --fcom-' . str_replace('_', '-', $colorKey);
1156 $colorSchemaCss .= $cssVar . ':' . $value . '; ';
1157 }
1158 }
1159 $colorSchemaCss .= '}';
1160
1161 return $colorSchemaCss;
1162 }
1163 }
1164