*/
class Aec_Public
{
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
* @since 1.0.0
*/
public function __construct($plugin_name, $version)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
}
/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_styles()
{
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Aec_Loader as all of the hooks are defined
* in that particular class.
*
* The Aec_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/aec-public.css', array(), $this->version, 'all');
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts()
{
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Aec_Loader as all of the hooks are defined
* in that particular class.
*
* The Aec_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/aec-public.js', ['jquery'], $this->version, false);
$data = [
'aec_load_kiosque_aec_build' => get_option('aec_load_kiosque_aec_build'),
];
wp_localize_script($this->plugin_name, 'aec_options', $data);
}
public function enqueue_extranet_footer()
{
if (get_option('aec_load_kiosque_aec_build') !== 'on' || is_admin()) {
$this->load_webapp_aec();
} else {
$this->load_kiosque_aec();
}
}
private function load_webapp_aec()
{
if (wp_get_environment_type() !== AEC_ENVIRONMENT_DEV) {
$aec_webapp_file = esc_url(AEC()->get_setting('extranet_url') . '/js/aec-webapp.js');
wp_enqueue_script('aec-webapp', $aec_webapp_file, ['jquery'], AEC()->get_file_version($aec_webapp_file), ['in_footer' => true]);
return;
}
$polyfills_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/dist/polyfills.js');
$runtime_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/dist/runtime.js');
$vendor_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/dist/vendor.js');
$main_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/dist/main.js');
wp_enqueue_script('aec-webapp-polyfills', $polyfills_file, [], AEC()->get_file_version($polyfills_file), ['in_footer' => true]);
wp_enqueue_script('aec-webapp-runtime', $runtime_file, [], AEC()->get_file_version($runtime_file), ['in_footer' => true]);
wp_enqueue_script('aec-webapp-vendor', $vendor_file, [], AEC()->get_file_version($vendor_file), ['in_footer' => true]);
wp_enqueue_script('aec-webapp-main', $main_file, [], AEC()->get_file_version($main_file), ['in_footer' => true]);
}
private function load_kiosque_aec()
{
if (wp_get_environment_type() !== AEC_ENVIRONMENT_DEV) {
$kiosque_aec_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque-aec.js');
wp_enqueue_script('kiosque-aec', $kiosque_aec_file, [], AEC()->get_file_version($kiosque_aec_file), ['in_footer' => true]);
return;
}
$polyfills_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/kiosque-dist/kiosque-polyfills.js');
$runtime_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/kiosque-dist/runtime.js');
$vendor_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/kiosque-dist/kiosque-vendor.js');
$main_file = esc_url(AEC()->get_setting('extranet_url') . '/js/kiosque/kiosque-dist/kiosque-main.js');
wp_enqueue_script('kiosque-aec-polyfills', $polyfills_file, [], AEC()->get_file_version($polyfills_file), ['in_footer' => true]);
wp_enqueue_script('kiosque-aec-runtime', $runtime_file, [], AEC()->get_file_version($runtime_file), ['in_footer' => true]);
wp_enqueue_script('kiosque-aec-vendor', $vendor_file, [], AEC()->get_file_version($vendor_file), ['in_footer' => true]);
wp_enqueue_script('kiosque-aec-main', $main_file, [], AEC()->get_file_version($main_file), ['in_footer' => true]);
}
public function add_defer_attribute(string $tag, string $handle): string
{
$scripts_to_defer = [
'kiosque-aec',
'aec-webapp'
];
if (in_array($handle, $scripts_to_defer) && !strpos($tag, 'async') && !strpos($tag, 'defer')) {
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
public function defer_style(string $tag, string $handle, string $href): string
{
$defer_style = [
'webapp-if',
'webapp-af',
'webapp-aec',
'jqueryui'
];
if (in_array($handle, $defer_style)) {
return '' . "\n";
}
return $tag;
}
/**
* Adds scripts and styles into site header
*
* @since 1.0.0
*/
public function enqueue_extranet_head()
{
echo "\n";
wp_add_inline_style('nodepcss-handle', $this->load_fonts());
// Prefetch and Preload Critical CSS
echo '';
echo '';
// Inline Scripts
$inline_scripts = [
'var root_directory = "' . esc_js(AEC()->get_setting('extranet_url')) . '/";',
'var aecWordpressURL = "' . esc_js(AEC()->get_setting('wordpress_url')) . '";',
'var aecExtranetURL = "' . esc_js(AEC::instance()->get_extranet_url()) . '/";',
'var aec_app_url = "' . esc_js(AEC()->get_setting('app_url')) . '";',
'var aecExtranetWebAppsAPIKey = "' . esc_js(get_option('aec_extranet_api_token')) . '";',
'var templateInUse = "' . esc_js(get_option('aec_template_in_use')) . '";',
'var currentLanguage = "' . esc_js(AEC::instance()->get_locale()) . '";',
'var clientInstance = "' . esc_js(AEC::instance()->get_client_instance()) . '";',
'var aecAppWPURL = "' . esc_js(AEC()->get_aec_api_url()) . '";',
$this->set_session_language()
];
foreach ($inline_scripts as $script) {
wp_print_inline_script_tag($script);
}
// Enqueue Conditional Styles
$prefix = AEC()->get_styles_prefix();
$webapp_file = esc_url(AEC()->get_setting('extranet_url') . '/css/webapp-' . AEC()->get_setting('establishment_type') . $prefix);
wp_enqueue_style('webapp-' . get_option('aec_etablishment_type'), $webapp_file, [], AEC()->get_file_version($webapp_file));
if (is_rtl()) {
$rtl_file = esc_url(AEC()->get_setting('extranet_url') . '/css/rtl/aec' . $prefix);
wp_enqueue_style('webapp-rtl-' . get_option('aec_etablishment_type'), $rtl_file, [], AEC()->get_file_version($rtl_file));
}
$jquery_file = esc_url(AEC()->get_setting('extranet_url') . '/css/jqueryui' . $prefix);
wp_enqueue_style('jqueryui', $jquery_file, [], AEC()->get_file_version($jquery_file));
}
/**
* Set session language
*
* This key is used both in Extranet and WordPress for setting the webapp language
*
* @since 1.1.3
*/
public function set_session_language(): string
{
$lang_to_set = AEC::instance()->get_locale();
if (!$lang_to_set) {
return '';
}
$escapedLang = esc_js($lang_to_set);
return 'window.sessionStorage.setItem("langToSetFromWP", "' . $escapedLang . '");';
}
/**
* Adds scripts and styles into site header
*
* @since 1.0.2
*/
public function load_fonts(): string
{
if (!is_admin()) {
return '';
}
return '
@font-face {
font-family: FoundersGrotesk;
src: url(' . AEC_URL . '/admin/fonts/FoundersGroteskX-CondensedWeb-Semibold.eot' . ')
format("embedded-opentype"),
url(' . AEC_URL . '/admin/fonts/FoundersGroteskX-CondensedWeb-Semibold.woff' . ') format("woff"),
url(' . AEC_URL . '/admin/fonts/FoundersGroteskX-CondensedWeb-Semibold.woff2' . ') format("woff2");
font-weight: 500;
}
';
}
public function add_url_nonce_to_request(string $url): string
{
$nonce_key = AEC()->get_url_nonce_key();
$nonce_action = AEC()->get_url_nonce_action();
$_GET[$nonce_key] = wp_create_nonce($nonce_action);
return $url;
}
}