* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * @package PublishPress\WordpressVersionNotices * @category Core * @author PublishPress * @copyright Copyright (c) 2020 PublishPress. All rights reserved. **/ namespace PublishPress\WordpressVersionNotices\Module\TopNotice; use PublishPress\WordpressVersionNotices\Module\AdInterface; use PublishPress\WordpressVersionNotices\Template\TemplateInvalidArgumentsException; use PublishPress\WordpressVersionNotices\Template\TemplateLoaderInterface; /** * Class Module * * @package PublishPress\WordpressVersionNotices */ class Module implements AdInterface { const SETTINGS_FILTER = 'pp_version_notice_top_notice_settings'; const DISPLAY_ACTION = 'pp_version_notice_display_top_notice'; /** * @var TemplateLoaderInterface */ private $templateLoader; /** * @var array */ private $exceptions = []; /** * @var array */ private $settings = []; public function __construct(TemplateLoaderInterface $templateLoader) { $this->templateLoader = $templateLoader; } public function init() { add_action(self::DISPLAY_ACTION, [$this, 'display'], 10, 2); add_action('in_admin_header', [$this, 'displayTopNotice']); add_action('admin_init', [$this, 'collectTheSettings'], 50); add_action('admin_head', [$this, 'adminHeadAddStyle']); } public function collectTheSettings() { $this->settings = apply_filters(self::SETTINGS_FILTER, []); } /** * @param string $message * @param string $linkURL */ public function display($message = '', $linkURL = '') { try { if (empty($message) || empty($linkURL)) { throw new TemplateInvalidArgumentsException(); } $context = [ 'message' => $message, 'linkURL' => $linkURL ]; $this->templateLoader->displayOutput('top-notice', 'notice', $context); } catch (\Exception $e) { if (defined('WP_DEBUG') && WP_DEBUG) { $this->exceptions[] = $e->getMessage(); add_action('admin_notices', [$this, 'showNoticeWithException']); } } } /** * @return array|false */ private function isValidScreen() { $screen = get_current_screen(); if (!empty($screen)) { foreach ($this->settings as $pluginName => $setting) { if (!is_array($setting) || !isset($setting['screens'])) { continue; } foreach ($setting['screens'] as $screenParams) { if ($screenParams === true) { return $setting; } $validVars = 0; foreach ($screenParams as $var => $value) { if (isset($screen->$var) && $screen->$var === $value) { $validVars++; } } if ($validVars === count($screenParams)) { return $setting; } } } } return false; } public function adminHeadAddStyle() { if (! $this->isValidScreen()) { return; } ?> isValidScreen()) { do_action(self::DISPLAY_ACTION, $settings['message'], $settings['link']); } } public function showNoticeWithException() { $class = 'notice notice-error'; $message = implode("
", $this->exceptions); printf('

%2$s

', esc_attr($class), esc_html($message)); } }