| 1 |
<?php |
| 2 |
/** |
| 3 |
* System Handler |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_System_Handler |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_System_Handler { |
| 16 |
|
| 17 |
// phpcs:disable WordPress.WP.AlternativeFunctions -- use system functions |
| 18 |
|
| 19 |
/** |
| 20 |
* Private static variable to hold the single instance of the class. |
| 21 |
* |
| 22 |
* @static |
| 23 |
* |
| 24 |
* @var mixed Default null |
| 25 |
*/ |
| 26 |
private static $instance = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Private variable to hold the upgrade version info. |
| 30 |
* |
| 31 |
* @var null Version info. |
| 32 |
*/ |
| 33 |
private $upgradeVersionInfo = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Method instance() |
| 37 |
* |
| 38 |
* Create public static instance. |
| 39 |
* |
| 40 |
* @static |
| 41 |
* @return MainWP_System |
| 42 |
*/ |
| 43 |
public static function instance() { |
| 44 |
if ( null == self::$instance ) { |
| 45 |
self::$instance = new self(); |
| 46 |
} |
| 47 |
|
| 48 |
return self::$instance; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* MainWP_System_Handler constructor. |
| 53 |
* |
| 54 |
* Run each time the class is called. |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
add_filter( 'mainwp-extension-enabled-check', array( MainWP_Extensions_Handler::get_class_name(), 'is_extension_enabled' ) ); // @deprecated Use 'mainwp_extension_enabled_check' instead. |
| 58 |
add_filter( 'mainwp_extension_enabled_check', array( MainWP_Extensions_Handler::get_class_name(), 'is_extension_enabled' ) ); |
| 59 |
|
| 60 |
/** |
| 61 |
* This hook allows you to get a list of sites via the 'mainwp-getsites' filter. |
| 62 |
* |
| 63 |
* @link http://codex.mainwp.com/#mainwp-getsites |
| 64 |
* |
| 65 |
* @see \MainWP_Extensions::hook_get_sites |
| 66 |
*/ |
| 67 |
add_filter( 'mainwp-getsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_sites' ), 10, 5 ); // @deprecated Use 'mainwp_getsites' instead. |
| 68 |
add_filter( 'mainwp-getdbsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_db_sites' ), 10, 5 ); // @deprecated Use 'mainwp_getdbsites' instead. |
| 69 |
|
| 70 |
add_filter( 'mainwp_getsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_sites' ), 10, 5 ); |
| 71 |
add_filter( 'mainwp_getdbsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_db_sites' ), 10, 5 ); |
| 72 |
|
| 73 |
/** |
| 74 |
* This hook allows you to get a information about groups via the 'mainwp-getgroups' filter. |
| 75 |
* |
| 76 |
* @link http://codex.mainwp.com/#mainwp-getgroups |
| 77 |
* |
| 78 |
* @see \MainWP_Extensions::hook_get_groups |
| 79 |
*/ |
| 80 |
add_filter( 'mainwp-getgroups', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_groups' ), 10, 4 ); // @deprecated Use 'mainwp_getgroups' instead. |
| 81 |
add_filter( 'mainwp_getgroups', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_groups' ), 10, 4 ); |
| 82 |
add_action( 'mainwp_fetchurlsauthed', array( &$this, 'filter_fetch_urls_authed' ), 10, 7 ); |
| 83 |
add_filter( 'mainwp_fetchurlauthed', array( &$this, 'filter_fetch_url_authed' ), 10, 6 ); |
| 84 |
add_filter( |
| 85 |
'mainwp_getdashboardsites', |
| 86 |
array( |
| 87 |
MainWP_Extensions_Handler::get_class_name(), |
| 88 |
'hook_get_dashboard_sites', |
| 89 |
), |
| 90 |
10, |
| 91 |
7 |
| 92 |
); |
| 93 |
|
| 94 |
// @deprecated Use 'mainwp_manager_getextensions' instead. |
| 95 |
add_filter( |
| 96 |
'mainwp-manager-getextensions', |
| 97 |
array( |
| 98 |
MainWP_Extensions_Handler::get_class_name(), |
| 99 |
'hook_get_all_extensions', |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
add_filter( |
| 104 |
'mainwp_manager_getextensions', |
| 105 |
array( |
| 106 |
MainWP_Extensions_Handler::get_class_name(), |
| 107 |
'hook_get_all_extensions', |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
if ( '' != get_option( 'mainwp_upgradeVersionInfo' ) ) { |
| 112 |
$this->upgradeVersionInfo = get_option( 'mainwp_upgradeVersionInfo' ); |
| 113 |
if ( ! is_object( $this->upgradeVersionInfo ) ) { |
| 114 |
$this->upgradeVersionInfo = new \stdClass(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Method filter_fetch_urls_authed() |
| 121 |
* |
| 122 |
* Filter fetch authorized urls. |
| 123 |
* |
| 124 |
* @param mixed $pluginFile MainWP extention. |
| 125 |
* @param string $key MainWP Licence Key. |
| 126 |
* @param object $dbwebsites Child Sites. |
| 127 |
* @param string $what Function to perform. |
| 128 |
* @param array $params Function paramerters. |
| 129 |
* @param mixed $handle Function handle. |
| 130 |
* @param mixed $output Function output. |
| 131 |
* |
| 132 |
* @return mixed MainWP_Extensions_Handler::hook_fetch_urls_authed() Hook fetch authorized URLs. |
| 133 |
*/ |
| 134 |
public function filter_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) { |
| 135 |
return MainWP_Extensions_Handler::hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output, $is_external_hook = true ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Method filter_fetch_url_authed() |
| 140 |
* |
| 141 |
* Filter fetch Authorized URL. |
| 142 |
* |
| 143 |
* @param mixed $pluginFile MainWP extention. |
| 144 |
* @param string $key MainWP licence key. |
| 145 |
* @param int $websiteId Website ID. |
| 146 |
* @param string $what Function to perform. |
| 147 |
* @param array $params Function paramerters. |
| 148 |
* @param null $raw_response Raw response. |
| 149 |
* |
| 150 |
* @return mixed MainWP_Extensions_Handler::hook_fetch_url_authed() Hook fetch authorized URL. |
| 151 |
*/ |
| 152 |
public function filter_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $raw_response = null ) { |
| 153 |
return MainWP_Extensions_Handler::hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $raw_response ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Method apply_filter() |
| 158 |
* |
| 159 |
* Apply filter |
| 160 |
* |
| 161 |
* @param string $filter The filter. |
| 162 |
* @param array $value Input value. |
| 163 |
* |
| 164 |
* @return array $output Output array. |
| 165 |
*/ |
| 166 |
public function apply_filters( $filter, $value = array() ) { |
| 167 |
|
| 168 |
if ( 'mainwp-getmetaboxes' === $filter ) { |
| 169 |
$output = apply_filters_deprecated( 'mainwp-getmetaboxes', array( $value ), '4.0.7.2', 'mainwp_getmetaboxes' ); // @deprecated Use 'mainwp_getmetaboxes' instead. |
| 170 |
} else { |
| 171 |
$output = apply_filters( $filter, $value ); |
| 172 |
} |
| 173 |
|
| 174 |
if ( ! is_array( $output ) ) { |
| 175 |
return array(); |
| 176 |
} |
| 177 |
$count = count( $output ); |
| 178 |
for ( $i = 0; $i < $count; $i ++ ) { |
| 179 |
if ( ! isset( $output[ $i ]['plugin'] ) || ! isset( $output[ $i ]['key'] ) ) { |
| 180 |
unset( $output[ $i ] ); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
if ( ! MainWP_Extensions_Handler::hook_verify( $output[ $i ]['plugin'], $output[ $i ]['key'] ) ) { |
| 185 |
unset( $output[ $i ] ); |
| 186 |
continue; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $output; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Method handle_manage_sites_screen_settings() |
| 195 |
* |
| 196 |
* Handle manage sites screen settings |
| 197 |
*/ |
| 198 |
public function handle_manage_sites_screen_settings() { |
| 199 |
if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'ManageSitesScrOptions' ) ) { |
| 200 |
$hide_cols = array(); |
| 201 |
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST ) ) as $key => $val ) { |
| 202 |
if ( false !== strpos( $key, 'mainwp_hide_column_' ) ) { |
| 203 |
$col = str_replace( 'mainwp_hide_column_', '', $key ); |
| 204 |
$hide_cols[] = $col; |
| 205 |
} |
| 206 |
} |
| 207 |
$user = wp_get_current_user(); |
| 208 |
if ( $user ) { |
| 209 |
update_user_option( $user->ID, 'mainwp_settings_hide_manage_sites_columns', $hide_cols, true ); |
| 210 |
update_option( 'mainwp_default_sites_per_page', ( isset( $_POST['mainwp_default_sites_per_page'] ) ? intval( $_POST['mainwp_default_sites_per_page'] ) : 25 ) ); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Method handle_mainwp_tools_settings() |
| 217 |
* |
| 218 |
* Handle mainwp tools settings |
| 219 |
*/ |
| 220 |
public function handle_mainwp_tools_settings() { |
| 221 |
$update_screen_options = false; |
| 222 |
if ( isset( $_POST['submit'] ) && isset( $_GET['page'] ) && 'MainWPTools' === $_GET['page'] ) { |
| 223 |
if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'MainWPTools' ) ) { |
| 224 |
$update_screen_options = true; |
| 225 |
MainWP_Utility::update_option( 'mainwp_enable_managed_cr_for_wc', ( ! isset( $_POST['enable_managed_cr_for_wc'] ) ? 0 : 1 ) ); |
| 226 |
MainWP_Utility::update_option( 'mainwp_use_favicon', ( ! isset( $_POST['mainwp_use_favicon'] ) ? 0 : 1 ) ); |
| 227 |
|
| 228 |
$enabled_twit = ! isset( $_POST['mainwp_hide_twitters_message'] ) ? 0 : 1; |
| 229 |
MainWP_Utility::update_option( 'mainwp_hide_twitters_message', $enabled_twit ); |
| 230 |
if ( ! $enabled_twit ) { |
| 231 |
MainWP_Twitter::clear_all_twitter_messages(); |
| 232 |
} |
| 233 |
} |
| 234 |
} elseif ( ( isset( $_GET['page'] ) && 'mainwp_tab' === $_GET['page'] ) || isset( $_GET['dashboard'] ) ) { |
| 235 |
if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'MainWPScrOptions' ) ) { |
| 236 |
$update_screen_options = true; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if ( $update_screen_options ) { |
| 241 |
$hide_wids = array(); |
| 242 |
if ( isset( $_POST['mainwp_hide_widgets'] ) && is_array( $_POST['mainwp_hide_widgets'] ) ) { |
| 243 |
$hide_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_hide_widgets'] ) ); |
| 244 |
} |
| 245 |
$user = wp_get_current_user(); |
| 246 |
if ( $user ) { |
| 247 |
update_user_option( $user->ID, 'mainwp_settings_hide_widgets', $hide_wids, true ); |
| 248 |
} |
| 249 |
|
| 250 |
MainWP_Utility::update_option( 'mainwp_hide_update_everything', ( ! isset( $_POST['hide_update_everything'] ) ? 0 : 1 ) ); |
| 251 |
MainWP_Utility::update_option( 'mainwp_show_usersnap', ( ! isset( $_POST['mainwp_show_usersnap'] ) ? 0 : time() ) ); |
| 252 |
MainWP_Utility::update_option( 'mainwp_number_overview_columns', ( isset( $_POST['number_overview_columns'] ) ? intval( $_POST['number_overview_columns'] ) : 2 ) ); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Method handle_settings_post() |
| 258 |
* |
| 259 |
* Handle saving settings page. |
| 260 |
*/ |
| 261 |
public function handle_settings_post() { |
| 262 |
if ( ! function_exists( 'wp_create_nonce' ) ) { |
| 263 |
include_once ABSPATH . WPINC . '/pluggable.php'; |
| 264 |
} |
| 265 |
|
| 266 |
if ( isset( $_GET['page'] ) && isset( $_POST['wp_nonce'] ) ) { |
| 267 |
$this->handle_mainwp_tools_settings(); |
| 268 |
$this->handle_manage_sites_screen_settings(); |
| 269 |
} |
| 270 |
|
| 271 |
if ( isset( $_POST['select_mainwp_options_siteview'] ) && check_admin_referer( 'mainwp-admin-nonce' ) ) { |
| 272 |
$userExtension = MainWP_DB_Common::instance()->get_user_extension(); |
| 273 |
$userExtension->site_view = ( empty( $_POST['select_mainwp_options_siteview'] ) ? MAINWP_VIEW_PER_PLUGIN_THEME : intval( $_POST['select_mainwp_options_siteview'] ) ); |
| 274 |
MainWP_DB_Common::instance()->update_user_extension( $userExtension ); |
| 275 |
} |
| 276 |
|
| 277 |
if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) ) { |
| 278 |
if ( wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) { |
| 279 |
$updated = MainWP_Settings::handle_settings_post(); |
| 280 |
$updated |= MainWP_Backup_Handler::handle_settings_post(); |
| 281 |
$updated |= MainWP_Monitoring_Handler::handle_settings_post(); |
| 282 |
$msg = ''; |
| 283 |
if ( $updated ) { |
| 284 |
$msg = '&message=saved'; |
| 285 |
} |
| 286 |
wp_safe_redirect( admin_url( 'admin.php?page=Settings' . $msg ) ); |
| 287 |
exit(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Method plugins_api_info() |
| 294 |
* |
| 295 |
* Get MainWP Extension api information. |
| 296 |
* |
| 297 |
* @param mixed $false Return value. |
| 298 |
* @param mixed $action Action being performed. |
| 299 |
* @param mixed $arg Action arguments. Should be the plugin slug. |
| 300 |
* |
| 301 |
* @return mixed $info|$false |
| 302 |
* |
| 303 |
* @uses \MainWP\Dashboard\MainWP_API_Handler::get_plugin_information() |
| 304 |
*/ |
| 305 |
public function plugins_api_info( $false, $action, $arg ) { |
| 306 |
if ( 'plugin_information' !== $action ) { |
| 307 |
return $false; |
| 308 |
} |
| 309 |
|
| 310 |
if ( ! isset( $arg->slug ) || ( '' === $arg->slug ) ) { |
| 311 |
return $false; |
| 312 |
} |
| 313 |
|
| 314 |
if ( dirname( MainWP_System::instance()->get_plugin_slug() ) === $arg->slug ) { |
| 315 |
return $false; |
| 316 |
} |
| 317 |
|
| 318 |
$result = MainWP_Extensions_Handler::get_slugs(); |
| 319 |
$am_slugs = $result['am_slugs']; |
| 320 |
|
| 321 |
if ( '' !== $am_slugs ) { |
| 322 |
$am_slugs = explode( ',', $am_slugs ); |
| 323 |
if ( in_array( $arg->slug, $am_slugs ) ) { |
| 324 |
$info = MainWP_API_Handler::get_plugin_information( $arg->slug ); |
| 325 |
if ( is_object( $info ) && property_exists( $info, 'sections' ) ) { |
| 326 |
if ( ! is_array( $info->sections ) || ! isset( $info->sections['changelog'] ) || empty( $info->sections['changelog'] ) ) { |
| 327 |
$exts_data = MainWP_Extensions_View::get_available_extensions(); |
| 328 |
if ( isset( $exts_data[ $arg->slug ] ) ) { |
| 329 |
$ext_info = $exts_data[ $arg->slug ]; |
| 330 |
$changelog_link = rtrim( $ext_info['link'], '/' ); |
| 331 |
$info->sections['changelog'] = '<a href="' . $changelog_link . '#tab-changelog" target="_blank">' . $changelog_link . '#tab-changelog</a>'; |
| 332 |
} |
| 333 |
} |
| 334 |
return $info; |
| 335 |
} |
| 336 |
return $info; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return $false; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Method check_update_custom() |
| 345 |
* |
| 346 |
* Check MainWP Exensions for updates. |
| 347 |
* |
| 348 |
* @param object $transient Transient information. |
| 349 |
* |
| 350 |
* @return object $transient Transient information. |
| 351 |
* |
| 352 |
* @uses \MainWP\Dashboard\MainWP_API_Handler::get_upgrade_information() |
| 353 |
*/ |
| 354 |
public function check_update_custom( $transient ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated. |
| 355 |
if ( isset( $_POST['action'] ) && ( ( 'update-plugin' === $_POST['action'] ) || ( 'update-selected' === $_POST['action'] ) ) ) { |
| 356 |
$extensions = MainWP_Extensions_Handler::get_indexed_extensions_infor( array( 'activated' => true ) ); |
| 357 |
if ( defined( 'DOING_AJAX' ) && isset( $_POST['plugin'] ) && 'update-plugin' == $_POST['action'] ) { |
| 358 |
$plugin_slug = sanitize_text_field( wp_unslash( $_POST['plugin'] ) ); |
| 359 |
if ( isset( $extensions[ $plugin_slug ] ) ) { |
| 360 |
if ( isset( $transient->response[ $plugin_slug ] ) && version_compare( $transient->response[ $plugin_slug ]->new_version, $extensions[ $plugin_slug ]['version'], '=' ) ) { |
| 361 |
return $transient; |
| 362 |
} |
| 363 |
|
| 364 |
$api_slug = dirname( $plugin_slug ); |
| 365 |
$rslt = MainWP_API_Handler::get_upgrade_information( $api_slug ); |
| 366 |
|
| 367 |
if ( ! empty( $rslt ) && isset( $rslt->latest_version ) && version_compare( $rslt->latest_version, $extensions[ $plugin_slug ]['version'], '>' ) ) { |
| 368 |
$transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt ); |
| 369 |
} |
| 370 |
|
| 371 |
return $transient; |
| 372 |
} |
| 373 |
} elseif ( 'update-selected' === $_POST['action'] && isset( $_POST['checked'] ) && is_array( $_POST['checked'] ) ) { |
| 374 |
$updated = false; |
| 375 |
foreach ( wp_unslash( $_POST['checked'] ) as $plugin_slug ) { |
| 376 |
if ( isset( $extensions[ $plugin_slug ] ) ) { |
| 377 |
if ( isset( $transient->response[ $plugin_slug ] ) && version_compare( $transient->response[ $plugin_slug ]->new_version, $extensions[ $plugin_slug ]['version'], '=' ) ) { |
| 378 |
continue; |
| 379 |
} |
| 380 |
$api_slug = dirname( $plugin_slug ); |
| 381 |
$rslt = MainWP_API_Handler::get_upgrade_information( $api_slug ); |
| 382 |
if ( ! empty( $rslt ) && isset( $rslt->latest_version ) && version_compare( $rslt->latest_version, $extensions[ $plugin_slug ]['version'], '>' ) ) { |
| 383 |
|
| 384 |
$this->upgradeVersionInfo->result[ $api_slug ] = $rslt; |
| 385 |
$transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt ); |
| 386 |
$updated = true; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
if ( $updated ) { |
| 391 |
MainWP_Utility::update_option( 'mainwp_upgradeVersionInfo', $this->upgradeVersionInfo ); |
| 392 |
} |
| 393 |
|
| 394 |
return $transient; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if ( empty( $transient->checked ) ) { |
| 399 |
return $transient; |
| 400 |
} |
| 401 |
|
| 402 |
if ( isset( $_GET['do'] ) && 'checkUpgrade' === $_GET['do'] && ( ( time() - $this->upgradeVersionInfo->updated ) > 30 ) ) { |
| 403 |
$this->check_upgrade(); |
| 404 |
} |
| 405 |
|
| 406 |
if ( null != $this->upgradeVersionInfo && property_exists( $this->upgradeVersionInfo, 'result' ) && is_array( $this->upgradeVersionInfo->result ) ) { |
| 407 |
foreach ( $this->upgradeVersionInfo->result as $rslt ) { |
| 408 |
if ( ! isset( $rslt->slug ) ) { |
| 409 |
continue; |
| 410 |
} |
| 411 |
|
| 412 |
$plugin_slug = MainWP_Extensions_Handler::get_extension_slug( $rslt->slug ); |
| 413 |
if ( isset( $transient->checked[ $plugin_slug ] ) && version_compare( $rslt->latest_version, $transient->checked[ $plugin_slug ], '>' ) ) { |
| 414 |
$transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt ); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
return $transient; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
/** |
| 424 |
* Method map_rslt_obj() |
| 425 |
* |
| 426 |
* Map resulting object. |
| 427 |
* |
| 428 |
* @param object $result Resulting information. |
| 429 |
* |
| 430 |
* @return object $obj Mapped resulting object. |
| 431 |
*/ |
| 432 |
public static function map_rslt_obj( $result ) { |
| 433 |
$obj = new \stdClass(); |
| 434 |
$obj->slug = $result->slug; |
| 435 |
$obj->new_version = $result->latest_version; |
| 436 |
$obj->url = 'https://mainwp.com/'; |
| 437 |
$obj->package = $result->download_url; |
| 438 |
|
| 439 |
return $obj; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Method check_upgrade() |
| 444 |
* |
| 445 |
* Check if Extension has an update. |
| 446 |
* |
| 447 |
* @uses \MainWP\Dashboard\MainWP_API_Handler::check_exts_upgrade() |
| 448 |
*/ |
| 449 |
private function check_upgrade() { |
| 450 |
$result = MainWP_API_Handler::check_exts_upgrade(); |
| 451 |
if ( null == $this->upgradeVersionInfo ) { |
| 452 |
$this->upgradeVersionInfo = new \stdClass(); |
| 453 |
} |
| 454 |
$this->upgradeVersionInfo->updated = time(); |
| 455 |
if ( ! empty( $result ) ) { |
| 456 |
$this->upgradeVersionInfo->result = $result; |
| 457 |
} |
| 458 |
MainWP_Utility::update_option( 'mainwp_upgradeVersionInfo', $this->upgradeVersionInfo ); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Method pre_check_update_custom() |
| 463 |
* |
| 464 |
* Pre-check for extension updates. |
| 465 |
* |
| 466 |
* @param object $transient Transient information. |
| 467 |
* |
| 468 |
* @return object $transient Transient information. |
| 469 |
*/ |
| 470 |
public function pre_check_update_custom( $transient ) { |
| 471 |
if ( ! isset( $transient->checked ) ) { |
| 472 |
return $transient; |
| 473 |
} |
| 474 |
|
| 475 |
if ( ( null == $this->upgradeVersionInfo ) || ! property_exists( $this->upgradeVersionInfo, 'updated' ) || ( ( time() - $this->upgradeVersionInfo->updated ) > 60 ) ) { |
| 476 |
$this->check_upgrade(); |
| 477 |
} |
| 478 |
|
| 479 |
if ( null != $this->upgradeVersionInfo && property_exists( $this->upgradeVersionInfo, 'result' ) && is_array( $this->upgradeVersionInfo->result ) ) { |
| 480 |
$extensions = MainWP_Extensions_Handler::get_indexed_extensions_infor( array( 'activated' => true ) ); |
| 481 |
foreach ( $this->upgradeVersionInfo->result as $rslt ) { |
| 482 |
$plugin_slug = MainWP_Extensions_Handler::get_extension_slug( $rslt->slug ); |
| 483 |
if ( isset( $extensions[ $plugin_slug ] ) && version_compare( $rslt->latest_version, $extensions[ $plugin_slug ]['version'], '>' ) ) { |
| 484 |
$transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt ); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
return $transient; |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Method upload_file() |
| 494 |
* |
| 495 |
* Upload a file. |
| 496 |
* |
| 497 |
* @param mixed $file File to upload. |
| 498 |
* |
| 499 |
* @return void |
| 500 |
*/ |
| 501 |
public function upload_file( $file ) { |
| 502 |
header( 'Content-Description: File Transfer' ); |
| 503 |
if ( MainWP_Utility::ends_with( $file, '.tar.gz' ) ) { |
| 504 |
header( 'Content-Type: application/x-gzip' ); |
| 505 |
header( 'Content-Encoding: gzip' ); |
| 506 |
} else { |
| 507 |
header( 'Content-Type: application/octet-stream' ); |
| 508 |
} |
| 509 |
header( 'Content-Disposition: attachment; filename="' . basename( $file ) . '"' ); |
| 510 |
header( 'Expires: 0' ); |
| 511 |
header( 'Cache-Control: must-revalidate' ); |
| 512 |
header( 'Pragma: public' ); |
| 513 |
header( 'Content-Length: ' . filesize( $file ) ); |
| 514 |
while ( ob_get_level() ) { |
| 515 |
ob_end_clean(); |
| 516 |
} |
| 517 |
$this->readfile_chunked( $file ); |
| 518 |
exit(); |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Method readfile_chunked() |
| 523 |
* |
| 524 |
* Read Chunked File. |
| 525 |
* |
| 526 |
* @param mixed $filename Name of file. |
| 527 |
* |
| 528 |
* @return mixed echo $buffer|false|$handle. |
| 529 |
*/ |
| 530 |
public function readfile_chunked( $filename ) { |
| 531 |
$chunksize = 1024; |
| 532 |
$handle = fopen( $filename, 'rb' ); |
| 533 |
if ( false === $handle ) { |
| 534 |
return false; |
| 535 |
} |
| 536 |
|
| 537 |
while ( ! feof( $handle ) ) { |
| 538 |
$buffer = fread( $handle, $chunksize ); |
| 539 |
echo $buffer; |
| 540 |
ob_flush(); |
| 541 |
flush(); |
| 542 |
$buffer = null; |
| 543 |
} |
| 544 |
|
| 545 |
return fclose( $handle ); |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Method activate_redirection() |
| 550 |
* |
| 551 |
* Redirect after activating MainWP Extension. |
| 552 |
* |
| 553 |
* @param mixed $location Location to redirect to. |
| 554 |
* |
| 555 |
* @return $location Admin URL + the page to redirect to. |
| 556 |
*/ |
| 557 |
public function activate_redirect( $location ) { |
| 558 |
$location = admin_url( 'admin.php?page=Extensions' ); |
| 559 |
return $location; |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Method activate_extension() |
| 564 |
* |
| 565 |
* Activate MainWP Extension. |
| 566 |
* |
| 567 |
* @param mixed $ext_key Extension API Key. |
| 568 |
* @param array $info Extension Info. |
| 569 |
*/ |
| 570 |
public function activate_extension( $ext_key, $info = array() ) { |
| 571 |
|
| 572 |
add_filter( 'wp_redirect', array( $this, 'activate_redirect' ) ); |
| 573 |
|
| 574 |
if ( is_array( $info ) && isset( $info['product_id'] ) && isset( $info['software_version'] ) ) { |
| 575 |
$act_info = array( |
| 576 |
'product_id' => $info['product_id'], |
| 577 |
'software_version' => $info['software_version'], |
| 578 |
'activated_key' => 'Deactivated', |
| 579 |
'instance_id' => MainWP_Api_Manager_Password_Management::generate_password( 12, false ), |
| 580 |
); |
| 581 |
MainWP_Api_Manager::instance()->set_activation_info( $ext_key, $act_info ); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Method deactivate_extension() |
| 587 |
* |
| 588 |
* Deactivate MaiNWP Extension. |
| 589 |
* |
| 590 |
* @param mixed $ext_key Exnension API Key. |
| 591 |
*/ |
| 592 |
public function deactivate_extension( $ext_key ) { |
| 593 |
MainWP_Api_Manager::instance()->set_activation_info( $ext_key, '' ); |
| 594 |
} |
| 595 |
|
| 596 |
} |
| 597 |
|