t. * * @since 4.1.9 * * @param string $item An item to retrieve. * @return string|null The asset item. */ private function getAssetManifestItem( $item ) { $assetManifest = $this->getManifest(); return ! empty( $assetManifest[ $item ] ) ? $assetManifest[ $item ] : null; } /** * Get an asset's array of URLs to import. * * @since 4.1.9 * * @param string $asset The asset to find imports for. * @return array An array of imports. */ private function importsUrls( $asset ) { $urls = []; $manifestAsset = $this->getManifestItem( $asset ); if ( ! empty( $manifestAsset['imports'] ) ) { foreach ( $manifestAsset['imports'] as $import ) { $importAsset = $this->getManifestItem( $import ); if ( ! empty( $importAsset['file'] ) ) { $urls[] = $this->getPublicUrlBase() . $importAsset['file']; // Load the import's CSS if any. $this->loadCss( $import ); } } } return $urls; } /** * Returns an asset's CSS urls. * * @since 4.1.9 * * @param string $asset The asset to find CSS URLs for. * @return array An array of CSS URLs to load. */ private function getCssUrls( $asset ) { $urls = []; $manifestAsset = $this->getManifestItem( $asset ); if ( ! empty( $manifestAsset['css'] ) ) { foreach ( $manifestAsset['css'] as $file ) { $urls[ $file ] = $this->getPublicUrlBase() . $file; } } return $urls; } /** * Check if we should load the dev watcher scripts. * * @since 4.1.9 * * @return boolean True if we should load the dev watcher scripts. */ private function shouldLoadDev() { if ( null !== $this->shouldLoadDevScripts ) { return $this->shouldLoadDevScripts; } if ( ! $this->isDev || ( defined( 'AIOSEO_LOAD_DEV_SCRIPTS' ) && false === AIOSEO_LOAD_DEV_SCRIPTS ) ) { $this->shouldLoadDevScripts = false; return $this->shouldLoadDevScripts; } if ( ! $this->domain && ! $this->port ) { $this->shouldLoadDevScripts = false; return $this->shouldLoadDevScripts; } set_error_handler( function() {} ); $connection = fsockopen( $this->domain, $this->port ); // phpcs:ignore WordPress.WP.AlternativeFunctions restore_error_handler(); if ( ! $connection ) { $this->shouldLoadDevScripts = false; return $this->shouldLoadDevScripts; } $this->shouldLoadDevScripts = true; return $this->shouldLoadDevScripts; } /** * Get the path for the assets. * * @since 4.1.9 * * @param bool $maybeDev Whether to try and load dev scripts. * @return string The path for the assets. */ public function getAssetsPath( $maybeDev = true ) { return $maybeDev && $this->shouldLoadDev() ? $this->getDevUrl() : $this->basePath(); } /** * Finds out if a handle should be loaded as regular JS and not as modern JS module. * * @since 4.1.9 * * @param string $handle The script handle. * @return bool Should the module tag be skipped. */ public function skipModuleTag( $handle ) { if ( ! aioseo()->helpers->stringContains( $handle, $this->jsHandle( '' ) ) ) { return true; } foreach ( $this->noModuleTag as $tag ) { if ( aioseo()->helpers->stringContains( $handle, $tag ) ) { return true; } } return false; } /** * Normalize the assets host. Some sites manually set the WP_PLUGINS_URL * and if that domain has www. and the site_url does not, then it will fail to load * our assets. This doesn't fix the issue 100% because it will still fail on * sub-domains that don't have the proper CORS headers. Those sites will need * manual fixes. * * @since 4.1.10 * * @param string $path The path to normalize. * @return string The normalized path. */ public function normalizeAssetsHost( $path ) { static $paths = []; if ( isset( $paths[ $path ] ) ) { return apply_filters( 'aioseo_normalize_assets_host', $paths[ $path ] ); } // We need to verify the domain on the $path attribute matches // what's in site_url() for our assets or they won't load. $siteUrl = site_url(); $siteUrlEscaped = aioseo()->helpers->escapeRegex( $siteUrl ); if ( preg_match( "/^$siteUrlEscaped/i", $path ) ) { $paths[ $path ] = $path; return apply_filters( 'aioseo_normalize_assets_host', $paths[ $path ] ); } // We now know that the path doesn't contain the site_url(). $newPath = $path; $siteUrlParsed = wp_parse_url( $siteUrl ); $host = aioseo()->helpers->escapeRegex( str_replace( 'www.', '', $siteUrlParsed['host'] ) ); $scheme = aioseo()->helpers->escapeRegex( $siteUrlParsed['scheme'] ); $siteUrlHasWww = preg_match( "/^{$scheme}:\/\/www\.$host/", $siteUrl ); $pathHasWww = preg_match( "/^{$scheme}:\/\/www\.$host/", $path ); // Check if the path contains www. if ( $pathHasWww && ! $siteUrlHasWww ) { // If the path contains www., we want to strip it out. $newPath = preg_replace( "/^({$scheme}:\/\/)(www\.)($host)/", '$1$3', $path ); } // Check if the site_url contains www. if ( $siteUrlHasWww && ! $pathHasWww ) { // If the site_url contains www., we want to add it in to the path. $newPath = preg_replace( "/^({$scheme}:\/\/)($host)/", '$1www.$2', $path ); } $paths[ $path ] = $newPath; return apply_filters( 'aioseo_normalize_assets_host', $paths[ $path ] ); } /** * Get all the CSS files which a JS asset depends on. * This won't work properly unless you've run `npm run build` first. * * @since 4.3.1 * * @param string $asset The asset to find the CSS dependencies for. * @return array All the asset's CSS dependencies if any. */ public function getJsAssetCssQueue( $asset ) { $queue = []; foreach ( $this->getCssUrls( $asset ) as $file => $url ) { $queue[] = [ 'handle' => $this->cssHandle( $file ), 'url' => $url ]; } $manifestAsset = $this->getManifestItem( $asset ); if ( ! empty( $manifestAsset['imports'] ) ) { foreach ( $manifestAsset['imports'] as $subAsset ) { foreach ( $this->getCssUrls( $subAsset ) as $file => $url ) { $queue[] = [ 'handle' => $this->cssHandle( $file ), 'url' => $url ]; } } } return $queue; } }