| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use stdClass; |
| 6 |
use Templately\Utils\Helper; |
| 7 |
use Templately\Utils\Database; |
| 8 |
use Templately\Utils\Installer; |
| 9 |
use WP_REST_Request; |
| 10 |
|
| 11 |
use function current_user_can; |
| 12 |
|
| 13 |
|
| 14 |
class Dependencies extends API { |
| 15 |
|
| 16 |
private $endpoint = 'dependencies'; |
| 17 |
public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }'; |
| 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 |
|
| 27 |
public function permission_check( WP_REST_Request $request ) { |
| 28 |
$this->request = $request; |
| 29 |
// $_route = $request->get_route(); |
| 30 |
|
| 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 |
// } |
| 34 |
|
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
public function register_routes() { |
| 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' ] ); |
| 42 |
$this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 43 |
$this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 44 |
} |
| 45 |
|
| 46 |
public function get_dependencies() { |
| 47 |
$dependencies = Database::get_transient( $this->endpoint ); |
| 48 |
|
| 49 |
if( $dependencies ) { |
| 50 |
return $this->success( $dependencies ); |
| 51 |
} |
| 52 |
|
| 53 |
$response = $this->http()->query( |
| 54 |
'dependencies', |
| 55 |
'id, name, icon, is_pro, platforms{ id, name, file_type, icon }' |
| 56 |
)->post(); |
| 57 |
|
| 58 |
if( ! is_wp_error( $response ) ) { |
| 59 |
$_dependencies = []; |
| 60 |
if( ! empty( $response ) ) { |
| 61 |
$_dependencies[ 'unknown' ] = []; |
| 62 |
foreach( $response as $dependency ) { |
| 63 |
if( ! empty( $dependency['platforms'] ) ) { |
| 64 |
foreach( $dependency['platforms'] as $platform ) { |
| 65 |
if( ! isset( $_dependencies[ $platform['name'] ] ) ) { |
| 66 |
$_dependencies[ $platform['name'] ] = []; |
| 67 |
} |
| 68 |
$_dependencies[ $platform['name'] ][] = $dependency; |
| 69 |
} |
| 70 |
} else { |
| 71 |
$_dependencies[ 'unknown' ][] = $dependency; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
Database::set_transient( $this->endpoint, $_dependencies ); |
| 77 |
return $_dependencies; |
| 78 |
} |
| 79 |
|
| 80 |
return $response; |
| 81 |
} |
| 82 |
|
| 83 |
public function check_dependencies(){ |
| 84 |
$dependencies = $this->get_param( 'dependencies', '', '' ); |
| 85 |
$platform = $this->get_param( 'platform', 'elementor' ); |
| 86 |
|
| 87 |
$_inactive_plugins = []; |
| 88 |
$_plugins = Helper::get_plugins(); |
| 89 |
|
| 90 |
if( $platform === 'elementor' ) { |
| 91 |
$elementor_plugin = new stdClass(); |
| 92 |
$elementor_plugin->name = __( 'Elementor', 'templately' ); |
| 93 |
$elementor_plugin->plugin_file = 'elementor/elementor.php'; |
| 94 |
$elementor_plugin->slug = 'elementor'; |
| 95 |
$elementor_plugin->is_pro = false; |
| 96 |
$elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' ); |
| 97 |
|
| 98 |
$_inactive_plugins[] = $elementor_plugin; |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { |
| 102 |
foreach ( $dependencies as $dependency ) { |
| 103 |
if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$dependency = ( object ) $dependency; |
| 108 |
if ( is_null( $dependency->plugin_file ) ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file ); |
| 113 |
|
| 114 |
if( isset( $dependency->plugin_original_slug ) ) { |
| 115 |
$dependency->slug = $dependency->plugin_original_slug; |
| 116 |
unset( $dependency->plugin_original_slug ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( $dependency->is_pro ) { |
| 120 |
if ( isset( $_plugins[ $dependency->plugin_file ] ) ) { |
| 121 |
unset( $dependency->is_pro ); |
| 122 |
$dependency->message = __( 'You have the plugin installed.', 'templately' ); |
| 123 |
} |
| 124 |
} |
| 125 |
$_inactive_plugins[] = $dependency; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'dependencies' => $_inactive_plugins |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
public function install_dependencies(){ |
| 135 |
$requirements = $this->get_param( 'requirement', [], '' ); |
| 136 |
if( empty( $requirements ) ) { |
| 137 |
return $this->error( |
| 138 |
'invalid_requirements', |
| 139 |
__('You have supplied an invalid requirements. Please reload the page and try again.'), |
| 140 |
'/install', |
| 141 |
400 |
| 142 |
); |
| 143 |
} |
| 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; |
| 149 |
} |
| 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 |
} |
| 402 |
|