| 1 |
<?php |
| 2 |
namespace Elementor\Core\Admin; |
| 3 |
|
| 4 |
use Elementor\Api; |
| 5 |
use Elementor\Core\Base\Module; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* TODO: Move this class to pro version for better architecture. |
| 13 |
*/ |
| 14 |
class Canary_Deployment extends Module { |
| 15 |
|
| 16 |
const CURRENT_VERSION = ELEMENTOR_VERSION; |
| 17 |
const PLUGIN_BASE = ELEMENTOR_PLUGIN_BASE; |
| 18 |
|
| 19 |
private $canary_deployment_info = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get module name. |
| 23 |
* |
| 24 |
* Retrieve the module name. |
| 25 |
* |
| 26 |
* @since 2.6.0 |
| 27 |
* @access public |
| 28 |
* |
| 29 |
* @return string Module name. |
| 30 |
*/ |
| 31 |
public function get_name() { |
| 32 |
return 'canary-deployment'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Check version. |
| 37 |
* |
| 38 |
* @since 2.6.0 |
| 39 |
* @access public |
| 40 |
* |
| 41 |
* @param object $transient Plugin updates data. |
| 42 |
* |
| 43 |
* @return object Plugin updates data. |
| 44 |
*/ |
| 45 |
public function check_version( $transient ) { |
| 46 |
// First transient before the real check. |
| 47 |
if ( ! isset( $transient->response ) ) { |
| 48 |
return $transient; |
| 49 |
} |
| 50 |
|
| 51 |
// Placeholder |
| 52 |
$stable_version = '0.0.0'; |
| 53 |
|
| 54 |
if ( ! empty( $transient->response[ static::PLUGIN_BASE ]->new_version ) ) { |
| 55 |
$stable_version = $transient->response[ static::PLUGIN_BASE ]->new_version; |
| 56 |
} |
| 57 |
|
| 58 |
if ( null === $this->canary_deployment_info ) { |
| 59 |
$this->canary_deployment_info = $this->get_canary_deployment_info(); |
| 60 |
} |
| 61 |
|
| 62 |
// Can be false - if canary version is not available. |
| 63 |
if ( empty( $this->canary_deployment_info ) ) { |
| 64 |
return $transient; |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! version_compare( $this->canary_deployment_info['new_version'], $stable_version, '>' ) ) { |
| 68 |
return $transient; |
| 69 |
} |
| 70 |
|
| 71 |
$canary_deployment_info = $this->canary_deployment_info; |
| 72 |
|
| 73 |
// Most of plugin info comes from the $transient but on first check - the response is empty. |
| 74 |
if ( ! empty( $transient->response[ static::PLUGIN_BASE ] ) ) { |
| 75 |
$canary_deployment_info = array_merge( (array) $transient->response[ static::PLUGIN_BASE ], $canary_deployment_info ); |
| 76 |
} |
| 77 |
|
| 78 |
$transient->response[ static::PLUGIN_BASE ] = (object) $canary_deployment_info; |
| 79 |
|
| 80 |
return $transient; |
| 81 |
} |
| 82 |
|
| 83 |
protected function get_canary_deployment_remote_info( $force ) { |
| 84 |
return Api::get_canary_deployment_info( $force ); |
| 85 |
} |
| 86 |
|
| 87 |
private function get_canary_deployment_info() { |
| 88 |
global $pagenow; |
| 89 |
|
| 90 |
$force = 'update-core.php' === $pagenow && isset( $_GET['force-check'] ); |
| 91 |
|
| 92 |
$canary_deployment = $this->get_canary_deployment_remote_info( $force ); |
| 93 |
|
| 94 |
if ( empty( $canary_deployment['plugin_info']['new_version'] ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$canary_version = $canary_deployment['plugin_info']['new_version']; |
| 99 |
|
| 100 |
if ( version_compare( $canary_version, static::CURRENT_VERSION, '<=' ) ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! empty( $canary_deployment['conditions'] ) && ! $this->check_conditions( $canary_deployment['conditions'] ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
return $canary_deployment['plugin_info']; |
| 109 |
} |
| 110 |
|
| 111 |
private function check_conditions( $groups ) { |
| 112 |
foreach ( $groups as $group ) { |
| 113 |
if ( $this->check_group( $group ) ) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
private function check_group( $group ) { |
| 122 |
$is_or_relation = ! empty( $group['relation'] ) && 'OR' === $group['relation']; |
| 123 |
unset( $group['relation'] ); |
| 124 |
$result = false; |
| 125 |
|
| 126 |
foreach ( $group as $condition ) { |
| 127 |
// Reset results for each condition. |
| 128 |
$result = false; |
| 129 |
switch ( $condition['type'] ) { |
| 130 |
case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText |
| 131 |
// include an unmodified $wp_version |
| 132 |
include ABSPATH . WPINC . '/version.php'; |
| 133 |
$result = version_compare( $wp_version, $condition['version'], $condition['operator'] ); |
| 134 |
break; |
| 135 |
case 'multisite': |
| 136 |
$result = is_multisite() === $condition['multisite']; |
| 137 |
break; |
| 138 |
case 'language': |
| 139 |
$in_array = in_array( get_locale(), $condition['languages'], true ); |
| 140 |
$result = 'in' === $condition['operator'] ? $in_array : ! $in_array; |
| 141 |
break; |
| 142 |
case 'plugin': |
| 143 |
if ( ! empty( $condition['plugin_file'] ) ) { |
| 144 |
$plugin_file = $condition['plugin_file']; // For PHP Unit tests. |
| 145 |
} else { |
| 146 |
$plugin_file = WP_PLUGIN_DIR . '/' . $condition['plugin']; // Default. |
| 147 |
} |
| 148 |
|
| 149 |
$version = ''; |
| 150 |
|
| 151 |
if ( is_plugin_active( $condition['plugin'] ) && file_exists( $plugin_file ) ) { |
| 152 |
$plugin_data = get_plugin_data( $plugin_file ); |
| 153 |
if ( isset( $plugin_data['Version'] ) ) { |
| 154 |
$version = $plugin_data['Version']; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
$result = version_compare( $version, $condition['version'], $condition['operator'] ); |
| 159 |
break; |
| 160 |
case 'theme': |
| 161 |
$theme = wp_get_theme(); |
| 162 |
if ( wp_get_theme()->parent() ) { |
| 163 |
$theme = wp_get_theme()->parent(); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $theme->get_template() === $condition['theme'] ) { |
| 167 |
$version = $theme->version; |
| 168 |
} else { |
| 169 |
$version = ''; |
| 170 |
} |
| 171 |
|
| 172 |
$result = version_compare( $version, $condition['version'], $condition['operator'] ); |
| 173 |
break; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
if ( ( $is_or_relation && $result ) || ( ! $is_or_relation && ! $result ) ) { |
| 178 |
return $result; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $result; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @since 2.6.0 |
| 187 |
* @access public |
| 188 |
*/ |
| 189 |
public function __construct() { |
| 190 |
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'check_version' ] ); |
| 191 |
} |
| 192 |
} |
| 193 |
|