class-theme-update-manager.php
3.76 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
<?php
if( class_exists( 'Yoast_Update_Manager' ) && ! class_exists( "Yoast_Theme_Update_Manager", false ) ) {
class Yoast_Theme_Update_Manager extends Yoast_Update_Manager {
/**
* Constructor
*
* @param string $api_url
* @param string $item_name
* @param string $license_key
* @param string $slug
* @param string $theme_version
* @param string $author (optional)
*/
public function __construct( Yoast_Product $product, $license_key ) {
parent::__construct( $product, $license_key );
// setup hooks
$this->setup_hooks();
}
/**
* Get the current theme version
*
* @return string The version number
*/
private function get_theme_version() {
// if version was not set, get it from the Theme stylesheet
if( $this->product->get_version() === '' ) {
$theme = wp_get_theme( $this->product->get_slug() );
return $theme->get( 'Version' );
}
return $this->product->get_version();
}
/**
* Setup hooks
*/
private function setup_hooks() {
add_filter( 'site_transient_update_themes', array( $this, 'set_theme_update_transient' ) );
add_action( 'load-themes.php', array( $this, 'load_themes_screen' ) );
}
/**
* Set "updates available" transient
*/
public function set_theme_update_transient( $value ) {
$update_data = $this->get_update_data();
if( $update_data === false ) {
return $value;
}
// add update data to "updates available" array. convert object to array.
$value->response[ $this->product->get_slug() ] = (array) $update_data;
return $value;
}
/**
* Add hooks and scripts to the Appearance > Themes screen
*/
public function load_themes_screen() {
$update_data = $this->get_update_data();
// only do if an update is available
if( $update_data === false ) {
return;
}
add_thickbox();
add_action( 'admin_notices', array( $this, 'show_update_details' ) );
}
/**
* Show update link.
* Opens Thickbox with Changelog.
*/
public function show_update_details() {
$update_data = $this->get_update_data();
// only show if an update is available
if( $update_data === false ) {
return;
}
$update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_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;"';
?>
<div id="update-nag">
<?php
printf(
__( '<strong>%s version %s</strong> is available. <a href="%s" class="thickbox" title="%s">Check out what\'s new</a> or <a href="%s" %s>update now</a>.' ),
$this->product->get_item_name(),
$update_data->new_version,
'#TB_inline?width=640&inlineId=' . $this->product->get_slug() . '_changelog',
$this->get_item_name(),
$update_url,
$update_onclick
);
?>
</div>
<div id="<?php echo $this->product->get_slug(); ?>_changelog" style="display: none;">
<?php echo wpautop( $update_data->sections['changelog'] ); ?>
</div>
<?php
}
/**
* Get update data
*
* This gets the update data from a transient (12 hours), if set.
* If not, it will make a remote request and get the update data.
*
* @return object $update_data Object containing the update data
*/
public function get_update_data() {
$api_response = $this->get_remote_data();
if( false === $api_response ) {
return false;
}
$update_data = $api_response;
// check if a new version is available.
if ( version_compare( $this->get_theme_version(), $update_data->new_version, '>=' ) ) {
return false;
}
// an update is available
return $update_data;
}
}
}