| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\SingleImport\REST; |
| 4 |
|
| 5 |
use stdClass; |
| 6 |
use Templately\API\API; |
| 7 |
use Templately\Utils\Helper; |
| 8 |
use Templately\Utils\Database; |
| 9 |
use Templately\Utils\Installer; |
| 10 |
use WP_REST_Request; |
| 11 |
use Templately\Utils\Caching; |
| 12 |
|
| 13 |
use function current_user_can; |
| 14 |
|
| 15 |
|
| 16 |
class Dependencies extends API { |
| 17 |
|
| 18 |
private $endpoint = 'dependencies'; |
| 19 |
|
| 20 |
public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }'; |
| 21 |
|
| 22 |
/** |
| 23 |
* How old WordPress's cached update check may be before this endpoint refreshes it. |
| 24 |
* Long enough that the dependency step almost always answers from cache. |
| 25 |
*/ |
| 26 |
const UPDATE_CHECK_MAX_AGE = 12 * HOUR_IN_SECONDS; |
| 27 |
|
| 28 |
/** Guards against concurrent wizards each firing their own wordpress.org check. */ |
| 29 |
const UPDATE_CHECK_LOCK = 'plugin_update_check_lock'; |
| 30 |
|
| 31 |
public function __construct() { |
| 32 |
parent::__construct(); |
| 33 |
|
| 34 |
if(!empty($_GET['disable_redirect'])) { |
| 35 |
add_filter('wp_redirect', '__return_false', 999); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function permission_check( WP_REST_Request $request ) { |
| 40 |
$this->request = $request; |
| 41 |
|
| 42 |
// Installing reaches beyond template content, so it does not ride the |
| 43 |
// `delete_posts` base gate the read routes are happy with. Installer::install() |
| 44 |
// checks `install_plugins` / `activate_plugins` per plugin as well; this is the |
| 45 |
// front gate, so the route cannot be probed at all without the capability. |
| 46 |
if ( $request->get_route() === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) { |
| 47 |
return $this->error( |
| 48 |
'invalid_permission', |
| 49 |
__( 'Sorry, you do not have permission to install a plugin.', 'templately' ), |
| 50 |
'dependencies/install', |
| 51 |
rest_authorization_required_code() |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
public function register_routes() { |
| 59 |
$this->get( $this->endpoint, [ $this, 'get_dependencies' ] ); |
| 60 |
$this->post( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); |
| 61 |
$this->post( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); |
| 62 |
$this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 63 |
$this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 64 |
$this->post( $this->endpoint . '/update', [$this, 'update_dependencies'] ); |
| 65 |
} |
| 66 |
|
| 67 |
public function get_dependencies() { |
| 68 |
$dependencies = Database::get_transient( $this->endpoint ); |
| 69 |
|
| 70 |
if( $dependencies ) { |
| 71 |
return $this->success( $dependencies ); |
| 72 |
} |
| 73 |
|
| 74 |
$response = $this->http()->query( |
| 75 |
'dependencies', |
| 76 |
'id, name, icon, is_pro, platforms{ id, name, file_type, icon }' |
| 77 |
)->post(); |
| 78 |
|
| 79 |
if( ! is_wp_error( $response ) ) { |
| 80 |
$_dependencies = []; |
| 81 |
if( ! empty( $response ) ) { |
| 82 |
$_dependencies[ 'unknown' ] = []; |
| 83 |
foreach( $response as $dependency ) { |
| 84 |
if( ! empty( $dependency['platforms'] ) ) { |
| 85 |
foreach( $dependency['platforms'] as $platform ) { |
| 86 |
if( ! isset( $_dependencies[ $platform['name'] ] ) ) { |
| 87 |
$_dependencies[ $platform['name'] ] = []; |
| 88 |
} |
| 89 |
$_dependencies[ $platform['name'] ][] = $dependency; |
| 90 |
} |
| 91 |
} else { |
| 92 |
$_dependencies[ 'unknown' ][] = $dependency; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
Database::set_transient( $this->endpoint, $_dependencies ); |
| 98 |
return $_dependencies; |
| 99 |
} |
| 100 |
|
| 101 |
return $response; |
| 102 |
} |
| 103 |
|
| 104 |
public function check_dependencies(){ |
| 105 |
$dependencies = $this->get_param( 'dependencies', '', '' ); |
| 106 |
$platform = $this->get_param( 'platform', 'elementor' ); |
| 107 |
|
| 108 |
$_inactive_plugins = []; |
| 109 |
$_plugins = Helper::get_plugins(); |
| 110 |
|
| 111 |
if( $platform === 'elementor' ) { |
| 112 |
$elementor_plugin = new stdClass(); |
| 113 |
$elementor_plugin->name = __( 'Elementor', 'templately' ); |
| 114 |
$elementor_plugin->plugin_file = 'elementor/elementor.php'; |
| 115 |
$elementor_plugin->slug = 'elementor'; |
| 116 |
$elementor_plugin->is_pro = false; |
| 117 |
$elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' ); |
| 118 |
|
| 119 |
$_inactive_plugins[] = $elementor_plugin; |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { |
| 123 |
foreach ( $dependencies as $dependency ) { |
| 124 |
if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$dependency = ( object ) $dependency; |
| 129 |
if ( is_null( $dependency->plugin_file ) ) { |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
$dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file ); |
| 134 |
|
| 135 |
if( isset( $dependency->plugin_original_slug ) ) { |
| 136 |
$dependency->slug = $dependency->plugin_original_slug; |
| 137 |
unset( $dependency->plugin_original_slug ); |
| 138 |
} |
| 139 |
|
| 140 |
if ( $dependency->is_pro ) { |
| 141 |
if ( isset( $_plugins[ $dependency->plugin_file ] ) ) { |
| 142 |
unset( $dependency->is_pro ); |
| 143 |
$dependency->message = __( 'You have the plugin installed.', 'templately' ); |
| 144 |
} |
| 145 |
} |
| 146 |
$_inactive_plugins[] = $dependency; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Same version-drift reasoning as the full-site path: a template's markup comes from the |
| 151 |
// plugin version on the AUTHORING site, so an out-of-date dependency here produces blocks |
| 152 |
// the editor reports as invalid. This endpoint feeds single-import's dependency UI, which |
| 153 |
// reads `/check` and not `/plugins` — decorating only the latter left this surface blind. |
| 154 |
$pending = $this->get_pending_plugin_updates(); |
| 155 |
$decoratable = array_map( function ( $plugin ) { |
| 156 |
return is_object( $plugin ) ? (array) $plugin : $plugin; |
| 157 |
}, $_inactive_plugins ); |
| 158 |
|
| 159 |
$decorated = $this->decorate_with_update_state( $decoratable, $pending ); |
| 160 |
|
| 161 |
return [ |
| 162 |
'dependencies' => array_map( function ( $plugin ) { |
| 163 |
return (object) $plugin; |
| 164 |
}, $decorated ), |
| 165 |
'updates' => [ |
| 166 |
'available' => array_values( array_filter( $decorated, function ( $plugin ) { |
| 167 |
return ! empty( $plugin['update_available'] ); |
| 168 |
} ) ), |
| 169 |
'checked_at' => $this->get_update_check_timestamp(), |
| 170 |
], |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
public function install_dependencies(){ |
| 175 |
// Installing/activating a dependency runs foreign plugin code in THIS |
| 176 |
// request — the classic fatal source. Capture it instead of replying |
| 177 |
// with an unparseable blank 500. |
| 178 |
\Templately\Utils\Response\FatalGuard::arm( 'single-import/install-dependencies' ); |
| 179 |
|
| 180 |
$requirements = $this->get_param( 'requirement', [], '' ); |
| 181 |
if( empty( $requirements ) ) { |
| 182 |
return $this->error( |
| 183 |
'invalid_requirements', |
| 184 |
__('You have supplied an invalid requirements. Please reload the page and try again.'), |
| 185 |
'/install', |
| 186 |
400 |
| 187 |
); |
| 188 |
} |
| 189 |
$installed = Installer::get_instance()->install( $requirements ); |
| 190 |
if(empty($installed['success'])){ |
| 191 |
// `code` and `message` are NOT set on every failing branch of Installer::install() |
| 192 |
// — activation failing without a WP_Error leaves both unset, and reading them |
| 193 |
// raised "Undefined array key" warnings on a path whose whole job is to report a |
| 194 |
// failure cleanly. Defaults, so a failure never becomes a second failure. |
| 195 |
return $this->error( |
| 196 |
! empty( $installed['code'] ) ? $installed['code'] : 'install_failed', |
| 197 |
! empty( $installed['message'] ) ? $installed['message'] : __( 'The plugin could not be installed. Please try again, or install it from the Plugins screen.', 'templately' ), |
| 198 |
'dependencies/install', |
| 199 |
403 |
| 200 |
); |
| 201 |
} |
| 202 |
return $installed; |
| 203 |
} |
| 204 |
|
| 205 |
public function get_plugins() { |
| 206 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 207 |
|
| 208 |
// Get parameters from request |
| 209 |
$dependencies = $this->get_param( 'dependencies', [], null ); |
| 210 |
$platform = $this->get_param( 'platform', 'elementor' ); |
| 211 |
$categories = $this->get_param( 'categories', [], null ); |
| 212 |
|
| 213 |
$pending_updates = $this->get_pending_plugin_updates(); |
| 214 |
|
| 215 |
// Get all plugins for checking status |
| 216 |
$all_plugins = array(); |
| 217 |
foreach ( get_plugins() as $file => $data ) { |
| 218 |
$plugin_data = array( |
| 219 |
'plugin' => substr( $file, 0, - 4 ), |
| 220 |
'plugin_file' => $file, |
| 221 |
'status' => $this->get_plugin_status( $file ), |
| 222 |
'name' => $data['Name'], |
| 223 |
'plugin_uri' => $data['PluginURI'], |
| 224 |
'author' => $data['Author'], |
| 225 |
'author_uri' => $data['AuthorURI'], |
| 226 |
'description' => array( |
| 227 |
'raw' => $data['Description'], |
| 228 |
'rendered' => $data['Description'], |
| 229 |
), |
| 230 |
'version' => $data['Version'], |
| 231 |
'network_only' => $data['Network'], |
| 232 |
'requires_wp' => $data['RequiresWP'], |
| 233 |
'requires_php' => $data['RequiresPHP'], |
| 234 |
'textdomain' => $data['TextDomain'], |
| 235 |
); |
| 236 |
$all_plugins[] = $plugin_data; |
| 237 |
} |
| 238 |
|
| 239 |
// Process dependencies and return only necessary data |
| 240 |
$new_dependency_list = []; |
| 241 |
$new_required_plugins = []; |
| 242 |
|
| 243 |
// Platform-specific plugins |
| 244 |
if ( $platform === 'elementor' ) { |
| 245 |
$elementor_plugin = $this->find_plugin_by_name( $all_plugins, 'elementor/elementor' ); |
| 246 |
$e_plugin = array( |
| 247 |
'plugin_file' => 'elementor/elementor.php', |
| 248 |
'plugin_original_slug' => 'elementor', |
| 249 |
'name' => $elementor_plugin ? $elementor_plugin['name'] : 'Elementor', |
| 250 |
'installed' => $elementor_plugin ? ( $elementor_plugin['status'] === 'active' ) : false, |
| 251 |
'mustHave' => true, |
| 252 |
); |
| 253 |
$new_dependency_list[] = $e_plugin; |
| 254 |
if ( ! $elementor_plugin || $elementor_plugin['status'] !== 'active' ) { |
| 255 |
$new_required_plugins[] = $e_plugin; |
| 256 |
} |
| 257 |
} elseif ( $platform === 'gutenberg' ) { |
| 258 |
// Check if WordPress supports Gutenberg natively or if Gutenberg plugin is needed |
| 259 |
$gutenberg_plugin = $this->find_plugin_by_name( $all_plugins, 'gutenberg/gutenberg' ); |
| 260 |
|
| 261 |
// Note: templately.is_wp_support_gutenberg is a frontend variable, |
| 262 |
// we'll assume Gutenberg plugin is needed if it exists and is inactive |
| 263 |
if ( $gutenberg_plugin && $gutenberg_plugin['status'] === 'inactive' ) { |
| 264 |
$g_plugin = array( |
| 265 |
'plugin_file' => 'gutenberg/gutenberg.php', |
| 266 |
'plugin_original_slug' => 'gutenberg', |
| 267 |
'name' => $gutenberg_plugin['name'], |
| 268 |
'mustHave' => true, |
| 269 |
); |
| 270 |
$new_dependency_list[] = $g_plugin; |
| 271 |
$new_required_plugins[] = $g_plugin; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
// Process template dependencies |
| 276 |
if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { |
| 277 |
foreach ( $dependencies as $plugin_file => $plugin_obj ) { |
| 278 |
if ( ! is_array( $plugin_obj ) ) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
|
| 282 |
$plugin_name = str_replace( '.php', '', $plugin_file ); |
| 283 |
$plugin = $this->find_plugin_by_name( $all_plugins, $plugin_name ); |
| 284 |
|
| 285 |
if ( $plugin && $plugin['status'] === 'active' ) { |
| 286 |
$plugin_obj['installed'] = true; |
| 287 |
$new_dependency_list[] = $plugin_obj; |
| 288 |
} else { |
| 289 |
$new_dependency_list[] = $plugin_obj; |
| 290 |
$new_required_plugins[] = $plugin_obj; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// NotificationX used to be INJECTED here for any item in one of 15 categories |
| 296 |
// (essentially the whole catalog, `miscellaneous` and `multipurpose` included), and |
| 297 |
// pushed into the REQUIRED list — so imports installed it whether or not the template |
| 298 |
// contained a single NotificationX element. A dependency is something the template |
| 299 |
// needs to render; this was a recommendation wearing a requirement's clothes. |
| 300 |
// Removed 2026-09-21. Dependencies now come from the pack, which is the only thing |
| 301 |
// that knows what the template actually uses. |
| 302 |
// |
| 303 |
// The caching suggestion (3.7.5, merged from `dev`) arrived wrapped around that |
| 304 |
// injection as an either/or: offer the caching plugin, OR fall through to the category plugins. |
| 305 |
// The either/or is kept for what is left of that branch (EmbedPress); NotificationX |
| 306 |
// stays removed rather than returning through the merge. |
| 307 |
if ( $this->should_offer_caching() ) { |
| 308 |
$caching = Caching::dependency_entry(); |
| 309 |
Caching::mark_offered(); |
| 310 |
|
| 311 |
$new_dependency_list[] = $caching; |
| 312 |
$new_required_plugins[] = $caching; |
| 313 |
} else { |
| 314 |
// EmbedPress for blog-magazine category |
| 315 |
$ep_is_any_category_included = false; |
| 316 |
if ( ! empty( $categories ) && is_array( $categories ) ) { |
| 317 |
foreach ( $categories as $category ) { |
| 318 |
if ( isset( $category['slug'] ) && $category['slug'] === 'blog-magazine' ) { |
| 319 |
$ep_is_any_category_included = true; |
| 320 |
break; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if ( $ep_is_any_category_included ) { |
| 326 |
$ep_plugin = $this->find_plugin_by_name( $all_plugins, 'embedpress/embedpress' ); |
| 327 |
$embed_press = array( |
| 328 |
'name' => 'EmbedPress', |
| 329 |
'icon' => 'https://ps.w.org/embedpress/assets/icon-256x256.gif?rev=2783824', |
| 330 |
'plugin_file' => 'embedpress/embedpress.php', |
| 331 |
'plugin_original_slug' => 'embedpress', |
| 332 |
'is_pro' => false, |
| 333 |
'installed' => $ep_plugin ? ( $ep_plugin['status'] === 'active' ) : false, |
| 334 |
'link' => 'https://wordpress.org/plugins/embedpress/', |
| 335 |
); |
| 336 |
$new_dependency_list[] = $embed_press; |
| 337 |
$new_required_plugins[] = $embed_press; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
$new_dependency_list = $this->decorate_with_update_state( $new_dependency_list, $pending_updates ); |
| 342 |
$new_required_plugins = $this->decorate_with_update_state( $new_required_plugins, $pending_updates ); |
| 343 |
|
| 344 |
// Only dependencies of THIS import are offered for update — the site's other outdated |
| 345 |
// plugins are not this step's business and listing them turns a targeted, explainable |
| 346 |
// recommendation into a nag screen. |
| 347 |
$updatable = array_values( array_filter( $new_dependency_list, function ( $plugin ) { |
| 348 |
return ! empty( $plugin['update_available'] ); |
| 349 |
} ) ); |
| 350 |
|
| 351 |
return new \WP_REST_Response( array( |
| 352 |
'dependencyList' => $new_dependency_list, |
| 353 |
'requiredPlugins' => $new_required_plugins, |
| 354 |
'updates' => array( |
| 355 |
'available' => $updatable, |
| 356 |
'checked_at' => $this->get_update_check_timestamp(), |
| 357 |
), |
| 358 |
) ); |
| 359 |
} |
| 360 |
|
| 361 |
protected function should_offer_caching(): bool { |
| 362 |
return Caching::should_offer(); |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Plugin updates WordPress already knows about, keyed by plugin file. |
| 367 |
* |
| 368 |
* Reads the `update_plugins` transient rather than forcing a wordpress.org round trip: |
| 369 |
* this runs inside the import wizard's dependency step, and a cold remote check there is |
| 370 |
* several seconds of the user staring at a spinner. A refresh is triggered only when the |
| 371 |
* cached answer is older than {@see self::UPDATE_CHECK_MAX_AGE}, and even then it is |
| 372 |
* lock-guarded so a re-opened wizard cannot stack requests. |
| 373 |
* |
| 374 |
* @return array<string,object> plugin_file => update object (`new_version`, `package`, …) |
| 375 |
*/ |
| 376 |
private function get_pending_plugin_updates(): array { |
| 377 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 378 |
|
| 379 |
$transient = get_site_transient( 'update_plugins' ); |
| 380 |
$is_stale = empty( $transient ) || empty( $transient->last_checked ) |
| 381 |
|| ( time() - (int) $transient->last_checked ) > self::UPDATE_CHECK_MAX_AGE; |
| 382 |
|
| 383 |
if ( $is_stale && ! Database::get_transient( self::UPDATE_CHECK_LOCK ) ) { |
| 384 |
// Short lock, not the full max-age: if the refresh fails we want to retry sooner |
| 385 |
// than a whole check window, but never from two wizards at once. |
| 386 |
Database::set_transient( self::UPDATE_CHECK_LOCK, time(), 5 * MINUTE_IN_SECONDS ); |
| 387 |
wp_update_plugins(); |
| 388 |
$transient = get_site_transient( 'update_plugins' ); |
| 389 |
} |
| 390 |
|
| 391 |
return ( ! empty( $transient->response ) && is_array( $transient->response ) ) ? $transient->response : []; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* When WordPress last checked for plugin updates, so the UI can say how fresh this is. |
| 396 |
*/ |
| 397 |
private function get_update_check_timestamp(): int { |
| 398 |
$transient = get_site_transient( 'update_plugins' ); |
| 399 |
|
| 400 |
return ! empty( $transient->last_checked ) ? (int) $transient->last_checked : 0; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Annotate dependency rows with what is installed and what is available. |
| 405 |
* |
| 406 |
* A pack's markup is generated by the block plugin version on the AUTHORING site. When the |
| 407 |
* importing site is behind, blocks whose `save()` output has changed since — or that use |
| 408 |
* attributes this version does not declare — fail Gutenberg's validation and open with |
| 409 |
* "Attempt recovery". Surfacing the gap here is the only point in the flow where it is |
| 410 |
* still cheap to fix, so the data has to reach the step even when the user declines. |
| 411 |
* |
| 412 |
* Pro plugins are marked `auto_updatable: false`: they update through their own licensed |
| 413 |
* channel, and offering a one-click update that cannot work is worse than a link. |
| 414 |
* |
| 415 |
* @param array $plugins Dependency rows. |
| 416 |
* @param array<string,object> $pending plugin_file => update object. |
| 417 |
* @return array |
| 418 |
*/ |
| 419 |
private function decorate_with_update_state( array $plugins, array $pending ): array { |
| 420 |
$installed = function_exists( 'get_plugins' ) ? get_plugins() : []; |
| 421 |
|
| 422 |
foreach ( $plugins as $index => $plugin ) { |
| 423 |
$file = $plugin['plugin_file'] ?? null; |
| 424 |
|
| 425 |
$plugins[ $index ]['version'] = null; |
| 426 |
$plugins[ $index ]['new_version'] = null; |
| 427 |
$plugins[ $index ]['update_available'] = false; |
| 428 |
$plugins[ $index ]['auto_updatable'] = false; |
| 429 |
|
| 430 |
if ( empty( $file ) || ! isset( $installed[ $file ] ) ) { |
| 431 |
continue; |
| 432 |
} |
| 433 |
|
| 434 |
$plugins[ $index ]['version'] = $installed[ $file ]['Version'] ?? null; |
| 435 |
|
| 436 |
if ( empty( $pending[ $file ]->new_version ) ) { |
| 437 |
continue; |
| 438 |
} |
| 439 |
|
| 440 |
// The transient is a CACHE, and it is not always invalidated when a plugin is |
| 441 |
// updated — an entry can outlive the update it described, naming a version the |
| 442 |
// site already runs. Trusting it blindly is what put already-updated plugins in |
| 443 |
// "Updates Recommended". Only a version we do not yet have is an update. |
| 444 |
$current = $plugins[ $index ]['version']; |
| 445 |
if ( ! empty( $current ) && version_compare( $current, $pending[ $file ]->new_version, '>=' ) ) { |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
$plugins[ $index ]['new_version'] = $pending[ $file ]->new_version; |
| 450 |
$plugins[ $index ]['update_available'] = true; |
| 451 |
$plugins[ $index ]['auto_updatable'] = empty( $plugin['is_pro'] ) && ! empty( $pending[ $file ]->package ); |
| 452 |
} |
| 453 |
|
| 454 |
return $plugins; |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Update installed dependency plugins. |
| 459 |
* |
| 460 |
* Scoped deliberately: only plugins this site actually has an update for may be named, so |
| 461 |
* the route cannot be used to drive arbitrary upgrades. |
| 462 |
*/ |
| 463 |
public function update_dependencies() { |
| 464 |
// Upgrading runs foreign plugin code in THIS request — capture a fatal as an envelope |
| 465 |
// instead of an unparseable blank 500, same as install_dependencies(). |
| 466 |
\Templately\Utils\Response\FatalGuard::arm( 'single-import/update-dependencies' ); |
| 467 |
|
| 468 |
$plugin_files = $this->get_param( 'plugin_files', [], 'sanitize_text_field' ); |
| 469 |
|
| 470 |
if ( empty( $plugin_files ) || ! is_array( $plugin_files ) ) { |
| 471 |
return $this->error( |
| 472 |
'invalid_requirements', |
| 473 |
__( 'No plugins were selected for update.', 'templately' ), |
| 474 |
'dependencies/update', |
| 475 |
400 |
| 476 |
); |
| 477 |
} |
| 478 |
|
| 479 |
$pending = $this->get_pending_plugin_updates(); |
| 480 |
$eligible = array_values( array_intersect( $plugin_files, array_keys( $pending ) ) ); |
| 481 |
|
| 482 |
// A selected plugin with no pending update is ALREADY UP TO DATE, which is the |
| 483 |
// outcome this step wants — not a failure. It happens routinely: the user updated |
| 484 |
// from the Plugins screen in another tab, an auto-update landed, or a previous |
| 485 |
// attempt here succeeded and the caller is working from the list it fetched before. |
| 486 |
// Reporting it as an error painted "None of the selected plugins have an update |
| 487 |
// available" under every row of a fully up-to-date site. |
| 488 |
$already = array_values( array_diff( $plugin_files, $eligible ) ); |
| 489 |
$settled = array_map( function ( $plugin_file ) { |
| 490 |
return [ |
| 491 |
'plugin_file' => $plugin_file, |
| 492 |
'updated' => true, |
| 493 |
'code' => 'already_updated', |
| 494 |
]; |
| 495 |
}, $already ); |
| 496 |
|
| 497 |
if ( empty( $eligible ) ) { |
| 498 |
return [ |
| 499 |
'success' => true, |
| 500 |
'results' => $settled, |
| 501 |
]; |
| 502 |
} |
| 503 |
|
| 504 |
$updated = Installer::get_instance()->update( $eligible ); |
| 505 |
|
| 506 |
if ( empty( $updated['success'] ) && empty( $updated['results'] ) ) { |
| 507 |
return $this->error( |
| 508 |
$updated['code'] ?? 'update_failed', |
| 509 |
$updated['message'] ?? __( 'The plugins could not be updated.', 'templately' ), |
| 510 |
'dependencies/update', |
| 511 |
403 |
| 512 |
); |
| 513 |
} |
| 514 |
|
| 515 |
// `success: false` here means SOME plugin failed; the per-plugin results say which, and |
| 516 |
// the caller decides whether that is worth stopping for. The updated code is not loaded |
| 517 |
// in this request, so callers must re-check dependencies in a fresh one. |
| 518 |
$updated['results'] = array_merge( $updated['results'] ?? [], $settled ); |
| 519 |
|
| 520 |
return $updated; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Helper method to find a plugin by its name/slug |
| 525 |
* |
| 526 |
* @param array $plugins Array of all plugins |
| 527 |
* @param string $plugin_name Plugin name to search for |
| 528 |
* @return array|null Plugin data or null if not found |
| 529 |
*/ |
| 530 |
private function find_plugin_by_name( $plugins, $plugin_name ) { |
| 531 |
foreach ( $plugins as $plugin ) { |
| 532 |
if ( $plugin['plugin'] === $plugin_name ) { |
| 533 |
return $plugin; |
| 534 |
} |
| 535 |
} |
| 536 |
return null; |
| 537 |
} |
| 538 |
|
| 539 |
public function get_themes() { |
| 540 |
// Get platform parameter from request |
| 541 |
$platform = $this->get_param( 'platform', 'elementor' ); |
| 542 |
|
| 543 |
$active_themes = wp_get_themes(); |
| 544 |
$current_theme = wp_get_theme(); |
| 545 |
|
| 546 |
$recommended_theme = array(); |
| 547 |
|
| 548 |
if ( $platform === 'elementor' ) { |
| 549 |
// Look for Hello Elementor theme |
| 550 |
$target_stylesheet = 'hello-elementor'; |
| 551 |
$elementor_theme = null; |
| 552 |
|
| 553 |
foreach ( $active_themes as $theme ) { |
| 554 |
if ( $theme->get_stylesheet() === $target_stylesheet ) { |
| 555 |
$elementor_theme = $theme; |
| 556 |
break; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
$recommended_theme = array( |
| 561 |
'name' => $elementor_theme ? $elementor_theme->display( 'Name' ) : 'Hello Elementor', |
| 562 |
'status' => $elementor_theme ? ( $elementor_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', |
| 563 |
'template' => $elementor_theme ? $elementor_theme->get_template() : 'hello-elementor', |
| 564 |
'stylesheet' => $elementor_theme ? $elementor_theme->get_stylesheet() : 'hello-elementor', |
| 565 |
); |
| 566 |
|
| 567 |
} elseif ( $platform === 'gutenberg' ) { |
| 568 |
// Look for Twenty Twenty-Four theme |
| 569 |
$target_stylesheet = 'twentytwentyfour'; |
| 570 |
$gutenberg_theme = null; |
| 571 |
|
| 572 |
foreach ( $active_themes as $theme ) { |
| 573 |
if ( $theme->get_stylesheet() === $target_stylesheet ) { |
| 574 |
$gutenberg_theme = $theme; |
| 575 |
break; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
$recommended_theme = array( |
| 580 |
'name' => $gutenberg_theme ? $gutenberg_theme->display( 'Name' ) : 'Twenty Twenty-Four', |
| 581 |
'status' => $gutenberg_theme ? ( $gutenberg_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive', |
| 582 |
'template' => $gutenberg_theme ? $gutenberg_theme->get_template() : 'twentytwentyfour', |
| 583 |
'stylesheet' => $gutenberg_theme ? $gutenberg_theme->get_stylesheet() : 'twentytwentyfour', |
| 584 |
); |
| 585 |
} |
| 586 |
|
| 587 |
return new \WP_REST_Response( $recommended_theme ); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Get's the activation status for a plugin. |
| 592 |
* |
| 593 |
* @since 5.5.0 |
| 594 |
* |
| 595 |
* @param string $plugin The plugin file to check. |
| 596 |
* @return string Either 'active' or 'inactive'. |
| 597 |
*/ |
| 598 |
protected function get_plugin_status( $plugin ) { |
| 599 |
if ( is_plugin_active_for_network( $plugin ) ) { |
| 600 |
return 'active'; |
| 601 |
} |
| 602 |
|
| 603 |
if ( is_plugin_active( $plugin ) ) { |
| 604 |
return 'active'; |
| 605 |
} |
| 606 |
|
| 607 |
return 'inactive'; |
| 608 |
} |
| 609 |
|
| 610 |
} |
| 611 |
|