class-itthinx-updates-bridge.php
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
class Itthinx_Updates_Bridge {
const UPDATE_SERVICE_URL = 'http://service.itthinx.com/update-service.php';
const PRIORITY_UPDATE_PLUGINS = 999;
const PRIORITY_PLUGINS_API = 999;
private $version = null;
private $file = null;
private $plugin = null;
private $basename = null;
private $service_key = null;
public function __construct( $file ) {
$this->file = $file;
$this->plugin = plugin_basename( $file );
$this->basename = basename( $file, '.php' );
if ( !function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( $this->file );
if ( isset( $plugin_data['Version'] ) ) {
$this->version = $plugin_data['Version'];
}
if ( $this->version ) {
$this->service_key = Itthinx_Updates::get_service_key();
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'pre_set_site_transient_update_plugins' ), self::PRIORITY_UPDATE_PLUGINS );
add_filter( 'plugins_api', array( $this, 'plugins_api'), self::PRIORITY_PLUGINS_API, 3 );
}
}
/**
* Adds the plugin info to the update_plugins transient if a new version is available.
* @param array $value update_plugins transient
* @return (possibly modified) update_plugins transient
*/
public function pre_set_site_transient_update_plugins( $value ) {
$info = $this->get_info();
if ( $info ) {
if ( isset( $info->new_version ) && ( version_compare( $this->version , $info->new_version ) < 0 ) ) {
$value->response[$this->plugin] = $info;
}
}
return $value;
}
/**
* Returns plugin info when requested for this plugin, $result otherwise.
* @param object|boolean $result
* @param string $action
* @param array $args
* @return object|boolean plugin info for this plugin if requested, $result otherwise
*/
public function plugins_api( $result, $action, $args ) {
if ( $action == 'plugin_information' ) {
if ( $args->slug === dirname( $this->plugin ) ) {
$result = false;
$info = $this->get_info();
if ( $info ) {
$result = $info;
}
}
}
return $result;
}
/**
* Retrieves plugin information from update server.
* @return object plugin information when successfully retrieved, null otherwise
*/
public function get_info() {
$result = null;
$request = wp_remote_post(
self::UPDATE_SERVICE_URL,
array(
'body' => array(
'action' => 'info',
'plugin' => $this->basename,
'service_key' => $this->service_key
)
)
);
if ( !is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) === 200) {
$result = unserialize( $request['body'] );
if ( isset( $result->download_link ) ) {
if ( !empty( $this->service_key ) ) {
$sep = '?';
if ( parse_url( $result->download_link, PHP_URL_QUERY ) ) {
$sep = '&';
}
$result->download_link .= $sep . 'service_key=' . $this->service_key;
if ( !isset( $result->package ) ) {
$result->package = $result->download_link;
} else {
$result->package .= $sep . 'service_key=' . $this->service_key;
}
}
}
}
return $result;
}
/**
* Retrieves plugin list.
* @return array or null
*/
public static function get_plugins() {
$result = null;
$request = wp_remote_post(
self::UPDATE_SERVICE_URL,
array(
'body' => array(
'action' => 'plugins'
)
)
);
if ( !is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) === 200) {
$result = unserialize( $request['body'] );
}
return $result;
}
/**
* Plugin info request.
* @param string $plugin
* @return object plugin information when successfully retrieved, null otherwise
*/
public static function get_plugin_info( $plugin ) {
$result = null;
$service_key = Itthinx_Updates::get_service_key();
$request = wp_remote_post(
self::UPDATE_SERVICE_URL,
array(
'body' => array(
'action' => 'info',
'plugin' => $plugin,
'service_key' => $service_key
)
)
);
if ( !is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) === 200) {
$result = unserialize( $request['body'] );
if ( isset( $result->download_link ) ) {
if ( !empty( $service_key ) ) {
$sep = '?';
if ( parse_url( $result->download_link, PHP_URL_QUERY ) ) {
$sep = '&';
}
$result->download_link .= $sep . 'service_key=' . $service_key;
if ( !isset( $result->package ) ) {
$result->package = $result->download_link;
} else {
$result->package .= $sep . 'service_key=' . $service_key;
}
}
}
}
return $result;
}
}
// Itthinx_Updates_Bridge::init();