Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: TPS Save To User
- Description: saves something (like an audition) to the user's account
- */
- add_action("wp_ajax_tps_save_audition", "tps_save_audition");
- //add_action("wp_ajax_nopriv_tps_save_audition", "my_must_login");
- function tps_save_audition() {
- if ( !wp_verify_nonce( $_REQUEST['nonce'], "tps_save_audition_nonce")) {
- exit("Hey, no naughty business please!");
- }
- $audition = $_REQUEST['post_id'];
- $user = get_current_user_id();
- $saved = get_user_meta($user, 'saved_auditions', true);
- if (in_array($audition, $saved)){
- //If the audition that was clicked on is already saved to the user, we assume they want to remove it
- $remove = array_search($audition, $saved);
- if( false !== $remove ){
- // Remove audition
- unset($saved[$remove]);
- $array = ( is_array( $saved ) ) ? $saved : array( $saved );
- $removeAudition = update_user_meta( $user, 'saved_auditions', $array );
- }
- if (!$removeAudition) {
- $result['type'] = "error";
- $result['saved_audition'] = $audition;
- $result['message'] = 'Audition has NOT been un-saved!';
- } else {
- $result['type'] = "removed";
- $result['saved_audition'] = 0;
- $result['message'] = 'Audition has been un-saved!';
- }
- } else {
- //If the audition is NOT already saved to the user, we go ahead and add it to their meta
- $saved[] = $audition;
- $saveAudition = update_user_meta( $user, 'saved_auditions', $saved);
- if (!$saveAudition) {
- $result['type'] = "error";
- $result['saved_audition'] = 0;
- $result['message'] = 'Audition not saved!';
- } else {
- $result['type'] = "success";
- $result['saved_audition'] = $audition;
- $result['message'] = 'Audition saved!';
- }
- //delete_user_meta( $user, 'saved_auditions');
- }
- //Checks if ajax request is present (meaning ajax is enabled on browser)
- if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
- $result = json_encode($result);
- echo $result;
- } else {
- //if not, we just redirect back to the original page
- header("Location: ".$_SERVER["HTTP_REFERER"]);
- }
- die();
- }
- add_action("wp_ajax_tps_hide_audition", "tps_hide_audition");
- //add_action("wp_ajax_nopriv_tps_save_audition", "my_must_login");
- function tps_hide_audition() {
- if ( !wp_verify_nonce( $_REQUEST['nonce'], "tps_hide_audition_nonce")) {
- exit("Hey, no naughty business please!");
- }
- $audition = $_REQUEST['post_id'];
- $user = get_current_user_id();
- $hidden = get_user_meta($user, 'hidden_auditions', true);
- if (in_array($audition, $hidden)){
- //Post already hidden
- } else {
- //If the audition is NOT already saved to the user, we go ahead and add it to their meta
- $hidden[] = $audition;
- $hideAudition = update_user_meta( $user, 'hidden_auditions', $hidden);
- if (!$hideAudition) {
- $result['type'] = "error";
- $result['saved_audition'] = 0;
- $result['message'] = 'Audition not hidden!';
- } else {
- $result['type'] = "success";
- $result['saved_audition'] = $audition;
- $result['message'] = 'Audition hidden!';
- }
- //delete_user_meta( $user, 'saved_auditions');
- }
- //Checks if ajax request is present (meaning ajax is enabled on browser)
- if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
- $result = json_encode($result);
- echo $result;
- } else {
- //if not, we just redirect back to the original page
- header("Location: ".$_SERVER["HTTP_REFERER"]);
- }
- die();
- }
- add_action( 'init', 'save_to_user_script_enqueuer' );
- function save_to_user_script_enqueuer() {
- wp_register_script( "save-to-user", plugins_url('', __FILE__).'/save-to-user.js', array('jquery') );
- wp_localize_script( 'save-to-user', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
- wp_enqueue_script( 'jquery' );
- wp_enqueue_script( 'save-to-user' );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement