| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | use Templately\Utils\Helper; |
| 7 | 7 | use Templately\Utils\Database; |
| 8 | 8 | use Templately\Utils\Installer; |
| 9 | 9 | use WP_REST_Request; |
| 10 | -use Templately\Utils\Caching; | |
| 11 | 10 | |
| 12 | 11 | use function current_user_can; |
| 13 | 12 | |
| 14 | 13 | |
| @@ -14,33 +13,16 @@ | ||
| 14 | 13 | |
| 15 | 14 | class Dependencies extends API { |
| 16 | 15 | |
| 17 | 16 | private $endpoint = 'dependencies'; |
| 18 | - | |
| 19 | 17 | public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }'; |
| 20 | 18 | |
| 21 | - public function __construct() { | |
| 22 | - parent::__construct(); | |
| 23 | - | |
| 24 | - if(!empty($_GET['disable_redirect'])) { | |
| 25 | - add_filter('wp_redirect', '__return_false', 999); | |
| 26 | - } | |
| 27 | - } | |
| 28 | - | |
| 29 | 19 | public function permission_check( WP_REST_Request $request ) { |
| 30 | 20 | $this->request = $request; |
| 21 | + $_route = $request->get_route(); | |
| 31 | 22 | |
| 32 | - // Installing reaches beyond template content, so it does not ride the | |
| 33 | - // `delete_posts` base gate the read routes are happy with. Installer::install() | |
| 34 | - // checks `install_plugins` / `activate_plugins` per plugin as well; this is the | |
| 35 | - // front gate, so the route cannot be probed at all without the capability. | |
| 36 | - if ( $request->get_route() === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) { | |
| 37 | - return $this->error( | |
| 38 | - 'invalid_permission', | |
| 39 | - __( 'Sorry, you do not have permission to install a plugin.', 'templately' ), | |
| 40 | - 'dependencies/install', | |
| 41 | - rest_authorization_required_code() | |
| 42 | - ); | |
| 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 ); | |
| 43 | 25 | } |
| 44 | 26 | |
| 45 | 27 | return true; |
| 46 | 28 | } |
| @@ -46,10 +28,8 @@ | ||
| 46 | 28 | } |
| 47 | 29 | |
| 48 | 30 | public function register_routes() { |
| 49 | 31 | $this->get( $this->endpoint, [ $this, 'get_dependencies' ] ); |
| 50 | - $this->post( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); | |
| 51 | - $this->post( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); | |
| 52 | 32 | $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 53 | 33 | $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 54 | 34 | } |
| 55 | 35 | |
| @@ -96,15 +76,14 @@ | ||
| 96 | 76 | |
| 97 | 77 | $_inactive_plugins = []; |
| 98 | 78 | $_plugins = Helper::get_plugins(); |
| 99 | 79 | |
| 100 | - if( $platform === 'elementor' ) { | |
| 80 | + if( ! Helper::is_plugin_active( 'elementor/elementor.php' ) && $platform === 'elementor' ) { | |
| 101 | 81 | $elementor_plugin = new stdClass(); |
| 102 | 82 | $elementor_plugin->name = __( 'Elementor', 'templately' ); |
| 103 | 83 | $elementor_plugin->plugin_file = 'elementor/elementor.php'; |
| 104 | 84 | $elementor_plugin->slug = 'elementor'; |
| 105 | 85 | $elementor_plugin->is_pro = false; |
| 106 | - $elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' ); | |
| 107 | 86 | |
| 108 | 87 | $_inactive_plugins[] = $elementor_plugin; |
| 109 | 88 | } |
| 110 | 89 | |
| @@ -114,14 +93,12 @@ | ||
| 114 | 93 | continue; |
| 115 | 94 | } |
| 116 | 95 | |
| 117 | 96 | $dependency = ( object ) $dependency; |
| 118 | - if ( is_null( $dependency->plugin_file ) ) { | |
| 97 | + if ( is_null( $dependency->plugin_file ) || Helper::is_plugin_active( $dependency->plugin_file ) ) { | |
| 119 | 98 | continue; |
| 120 | 99 | } |
| 121 | 100 | |
| 122 | - $dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file ); | |
| 123 | - | |
| 124 | 101 | if( isset( $dependency->plugin_original_slug ) ) { |
| 125 | 102 | $dependency->slug = $dependency->plugin_original_slug; |
| 126 | 103 | unset( $dependency->plugin_original_slug ); |
| 127 | 104 | } |
| @@ -150,274 +127,7 @@ | ||
| 150 | 127 | '/install', |
| 151 | 128 | 400 |
| 152 | 129 | ); |
| 153 | 130 | } |
| 154 | - $installed = Installer::get_instance()->install( $requirements ); | |
| 155 | - if(empty($installed['success'])){ | |
| 156 | - return $this->error($installed['code'], $installed['message'], 'dependencies/install', 403 ); | |
| 157 | - } | |
| 158 | - return $installed; | |
| 131 | + return Installer::get_instance()->install( $requirements ); | |
| 159 | 132 | } |
| 160 | - | |
| 161 | - public function get_plugins() { | |
| 162 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 163 | - | |
| 164 | - // Get parameters from request | |
| 165 | - $dependencies = $this->get_param( 'dependencies', [], null ); | |
| 166 | - $platform = $this->get_param( 'platform', 'elementor' ); | |
| 167 | - $categories = $this->get_param( 'categories', [], null ); | |
| 168 | - | |
| 169 | - // Get all plugins for checking status | |
| 170 | - $all_plugins = array(); | |
| 171 | - foreach ( get_plugins() as $file => $data ) { | |
| 172 | - $plugin_data = array( | |
| 173 | - 'plugin' => substr( $file, 0, - 4 ), | |
| 174 | - 'status' => $this->get_plugin_status( $file ), | |
| 175 | - 'name' => $data['Name'], | |
| 176 | - 'plugin_uri' => $data['PluginURI'], | |
| 177 | - 'author' => $data['Author'], | |
| 178 | - 'author_uri' => $data['AuthorURI'], | |
| 179 | - 'description' => array( | |
| 180 | - 'raw' => $data['Description'], | |
| 181 | - 'rendered' => $data['Description'], | |
| 182 | - ), | |
| 183 | - 'version' => $data['Version'], | |
| 184 | - 'network_only' => $data['Network'], | |
| 185 | - 'requires_wp' => $data['RequiresWP'], | |
| 186 | - 'requires_php' => $data['RequiresPHP'], | |
| 187 | - 'textdomain' => $data['TextDomain'], | |
| 188 | - ); | |
| 189 | - $all_plugins[] = $plugin_data; | |
| 190 | - } | |
| 191 | - | |
| 192 | - // Process dependencies and return only necessary data | |
| 193 | - $new_dependency_list = []; | |
| 194 | - $new_required_plugins = []; | |
| 195 | - | |
| 196 | - // Platform-specific plugins | |
| 197 | - if ( $platform === 'elementor' ) { | |
| 198 | - $elementor_plugin = $this->find_plugin_by_name( $all_plugins, 'elementor/elementor' ); | |
| 199 | - $e_plugin = array( | |
| 200 | - 'plugin_file' => 'elementor/elementor.php', | |
| 201 | - 'plugin_original_slug' => 'elementor', | |
| 202 | - 'name' => $elementor_plugin ? $elementor_plugin['name'] : 'Elementor', | |
| 203 | - 'installed' => $elementor_plugin ? ( $elementor_plugin['status'] === 'active' ) : false, | |
| 204 | - 'mustHave' => true, | |
| 205 | - ); | |
| 206 | - $new_dependency_list[] = $e_plugin; | |
| 207 | - if ( ! $elementor_plugin || $elementor_plugin['status'] !== 'active' ) { | |
| 208 | - $new_required_plugins[] = $e_plugin; | |
| 209 | - } | |
| 210 | - } elseif ( $platform === 'gutenberg' ) { | |
| 211 | - // Check if WordPress supports Gutenberg natively or if Gutenberg plugin is needed | |
| 212 | - $gutenberg_plugin = $this->find_plugin_by_name( $all_plugins, 'gutenberg/gutenberg' ); | |
| 213 | - | |
| 214 | - // Note: templately.is_wp_support_gutenberg is a frontend variable, | |
| 215 | - // we'll assume Gutenberg plugin is needed if it exists and is inactive | |
| 216 | - if ( $gutenberg_plugin && $gutenberg_plugin['status'] === 'inactive' ) { | |
| 217 | - $g_plugin = array( | |
| 218 | - 'plugin_file' => 'gutenberg/gutenberg.php', | |
| 219 | - 'plugin_original_slug' => 'gutenberg', | |
| 220 | - 'name' => $gutenberg_plugin['name'], | |
| 221 | - 'mustHave' => true, | |
| 222 | - ); | |
| 223 | - $new_dependency_list[] = $g_plugin; | |
| 224 | - $new_required_plugins[] = $g_plugin; | |
| 225 | - } | |
| 226 | - } | |
| 227 | - | |
| 228 | - // Process template dependencies | |
| 229 | - if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { | |
| 230 | - foreach ( $dependencies as $plugin_file => $plugin_obj ) { | |
| 231 | - if ( ! is_array( $plugin_obj ) ) { | |
| 232 | - continue; | |
| 233 | - } | |
| 234 | - | |
| 235 | - $plugin_name = str_replace( '.php', '', $plugin_file ); | |
| 236 | - $plugin = $this->find_plugin_by_name( $all_plugins, $plugin_name ); | |
| 237 | - | |
| 238 | - if ( $plugin && $plugin['status'] === 'active' ) { | |
| 239 | - $plugin_obj['installed'] = true; | |
| 240 | - $new_dependency_list[] = $plugin_obj; | |
| 241 | - } else { | |
| 242 | - $new_dependency_list[] = $plugin_obj; | |
| 243 | - $new_required_plugins[] = $plugin_obj; | |
| 244 | - } | |
| 245 | - } | |
| 246 | - } | |
| 247 | - | |
| 248 | - if ( $this->should_offer_caching() ) { | |
| 249 | - $caching = Caching::dependency_entry(); | |
| 250 | - Caching::mark_offered(); | |
| 251 | - | |
| 252 | - $new_dependency_list[] = $caching; | |
| 253 | - $new_required_plugins[] = $caching; | |
| 254 | - } else { | |
| 255 | - // Category-based plugins | |
| 256 | - $included_categories = array( | |
| 257 | - 'blog-magazine', | |
| 258 | - 'construction', | |
| 259 | - 'ecommerce', | |
| 260 | - 'education', | |
| 261 | - 'entertainment', | |
| 262 | - 'fashion-lifestyle', | |
| 263 | - 'features-services', | |
| 264 | - 'food-restaurant', | |
| 265 | - 'marketing', | |
| 266 | - 'miscellaneous', | |
| 267 | - 'multipurpose', | |
| 268 | - 'nft-cryptocurrency', | |
| 269 | - 'non-profit', | |
| 270 | - 'technology', | |
| 271 | - 'travel', | |
| 272 | - ); | |
| 273 | - | |
| 274 | - $is_any_category_included = false; | |
| 275 | - if ( ! empty( $categories ) && is_array( $categories ) ) { | |
| 276 | - foreach ( $categories as $category ) { | |
| 277 | - if ( isset( $category['slug'] ) && in_array( $category['slug'], $included_categories, true ) ) { | |
| 278 | - $is_any_category_included = true; | |
| 279 | - break; | |
| 280 | - } | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | - if ( $is_any_category_included ) { | |
| 285 | - $nx_plugin = $this->find_plugin_by_name( $all_plugins, 'notificationx/notificationx' ); | |
| 286 | - $notificationx = array( | |
| 287 | - 'name' => 'NotificationX', | |
| 288 | - 'icon' => 'https://ps.w.org/notificationx/assets/icon-256x256.gif?rev=2783824', | |
| 289 | - 'plugin_file' => 'notificationx/notificationx.php', | |
| 290 | - 'plugin_original_slug' => 'notificationx', | |
| 291 | - 'is_pro' => false, | |
| 292 | - 'installed' => $nx_plugin ? ( $nx_plugin['status'] === 'active' ) : false, | |
| 293 | - 'link' => 'https://wordpress.org/plugins/notificationx/', | |
| 294 | - ); | |
| 295 | - $new_dependency_list[] = $notificationx; | |
| 296 | - $new_required_plugins[] = $notificationx; | |
| 297 | - } | |
| 298 | - | |
| 299 | - // EmbedPress for blog-magazine category | |
| 300 | - $ep_is_any_category_included = false; | |
| 301 | - if ( ! empty( $categories ) && is_array( $categories ) ) { | |
| 302 | - foreach ( $categories as $category ) { | |
| 303 | - if ( isset( $category['slug'] ) && $category['slug'] === 'blog-magazine' ) { | |
| 304 | - $ep_is_any_category_included = true; | |
| 305 | - break; | |
| 306 | - } | |
| 307 | - } | |
| 308 | - } | |
| 309 | - | |
| 310 | - if ( $ep_is_any_category_included ) { | |
| 311 | - $ep_plugin = $this->find_plugin_by_name( $all_plugins, 'embedpress/embedpress' ); | |
| 312 | - $embed_press = array( | |
| 313 | - 'name' => 'EmbedPress', | |
| 314 | - 'icon' => 'https://ps.w.org/embedpress/assets/icon-256x256.gif?rev=2783824', | |
| 315 | - 'plugin_file' => 'embedpress/embedpress.php', | |
| 316 | - 'plugin_original_slug' => 'embedpress', | |
| 317 | - 'is_pro' => false, | |
| 318 | - 'installed' => $ep_plugin ? ( $ep_plugin['status'] === 'active' ) : false, | |
| 319 | - 'link' => 'https://wordpress.org/plugins/embedpress/', | |
| 320 | - ); | |
| 321 | - $new_dependency_list[] = $embed_press; | |
| 322 | - $new_required_plugins[] = $embed_press; | |
| 323 | - } | |
| 324 | - } | |
| 325 | - | |
| 326 | - return new \WP_REST_Response( array( | |
| 327 | - 'dependencyList' => $new_dependency_list, | |
| 328 | - 'requiredPlugins' => $new_required_plugins | |
| 329 | - ) ); | |
| 330 | - } | |
| 331 | - | |
| 332 | - protected function should_offer_caching(): bool { | |
| 333 | - return Caching::should_offer(); | |
| 334 | - } | |
| 335 | - | |
| 336 | - /** | |
| 337 | - * Helper method to find a plugin by its name/slug | |
| 338 | - * | |
| 339 | - * @param array $plugins Array of all plugins | |
| 340 | - * @param string $plugin_name Plugin name to search for | |
| 341 | - * @return array|null Plugin data or null if not found | |
| 342 | - */ | |
| 343 | - private function find_plugin_by_name( $plugins, $plugin_name ) { | |
| 344 | - foreach ( $plugins as $plugin ) { | |
| 345 | - if ( $plugin['plugin'] === $plugin_name ) { | |
| 346 | - return $plugin; | |
| 347 | - } | |
| 348 | - } | |
| 349 | - return null; | |
| 350 | - } | |
| 351 | - | |
| 352 | - public function get_themes() { | |
| 353 | - // Get platform parameter from request | |
| 354 | - $platform = $this->get_param( 'platform', 'elementor' ); | |
| 355 | - | |
| 356 | - $active_themes = wp_get_themes(); | |
| 357 | - $current_theme = wp_get_theme(); | |
| 358 | - | |
| 359 | - $recommended_theme = array(); | |
| 360 | - | |
| 361 | - if ( $platform === 'elementor' ) { | |
| 362 | - // Look for Hello Elementor theme | |
| 363 | - $target_stylesheet = 'hello-elementor'; | |
| 364 | - $elementor_theme = null; | |
| 365 | - | |
| 366 | - foreach ( $active_themes as $theme ) { | |
| 367 | - if ( $theme->get_stylesheet() === $target_stylesheet ) { | |
| 368 | - $elementor_theme = $theme; | |
| 369 | - break; | |
| 370 | - } | |
| 371 | - } | |
| 372 | - | |
| 373 | - $recommended_theme = array( | |
| 374 | - 'name' => $elementor_theme ? $elementor_theme->display( 'Name' ) : 'Hello Elementor', | |
| 375 | - 'status' => $elementor_theme ? ( $elementor_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', | |
| 376 | - 'template' => $elementor_theme ? $elementor_theme->get_template() : 'hello-elementor', | |
| 377 | - 'stylesheet' => $elementor_theme ? $elementor_theme->get_stylesheet() : 'hello-elementor', | |
| 378 | - ); | |
| 379 | - | |
| 380 | - } elseif ( $platform === 'gutenberg' ) { | |
| 381 | - // Look for Twenty Twenty-Four theme | |
| 382 | - $target_stylesheet = 'twentytwentyfour'; | |
| 383 | - $gutenberg_theme = null; | |
| 384 | - | |
| 385 | - foreach ( $active_themes as $theme ) { | |
| 386 | - if ( $theme->get_stylesheet() === $target_stylesheet ) { | |
| 387 | - $gutenberg_theme = $theme; | |
| 388 | - break; | |
| 389 | - } | |
| 390 | - } | |
| 391 | - | |
| 392 | - $recommended_theme = array( | |
| 393 | - 'name' => $gutenberg_theme ? $gutenberg_theme->display( 'Name' ) : 'Twenty Twenty-Four', | |
| 394 | - 'status' => $gutenberg_theme ? ( $gutenberg_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', | |
| 395 | - 'template' => $gutenberg_theme ? $gutenberg_theme->get_template() : 'twentytwentyfour', | |
| 396 | - 'stylesheet' => $gutenberg_theme ? $gutenberg_theme->get_stylesheet() : 'twentytwentyfour', | |
| 397 | - ); | |
| 398 | - } | |
| 399 | - | |
| 400 | - return new \WP_REST_Response( $recommended_theme ); | |
| 401 | - } | |
| 402 | - | |
| 403 | - /** | |
| 404 | - * Get's the activation status for a plugin. | |
| 405 | - * | |
| 406 | - * @since 5.5.0 | |
| 407 | - * | |
| 408 | - * @param string $plugin The plugin file to check. | |
| 409 | - * @return string Either 'active' or 'inactive'. | |
| 410 | - */ | |
| 411 | - protected function get_plugin_status( $plugin ) { | |
| 412 | - if ( is_plugin_active_for_network( $plugin ) ) { | |
| 413 | - return 'active'; | |
| 414 | - } | |
| 415 | - | |
| 416 | - if ( is_plugin_active( $plugin ) ) { | |
| 417 | - return 'active'; | |
| 418 | - } | |
| 419 | - | |
| 420 | - return 'inactive'; | |
| 421 | - } | |
| 422 | - | |
| 423 | -} | |
| 133 | +} | |