Background
1 year ago
Blocks
1 year ago
GlobalElements
1 year ago
Layout
1 year ago
License
1 year ago
Separators
11 months ago
StyleManager
1 year ago
Styles
1 year ago
Activation.php
1 year ago
Backup.php
1 year ago
CustomizerImporter.php
11 months ago
Deactivation.php
1 year ago
EditInKubioCustomizerPanel.php
1 year ago
Element.php
1 year ago
ElementBase.php
4 years ago
Importer.php
11 months ago
InnerBlocks.php
1 year ago
KubioFrontPageRevertNotice.php
1 year ago
LodashBasic.php
1 year ago
Registry.php
1 year ago
ThirdPartyPluginAssetLoaderInEditor.php
11 months ago
Utils.php
11 months ago
ThirdPartyPluginAssetLoaderInEditor.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core; |
| 4 | |
| 5 | |
| 6 | |
| 7 | /** |
| 8 | * For some plugins we want to load panels that are found in the default editor like for yoast/rank math so we load assets from the page |
| 9 | * in gutenberg |
| 10 | */ |
| 11 | class ThirdPartyPluginAssetLoaderInEditor |
| 12 | { |
| 13 | |
| 14 | public $pluginsConfigs = []; |
| 15 | |
| 16 | |
| 17 | private static $instance = null; |
| 18 | |
| 19 | |
| 20 | |
| 21 | protected function __construct() |
| 22 | { |
| 23 | add_action('admin_print_scripts', array($this, 'printAssetsList'), 20); |
| 24 | add_action('kubio/editor/enqueue_assets', array($this, 'dequeuePluginAssetsInKubioEditor'), 20); |
| 25 | add_action('enqueue_block_editor_assets', array($this, 'dequeueKubioScriptsInIframeRequest'), 9999); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | //to not cause weird issues |
| 30 | public function dequeueKubioScriptsInIframeRequest() { |
| 31 | if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) { |
| 32 | return; |
| 33 | } |
| 34 | global $wp_scripts, $wp_styles; |
| 35 | |
| 36 | foreach ($wp_scripts->queue as $handle) { |
| 37 | if ($this->getScriptIsFromTargetPlugin($handle, 'kubio')) { |
| 38 | wp_dequeue_script($handle); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | |
| 44 | |
| 45 | public static function addPlugin($pluginPathSearchString, $isActiveCallback, $shouldDequeueAssets = false) |
| 46 | { |
| 47 | $config = [ |
| 48 | 'pluginPathSearchString' => $pluginPathSearchString, |
| 49 | 'isActiveCallback' => $isActiveCallback, |
| 50 | 'shouldDequeueAssets' => $shouldDequeueAssets |
| 51 | ]; |
| 52 | static::getInstance()->pluginsConfigs[] = $config; |
| 53 | } |
| 54 | |
| 55 | public function dequeuePluginAssetsInKubioEditor() |
| 56 | { |
| 57 | foreach ($this->pluginsConfigs as $config) { |
| 58 | $currentPluginPathSearchString = $config['pluginPathSearchString']; |
| 59 | $isActiveCallback = $config['isActiveCallback']; |
| 60 | $shouldDequeueAssets = $config['shouldDequeueAssets']; |
| 61 | |
| 62 | if(!$shouldDequeueAssets) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | $isActive = call_user_func($isActiveCallback); |
| 67 | if (!$isActive) { |
| 68 | return; |
| 69 | } |
| 70 | global $wp_scripts, $wp_styles; |
| 71 | |
| 72 | foreach ($wp_scripts->queue as $handle) { |
| 73 | if ($this->getScriptIsFromTargetPlugin($handle, $currentPluginPathSearchString)) { |
| 74 | wp_dequeue_script($handle); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | foreach ($wp_styles->queue as $handle) { |
| 79 | if ($this->getStyleIsFromTargetPlugin($handle, $currentPluginPathSearchString)) { |
| 80 | wp_dequeue_style($handle); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | public function printAssetsList() |
| 86 | { |
| 87 | if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | //search for a part of the plugin folder name in the url to find assets |
| 92 | $pluginPathSearch = $_GET['kubio-get-3rd-party-plugin-assets']; |
| 93 | global $wp_scripts; |
| 94 | |
| 95 | |
| 96 | foreach ($this->pluginsConfigs as $config) { |
| 97 | $currentPluginPathSearchString = $config['pluginPathSearchString']; |
| 98 | $isActiveCallback = $config['isActiveCallback']; |
| 99 | |
| 100 | $isActive = call_user_func($isActiveCallback); |
| 101 | if (!$isActive || $currentPluginPathSearchString !== $pluginPathSearch) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | $pluginScriptsHandles = []; |
| 107 | |
| 108 | foreach ($wp_scripts->queue as $handle) { |
| 109 | if (!$this->getScriptIsFromTargetPlugin($handle, $pluginPathSearch)) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | $pluginScriptsHandles[] = $handle; |
| 114 | } |
| 115 | $dependencies = $this->getAllScriptDependencies($pluginScriptsHandles); |
| 116 | |
| 117 | |
| 118 | wp_print_inline_script_tag( |
| 119 | sprintf('window.kubioThirdParthyPluginsAssets = window.kubioThirdParthyPluginsAssets || {}; |
| 120 | window.kubioThirdParthyPluginsAssets["%s"] = %s;', wp_kses_post($pluginPathSearch), wp_json_encode($dependencies)) |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public function getAllScriptDependencies($pluginScriptsHandles) |
| 126 | { |
| 127 | $collected = []; |
| 128 | $this->getAllScriptDependenciesRecursive($pluginScriptsHandles, $collected); |
| 129 | $singleArrayDeps = []; |
| 130 | foreach ($collected as $key => $value) { |
| 131 | $singleArrayDeps[] = $key; |
| 132 | $singleArrayDeps = array_merge(array(), $singleArrayDeps, $value); |
| 133 | }; |
| 134 | $singleArrayDeps = array_values(array_unique($singleArrayDeps)); |
| 135 | return $singleArrayDeps; |
| 136 | } |
| 137 | |
| 138 | public function getAllScriptDependenciesRecursive($handles, &$collected = []) |
| 139 | { |
| 140 | global $wp_scripts; |
| 141 | |
| 142 | foreach ($handles as $handle) { |
| 143 | // Skip if already processed |
| 144 | if (isset($collected[$handle])) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | // Store the handle |
| 149 | $collected[$handle] = !empty($wp_scripts->registered[$handle]->deps) ? $wp_scripts->registered[$handle]->deps : []; |
| 150 | |
| 151 | // Recursively get dependencies of dependencies |
| 152 | if (!empty($collected[$handle])) { |
| 153 | $collected = $this->getAllScriptDependenciesRecursive($collected[$handle], $collected); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return $collected; |
| 158 | } |
| 159 | |
| 160 | public function getScriptIsFromTargetPlugin($handle, $pluginPathSearch) |
| 161 | { |
| 162 | global $wp_scripts; |
| 163 | if (!isset($wp_scripts->registered[$handle])) { |
| 164 | return false; |
| 165 | } |
| 166 | $reqistered_script = $wp_scripts->registered[$handle]; |
| 167 | $src = $reqistered_script->src; |
| 168 | $path_to_search = "plugins/$pluginPathSearch"; |
| 169 | return strpos($src, $path_to_search) !== false; |
| 170 | } |
| 171 | public function getStyleIsFromTargetPlugin($handle, $pluginPathSearch) |
| 172 | { |
| 173 | global $wp_styles; |
| 174 | if (!isset($wp_styles->registered[$handle])) { |
| 175 | return false; |
| 176 | } |
| 177 | $reqistered = $wp_styles->registered[$handle]; |
| 178 | $src = $reqistered->src; |
| 179 | $path_to_search = "plugins/$pluginPathSearch"; |
| 180 | return strpos($src, $path_to_search) !== false; |
| 181 | } |
| 182 | public static function getInstance() |
| 183 | { |
| 184 | if (! self::$instance) { |
| 185 | self::$instance = new self(); |
| 186 | } |
| 187 | |
| 188 | return self::$instance; |
| 189 | } |
| 190 | |
| 191 | public static function load() |
| 192 | { |
| 193 | return static::getInstance(); |
| 194 | } |
| 195 | } |
| 196 |