| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle theme update notice |
| 4 |
*/ |
| 5 |
class WPStream_Theme_Notice { |
| 6 |
|
| 7 |
private $theme_slug = 'hello-wpstream'; |
| 8 |
private $notice_option = 'wpstream_theme_notice_dismissed'; |
| 9 |
private $nonce_action = 'wpstream_dismiss_notice'; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
add_action( 'admin_notices', array( $this, 'display_admin_notice' ) ); |
| 13 |
} |
| 14 |
|
| 15 |
public function display_admin_notice() { |
| 16 |
if ( !$this->should_display_notice() ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$theme_data = $this->get_theme_update_data(); |
| 21 |
if ( !$theme_data ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$this->render_notice( $theme_data ); |
| 26 |
} |
| 27 |
|
| 28 |
private function should_display_notice() { |
| 29 |
return !get_option( $this->notice_option ) && |
| 30 |
current_user_can( 'update_themes' ) && |
| 31 |
!wp_doing_ajax(); |
| 32 |
} |
| 33 |
|
| 34 |
private function get_theme_update_data() { |
| 35 |
$theme = wp_get_theme( $this->theme_slug ); |
| 36 |
if ( !$theme->exists() ) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
$updates = get_site_transient( 'update_themes' ); |
| 41 |
if ( !isset( $updates->response[$this->theme_slug] ) ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
return array( |
| 46 |
'current_version' => $theme->get( 'Version' ), |
| 47 |
'new_version' => $updates->response[$this->theme_slug]['new_version'], |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
public function render_notice( $data ) { |
| 53 |
include plugin_dir_path( __FILE__ ) . 'templates/wpstream-theme-update-notice.php'; |
| 54 |
} |
| 55 |
} |