hooks(); } return self::$instance; } private function __construct() {} private function hooks(): void { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) ); add_action( 'wp_head', array( $this, 'output_inline_vars' ), 7 ); add_action( 'wp_head', array( $this, 'output_nav_auth_css' ), 7 ); } /** * Returns the stored asset cache version. * * Updated by Cache_Purge::bump_asset_version() on cache purge. Falls back * to the compile-time constant when no option has been set yet. */ public static function get_asset_version(): string { static $version = null; if ( null === $version ) { $version = (string) get_option( 'arshid6social_assets_version', ARSHID6SOCIAL_ASSET_VER ); } return $version; } /** * Returns cached asset suffixes and versions, computing them at most once per * plugin version (stored in a site option so filesystem calls only happen when * the plugin is updated, not on every page load). * * @return array{css_suffix: string, js_suffix: string, js_ver: string} */ private function get_asset_variants(): array { static $variants = null; if ( null !== $variants ) { return $variants; } $cache_key = 'arshid6social_asset_variants_' . ARSHID6SOCIAL_ASSET_VER; $cached = get_site_transient( $cache_key ); if ( is_array( $cached ) ) { $variants = $cached; return $variants; } $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; $av = self::get_asset_version(); $css_dir = ARSHID6SOCIAL_PLUGIN_DIR . 'assets/css/'; $js_dir = ARSHID6SOCIAL_PLUGIN_DIR . 'assets/js/'; $has_min_css = ! $debug && file_exists( $css_dir . 'social-network.min.css' ); $css_suffix = $has_min_css ? '.min' : ''; $has_min_js = ! $debug && file_exists( $js_dir . 'social-network.min.js' ); if ( $has_min_js ) { $src_file = $js_dir . 'social-network.js'; $min_file = $js_dir . 'social-network.min.js'; $src_mtime = file_exists( $src_file ) ? (int) filemtime( $src_file ) : 0; $min_mtime = file_exists( $min_file ) ? (int) filemtime( $min_file ) : 0; $js_suffix = ( $min_mtime >= $src_mtime ) ? '.min' : ''; } else { $js_suffix = ''; } $js_ver = $av; $variants = compact( 'css_suffix', 'js_suffix', 'js_ver' ); set_site_transient( $cache_key, $variants, WEEK_IN_SECONDS ); return $variants; } /** * Enqueues frontend assets only on plugin pages. */ public function enqueue_frontend(): void { if ( ! $this->is_plugin_page() ) { return; } $av = self::get_asset_version(); [ 'css_suffix' => $suffix, 'js_suffix' => $js_suffix, 'js_ver' => $js_ver ] = $this->get_asset_variants(); wp_enqueue_style( 'arshid6social-main', ARSHID6SOCIAL_ASSETS_URL . "css/social-network{$suffix}.css", array(), $av ); // RTL stylesheet. if ( is_rtl() ) { wp_enqueue_style( 'arshid6social-rtl', ARSHID6SOCIAL_ASSETS_URL . 'css/rtl.css', array( 'arshid6social-main' ), $av ); } wp_enqueue_script( 'arshid6social-main', ARSHID6SOCIAL_ASSETS_URL . "js/social-network{$js_suffix}.js", array(), $js_ver, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-main', 'ARSHID6SOCIALConfig', $this->frontend_script_data() ); // Feed ads config. $ads_data = Ads::get_feed_ads_for_js(); wp_localize_script( 'arshid6social-main', 'ARSHID6SOCIALAds', array( 'ads' => $ads_data['ads'], 'everyNPosts' => $ads_data['every_n_posts'], 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'arshid6social_ajax_nonce' ), ) ); // Messages page script (external file avoids inline-script encoding issues). wp_enqueue_script( 'arshid6social-messages', ARSHID6SOCIAL_ASSETS_URL . 'js/messages.js', array( 'arshid6social-main' ), $av, array( 'strategy' => 'defer', 'in_footer' => true, ) ); // Stories assets. if ( get_option( 'arshid6social_stories_enabled', false ) ) { wp_enqueue_style( 'arshid6social-stories', ARSHID6SOCIAL_ASSETS_URL . 'css/stories.css', array( 'arshid6social-main' ), $av ); wp_enqueue_script( 'arshid6social-stories', ARSHID6SOCIAL_ASSETS_URL . 'js/stories.js', array( 'arshid6social-main' ), $av, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-stories', 'ARSHID6SOCIALStories', array( 'nonce' => wp_create_nonce( 'arshid6social_ajax_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'viewerId' => get_current_user_id(), 'storyReply' => (bool) get_option( 'arshid6social_messages_story_enabled', false ), 'i18n' => array( 'confirmDelete' => __( 'Delete this story?', '6arshid-social-community' ), 'highlightTitle' => __( 'Highlight name:', '6arshid-social-community' ), 'noViewers' => __( 'No viewers yet.', '6arshid-social-community' ), ), ) ); } // Blocking assets. if ( get_option( 'arshid6social_blocking_enabled', true ) ) { wp_enqueue_style( 'arshid6social-blocking', ARSHID6SOCIAL_ASSETS_URL . 'css/blocking.css', array( 'arshid6social-main' ), $av ); wp_enqueue_script( 'arshid6social-blocking', ARSHID6SOCIAL_ASSETS_URL . 'js/blocking.js', array( 'arshid6social-main' ), $av, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-blocking', 'ARSHID6SOCIALBlocking', array( 'nonce' => wp_create_nonce( 'arshid6social_ajax_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'showReason' => (bool) get_option( 'arshid6social_blocking_show_reason', true ), 'i18n' => array( 'reasonPrompt' => __( 'Reason for blocking (optional):', '6arshid-social-community' ), 'confirmBlock' => __( 'Block this user?', '6arshid-social-community' ), 'confirmUnblock' => __( 'Unblock this user?', '6arshid-social-community' ), 'blocked' => __( 'Blocked', '6arshid-social-community' ), 'block' => __( 'Block', '6arshid-social-community' ), 'blockError' => __( 'Could not block user. Please try again.', '6arshid-social-community' ), 'emptyList' => __( 'You have not blocked anyone.', '6arshid-social-community' ), ), ) ); } // Verification assets. if ( get_option( 'arshid6social_verification_enabled', false ) ) { wp_enqueue_style( 'arshid6social-verification', ARSHID6SOCIAL_ASSETS_URL . 'css/verification.css', array( 'arshid6social-main' ), $av ); wp_enqueue_script( 'arshid6social-verification', ARSHID6SOCIAL_ASSETS_URL . 'js/verification.js', array( 'arshid6social-main' ), $av, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-verification', 'ARSHID6SOCIALVerification', array( 'nonce' => wp_create_nonce( 'arshid6social_ajax_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'i18n' => array( 'selectType' => __( 'Please select a verification type.', '6arshid-social-community' ), 'nameRequired' => __( 'Please enter your full name.', '6arshid-social-community' ), 'requestSubmitted' => __( 'Your request has been submitted. We\'ll review it and notify you.', '6arshid-social-community' ), 'submitError' => __( 'Could not submit request.', '6arshid-social-community' ), ), ) ); } // Search assets — loaded on every search page (WordPress /?s= queries). if ( is_search() ) { $search_ver = self::get_asset_version(); wp_enqueue_style( 'arshid6social-search', ARSHID6SOCIAL_ASSETS_URL . 'css/search.css', array(), $search_ver ); wp_enqueue_script( 'arshid6social-search', ARSHID6SOCIAL_ASSETS_URL . 'js/search.js', array(), $search_ver, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-search', 'ARSHID6SOCIALSearch', array( 'restUrl' => esc_url_raw( rest_url( 'arshid6social/v1/' ) ), 'searchEndpoint' => esc_url_raw( rest_url( 'arshid6social/v1/search' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'siteUrl' => esc_url( home_url( '/' ) ), 'paginationType' => sanitize_key( get_option( 'arshid6social_search_pagination_type', 'pagination' ) ), 'i18n' => array( 'all' => __( 'All', '6arshid-social-community' ), 'activity' => __( 'Activity', '6arshid-social-community' ), 'members' => __( 'People', '6arshid-social-community' ), 'groups' => __( 'Groups', '6arshid-social-community' ), 'marketplace' => __( 'Marketplace', '6arshid-social-community' ), 'search' => __( 'Search', '6arshid-social-community' ), 'noResults' => __( 'No results found', '6arshid-social-community' ), 'noResultsSub' => __( 'Try a different keyword.', '6arshid-social-community' ), 'seeAll' => __( 'See all', '6arshid-social-community' ), 'results' => __( 'results', '6arshid-social-community' ), 'loadMore' => __( 'Load more', '6arshid-social-community' ), 'loading' => __( 'Loading…', '6arshid-social-community' ), 'viewPost' => __( 'View post', '6arshid-social-community' ), 'free' => __( 'Free', '6arshid-social-community' ), 'members_lc' => __( 'members', '6arshid-social-community' ), 'prev' => __( '← Prev', '6arshid-social-community' ), 'next' => __( 'Next →', '6arshid-social-community' ), ), ) ); } } /** * Enqueues admin assets on plugin admin pages. * * @param string $hook_suffix Current admin page hook suffix. */ public function enqueue_admin( string $hook_suffix ): void { if ( ! str_contains( $hook_suffix, 'arshid6social' ) && ! str_contains( $hook_suffix, 'social-network' ) ) { return; } wp_enqueue_style( 'arshid6social-admin', ARSHID6SOCIAL_ASSETS_URL . 'css/admin.css', array(), ARSHID6SOCIAL_VERSION ); wp_enqueue_script( 'arshid6social-admin', ARSHID6SOCIAL_ASSETS_URL . 'js/admin.js', array( 'wp-api' ), ARSHID6SOCIAL_VERSION, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'arshid6social-admin', 'ARSHID6SOCIALAdminConfig', array( 'nonce' => wp_create_nonce( 'arshid6social_admin_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'restUrl' => esc_url_raw( rest_url( 'arshid6social/v1/' ) ), 'i18n' => $this->admin_i18n(), ) ); } /** * Outputs inline CSS on every frontend page to show/hide nav items by auth state. * Uses body.logged-in (added by WordPress) + href attribute selectors. * Works with classic menus, Gutenberg block nav, and Elementor — no PHP filter needed. */ public function output_nav_auth_css(): void { if ( is_admin() ) { return; } $guest_slugs = array( 'login', 'register' ); $member_slugs = array( 'activity', 'dashboard', 'groups', 'messages', 'notifications', 'saved-posts' ); $member_hide = array(); foreach ( $member_slugs as $slug ) { $member_hide[] = 'body:not(.logged-in) .wp-block-navigation-item:has(>a[href$="/' . $slug . '/"])'; $member_hide[] = 'body:not(.logged-in) li.menu-item:has(>a[href$="/' . $slug . '/"])'; } $guest_hide = array(); foreach ( $guest_slugs as $slug ) { $guest_hide[] = 'body.logged-in .wp-block-navigation-item:has(>a[href$="/' . $slug . '/"])'; $guest_hide[] = 'body.logged-in li.menu-item:has(>a[href$="/' . $slug . '/"])'; } $css = implode( ',' . "\n", $member_hide ) . '{display:none!important;}' . "\n"; $css .= implode( ',' . "\n", $guest_hide ) . '{display:none!important;}'; wp_add_inline_style( 'arshid6social-main', $css ); } /** * Outputs inline CSS for admin-configurable values (colour, dark mode). * Also applies dark mode data attribute to
when needed. */ public function output_inline_vars(): void { $primary = sanitize_hex_color( get_option( 'arshid6social_primary_color', '#2563eb' ) ) ?: '#2563eb'; // Primary colour variable is output on every front-end page so nav badges // (which appear site-wide) always inherit the configured accent colour. // Register a minimal handle so wp_add_inline_style always has something to attach to, // even on non-plugin pages where arshid6social-main is not enqueued. if ( ! wp_style_is( 'arshid6social-main', 'enqueued' ) ) { wp_register_style( 'arshid6social-inline-vars', false, array(), null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters wp_enqueue_style( 'arshid6social-inline-vars' ); $style_handle = 'arshid6social-inline-vars'; } else { $style_handle = 'arshid6social-main'; } $vars_css = ':root{--arshid6social-primary:' . esc_html( $primary ) . ';--arshid6social-primary-dark:' . esc_html( $this->darken_hex( $primary ) ) . ';}'; wp_add_inline_style( $style_handle, $vars_css ); if ( ! $this->is_plugin_page() ) { return; } $dark_mode = sanitize_key( get_option( 'arshid6social_dark_mode', 'off' ) ); if ( 'on' === $dark_mode ) { // Force dark mode immediately — no flash. wp_add_inline_script( 'arshid6social-main', 'document.documentElement.setAttribute("data-arshid6social-dark","true");document.body&&document.body.setAttribute("data-arshid6social-dark","true");', 'before' ); } elseif ( 'auto' === $dark_mode ) { // Follow system preference via JS — never uses CSS media query so // it can't override a theme that doesn't support dark mode. wp_add_inline_script( 'arshid6social-main', '(function(){function arshid6social_apply(mq){var v=mq.matches?"true":"false";document.documentElement.setAttribute("data-arshid6social-dark",v);if(document.body)document.body.setAttribute("data-arshid6social-dark",v);}var mq=window.matchMedia("(prefers-color-scheme:dark)");arshid6social_apply(mq);mq.addEventListener("change",arshid6social_apply);})()', 'before' ); } // 'off' = no dark styles (default — theme-compatible light mode). } /** * Naive hex darkening — reduces each RGB channel by ~15%. * * @param string $hex Hex colour e.g. '#2563eb'. * @return string Darkened hex colour. */ private function darken_hex( string $hex ): string { $hex = ltrim( $hex, '#' ); if ( strlen( $hex ) === 3 ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $r = max( 0, hexdec( substr( $hex, 0, 2 ) ) - 30 ); $g = max( 0, hexdec( substr( $hex, 2, 2 ) ) - 30 ); $b = max( 0, hexdec( substr( $hex, 4, 2 ) ) - 30 ); return sprintf( '#%02x%02x%02x', $r, $g, $b ); } /** * Returns data to be passed to the frontend JS bundle. * * @return array