Completed
Push — master ( 83d701...cb55bd )
by Stephanie
04:35
created
classes/models/EDD_SL_Plugin_Updater.php 2 patches
Indentation   +226 added lines, -226 removed lines patch added patch discarded remove patch
@@ -15,254 +15,254 @@  discard block
 block discarded – undo
15 15
  * @version 1.6
16 16
  */
17 17
 class EDD_SL_Plugin_Updater {
18
-    private $api_url   = '';
19
-    private $api_data  = array();
20
-    private $name      = '';
21
-    private $slug      = '';
22
-    private $version   = '';
23
-
24
-    /**
25
-     * Class constructor.
26
-     *
27
-     * @uses plugin_basename()
28
-     * @uses hook()
29
-     *
30
-     * @param string  $_api_url     The URL pointing to the custom API endpoint.
31
-     * @param string  $_plugin_file Path to the plugin file.
32
-     * @param array   $_api_data    Optional data to send with API calls.
33
-     */
34
-    public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
35
-        $this->api_url  = trailingslashit( $_api_url );
36
-        $this->api_data = $_api_data;
37
-        $this->name     = plugin_basename( $_plugin_file );
38
-        $this->slug     = basename( $_plugin_file, '.php' );
39
-        $this->version  = $_api_data['version'];
40
-
41
-        // Set up hooks.
42
-        $this->init();
43
-        add_action( 'admin_init', array( $this, 'show_changelog' ) );
44
-    }
45
-
46
-    /**
47
-     * Set up WordPress filters to hook into WP's update process.
48
-     *
49
-     * @uses add_filter()
50
-     *
51
-     * @return void
52
-     */
53
-    public function init() {
54
-
55
-        add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
56
-        add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
18
+	private $api_url   = '';
19
+	private $api_data  = array();
20
+	private $name      = '';
21
+	private $slug      = '';
22
+	private $version   = '';
23
+
24
+	/**
25
+	 * Class constructor.
26
+	 *
27
+	 * @uses plugin_basename()
28
+	 * @uses hook()
29
+	 *
30
+	 * @param string  $_api_url     The URL pointing to the custom API endpoint.
31
+	 * @param string  $_plugin_file Path to the plugin file.
32
+	 * @param array   $_api_data    Optional data to send with API calls.
33
+	 */
34
+	public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
35
+		$this->api_url  = trailingslashit( $_api_url );
36
+		$this->api_data = $_api_data;
37
+		$this->name     = plugin_basename( $_plugin_file );
38
+		$this->slug     = basename( $_plugin_file, '.php' );
39
+		$this->version  = $_api_data['version'];
40
+
41
+		// Set up hooks.
42
+		$this->init();
43
+		add_action( 'admin_init', array( $this, 'show_changelog' ) );
44
+	}
45
+
46
+	/**
47
+	 * Set up WordPress filters to hook into WP's update process.
48
+	 *
49
+	 * @uses add_filter()
50
+	 *
51
+	 * @return void
52
+	 */
53
+	public function init() {
54
+
55
+		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
56
+		add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
57 57
 
58 58
 		add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 );
59
-    }
60
-
61
-    /**
62
-     * Check for Updates at the defined API endpoint and modify the update array.
63
-     *
64
-     * This function dives into the update API just when WordPress creates its update array,
65
-     * then adds a custom API call and injects the custom plugin data retrieved from the API.
66
-     * It is reassembled from parts of the native WordPress plugin update code.
67
-     * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
68
-     *
69
-     * @uses api_request()
70
-     *
71
-     * @param array   $_transient_data Update array build by WordPress.
72
-     * @return array Modified update array with custom plugin data.
73
-     */
74
-    public function check_update( $_transient_data ) {
75
-
76
-        global $pagenow;
77
-
78
-        if ( ! is_object( $_transient_data ) ) {
79
-            $_transient_data = new stdClass;
80
-        }
59
+	}
60
+
61
+	/**
62
+	 * Check for Updates at the defined API endpoint and modify the update array.
63
+	 *
64
+	 * This function dives into the update API just when WordPress creates its update array,
65
+	 * then adds a custom API call and injects the custom plugin data retrieved from the API.
66
+	 * It is reassembled from parts of the native WordPress plugin update code.
67
+	 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
68
+	 *
69
+	 * @uses api_request()
70
+	 *
71
+	 * @param array   $_transient_data Update array build by WordPress.
72
+	 * @return array Modified update array with custom plugin data.
73
+	 */
74
+	public function check_update( $_transient_data ) {
75
+
76
+		global $pagenow;
77
+
78
+		if ( ! is_object( $_transient_data ) ) {
79
+			$_transient_data = new stdClass;
80
+		}
81 81
 
82
-        if ( 'plugins.php' == $pagenow && is_multisite() ) {
83
-            return $_transient_data;
84
-        }
82
+		if ( 'plugins.php' == $pagenow && is_multisite() ) {
83
+			return $_transient_data;
84
+		}
85 85
 
86
-        if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
86
+		if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
87 87
 
88
-            $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
88
+			$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
89 89
 
90
-            if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
90
+			if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
91 91
 
92
-                if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
92
+				if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
93 93
 
94 94
 					if ( empty( $version_info->plugin ) ) {
95 95
 						$version_info->plugin = $this->name;
96 96
 					}
97 97
 
98
-                    $_transient_data->response[ $this->name ] = $version_info;
99
-
100
-                }
101
-
102
-                $_transient_data->last_checked = time();
103
-                $_transient_data->checked[ $this->name ] = $this->version;
98
+					$_transient_data->response[ $this->name ] = $version_info;
104 99
 
105
-            }
106
-        }
100
+				}
107 101
 
108
-        return $_transient_data;
109
-    }
102
+				$_transient_data->last_checked = time();
103
+				$_transient_data->checked[ $this->name ] = $this->version;
110 104
 
111
-    /**
112
-     * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
113
-     *
114
-     * @param string  $file
115
-     * @param array   $plugin
116
-     */
117
-    public function show_update_notification( $file, $plugin ) {
118
-
119
-        if ( ! current_user_can( 'update_plugins' ) ) {
120
-            return;
121
-        }
122
-
123
-        if ( ! is_multisite() ) {
124
-            return;
125
-        }
126
-
127
-        if ( $this->name != $file ) {
128
-            return;
129
-        }
130
-
131
-        // Remove our filter on the site transient
132
-        remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
105
+			}
106
+		}
133 107
 
134
-        $update_cache = get_site_transient( 'update_plugins' );
108
+		return $_transient_data;
109
+	}
135 110
 
136
-        if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
111
+	/**
112
+	 * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
113
+	 *
114
+	 * @param string  $file
115
+	 * @param array   $plugin
116
+	 */
117
+	public function show_update_notification( $file, $plugin ) {
137 118
 
138
-            $cache_key    = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' );
139
-            $version_info = get_transient( $cache_key );
119
+		if ( ! current_user_can( 'update_plugins' ) ) {
120
+			return;
121
+		}
140 122
 
141
-            if ( false === $version_info ) {
123
+		if ( ! is_multisite() ) {
124
+			return;
125
+		}
142 126
 
143
-                $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
127
+		if ( $this->name != $file ) {
128
+			return;
129
+		}
144 130
 
145
-                set_transient( $cache_key, $version_info, HOUR_IN_SECONDS );
146
-            }
131
+		// Remove our filter on the site transient
132
+		remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
147 133
 
148
-            if ( ! is_object( $version_info ) ) {
149
-                return;
150
-            }
134
+		$update_cache = get_site_transient( 'update_plugins' );
151 135
 
152
-            if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
136
+		if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
153 137
 
154
-                $update_cache->response[ $this->name ] = $version_info;
138
+			$cache_key    = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' );
139
+			$version_info = get_transient( $cache_key );
155 140
 
156
-            }
141
+			if ( false === $version_info ) {
157 142
 
158
-            $update_cache->last_checked = time();
159
-            $update_cache->checked[ $this->name ] = $this->version;
143
+				$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
160 144
 
161
-            set_site_transient( 'update_plugins', $update_cache );
145
+				set_transient( $cache_key, $version_info, HOUR_IN_SECONDS );
146
+			}
162 147
 
163
-        } else {
148
+			if ( ! is_object( $version_info ) ) {
149
+				return;
150
+			}
164 151
 
165
-            $version_info = $update_cache->response[ $this->name ];
152
+			if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
166 153
 
167
-        }
154
+				$update_cache->response[ $this->name ] = $version_info;
168 155
 
169
-        // Restore our filter
170
-        add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
171
-    }
156
+			}
172 157
 
158
+			$update_cache->last_checked = time();
159
+			$update_cache->checked[ $this->name ] = $this->version;
173 160
 
174
-    /**
175
-     * Updates information on the "View version x.x details" page with custom data.
176
-     *
177
-     * @uses api_request()
178
-     *
179
-     * @param mixed   $_data
180
-     * @param string  $_action
181
-     * @param object  $_args
182
-     * @return object $_data
183
-     */
184
-    public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
161
+			set_site_transient( 'update_plugins', $update_cache );
185 162
 
186
-        if ( $_action != 'plugin_information' ) {
163
+		} else {
187 164
 
188
-            return $_data;
165
+			$version_info = $update_cache->response[ $this->name ];
189 166
 
190
-        }
167
+		}
191 168
 
192
-        if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
169
+		// Restore our filter
170
+		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
171
+	}
193 172
 
194
-            return $_data;
195 173
 
196
-        }
174
+	/**
175
+	 * Updates information on the "View version x.x details" page with custom data.
176
+	 *
177
+	 * @uses api_request()
178
+	 *
179
+	 * @param mixed   $_data
180
+	 * @param string  $_action
181
+	 * @param object  $_args
182
+	 * @return object $_data
183
+	 */
184
+	public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
197 185
 
198
-        $to_send = array(
199
-            'slug'   => $this->slug,
200
-            'is_ssl' => is_ssl(),
201
-            'fields' => array(
202
-                'banners' => false, // These will be supported soon hopefully
203
-                'reviews' => false,
204
-            ),
205
-        );
186
+		if ( $_action != 'plugin_information' ) {
206 187
 
207
-        $api_response = $this->api_request( 'plugin_information', $to_send );
188
+			return $_data;
208 189
 
209
-        if ( false !== $api_response ) {
210
-            $_data = $api_response;
211
-        }
190
+		}
212 191
 
213
-        return $_data;
214
-    }
192
+		if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
215 193
 
194
+			return $_data;
216 195
 
217
-    /**
218
-     * Disable SSL verification in order to prevent download update failures
219
-     *
220
-     * @param array   $args
221
-     * @param string  $url
222
-     * @return object $array
223
-     */
224
-    public function http_request_args( $args, $url ) {
225
-        // If it is an https request and we are performing a package download, disable ssl verification
226
-        if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
227
-            $args['sslverify'] = false;
228
-        }
229
-        return $args;
230
-    }
196
+		}
231 197
 
232
-    /**
233
-     * Calls the API and, if successfull, returns the object delivered by the API.
234
-     *
235
-     * @uses get_bloginfo()
236
-     * @uses wp_remote_post()
237
-     * @uses is_wp_error()
238
-     *
239
-     * @param string  $_action The requested action.
240
-     * @param array   $_data   Parameters for the API action.
241
-     * @return false|object
242
-     */
243
-    private function api_request( $_action, $_data ) {
198
+		$to_send = array(
199
+			'slug'   => $this->slug,
200
+			'is_ssl' => is_ssl(),
201
+			'fields' => array(
202
+				'banners' => false, // These will be supported soon hopefully
203
+				'reviews' => false,
204
+			),
205
+		);
244 206
 
245
-        global $wp_version;
207
+		$api_response = $this->api_request( 'plugin_information', $to_send );
246 208
 
247
-        $data = array_merge( $this->api_data, $_data );
209
+		if ( false !== $api_response ) {
210
+			$_data = $api_response;
211
+		}
248 212
 
249
-        if ( $data['slug'] != $this->slug ) {
250
-            return;
213
+		return $_data;
214
+	}
215
+
216
+
217
+	/**
218
+	 * Disable SSL verification in order to prevent download update failures
219
+	 *
220
+	 * @param array   $args
221
+	 * @param string  $url
222
+	 * @return object $array
223
+	 */
224
+	public function http_request_args( $args, $url ) {
225
+		// If it is an https request and we are performing a package download, disable ssl verification
226
+		if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
227
+			$args['sslverify'] = false;
228
+		}
229
+		return $args;
230
+	}
231
+
232
+	/**
233
+	 * Calls the API and, if successfull, returns the object delivered by the API.
234
+	 *
235
+	 * @uses get_bloginfo()
236
+	 * @uses wp_remote_post()
237
+	 * @uses is_wp_error()
238
+	 *
239
+	 * @param string  $_action The requested action.
240
+	 * @param array   $_data   Parameters for the API action.
241
+	 * @return false|object
242
+	 */
243
+	private function api_request( $_action, $_data ) {
244
+
245
+		global $wp_version;
246
+
247
+		$data = array_merge( $this->api_data, $_data );
248
+
249
+		if ( $data['slug'] != $this->slug ) {
250
+			return;
251 251
 		}
252 252
 
253
-        if ( $this->api_url == home_url() ) {
254
-            return false; // Don't allow a plugin to ping itself
255
-        }
253
+		if ( $this->api_url == home_url() ) {
254
+			return false; // Don't allow a plugin to ping itself
255
+		}
256 256
 
257
-        $api_params = array(
258
-            'edd_action' => 'get_version',
259
-            'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
260
-            'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
261
-            'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
262
-            'slug'       => $data['slug'],
263
-            'author'     => $data['author'],
264
-            'url'        => home_url(),
265
-        );
257
+		$api_params = array(
258
+			'edd_action' => 'get_version',
259
+			'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
260
+			'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
261
+			'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
262
+			'slug'       => $data['slug'],
263
+			'author'     => $data['author'],
264
+			'url'        => home_url(),
265
+		);
266 266
 
267 267
 		$cache_key = 'edd_plugin_' . md5( sanitize_key( $api_params['license'] . $this->version ) . '_' . $api_params['edd_action'] );
268 268
 		$cached_response = get_transient( $cache_key );
@@ -271,47 +271,47 @@  discard block
 block discarded – undo
271 271
 			return $cached_response;
272 272
 		}
273 273
 
274
-        $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
274
+		$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
275 275
 
276
-        if ( ! is_wp_error( $request ) ) {
277
-            $request = json_decode( wp_remote_retrieve_body( $request ) );
278
-        }
276
+		if ( ! is_wp_error( $request ) ) {
277
+			$request = json_decode( wp_remote_retrieve_body( $request ) );
278
+		}
279 279
 
280
-        if ( $request && isset( $request->sections ) ) {
281
-            $request->sections = maybe_unserialize( $request->sections );
280
+		if ( $request && isset( $request->sections ) ) {
281
+			$request->sections = maybe_unserialize( $request->sections );
282 282
 			set_transient( $cache_key, $request, DAY_IN_SECONDS );
283
-        } else {
284
-            $request = false;
283
+		} else {
284
+			$request = false;
285 285
 			set_transient( $cache_key, 0, DAY_IN_SECONDS );
286
-        }
286
+		}
287 287
 
288
-        return $request;
289
-    }
288
+		return $request;
289
+	}
290 290
 
291
-    public function show_changelog() {
291
+	public function show_changelog() {
292 292
 
293
-        if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
294
-            return;
295
-        }
293
+		if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
294
+			return;
295
+		}
296 296
 
297
-        if ( empty( $_REQUEST['plugin'] ) ) {
298
-            return;
299
-        }
297
+		if ( empty( $_REQUEST['plugin'] ) ) {
298
+			return;
299
+		}
300 300
 
301
-        if ( empty( $_REQUEST['slug'] ) ) {
302
-            return;
303
-        }
301
+		if ( empty( $_REQUEST['slug'] ) ) {
302
+			return;
303
+		}
304 304
 
305
-        if ( ! current_user_can( 'update_plugins' ) ) {
306
-            wp_die( __( 'You do not have permission to install plugin updates', 'edd' ), __( 'Error', 'edd' ), array( 'response' => 403 ) );
307
-        }
305
+		if ( ! current_user_can( 'update_plugins' ) ) {
306
+			wp_die( __( 'You do not have permission to install plugin updates', 'edd' ), __( 'Error', 'edd' ), array( 'response' => 403 ) );
307
+		}
308 308
 
309
-        $response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
309
+		$response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
310 310
 
311
-        if ( $response && isset( $response->sections['changelog'] ) ) {
312
-            echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
313
-        }
311
+		if ( $response && isset( $response->sections['changelog'] ) ) {
312
+			echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
313
+		}
314 314
 
315
-        exit;
316
-    }
315
+		exit;
316
+	}
317 317
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
             return $_transient_data;
84 84
         }
85 85
 
86
-        if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
86
+        if ( empty( $_transient_data->response ) || empty( $_transient_data->response[$this->name] ) ) {
87 87
 
88 88
             $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
89 89
 
@@ -95,12 +95,12 @@  discard block
 block discarded – undo
95 95
 						$version_info->plugin = $this->name;
96 96
 					}
97 97
 
98
-                    $_transient_data->response[ $this->name ] = $version_info;
98
+                    $_transient_data->response[$this->name] = $version_info;
99 99
 
100 100
                 }
101 101
 
102 102
                 $_transient_data->last_checked = time();
103
-                $_transient_data->checked[ $this->name ] = $this->version;
103
+                $_transient_data->checked[$this->name] = $this->version;
104 104
 
105 105
             }
106 106
         }
@@ -133,9 +133,9 @@  discard block
 block discarded – undo
133 133
 
134 134
         $update_cache = get_site_transient( 'update_plugins' );
135 135
 
136
-        if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
136
+        if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[$this->name] ) ) {
137 137
 
138
-            $cache_key    = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' );
138
+            $cache_key    = md5( 'edd_plugin_' . sanitize_key( $this->name ) . '_version_info' );
139 139
             $version_info = get_transient( $cache_key );
140 140
 
141 141
             if ( false === $version_info ) {
@@ -151,18 +151,18 @@  discard block
 block discarded – undo
151 151
 
152 152
             if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
153 153
 
154
-                $update_cache->response[ $this->name ] = $version_info;
154
+                $update_cache->response[$this->name] = $version_info;
155 155
 
156 156
             }
157 157
 
158 158
             $update_cache->last_checked = time();
159
-            $update_cache->checked[ $this->name ] = $this->version;
159
+            $update_cache->checked[$this->name] = $this->version;
160 160
 
161 161
             set_site_transient( 'update_plugins', $update_cache );
162 162
 
163 163
         } else {
164 164
 
165
-            $version_info = $update_cache->response[ $this->name ];
165
+            $version_info = $update_cache->response[$this->name];
166 166
 
167 167
         }
168 168
 
Please login to merge, or discard this patch.