Completed
Push — master ( ece780...467b3a )
by Stephanie
10:37
created
classes/models/FrmEDD_SL_Plugin_Updater.php 1 patch
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -15,184 +15,184 @@  discard block
 block discarded – undo
15 15
  * @version 1.6
16 16
  */
17 17
 class FrmEDD_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 ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
80
-
81
-            $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
82
-
83
-            if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
84
-
85
-                if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
86
-
87
-					if ( empty( $version_info->plugin ) ) {
88
-						$version_info->plugin = $this->name;
89
-					}
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
+		}
90 78
 
91
-                    $_transient_data->response[ $this->name ] = $version_info;
79
+		if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) {
92 80
 
93
-                }
94
-
95
-                $_transient_data->last_checked = time();
96
-                $_transient_data->checked[ $this->name ] = $this->version;
81
+			$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
97 82
 
98
-            }
99
-        }
83
+			if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
100 84
 
101
-        return $_transient_data;
102
-    }
85
+				if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
103 86
 
104
-    /**
105
-     * Updates information on the "View version x.x details" page with custom data.
106
-     *
107
-     * @uses api_request()
108
-     *
109
-     * @param mixed   $_data
110
-     * @param string  $_action
111
-     * @param object  $_args
112
-     * @return object $_data
113
-     */
114
-    public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
87
+					if ( empty( $version_info->plugin ) ) {
88
+						$version_info->plugin = $this->name;
89
+					}
115 90
 
116
-        if ( $_action != 'plugin_information' ) {
91
+					$_transient_data->response[ $this->name ] = $version_info;
117 92
 
118
-            return $_data;
93
+				}
119 94
 
120
-        }
95
+				$_transient_data->last_checked = time();
96
+				$_transient_data->checked[ $this->name ] = $this->version;
121 97
 
122
-        if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
98
+			}
99
+		}
123 100
 
124
-            return $_data;
101
+		return $_transient_data;
102
+	}
125 103
 
126
-        }
104
+	/**
105
+	 * Updates information on the "View version x.x details" page with custom data.
106
+	 *
107
+	 * @uses api_request()
108
+	 *
109
+	 * @param mixed   $_data
110
+	 * @param string  $_action
111
+	 * @param object  $_args
112
+	 * @return object $_data
113
+	 */
114
+	public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
127 115
 
128
-        $to_send = array(
129
-            'slug'   => $this->slug,
130
-            'is_ssl' => is_ssl(),
131
-            'fields' => array(
132
-                'banners' => false, // These will be supported soon hopefully
133
-                'reviews' => false,
134
-            ),
135
-        );
116
+		if ( $_action != 'plugin_information' ) {
136 117
 
137
-        $api_response = $this->api_request( 'plugin_information', $to_send );
118
+			return $_data;
138 119
 
139
-        if ( false !== $api_response ) {
140
-            $_data = $api_response;
141
-        }
120
+		}
142 121
 
143
-        return $_data;
144
-    }
122
+		if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
145 123
 
124
+			return $_data;
146 125
 
147
-    /**
148
-     * Disable SSL verification in order to prevent download update failures
149
-     *
150
-     * @param array   $args
151
-     * @param string  $url
152
-     * @return object $array
153
-     */
154
-    public function http_request_args( $args, $url ) {
155
-        // If it is an https request and we are performing a package download, disable ssl verification
156
-        if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
157
-            $args['sslverify'] = false;
158
-        }
159
-        return $args;
160
-    }
126
+		}
161 127
 
162
-    /**
163
-     * Calls the API and, if successfull, returns the object delivered by the API.
164
-     *
165
-     * @uses get_bloginfo()
166
-     * @uses wp_remote_post()
167
-     * @uses is_wp_error()
168
-     *
169
-     * @param string  $_action The requested action.
170
-     * @param array   $_data   Parameters for the API action.
171
-     * @return false|object
172
-     */
173
-    private function api_request( $_action, $_data ) {
128
+		$to_send = array(
129
+			'slug'   => $this->slug,
130
+			'is_ssl' => is_ssl(),
131
+			'fields' => array(
132
+				'banners' => false, // These will be supported soon hopefully
133
+				'reviews' => false,
134
+			),
135
+		);
174 136
 
175
-        global $wp_version;
137
+		$api_response = $this->api_request( 'plugin_information', $to_send );
176 138
 
177
-        $data = array_merge( $this->api_data, $_data );
139
+		if ( false !== $api_response ) {
140
+			$_data = $api_response;
141
+		}
178 142
 
179
-        if ( $data['slug'] != $this->slug ) {
180
-            return;
143
+		return $_data;
144
+	}
145
+
146
+
147
+	/**
148
+	 * Disable SSL verification in order to prevent download update failures
149
+	 *
150
+	 * @param array   $args
151
+	 * @param string  $url
152
+	 * @return object $array
153
+	 */
154
+	public function http_request_args( $args, $url ) {
155
+		// If it is an https request and we are performing a package download, disable ssl verification
156
+		if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
157
+			$args['sslverify'] = false;
158
+		}
159
+		return $args;
160
+	}
161
+
162
+	/**
163
+	 * Calls the API and, if successfull, returns the object delivered by the API.
164
+	 *
165
+	 * @uses get_bloginfo()
166
+	 * @uses wp_remote_post()
167
+	 * @uses is_wp_error()
168
+	 *
169
+	 * @param string  $_action The requested action.
170
+	 * @param array   $_data   Parameters for the API action.
171
+	 * @return false|object
172
+	 */
173
+	private function api_request( $_action, $_data ) {
174
+
175
+		global $wp_version;
176
+
177
+		$data = array_merge( $this->api_data, $_data );
178
+
179
+		if ( $data['slug'] != $this->slug ) {
180
+			return;
181 181
 		}
182 182
 
183
-        if ( $this->api_url == home_url() ) {
184
-            return false; // Don't allow a plugin to ping itself
185
-        }
183
+		if ( $this->api_url == home_url() ) {
184
+			return false; // Don't allow a plugin to ping itself
185
+		}
186 186
 
187
-        $api_params = array(
188
-            'edd_action' => 'get_version',
189
-            'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
190
-            'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
191
-            'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
192
-            'slug'       => $data['slug'],
193
-            'author'     => $data['author'],
194
-            'url'        => home_url(),
195
-        );
187
+		$api_params = array(
188
+			'edd_action' => 'get_version',
189
+			'license'    => ! empty( $data['license'] ) ? $data['license'] : '',
190
+			'item_name'  => isset( $data['item_name'] ) ? $data['item_name'] : false,
191
+			'item_id'    => isset( $data['item_id'] ) ? $data['item_id'] : false,
192
+			'slug'       => $data['slug'],
193
+			'author'     => $data['author'],
194
+			'url'        => home_url(),
195
+		);
196 196
 
197 197
 		$cache_key = 'edd_plugin_' . md5( sanitize_key( $api_params['license'] . $this->version ) . '_' . $api_params['edd_action'] );
198 198
 		$cached_response = get_transient( $cache_key );
@@ -201,47 +201,47 @@  discard block
 block discarded – undo
201 201
 			return $cached_response;
202 202
 		}
203 203
 
204
-        $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
204
+		$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
205 205
 
206
-        if ( ! is_wp_error( $request ) ) {
207
-            $request = json_decode( wp_remote_retrieve_body( $request ) );
208
-        }
206
+		if ( ! is_wp_error( $request ) ) {
207
+			$request = json_decode( wp_remote_retrieve_body( $request ) );
208
+		}
209 209
 
210
-        if ( $request && isset( $request->sections ) ) {
211
-            $request->sections = maybe_unserialize( $request->sections );
210
+		if ( $request && isset( $request->sections ) ) {
211
+			$request->sections = maybe_unserialize( $request->sections );
212 212
 			set_transient( $cache_key, $request, DAY_IN_SECONDS );
213
-        } else {
214
-            $request = false;
213
+		} else {
214
+			$request = false;
215 215
 			set_transient( $cache_key, 0, DAY_IN_SECONDS );
216
-        }
216
+		}
217 217
 
218
-        return $request;
219
-    }
218
+		return $request;
219
+	}
220 220
 
221
-    public function show_changelog() {
221
+	public function show_changelog() {
222 222
 
223
-        if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
224
-            return;
225
-        }
223
+		if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
224
+			return;
225
+		}
226 226
 
227
-        if ( empty( $_REQUEST['plugin'] ) ) {
228
-            return;
229
-        }
227
+		if ( empty( $_REQUEST['plugin'] ) ) {
228
+			return;
229
+		}
230 230
 
231
-        if ( empty( $_REQUEST['slug'] ) ) {
232
-            return;
233
-        }
231
+		if ( empty( $_REQUEST['slug'] ) ) {
232
+			return;
233
+		}
234 234
 
235
-        if ( ! current_user_can( 'update_plugins' ) ) {
236
-            wp_die( __( 'You do not have permission to install plugin updates', 'formidable' ), __( 'Error', 'formidable' ), array( 'response' => 403 ) );
237
-        }
235
+		if ( ! current_user_can( 'update_plugins' ) ) {
236
+			wp_die( __( 'You do not have permission to install plugin updates', 'formidable' ), __( 'Error', 'formidable' ), array( 'response' => 403 ) );
237
+		}
238 238
 
239
-        $response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
239
+		$response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) );
240 240
 
241
-        if ( $response && isset( $response->sections['changelog'] ) ) {
242
-            echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
243
-        }
241
+		if ( $response && isset( $response->sections['changelog'] ) ) {
242
+			echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>';
243
+		}
244 244
 
245
-        exit;
246
-    }
245
+		exit;
246
+	}
247 247
 }
Please login to merge, or discard this patch.