Advertisement
verygoodplugins

Untitled

May 14th, 2021
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.51 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7.  
  8. class WPF_WPPizza 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 = 'wppizza';
  21.  
  22.         add_action( 'wppizza_on_order_execute', array( $this, 'order_execute' ), 10, 4 );
  23.  
  24.         add_filter( 'wpf_meta_field_groups', array( $this, 'add_meta_field_group' ) );
  25.         add_filter( 'wpf_meta_fields', array( $this, 'add_meta_fields' ) );
  26.  
  27.         add_filter( 'wppizza_filter_admin_metaboxes', array( $this, 'admin_metabox' ), 80, 4 );
  28.         add_filter( 'wppizza_filter_admin_save_metaboxes', array( $this, 'admin_save_metabox' ), 10, 3 );
  29.  
  30.     }
  31.  
  32.  
  33.     /**
  34.      * Handle customer data when an order is placed
  35.      *
  36.      * @access public
  37.      * @return void
  38.      */
  39.  
  40.     public function order_execute( $order_id, $deprecated, $print_templates, $order_details ) {
  41.  
  42.         // Create / update contact
  43.  
  44.         $update_data = array();
  45.  
  46.         foreach ( $order_details['sections']['customer'] as $field => $data ) {
  47.  
  48.             $update_data[ $field ] = $data['value'];
  49.  
  50.         }
  51.  
  52.         $field_map = array(
  53.             'cname'  => 'first_name',
  54.             'cemail' => 'user_email',
  55.         );
  56.  
  57.         $update_data = $this->map_meta_fields( $update_data, $field_map );
  58.  
  59.         if ( wpf_is_user_logged_in() ) {
  60.  
  61.             // Registered user
  62.  
  63.             wp_fusion()->user->push_user_meta( wpf_get_current_user_id(), $update_data );
  64.  
  65.         } else {
  66.  
  67.             // Guest checkout
  68.  
  69.             wpf_log( 'info', 0, 'WPPizza guest checkout:', array( 'meta_array' => $update_data ) );
  70.  
  71.             $contact_id = wp_fusion()->crm->get_contact_id( $update_data['user_email'] );
  72.  
  73.             if ( empty( $contact_id ) ) {
  74.                 $contact_id = wp_fusion()->crm->add_contact( $update_data );
  75.             } else {
  76.                 wp_fusion()->crm->update_contact( $contact_id, $update_data );
  77.             }
  78.  
  79.             if ( is_wp_error( $contact_id ) ) {
  80.  
  81.                 wpf_log( $contact_id->get_error_code(), 0, 'Error adding contact: ' . $contact_id->get_error_message() );
  82.                 return;
  83.  
  84.             }
  85.  
  86.             update_post_meta( $order_id, wp_fusion()->crm->slug . '_contact_id', $contact_id );
  87.  
  88.         }
  89.  
  90.         // Apply tags
  91.         $apply_tags = array();
  92.  
  93.         foreach ( $order_details['sections']['order']['items'] as $item ) {
  94.  
  95.             $settings = get_post_meta( $item['post_id'], 'wppizza', true );
  96.  
  97.             if ( ! empty( $settings ) && ! empty( $settings['apply_tags'] ) ) {
  98.                 $apply_tags = array_merge( $apply_tags, $settings['apply_tags'] );
  99.             }
  100.         }
  101.  
  102.         if ( ! empty( $apply_tags ) ) {
  103.  
  104.             if ( wpf_is_user_logged_in() ) {
  105.  
  106.                 wp_fusion()->user->apply_tags( $apply_tags );
  107.  
  108.             } else {
  109.  
  110.                 wpf_log( 'info', 0, 'WPPizza guest checkout applying tag(s): ', array( 'tag_array' => $apply_tags ) );
  111.  
  112.                 wp_fusion()->crm->apply_tags( $apply_tags, $contact_id );
  113.  
  114.             }
  115.         }
  116.  
  117.     }
  118.  
  119.     /**
  120.      * Adds WPPizza field group to meta fields list
  121.      *
  122.      * @access  public
  123.      * @return  array Field groups
  124.      */
  125.  
  126.     public function add_meta_field_group( $field_groups ) {
  127.  
  128.         $field_groups['wppizza'] = array(
  129.             'title'  => 'WPPizza',
  130.             'fields' => array(),
  131.         );
  132.  
  133.         return $field_groups;
  134.  
  135.     }
  136.  
  137.     /**
  138.      * Add WPpizza fields
  139.      *
  140.      * @access  public
  141.      * @return  array Meta Fields
  142.      */
  143.  
  144.     public function add_meta_fields( $meta_fields ) {
  145.  
  146.         $options = get_option( 'wppizza', array() );
  147.  
  148.         if ( ! empty( $options['order_form'] ) ) {
  149.  
  150.             foreach ( $options['order_form'] as $id => $field ) {
  151.  
  152.                 $meta_fields[ $id ] = array(
  153.                     'label' => str_replace( ' :', '', $field['lbl'] ),
  154.                     'type'  => $field['type'],
  155.                     'group' => 'wppizza',
  156.                 );
  157.  
  158.             }
  159.         }
  160.  
  161.         return $meta_fields;
  162.  
  163.     }
  164.  
  165.     /**
  166.      * Register settings on menu item
  167.      *
  168.      * @access  public
  169.      * @return  array Meta Box
  170.      */
  171.  
  172.     function admin_metabox( $wppizza_meta_box, $meta_values, $meal_sizes, $wppizza_options ) {
  173.  
  174.         $wppizza_meta_box['wpfusion'] = '<div class="wppizza_option_meta"><label class="wppizza-meta-label">' . __( 'Apply Tags', 'wp-fusion' ) . ':</label>';
  175.  
  176.         ob_start();
  177.  
  178.         $args = array(
  179.             'setting'   => $meta_values['apply_tags'],
  180.             'meta_name' => 'wppizza',
  181.             'field_id'  => 'apply_tags',
  182.         );
  183.  
  184.         wpf_render_tag_multiselect( $args );
  185.  
  186.         $wppizza_meta_box['wpfusion'] .= ob_get_clean();
  187.  
  188.         $wppizza_meta_box['wpfusion'] .= '</div>';
  189.  
  190.         return $wppizza_meta_box;
  191.  
  192.     }
  193.  
  194.     /**
  195.      * Save admin metabox
  196.      *
  197.      * @access  public
  198.      * @return  array Item Meta
  199.      */
  200.  
  201.     function admin_save_metabox( $item_meta, $item_id, $wppizza_options ) {
  202.  
  203.         $item_meta['apply_tags'] = $_POST['wppizza']['apply_tags'];
  204.  
  205.         return $item_meta;
  206.  
  207.     }
  208.  
  209.  
  210. }
  211.  
  212. new WPF_WPPizza();
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement