$value) { // If the key exists in the merged array and both values are arrays, recurse. if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { $merged[$key] = ContentHelper::array_merge_distinct($merged[$key], $value); } else { // Otherwise, just overwrite the value. $merged[$key] = $value; } } } return $merged; } public static function getScriptDepsFromMeta(&$npm_dependencies, $meta, $isString = true, $justStatements = false, $incImportedInline = false){ if ($isString){ $meta = !empty($meta) ? json_decode($meta, true) : array(); } $statements = ''; $auto = !empty($meta['auto_script_deps']) ? $meta['auto_script_deps'] : array(); $manual = !empty($meta['manual_script_deps']) ? $meta['manual_script_deps'] : array(); $imported = $incImportedInline && !empty($meta['imported_script_deps']) ? $meta['imported_script_deps'] : array(); //return $meta; // exclude auto dependencies that have been manually disabled foreach ($manual as $packName => $config) { if (!empty($config['autoDisabled'])){ unset($auto[$packName]); unset($manual[$packName]['autoDisabled']); if (empty($manual[$packName])){ unset($manual[$packName]); } } } // Combine deps $totalDeps = ContentHelper::array_merge_distinct($auto, $manual, $imported); // Ensure importSyntax is set foreach ($totalDeps as $packName => $config) { if (empty($config['importSyntax'])){ // Fall back to the import statement set at the package level if (!empty($npm_dependencies->$packName['importSyntax'] )){ $config['importSyntax'] = $npm_dependencies->$packName['importSyntax']; } // we do not have a valid option to choose from - do not add dependency // perhaps we could default to the pack name... else { unset($totalDeps[$packName]); } } } //echo('$totalDeps:
'.print_r($totalDeps, 1).'' . $statements); if ($justStatements){ $register = array(); foreach ($totalDeps as $packageName => $config){ $module = !empty($config['module']) ? $config['module'] : $packageName; $statements.= "import " . $config['importSyntax'] . " from '".$module."';\n"; ContentHelper::populateRegistered($packageName, $config['importSyntax'], $register); } ContentHelper::applyRegistered($register, $statements); //wp_die('getScriptDepsFromMeta:
'.print_r($totalDeps, 1).'' . $statements); } /*if (count($totalDeps)){ wp_die('$totalDeps:
'.print_r([ 'auto' => $auto, 'import' => $import, 'manual' => $manual, 'total' => $totalDeps, ], 1).'' . $statements); }*/ return count($totalDeps) ? ($justStatements ? $statements : $totalDeps) : ''; } public static function populateRegistered($packageName, $importSyntax, &$register){ if (str_contains($packageName, 'gsap') && $packageName !== 'gsap'){ $register['gsap']['registerSyntax'] = "gsap.registerPlugin({addons});\n"; $register['gsap']['addons'][] = $importSyntax; } } public static function applyRegistered($register, &$statements){ foreach($register as $array){ $statements.= str_replace( '{addons}', implode(', ', $array['addons']), $array['registerSyntax'] ); } } public static function getJsFileName(&$snippet, $snippet_id, $withExtension = true){ //wp_die('getJsFileName:
'.print_r($snippet, true).''); $baseName = !empty($snippet['meta']['manual_name']) ? pathinfo(trim($snippet['meta']['manual_name']), PATHINFO_FILENAME) : $snippet_id; return sanitize_file_name($baseName) . ($withExtension ? '.js' : ''); } }