Completed
Push — master ( 703846...83d701 )
by Stephanie
05:19
created
classes/models/EDD_SL_Plugin_Updater.php 1 patch
Indentation   +266 added lines, -266 removed lines patch added patch discarded remove patch
@@ -15,282 +15,282 @@  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
-        add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
55
-        add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
56
-    }
57
-
58
-    /**
59
-     * Check for Updates at the defined API endpoint and modify the update array.
60
-     *
61
-     * This function dives into the update API just when WordPress creates its update array,
62
-     * then adds a custom API call and injects the custom plugin data retrieved from the API.
63
-     * It is reassembled from parts of the native WordPress plugin update code.
64
-     * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
65
-     *
66
-     * @uses api_request()
67
-     *
68
-     * @param array   $_transient_data Update array build by WordPress.
69
-     * @return array Modified update array with custom plugin data.
70
-     */
71
-    public function check_update( $_transient_data ) {
72
-
73
-        global $pagenow;
74
-
75
-        if ( ! is_object( $_transient_data ) ) {
76
-            $_transient_data = new stdClass;
77
-        }
78
-
79
-        if ( 'plugins.php' == $pagenow && is_multisite() ) {
80
-            return $_transient_data;
81
-        }
82
-
83
-        if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
84
-
85
-            $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
86
-
87
-            if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
88
-
89
-                if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
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
+		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
55
+		add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
56
+	}
57
+
58
+	/**
59
+	 * Check for Updates at the defined API endpoint and modify the update array.
60
+	 *
61
+	 * This function dives into the update API just when WordPress creates its update array,
62
+	 * then adds a custom API call and injects the custom plugin data retrieved from the API.
63
+	 * It is reassembled from parts of the native WordPress plugin update code.
64
+	 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
65
+	 *
66
+	 * @uses api_request()
67
+	 *
68
+	 * @param array   $_transient_data Update array build by WordPress.
69
+	 * @return array Modified update array with custom plugin data.
70
+	 */
71
+	public function check_update( $_transient_data ) {
72
+
73
+		global $pagenow;
74
+
75
+		if ( ! is_object( $_transient_data ) ) {
76
+			$_transient_data = new stdClass;
77
+		}
78
+
79
+		if ( 'plugins.php' == $pagenow && is_multisite() ) {
80
+			return $_transient_data;
81
+		}
82
+
83
+		if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
84
+
85
+			$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
86
+
87
+			if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
88
+
89
+				if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
90 90
 
91 91
 					if ( empty( $version_info->plugin ) ) {
92 92
 						$version_info->plugin = $this->name;
93 93
 					}
94 94
 
95
-                    $_transient_data->response[ $this->name ] = $version_info;
95
+					$_transient_data->response[ $this->name ] = $version_info;
96 96
 
97
-                }
97
+				}
98 98
 
99
-                $_transient_data->last_checked = time();
100
-                $_transient_data->checked[ $this->name ] = $this->version;
99
+				$_transient_data->last_checked = time();
100
+				$_transient_data->checked[ $this->name ] = $this->version;
101 101
 
102
-            }
103
-        }
102
+			}
103
+		}
104 104
 
105
-        return $_transient_data;
106
-    }
105
+		return $_transient_data;
106
+	}
107 107
 
108
-    /**
109
-     * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
110
-     *
111
-     * @param string  $file
112
-     * @param array   $plugin
113
-     */
114
-    public function show_update_notification( $file, $plugin ) {
108
+	/**
109
+	 * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
110
+	 *
111
+	 * @param string  $file
112
+	 * @param array   $plugin
113
+	 */
114
+	public function show_update_notification( $file, $plugin ) {
115 115
 
116
-        if ( ! current_user_can( 'update_plugins' ) ) {
117
-            return;
118
-        }
116
+		if ( ! current_user_can( 'update_plugins' ) ) {
117
+			return;
118
+		}
119 119
 
120
-        if ( ! is_multisite() ) {
121
-            return;
122
-        }
120
+		if ( ! is_multisite() ) {
121
+			return;
122
+		}
123 123
 
124
-        if ( $this->name != $file ) {
125
-            return;
126
-        }
124
+		if ( $this->name != $file ) {
125
+			return;
126
+		}
127 127
 
128
-        // Remove our filter on the site transient
129
-        remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
128
+		// Remove our filter on the site transient
129
+		remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
130 130
 
131
-        $update_cache = get_site_transient( 'update_plugins' );
131
+		$update_cache = get_site_transient( 'update_plugins' );
132 132
 
133
-        if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
133
+		if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
134 134
 
135
-            $cache_key    = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' );
136
-            $version_info = get_transient( $cache_key );
135
+			$cache_key    = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' );
136
+			$version_info = get_transient( $cache_key );
137 137
 
138
-            if ( false === $version_info ) {
138
+			if ( false === $version_info ) {
139 139
 
140
-                $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
140
+				$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
141 141
 
142
-                set_transient( $cache_key, $version_info, HOUR_IN_SECONDS );
143
-            }
142
+				set_transient( $cache_key, $version_info, HOUR_IN_SECONDS );
143
+			}
144 144
 
145
-            if ( ! is_object( $version_info ) ) {
146
-                return;
147
-            }
145
+			if ( ! is_object( $version_info ) ) {
146
+				return;
147
+			}
148 148
 
149
-            if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
149
+			if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
150 150
 
151
-                $update_cache->response[ $this->name ] = $version_info;
151
+				$update_cache->response[ $this->name ] = $version_info;
152 152
 
153
-            }
153
+			}
154 154
 
155
-            $update_cache->last_checked = time();
156
-            $update_cache->checked[ $this->name ] = $this->version;
155
+			$update_cache->last_checked = time();
156
+			$update_cache->checked[ $this->name ] = $this->version;
157 157
 
158
-            set_site_transient( 'update_plugins', $update_cache );
158
+			set_site_transient( 'update_plugins', $update_cache );
159 159
 
160
-        } else {
160
+		} else {
161 161
 
162
-            $version_info = $update_cache->response[ $this->name ];
162
+			$version_info = $update_cache->response[ $this->name ];
163 163
 
164
-        }
164
+		}
165 165
 
166
-        // Restore our filter
167
-        add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
166
+		// Restore our filter
167
+		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
168 168
 
169
-        if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
169
+		if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
170 170
 
171
-            // build a plugin list row, with update notification
172
-            $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
173
-            echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
171
+			// build a plugin list row, with update notification
172
+			$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
173
+			echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
174 174
 
175
-            $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );
175
+			$changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );
176 176
 
177
-            if ( empty( $version_info->download_link ) ) {
178
-                printf(
179
-                    __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'edd' ),
180
-                    esc_html( $version_info->name ),
181
-                    '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
182
-                    esc_html( $version_info->new_version ),
177
+			if ( empty( $version_info->download_link ) ) {
178
+				printf(
179
+					__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'edd' ),
180
+					esc_html( $version_info->name ),
181
+					'<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
182
+					esc_html( $version_info->new_version ),
183 183
 					'</a>'
184
-                );
185
-            } else {
186
-                printf(
187
-                    __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'edd' ),
188
-                    esc_html( $version_info->name ),
189
-                    '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
190
-                    esc_html( $version_info->new_version ),
184
+				);
185
+			} else {
186
+				printf(
187
+					__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'edd' ),
188
+					esc_html( $version_info->name ),
189
+					'<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
190
+					esc_html( $version_info->new_version ),
191 191
 					'</a>',
192
-                    '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) .'">',
192
+					'<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) .'">',
193 193
 					'</a>'
194
-                );
195
-            }
196
-
197
-            echo '</div></td></tr>';
198
-        }
199
-    }
200
-
201
-
202
-    /**
203
-     * Updates information on the "View version x.x details" page with custom data.
204
-     *
205
-     * @uses api_request()
206
-     *
207
-     * @param mixed   $_data
208
-     * @param string  $_action
209
-     * @param object  $_args
210
-     * @return object $_data
211
-     */
212
-    public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
213
-
214
-        if ( $_action != 'plugin_information' ) {
215
-
216
-            return $_data;
217
-
218
-        }
219
-
220
-        if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
221
-
222
-            return $_data;
223
-
224
-        }
225
-
226
-        $to_send = array(
227
-            'slug'   => $this->slug,
228
-            'is_ssl' => is_ssl(),
229
-            'fields' => array(
230
-                'banners' => false, // These will be supported soon hopefully
231
-                'reviews' => false,
232
-            ),
233
-        );
234
-
235
-        $api_response = $this->api_request( 'plugin_information', $to_send );
236
-
237
-        if ( false !== $api_response ) {
238
-            $_data = $api_response;
239
-        }
240
-
241
-        return $_data;
242
-    }
243
-
244
-
245
-    /**
246
-     * Disable SSL verification in order to prevent download update failures
247
-     *
248
-     * @param array   $args
249
-     * @param string  $url
250
-     * @return object $array
251
-     */
252
-    public function http_request_args( $args, $url ) {
253
-        // If it is an https request and we are performing a package download, disable ssl verification
254
-        if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
255
-            $args['sslverify'] = false;
256
-        }
257
-        return $args;
258
-    }
259
-
260
-    /**
261
-     * Calls the API and, if successfull, returns the object delivered by the API.
262
-     *
263
-     * @uses get_bloginfo()
264
-     * @uses wp_remote_post()
265
-     * @uses is_wp_error()
266
-     *
267
-     * @param string  $_action The requested action.
268
-     * @param array   $_data   Parameters for the API action.
269
-     * @return false|object
270
-     */
271
-    private function api_request( $_action, $_data ) {
272
-
273
-        global $wp_version;
274
-
275
-        $data = array_merge( $this->api_data, $_data );
276
-
277
-        if ( $data['slug'] != $this->slug ) {
278
-            return;
194
+				);
195
+			}
196
+
197
+			echo '</div></td></tr>';
198
+		}
199
+	}
200
+
201
+
202
+	/**
203
+	 * Updates information on the "View version x.x details" page with custom data.
204
+	 *
205
+	 * @uses api_request()
206
+	 *
207
+	 * @param mixed   $_data
208
+	 * @param string  $_action
209
+	 * @param object  $_args
210
+	 * @return object $_data
211
+	 */
212
+	public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
213
+
214
+		if ( $_action != 'plugin_information' ) {
215
+
216
+			return $_data;
217
+
279 218
 		}
280 219
 
281
-        if ( $this->api_url == home_url() ) {
282
-            return false; // Don't allow a plugin to ping itself
283
-        }
220
+		if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
284 221
 
285
-        $api_params = array(
286
-            'edd_action' => 'get_version',
287
-            'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
288
-            'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
289
-            'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
290
-            'slug'       => $data['slug'],
291
-            'author'     => $data['author'],
292
-            'url'        => home_url(),
293
-        );
222
+			return $_data;
223
+
224
+		}
225
+
226
+		$to_send = array(
227
+			'slug'   => $this->slug,
228
+			'is_ssl' => is_ssl(),
229
+			'fields' => array(
230
+				'banners' => false, // These will be supported soon hopefully
231
+				'reviews' => false,
232
+			),
233
+		);
234
+
235
+		$api_response = $this->api_request( 'plugin_information', $to_send );
236
+
237
+		if ( false !== $api_response ) {
238
+			$_data = $api_response;
239
+		}
240
+
241
+		return $_data;
242
+	}
243
+
244
+
245
+	/**
246
+	 * Disable SSL verification in order to prevent download update failures
247
+	 *
248
+	 * @param array   $args
249
+	 * @param string  $url
250
+	 * @return object $array
251
+	 */
252
+	public function http_request_args( $args, $url ) {
253
+		// If it is an https request and we are performing a package download, disable ssl verification
254
+		if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
255
+			$args['sslverify'] = false;
256
+		}
257
+		return $args;
258
+	}
259
+
260
+	/**
261
+	 * Calls the API and, if successfull, returns the object delivered by the API.
262
+	 *
263
+	 * @uses get_bloginfo()
264
+	 * @uses wp_remote_post()
265
+	 * @uses is_wp_error()
266
+	 *
267
+	 * @param string  $_action The requested action.
268
+	 * @param array   $_data   Parameters for the API action.
269
+	 * @return false|object
270
+	 */
271
+	private function api_request( $_action, $_data ) {
272
+
273
+		global $wp_version;
274
+
275
+		$data = array_merge( $this->api_data, $_data );
276
+
277
+		if ( $data['slug'] != $this->slug ) {
278
+			return;
279
+		}
280
+
281
+		if ( $this->api_url == home_url() ) {
282
+			return false; // Don't allow a plugin to ping itself
283
+		}
284
+
285
+		$api_params = array(
286
+			'edd_action' => 'get_version',
287
+			'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
288
+			'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
289
+			'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
290
+			'slug'       => $data['slug'],
291
+			'author'     => $data['author'],
292
+			'url'        => home_url(),
293
+		);
294 294
 
295 295
 		$cache_key = 'edd_plugin_' . md5( sanitize_key( $api_params['license'] . $this->version ) . '_' . $api_params['edd_action'] );
296 296
 		$cached_response = get_transient( $cache_key );
@@ -299,47 +299,47 @@  discard block
 block discarded – undo
299 299
 			return $cached_response;
300 300
 		}
301 301
 
302
-        $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
302
+		$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
303 303
 
304
-        if ( ! is_wp_error( $request ) ) {
305
-            $request = json_decode( wp_remote_retrieve_body( $request ) );
306
-        }
304
+		if ( ! is_wp_error( $request ) ) {
305
+			$request = json_decode( wp_remote_retrieve_body( $request ) );
306
+		}
307 307
 
308
-        if ( $request && isset( $request->sections ) ) {
309
-            $request->sections = maybe_unserialize( $request->sections );
308
+		if ( $request && isset( $request->sections ) ) {
309
+			$request->sections = maybe_unserialize( $request->sections );
310 310
 			set_transient( $cache_key, $request, DAY_IN_SECONDS );
311
-        } else {
312
-            $request = false;
311
+		} else {
312
+			$request = false;
313 313
 			set_transient( $cache_key, 0, DAY_IN_SECONDS );
314
-        }
314
+		}
315 315
 
316
-        return $request;
317
-    }
316
+		return $request;
317
+	}
318 318
 
319
-    public function show_changelog() {
319
+	public function show_changelog() {
320 320
 
321
-        if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
322
-            return;
323
-        }
321
+		if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
322
+			return;
323
+		}
324 324
 
325
-        if ( empty( $_REQUEST['plugin'] ) ) {
326
-            return;
327
-        }
325
+		if ( empty( $_REQUEST['plugin'] ) ) {
326
+			return;
327
+		}
328 328
 
329
-        if ( empty( $_REQUEST['slug'] ) ) {
330
-            return;
331
-        }
329
+		if ( empty( $_REQUEST['slug'] ) ) {
330
+			return;
331
+		}
332 332
 
333
-        if ( ! current_user_can( 'update_plugins' ) ) {
334
-            wp_die( __( 'You do not have permission to install plugin updates', 'edd' ), __( 'Error', 'edd' ), array( 'response' => 403 ) );
335
-        }
333
+		if ( ! current_user_can( 'update_plugins' ) ) {
334
+			wp_die( __( 'You do not have permission to install plugin updates', 'edd' ), __( 'Error', 'edd' ), array( 'response' => 403 ) );
335
+		}
336 336
 
337
-        $response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
337
+		$response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
338 338
 
339
-        if ( $response && isset( $response->sections['changelog'] ) ) {
340
-            echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
341
-        }
339
+		if ( $response && isset( $response->sections['changelog'] ) ) {
340
+			echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
341
+		}
342 342
 
343
-        exit;
344
-    }
343
+		exit;
344
+	}
345 345
 }
Please login to merge, or discard this patch.