PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-api-manager-plugin-update.php

class-mainwp-api-manager-plugin-update.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.1, at class/class-mainwp-api-manager-plugin-update.php

203 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP API Manager Update Handler
4 *
5 * This class handles updates for MainWP Extension.
6 *
7 * @package MainWP API Manager/Update Handler
8 */
9
10 namespace MainWP\Dashboard;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class MainWP_Api_Manager_Plugin_Update
19 *
20 * MainWP API Manager Update Handler.
21 *
22 * @package MainWP API Manager/Update Handler
23 * @author Todd Lahman LLC
24 * @copyright Copyright (c) Todd Lahman LLC
25 * @since 1.0.0
26 */
27 class MainWP_Api_Manager_Plugin_Update {
28
29 /**
30 * Protected static varibale to hold the instance.
31 *
32 * @var null Default value.
33 */
34 protected static $instance = null;
35
36 /**
37 * Method instance()
38 *
39 * @static
40 * @return class instance
41 */
42 public static function instance() {
43 if ( is_null( self::$instance ) ) {
44 self::$instance = new self();
45 }
46
47 return self::$instance;
48 }
49
50 /**
51 * MainWP_Api_Manager_Plugin_Update constructor.
52 *
53 * Run each time the class is called.
54 */
55 public function __construct() {
56
57 // API data.
58 }
59
60
61 /**
62 * Create upgrade request API URL.
63 *
64 * @param array $args Request arguments.
65 * @param bool $bulk_check Bulk check request.
66 *
67 * @return string Build URL.
68 */
69 private function create_upgrade_api_url( $args, $bulk_check = true ) {
70 if ( $bulk_check ) {
71 $upgrade_url = esc_url_raw( add_query_arg( 'mainwp-api', 'am-software-api', MainWP_Api_Manager::instance()->get_upgrade_url() ) );
72 } else {
73 $upgrade_url = esc_url_raw( add_query_arg( 'wc-api', 'upgrade-api', MainWP_Api_Manager::instance()->get_upgrade_url() ) );
74 }
75
76 $query_url = '';
77 foreach ( $args as $key => $value ) {
78 $query_url .= $key . '=' . rawurlencode( $value ) . '&';
79 }
80 $query_url = rtrim( $query_url, '&' );
81
82 return $upgrade_url . '&' . $query_url;
83 }
84
85 /**
86 * Returns plugin information in an array.
87 *
88 * @param array $plugin Plugin to check.
89 *
90 * @return array Plugin information.
91 */
92 public function update_check( $plugin ) {
93
94 $args = array(
95 'request' => 'pluginupdatecheck',
96 'plugin_name' => $plugin['plugin_name'],
97 'version' => $plugin['software_version'],
98 'product_id' => $plugin['product_id'],
99 'api_key' => $plugin['api_key'],
100 'activation_email' => $plugin['activation_email'],
101 'instance' => $plugin['instance'],
102 'domain' => $plugin['domain'],
103 'software_version' => $plugin['software_version'],
104 'extra' => isset( $plugin['extra'] ) ? $plugin['extra'] : '',
105 );
106
107 // Check for a plugin update.
108 return $this->plugin_information( $args );
109 }
110
111
112 /**
113 * Check if bulkupdateapi is true|false & grab domain name adn extensions list.
114 *
115 * @param array $plugins List of plugins (extensions).
116 *
117 * @return array Plugin Information & bulkupdatecheck.
118 */
119 public function bulk_update_check( $plugins ) {
120 $args = array(
121 'request' => 'bulkupdatecheck',
122 'domain' => MainWP_Api_Manager::instance()->get_domain(),
123 'extensions' => base64_encode( serialize( $plugins ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
124 );
125 return $this->plugin_information( $args, true ); // bulk update check.
126 }
127
128
129 /**
130 * Check $args, if there is a response, an object eists & response is not false.
131 *
132 * @param array $args Request arguments.
133 *
134 * @return object $response Plugin information.
135 */
136 public function request( $args ) {
137 $args['request'] = 'plugininformation';
138
139 $response = $this->plugin_information( $args );
140
141 // If everything is okay return the response.
142 if ( isset( $response ) && is_object( $response ) && false !== $response ) {
143 return $response;
144 }
145 }
146
147 /**
148 * Sends and receives data to and from the server API.
149 *
150 * @access public
151 *
152 * @since 1.0.0
153 *
154 * @param array $args Request arguments.
155 * @param bool $bulk_check Check if updating in bulk true|false.
156 *
157 * @return object Plugin information.
158 */
159 public function plugin_information( $args, $bulk_check = false ) {
160 $target_url = $this->create_upgrade_api_url( $args, $bulk_check );
161 $apisslverify = ( ( get_option( 'mainwp_api_sslVerifyCertificate' ) === false ) || ( get_option( 'mainwp_api_sslVerifyCertificate' ) == 1 ) ) ? 1 : 0;
162 $request = wp_remote_get(
163 $target_url,
164 array(
165 'timeout' => 50,
166 'sslverify' => $apisslverify,
167 )
168 );
169
170 if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
171 return false;
172 }
173
174 if ( $bulk_check ) {
175 $response = wp_remote_retrieve_body( $request );
176 $response = MainWP_System_Utility::maybe_unserialyze( $response );
177 } else {
178 $response = MainWP_System_Utility::maybe_unserialyze( wp_remote_retrieve_body( $request ) );
179 }
180
181 /**
182 * For debugging errors from the API
183 * For errors like: unserialize(): Error at offset 0 of 170 bytes
184 * Comment out $response above first
185 */
186 if ( ! $bulk_check ) {
187 if ( is_object( $response ) ) {
188 if ( isset( $response->package ) ) {
189 $response->package = apply_filters( 'mainwp_api_manager_upgrade_url', $response->package );
190 }
191 return $response;
192 }
193 } elseif ( is_array( $response ) ) {
194 return $response;
195 }
196
197 return false;
198 }
199
200 }
201
202 // End of class.
203