This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
/
EDD_SL_Theme_Updater.php
executable file
·144 lines (117 loc) · 4.52 KB
/
EDD_SL_Theme_Updater.php
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
<?php
class EDD_SL_Theme_Updater {
private $remote_api_url;
private $request_data;
private $response_key;
private $theme_slug;
private $license_key;
private $version;
private $author;
function __construct( $args = array() ) {
$args = wp_parse_args( $args, array(
'remote_api_url' => 'https://easydigitaldownloads.com',
'request_data' => array(),
'theme_slug' => get_template(),
'item_name' => '',
'license' => '',
'version' => '',
'author' => ''
) );
extract( $args );
$theme = wp_get_theme( sanitize_key( $theme_slug ) );
$this->license = $license;
$this->item_name = $item_name;
$this->version = ! empty( $version ) ? $version : $theme->get( 'Version' );
$this->theme_slug = sanitize_key( $theme_slug );
$this->author = $author;
$this->remote_api_url = $remote_api_url;
$this->response_key = $this->theme_slug . '-update-response';
add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) );
add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) );
add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) );
}
function load_themes_screen() {
add_thickbox();
add_action( 'admin_notices', array( &$this, 'update_nag' ) );
}
function update_nag() {
$theme = wp_get_theme( $this->theme_slug );
$api_response = get_transient( $this->response_key );
if( false === $api_response )
return;
$update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->theme_slug ), 'upgrade-theme_' . $this->theme_slug );
$update_onclick = ' onclick="if ( confirm(\'' . esc_js( __( "Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update." ) ) . '\') ) {return true;}return false;"';
if ( version_compare( $this->version, $api_response->new_version, '<' ) ) {
echo '<div id="update-nag">';
printf( '<strong>%1$s %2$s</strong> is available. <a href="%3$s" class="thickbox" title="%4s">Check out what\'s new</a> or <a href="%5$s"%6$s>update now</a>.',
$theme->get( 'Name' ),
$api_response->new_version,
'#TB_inline?width=640&inlineId=' . $this->theme_slug . '_changelog',
$theme->get( 'Name' ),
$update_url,
$update_onclick
);
echo '</div>';
echo '<div id="' . $this->theme_slug . '_' . 'changelog" style="display:none;">';
echo wpautop( $api_response->sections['changelog'] );
echo '</div>';
}
}
function theme_update_transient( $value ) {
$update_data = $this->check_for_update();
if ( $update_data ) {
$value->response[ $this->theme_slug ] = $update_data;
}
return $value;
}
function delete_theme_update_transient() {
delete_transient( $this->response_key );
}
function check_for_update() {
$theme = wp_get_theme( $this->theme_slug );
$update_data = get_transient( $this->response_key );
if ( false === $update_data ) {
$failed = false;
if( empty( $this->license ) )
return false;
$api_params = array(
'edd_action' => 'get_version',
'license' => $this->license,
'name' => $this->item_name,
'slug' => $this->theme_slug,
'author' => $this->author,
'url' => home_url()
);
$response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// make sure the response was successful
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
$failed = true;
}
$update_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! is_object( $update_data ) ) {
$failed = true;
}
// if the response failed, try again in 30 minutes
if ( $failed ) {
$data = new stdClass;
$data->new_version = $this->version;
set_transient( $this->response_key, $data, strtotime( '+30 minutes' ) );
return false;
}
// if the status is 'ok', return the update arguments
if ( ! $failed ) {
$update_data->sections = maybe_unserialize( $update_data->sections );
set_transient( $this->response_key, $update_data, strtotime( '+12 hours' ) );
}
}
if( ! isset( $update_data->new_version ) ) {
return false;
}
if ( version_compare( $this->version, $update_data->new_version, '>=' ) ) {
return false;
}
return (array) $update_data;
}
}