PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.93
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.93
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 1.1.0 All 77 releases
fluent-community / Modules / FeaturesHandler.php

FeaturesHandler.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.93, at Modules/FeaturesHandler.php

197 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Services\Helper;
7 use FluentCommunity\Framework\Foundation\Application;
8 use FluentCommunity\Framework\Support\Arr;
9
10 class FeaturesHandler
11 {
12 public function register(Application $app)
13 {
14 add_filter('fluent_community/portal_page_headless', '__return_true');
15
16 $this->registerModules($app);
17
18 add_action('fluent_community/portal_header', [$this, 'maybePushDarkModeScript']);
19 add_action('fluent_community/portal_sidebar', [$this, 'maybePushDarkModeScript']);
20 add_action('init', [$this, 'maybeOembedRequest']);
21
22 add_action('fluent_community/rendering_headless_portal', [$this, 'loadHeadlessPortalAssets']);
23
24 // require the CLI
25 if (defined('WP_CLI') && WP_CLI) {
26 \WP_CLI::add_command('fluent_community', '\FluentCommunity\Modules\CLI\Commands');
27 \WP_CLI::add_command('fluent_com_dummy', '\FluentCommunity\Modules\CLI\DummyCommands');
28 }
29 }
30
31 public function loadHeadlessPortalAssets($data)
32 {
33 add_action('fluent_community/portal_head', function () use ($data) {
34 $cssFiles = Arr::get($data, 'css_files', []);
35 foreach ($cssFiles as $file) {
36 ?>
37 <link rel="stylesheet"
38 href="<?php echo esc_url($file['url']); ?>?version=<?php echo esc_attr(FLUENT_COMMUNITY_PLUGIN_VERSION); ?>"
39 media="screen"/>
40 <?php
41 }
42 });
43
44 add_action('fluent_community/before_js_loaded', function () use ($data) {
45 $jsVars = Arr::get($data, 'js_vars', []);
46 ?>
47 <script>
48 <?php foreach ($jsVars as $varKey => $values): ?>
49 var <?php echo esc_attr($varKey); ?> = <?php echo wp_json_encode($values); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>;
50 <?php endforeach; ?>
51 </script>
52 <?php
53 });
54
55 add_action('fluent_community/portal_footer', function () use ($data) {
56
57 $jsFiles = Arr::get($data, 'js_files', []);
58
59 foreach ($jsFiles as $file) {
60 ?>
61 <script type="module" src="<?php echo esc_url($file['url']); ?>?version=<?php echo esc_attr(FLUENT_COMMUNITY_PLUGIN_VERSION); ?>" defer="defer"></script>
62 <?php
63 }
64 });
65
66 }
67
68 public function maybePushDarkModeScript()
69 {
70 static $pushed;
71 if ($pushed) {
72 return;
73 }
74 $pushed = true;
75
76 if (!Helper::hasColorScheme()) {
77 return;
78 }
79
80 ?>
81 <script type="text/javascript">
82 (function () {
83 var globalStorage = localStorage.getItem('fcom_global_storage');
84 var currentMode = '';
85 if (globalStorage) {
86 globalStorage = JSON.parse(globalStorage);
87 if (globalStorage) {
88 currentMode = globalStorage.fcom_color_mode;
89 }
90 }
91
92 if (!currentMode) {
93 currentMode = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
94 }
95
96 if (currentMode === 'dark') {
97 document.documentElement.classList.add('dark');
98 }
99 })();
100 </script>
101 <?php
102 }
103
104 public function maybeOembedRequest()
105 {
106 // this is temp hack
107 if (!isset($_GET['url'])) {
108 return;
109 }
110
111 if (defined('REST_REQUEST')) {
112 return;
113 }
114
115 if (!is_user_logged_in()) {
116 return;
117 }
118
119 $requestUrl = Arr::get($_SERVER, 'REQUEST_URI');
120 if (!strpos($requestUrl, 'oembed/1.0/proxy')) {
121 return;
122 }
123
124 global $wp_embed, $wp_scripts;
125 $url = sanitize_url($_GET['url']);
126
127 $request = \FluentCommunity\App\App::make('request');
128
129 $args = $request->all();
130 unset($args['url']);
131
132 // Copy maxwidth/maxheight to width/height since WP_oEmbed::fetch() uses these arg names.
133 if (isset($args['maxwidth'])) {
134 $args['width'] = $args['maxwidth'];
135 } else {
136 $args['maxwidth'] = 600;
137 }
138
139 if (isset($args['maxheight'])) {
140 $args['height'] = $args['maxheight'];
141 }
142
143 // Short-circuit process for URLs belonging to the current site.
144 $data = get_oembed_response_data_for_url($url, $args);
145
146 if ($data) {
147 wp_send_json($data, 200);
148 }
149
150 $data = _wp_oembed_get_object()->get_data($url, $args);
151
152 if (false === $data) {
153 // Try using a classic embed, instead.
154 /* @var \WP_Embed $wp_embed */
155 $html = $wp_embed->get_embed_handler_html($args, $url);
156
157 if ($html) {
158 // Check if any scripts were enqueued by the shortcode, and include them in the response.
159 $enqueued_scripts = array();
160
161 foreach ($wp_scripts->queue as $script) {
162 $enqueued_scripts[] = $wp_scripts->registered[$script]->src;
163 }
164
165 wp_send_json(array(
166 'provider_name' => __('Embed Handler', 'fluent-community'),
167 'html' => $html,
168 'scripts' => $enqueued_scripts,
169 ), 200);
170 }
171
172 wp_send_json([
173 'error' => 'not_found',
174 ], 404);
175 }
176
177 /** This filter is documented in wp-includes/class-wp-oembed.php */
178 $data->html = apply_filters('oembed_result', _wp_oembed_get_object()->data2html((object)$data, $url), $url, $args);
179
180 wp_send_json($data, 200);
181 }
182
183 private function registerModules($app)
184 {
185 if (Helper::isFeatureEnabled('course_module')) {
186 // Regitser the modules here
187 (new \FluentCommunity\Modules\Course\CourseModule())->register($app);
188 }
189
190 if (apply_filters('fluent_community/use_editor_block', true)) {
191 (new \FluentCommunity\Modules\Gutenberg\EditorBlock())->register($app);
192 }
193
194 (new \FluentCommunity\Modules\Auth\AuthModdule())->register($app);
195 }
196 }
197