├── composer.json ├── src ├── css │ └── style.css ├── js │ └── common.js ├── functions.php └── Caldera_Warnings_Dismissible_Notice.php └── README.md /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calderawp/dismissible-notice", 3 | "license": "GPL v2+", 4 | "description" : "Creates a dismissible--via AJAX--admin nag.", 5 | "authors": [ 6 | { 7 | "name": "Josh Pollock", 8 | "email": "Josh@JoshPress.net" 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /src/css/style.css: -------------------------------------------------------------------------------- 1 | /* Copied from WordPress 4.2 */ 2 | .wp-core-ui .notice.is-dismissible { 3 | padding-right: 38px; 4 | position: relative; 5 | } 6 | 7 | .notice-dismiss:before { 8 | background: none; 9 | color: #b4b9be; 10 | content: '\f153'; 11 | display: block; 12 | font: normal 16px/20px 'dashicons'; 13 | speak: none; 14 | height: 20px; 15 | text-align: center; 16 | width: 20px; 17 | -webkit-font-smoothing: antialiased; 18 | -moz-osx-font-smoothing: grayscale; 19 | } 20 | .notice-dismiss { 21 | position: absolute; 22 | top: 0; 23 | right: 1px; 24 | border: none; 25 | margin: 0; 26 | padding: 9px; 27 | background: none; 28 | color: #b4b9be; 29 | cursor: pointer; 30 | } 31 | 32 | .notice-dismiss:hover:before, 33 | .notice-dismiss:active:before, 34 | .notice-dismiss:focus:before { 35 | color: #c00; 36 | } 37 | 38 | .notice-dismiss:focus { 39 | outline: none; 40 | -webkit-box-shadow: 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, .8); 41 | box-shadow: 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, .8); 42 | } 43 | 44 | .ie8 .notice-dismiss:focus { 45 | outline: 1px solid #5b9dd9; 46 | } 47 | 48 | @media screen and ( max-width: 782px ) { 49 | .notice-dismiss { 50 | padding: 13px; 51 | } 52 | .wp-core-ui .notice.is-dismissible { 53 | padding-right: 46px; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/js/common.js: -------------------------------------------------------------------------------- 1 | jQuery(document).ready(function($) { 2 | // Save dismiss state 3 | $( '.notice.is-dismissible' ).on('click', '.notice-dismiss', function ( event ) { 4 | event.preventDefault(); 5 | var $this = $(this); 6 | if( ! $this.parent().data( 'key' ) ){ 7 | return; 8 | } 9 | $.post( ajaxurl, { 10 | action: "caldera_warnings_dismissible_notice", 11 | url: ajaxurl, 12 | nag: $this.parent().data( 'key' ), 13 | nonce: caldera_commonL10n.nonce || '' 14 | }); 15 | 16 | }); 17 | 18 | // Make notices dismissible - backward compatabity -4.2 - copied from WordPress 4.2 19 | $( '.notice.is-dismissible' ).each( function() { 20 | if( caldera_commonL10n.wp_version ){ 21 | return; 22 | } 23 | 24 | var $this = $( this ), 25 | $button = $( '' ), 26 | btnText = caldera_commonL10n.dismiss || ''; 27 | 28 | // Ensure plain text 29 | $button.find( '.screen-reader-text' ).text( btnText ); 30 | 31 | $this.append( $button ); 32 | 33 | $button.on( 'click.wp-dismiss-notice', function( event ) { 34 | event.preventDefault(); 35 | $this.fadeTo( 100 , 0, function() { 36 | $(this).slideUp( 100, function() { 37 | $(this).remove(); 38 | }); 39 | }); 40 | }); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A simple way to add an admin nag, that can be dismissed via AJAX. 2 | 3 | ### Example 4 | Show a warning if current version of PHP is less than 5.3. 5 | ```php 6 | add_action( 'plugins_loaded', 'caeq_bootstrap' ); 7 | function caeq_bootstrap(){ 8 | 9 | if ( ! version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { 10 | if ( is_admin() || defined( 'DOING_AJAX' ) && DOING_AJAX ) { 11 | include_once CAEQ_PATH . 'vendor/calderawp/dismissible-notice/src/callback_hook.php'; 12 | } 13 | 14 | if ( is_admin() ) { 15 | //BIG nope nope nope! 16 | 17 | $message = sprintf( __( 'Caldera Easy Queries requires PHP version %1s or later. We strongly recommend PHP 5.4 or later for security and performance reasons. Current version is %2s.', '5.3.0', 'caldera-text-domain' ), PHP_VERSION ); 18 | echo caldera_warnings_dismissible_notice( $message, true, 'activate_plugins' ); 19 | } 20 | 21 | }else{ 22 | //bootstrap plugin 23 | require_once( CAEQ_PATH . 'bootstrap.php' ); 24 | 25 | } 26 | 27 | } 28 | ``` 29 | 30 | ### Backwards Compat and such 31 | This library was written with PHP 5.2 backwards-compat in mind. Hence the prefixed, instead of namespaced class name. You will also need to manually include the class. 32 | 33 | 34 | ### License & Copyright 35 | Copyright 2015 [Josh Pollock](http://JoshPress.net) 36 | 37 | Licensed under the terms of the [GNU General Public License version 2](http://www.gnu.org/licenses/gpl-2.0.html) or later. Please share with your neighbor. 38 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | if ( ! function_exists( 'caldera_warnings_dismissible_notice_cb' ) ) { 12 | /** 13 | * Hook to AJAX 14 | * 15 | * @since 0.2.0 16 | */ 17 | add_action( 'wp_ajax_caldera_warnings_dismissible_notice', 'caldera_warnings_dismissible_notice_cb' ); 18 | function caldera_warnings_dismissible_notice_cb() { 19 | return Caldera_Warnings_Dismissible_Notice::ajax_cb(); 20 | } 21 | } 22 | 23 | if ( ! function_exists( 'caldera_warnings_dismissible_notice' ) ) { 24 | /** 25 | * Create a dismissible notice. 26 | * 27 | * @since 0.2.0 28 | * 29 | * @param string $message The text of the message. 30 | * @param bool $error Optional. Whether to show as error or update. Default is error. 31 | * @param string $cap_check Optional. Minimum user capability to show nag to. Default is "activate_plugins" 32 | * @param string|bool $ignore_key Optional. The user meta key to use for storing if this message has been dismissed by current user or not. If false, it will be generated. 33 | * 34 | * @return string|void Admin notice if is_admin() and not dismissed. 35 | */ 36 | function caldera_warnings_dismissible_notice( $message, $error = true, $cap_check = 'activate_plugins', $ignore_key = false ) { 37 | include_once dirname( __FILE__ ) . '/Caldera_Warnings_Dismissible_Notice.php'; 38 | 39 | return Caldera_Warnings_Dismissible_Notice::notice( $message, $error, $cap_check, $ignore_key ); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/Caldera_Warnings_Dismissible_Notice.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @copyright 2015 Josh Pollock 9 | */ 10 | /** 11 | * Version 0.1.0 12 | */ 13 | 14 | if ( class_exists( 'Caldera_Warnings_Dismissible_Notice' ) ) { 15 | return; 16 | 17 | } 18 | 19 | /** 20 | * Class Caldera_Warnings_Dismissible_Notice 21 | * 22 | * @package @Caldera_Warnings_Dismissible_Notice 23 | */ 24 | class Caldera_Warnings_Dismissible_Notice { 25 | 26 | /** 27 | * The action for the nonce 28 | * 29 | * @since 0.1.0 30 | * 31 | * @access protected 32 | * 33 | * @var string 34 | */ 35 | protected static $nonce_action = 'caldera_admin_nag'; 36 | 37 | /** 38 | * The counce field 39 | * 40 | * @since 0.3.0 41 | * 42 | * @access protected 43 | * 44 | * @var string 45 | */ 46 | protected static $nonce_field = ''; 47 | 48 | /** 49 | * The ignore key 50 | * 51 | * @since 0.3.0 52 | * 53 | * @access protected 54 | * 55 | * @var string 56 | */ 57 | protected static $ignore_key = ''; 58 | 59 | /** 60 | * Output the message 61 | * 62 | * @since 0.1.0 63 | * 64 | * @param string $message The text of the message. 65 | * @param bool $error Optional. Whether to show as error or update. Default is error. 66 | * @param string $cap_check Optional. Minimum user capability to show nag to. Default is "activate_plugins" 67 | * @param string|bool $ignore_key Optional. The user meta key to use for storing if this message has been dismissed by current user or not. If false, it will be generated. 68 | * 69 | * @return string|void Admin notice if is_admin() and not dismissed. 70 | */ 71 | public static function notice( $message, $error = true, $cap_check = 'activate_plugins', $ignore_key = false ) { 72 | if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) { 73 | if ( current_user_can( $cap_check ) ) { 74 | $user_id = get_current_user_id(); 75 | if ( ! is_string( $ignore_key ) ) { 76 | // cal_wd_ig_3911b2583433f696e5813a503bbb2e65 77 | $ignore_key = 'cal_wd_ig_' . substr( md5( $ignore_key ), 0, 40 ); 78 | } 79 | 80 | self::$ignore_key = sanitize_key( $ignore_key ); 81 | 82 | $dissmised = get_user_meta( $user_id, self::$ignore_key, true ); 83 | 84 | if ( ! $dissmised ) { 85 | if ( $error ) { 86 | $class = 'error'; 87 | } else { 88 | $class = 'updated'; 89 | } 90 | 91 | self::$nonce_field = wp_nonce_field( self::$nonce_action ); 92 | 93 | $out[] = sprintf( '
', self::$ignore_key, self::$ignore_key, $class ); 94 | $out[] = $message; 95 | $out[] = self::$nonce_field; 96 | $out[] = '