Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.21 KB | None | 0 0
  1. /***************************
  2. * create contact metaboxes
  3. ***************************/
  4. function p52_register_quote_contact_meta_boxes( $meta_boxes ) {
  5.     $prefix = 'p52_quote_';
  6.    
  7.     // Get all customer custom post type posts
  8.     $customers = get_posts( array(
  9.         'posts_per_page' => -1,
  10.         'post_type' => 'p52-contact'
  11.     ) );
  12.    
  13.     // start customer
  14.     $meta_box = array(
  15.         'id'       => $prefix . 'customer',
  16.         'title'    => 'Customer Info',
  17.         'pages'    => 'p52-quote',
  18.         'context'  => 'normal',
  19.         'priority' => 'low',
  20.        
  21.  
  22.         'fields' => array(            
  23.        
  24.             array(
  25.                 'name'  => 'Name',
  26.                 'id'    => $prefix . 'name',
  27.                 'type'  => 'post',
  28.                 'post_type' => 'p52-contact',
  29.                 'placeholder' => 'Select a Customer',
  30.                 'columns' => 6
  31.             ),
  32.        
  33.            
  34.             array(
  35.                 'name'  => 'Company',
  36.                 'id'    => $prefix . 'company',
  37.                 'type'  => 'hidden',
  38.                 'columns' => 6
  39.             ),
  40.            
  41.             array(
  42.                 'name'  => 'Email',
  43.                 'id'    => $prefix . 'email',
  44.                 'type'  => 'hidden',
  45.                 'columns' => 6
  46.             ),
  47.            
  48.             array(
  49.                 'name'  => 'Phone',
  50.                 'id'    => $prefix . 'phone',
  51.                 'type'  => 'hidden',
  52.                 'columns' => 6
  53.             ),
  54.            
  55.             array(
  56.                 'name'  => 'Address',
  57.                 'id'    => $prefix . 'address',
  58.                 'type'  => 'hidden',
  59.                 'columns' => 6
  60.             ),
  61.            
  62.             array(
  63.                 'name'  => 'Delivery Address',
  64.                 'id'    => $prefix . 'delivery_address',
  65.                 'type'  => 'hidden',
  66.                 'columns' => 6
  67.             ),
  68.            
  69.                        
  70.         ), // end customer fields
  71.     ); // end customer
  72.    
  73.         // Append phone or another field of customer to meta box above.
  74.         foreach ( $customers as $customer )
  75.         {
  76.             // Show this email field if match name.
  77.             $meta_box['fields'][] = array(
  78.                 'name'      => 'New Email',
  79.                 'id'        => $prefix . 'email_' . $customer->ID,
  80.                 'type'      => 'text',
  81.                 'std'       => get_post_meta( $customer->ID, 'p52_quote_email', true ),
  82.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  83.                 'columns' => 6
  84.             );
  85.            
  86.             // Show this phone field if match name.
  87.             $meta_box['fields'][] = array(
  88.                 'name'      => 'New Phone',
  89.                 'id'        => $prefix . 'phone_' . $customer->ID,
  90.                 'type'      => 'text',
  91.                 'std'       => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
  92.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  93.                 'columns' => 6
  94.             );
  95.            
  96.             // Show this company field if match name.
  97.             $meta_box['fields'][] = array(
  98.                 'name'      => 'New Company',
  99.                 'id'        => $prefix . 'company_' . $customer->ID,
  100.                 'type'      => 'text',
  101.                 'std'       => get_post_meta( $customer->ID, 'p52_quote_company', true ),
  102.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  103.                 'columns' => 6
  104.             );
  105.            
  106.             // Show this address field if match name.
  107.             $meta_box['fields'][] = array(
  108.                 'name'      => 'New Address',
  109.                 'id'        => $prefix . 'address_' . $customer->ID,
  110.                 'type'      => 'textarea',
  111.                 'std'       => get_post_meta( $customer->ID, 'p52_quote_address', true ),
  112.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  113.                 'columns' => 6
  114.             );
  115.            
  116.             // Show this delivery address field if match name.
  117.             $meta_box['fields'][] = array(
  118.                 'name'      => 'New Delivery Address',
  119.                 'id'        => $prefix . 'delivery_address_' . $customer->ID,
  120.                 'type'      => 'textarea',
  121.                 'std'       => get_post_meta( $customer->ID, 'p52_quote_delivery_address_', true ),
  122.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  123.                 'columns' => 6
  124.             );
  125.            
  126.         }
  127.         $meta_boxes[] = $meta_box;
  128.  
  129.     return $meta_boxes;
  130. }
  131.  
  132. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_contact_meta_boxes' );
  133.  
  134. // Save data to phone field
  135. add_action('rwmb_before_save_post', function($post_id)
  136. {
  137.     // Get person ID to save from "Select a Customer" field
  138.     $person_to_save = intval( $_POST['p52_quote_name'] );
  139.     // Save related field to phone field
  140.     $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
  141.     // Unset all hidden fields
  142.     foreach ( $_POST as $key => $value )
  143.     {
  144.         if ( strpos( $key, 'p52_quote_phone_' ) )
  145.             unset( $_POST[$key] );
  146.     }
  147.    
  148.     // Save related field to company field
  149.     $_POST['p52_quote_company'] = $_POST['p52_quote_company_' . $person_to_save];
  150.     // Unset all hidden fields
  151.     foreach ( $_POST as $key => $value )
  152.     {
  153.         if ( strpos( $key, 'p52_quote_company_' ) )
  154.             unset( $_POST[$key] );
  155.     }
  156.    
  157.     // Save related field to email field
  158.     $_POST['p52_quote_email'] = $_POST['p52_quote_email_' . $person_to_save];
  159.     // Unset all hidden fields
  160.     foreach ( $_POST as $key => $value )
  161.     {
  162.         if ( strpos( $key, 'p52_quote_email_' ) )
  163.             unset( $_POST[$key] );
  164.     }
  165.    
  166.     // Save related field to address field
  167.     $_POST['p52_quote_address'] = $_POST['p52_quote_address_' . $person_to_save];
  168.     // Unset all hidden fields
  169.     foreach ( $_POST as $key => $value )
  170.     {
  171.         if ( strpos( $key, 'p52_quote_address_' ) )
  172.             unset( $_POST[$key] );
  173.     }
  174.    
  175.     // Save related field to delivery address field
  176.     $_POST['p52_quote_delivery_address'] = $_POST['p52_quote_delivery_address_' . $person_to_save];
  177.     // Unset all hidden fields
  178.     foreach ( $_POST as $key => $value )
  179.     {
  180.         if ( strpos( $key, 'p52_quote_delivery_address_' ) )
  181.             unset( $_POST[$key] );
  182.     }
  183.    
  184. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement