Skip to content

Commit

Permalink
admin_enqueue_scripts after common.js
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Mar 7, 2023
1 parent 42101e8 commit af52f56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
60 changes: 32 additions & 28 deletions src/Dismiss.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,46 @@ public function __construct( $id, $prefix, $scope = 'global' ) {

// Handle AJAX requests to dismiss the notice.
add_action( 'wp_ajax_wptrt_dismiss_notice', [ $this, 'ajax_maybe_dismiss_notice' ] );

// Print the script after common.js.
add_action( 'admin_enqueue_scripts', array( $this, 'add_script' ) );
}

/**
* Print the script for dismissing the notice.
*
* @access private
* @since 1.0
* @return void
*/
public function print_script() {

// Create a nonce.
$nonce = wp_create_nonce( 'wptrt_dismiss_notice_' . $this->id );
?>
<script>
window.addEventListener( 'load', function() {
var dismissBtn = document.querySelector( '#wptrt-notice-<?php echo esc_attr( $this->id ); ?> .notice-dismiss' );

// Add an event listener to the dismiss button.
dismissBtn.addEventListener( 'click', function( event ) {
var httpRequest = new XMLHttpRequest(),
postData = '';

// Build the data to send in our request.
// Data has to be formatted as a string here.
postData += 'id=<?php echo esc_attr( rawurlencode( $this->id ) ); ?>';
postData += '&action=wptrt_dismiss_notice';
postData += '&nonce=<?php echo esc_html( $nonce ); ?>';

httpRequest.open( 'POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
});
</script>
<?php
public function add_script() {

$id = esc_attr( $this->id );
$nonce = wp_create_nonce( 'wptrt_dismiss_notice_' . $this->id );
$admin_ajax_url = esc_url( admin_url( 'admin-ajax.php' ) );

$script = <<<EOD
jQuery( function() {
var dismissBtn = document.querySelector( '#wptrt-notice-$id .notice-dismiss' );
// Add an event listener to the dismiss button.
dismissBtn.addEventListener( 'click', function( event ) {
var httpRequest = new XMLHttpRequest(),
postData = '';
// Build the data to send in our request.
// Data has to be formatted as a string here.
postData += 'id=$id';
postData += '&action=wptrt_dismiss_notice';
postData += '&nonce=$nonce';
httpRequest.open( 'POST', '$admin_ajax_url' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
});
EOD;

wp_add_inline_script( 'common', $script, 'after' );
}

/**
Expand Down
20 changes: 0 additions & 20 deletions src/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function boot() {

// Add the notice.
add_action( 'admin_notices', [ $this, 'the_notices' ] );

// Print the script to the footer.
add_action( 'admin_footer', [ $this, 'print_scripts' ] );
}

/**
Expand Down Expand Up @@ -115,21 +112,4 @@ public function the_notices() {
$notice->the_notice();
}
}

/**
* Prints scripts for the notices.
*
* @access public
* @since 1.0
* @return void
*/
public function print_scripts() {
$notices = $this->get_all();

foreach ( $notices as $notice ) {
if ( $notice->show() ) {
$notice->dismiss->print_script();
}
}
}
}

0 comments on commit af52f56

Please sign in to comment.