Skip to content

Instantly share code, notes, and snippets.

@Oceas
Last active July 21, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oceas/6c8de5e188952e7e231fc47c76af2dbf to your computer and use it in GitHub Desktop.
Save Oceas/6c8de5e188952e7e231fc47c76af2dbf to your computer and use it in GitHub Desktop.
;(function($){
$(document).ready(function (){
$('body').prepend('<div style="width:100%;background-color:red;color:white;text-align:center;">' + status_bar_settings.message + '</div>');
});
})(jQuery);
/**
* Called by Plugin class; register the hooks for this plugin
*
* @since 0.1.0
* @author Scott Anderson <scott.anderson@webdevstudios.com>
*/
public function register_hooks() : void {
// 1. First we register our endpoint to receive requests from our SAS.
add_action( 'rest_api_init', function () {
register_rest_route( 'site-updater', '/banner-bar/', array(
'methods' => 'POST',
'callback' => [ $this, 'banner_bar' ],
) );
} );
// 2. Next we enquie our scrips to inject the status bar at the top of the page.
add_action( 'wp_enqueue_scripts', [ $this, 'add_status_bar_styles' ] );
}
/**
* Register javascript for status bar.
*
* @author Scott Anderson <scott.anderson@webdevstudios.com>
* @since NEXT
*/
public function add_status_bar_styles() {
if ( get_option( 'cc_status_bar_active', false ) ) {
wp_enqueue_script( 'topbar_frontjs', plugins_url( '/assets/js/status-bar.js', __FILE__ ), array( 'jquery' ) );
$status_bar_settings = [
'message' => get_option( 'cc_status_bar_message', '' ),
];
// Sending settings to the status bar.
wp_localize_script( 'topbar_frontjs', 'status_bar_settings', $status_bar_settings );
}
}
/**
* Updates status bar message and display flag.
*
* @param object $args Options for the function.
* @author Scott Anderson <scott.anderson@webdevstudios.com>
* @return array
*/
public function banner_bar( \WP_REST_Request $request ) : array {
if ( 'true' === $request['active'] ) {
update_option( 'cc_status_bar_active', true );
} else {
update_option( 'cc_status_bar_active', false );
}
update_option( 'cc_status_bar_message', $request['message'] );
return [
'success' => true,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment