PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.6.7
Kubio AI Page Builder v2.6.7
2.8.4 2.8.3 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 License 1 year ago Separators 1 year ago StyleManager 1 year ago Styles 1 year ago Activation.php 1 year ago Backup.php 1 year ago CustomizerImporter.php 1 year ago Deactivation.php 1 year ago EditInKubioCustomizerPanel.php 1 year ago Element.php 1 year ago ElementBase.php 4 years ago Importer.php 1 year ago InnerBlocks.php 1 year ago KubioFrontPageRevertNotice.php 10 months ago LodashBasic.php 1 year ago Registry.php 1 year ago ThirdPartyPluginAssetLoaderInEditor.php 10 months ago Utils.php 1 year ago
ThirdPartyPluginAssetLoaderInEditor.php
199 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 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
32 if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) {
33 return;
34 }
35 global $wp_scripts, $wp_styles;
36
37 foreach ($wp_scripts->queue as $handle) {
38 if ($this->getScriptIsFromTargetPlugin($handle, 'kubio')) {
39 wp_dequeue_script($handle);
40 }
41 }
42
43 }
44
45
46 public static function addPlugin($pluginPathSearchString, $isActiveCallback, $shouldDequeueAssets = false)
47 {
48 $config = [
49 'pluginPathSearchString' => $pluginPathSearchString,
50 'isActiveCallback' => $isActiveCallback,
51 'shouldDequeueAssets' => $shouldDequeueAssets
52 ];
53 static::getInstance()->pluginsConfigs[] = $config;
54 }
55
56 public function dequeuePluginAssetsInKubioEditor()
57 {
58 foreach ($this->pluginsConfigs as $config) {
59 $currentPluginPathSearchString = $config['pluginPathSearchString'];
60 $isActiveCallback = $config['isActiveCallback'];
61 $shouldDequeueAssets = $config['shouldDequeueAssets'];
62
63 if(!$shouldDequeueAssets) {
64 continue;
65 }
66
67 $isActive = call_user_func($isActiveCallback);
68 if (!$isActive) {
69 return;
70 }
71 global $wp_scripts, $wp_styles;
72
73 foreach ($wp_scripts->queue as $handle) {
74 if ($this->getScriptIsFromTargetPlugin($handle, $currentPluginPathSearchString)) {
75 wp_dequeue_script($handle);
76 }
77 }
78
79 foreach ($wp_styles->queue as $handle) {
80 if ($this->getStyleIsFromTargetPlugin($handle, $currentPluginPathSearchString)) {
81 wp_dequeue_style($handle);
82 }
83 }
84 }
85 }
86 public function printAssetsList()
87 {
88 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
89 if (!isset($_GET['kubio-get-3rd-party-plugin-assets'])) {
90 return;
91 }
92
93 //search for a part of the plugin folder name in the url to find assets
94 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
95 $pluginPathSearch = sanitize_text_field( wp_unslash($_GET['kubio-get-3rd-party-plugin-assets'] ) ) ;
96 global $wp_scripts;
97
98
99 foreach ($this->pluginsConfigs as $config) {
100 $currentPluginPathSearchString = $config['pluginPathSearchString'];
101 $isActiveCallback = $config['isActiveCallback'];
102
103 $isActive = call_user_func($isActiveCallback);
104 if (!$isActive || $currentPluginPathSearchString !== $pluginPathSearch) {
105 continue;
106 }
107
108
109 $pluginScriptsHandles = [];
110
111 foreach ($wp_scripts->queue as $handle) {
112 if (!$this->getScriptIsFromTargetPlugin($handle, $pluginPathSearch)) {
113 continue;
114 }
115
116 $pluginScriptsHandles[] = $handle;
117 }
118 $dependencies = $this->getAllScriptDependencies($pluginScriptsHandles);
119
120
121 wp_print_inline_script_tag(
122 sprintf('window.kubioThirdParthyPluginsAssets = window.kubioThirdParthyPluginsAssets || {};
123 window.kubioThirdParthyPluginsAssets["%s"] = %s;', wp_kses_post($pluginPathSearch), wp_json_encode($dependencies))
124 );
125 }
126 }
127
128 public function getAllScriptDependencies($pluginScriptsHandles)
129 {
130 $collected = [];
131 $this->getAllScriptDependenciesRecursive($pluginScriptsHandles, $collected);
132 $singleArrayDeps = [];
133 foreach ($collected as $key => $value) {
134 $singleArrayDeps[] = $key;
135 $singleArrayDeps = array_merge(array(), $singleArrayDeps, $value);
136 };
137 $singleArrayDeps = array_values(array_unique($singleArrayDeps));
138 return $singleArrayDeps;
139 }
140
141 public function getAllScriptDependenciesRecursive($handles, &$collected = [])
142 {
143 global $wp_scripts;
144
145 foreach ($handles as $handle) {
146 // Skip if already processed
147 if (isset($collected[$handle])) {
148 continue;
149 }
150
151 // Store the handle
152 $collected[$handle] = !empty($wp_scripts->registered[$handle]->deps) ? $wp_scripts->registered[$handle]->deps : [];
153
154 // Recursively get dependencies of dependencies
155 if (!empty($collected[$handle])) {
156 $collected = $this->getAllScriptDependenciesRecursive($collected[$handle], $collected);
157 }
158 }
159
160 return $collected;
161 }
162
163 public function getScriptIsFromTargetPlugin($handle, $pluginPathSearch)
164 {
165 global $wp_scripts;
166 if (!isset($wp_scripts->registered[$handle])) {
167 return false;
168 }
169 $reqistered_script = $wp_scripts->registered[$handle];
170 $src = $reqistered_script->src;
171 $path_to_search = "plugins/$pluginPathSearch";
172 return strpos($src, $path_to_search) !== false;
173 }
174 public function getStyleIsFromTargetPlugin($handle, $pluginPathSearch)
175 {
176 global $wp_styles;
177 if (!isset($wp_styles->registered[$handle])) {
178 return false;
179 }
180 $reqistered = $wp_styles->registered[$handle];
181 $src = $reqistered->src;
182 $path_to_search = "plugins/$pluginPathSearch";
183 return strpos($src, $path_to_search) !== false;
184 }
185 public static function getInstance()
186 {
187 if (! self::$instance) {
188 self::$instance = new self();
189 }
190
191 return self::$instance;
192 }
193
194 public static function load()
195 {
196 return static::getInstance();
197 }
198 }
199