PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / trunk
Kubio AI Page Builder vtrunk
2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / ThirdPartyPluginAssetLoaderInEditor.php
kubio / lib / src / Core Last commit date
Background 1 year ago Blocks 1 year ago GlobalElements 1 year ago Layout 1 year ago Separators 11 months ago StyleManager 1 month 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 1 month ago InnerBlocks.php 1 year ago KubioFrontPageRevertNotice.php 9 months ago LodashBasic.php 1 year ago Registry.php 1 year ago ThirdPartyPluginAssetLoaderInEditor.php 3 months ago Utils.php 1 day ago
ThirdPartyPluginAssetLoaderInEditor.php
200 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('wp_print_scripts', array($this, 'printAssetsList'), 20);
25 add_action('kubio/editor/enqueue_assets', array($this, 'dequeuePluginAssetsInKubioEditor'), 20);
26 add_action('enqueue_block_editor_assets', array($this, 'dequeueKubioScriptsInIframeRequest'), 9999);
27 }
28
29
30 //to not cause weird issues
31 public function dequeueKubioScriptsInIframeRequest() {
32 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
33 if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) {
34 return;
35 }
36 global $wp_scripts, $wp_styles;
37
38 foreach ($wp_scripts->queue as $handle) {
39 if ($this->getScriptIsFromTargetPlugin($handle, 'kubio')) {
40 wp_dequeue_script($handle);
41 }
42 }
43
44 }
45
46
47 public static function addPlugin($pluginPathSearchString, $isActiveCallback, $shouldDequeueAssets = false)
48 {
49 $config = [
50 'pluginPathSearchString' => $pluginPathSearchString,
51 'isActiveCallback' => $isActiveCallback,
52 'shouldDequeueAssets' => $shouldDequeueAssets
53 ];
54 static::getInstance()->pluginsConfigs[] = $config;
55 }
56
57 public function dequeuePluginAssetsInKubioEditor()
58 {
59 foreach ($this->pluginsConfigs as $config) {
60 $currentPluginPathSearchString = $config['pluginPathSearchString'];
61 $isActiveCallback = $config['isActiveCallback'];
62 $shouldDequeueAssets = $config['shouldDequeueAssets'];
63
64 if(!$shouldDequeueAssets) {
65 continue;
66 }
67
68 $isActive = call_user_func($isActiveCallback);
69 if (!$isActive) {
70 return;
71 }
72 global $wp_scripts, $wp_styles;
73
74 foreach ($wp_scripts->queue as $handle) {
75 if ($this->getScriptIsFromTargetPlugin($handle, $currentPluginPathSearchString)) {
76 wp_dequeue_script($handle);
77 }
78 }
79
80 foreach ($wp_styles->queue as $handle) {
81 if ($this->getStyleIsFromTargetPlugin($handle, $currentPluginPathSearchString)) {
82 wp_dequeue_style($handle);
83 }
84 }
85 }
86 }
87 public function printAssetsList()
88 {
89 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
90 if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) {
91 return;
92 }
93
94 //search for a part of the plugin folder name in the url to find assets
95 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
96 $pluginPathSearch = sanitize_text_field( wp_unslash($_GET['kubio-get-3rd-party-plugin-assets'] ) ) ;
97 global $wp_scripts;
98
99
100 foreach ($this->pluginsConfigs as $config) {
101 $currentPluginPathSearchString = $config['pluginPathSearchString'];
102 $isActiveCallback = $config['isActiveCallback'];
103
104 $isActive = call_user_func($isActiveCallback);
105 if (!$isActive || $currentPluginPathSearchString !== $pluginPathSearch) {
106 continue;
107 }
108
109
110 $pluginScriptsHandles = [];
111
112 foreach ($wp_scripts->queue as $handle) {
113 if (!$this->getScriptIsFromTargetPlugin($handle, $pluginPathSearch)) {
114 continue;
115 }
116
117 $pluginScriptsHandles[] = $handle;
118 }
119 $dependencies = $this->getAllScriptDependencies($pluginScriptsHandles);
120
121
122 wp_print_inline_script_tag(
123 sprintf('window.kubioThirdParthyPluginsAssets = window.kubioThirdParthyPluginsAssets || {};
124 window.kubioThirdParthyPluginsAssets["%s"] = %s;', wp_kses_post($pluginPathSearch), wp_json_encode($dependencies))
125 );
126 }
127 }
128
129 public function getAllScriptDependencies($pluginScriptsHandles)
130 {
131 $collected = [];
132 $this->getAllScriptDependenciesRecursive($pluginScriptsHandles, $collected);
133 $singleArrayDeps = [];
134 foreach ($collected as $key => $value) {
135 $singleArrayDeps[] = $key;
136 $singleArrayDeps = array_merge(array(), $singleArrayDeps, $value);
137 };
138 $singleArrayDeps = array_values(array_unique($singleArrayDeps));
139 return $singleArrayDeps;
140 }
141
142 public function getAllScriptDependenciesRecursive($handles, &$collected = [])
143 {
144 global $wp_scripts;
145
146 foreach ($handles as $handle) {
147 // Skip if already processed
148 if (isset($collected[$handle])) {
149 continue;
150 }
151
152 // Store the handle
153 $collected[$handle] = !empty($wp_scripts->registered[$handle]->deps) ? $wp_scripts->registered[$handle]->deps : [];
154
155 // Recursively get dependencies of dependencies
156 if (!empty($collected[$handle])) {
157 $collected = $this->getAllScriptDependenciesRecursive($collected[$handle], $collected);
158 }
159 }
160
161 return $collected;
162 }
163
164 public function getScriptIsFromTargetPlugin($handle, $pluginPathSearch)
165 {
166 global $wp_scripts;
167 if (!isset($wp_scripts->registered[$handle])) {
168 return false;
169 }
170 $reqistered_script = $wp_scripts->registered[$handle];
171 $src = $reqistered_script->src;
172 $path_to_search = "plugins/$pluginPathSearch";
173 return strpos($src, $path_to_search) !== false;
174 }
175 public function getStyleIsFromTargetPlugin($handle, $pluginPathSearch)
176 {
177 global $wp_styles;
178 if (!isset($wp_styles->registered[$handle])) {
179 return false;
180 }
181 $reqistered = $wp_styles->registered[$handle];
182 $src = $reqistered->src;
183 $path_to_search = "plugins/$pluginPathSearch";
184 return strpos($src, $path_to_search) !== false;
185 }
186 public static function getInstance()
187 {
188 if (! self::$instance) {
189 self::$instance = new self();
190 }
191
192 return self::$instance;
193 }
194
195 public static function load()
196 {
197 return static::getInstance();
198 }
199 }
200