| | |
| | | <?php |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | | } |
| | | |
| | | use JVBase\base\Site; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\rest\routes\LoginRoutes; |
| | | |
| | | add_action('init', 'jvbRegisterScripts', 5); |
| | | |
| | | function jvbRegisterScripts() { |
| | | $version = '1.1.66'; |
| | | $version = '1.1.68'; |
| | | $strategy = [ |
| | | 'strategy' => 'defer', |
| | | 'in_footer' => true |
| | | ]; |
| | | wp_register_script( |
| | | 'jvb-login', |
| | | JVB_URL.'assets/js/min/login.min.js', |
| | | [ |
| | | 'jvb-auth', |
| | | 'jvb-form', |
| | | ], |
| | | $version |
| | | ); |
| | | |
| | | wp_register_style( |
| | | 'jvb-form', |
| | |
| | | ); |
| | | wp_localize_script('jvb-auth', 'jvbBase', ['base' => BASE]); |
| | | wp_localize_script('jvb-auth', 'jvbAuth', LoginRoutes::auth()); |
| | | wp_register_script( |
| | | 'jvb-interactions', |
| | | JVB_URL.'assets/js/min/interactions.min.js', |
| | | [ |
| | | 'jvb-queue', |
| | | 'jvb-data-store' |
| | | ], |
| | | $version, |
| | | $strategy |
| | | ); |
| | | // wp_register_script( |
| | | // 'jvb-interactions', |
| | | // JVB_URL.'assets/js/min/interactions.min.js', |
| | | // [ |
| | | // 'jvb-queue', |
| | | // 'jvb-data-store' |
| | | // ], |
| | | // $version, |
| | | // $strategy |
| | | // ); |
| | | |
| | | |
| | | wp_register_script( |
| | |
| | | [ |
| | | 'jvb-queue', |
| | | 'jvb-data-store', |
| | | 'jvb-interactions', |
| | | ], |
| | | $version, |
| | | $strategy |
| | |
| | | [ |
| | | 'jvb-queue', |
| | | 'jvb-data-store', |
| | | 'jvb-interactions', |
| | | ], |
| | | $version, |
| | | $strategy |
| | |
| | | $strategy |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Scripts |
| | | */ |
| | | add_action('wp_enqueue_scripts', 'jvbScripts', 10); |
| | | |
| | | function jvbScripts():void |
| | | { |
| | | add_action('wp_head', 'jvbInlineNavStyles'); |
| | | |
| | | if (Site::has('dashboard')) { |
| | | wp_enqueue_script('jvb-queue'); |
| | | } |
| | | |
| | | wp_enqueue_script('jvb-auth'); |
| | | wp_enqueue_script('jvb-settings'); |
| | | wp_enqueue_script('jvb-navigation'); |
| | | // wp_enqueue_script('jvb-ui'); |
| | | // wp_enqueue_script('jvb-media'); |
| | | wp_enqueue_script('jvb-gallery'); |
| | | wp_enqueue_script('jvb-cache'); |
| | | |
| | | |
| | | $interactions = []; |
| | | if (Site::has('favourites')) { |
| | | $interactions[] = 'favourites'; |
| | | } |
| | | if (!empty(Registrar::withFeature('karma'))) { |
| | | $interactions[] = 'karma'; |
| | | } |
| | | if (Site::has('notifications')) { |
| | | $interactions[] = 'notifications'; |
| | | } |
| | | |
| | | if (!empty($interactions)) { |
| | | foreach($interactions as $interaction) { |
| | | wp_enqueue_script('jvb-'.$interaction); |
| | | } |
| | | } |
| | | |
| | | |
| | | $queue = [ |
| | | 'api' => rest_url('jvb/v1/'), |
| | | 'redirect' => get_home_url(null, '/login/'), |
| | | 'labels' => Registrar::getLabels(), |
| | | ]; |
| | | |
| | | wp_localize_script('jvb-auth', 'jvbSettings', $queue); |
| | | |
| | | |
| | | $initUserSettings = 'async function initUserItems() { |
| | | if (!jvbSettings.currentUser) return; |
| | | |
| | | window.userFavourites = {}; |
| | | window.userVotes = {}; |
| | | '; |
| | | if (Site::has('favourites')) { |
| | | wp_enqueue_script('jvb-favourites'); |
| | | $initUserSettings .= ' |
| | | //Fetch user favourites |
| | | try { |
| | | const result = await window.jvbCache.fetchWithCache( |
| | | `${jvbSettings.api}favourites?all=true`, |
| | | { |
| | | method: \'GET\', |
| | | headers: {\'X-WP-Nonce\': jvbSettings.nonce} |
| | | } |
| | | ); |
| | | |
| | | if (result && result.favourites) { |
| | | for (const key in result.favourites) { |
| | | window.userFavourites[key] = new Set(); |
| | | result.favourites[key].forEach(id => { |
| | | window.userFavourites[key].add(parseInt(id)); |
| | | }); |
| | | } |
| | | } |
| | | } catch (error) { |
| | | console.error(\'Failed to fetch user favourites:\', error); |
| | | }'; |
| | | } |
| | | |
| | | if (!empty(Registrar::withFeature('karma'))) { |
| | | wp_enqueue_script('jvb-votes'); |
| | | $initUserSettings .= '// Fetch user votes |
| | | try { |
| | | const result = await window.jvbCache.fetchWithCache( |
| | | `${jvbSettings.api}vote?user=${jvbSettings.currentUser}`, |
| | | { |
| | | method: \'GET\', |
| | | headers: {\'X-WP-Nonce\': jvbSettings.nonce} |
| | | }, |
| | | {forceRefresh: true} |
| | | ); |
| | | |
| | | window.userVotes = {}; |
| | | if (result) { |
| | | for (const key in result) { |
| | | window.userVotes[key] = new Map(); |
| | | for (const [id, vote] of Object.entries(result[key])) { |
| | | window.userVotes[key].set(parseInt(id), vote); |
| | | } |
| | | } |
| | | } |
| | | } catch (error) { |
| | | console.error(\'Failed to fetch user votes:\', error); |
| | | }'; |
| | | } |
| | | |
| | | $initUserSettings .= '} initUserItems();'; |
| | | |
| | | // wp_add_inline_script('jvb-queue', $initUserSettings, 'after'); |
| | | |
| | | |
| | | if (is_search() || |
| | | is_tax(BASE.'shop') || |
| | | is_singular(BASE.'artist') || |
| | | jvbIsDirectory() |
| | | ) { |
| | | wp_enqueue_script('jvb-page-nav'); |
| | | } |
| | | |
| | | if (has_block('jvb/summaryBlock')) { |
| | | wp_enqueue_script('jvb-page-nav'); |
| | | } |
| | | |
| | | // Only load on single shop pages or other relevant pages |
| | | if (is_tax(BASE.'shop') || |
| | | is_singular(BASE.'partner')) { |
| | | $ID = get_queried_object_id(); |
| | | if (is_tax()) { |
| | | $check = get_term_meta($ID, BASE.'has_map', true); |
| | | } else { |
| | | $check = get_post_meta($ID, BASE.'has_map', true); |
| | | } |
| | | |
| | | // if ($check && $check !== '') { |
| | | //// wp_enqueue_script( |
| | | //// 'google-maps-frontend', |
| | | //// 'https://maps.googleapis.com/maps/api/js?key=' . GOOGLE_API . '&callback=googleMapsLoaded', |
| | | //// [], |
| | | //// null, |
| | | //// ['strategy' => 'async', |
| | | //// 'in_footer' => true] |
| | | //// ); |
| | | // |
| | | // // Add the callback function |
| | | // wp_add_inline_script('google-maps-frontend', ' |
| | | // function googleMapsLoaded() { |
| | | // if (window.googleMapsCallbacks && window.googleMapsCallbacks.length) { |
| | | // window.googleMapsCallbacks.forEach(callback => callback()); |
| | | // } |
| | | // } |
| | | // '); |
| | | // } |
| | | } |
| | | if (is_user_logged_in() && Site::has('notifications')) { |
| | | wp_enqueue_script('jvb-notifications'); |
| | | |
| | | wp_localize_script('jvb-notifications', 'notificationSettings', array( |
| | | 'homeUrl' => home_url(), |
| | | 'strings' => array( |
| | | 'loading' => 'Loading...', |
| | | 'error' => 'Failed to load notifications', |
| | | 'noNotifications' => 'No new notifications', |
| | | 'viewAll' => 'View All Notifications', |
| | | 'markRead' => 'Mark as read', |
| | | 'review' => 'Review' |
| | | ) |
| | | )); |
| | | } |
| | | |
| | | // jvbAddScriptDependency('jvb-feed-view-script', 'jvb-queue'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-selector'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-data-store'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-cache'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-a11y'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-utility'); |
| | | jvbAddScriptDependency('jvb-feed-view-script', 'jvb-gallery'); |
| | | // jvbAddScriptDependency('jvb-feed-view-script', 'jvb-loading'); |
| | | jvbAddScriptDependency('jvb-forms-view-script', 'jvb-queue'); |
| | | jvbAddScriptDependency('jvb-forms-view-script', 'jvb-quill'); |
| | | jvbAddScriptDependency('jvb-forms-view-script', 'jvb-form'); |
| | | jvbAddScriptDependency('jvb-forms-view-script', 'jvb-tabs'); |
| | | |
| | | jvbAddScriptDependency('jvb-timeline-view-script', 'jvb-gallery'); |
| | | } |
| | | |
| | | function jvbAddScriptDependency(string $handle, string $dep):bool |
| | | { |
| | | global $wp_scripts; |
| | | |
| | | $script = $wp_scripts->query($handle); |
| | | if (!$script) { |
| | | return false; |
| | | } |
| | | |
| | | if (!in_array($dep, $script->deps)) { |
| | | $script->deps[] = $dep; |
| | | } |
| | | |
| | | return true; |
| | | } |