Skip to content
Snippets Groups Projects
Select Git revision
  • 10f426a4f677fe22cbd5437137f29ccdd388d090
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.2
  • 2.3.1
  • 2.3
25 results

BasicField.php

Blame
  • Notice.php 4.38 KiB
    <?php
    
    namespace WPDesk\Notice;
    
    /**
     * Class Notice
     *
     * WordPress admin notice.
     * @package WPDesk\Notice
     */
    class Notice
    {
    
        const NOTICE_TYPE_ERROR = 'error';
        const NOTICE_TYPE_WARNING = 'warning';
        const NOTICE_TYPE_SUCCESS = 'success';
        const NOTICE_TYPE_INFO = 'info';
    
        /**
         * Notice type.
         *
         * @var string
         */
        protected $noticeType;
    
        /**
         * Notice content.
         *
         * @var string
         */
        protected $noticeContent;
    
        /**
         * Is dismissible.
         *
         * @var bool
         */
        protected $dismissible;
    
        /**
         * Notice hook priority.
         * @var int;
         */
        protected $priority;
    
        /**
         * Is action added?
         * @var bool
         */
        private $actionAdded = false;
    
        /**
         * Attributes.
         *
         * @var string[]
         */
        protected $attributes = array();
    
    
        /**
         * WPDesk_Flexible_Shipping_Notice constructor.
         *
         * @param string $noticeContent Notice content.
         * @param string $noticeType Notice type.
         * @param bool $dismissible Is dismissible.
         * @param int $priority Notice priority.
         * @param array $attributes Attributes.
         */
        public function __construct(
            $noticeContent,
            $noticeType = 'info',
            $dismissible = false,
            $priority = 10,
            $attributes = array()
        ) {
            $this->noticeContent = $noticeContent;
            $this->noticeType    = $noticeType;
            $this->dismissible   = $dismissible;
            $this->priority      = $priority;
            $this->attributes    = $attributes;
            $this->addAction();
        }
    
        /**
         * @return string
         */
        public function getNoticeContent()
        {
            return $this->noticeContent;
        }
    
        /**
         * @param string $noticeContent
         */
        public function setNoticeContent($noticeContent)
        {
            $this->noticeContent = $noticeContent;
        }
    
        /**
         * @return string
         */
        public function getNoticeType()
        {
            return $this->noticeType;
        }
    
        /**
         * @param string $noticeType
         */
        public function setNoticeType($noticeType)
        {
            $this->noticeType = $noticeType;
        }
    
        /**
         * @return bool
         */
        public function isDismissible()
        {
            return $this->dismissible;
        }
    
        /**
         * @param bool $dismissible
         */
        public function setDismissible($dismissible)
        {
            $this->dismissible = $dismissible;
        }
    
        /**
         * @return int
         */
        public function getPriority()
        {
            return $this->priority;
        }
    
        /**
         * @param int $priority
         */
        public function setPriority($priority)
        {
            $this->priority = $priority;
            if ($this->actionAdded) {
                $this->removeAction();
                $this->addAction();
            }
        }
    
        /**
         * Add notice action.
         */
        protected function addAction()
        {
            if (!$this->actionAdded) {
                add_action('admin_notices', [$this, 'showNotice'], $this->priority);
                $this->actionAdded = true;
            }
        }
    
        protected function removeAction()
        {
            if ($this->actionAdded) {
                remove_action('admin_notices', [$this, 'showNotice'], $this->priority);
                $this->actionAdded = false;
            }
        }
    
        /**
         * Get notice class.
         *
         * @return string
         */
        protected function getNoticeClass()
        {
            if ('updated' === $this->noticeType) {
                $notice_class = 'notice ' . $this->noticeType;
            } else {
                $notice_class = 'notice notice-' . $this->noticeType;
            }
            if ($this->dismissible) {
                $notice_class .= ' is-dismissible';
            }
            if (isset($this->attributes['class'])) {
                $notice_class .= $this->attributes['class'];
            }
            return $notice_class;
        }
    
        /**
         * Get attributes as string.
         *
         * @return string
         */
        protected function getAttributesAsString()
        {
            $attribute_string = sprintf('class="%1$s"', esc_attr($this->getNoticeClass()));
            foreach ($this->attributes as $attribute_name => $attribute_value) {
                $attribute_string .= sprintf(' %1$s="%2$s"', esc_html($attribute_name), esc_attr($attribute_value));
            }
            return $attribute_string;
        }
    
        /**
         * Show notice;
         */
        public function showNotice()
        {
            echo sprintf('<div %1$s><p>%2$s</p></div>', $this->getAttributesAsString(), $this->noticeContent);
        }
    
    }