| @@ -15,15 +15,23 @@ | ||
| 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 | - $_route = $request->get_route(); | |
| 29 | + // $_route = $request->get_route(); | |
| 22 | 30 | |
| 23 | - if( $_route === '/templately/v1/dependencies/install' && ! current_user_can( 'install_plugins' ) ) { | |
| 24 | - return $this->error('invalid_permission', __( 'Sorry, you do not have permission to install a plugin.', 'templately' ), 'dependencies/install', 403 ); | |
| 25 | - } | |
| 31 | + // if( $_route === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) { | |
| 32 | + // return Helper::error('invalid_permission', __( 'Sorry, you do not have permission to install a plugin.', 'templately' ), 'dependencies/install', 403 ); | |
| 33 | + // } | |
| 26 | 34 | |
| 27 | 35 | return true; |
| 28 | 36 | } |
| 29 | 37 | |
| @@ -28,8 +36,10 @@ | ||
| 28 | 36 | } |
| 29 | 37 | |
| 30 | 38 | public function register_routes() { |
| 31 | 39 | $this->get( $this->endpoint, [ $this, 'get_dependencies' ] ); |
| 40 | + $this->post( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); | |
| 41 | + $this->post( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); | |
| 32 | 42 | $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 33 | 43 | $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 34 | 44 | } |
| 35 | 45 | |
| @@ -130,7 +140,262 @@ | ||
| 130 | 140 | '/install', |
| 131 | 141 | 400 |
| 132 | 142 | ); |
| 133 | 143 | } |
| 134 | - return Installer::get_instance()->install( $requirements ); | |
| 144 | + $installed = Installer::get_instance()->install( $requirements ); | |
| 145 | + if(empty($installed['success'])){ | |
| 146 | + return $this->error($installed['code'], $installed['message'], 'dependencies/install', 403 ); | |
| 147 | + } | |
| 148 | + return $installed; | |
| 135 | 149 | } |
| 136 | -} | |
| 150 | + | |
| 151 | + public function get_plugins() { | |
| 152 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 153 | + | |
| 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 ); | |
| 158 | + | |
| 159 | + // Get all plugins for checking status | |
| 160 | + $all_plugins = array(); | |
| 161 | + foreach ( get_plugins() as $file => $data ) { | |
| 162 | + $plugin_data = array( | |
| 163 | + 'plugin' => substr( $file, 0, - 4 ), | |
| 164 | + 'status' => $this->get_plugin_status( $file ), | |
| 165 | + 'name' => $data['Name'], | |
| 166 | + 'plugin_uri' => $data['PluginURI'], | |
| 167 | + 'author' => $data['Author'], | |
| 168 | + 'author_uri' => $data['AuthorURI'], | |
| 169 | + 'description' => array( | |
| 170 | + 'raw' => $data['Description'], | |
| 171 | + 'rendered' => $data['Description'], | |
| 172 | + ), | |
| 173 | + 'version' => $data['Version'], | |
| 174 | + 'network_only' => $data['Network'], | |
| 175 | + 'requires_wp' => $data['RequiresWP'], | |
| 176 | + 'requires_php' => $data['RequiresPHP'], | |
| 177 | + 'textdomain' => $data['TextDomain'], | |
| 178 | + ); | |
| 179 | + $all_plugins[] = $plugin_data; | |
| 180 | + } | |
| 181 | + | |
| 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 | + } | |
| 216 | + } | |
| 217 | + | |
| 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 | + ) ); | |
| 312 | + } | |
| 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 | + | |
| 330 | + public function get_themes() { | |
| 331 | + // Get platform parameter from request | |
| 332 | + $platform = $this->get_param( 'platform', 'elementor' ); | |
| 333 | + | |
| 334 | + $active_themes = wp_get_themes(); | |
| 335 | + $current_theme = wp_get_theme(); | |
| 336 | + | |
| 337 | + $recommended_theme = array(); | |
| 338 | + | |
| 339 | + if ( $platform === 'elementor' ) { | |
| 340 | + // Look for Hello Elementor theme | |
| 341 | + $target_stylesheet = 'hello-elementor'; | |
| 342 | + $elementor_theme = null; | |
| 343 | + | |
| 344 | + foreach ( $active_themes as $theme ) { | |
| 345 | + if ( $theme->get_stylesheet() === $target_stylesheet ) { | |
| 346 | + $elementor_theme = $theme; | |
| 347 | + break; | |
| 348 | + } | |
| 349 | + } | |
| 350 | + | |
| 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', | |
| 356 | + ); | |
| 357 | + | |
| 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 | + } | |
| 368 | + } | |
| 369 | + | |
| 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 | + ); | |
| 376 | + } | |
| 377 | + | |
| 378 | + return new \WP_REST_Response( $recommended_theme ); | |
| 379 | + } | |
| 380 | + | |
| 381 | + /** | |
| 382 | + * Get's the activation status for a plugin. | |
| 383 | + * | |
| 384 | + * @since 5.5.0 | |
| 385 | + * | |
| 386 | + * @param string $plugin The plugin file to check. | |
| 387 | + * @return string Either 'active' or 'inactive'. | |
| 388 | + */ | |
| 389 | + protected function get_plugin_status( $plugin ) { | |
| 390 | + if ( is_plugin_active_for_network( $plugin ) ) { | |
| 391 | + return 'active'; | |
| 392 | + } | |
| 393 | + | |
| 394 | + if ( is_plugin_active( $plugin ) ) { | |
| 395 | + return 'active'; | |
| 396 | + } | |
| 397 | + | |
| 398 | + return 'inactive'; | |
| 399 | + } | |
| 400 | + | |
| 401 | +} | |