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