| 1 |
<?php |
| 2 |
/** |
| 3 |
* Install, activate and deactivate MainWP Extensions. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class MainWP_API_Handler() |
| 17 |
* |
| 18 |
* MainWP API settings page |
| 19 |
*/ |
| 20 |
class MainWP_API_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 21 |
|
| 22 |
/** |
| 23 |
* Get Class name. |
| 24 |
* |
| 25 |
* @return string __CLASS__. |
| 26 |
*/ |
| 27 |
public static function get_class_name() { |
| 28 |
return __CLASS__; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check if extension has an update. |
| 33 |
* |
| 34 |
* @return mixed $output List of results. |
| 35 |
* |
| 36 |
* @uses MainWP_Api_Manager_Plugin_Update::bulk_update_check() |
| 37 |
* @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_extensions() |
| 38 |
*/ |
| 39 |
public static function check_exts_upgrade() { // phpcs:ignore -- NOSONAR - complex. |
| 40 |
|
| 41 |
$extensions = MainWP_Extensions_Handler::get_extensions(); |
| 42 |
$output = array(); |
| 43 |
$check_exts = array(); |
| 44 |
foreach ( $extensions as $ext ) { |
| 45 |
if ( isset( $ext['activated_key'] ) && 'Activated' === $ext['activated_key'] ) { |
| 46 |
// MWP-1546: $ext['api_key'] no longer exists in the aggregated |
| 47 |
// mainwp_extensions option (the plaintext was removed from |
| 48 |
// there). Fetch the per-slug activation info, which decrypts |
| 49 |
// the api_key on read and returns plaintext only for this |
| 50 |
// request scope. |
| 51 |
$args = array(); |
| 52 |
$api_slug = isset( $ext['slug'] ) ? dirname( $ext['slug'] ) : ''; |
| 53 |
$activation = MainWP_Api_Manager::instance()->get_activation_info( $api_slug ); |
| 54 |
$args['plugin_name'] = $ext['api']; |
| 55 |
$args['version'] = $ext['version']; |
| 56 |
$args['product_id'] = $ext['product_id']; |
| 57 |
$args['api_key'] = is_array( $activation ) && isset( $activation['api_key'] ) ? $activation['api_key'] : ''; |
| 58 |
$args['instance'] = $ext['instance_id']; |
| 59 |
$args['software_version'] = $ext['software_version']; |
| 60 |
$check_exts[ $args['plugin_name'] ] = $args; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ( empty( $check_exts ) ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
$i = 0; |
| 69 |
$count = 0; |
| 70 |
$max_check = 6; |
| 71 |
$total_check = count( $check_exts ); |
| 72 |
$bulk_checks = array(); |
| 73 |
|
| 74 |
foreach ( $check_exts as $ext_name => $ext ) { |
| 75 |
$bulk_checks[ $ext_name ] = $ext; |
| 76 |
++$i; |
| 77 |
++$count; |
| 78 |
if ( $count === $max_check || $i === $total_check ) { |
| 79 |
$results = MainWP_Api_Manager_Plugin_Update::instance()->bulk_update_check( $bulk_checks ); // bulk check response array of info. |
| 80 |
if ( is_array( $results ) && ! empty( $results ) ) { |
| 81 |
foreach ( $results as $slug => $response ) { |
| 82 |
if ( ! is_array( $response ) || ! isset( $response['new_version'] ) ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
$rslt = new \stdClass(); |
| 86 |
$rslt->slug = $slug; |
| 87 |
$rslt->new_version = $response['new_version']; |
| 88 |
$rslt->package = $response['package']; |
| 89 |
$rslt->key_status = ''; |
| 90 |
$rslt->apiManager = 1; |
| 91 |
|
| 92 |
if ( isset( $response['errors'] ) ) { |
| 93 |
$rslt->error = $response['errors']; |
| 94 |
} |
| 95 |
$output[ $slug ] = $rslt; |
| 96 |
} |
| 97 |
} |
| 98 |
$count = 0; |
| 99 |
$bulk_checks = array(); |
| 100 |
sleep( 3 ); |
| 101 |
} |
| 102 |
} |
| 103 |
return $output; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Check extension information for an update. |
| 108 |
* |
| 109 |
* @param string $pSlug Extension slug. |
| 110 |
* |
| 111 |
* @return array $rslt An array containing update information. |
| 112 |
* |
| 113 |
* @uses \MainWP\Dashboard\MainWP_Api_Manager::update_check() |
| 114 |
* @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_extensions() |
| 115 |
*/ |
| 116 |
public static function get_upgrade_information( $pSlug ) { |
| 117 |
|
| 118 |
$extensions = MainWP_Extensions_Handler::get_extensions(); |
| 119 |
$rslt = null; |
| 120 |
foreach ( $extensions as $ext ) { |
| 121 |
if ( isset( $ext['api'] ) && ( $pSlug === $ext['api'] ) && isset( $ext['apiManager'] ) && ! empty( $ext['apiManager'] ) ) { |
| 122 |
// MWP-1546: same migration as check_exts_upgrade above -- |
| 123 |
// pull the api_key from per-slug get_activation_info() instead |
| 124 |
// of the (now-stripped) aggregated mainwp_extensions option. |
| 125 |
$api_slug = isset( $ext['slug'] ) ? dirname( $ext['slug'] ) : ''; |
| 126 |
$activation = MainWP_Api_Manager::instance()->get_activation_info( $api_slug ); |
| 127 |
$args = array(); |
| 128 |
$args['plugin_name'] = $ext['api']; |
| 129 |
$args['version'] = $ext['version']; |
| 130 |
$args['product_id'] = $ext['product_id']; |
| 131 |
$args['api_key'] = is_array( $activation ) && isset( $activation['api_key'] ) ? $activation['api_key'] : ''; |
| 132 |
$args['instance'] = $ext['instance_id']; |
| 133 |
$args['software_version'] = $ext['software_version']; |
| 134 |
$response = MainWP_Api_Manager_Plugin_Update::instance()->update_check( $args ); |
| 135 |
|
| 136 |
if ( ! empty( $response ) ) { |
| 137 |
$rslt = new \stdClass(); |
| 138 |
$rslt->slug = $ext['api']; |
| 139 |
$rslt->new_version = $response->new_version; |
| 140 |
$rslt->package = $response->package; |
| 141 |
$rslt->key_status = ''; |
| 142 |
$rslt->apiManager = 1; |
| 143 |
if ( isset( $response->errors ) ) { |
| 144 |
$rslt->error = $response->errors; |
| 145 |
} |
| 146 |
} |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
return $rslt; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get extension information. |
| 155 |
* |
| 156 |
* @param string $pSlug Extension slug. |
| 157 |
* |
| 158 |
* @return array $rslt An array containing extension information. |
| 159 |
* |
| 160 |
* @uses \MainWP\Dashboard\MainWP_Api_Manager::request_extension_information() |
| 161 |
* @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_extensions() |
| 162 |
*/ |
| 163 |
public static function get_update_information( $pSlug ) { |
| 164 |
$extensions = MainWP_Extensions_Handler::get_extensions(); |
| 165 |
$rslt = null; |
| 166 |
foreach ( $extensions as $ext ) { |
| 167 |
if ( isset( $ext['api'] ) && $pSlug === $ext['api'] && isset( $ext['apiManager'] ) && ! empty( $ext['apiManager'] ) ) { |
| 168 |
// MWP-1546: same migration as the two functions above. |
| 169 |
$api_slug = isset( $ext['slug'] ) ? dirname( $ext['slug'] ) : ''; |
| 170 |
$activation = MainWP_Api_Manager::instance()->get_activation_info( $api_slug ); |
| 171 |
$args = array(); |
| 172 |
$args['plugin_name'] = $ext['api']; |
| 173 |
$args['version'] = $ext['version']; |
| 174 |
$args['product_id'] = $ext['product_id']; |
| 175 |
$args['api_key'] = is_array( $activation ) && isset( $activation['api_key'] ) ? $activation['api_key'] : ''; |
| 176 |
$args['instance'] = $ext['instance_id']; |
| 177 |
$args['software_version'] = $ext['software_version']; |
| 178 |
$rslt = MainWP_Api_Manager::instance()->request_extension_information( $args ); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
return $rslt; |
| 183 |
} |
| 184 |
} |
| 185 |
|