Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ------------------ PAGE 1 bcw.php ------------------
- */
- <?php
- /**
- * sets up AJAX capability
- *
- * enqueues the needed jQuery script and creates a local ajax object for the script.
- * registers and enqueues the CSS for the AJAX capable elements
- *
- * @parm string current admin page $hook
- */
- function bcw_enqueue($hook) {
- if( 'edit-comments.php' != $hook) // Only applies to comments list
- return;
- $email_nonce = wp_create_nonce( 'bcw_comment_email' );
- wp_enqueue_script( 'ajax-script', plugins_url( '/js/bcw.js', __FILE__ ),
- array('jquery'));
- // in javascript, array elements are accessed as ajax_object.ajaxurl,
- // ajax_object.email, etc.
- wp_localize_script( 'ajax-script', 'ajax_object',
- array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'email'=>$email_nonce )
- );
- }
- add_action( 'admin_enqueue_scripts', 'bcw_enqueue' );
- /**
- * toggles email setting for comments
- *
- * no return - dies
- */
- function bcw_toggle_email() {
- $commentid = $_POST['commentid']; // getting variables from ajax post
- check_ajax_referer('bcw_comment_email'); // die if not correct referer
- if (current_user_can('moderate_comments')) {
- $reply = get_comment_meta( $commentid, 'bcw_email_reply', true );
- // prepare nonce for next request
- $email_nonce = wp_create_nonce( 'bcw_comment_email' );
- // Toggle the current setting
- if ($reply == 'Yes') {
- update_comment_meta($commentid, 'bcw_email_reply', 'x');
- echo 'x,' . $email_nonce;
- } else {
- update_comment_meta($commentid, 'bcw_email_reply', 'Yes');
- echo 'Yes,' . $email_nonce;
- }
- } else echo '!,' . $email_nonce;
- die(); // stop executing script
- }
- add_action( 'wp_ajax_email_action', 'bcw_toggle_email' ); // ajax for logged in users
- ?>
- /**
- * ------------- PAGE 2 bcw.js ------------------------
- */
- /**
- * javascript for BCW customizations
- */
- jQuery(function($){
- // toggle email comment setting
- $("td.email.column-email").click(function () {
- var td = this;
- $.post(ajax_object.ajaxurl, { //POST an AJAX request
- action: "email_action",
- commentid: $(this).find("input.commentid").attr("value"),
- //This nonce can only be used this one time
- _ajax_nonce: ajax_object.email
- }, function(data) { //Handle AJAX response
- //includes email setting plus hidden comment id field
- var child = $(td).html();
- data = data.split(',');
- //save new nonce from server for next request
- ajax_object.email = data[1];
- //replace current value with new and add on the hidden comment id field
- var sym = child.split('<');
- $(td).html(data[0] + '<' + sym[1]);
- });
- });
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement