| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Services\Helper; |
| 7 |
use FluentCommunity\App\Vite; |
| 8 |
use FluentCommunity\Framework\Support\Arr; |
| 9 |
|
| 10 |
class PortalSettingsHandler |
| 11 |
{ |
| 12 |
public function register() |
| 13 |
{ |
| 14 |
add_filter('fluent_community/portal_data_vars', function ($vars) { |
| 15 |
if (Arr::get($vars, 'route_group') != 'admin') { |
| 16 |
return $vars; |
| 17 |
} |
| 18 |
|
| 19 |
$isRtl = Helper::isRtl(); |
| 20 |
unset($vars['js_files']['fcom_app_admin']); |
| 21 |
$vars['js_files']['fcom_app'] = [ |
| 22 |
'url' => Vite::getStaticSrcUrl('admin_app.js'), |
| 23 |
'deps' => [] |
| 24 |
]; |
| 25 |
|
| 26 |
if (!Utility::isDev()) { |
| 27 |
$vars['css_files']['fcom_admin_vendor'] = [ |
| 28 |
'url' => Vite::getStaticSrcUrl('admin_app.css', $isRtl) // vite automatically build src/admin_app.css for all components also styles inside components |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
add_filter('fluent_community/header_vars', function ($vars) { |
| 33 |
$vars['menuItems'] = []; |
| 34 |
return $vars; |
| 35 |
}); |
| 36 |
|
| 37 |
add_filter('fluent_community/will_render_default_sidebar_items', '__return_false'); |
| 38 |
|
| 39 |
add_action('fluent_community/after_header_menu', function () { |
| 40 |
echo '<h4 style="margin: 0; font-size: 20px;">' . __('Portal Settings', 'fluent-community') . '</h4>'; |
| 41 |
}); |
| 42 |
|
| 43 |
$vars['js_vars']['fluentComAdmin']['portal_slug'] = ltrim(Helper::getPortalSlug(true), '/') . '/admin'; |
| 44 |
|
| 45 |
// check if current route starts with admin |
| 46 |
return $vars; |
| 47 |
}); |
| 48 |
|
| 49 |
add_action('admin_enqueue_scripts', function () { |
| 50 |
if(isset($_GET['page']) && $_GET['page'] === 'fluent-community') { |
| 51 |
wp_enqueue_style('fluent_community_admin', Vite::getStaticSrcUrl('onboarding.css'), [], FLUENT_COMMUNITY_PLUGIN_VERSION); |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
|
| 56 |
// add a link to admin menu which will redirect to /portal |
| 57 |
add_action('admin_menu', function () { |
| 58 |
add_menu_page( |
| 59 |
__('FluentCommunity', 'fluent-community'), |
| 60 |
__('FluentCommunity', 'fluent-community'), |
| 61 |
'edit_posts', |
| 62 |
'fluent-community', |
| 63 |
[$this, 'showAdminPage'], |
| 64 |
$this->getMenuIcon(), |
| 65 |
130 |
| 66 |
); |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
public function showAdminPage() |
| 71 |
{ |
| 72 |
$jsTags = ['fluent_community_onboarding']; |
| 73 |
|
| 74 |
add_filter('script_loader_tag', function ($tag, $handle) use ($jsTags) { |
| 75 |
if (!in_array($handle, $jsTags)) { |
| 76 |
return $tag; |
| 77 |
} |
| 78 |
$tag = str_replace(' src', ' type="module" src', $tag); |
| 79 |
return $tag; |
| 80 |
}, 10, 2); |
| 81 |
|
| 82 |
wp_enqueue_script('fluent_community_onboarding', Vite::getDynamicSrcUrl('Onboarding/onboarding.js'), ['jquery'], FLUENT_COMMUNITY_PLUGIN_VERSION, [ |
| 83 |
'in_footer' => true, |
| 84 |
'strategy' => 'defer', |
| 85 |
'type' => 'module' |
| 86 |
]); |
| 87 |
|
| 88 |
wp_localize_script('fluent_community_onboarding', 'fluentComAdmin', [ |
| 89 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 90 |
'rest' => $this->getRestInfo(), |
| 91 |
'urls' => [ |
| 92 |
'site_url' => site_url('/'), |
| 93 |
'portal_base' => Helper::baseUrl('/'), |
| 94 |
'permalink_url' => admin_url('options-permalink.php') |
| 95 |
], |
| 96 |
'logo' => Helper::assetUrl('images/logo.png'), |
| 97 |
'is_onboarded' => !!Utility::getOption('onboarding_sub_settings'), |
| 98 |
'permalink_structure' => get_option('permalink_structure'), |
| 99 |
'is_slug_defined' => defined('FLUENT_COMMUNITY_PORTAL_SLUG'), |
| 100 |
'has_pro' => defined('FLUENT_COMMUNITY_PRO_VERSION'), |
| 101 |
'upgrade_url' => Utility::getProductUrl(false), |
| 102 |
'settings_page_url' => admin_url('admin.php?page=fluent-community'), |
| 103 |
'is_license_page' => isset($_GET['license']) && $_GET['license'] === 'yes', |
| 104 |
'license_url_page' => defined('FLUENT_COMMUNITY_PRO_VERSION') ? admin_url('admin.php?page=fluent-community&license=yes') : '', |
| 105 |
]); |
| 106 |
|
| 107 |
echo '<div class="wrap"><div id="fcom_onboarding_app"></div></div>'; |
| 108 |
} |
| 109 |
|
| 110 |
private function getMenuIcon() |
| 111 |
{ |
| 112 |
return 'data:image/svg+xml;base64,' . base64_encode('<svg width="82" height="71" viewBox="0 0 82 71" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 113 |
<path d="M25.9424 49.1832L39.6888 41.2467L47.6253 54.9931C40.0334 59.3763 30.3256 56.7751 25.9424 49.1832Z" fill="white"/> |
| 114 |
<path fill-rule="evenodd" clip-rule="evenodd" d="M53.4348 33.3101L39.6884 41.2466L47.6249 54.993L61.3713 47.0565L53.4348 33.3101ZM67.1821 25.3734L53.4356 33.3099L61.3721 47.0564L75.1186 39.1199L67.1821 25.3734Z" fill="white"/> |
| 115 |
<path d="M67.182 25.3736C70.978 23.182 75.8319 24.4826 78.0235 28.2786L81.9917 35.1518L75.1185 39.12L67.182 25.3736Z" fill="white"/> |
| 116 |
<path d="M42.593 30.4052L28.8466 38.3417L20.9101 24.5953L34.6565 16.6588L42.593 30.4052Z" fill="white"/> |
| 117 |
<path d="M56.3397 22.4683L42.5933 30.4048L34.6568 16.6584C42.2487 12.2752 51.9565 14.8764 56.3397 22.4683Z" fill="white"/> |
| 118 |
<path d="M28.847 38.3418L15.1006 46.2783L7.16409 32.5318L20.9105 24.5953L28.847 38.3418Z" fill="white"/> |
| 119 |
<path d="M15.1011 46.2783C11.3051 48.4699 6.4512 47.1693 4.25959 43.3733L0.291343 36.5001L7.16456 32.5319L15.1011 46.2783Z" fill="white"/> |
| 120 |
</svg>'); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
protected function getRestInfo() |
| 125 |
{ |
| 126 |
$app = fluentCommunityApp(); |
| 127 |
$ns = $app->config->get('app.rest_namespace'); |
| 128 |
$v = $app->config->get('app.rest_version'); |
| 129 |
|
| 130 |
return [ |
| 131 |
'base_url' => esc_url_raw(rest_url()), |
| 132 |
'url' => rest_url($ns . '/' . $v), |
| 133 |
'nonce' => wp_create_nonce('wp_rest'), |
| 134 |
'namespace' => $ns, |
| 135 |
'version' => $v, |
| 136 |
]; |
| 137 |
} |
| 138 |
} |
| 139 |
|