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
163
164
165
|
<?php
if (!class_exists('DreamTeam_Updater')) { class DreamTeam_Updater { public $plugin_slug; public $plugin; public $version; public $cache_key; public $cache_allowed; public $_req_url;
public function __construct($args) { if ( !is_admin() ) { return false; } $this->plugin_slug = !empty($args['plugin_slug'])? $args['plugin_slug'] : false; $this->plugin = !empty($args['plugin'])? $args['plugin'] : false; $this->version = !empty($args['version'])? $args['version'] : false;
if (!$this->plugin_slug || !$this->plugin || !$this->version) { return; }
$this->cache_key = str_replace('-', '_', $this->plugin_slug . '_update_checker'); $this->cache_allowed = true; $this->_req_url = 'https://dreamteam-src.s3.us-east-1.amazonaws.com/wp_plugins/' . $this->plugin_slug . '.json';
add_filter( 'plugins_api', array($this, 'info'), 20, 3 ); add_filter( 'site_transient_update_plugins', array($this, 'update') ); add_action( 'upgrader_process_complete', array($this, 'purge'), 10, 2 ); add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
$this->ajax_functions(); }
public function isPluginPage() { return strpos($_SERVER['REQUEST_URI'], 'wp-admin/plugins.php') !== false; }
public function request() { $remote = get_transient( $this->cache_key ); if (false === $remote || (!$this->cache_allowed && $this->isPluginPage())) { $remote = wp_remote_get( $this->_req_url, [ 'timeout' => 10, 'headers' => [ 'Accept' => 'application/json' ] ] );
if (is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) || empty( wp_remote_retrieve_body( $remote ) )) { return false; } set_transient( $this->cache_key, $remote, DAY_IN_SECONDS ); } $remote = json_decode( wp_remote_retrieve_body( $remote ) ); return $remote; }
function info($res, $action, $args) { if ( 'plugin_information' !== $action ) { return $res; }
// do nothing if it is not our plugin if ( $this->plugin_slug !== $args->slug ) { return $res; }
// get updates $remote = $this->request();
$res = new stdClass(); $res->name = $remote->name; $res->slug = $remote->slug; $res->version = $remote->version; $res->tested = $remote->tested; $res->requires = $remote->requires; $res->author = $remote->author; $res->author_profile = $remote->author_profile; $res->download_link = $remote->download_url; $res->trunk = $remote->download_url; $res->requires_php = $remote->requires_php; $res->last_updated = $remote->last_updated;
$res->sections = [ 'description' => $remote->sections->description, 'installation' => $remote->sections->installation, 'changelog' => $remote->sections->changelog ]; if ( ! empty( $remote->banners ) ) { $res->banners = [ 'low' => $remote->banners->low, 'high' => $remote->banners->high ]; }
return $res; }
public function update($transient) { if ( empty( $transient->checked ) ) { return $transient; }
$remote = $this->request();
if ( $remote && version_compare( $this->version, $remote->version, '<' ) && version_compare( $remote->requires, get_bloginfo( 'version' ), '<' ) && version_compare( $remote->requires_php, PHP_VERSION, '<' ) ) { $res = new stdClass(); $res->slug = $this->plugin_slug; $res->plugin = $this->plugin; $res->new_version = $remote->version; $res->tested = $remote->tested; $res->package = $remote->download_url;
$transient->response[ $res->plugin ] = $res; }
return $transient; }
public function purge($upgrader_object, $options) { if ( $this->cache_allowed && 'update' === $options['action'] && 'plugin' === $options['type'] ) { // just clean the cache when new plugin version is installed delete_transient( $this->cache_key ); } }
public function plugin_row_meta( $plugin_meta, $plugin_file) { if ( $this->plugin !== $plugin_file ) { return $plugin_meta; }
$key = $this->plugin_slug . '-get-update'; $callback = str_replace('-', '_', $key); $row_meta = array(); $row_meta[$key] = '<a class="dream-team-ajax-link" href="' . get_admin_url(null, 'admin-ajax.php?action='.$callback.'&security='.wp_create_nonce( 'dream-team' )) . '" aria-label="' . esc_attr__( 'Manual get Update Info', 'dream-team' ) . '">' . esc_html__( 'Get Update Info', 'dream-team' ) . '</a>'; return array_merge( $plugin_meta, $row_meta ); }
public function ajax_functions() { $callback = str_replace('-', '_', $this->plugin_slug . '_get_update'); add_action( 'wp_ajax_' . $callback, array($this, 'clear_update_info') ); }
public function clear_update_info() { check_ajax_referer( 'dream-team', 'security' ); delete_transient($this->cache_key); $checkData = get_transient($this->cache_key); wp_send_json(array( 'status' => 'success', 'body' => $checkData, 'slug' => $this->cache_key )); } } }
|