]> _ Git - couzy.git/blob
fa8093368ed54bb8665faa860e979be3c95ab42e
[couzy.git] /
1 <?php
2
3 if( class_exists( 'Yoast_Update_Manager' ) && ! class_exists( "Yoast_Theme_Update_Manager", false ) ) {
4
5         class Yoast_Theme_Update_Manager extends Yoast_Update_Manager {
6
7                 /**
8                 * Constructor
9                 *
10                 * @param string $api_url
11                 * @param string $item_name
12                 * @param string $license_key
13                 * @param string $slug
14                 * @param string $theme_version
15                 * @param string $author (optional)
16                 */
17                 public function __construct( Yoast_Product $product, $license_key ) {
18                         
19                         parent::__construct( $product, $license_key );
20
21                         // setup hooks
22                         $this->setup_hooks();
23                 }
24
25                 /**
26                 * Get the current theme version
27                 *
28                 * @return string The version number
29                 */
30                 private function get_theme_version() {
31
32                         // if version was not set, get it from the Theme stylesheet
33                         if( $this->product->get_version() === '' ) {
34                                 $theme = wp_get_theme( $this->product->get_slug() );
35                                 return $theme->get( 'Version' );
36                         }
37
38                         return $this->product->get_version();
39                 }
40
41                 /**
42                 * Setup hooks
43                 */
44                 private function setup_hooks() {
45                         add_filter( 'site_transient_update_themes', array( $this, 'set_theme_update_transient' ) );
46                         add_action( 'load-themes.php', array( $this, 'load_themes_screen' ) );
47                 }
48
49                 /**
50                 * Set "updates available" transient
51                 */
52                 public function set_theme_update_transient( $value ) {
53
54                         $update_data = $this->get_update_data();
55
56                         if( $update_data === false ) {
57                                 return $value;
58                         }
59                                 
60                         // add update data to "updates available" array. convert object to array.
61                         $value->response[ $this->product->get_slug() ] = (array) $update_data;
62
63                         return $value;
64                 }
65
66                 /**
67                 * Add hooks and scripts to the Appearance > Themes screen
68                 */
69                 public function load_themes_screen() {
70
71                         $update_data = $this->get_update_data();
72
73                         // only do if an update is available
74                         if( $update_data === false ) {
75                                 return;
76                         }
77
78                         add_thickbox();
79                         add_action( 'admin_notices', array( $this, 'show_update_details' ) );
80                 }
81
82                 /**
83                 * Show update link. 
84                 * Opens Thickbox with Changelog.
85                 */
86                 public function show_update_details() {
87                         
88                         $update_data = $this->get_update_data();
89
90                         // only show if an update is available
91                         if( $update_data === false ) {
92                                 return;
93                         }
94
95                         $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() );
96                         $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;"';
97                         ?>
98                         <div id="update-nag">
99                                 <?php
100                                         printf( 
101                                                 __( '<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>.' ),
102                                         $this->product->get_item_name(),
103                                                 $update_data->new_version,
104                                         '#TB_inline?width=640&amp;inlineId=' . $this->product->get_slug() . '_changelog',
105                                         $this->get_item_name(),
106                                                 $update_url,
107                                                 $update_onclick
108                                         );
109                                 ?>
110                         </div>
111                         <div id="<?php echo $this->product->get_slug(); ?>_changelog" style="display: none;">
112                                 <?php echo wpautop( $update_data->sections['changelog'] ); ?>
113                         </div>  
114                         <?php
115                 }
116
117                 
118                 /**
119                 * Get update data
120                 *
121                 * This gets the update data from a transient (12 hours), if set.
122                 * If not, it will make a remote request and get the update data.
123                 *
124                 * @return object $update_data Object containing the update data
125                 */
126                 public function get_update_data() {
127
128                         $api_response = $this->get_remote_data();
129
130                         if( false === $api_response ) {
131                                 return false;
132                         }
133
134                         $update_data = $api_response;
135
136                         // check if a new version is available. 
137                         if ( version_compare( $this->get_theme_version(), $update_data->new_version, '>=' ) ) {
138                                 return false;
139                         }
140
141
142                         // an update is available
143                         return $update_data;
144                 }
145
146
147         }
148
149 }