| 1 |
<?php |
| 2 |
/** |
| 3 |
* The compatibilities model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
use ThemeisleSDK\Common\Abstract_Module; |
| 15 |
use ThemeisleSDK\Product; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Promotions module for ThemeIsle SDK. |
| 24 |
*/ |
| 25 |
class Compatibilities extends Abstract_Module { |
| 26 |
const REQUIRED = 'required'; |
| 27 |
const TESTED_UP = 'tested_up'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Should we load this module. |
| 31 |
* |
| 32 |
* @param Product $product Product object. |
| 33 |
* |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
public function can_load( $product ) { |
| 37 |
if ( $this->is_from_partner( $product ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
if ( $product->is_theme() && ! current_user_can( 'switch_themes' ) ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
if ( $product->is_plugin() && ! current_user_can( 'install_plugins' ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Registers the hooks. |
| 53 |
* |
| 54 |
* @param Product $product Product to load. |
| 55 |
* |
| 56 |
* @throws \Exception If the configuration is invalid. |
| 57 |
* |
| 58 |
* @return Compatibilities Module instance. |
| 59 |
*/ |
| 60 |
public function load( $product ) { |
| 61 |
|
| 62 |
|
| 63 |
$this->product = $product; |
| 64 |
|
| 65 |
$compatibilities = apply_filters( 'themeisle_sdk_compatibilities/' . $this->product->get_slug(), [] ); |
| 66 |
if ( empty( $compatibilities ) ) { |
| 67 |
return $this; |
| 68 |
} |
| 69 |
$requirement = null; |
| 70 |
$check_type = null; |
| 71 |
foreach ( $compatibilities as $compatibility ) { |
| 72 |
|
| 73 |
if ( empty( $compatibility['basefile'] ) ) { |
| 74 |
return $this; |
| 75 |
} |
| 76 |
$requirement = new Product( $compatibility['basefile'] ); |
| 77 |
$tested_up = isset( $compatibility[ self::TESTED_UP ] ) ? $compatibility[ self::TESTED_UP ] : '999'; |
| 78 |
$required = $compatibility[ self::REQUIRED ]; |
| 79 |
if ( ! version_compare( $required, $tested_up, '<' ) ) { |
| 80 |
throw new \Exception( sprintf( 'Invalid required/tested_up configuration. Required version %s should be lower than tested_up %s.', $required, $tested_up ) ); |
| 81 |
} |
| 82 |
$check_type = self::REQUIRED; |
| 83 |
if ( ! version_compare( $requirement->get_version(), $required, '<' ) ) { |
| 84 |
$check_type = self::TESTED_UP; |
| 85 |
if ( version_compare( $requirement->get_version(), $tested_up . '.9999', '<' ) ) { |
| 86 |
return $this; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
break; |
| 91 |
} |
| 92 |
if ( empty( $requirement ) ) { |
| 93 |
return $this; |
| 94 |
} |
| 95 |
if ( $check_type === self::REQUIRED ) { |
| 96 |
$this->mark_required( $product, $requirement ); |
| 97 |
} |
| 98 |
if ( $check_type === self::TESTED_UP ) { |
| 99 |
$this->mark_testedup( $product, $requirement ); |
| 100 |
} |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Mark the product tested up. |
| 107 |
* |
| 108 |
* @param Product $product Product object. |
| 109 |
* @param Product $requirement Requirement object. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function mark_testedup( $product, $requirement ) { |
| 114 |
add_action( |
| 115 |
'admin_head', |
| 116 |
function () use ( $product, $requirement ) { |
| 117 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : ''; |
| 118 |
|
| 119 |
if ( empty( $screen ) || ! isset( $screen->id ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
if ( $requirement->is_theme() && $screen->id === 'themes' ) { |
| 123 |
?> |
| 124 |
<script type="text/javascript"> |
| 125 |
jQuery(document).ready(function ($) { |
| 126 |
setInterval(checkTheme, 500); |
| 127 |
function checkTheme() { |
| 128 |
var theme = jQuery( '.theme.active[data-slug="<?php echo esc_attr( $requirement->get_slug() ); ?>"]' ); |
| 129 |
var notice_id = 'testedup<?php echo esc_attr( $requirement->get_slug() . $product->get_slug() ); ?>'; |
| 130 |
var product_name = '<?php echo esc_attr( $product->get_friendly_name() ); ?>'; |
| 131 |
if (theme.length > 0 && jQuery('#' + notice_id).length === 0) { |
| 132 |
theme.find('.theme-id-container').prepend('<div style="bottom:100%;top:auto;" id="'+notice_id+'" class="notice notice-warning"><strong>Warning:</strong> This theme has not been tested with your current version of <strong>' + product_name +'</strong>. Please update '+product_name+' plugin.</div>'); |
| 133 |
} |
| 134 |
if (theme.length > 0 && jQuery('#' + notice_id + 'overlay').length === 0) { |
| 135 |
jQuery('.theme-overlay.active .theme-author').after('<div style="bottom:100%;top:auto;" id="'+notice_id+'overlay" class="notice notice-warning"><p><strong>Warning:</strong> This theme has not been tested with your current version of <strong>' + product_name +'</strong>. Please update '+product_name+' plugin.</p></div>'); |
| 136 |
} |
| 137 |
} |
| 138 |
}) |
| 139 |
|
| 140 |
</script> |
| 141 |
<?php |
| 142 |
} |
| 143 |
if ( $requirement->is_plugin() && $screen->id === 'plugins' ) { |
| 144 |
?> |
| 145 |
<script type="text/javascript"> |
| 146 |
jQuery(document).ready(function ($) { |
| 147 |
setInterval(checkPlugin, 500); |
| 148 |
function checkPlugin() { |
| 149 |
var plugin = jQuery( '.plugins .active[data-slug="<?php echo esc_attr( $requirement->get_slug() ); ?>"]' ); |
| 150 |
var notice_id = 'testedup<?php echo esc_attr( $requirement->get_slug() . $product->get_slug() ); ?>'; |
| 151 |
var product_name = '<?php echo esc_attr( $product->get_friendly_name() ); ?>'; |
| 152 |
var product_type = '<?php echo ( $product->is_plugin() ? 'plugin' : 'theme' ); ?>'; |
| 153 |
if (plugin.length > 0 && jQuery('#' + notice_id).length === 0) { |
| 154 |
plugin.find('.column-description').append('<div style="bottom:100%;top:auto;" id="'+notice_id+'" class="notice notice-warning notice-alt notice-inline"><strong>Warning:</strong> This plugin has not been tested with your current version of <strong>' + product_name +'</strong>. Please update '+product_name+' '+product_type+'.</div>'); |
| 155 |
} |
| 156 |
} |
| 157 |
}) |
| 158 |
|
| 159 |
</script> |
| 160 |
<?php |
| 161 |
} |
| 162 |
} |
| 163 |
); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Mark the product requirements. |
| 169 |
* |
| 170 |
* @param Product $product Product object. |
| 171 |
* @param Product $requirement Requirement object. |
| 172 |
* |
| 173 |
* @return void |
| 174 |
*/ |
| 175 |
public function mark_required( $product, $requirement ) { |
| 176 |
add_filter( |
| 177 |
'upgrader_pre_download', |
| 178 |
function ( $return, $package, $upgrader ) use ( $product, $requirement ) { |
| 179 |
/** |
| 180 |
* Upgrader object. |
| 181 |
* |
| 182 |
* @var \WP_Upgrader $upgrader Upgrader object. |
| 183 |
*/ |
| 184 |
$should_block = false; |
| 185 |
if ( $product->is_theme() |
| 186 |
&& property_exists( $upgrader, 'skin' ) |
| 187 |
&& property_exists( $upgrader->skin, 'theme_info' ) |
| 188 |
&& $upgrader->skin->theme_info->template === $product->get_slug() ) { |
| 189 |
$should_block = true; |
| 190 |
|
| 191 |
} |
| 192 |
if ( ! $should_block && $product->is_plugin() |
| 193 |
&& property_exists( $upgrader, 'skin' ) |
| 194 |
&& property_exists( $upgrader->skin, 'plugin_info' ) |
| 195 |
&& $upgrader->skin->plugin_info['Name'] === $product->get_name() ) { |
| 196 |
$should_block = true; |
| 197 |
} |
| 198 |
if ( $should_block ) { |
| 199 |
echo( sprintf( |
| 200 |
'%s update requires a newer version of %s. Please %supdate%s %s %s.', |
| 201 |
esc_attr( $product->get_friendly_name() ), |
| 202 |
esc_attr( $requirement->get_friendly_name() ), |
| 203 |
'<a href="' . esc_url( admin_url( $requirement->is_theme() ? 'themes.php' : 'plugins.php' ) ) . '">', |
| 204 |
'</a>', |
| 205 |
esc_attr( $requirement->get_friendly_name() ), |
| 206 |
esc_attr( $requirement->is_theme() ? 'theme' : 'plugin' ) |
| 207 |
) ); |
| 208 |
$upgrader->maintenance_mode( false ); |
| 209 |
die(); |
| 210 |
} |
| 211 |
|
| 212 |
return $return; |
| 213 |
}, |
| 214 |
10, |
| 215 |
3 |
| 216 |
); |
| 217 |
|
| 218 |
add_action( |
| 219 |
'admin_notices', |
| 220 |
function () use ( $product, $requirement ) { |
| 221 |
echo '<div class="notice notice-error "><p>'; |
| 222 |
echo( sprintf( |
| 223 |
'%s requires a newer version of %s. Please %supdate%s %s %s to the latest version.', |
| 224 |
'<strong>' . esc_attr( $product->get_friendly_name() ) . '</strong>', |
| 225 |
'<strong>' . esc_attr( $requirement->get_friendly_name() ) . '</strong>', |
| 226 |
'<a href="' . esc_url( admin_url( $requirement->is_theme() ? 'themes.php' : 'plugins.php' ) ) . '">', |
| 227 |
'</a>', |
| 228 |
'<strong>' . esc_attr( $requirement->get_friendly_name() ) . '</strong>', |
| 229 |
esc_attr( $requirement->is_theme() ? 'theme' : 'plugin' ) |
| 230 |
) ); |
| 231 |
echo '</p></div>'; |
| 232 |
} |
| 233 |
); |
| 234 |
|
| 235 |
} |
| 236 |
} |
| 237 |
|