PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
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.7.0, at app/Hooks/Handlers/FluentBlockEditorHandler.php

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