Advertisement
verygoodplugins

Untitled

May 14th, 2021
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.73 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7.  
  8. class WPF_Salon_Booking extends WPF_Integrations_Base {
  9.  
  10.     /**
  11.      * Gets things started
  12.      *
  13.      * @access  public
  14.      * @since   1.0
  15.      * @return  void
  16.      */
  17.  
  18.     public function init() {
  19.  
  20.         $this->slug = 'salon-booking';
  21.  
  22.         add_action( 'sln.booking_builder.create.booking_created', array( $this, 'booking_created' ) );
  23.  
  24.         add_filter( 'wpf_user_register', array( $this, 'filter_form_fields' ), 10, 2 );
  25.  
  26.         add_filter( 'wpf_meta_box_post_types', array( $this, 'unset_wpf_meta_boxes' ) );
  27.         add_filter( 'wpf_meta_field_groups', array( $this, 'add_meta_field_group' ) );
  28.         add_filter( 'wpf_meta_fields', array( $this, 'add_meta_fields' ) );
  29.  
  30.         add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
  31.  
  32.     }
  33.  
  34.     /**
  35.      * Booking created
  36.      *
  37.      * @access public
  38.      * @return void
  39.      */
  40.  
  41.     public function booking_created( $booking ) {
  42.  
  43.         // Maybe create contact if it's a guest booking
  44.  
  45.         $user_id = $booking->getUserId();
  46.  
  47.         $contact_data = array(
  48.             'first_name'   => $booking->getFirstName(),
  49.             'last_name'    => $booking->getLastName(),
  50.             'user_email'   => $booking->getEmail(),
  51.             '_sln_address' => $booking->getAddress(),
  52.             '_sln_phone'   => $booking->getPhone(),
  53.         );
  54.  
  55.         if ( ! empty( $user_id ) ) {
  56.  
  57.             wp_fusion()->user->push_user_meta( $user_id, $contact_data );
  58.  
  59.         } else {
  60.  
  61.             wpf_log( 'info', 0, 'Salon Bookings guest booking:', array( 'meta_array' => $contact_data ) );
  62.  
  63.             $contact_id = wp_fusion()->crm->get_contact_id( $contact_data['user_email'] );
  64.  
  65.             if ( false == $contact_id ) {
  66.  
  67.                 $contact_id = wp_fusion()->crm->add_contact( $contact_data );
  68.  
  69.             } else {
  70.  
  71.                 wp_fusion()->crm->update_contact( $contact_id, $contact_data );
  72.  
  73.             }
  74.  
  75.             if ( is_wp_error( $contact_id ) ) {
  76.  
  77.                 wpf_log( $contact_id->get_error_code(), 0, 'Error adding contact: ' . $contact_id->get_error_message() );
  78.                 return;
  79.  
  80.             }
  81.  
  82.             update_post_meta( $booking->getId(), wp_fusion()->crm->slug . '_contact_id', $contact_id );
  83.  
  84.         }
  85.  
  86.         // Apply tags
  87.         $apply_tags = array();
  88.  
  89.         $services = $booking->getServicesIds();
  90.  
  91.         foreach ( $services as $service_id ) {
  92.  
  93.             $settings = get_post_meta( $service_id, 'wpf-settings', true );
  94.  
  95.             if ( ! empty( $settings ) && ! empty( $settings['apply_tags_service'] ) ) {
  96.                 $apply_tags = array_merge( $apply_tags, $settings['apply_tags_service'] );
  97.             }
  98.         }
  99.  
  100.         if ( ! empty( $apply_tags ) ) {
  101.  
  102.             if ( ! empty( $user_id ) ) {
  103.  
  104.                 wp_fusion()->user->apply_tags( $apply_tags, $user_id );
  105.  
  106.             } else {
  107.  
  108.                 wpf_log( 'info', $user_id, 'Salon Bookings guest booking applying tag(s): ', array( 'tag_array' => $apply_tags ) );
  109.  
  110.                 wp_fusion()->crm->apply_tags( $apply_tags, $contact_id );
  111.  
  112.             }
  113.         }
  114.  
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Filters registration data before sending to the CRM
  120.      *
  121.      * @access public
  122.      * @return array Registration / Update Data
  123.      */
  124.  
  125.     public function filter_form_fields( $post_data, $user_id ) {
  126.  
  127.         if ( ! isset( $post_data['sln'] ) ) {
  128.             return $post_data;
  129.         }
  130.  
  131.         $post_data = array_merge( $post_data, $post_data['sln'] );
  132.  
  133.         $field_map = array(
  134.             'firstname' => 'first_name',
  135.             'lastname'  => 'last_name',
  136.             'email'     => 'user_email',
  137.             'password'  => 'user_pass',
  138.             'phone'     => '_sln_phone',
  139.             'address'   => '_sln_address',
  140.         );
  141.  
  142.         $post_data = $this->map_meta_fields( $post_data, $field_map );
  143.  
  144.         return $post_data;
  145.  
  146.     }
  147.  
  148.     /**
  149.      * Adds Salon Booking field group to meta fields list
  150.      *
  151.      * @access  public
  152.      * @return  array Field groups
  153.      */
  154.  
  155.     public function add_meta_field_group( $field_groups ) {
  156.  
  157.         $field_groups['salon_booking'] = array(
  158.             'title'  => 'Salon Booking',
  159.             'fields' => array(),
  160.         );
  161.  
  162.         return $field_groups;
  163.  
  164.     }
  165.  
  166.     /**
  167.      * Set field keys / labels for Salon Booking fields
  168.      *
  169.      * @access public
  170.      * @return array Settings
  171.      */
  172.  
  173.     public function add_meta_fields( $meta_fields ) {
  174.  
  175.         $meta_fields['_sln_address'] = array(
  176.             'label' => 'Address',
  177.             'group' => 'salon_booking',
  178.         );
  179.  
  180.         $meta_fields['_sln_phone'] = array(
  181.             'label' => 'Phone Number',
  182.             'group' => 'salon_booking',
  183.         );
  184.  
  185.         return $meta_fields;
  186.  
  187.     }
  188.  
  189.  
  190.     /**
  191.      * Removes standard WPF meta boxes from Salon related post types
  192.      *
  193.      * @access  public
  194.      * @return  array Post Types
  195.      */
  196.  
  197.     public function unset_wpf_meta_boxes( $post_types ) {
  198.  
  199.         unset( $post_types['sln_booking'] );
  200.         unset( $post_types['sln_service'] );
  201.         unset( $post_types['sln_attendant'] );
  202.  
  203.         return $post_types;
  204.  
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Register WPF meta boxes
  210.      *
  211.      * @access  public
  212.      * @return  void
  213.      */
  214.  
  215.     public function add_meta_boxes( $service ) {
  216.  
  217.         add_meta_box( 'wpf-service-meta', 'WP Fusion - Service Settings', array( $this, 'meta_box_callback_service' ), 'sln_service' );
  218.  
  219.     }
  220.  
  221.  
  222.     /**
  223.      * Displays meta box content
  224.      *
  225.      * @access public
  226.      * @return mixed
  227.      */
  228.  
  229.     public function meta_box_callback_service( $post ) {
  230.  
  231.         $settings = array(
  232.             'apply_tags_service' => array(),
  233.         );
  234.  
  235.         if ( get_post_meta( $post->ID, 'wpf-settings', true ) ) {
  236.             $settings = array_merge( $settings, get_post_meta( $post->ID, 'wpf-settings', true ) );
  237.         }
  238.  
  239.         echo '<table class="form-table"><tbody>';
  240.  
  241.         echo '<tr>';
  242.  
  243.         echo '<th scope="row"><label for="tag_link">' . __( 'Apply tags', 'wp-fusion' ) . '</label></th>';
  244.         echo '<td>';
  245.  
  246.         $args = array(
  247.             'setting'   => $settings['apply_tags_service'],
  248.             'meta_name' => 'wpf-settings',
  249.             'field_id'  => 'apply_tags_service',
  250.         );
  251.  
  252.         wpf_render_tag_multiselect( $args );
  253.  
  254.         echo '<span class="description">' . __( 'Select tags to be applied when someone books this service', 'wp-fusion' ) . '</span>';
  255.         echo '</td>';
  256.  
  257.         echo '</tr>';
  258.  
  259.         echo '</tbody></table>';
  260.  
  261.     }
  262.  
  263.  
  264. }
  265.  
  266. new WPF_Salon_Booking();
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement