view. */ public const ARCHIVE = ['1.0' => 'whats-new-1.0.php']; /** * The release whose news this site has had (VERSION when it was installed with it). A site where it is older or * missing was updated: it gets the notice once. */ public const SEEN_OPTION = Plugin::PREFIX_ . 'whats_new_version'; /** Set when an update needs the notice (the version it is for); removed when it was seen or dismissed. */ public const SHOW_OPTION = Plugin::PREFIX_ . 'show_whats_new'; public const DISMISS_ACTION = Plugin::PREFIX_ . 'dismiss_whats_new'; public const SCREEN = 'admin_page_' . self::SLUG; public static function register(): void { add_action('admin_init', [self::class, 'checkRelease']); add_action('admin_notices', [self::class, 'notice']); add_action('wp_ajax_' . self::DISMISS_ACTION, [self::class, 'dismiss']); add_action('admin_enqueue_scripts', [self::class, 'enqueue']); } /** * @param string $version Empty for the latest release, or a key of ARCHIVE. */ public static function url(string $version = ''): string { return admin_url('admin.php?page=' . self::SLUG . ($version !== '' ? '&version=' . rawurlencode($version) : '')); } /** * A site that has not had the news of this release yet (an update from 1.0 or older) gets the notice once. A * fresh install has SEEN_OPTION already (Plugin::addOptions()). */ public static function checkRelease(): void { if (get_option(self::SEEN_OPTION) === self::VERSION) { return; } update_option(self::SEEN_OPTION, self::VERSION, false); self::remember(); } /** * An update from an older version: show the notice. */ public static function remember(): void { update_option(self::SHOW_OPTION, self::VERSION, false); } public static function forget(): void { delete_option(self::SHOW_OPTION); } public static function enqueue(): void { $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (!$screen || $screen->id !== self::SCREEN) { return; } wp_enqueue_style( Plugin::PREFIX . 'whats-new', Plugin::assetsUrl() . 'css/aop-whats-new.css', [], Enqueue::version('css/aop-whats-new.css') ); // The code example in the colors of the code editor of WordPress (CodeMirror, as in the plugin editor): // colored once with its runMode, it stays a plain block of text. False when the user switched syntax // highlighting off in the profile; then it stays plain. if (wp_enqueue_code_editor(['type' => 'application/x-httpd-php'])) { wp_add_inline_script( 'wp-codemirror', 'document.addEventListener("DOMContentLoaded",function(){document.querySelectorAll(".aop-whats-new__code code").forEach(function(code){var text=code.textContent;code.textContent="";window.wp.CodeMirror.runMode(text,{name:"php",startOpen:true},code);code.parentNode.classList.add("cm-s-default");});});' ); } } /** * The notice after an update, for administrators, only where it belongs: the dashboard, the list of plugins and * the screens of this plugin (not on this page itself). WordPress.org asks plugins not to take over the admin. */ public static function notice(): void { if (!get_option(self::SHOW_OPTION) || !current_user_can('manage_options') || !self::isNoticeScreen()) { return; } printf( '
%3$s %4$s