class-affiliates-users-update.php
2.73 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
<?php
/**
* class-affiliates-users-update.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is provided subject to the license granted.
* Unauthorized use and distribution is prohibited.
* See COPYRIGHT.txt and LICENSE.txt
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates-users
* @since affiliates-users 1.3.0
*/
/**
* Update manager.
*/
class Affiliates_Users_Update {
const UPDATE_SERVICE_URL = 'http://service.itthinx.com/updates.php';
private static $version = AFFILIATES_USERS_PLUGIN_VERSION;
private static $file = AFFILIATES_USERS_PLUGIN_FILE;
private static $plugin = 'affiliates-users';
/**
* Update hooks.
*/
public static function init() {
add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'pre_set_site_transient_update_plugins' ) );
add_filter( 'plugins_api', array( __CLASS__, 'plugins_api'), 10, 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 static function pre_set_site_transient_update_plugins( $value ) {
$info = self::get_info();
if ( $info ) {
if ( isset( $info->new_version ) && ( version_compare( self::$version , $info->new_version ) < 0 ) ) {
$value->response[plugin_basename( self::$file )] = $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 static function plugins_api( $result, $action, $args ) {
if ( $action == 'plugin_information' ) {
if ( $args->slug === dirname( plugin_basename( self::$file ) ) ) {
$result = false;
$info = self::get_info();
if ( $info ) {
$result = $info;
}
}
}
return $result;
}
/**
* Retrieves plugin information from update server.
* @return object plugin information when successfully retrieved, null otherwise
*/
public static function get_info() {
$result = null;
$request = wp_remote_post(
self::UPDATE_SERVICE_URL,
array(
'body' => array(
'action' => 'info',
'plugin' => self::$plugin
)
)
);
if ( !is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200) {
$result = unserialize( $request['body'] );
}
return $result;
}
}
Affiliates_Users_Update::init();