Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 103 lines (73 sloc) 2.798 kB
ac1e038 @jeremyclark13 Intial version of self hosted plugin and theme update scripts
authored
1 <?php
2 /*
3 Plugin Name: Test Plugin Update
4 Plugin URI: http://clark-technet.com
5 Description: Test plugin updates
6 Version: 0.9
7 Author: Jeremy Clark
8 Author URI: http://clark-technet.com
9 */
10
11
12 /*
13 // TEMP: Enable update check on every request. Normally you don't need this! This is for testing only!
14 set_site_transient('update_plugins', null);
15
16 // TEMP: Show which variables are being requested when query plugin API
17 add_filter('plugins_api_result', 'aaa_result', 10, 3);
18 function aaa_result($res, $action, $args) {
19 print_r($res);
20 return $res;
21 }
22 */
23
24
25 $api_url = 'http://api.damb.is/';
26 $plugin_slug = basename(dirname(__FILE__));
27
28
29 // Take over the update check
30 add_filter('pre_set_site_transient_update_plugins', 'check_for_plugin_update');
31
32 function check_for_plugin_update($checked_data) {
33 global $api_url, $plugin_slug;
34
35 if (empty($checked_data->checked))
36 return $checked_data;
37
38 $request_args = array(
39 'slug' => $plugin_slug,
40 'version' => $checked_data->checked[$plugin_slug .'/'. $plugin_slug .'.php'],
41 );
42
43 $request_string = prepare_request('basic_check', $request_args);
44
45 // Start checking for an update
46 $raw_response = wp_remote_post($api_url, $request_string);
47
48 if (!is_wp_error($raw_response) && ($raw_response['response']['code'] == 200))
49 $response = unserialize($raw_response['body']);
50
51 if (is_object($response) && !empty($response)) // Feed the update data into WP updater
52 $checked_data->response[$plugin_slug .'/'. $plugin_slug .'.php'] = $response;
53
54 return $checked_data;
55 }
56
57
58 // Take over the Plugin info screen
59 add_filter('plugins_api', 'my_plugin_api_call', 10, 3);
60
61 function my_plugin_api_call($def, $action, $args) {
62 global $plugin_slug, $api_url;
63
64 if ($args->slug != $plugin_slug)
65 return false;
66
67 // Get the current version
68 $plugin_info = get_site_transient('update_plugins');
69 $current_version = $plugin_info->checked[$plugin_slug .'/'. $plugin_slug .'.php'];
70 $args->version = $current_version;
71
72 $request_string = prepare_request($action, $args);
73
74 $request = wp_remote_post($api_url, $request_string);
75
76 if (is_wp_error($request)) {
77 $res = new WP_Error('plugins_api_failed', __('An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>'), $request->get_error_message());
78 } else {
79 $res = unserialize($request['body']);
80
81 if ($res === false)
82 $res = new WP_Error('plugins_api_failed', __('An unknown error occurred'), $request['body']);
83 }
84
85 return $res;
86 }
87
88
89 function prepare_request($action, $args) {
90 global $wp_version;
91
92 return array(
93 'body' => array(
94 'action' => $action,
95 'request' => serialize($args),
96 'api-key' => md5(get_bloginfo('url'))
97 ),
98 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
99 );
100 }
101
102 ?>
Something went wrong with that request. Please try again.