Advertisement
Cornerstone

Untitled

May 3rd, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: TPS Save To User
  4. Description: saves something (like an audition) to the user's account
  5.  
  6. */
  7.  
  8. add_action("wp_ajax_tps_save_audition", "tps_save_audition");
  9. //add_action("wp_ajax_nopriv_tps_save_audition", "my_must_login");
  10.  
  11. function tps_save_audition() {
  12.    
  13.     if ( !wp_verify_nonce( $_REQUEST['nonce'], "tps_save_audition_nonce")) {
  14.       exit("Hey, no naughty business please!");
  15.    }
  16.    
  17.     $audition = $_REQUEST['post_id'];
  18.     $user = get_current_user_id();
  19.     $saved = get_user_meta($user, 'saved_auditions', true);
  20.    
  21.     if (in_array($audition, $saved)){
  22.         //If the audition that was clicked on is already saved to the user, we assume they want to remove it
  23.         $remove = array_search($audition, $saved);
  24.         if( false !== $remove ){
  25.             // Remove audition
  26.             unset($saved[$remove]);
  27.             $array = ( is_array( $saved ) ) ? $saved : array( $saved );
  28.             $removeAudition = update_user_meta( $user, 'saved_auditions', $array );
  29.         }
  30.         if (!$removeAudition) {
  31.             $result['type'] = "error";
  32.             $result['saved_audition'] = $audition;
  33.             $result['message'] = 'Audition has NOT been un-saved!';
  34.         } else {
  35.             $result['type'] = "removed";
  36.             $result['saved_audition'] = 0;
  37.             $result['message'] = 'Audition has been un-saved!';
  38.         }
  39.     } else {
  40.         //If the audition is NOT already saved to the user, we go ahead and add it to their meta
  41.         $saved[] = $audition;
  42.         $saveAudition = update_user_meta( $user, 'saved_auditions', $saved);
  43.         if (!$saveAudition) {
  44.             $result['type'] = "error";
  45.             $result['saved_audition'] = 0;
  46.             $result['message'] = 'Audition not saved!';
  47.         } else {
  48.             $result['type'] = "success";
  49.             $result['saved_audition'] = $audition;
  50.             $result['message'] = 'Audition saved!';
  51.         }
  52.         //delete_user_meta( $user, 'saved_auditions');
  53.     }
  54.    
  55.     //Checks if ajax request is present (meaning ajax is enabled on browser)
  56.     if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  57.       $result = json_encode($result);
  58.       echo $result;
  59.    } else {
  60.       //if not, we just redirect back to the original page
  61.       header("Location: ".$_SERVER["HTTP_REFERER"]);
  62.    }
  63.    
  64.     die();
  65. }
  66.  
  67.  
  68. add_action("wp_ajax_tps_hide_audition", "tps_hide_audition");
  69. //add_action("wp_ajax_nopriv_tps_save_audition", "my_must_login");
  70.  
  71. function tps_hide_audition() {
  72.    
  73.     if ( !wp_verify_nonce( $_REQUEST['nonce'], "tps_hide_audition_nonce")) {
  74.       exit("Hey, no naughty business please!");
  75.    }
  76.    
  77.     $audition = $_REQUEST['post_id'];
  78.     $user = get_current_user_id();
  79.     $hidden = get_user_meta($user, 'hidden_auditions', true);
  80.    
  81.     if (in_array($audition, $hidden)){
  82.         //Post already hidden
  83.     } else {
  84.         //If the audition is NOT already saved to the user, we go ahead and add it to their meta
  85.         $hidden[] = $audition;
  86.         $hideAudition = update_user_meta( $user, 'hidden_auditions', $hidden);
  87.         if (!$hideAudition) {
  88.             $result['type'] = "error";
  89.             $result['saved_audition'] = 0;
  90.             $result['message'] = 'Audition not hidden!';
  91.         } else {
  92.             $result['type'] = "success";
  93.             $result['saved_audition'] = $audition;
  94.             $result['message'] = 'Audition hidden!';
  95.         }
  96.         //delete_user_meta( $user, 'saved_auditions');
  97.     }
  98.    
  99.     //Checks if ajax request is present (meaning ajax is enabled on browser)
  100.     if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  101.       $result = json_encode($result);
  102.       echo $result;
  103.    } else {
  104.       //if not, we just redirect back to the original page
  105.       header("Location: ".$_SERVER["HTTP_REFERER"]);
  106.    }
  107.    
  108.     die();
  109. }
  110.  
  111.  
  112. add_action( 'init', 'save_to_user_script_enqueuer' );
  113.  
  114. function save_to_user_script_enqueuer() {
  115.    wp_register_script( "save-to-user", plugins_url('', __FILE__).'/save-to-user.js', array('jquery') );
  116.    wp_localize_script( 'save-to-user', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        
  117.  
  118.    wp_enqueue_script( 'jquery' );
  119.    wp_enqueue_script( 'save-to-user' );
  120.  
  121. }
  122.  
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement