Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created September 1, 2016 17:19
Show Gist options
  • Save lgedeon/065e7045a4acc4d22691a61f559e6c41 to your computer and use it in GitHub Desktop.
Save lgedeon/065e7045a4acc4d22691a61f559e6c41 to your computer and use it in GitHub Desktop.
Concept for handling multiple notification templates. We went with a generic template solution, but saving this in case it is needed later.
<?php
/**
* Send a template for handling notifications that other plugins can hook into.
*/
add_action( 'customize_controls_print_footer_scripts', function () {
?>
<script type="text/html" id="tmpl-customize-unified-notifications">
<ul>
<# _.each( data.notifications, function( notification ) { #>
<li class="notice notice-{{ notification.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ notification.code }}" data-type="{{ notification.type }}">
<?php do_action( 'customize_unified_notifications' ); ?>
</li>
<# } ); #>
</ul>
</script>
<?php
} );
/*
* Default message at priority 10. Use 9 and 11 to go before or after.
*/
add_action( 'customize_unified_notifications', function() {
?>
{{ notification.message || notification.code }}
<?php
} );
/*
* Example of usage in customize_concurrency.
*/
add_action( 'customize_unified_notifications', function() {
?>
<# if ( /concurrency_conflict/.test( notification.code ) ) { #>
<button class="button concurrency-conflict concurrency-conflict-accept" type="button"><span class="dashicons dashicons-thumbs-up"></span></button>
<button class="button concurrency-conflict concurrency-conflict-override" type="button"><span class="dashicons dashicons-thumbs-down"></span></button>
<# } #>
<?php
}, 9 );
/*
* Function to make adding a dashicon simple.
*/
function add_customize_unified_notification_dashicon ( $code, $dashicon ) {
add_action( 'customize_unified_notifications', function() use ( $code, $dashicon ) {
?>
<# if ( /<?php echo sanitize_key( $code ); ?>/.test( notification.code ) ) { #>
<span class="dashicons dashicons-<?php echo sanitize_key( $dashicon ) ?>"></span>
<# } #>
<?php
}, 9 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment