| @@ -15,8 +15,16 @@ | ||
| 15 | 15 | |
| 16 | 16 | private $endpoint = 'dependencies'; |
| 17 | 17 | public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }'; |
| 18 | 18 | |
| 19 | + public function __construct() { | |
| 20 | + parent::__construct(); | |
| 21 | + | |
| 22 | + if(!empty($_GET['disable_redirect'])) { | |
| 23 | + add_filter('wp_redirect', '__return_false', 999); | |
| 24 | + } | |
| 25 | + } | |
| 26 | + | |
| 19 | 27 | public function permission_check( WP_REST_Request $request ) { |
| 20 | 28 | $this->request = $request; |
| 21 | 29 | // $_route = $request->get_route(); |
| 22 | 30 | |
| @@ -28,10 +36,10 @@ | ||
| 28 | 36 | } |
| 29 | 37 | |
| 30 | 38 | public function register_routes() { |
| 31 | 39 | $this->get( $this->endpoint, [ $this, 'get_dependencies' ] ); |
| 32 | - $this->get( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); | |
| 33 | - $this->get( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); | |
| 40 | + $this->post( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); | |
| 41 | + $this->post( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); | |
| 34 | 42 | $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 35 | 43 | $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 36 | 44 | } |
| 37 | 45 | |
| @@ -142,16 +150,17 @@ | ||
| 142 | 150 | |
| 143 | 151 | public function get_plugins() { |
| 144 | 152 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 145 | 153 | |
| 146 | - $plugins = array(); | |
| 154 | + // Get parameters from request | |
| 155 | + $dependencies = $this->get_param( 'dependencies', [], null ); | |
| 156 | + $platform = $this->get_param( 'platform', 'elementor' ); | |
| 157 | + $categories = $this->get_param( 'categories', [], null ); | |
| 147 | 158 | |
| 159 | + // Get all plugins for checking status | |
| 160 | + $all_plugins = array(); | |
| 148 | 161 | foreach ( get_plugins() as $file => $data ) { |
| 149 | - // if ( is_wp_error( $this->check_read_permission( $file ) ) ) { | |
| 150 | - // continue; | |
| 151 | - // } | |
| 152 | - | |
| 153 | - $data = array( | |
| 162 | + $plugin_data = array( | |
| 154 | 163 | 'plugin' => substr( $file, 0, - 4 ), |
| 155 | 164 | 'status' => $this->get_plugin_status( $file ), |
| 156 | 165 | 'name' => $data['Name'], |
| 157 | 166 | 'plugin_uri' => $data['PluginURI'], |
| @@ -166,58 +175,208 @@ | ||
| 166 | 175 | 'requires_wp' => $data['RequiresWP'], |
| 167 | 176 | 'requires_php' => $data['RequiresPHP'], |
| 168 | 177 | 'textdomain' => $data['TextDomain'], |
| 169 | 178 | ); |
| 179 | + $all_plugins[] = $plugin_data; | |
| 180 | + } | |
| 170 | 181 | |
| 171 | - $plugins[] = $data; | |
| 182 | + // Process dependencies and return only necessary data | |
| 183 | + $new_dependency_list = []; | |
| 184 | + $new_required_plugins = []; | |
| 185 | + | |
| 186 | + // Platform-specific plugins | |
| 187 | + if ( $platform === 'elementor' ) { | |
| 188 | + $elementor_plugin = $this->find_plugin_by_name( $all_plugins, 'elementor/elementor' ); | |
| 189 | + $e_plugin = array( | |
| 190 | + 'plugin_file' => 'elementor/elementor.php', | |
| 191 | + 'plugin_original_slug' => 'elementor', | |
| 192 | + 'name' => $elementor_plugin ? $elementor_plugin['name'] : 'Elementor', | |
| 193 | + 'installed' => $elementor_plugin ? ( $elementor_plugin['status'] === 'active' ) : false, | |
| 194 | + 'mustHave' => true, | |
| 195 | + ); | |
| 196 | + $new_dependency_list[] = $e_plugin; | |
| 197 | + if ( ! $elementor_plugin || $elementor_plugin['status'] !== 'active' ) { | |
| 198 | + $new_required_plugins[] = $e_plugin; | |
| 199 | + } | |
| 200 | + } elseif ( $platform === 'gutenberg' ) { | |
| 201 | + // Check if WordPress supports Gutenberg natively or if Gutenberg plugin is needed | |
| 202 | + $gutenberg_plugin = $this->find_plugin_by_name( $all_plugins, 'gutenberg/gutenberg' ); | |
| 203 | + | |
| 204 | + // Note: templately.is_wp_support_gutenberg is a frontend variable, | |
| 205 | + // we'll assume Gutenberg plugin is needed if it exists and is inactive | |
| 206 | + if ( $gutenberg_plugin && $gutenberg_plugin['status'] === 'inactive' ) { | |
| 207 | + $g_plugin = array( | |
| 208 | + 'plugin_file' => 'gutenberg/gutenberg.php', | |
| 209 | + 'plugin_original_slug' => 'gutenberg', | |
| 210 | + 'name' => $gutenberg_plugin['name'], | |
| 211 | + 'mustHave' => true, | |
| 212 | + ); | |
| 213 | + $new_dependency_list[] = $g_plugin; | |
| 214 | + $new_required_plugins[] = $g_plugin; | |
| 215 | + } | |
| 172 | 216 | } |
| 173 | 217 | |
| 174 | - return new \WP_REST_Response( $plugins ); | |
| 218 | + // Process template dependencies | |
| 219 | + if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { | |
| 220 | + foreach ( $dependencies as $plugin_file => $plugin_obj ) { | |
| 221 | + if ( ! is_array( $plugin_obj ) ) { | |
| 222 | + continue; | |
| 223 | + } | |
| 224 | + | |
| 225 | + $plugin_name = str_replace( '.php', '', $plugin_file ); | |
| 226 | + $plugin = $this->find_plugin_by_name( $all_plugins, $plugin_name ); | |
| 227 | + | |
| 228 | + if ( $plugin && $plugin['status'] === 'active' ) { | |
| 229 | + $plugin_obj['installed'] = true; | |
| 230 | + $new_dependency_list[] = $plugin_obj; | |
| 231 | + } else { | |
| 232 | + $new_dependency_list[] = $plugin_obj; | |
| 233 | + $new_required_plugins[] = $plugin_obj; | |
| 234 | + } | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + // Category-based plugins | |
| 239 | + $included_categories = array( | |
| 240 | + 'blog-magazine', | |
| 241 | + 'construction', | |
| 242 | + 'ecommerce', | |
| 243 | + 'education', | |
| 244 | + 'entertainment', | |
| 245 | + 'fashion-lifestyle', | |
| 246 | + 'features-services', | |
| 247 | + 'food-restaurant', | |
| 248 | + 'marketing', | |
| 249 | + 'miscellaneous', | |
| 250 | + 'multipurpose', | |
| 251 | + 'nft-cryptocurrency', | |
| 252 | + 'non-profit', | |
| 253 | + 'technology', | |
| 254 | + 'travel', | |
| 255 | + ); | |
| 256 | + | |
| 257 | + $is_any_category_included = false; | |
| 258 | + if ( ! empty( $categories ) && is_array( $categories ) ) { | |
| 259 | + foreach ( $categories as $category ) { | |
| 260 | + if ( isset( $category['slug'] ) && in_array( $category['slug'], $included_categories, true ) ) { | |
| 261 | + $is_any_category_included = true; | |
| 262 | + break; | |
| 263 | + } | |
| 264 | + } | |
| 265 | + } | |
| 266 | + | |
| 267 | + if ( $is_any_category_included ) { | |
| 268 | + $nx_plugin = $this->find_plugin_by_name( $all_plugins, 'notificationx/notificationx' ); | |
| 269 | + $notificationx = array( | |
| 270 | + 'name' => 'NotificationX', | |
| 271 | + 'icon' => 'https://ps.w.org/notificationx/assets/icon-256x256.gif?rev=2783824', | |
| 272 | + 'plugin_file' => 'notificationx/notificationx.php', | |
| 273 | + 'plugin_original_slug' => 'notificationx', | |
| 274 | + 'is_pro' => false, | |
| 275 | + 'installed' => $nx_plugin ? ( $nx_plugin['status'] === 'active' ) : false, | |
| 276 | + 'link' => 'https://wordpress.org/plugins/notificationx/', | |
| 277 | + ); | |
| 278 | + $new_dependency_list[] = $notificationx; | |
| 279 | + $new_required_plugins[] = $notificationx; | |
| 280 | + } | |
| 281 | + | |
| 282 | + // EmbedPress for blog-magazine category | |
| 283 | + $ep_is_any_category_included = false; | |
| 284 | + if ( ! empty( $categories ) && is_array( $categories ) ) { | |
| 285 | + foreach ( $categories as $category ) { | |
| 286 | + if ( isset( $category['slug'] ) && $category['slug'] === 'blog-magazine' ) { | |
| 287 | + $ep_is_any_category_included = true; | |
| 288 | + break; | |
| 289 | + } | |
| 290 | + } | |
| 291 | + } | |
| 292 | + | |
| 293 | + if ( $ep_is_any_category_included ) { | |
| 294 | + $ep_plugin = $this->find_plugin_by_name( $all_plugins, 'embedpress/embedpress' ); | |
| 295 | + $embed_press = array( | |
| 296 | + 'name' => 'EmbedPress', | |
| 297 | + 'icon' => 'https://ps.w.org/embedpress/assets/icon-256x256.gif?rev=2783824', | |
| 298 | + 'plugin_file' => 'embedpress/embedpress.php', | |
| 299 | + 'plugin_original_slug' => 'embedpress', | |
| 300 | + 'is_pro' => false, | |
| 301 | + 'installed' => $ep_plugin ? ( $ep_plugin['status'] === 'active' ) : false, | |
| 302 | + 'link' => 'https://wordpress.org/plugins/embedpress/', | |
| 303 | + ); | |
| 304 | + $new_dependency_list[] = $embed_press; | |
| 305 | + $new_required_plugins[] = $embed_press; | |
| 306 | + } | |
| 307 | + | |
| 308 | + return new \WP_REST_Response( array( | |
| 309 | + 'dependencyList' => $new_dependency_list, | |
| 310 | + 'requiredPlugins' => $new_required_plugins | |
| 311 | + ) ); | |
| 175 | 312 | } |
| 176 | 313 | |
| 314 | + /** | |
| 315 | + * Helper method to find a plugin by its name/slug | |
| 316 | + * | |
| 317 | + * @param array $plugins Array of all plugins | |
| 318 | + * @param string $plugin_name Plugin name to search for | |
| 319 | + * @return array|null Plugin data or null if not found | |
| 320 | + */ | |
| 321 | + private function find_plugin_by_name( $plugins, $plugin_name ) { | |
| 322 | + foreach ( $plugins as $plugin ) { | |
| 323 | + if ( $plugin['plugin'] === $plugin_name ) { | |
| 324 | + return $plugin; | |
| 325 | + } | |
| 326 | + } | |
| 327 | + return null; | |
| 328 | + } | |
| 329 | + | |
| 177 | 330 | public function get_themes() { |
| 178 | - $themes = array(); | |
| 331 | + // Get platform parameter from request | |
| 332 | + $platform = $this->get_param( 'platform', 'elementor' ); | |
| 179 | 333 | |
| 180 | 334 | $active_themes = wp_get_themes(); |
| 181 | 335 | $current_theme = wp_get_theme(); |
| 182 | 336 | |
| 183 | - foreach ( $active_themes as $theme_name => $theme ) { | |
| 184 | - $data = array( | |
| 185 | - 'status' => $theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive', | |
| 186 | - 'template' => $theme->get_template(), | |
| 187 | - 'stylesheet' => $theme->get_stylesheet(), | |
| 188 | - ); | |
| 337 | + $recommended_theme = array(); | |
| 189 | 338 | |
| 339 | + if ( $platform === 'elementor' ) { | |
| 340 | + // Look for Hello Elementor theme | |
| 341 | + $target_stylesheet = 'hello-elementor'; | |
| 342 | + $elementor_theme = null; | |
| 190 | 343 | |
| 191 | - $plain_field_mappings = array( | |
| 192 | - 'requires_php' => 'RequiresPHP', | |
| 193 | - 'requires_wp' => 'RequiresWP', | |
| 194 | - 'textdomain' => 'TextDomain', | |
| 195 | - 'version' => 'Version', | |
| 196 | - ); | |
| 197 | - | |
| 198 | - foreach ( $plain_field_mappings as $field => $header ) { | |
| 199 | - $data[ $field ] = $theme->get( $header ); | |
| 344 | + foreach ( $active_themes as $theme ) { | |
| 345 | + if ( $theme->get_stylesheet() === $target_stylesheet ) { | |
| 346 | + $elementor_theme = $theme; | |
| 347 | + break; | |
| 348 | + } | |
| 200 | 349 | } |
| 201 | 350 | |
| 202 | - $rich_field_mappings = array( | |
| 203 | - 'author' => 'Author', | |
| 204 | - 'author_uri' => 'AuthorURI', | |
| 205 | - 'description' => 'Description', | |
| 206 | - 'name' => 'Name', | |
| 207 | - 'tags' => 'Tags', | |
| 208 | - 'theme_uri' => 'ThemeURI', | |
| 351 | + $recommended_theme = array( | |
| 352 | + 'name' => $elementor_theme ? $elementor_theme->display( 'Name' ) : 'Hello Elementor', | |
| 353 | + 'status' => $elementor_theme ? ( $elementor_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', | |
| 354 | + 'template' => $elementor_theme ? $elementor_theme->get_template() : 'hello-elementor', | |
| 355 | + 'stylesheet' => $elementor_theme ? $elementor_theme->get_stylesheet() : 'hello-elementor', | |
| 209 | 356 | ); |
| 210 | 357 | |
| 211 | - foreach ( $rich_field_mappings as $field => $header ) { | |
| 212 | - $data[ $field ]['raw'] = $theme->display( $header, false, true ); | |
| 213 | - $data[ $field ]['rendered'] = $theme->display( $header ); | |
| 358 | + } elseif ( $platform === 'gutenberg' ) { | |
| 359 | + // Look for Twenty Twenty-Four theme | |
| 360 | + $target_stylesheet = 'twentytwentyfour'; | |
| 361 | + $gutenberg_theme = null; | |
| 362 | + | |
| 363 | + foreach ( $active_themes as $theme ) { | |
| 364 | + if ( $theme->get_stylesheet() === $target_stylesheet ) { | |
| 365 | + $gutenberg_theme = $theme; | |
| 366 | + break; | |
| 367 | + } | |
| 214 | 368 | } |
| 215 | 369 | |
| 216 | - $themes[] = $data; | |
| 370 | + $recommended_theme = array( | |
| 371 | + 'name' => $gutenberg_theme ? $gutenberg_theme->display( 'Name' ) : 'Twenty Twenty-Four', | |
| 372 | + 'status' => $gutenberg_theme ? ( $gutenberg_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', | |
| 373 | + 'template' => $gutenberg_theme ? $gutenberg_theme->get_template() : 'twentytwentyfour', | |
| 374 | + 'stylesheet' => $gutenberg_theme ? $gutenberg_theme->get_stylesheet() : 'twentytwentyfour', | |
| 375 | + ); | |
| 217 | 376 | } |
| 218 | 377 | |
| 219 | - return new \WP_REST_Response( $themes ); | |
| 378 | + return new \WP_REST_Response( $recommended_theme ); | |
| 220 | 379 | } |
| 221 | 380 | |
| 222 | 381 | /** |
| 223 | 382 | * Get's the activation status for a plugin. |
| @@ -238,5 +397,5 @@ | ||
| 238 | 397 | |
| 239 | 398 | return 'inactive'; |
| 240 | 399 | } |
| 241 | 400 | |
| 242 | -} | |
| 401 | +} | |