| 1 |
<?php |
| 2 |
|
| 3 |
namespace Microthemer\Content; |
| 4 |
|
| 5 |
class ContentHelper { |
| 6 |
|
| 7 |
/** |
| 8 |
* Perform safe memory/DOM cleanup after heavy XML/DOM operations. |
| 9 |
* Compatible with PHP 5.1+ (skips unavailable functions). |
| 10 |
*/ |
| 11 |
public static function cleanupMemory() |
| 12 |
{ |
| 13 |
// Clear libxml internal error buffer |
| 14 |
if (function_exists('libxml_clear_errors')) { |
| 15 |
libxml_clear_errors(); |
| 16 |
} |
| 17 |
|
| 18 |
// Trigger garbage collection for cyclic references (PHP 5.3+) |
| 19 |
if (function_exists('gc_collect_cycles')) { |
| 20 |
gc_collect_cycles(); |
| 21 |
} |
| 22 |
|
| 23 |
// Free unused memory from Zend allocator (PHP 7.0+) |
| 24 |
if (function_exists('gc_mem_caches')) { |
| 25 |
gc_mem_caches(); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Merges any number of arrays recursively, preserving keys. |
| 31 |
* |
| 32 |
* For non-array values at the same key, the value from the later array will overwrite the earlier one. |
| 33 |
* If a key exists in both arrays and both values are arrays, the two arrays will be merged recursively. |
| 34 |
* |
| 35 |
* @param array ...$arrays The arrays to merge. |
| 36 |
* @return array The merged array. |
| 37 |
*/ |
| 38 |
public static function array_merge_distinct(...$arrays){ |
| 39 |
|
| 40 |
// If no arrays are provided, return an empty array. |
| 41 |
if (empty($arrays)) { |
| 42 |
return []; |
| 43 |
} |
| 44 |
|
| 45 |
// Get the first array as the base. |
| 46 |
$merged = array_shift($arrays); |
| 47 |
|
| 48 |
// Loop through the remaining arrays. |
| 49 |
foreach ($arrays as $array) { |
| 50 |
|
| 51 |
// Iterate over the current array to merge it into the base. |
| 52 |
foreach ($array as $key => $value) { |
| 53 |
|
| 54 |
// If the key exists in the merged array and both values are arrays, recurse. |
| 55 |
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
| 56 |
$merged[$key] = ContentHelper::array_merge_distinct($merged[$key], $value); |
| 57 |
} else { |
| 58 |
// Otherwise, just overwrite the value. |
| 59 |
$merged[$key] = $value; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $merged; |
| 65 |
} |
| 66 |
|
| 67 |
public static function getScriptDepsFromMeta(&$npm_dependencies, $meta, $isString = true, $justStatements = false, $incImportedInline = false){ |
| 68 |
|
| 69 |
if ($isString){ |
| 70 |
$meta = !empty($meta) ? json_decode($meta, true) : array(); |
| 71 |
} |
| 72 |
|
| 73 |
$statements = ''; |
| 74 |
$auto = !empty($meta['auto_script_deps']) ? $meta['auto_script_deps'] : array(); |
| 75 |
$manual = !empty($meta['manual_script_deps']) ? $meta['manual_script_deps'] : array(); |
| 76 |
$imported = $incImportedInline && !empty($meta['imported_script_deps']) ? $meta['imported_script_deps'] : array(); |
| 77 |
|
| 78 |
//return $meta; |
| 79 |
|
| 80 |
// exclude auto dependencies that have been manually disabled |
| 81 |
foreach ($manual as $packName => $config) { |
| 82 |
if (!empty($config['autoDisabled'])){ |
| 83 |
unset($auto[$packName]); |
| 84 |
unset($manual[$packName]['autoDisabled']); |
| 85 |
if (empty($manual[$packName])){ |
| 86 |
unset($manual[$packName]); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Combine deps |
| 92 |
$totalDeps = ContentHelper::array_merge_distinct($auto, $manual, $imported); |
| 93 |
|
| 94 |
// Ensure importSyntax is set |
| 95 |
foreach ($totalDeps as $packName => $config) { |
| 96 |
if (empty($config['importSyntax'])){ |
| 97 |
|
| 98 |
// Fall back to the import statement set at the package level |
| 99 |
if (!empty($npm_dependencies->$packName['importSyntax'] )){ |
| 100 |
$config['importSyntax'] = $npm_dependencies->$packName['importSyntax']; |
| 101 |
} |
| 102 |
|
| 103 |
// we do not have a valid option to choose from - do not add dependency |
| 104 |
// perhaps we could default to the pack name... |
| 105 |
else { |
| 106 |
unset($totalDeps[$packName]); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
//echo('$totalDeps: <pre>'.print_r($totalDeps, 1).'</pre>' . $statements); |
| 112 |
if ($justStatements){ |
| 113 |
$register = array(); |
| 114 |
foreach ($totalDeps as $packageName => $config){ |
| 115 |
$module = !empty($config['module']) ? $config['module'] : $packageName; |
| 116 |
$statements.= "import " . $config['importSyntax'] . " from '".$module."';\n"; |
| 117 |
ContentHelper::populateRegistered($packageName, $config['importSyntax'], $register); |
| 118 |
} |
| 119 |
ContentHelper::applyRegistered($register, $statements); |
| 120 |
//wp_die('getScriptDepsFromMeta: <pre>'.print_r($totalDeps, 1).'</pre>' . $statements); |
| 121 |
} |
| 122 |
|
| 123 |
/*if (count($totalDeps)){ |
| 124 |
wp_die('$totalDeps: <pre>'.print_r([ |
| 125 |
'auto' => $auto, |
| 126 |
'import' => $import, |
| 127 |
'manual' => $manual, |
| 128 |
'total' => $totalDeps, |
| 129 |
], 1).'</pre>' . $statements); |
| 130 |
}*/ |
| 131 |
|
| 132 |
return count($totalDeps) |
| 133 |
? ($justStatements ? $statements : $totalDeps) |
| 134 |
: ''; |
| 135 |
} |
| 136 |
|
| 137 |
public static function populateRegistered($packageName, $importSyntax, &$register){ |
| 138 |
if (str_contains($packageName, 'gsap') && $packageName !== 'gsap'){ |
| 139 |
$register['gsap']['registerSyntax'] = "gsap.registerPlugin({addons});\n"; |
| 140 |
$register['gsap']['addons'][] = $importSyntax; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public static function applyRegistered($register, &$statements){ |
| 145 |
foreach($register as $array){ |
| 146 |
$statements.= str_replace( |
| 147 |
'{addons}', implode(', ', $array['addons']), $array['registerSyntax'] |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
public static function getJsFileName(&$snippet, $snippet_id, $withExtension = true){ |
| 154 |
|
| 155 |
//wp_die('getJsFileName: <pre>'.print_r($snippet, true).'</pre>'); |
| 156 |
|
| 157 |
$baseName = !empty($snippet['meta']['manual_name']) |
| 158 |
? pathinfo(trim($snippet['meta']['manual_name']), PATHINFO_FILENAME) |
| 159 |
: $snippet_id; |
| 160 |
|
| 161 |
return sanitize_file_name($baseName) . ($withExtension ? '.js' : ''); |
| 162 |
} |
| 163 |
} |