| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Integration Helper |
| 4 |
* |
| 5 |
* Helper functions for managing plugin integrations in setup wizard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace UltimatePostKit\SetupWizard; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Plugin_Integration_Helper { |
| 15 |
|
| 16 |
/** |
| 17 |
* Get predefined plugin configurations |
| 18 |
* |
| 19 |
* @return array Plugin configurations |
| 20 |
*/ |
| 21 |
public static function get_predefined_plugins() { |
| 22 |
return [ |
| 23 |
'bdthemes-element-pack-lite' => [ |
| 24 |
'recommended' => true, |
| 25 |
'fallback' => [ |
| 26 |
'name' => 'Element Pack', |
| 27 |
'description' => 'Build your website with 100+ ready-made blocks and widgets for Elementor.', |
| 28 |
'logo' => 'https://ps.w.org/bdthemes-element-pack-lite/assets/icon-256x256.png', |
| 29 |
'rating' => 4.8, |
| 30 |
'num_ratings' => 1000, |
| 31 |
'active_installs' => '50,000+' |
| 32 |
] |
| 33 |
], |
| 34 |
'bdthemes-prime-slider-lite/bdthemes-prime-slider.php' => [ |
| 35 |
'recommended' => true, |
| 36 |
'fallback' => [ |
| 37 |
'name' => 'Prime Slider', |
| 38 |
'description' => 'Create eye-catching sliders for your website quickly and easily with 55+ modern Elementor slider widgets.', |
| 39 |
'logo' => 'https://ps.w.org/bdthemes-prime-slider-lite/assets/icon-256x256.png', |
| 40 |
'rating' => 4.7, |
| 41 |
'num_ratings' => 500, |
| 42 |
'active_installs' => '100,000+' |
| 43 |
] |
| 44 |
], |
| 45 |
'ultimate-post-kit' => [ |
| 46 |
'recommended' => true, |
| 47 |
'fallback' => [ |
| 48 |
'name' => 'Ultimate Post Kit', |
| 49 |
'description' => 'Design beautiful post layouts with simple, ready-made blocks.', |
| 50 |
'logo' => 'https://ps.w.org/ultimate-post-kit/assets/icon-256x256.png', |
| 51 |
'rating' => 4.8, |
| 52 |
'num_ratings' => 1000, |
| 53 |
'active_installs' => '50,000+' |
| 54 |
] |
| 55 |
], |
| 56 |
'live-copy-paste' => [ |
| 57 |
'recommended' => false, |
| 58 |
'fallback' => [ |
| 59 |
'name' => 'Live Copy Paste', |
| 60 |
'description' => 'Copy and paste website elements between WordPress sites instantly.', |
| 61 |
'logo' => 'https://ps.w.org/live-copy-paste/assets/icon-256x256.png', |
| 62 |
'rating' => 4.9, |
| 63 |
'num_ratings' => 200, |
| 64 |
'active_installs' => '10,000+' |
| 65 |
] |
| 66 |
], |
| 67 |
'ultimate-store-kit' => [ |
| 68 |
'recommended' => true, |
| 69 |
'fallback' => [ |
| 70 |
'name' => 'Ultimate Store Kit', |
| 71 |
'description' => 'Improve your online store with tools to display products better.', |
| 72 |
'logo' => 'https://ps.w.org/ultimate-store-kit/assets/icon-256x256.png', |
| 73 |
'rating' => 4.6, |
| 74 |
'num_ratings' => 300, |
| 75 |
'active_installs' => '20,000+' |
| 76 |
] |
| 77 |
], |
| 78 |
// 'pixel-gallery' => [ |
| 79 |
// 'recommended' => false, |
| 80 |
// 'fallback' => [ |
| 81 |
// 'name' => 'Pixel Gallery', |
| 82 |
// 'description' => 'Show off your photos in a stylish, responsive gallery.', |
| 83 |
// 'logo' => 'https://ps.w.org/pixel-gallery/assets/icon-256x256.gif', |
| 84 |
// 'rating' => 4.5, |
| 85 |
// 'num_ratings' => 150, |
| 86 |
// 'active_installs' => '5,000+' |
| 87 |
// ] |
| 88 |
// ], |
| 89 |
// 'zoloblocks' => [ |
| 90 |
// 'recommended' => false, |
| 91 |
// 'fallback' => [ |
| 92 |
// 'name' => 'ZoloBlocks', |
| 93 |
// 'description' => 'Build amazing WordPress pages with helpful and flexible Gutenberg blocks.', |
| 94 |
// 'logo' => 'https://ps.w.org/zoloblocks/assets/icon-256x256.gif', |
| 95 |
// 'rating' => 4.3, |
| 96 |
// 'num_ratings' => 100, |
| 97 |
// 'active_installs' => '3,000+' |
| 98 |
// ] |
| 99 |
// ], |
| 100 |
// 'spin-wheel' => [ |
| 101 |
// 'recommended' => false, |
| 102 |
// 'fallback' => [ |
| 103 |
// 'name' => 'Spin Wheel', |
| 104 |
// 'description' => 'Add a fun, interactive spin wheel to offer instant coupons, boost engagement, and grow your email list.', |
| 105 |
// 'logo' => 'https://ps.w.org/spin-wheel/assets/icon-256x256.gif', |
| 106 |
// 'rating' => 4.2, |
| 107 |
// 'num_ratings' => 50, |
| 108 |
// 'active_installs' => '1,000+' |
| 109 |
// ] |
| 110 |
// ], |
| 111 |
// 'ai-image' => [ |
| 112 |
// 'recommended' => false, |
| 113 |
// 'fallback' => [ |
| 114 |
// 'name' => 'Instant Image Generator', |
| 115 |
// 'description' => 'Instant Image Generator (One Click Image Uploads from Pixabay, Pexels and OpenAI).', |
| 116 |
// 'logo' => 'https://ps.w.org/ai-image/assets/icon-256x256.gif', |
| 117 |
// 'rating' => 4.0, |
| 118 |
// 'num_ratings' => 25, |
| 119 |
// 'active_installs' => '500+' |
| 120 |
// ] |
| 121 |
// ], |
| 122 |
// 'dark-reader' => [ |
| 123 |
// 'recommended' => false, |
| 124 |
// 'fallback' => [ |
| 125 |
// 'name' => 'Dark Reader', |
| 126 |
// 'description' => 'Add beautiful dark mode to your WordPress site with customizable settings.', |
| 127 |
// 'logo' => 'https://ps.w.org/dark-reader/assets/icon-256x256.gif', |
| 128 |
// 'rating' => 4.1, |
| 129 |
// 'num_ratings' => 30, |
| 130 |
// 'active_installs' => '800+' |
| 131 |
// ] |
| 132 |
// ], |
| 133 |
// 'ar-viewer' => [ |
| 134 |
// 'recommended' => false, |
| 135 |
// 'fallback' => [ |
| 136 |
// 'name' => 'AR Viewer', |
| 137 |
// 'description' => 'Augmented Reality Viewer – 3D Model Viewer.', |
| 138 |
// 'logo' => 'https://ps.w.org/ar-viewer/assets/icon-256x256.gif', |
| 139 |
// 'rating' => 3.9, |
| 140 |
// 'num_ratings' => 15, |
| 141 |
// 'active_installs' => '200+' |
| 142 |
// ] |
| 143 |
// ] |
| 144 |
]; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Build plugin data array with API data and fallbacks |
| 149 |
* |
| 150 |
* @param array $plugin_slugs Array of plugin slugs to include |
| 151 |
* @return array Complete plugin data array |
| 152 |
*/ |
| 153 |
public static function build_plugin_data($plugin_slugs = []) { |
| 154 |
$predefined = self::get_predefined_plugins(); |
| 155 |
|
| 156 |
// Use new non-blocking approach - get cached data only from Remote_Data_Handler |
| 157 |
$fetched_data = []; |
| 158 |
if (function_exists('upk_get_remote_plugins')) { |
| 159 |
$fetched_data = upk_get_remote_plugins(); |
| 160 |
} |
| 161 |
|
| 162 |
$plugins = []; |
| 163 |
|
| 164 |
foreach ($plugin_slugs as $slug) { |
| 165 |
$config = $predefined[$slug] ?? ['recommended' => false, 'fallback' => []]; |
| 166 |
|
| 167 |
// Try to get data from remote cache first |
| 168 |
$api_slug = (strpos($slug, '/') !== false) ? dirname($slug) : $slug; |
| 169 |
$api_data = $fetched_data[$api_slug] ?? null; |
| 170 |
|
| 171 |
// Ensure api_data is a valid array with required fields |
| 172 |
if ($api_data && self::validate_plugin_data($api_data)) { |
| 173 |
// Determine the correct plugin slug format |
| 174 |
// If slug already contains .php, use it as-is, otherwise append default format |
| 175 |
$plugin_slug = (strpos($slug, '.php') !== false) ? $slug : $slug . '/' . $slug . '.php'; |
| 176 |
|
| 177 |
// Use API data with fallbacks and proper null checking |
| 178 |
$plugins[] = [ |
| 179 |
'logo' => $api_data['logo'] ?? ($config['fallback']['logo'] ?? ''), |
| 180 |
'rating' => $api_data['rating'] ?? 0, |
| 181 |
'rating_percentage' => $api_data['rating_percentage'] ?? 0, |
| 182 |
'num_ratings' => $api_data['num_ratings'] ?? 0, |
| 183 |
'name' => $api_data['name'] ?? ($config['fallback']['name'] ?? $slug), |
| 184 |
'slug' => $plugin_slug, |
| 185 |
'description' => $api_data['description'] ?? ($config['fallback']['description'] ?? ''), |
| 186 |
'active_installs' => $api_data['active_installs'] ?? ($config['fallback']['active_installs'] ?? '0'), |
| 187 |
'active_installs_count' => $api_data['active_installs_count'] ?? 0, |
| 188 |
'downloaded' => $api_data['downloaded'] ?? 0, |
| 189 |
'downloaded_formatted' => $api_data['downloaded_formatted'] ?? '', |
| 190 |
'recommended' => $config['recommended'], |
| 191 |
'version' => $api_data['version'] ?? '', |
| 192 |
'tested' => $api_data['tested'] ?? '', |
| 193 |
'last_updated' => $api_data['last_updated'] ?? '', |
| 194 |
'homepage' => $api_data['homepage'] ?? '' |
| 195 |
]; |
| 196 |
} else { |
| 197 |
// Determine the correct plugin slug format |
| 198 |
// If slug already contains .php, use it as-is, otherwise append default format |
| 199 |
$plugin_slug = (strpos($slug, '.php') !== false) ? $slug : $slug . '/' . $slug . '.php'; |
| 200 |
|
| 201 |
// Use fallback data with proper null checking |
| 202 |
$fallback = $config['fallback'] ?? []; |
| 203 |
$plugins[] = [ |
| 204 |
'logo' => $fallback['logo'] ?? '', |
| 205 |
'rating' => $fallback['rating'] ?? 0, |
| 206 |
'rating_percentage' => 0, |
| 207 |
'num_ratings' => $fallback['num_ratings'] ?? 0, |
| 208 |
'name' => $fallback['name'] ?? $slug, |
| 209 |
'slug' => $plugin_slug, |
| 210 |
'description' => $fallback['description'] ?? '', |
| 211 |
'active_installs' => $fallback['active_installs'] ?? '0', |
| 212 |
'active_installs_count' => 0, |
| 213 |
'downloaded' => 0, |
| 214 |
'downloaded_formatted' => '', |
| 215 |
'recommended' => $config['recommended'], |
| 216 |
'version' => '', |
| 217 |
'tested' => '', |
| 218 |
'last_updated' => '', |
| 219 |
'homepage' => '' |
| 220 |
]; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return $plugins; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Get recommended plugins only |
| 229 |
* |
| 230 |
* @return array Recommended plugins data |
| 231 |
*/ |
| 232 |
public static function get_recommended_plugins() { |
| 233 |
$predefined = self::get_predefined_plugins(); |
| 234 |
$recommended_slugs = array_keys(array_filter($predefined, function($config) { |
| 235 |
return $config['recommended']; |
| 236 |
})); |
| 237 |
|
| 238 |
return self::build_plugin_data($recommended_slugs); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get all available plugins |
| 243 |
* |
| 244 |
* @return array All plugins data |
| 245 |
*/ |
| 246 |
public static function get_all_plugins() { |
| 247 |
$predefined = self::get_predefined_plugins(); |
| 248 |
$all_slugs = array_keys($predefined); |
| 249 |
|
| 250 |
return self::build_plugin_data($all_slugs); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Add a new plugin to the predefined list |
| 255 |
* |
| 256 |
* @param string $slug Plugin slug |
| 257 |
* @param array $config Plugin configuration |
| 258 |
*/ |
| 259 |
public static function add_plugin($slug, $config) { |
| 260 |
// This would typically be stored in a database or options table |
| 261 |
// For now, it's just a helper method |
| 262 |
$predefined = self::get_predefined_plugins(); |
| 263 |
$predefined[$slug] = $config; |
| 264 |
|
| 265 |
// In a real implementation, you'd save this to options |
| 266 |
// update_option('upk_predefined_plugins', $predefined); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Check if a plugin is installed and active |
| 271 |
* |
| 272 |
* @param string $plugin_file Plugin file path |
| 273 |
* @return bool True if plugin is active |
| 274 |
*/ |
| 275 |
public static function is_plugin_active($plugin_file) { |
| 276 |
return is_plugin_active($plugin_file); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Get plugin installation status |
| 281 |
* |
| 282 |
* @param string $plugin_file Plugin file path |
| 283 |
* @return string Status: 'active', 'installed', 'not_installed' |
| 284 |
*/ |
| 285 |
public static function get_plugin_status($plugin_file) { |
| 286 |
if (self::is_plugin_active($plugin_file)) { |
| 287 |
return 'active'; |
| 288 |
} |
| 289 |
|
| 290 |
$installed_plugins = get_plugins(); |
| 291 |
if (isset($installed_plugins[$plugin_file])) { |
| 292 |
return 'installed'; |
| 293 |
} |
| 294 |
|
| 295 |
return 'not_installed'; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Validate plugin data structure |
| 300 |
* |
| 301 |
* @param array $plugin_data Plugin data to validate |
| 302 |
* @return bool True if valid |
| 303 |
*/ |
| 304 |
public static function validate_plugin_data($plugin_data) { |
| 305 |
if (!is_array($plugin_data)) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
$required_fields = ['name', 'slug', 'description', 'active_installs', 'rating', 'num_ratings']; |
| 310 |
|
| 311 |
foreach ($required_fields as $field) { |
| 312 |
if (!array_key_exists($field, $plugin_data)) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
return true; |
| 318 |
} |
| 319 |
} |