| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Manager_Addons |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @version 1.0.0 |
| 7 |
* @since 4.2.1 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class LP_Manager_Addons { |
| 13 |
protected static $_instance; |
| 14 |
/** |
| 15 |
* @var string Link get list addons. |
| 16 |
*/ |
| 17 |
public $url_list_addons = 'https://learnpress.github.io/learnpress/version-addons.json'; |
| 18 |
//public $url_list_addons = LP_PLUGIN_URL . '/version-addons.json'; |
| 19 |
/** |
| 20 |
* @var string $link_addon_action Link download plugin from Thimpress. |
| 21 |
*/ |
| 22 |
private $link_addon_action = 'https://updates.thimpress.com/thim-addon-market/download-addon'; |
| 23 |
public $link_addons_purchased = 'https://updates.thimpress.com/thim-addon-market/info-addons-purchased'; |
| 24 |
//public $link_addon_action = 'http://updates/thim-addon-market/download-addon'; |
| 25 |
/** |
| 26 |
* @var string $link_addon_action Link active site. |
| 27 |
*/ |
| 28 |
private $link_active_site = 'https://updates.thimpress.com/thim-addon-market/active-site'; |
| 29 |
/** |
| 30 |
* @var string link download plugin from org. |
| 31 |
*/ |
| 32 |
public $link_org = 'https://downloads.wordpress.org/plugin/'; |
| 33 |
/** |
| 34 |
* @var WP_Ajax_Upgrader_Skin $upgrader_skin |
| 35 |
*/ |
| 36 |
public $upgrader_skin; |
| 37 |
/** |
| 38 |
* @var Plugin_Upgrader $plugin_upgrader |
| 39 |
*/ |
| 40 |
public $plugin_upgrader; |
| 41 |
/** |
| 42 |
* |
| 43 |
*/ |
| 44 |
public $key_purchase_addons = 'purchase_addons'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor |
| 48 |
*/ |
| 49 |
protected function __construct() { |
| 50 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 51 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 52 |
|
| 53 |
$this->upgrader_skin = new WP_Ajax_Upgrader_Skin(); |
| 54 |
$this->plugin_upgrader = new Plugin_Upgrader( $this->upgrader_skin ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Singleton |
| 59 |
* |
| 60 |
* @return LP_Manager_Addons |
| 61 |
*/ |
| 62 |
public static function instance(): LP_Manager_Addons { |
| 63 |
if ( self::$_instance == null ) { |
| 64 |
self::$_instance = new self(); |
| 65 |
} |
| 66 |
|
| 67 |
return self::$_instance; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Download addon from Thimpress. |
| 72 |
* |
| 73 |
* return string |
| 74 |
* @throws Exception |
| 75 |
*/ |
| 76 |
public function download_from_thimpress( array $addon = [], string $purchase_code = '' ): string { |
| 77 |
$lp_file_system = LP_WP_Filesystem::instance(); |
| 78 |
$link_download = $this->link_addon_action; |
| 79 |
$args = [ |
| 80 |
'method' => 'POST', |
| 81 |
'body' => [ |
| 82 |
'addon' => $addon['slug'], |
| 83 |
'version' => 'lastest', |
| 84 |
], |
| 85 |
'user-agent' => site_url(), |
| 86 |
]; |
| 87 |
|
| 88 |
// For addon must buy. |
| 89 |
if ( 0 == $addon['is_free'] ) { |
| 90 |
$key_purchase = LP_Settings::get_option( $this->key_purchase_addons, [] ); |
| 91 |
$key_purchase[ $addon['slug'] ] = $purchase_code; |
| 92 |
LP_Settings::update_option( $this->key_purchase_addons, $key_purchase ); |
| 93 |
|
| 94 |
$args['body']['purchase_code'] = $purchase_code; |
| 95 |
} |
| 96 |
|
| 97 |
$result = wp_remote_post( $link_download, $args ); |
| 98 |
if ( is_wp_error( $result ) ) { |
| 99 |
throw new Exception( $result->get_error_message() ); |
| 100 |
} |
| 101 |
|
| 102 |
$data = wp_remote_retrieve_body( $result ); |
| 103 |
if ( preg_match( '/^Error.*/', $data ) ) { |
| 104 |
throw new Exception( $data ); |
| 105 |
} |
| 106 |
|
| 107 |
// Create file temp zip addon to install with |
| 108 |
$wp_upload_dir = wp_upload_dir( null, false ); |
| 109 |
$name = 'addon.zip'; |
| 110 |
$path_file = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $name; |
| 111 |
$lp_file_system->put_contents( $path_file, $data ); |
| 112 |
|
| 113 |
return $path_file; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Install plugin. |
| 118 |
* |
| 119 |
* @param array $addon |
| 120 |
* @param string $package The full local path or URI of the package. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
* @throws Exception |
| 124 |
*/ |
| 125 |
public function install( array $addon = [], string $package = '' ) { |
| 126 |
$result_install = $this->plugin_upgrader->install( $package ); |
| 127 |
if ( is_wp_error( $result_install ) ) { |
| 128 |
throw new Exception( $result_install->get_error_message() ); |
| 129 |
} elseif ( ! $result_install ) { |
| 130 |
throw new Exception( __( 'Install failed!', 'learnpress' ) ); |
| 131 |
} |
| 132 |
|
| 133 |
$result_active = activate_plugin( $addon['basename'] ); |
| 134 |
if ( is_wp_error( $result_active ) ) { |
| 135 |
throw new Exception( $result_active->get_error_message() ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Update plugin. |
| 141 |
* |
| 142 |
* @param array $addon |
| 143 |
* @param string $package |
| 144 |
* |
| 145 |
* @throws Exception |
| 146 |
*/ |
| 147 |
public function update( array $addon = [], string $package = '' ) { |
| 148 |
$is_activate = is_plugin_active( $addon['basename'] ); |
| 149 |
// Must call this function to upgrade success. |
| 150 |
wp_update_plugins(); |
| 151 |
|
| 152 |
$args_upgrade = [ |
| 153 |
'package' => $package, |
| 154 |
'destination' => WP_PLUGIN_DIR, |
| 155 |
'clear_destination' => false, |
| 156 |
'clear_working' => true, |
| 157 |
'hook_extra' => [], |
| 158 |
'abort_if_destination_exists' => false, |
| 159 |
]; |
| 160 |
$result = $this->plugin_upgrader->run( $args_upgrade ); |
| 161 |
if ( ! $result ) { |
| 162 |
throw new Exception( __( 'Update failed!', 'learnpress' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
if ( $is_activate ) { |
| 166 |
$this->activate( $addon ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Activate plugin. |
| 172 |
* |
| 173 |
* @param array $addon |
| 174 |
* |
| 175 |
* @return bool|int|true|WP_Error |
| 176 |
* @throws Exception |
| 177 |
*/ |
| 178 |
public function activate( array $addon = [] ) { |
| 179 |
if ( isset( $addon['dependency'] ) ) { |
| 180 |
foreach ( $addon['dependency'] as $addon_slug => $addon_label ) { |
| 181 |
if ( ! is_plugin_active( $addon_slug ) ) { |
| 182 |
throw new Exception( sprintf( 'Please activate "%s" plugin before activate this add-on', $addon_label ) ); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$result_active = activate_plugin( $addon['basename'] ?? '' ); |
| 188 |
if ( is_wp_error( $result_active ) ) { |
| 189 |
throw new Exception( $result_active->get_error_message() ); |
| 190 |
} |
| 191 |
|
| 192 |
return $result_active; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Deactivate plugin. |
| 197 |
* |
| 198 |
* @param array $addon |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function deactivate( array $addon = [] ) { |
| 203 |
deactivate_plugins( $addon['basename'] ?? '' ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Active site if install plugin via upload zip has "purchase code". |
| 208 |
* |
| 209 |
* @param $addon_slug |
| 210 |
* @param $purchase_code |
| 211 |
* |
| 212 |
* @return void |
| 213 |
*/ |
| 214 |
public function active_site( $addon_slug, $purchase_code ) { |
| 215 |
try { |
| 216 |
$args = [ |
| 217 |
'method' => 'POST', |
| 218 |
'body' => [ |
| 219 |
'addon' => $addon_slug, |
| 220 |
'purchase_code' => $purchase_code, |
| 221 |
], |
| 222 |
'user-agent' => site_url(), |
| 223 |
]; |
| 224 |
|
| 225 |
$result = wp_remote_post( $this->link_active_site, $args ); |
| 226 |
if ( is_wp_error( $result ) ) { |
| 227 |
throw new Exception( $result->get_error_message() ); |
| 228 |
} |
| 229 |
|
| 230 |
$data = wp_remote_retrieve_body( $result ); |
| 231 |
if ( preg_match( '/^Error.*/', $data ) ) { |
| 232 |
throw new Exception( $data ); |
| 233 |
} |
| 234 |
|
| 235 |
// Save keys purchase code of addons to table WP Options. |
| 236 |
$key_purchases = LP_Settings::get_option( LP_Manager_Addons::instance()->key_purchase_addons, [] ); |
| 237 |
$key_purchases[ $addon_slug ] = $purchase_code; |
| 238 |
LP_Settings::update_option( LP_Manager_Addons::instance()->key_purchase_addons, $key_purchases ); |
| 239 |
} catch ( Throwable $e ) { |
| 240 |
error_log( $e->getMessage() ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Get list addon have new version. |
| 246 |
* |
| 247 |
* @return array |
| 248 |
*/ |
| 249 |
public function list_addon_new_version(): array { |
| 250 |
$addon_contr = new LP_REST_Addon_Controller(); |
| 251 |
$request = new WP_REST_Request(); |
| 252 |
$request->set_param( 'return_obj', true ); |
| 253 |
$addons_rs = $addon_contr->list_addons( $request ); |
| 254 |
$addons_new_version = []; |
| 255 |
if ( 'success' === $addons_rs->status ) { |
| 256 |
$addons = $addons_rs->data; |
| 257 |
$plugins = get_plugins(); |
| 258 |
|
| 259 |
foreach ( $addons as $addon ) { |
| 260 |
if ( isset( $plugins[ $addon->basename ] ) ) { |
| 261 |
$version = $plugins[ $addon->basename ]['Version']; |
| 262 |
if ( version_compare( $version, $addon->version, '<' ) ) { |
| 263 |
$addons_new_version[] = $addon; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return $addons_new_version; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Check addons purchased need extend. |
| 274 |
* |
| 275 |
* @return bool |
| 276 |
* @throws Exception |
| 277 |
* @since 4.2.5.9 |
| 278 |
* @version 1.0.0 |
| 279 |
*/ |
| 280 |
public function check_addons_purchased_need_extend(): bool { |
| 281 |
$addon_contr = new LP_REST_Addon_Controller(); |
| 282 |
$request = new WP_REST_Request(); |
| 283 |
$request->set_param( 'return_obj', true ); |
| 284 |
$addons_rs = $addon_contr->list_addons( $request ); |
| 285 |
foreach ( $addons_rs->data as $addon ) { |
| 286 |
if ( isset( $addon->purchase_info ) ) { |
| 287 |
$addon_purchased = $addon->purchase_info; |
| 288 |
$date_expired_str = $addon_purchased->date_expire ?? ''; |
| 289 |
// Test |
| 290 |
//$date_expired_str = '2024-02-01'; |
| 291 |
//$date_expired_str = '2023-01-12'; |
| 292 |
// End |
| 293 |
$date_expired = new DateTime( $date_expired_str ); |
| 294 |
$date_now = new DateTime( gmdate( 'Y-m-d' ) ); |
| 295 |
$date_diff = date_diff( $date_now, $date_expired ); |
| 296 |
$number_days_remaining = $date_diff->days; |
| 297 |
if ( $date_diff->invert ) { |
| 298 |
$number_days_remaining = 0; |
| 299 |
} |
| 300 |
|
| 301 |
if ( $number_days_remaining <= 60 ) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return false; |
| 308 |
} |
| 309 |
} |
| 310 |
|