| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin notice that tells the site owner when the bundled "hello-wpstream" |
| 4 |
* companion theme has an update available. |
| 5 |
* |
| 6 |
* The class hooks into `admin_notices`, decides whether a notice is warranted |
| 7 |
* (permission + not already dismissed + an update actually pending), gathers |
| 8 |
* the current/new version numbers from the theme update transient, and renders |
| 9 |
* the notice via a template partial. |
| 10 |
* |
| 11 |
* @package Wpstream |
| 12 |
* @subpackage Wpstream/includes |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Handles rendering of the "hello-wpstream theme update available" admin notice. |
| 23 |
*/ |
| 24 |
class WPStream_Theme_Notice { |
| 25 |
|
| 26 |
/** @var string Slug of the companion theme whose updates we announce. */ |
| 27 |
private $theme_slug = 'hello-wpstream'; |
| 28 |
|
| 29 |
/** @var string Option key that records the notice was dismissed by the user. */ |
| 30 |
private $notice_option = 'wpstream_theme_notice_dismissed'; |
| 31 |
|
| 32 |
/** @var string Nonce action used when the notice is dismissed. */ |
| 33 |
private $nonce_action = 'wpstream_dismiss_notice'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Register the notice on the admin_notices hook. |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
// Print the notice (if applicable) whenever WordPress renders admin notices. |
| 40 |
add_action( 'admin_notices', array( $this, 'display_admin_notice' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Gatekeeper callback for admin_notices: render the notice only when it is |
| 45 |
* eligible and there is genuine update data to show. |
| 46 |
*/ |
| 47 |
public function display_admin_notice() { |
| 48 |
// Bail early if the notice should not be shown in the current context. |
| 49 |
if ( !$this->should_display_notice() ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Fetch the theme's current/new version pair; false means no update pending. |
| 54 |
$theme_data = $this->get_theme_update_data(); |
| 55 |
if ( !$theme_data ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// All checks passed, so output the notice markup. |
| 60 |
$this->render_notice( $theme_data ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Decide whether the notice is allowed to appear. |
| 65 |
* |
| 66 |
* @return bool True when not previously dismissed, the user can update |
| 67 |
* themes, and this is not an AJAX request. |
| 68 |
*/ |
| 69 |
private function should_display_notice() { |
| 70 |
return !get_option( $this->notice_option ) && // not dismissed before |
| 71 |
current_user_can( 'update_themes' ) && // user has the capability |
| 72 |
!wp_doing_ajax(); // not during an AJAX call |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Resolve the companion theme's installed and available versions. |
| 77 |
* |
| 78 |
* @return array|false Array with 'current_version'/'new_version', or false |
| 79 |
* if the theme is missing or has no pending update. |
| 80 |
*/ |
| 81 |
private function get_theme_update_data() { |
| 82 |
// The companion theme must actually be installed on the site. |
| 83 |
$theme = wp_get_theme( $this->theme_slug ); |
| 84 |
if ( !$theme->exists() ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
// WordPress caches pending theme updates in this site transient. |
| 89 |
$updates = get_site_transient( 'update_themes' ); |
| 90 |
if ( !isset( $updates->response[$this->theme_slug] ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
// Hand back the installed version and the version WordPress is offering. |
| 95 |
return array( |
| 96 |
'current_version' => $theme->get( 'Version' ), |
| 97 |
'new_version' => $updates->response[$this->theme_slug]['new_version'], |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Render the notice markup from its template partial. |
| 104 |
* |
| 105 |
* @param array $data Version data made available to the included template. |
| 106 |
*/ |
| 107 |
public function render_notice( $data ) { |
| 108 |
// $data is in scope for the template to print current/new versions. |
| 109 |
include plugin_dir_path( dirname( __FILE__ ) ) . 'templates/admin/wpstream-theme-update-notice.php'; |
| 110 |
} |
| 111 |
} |