Advertisement
verygoodplugins

Untitled

Jun 24th, 2020
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.72 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7. class WPF_AJAX {
  8.  
  9.     public function __construct() {
  10.  
  11.         add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_link_click_script' ) );
  12.  
  13.         // AJAX handlers
  14.         add_action( 'wp_ajax_apply_tags', array( $this, 'apply_tags' ) );
  15.         add_action( 'wp_ajax_nopriv_apply_tags', array( $this, 'apply_tags' ) );
  16.  
  17.         add_action( 'wp_ajax_remove_tags', array( $this, 'remove_tags' ) );
  18.         add_action( 'wp_ajax_nopriv_remove_tags', array( $this, 'remove_tags' ) );
  19.  
  20.         add_action( 'wp_ajax_update_user', array( $this, 'update_user' ) );
  21.         //add_action( 'wp_ajax_nopriv_update_user', array( $this, 'update_user' ) );
  22.  
  23.     }
  24.  
  25.     /**
  26.      * Applies tags to a given user via AJAX call
  27.      *
  28.      * @access public
  29.      * @return void
  30.      */
  31.  
  32.     public function apply_tags() {
  33.  
  34.         $tags = $_POST['tags'];
  35.  
  36.         if( isset( $_POST['user_id'] ) ) {
  37.             $user_id = intval($_POST['user_id']);
  38.         } else {
  39.             $user_id = wpf_get_current_user_id();
  40.         }
  41.  
  42.         if( ! is_array( $tags ) ) {
  43.             $tags = explode(',', $tags);
  44.         }
  45.  
  46.         $tags = array_map('sanitize_text_field', $tags);
  47.  
  48.         $tags_to_apply = array();
  49.  
  50.         foreach ( $tags as $tag ) {
  51.  
  52.             $tag_id = wp_fusion()->user->get_tag_id( $tag );
  53.  
  54.             if ( false === $tag_id ) {
  55.  
  56.                 wpf_log( 'notice', $user_id, 'Unable to determine tag ID from tag with name <strong>' . $tag . '</strong>. Tag will not be applied.' );
  57.                 continue;
  58.  
  59.             }
  60.  
  61.             $tags_to_apply[] = $tag_id;
  62.         }
  63.  
  64.         if ( ! empty( $tags_to_apply ) ) {
  65.  
  66.             wp_fusion()->user->apply_tags( $tags_to_apply, $user_id );
  67.  
  68.         }
  69.  
  70.         die();
  71.  
  72.     }
  73.  
  74.     /**
  75.      * Applies tags to a given user via AJAX call
  76.      *
  77.      * @access public
  78.      * @return void
  79.      */
  80.  
  81.     public function remove_tags() {
  82.  
  83.         $tags = $_POST['tags'];
  84.  
  85.         if( isset( $_POST['user_id'] ) ) {
  86.             $user_id = intval($_POST['user_id']);
  87.         } else {
  88.             $user_id = wpf_get_current_user_id();
  89.         }
  90.  
  91.         if( ! is_array( $tags ) ) {
  92.             $tags = explode(',', $tags);
  93.         }
  94.  
  95.         $tags = array_map('sanitize_text_field', $tags);
  96.  
  97.         $tags_to_remove = array();
  98.  
  99.         foreach ( $tags as $tag ) {
  100.  
  101.             $tag_id = wp_fusion()->user->get_tag_id( $tag );
  102.  
  103.             if ( false === $tag_id ) {
  104.  
  105.                 wpf_log( 'notice', $user_id, 'Unable to determine tag ID from tag with name <strong>' . $tag . '</strong>. Tag will not be applied.' );
  106.                 continue;
  107.  
  108.             }
  109.  
  110.             $tags_to_remove[] = $tag_id;
  111.         }
  112.  
  113.         if ( ! empty( $tags_to_remove ) ) {
  114.  
  115.             wp_fusion()->user->remove_tags( $tags_to_remove, $user_id );
  116.  
  117.         }
  118.  
  119.         die();
  120.  
  121.     }
  122.  
  123.     /**
  124.      * Updates contact via AJAX call
  125.      *
  126.      * @access public
  127.      * @return void
  128.      */
  129.  
  130.     public function update_user() {
  131.  
  132.         $update_data = json_decode( stripslashes( $_POST['data'] ), true );
  133.  
  134.         if( isset( $_POST['user_id'] ) ) {
  135.             $user_id = intval($_POST['user_id']);
  136.         } else {
  137.             $user_id = wpf_get_current_user_id();
  138.         }
  139.  
  140.         if ( is_array( $update_data ) ) {
  141.  
  142.             $update_data = array_map( 'sanitize_text_field', $update_data );
  143.  
  144.             // Figure out how we're doing the update
  145.  
  146.             $contact_fields = wp_fusion()->settings->get( 'contact_fields', array() );
  147.             $crm_fields = wp_fusion()->settings->get( 'crm_fields', array() );
  148.  
  149.             // CRMs with field groupings
  150.  
  151.             if( is_array( reset( $crm_fields ) ) ) {
  152.  
  153.                 $crm_fields_tmp = array();
  154.  
  155.                 foreach( $crm_fields as $field_group ) {
  156.  
  157.                     foreach( $field_group as $field => $label ) {
  158.  
  159.                         $crm_fields_tmp[ $field ] = $label;
  160.  
  161.                     }
  162.  
  163.                 }
  164.  
  165.                 $crm_fields = $crm_fields_tmp;
  166.  
  167.             }
  168.  
  169.             $user_meta_data = array();
  170.             $crm_data = array();
  171.  
  172.             foreach( $update_data as $key => $value ) {
  173.  
  174.                 if( isset( $contact_fields[ $key ] ) && $contact_fields[ $key ]['active'] == true ) {
  175.  
  176.                     $user_meta_data[ $key ] = $value;
  177.  
  178.                 } else {
  179.  
  180.                     foreach( $crm_fields as $field => $label ) {
  181.  
  182.                         if( $key == $field || $key == $label ) {
  183.  
  184.                             $crm_data[ $field ] = $value;
  185.  
  186.                         }
  187.  
  188.                     }
  189.  
  190.                 }
  191.  
  192.             }
  193.  
  194.             if( ! empty( $user_meta_data ) ) {
  195.                 wp_fusion()->user->push_user_meta( $user_id, $user_meta_data );
  196.             }
  197.  
  198.             if( ! empty( $crm_data ) ) {
  199.                 $contact_id = wp_fusion()->user->get_contact_id( $user_id );
  200.                 wp_fusion()->crm->update_contact( $contact_id, $crm_data, false );
  201.             }
  202.  
  203.         }
  204.  
  205.         die();
  206.  
  207.     }
  208.  
  209.     /**
  210.      * Enqueues link click tracking scripts if option is enabled
  211.      *
  212.      * @access public
  213.      * @return void
  214.      */
  215.  
  216.     public function enqueue_link_click_script() {
  217.  
  218.         if ( ! wp_script_is( 'wpf-apply-tags' ) && wp_fusion()->settings->get( 'link_click_tracking' ) == true ) {
  219.  
  220.             wp_enqueue_script( 'wpf-apply-tags', WPF_DIR_URL . 'assets/js/wpf-apply-tags.js', array( 'jquery' ), WP_FUSION_VERSION, true );
  221.             wp_localize_script( 'wpf-apply-tags', 'wpf_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  222.  
  223.         }
  224.  
  225.     }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement