templates-patterns-collection
/
vendor
/
codeinwp
/
themeisle-sdk
/
src
/
Modules
/
Compatibilities.php
About_us.php
3 weeks ago
Abstract_Migration.php
2 months ago
Announcements.php
2 months ago
Compatibilities.php
2 years ago
Dashboard_widget.php
3 weeks ago
Featured_plugins.php
1 month ago
Float_widget.php
1 year ago
Licenser.php
2 months ago
Logger.php
10 months ago
Migrator.php
2 months ago
Notification.php
3 years ago
Promotions.php
3 weeks ago
Recommendation.php
3 years ago
Review.php
1 year ago
Rollback.php
1 year ago
Script_loader.php
1 year ago
Translate.php
5 years ago
Translations.php
1 year ago
Uninstall_feedback.php
2 days ago
Welcome.php
2 years ago
Compatibilities.php
236 lines
| 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\Loader; |
| 16 | use ThemeisleSDK\Product; |
| 17 | |
| 18 | // Exit if accessed directly. |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Promotions module for ThemeIsle SDK. |
| 25 | */ |
| 26 | class Compatibilities extends Abstract_Module { |
| 27 | const REQUIRED = 'required'; |
| 28 | const TESTED_UP = 'tested_up'; |
| 29 | |
| 30 | /** |
| 31 | * Should we load this module. |
| 32 | * |
| 33 | * @param Product $product Product object. |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function can_load( $product ) { |
| 38 | if ( $this->is_from_partner( $product ) ) { |
| 39 | return false; |
| 40 | } |
| 41 | if ( $product->is_theme() && ! current_user_can( 'switch_themes' ) ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if ( $product->is_plugin() && ! current_user_can( 'install_plugins' ) ) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Registers the hooks. |
| 54 | * |
| 55 | * @param Product $product Product to load. |
| 56 | * |
| 57 | * @return Compatibilities Module instance. |
| 58 | * @throws \Exception If the configuration is invalid. |
| 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 | |
| 128 | function checkTheme() { |
| 129 | var theme = jQuery('.theme.active[data-slug="<?php echo esc_attr( $requirement->get_slug() ); ?>"]'); |
| 130 | var notice_id = 'testedup<?php echo esc_attr( $requirement->get_slug() . $product->get_slug() ); ?>'; |
| 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"><?php echo sprintf( esc_html( Loader::$labels['compatibilities']['notice_theme'] ), '<strong>', '</strong>', esc_attr( $product->get_friendly_name() ) ); ?></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><?php echo sprintf( esc_html( Loader::$labels['compatibilities']['notice_theme'] ), '<strong>', '</strong>', esc_attr( $product->get_friendly_name() ) ); ?></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 | |
| 149 | function checkPlugin() { |
| 150 | var plugin = jQuery('.plugins .active[data-slug="<?php echo esc_attr( $requirement->get_slug() ); ?>"]'); |
| 151 | var notice_id = 'testedup<?php echo esc_attr( $requirement->get_slug() . $product->get_slug() ); ?>'; |
| 152 | if (plugin.length > 0 && jQuery('#' + notice_id).length === 0) { |
| 153 | plugin.find('.column-description').append('<div style="bottom:100%;top:auto;" id="' + notice_id + '" class="notice notice-warning notice-alt notice-inline"><?php echo sprintf( esc_html( Loader::$labels['compatibilities']['notice_plugin'] ), '<strong>', '</strong>', esc_attr( $product->get_friendly_name() ), esc_attr( $product->is_plugin() ? Loader::$labels['compatibilities']['plugin'] : Loader::$labels['compatibilities']['theme'] ) ); ?></div>'); |
| 154 | } |
| 155 | } |
| 156 | }) |
| 157 | |
| 158 | </script> |
| 159 | <?php |
| 160 | } |
| 161 | } |
| 162 | ); |
| 163 | |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Mark the product requirements. |
| 168 | * |
| 169 | * @param Product $product Product object. |
| 170 | * @param Product $requirement Requirement object. |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | public function mark_required( $product, $requirement ) { |
| 175 | add_filter( |
| 176 | 'upgrader_pre_download', |
| 177 | function ( $return, $package, $upgrader ) use ( $product, $requirement ) { |
| 178 | /** |
| 179 | * Upgrader object. |
| 180 | * |
| 181 | * @var \WP_Upgrader $upgrader Upgrader object. |
| 182 | */ |
| 183 | $should_block = false; |
| 184 | if ( $product->is_theme() |
| 185 | && property_exists( $upgrader, 'skin' ) |
| 186 | && property_exists( $upgrader->skin, 'theme_info' ) |
| 187 | && $upgrader->skin->theme_info->template === $product->get_slug() ) { |
| 188 | $should_block = true; |
| 189 | |
| 190 | } |
| 191 | if ( ! $should_block && $product->is_plugin() |
| 192 | && property_exists( $upgrader, 'skin' ) |
| 193 | && property_exists( $upgrader->skin, 'plugin_info' ) |
| 194 | && $upgrader->skin->plugin_info['Name'] === $product->get_name() ) { |
| 195 | $should_block = true; |
| 196 | } |
| 197 | if ( $should_block ) { |
| 198 | echo( sprintf( |
| 199 | esc_html( Loader::$labels['compatibilities']['notice2'] ), |
| 200 | esc_attr( $product->get_friendly_name() ), |
| 201 | esc_attr( $requirement->get_friendly_name() ), |
| 202 | '<a href="' . esc_url( admin_url( $requirement->is_theme() ? 'themes.php' : 'plugins.php' ) ) . '">', |
| 203 | '</a>', |
| 204 | esc_attr( $requirement->get_friendly_name() ), |
| 205 | esc_attr( $requirement->is_theme() ? Loader::$labels['compatibilities']['theme'] : Loader::$labels['compatibilities']['plugin'] ) |
| 206 | ) ); |
| 207 | $upgrader->maintenance_mode( false ); |
| 208 | die(); |
| 209 | } |
| 210 | |
| 211 | return $return; |
| 212 | }, |
| 213 | 10, |
| 214 | 3 |
| 215 | ); |
| 216 | |
| 217 | add_action( |
| 218 | 'admin_notices', |
| 219 | function () use ( $product, $requirement ) { |
| 220 | echo '<div class="notice notice-error "><p>'; |
| 221 | echo( sprintf( |
| 222 | esc_html( Loader::$labels['compatibilities']['notice'] ), |
| 223 | '<strong>' . esc_attr( $product->get_friendly_name() ) . '</strong>', |
| 224 | '<strong>' . esc_attr( $requirement->get_friendly_name() ) . '</strong>', |
| 225 | '<a href="' . esc_url( admin_url( $requirement->is_theme() ? 'themes.php' : 'plugins.php' ) ) . '">', |
| 226 | '</a>', |
| 227 | '<strong>' . esc_attr( $requirement->get_friendly_name() ) . '</strong>', |
| 228 | esc_attr( $requirement->is_theme() ? Loader::$labels['compatibilities']['theme'] : Loader::$labels['compatibilities']['plugin'] ) |
| 229 | ) ); |
| 230 | echo '</p></div>'; |
| 231 | } |
| 232 | ); |
| 233 | |
| 234 | } |
| 235 | } |
| 236 |