if (!defined('ABSPATH')) { return; } add_action('init', function () { register_post_type('pioneer_lead', [ 'labels' => ['name'=>'Moving Leads','singular_name'=>'Moving Lead','menu_name'=>'Moving Leads'], 'public'=>false, 'show_ui'=>true, 'show_in_menu'=>true, 'supports'=>['title'], 'capability_type'=>'post', 'map_meta_cap'=>true, 'menu_icon'=>'dashicons-phone' ]); }); add_action('admin_menu', function () { add_submenu_page('edit.php?post_type=pioneer_lead','Export Leads','Export CSV','manage_options','pioneer-lead-export',function () { if (!current_user_can('manage_options')) return; echo '
'; }); }); add_action('admin_post_pioneer_export_leads', function () { if (!current_user_can('manage_options')) wp_die('Not allowed.'); check_admin_referer('pioneer_export_leads'); $items=get_posts(['post_type'=>'pioneer_lead','post_status'=>'private','numberposts'=>-1,'orderby'=>'date','order'=>'DESC']); nocache_headers(); header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=pioneer-moving-leads-'.gmdate('Y-m-d').'.csv'); $out=fopen('php://output','w'); fputcsv($out,['Lead ID','Created','Name','Mobile','Email','Pickup','Destination','Service','Moving Date','Message','Status','Landing Page','UTM Source','UTM Medium','UTM Campaign']); foreach($items as $p){$m=get_post_meta($p->ID);fputcsv($out,[$p->ID,$p->post_date,$m['pioneer_name'][0]??'',$m['pioneer_mobile'][0]??'',$m['pioneer_email'][0]??'',$m['pioneer_from'][0]??'',$m['pioneer_to'][0]??'',$m['pioneer_service'][0]??'',$m['pioneer_date'][0]??'',$m['pioneer_message'][0]??'',$m['pioneer_status'][0]??'New',$m['pioneer_landing'][0]??'',$m['pioneer_utm_source'][0]??'',$m['pioneer_utm_medium'][0]??'',$m['pioneer_utm_campaign'][0]??'']);} fclose($out); exit; }); add_action('admin_post_nopriv_pioneer_submit_lead','pioneer_submit_lead'); add_action('admin_post_pioneer_submit_lead','pioneer_submit_lead'); function pioneer_submit_lead() { if (($_SERVER['REQUEST_METHOD']??'')!=='POST') { wp_safe_redirect(home_url('/')); exit; } if (!empty($_POST['company_website'])) { wp_safe_redirect(add_query_arg('quote','sent',home_url('/thank-you/'))); exit; } $nonce=sanitize_text_field(wp_unslash($_POST['pioneer_nonce']??'')); if (!wp_verify_nonce($nonce,'pioneer_quote_form')) wp_die('Your form session expired. Please go back and try again.','Form expired',['response'=>403]); $name=sanitize_text_field(wp_unslash($_POST['name']??'')); $mobile=preg_replace('/[^0-9]/','',(string)wp_unslash($_POST['mobile']??'')); $email=sanitize_email(wp_unslash($_POST['email']??'')); $from=sanitize_text_field(wp_unslash($_POST['from_city']??'')); $to=sanitize_text_field(wp_unslash($_POST['to_city']??'')); $service=sanitize_text_field(wp_unslash($_POST['service']??'')); $date=sanitize_text_field(wp_unslash($_POST['moving_date']??'')); $message=sanitize_textarea_field(wp_unslash($_POST['message']??'')); if (!$name || !preg_match('/^[6-9][0-9]{9}$/',$mobile) || !$from || !$to || !$service) { wp_safe_redirect(add_query_arg('quote','invalid',wp_get_referer()?:home_url('/'))); exit; } $id=wp_insert_post(['post_type'=>'pioneer_lead','post_status'=>'private','post_title'=>sprintf('%s | %s | %s',$name,$mobile,current_time('Y-m-d H:i'))],true); if (is_wp_error($id)) wp_die('We could not save your enquiry. Please call or WhatsApp Pioneer directly.','Submission failed',['response'=>500]); $fields=['name'=>$name,'mobile'=>$mobile,'email'=>$email,'from'=>$from,'to'=>$to,'service'=>$service,'date'=>$date,'message'=>$message,'status'=>'New','landing'=>esc_url_raw(wp_unslash($_POST['landing_page']??'')),'utm_source'=>sanitize_text_field(wp_unslash($_POST['utm_source']??'')),'utm_medium'=>sanitize_text_field(wp_unslash($_POST['utm_medium']??'')),'utm_campaign'=>sanitize_text_field(wp_unslash($_POST['utm_campaign']??'')),'gclid'=>sanitize_text_field(wp_unslash($_POST['gclid']??'')),'fbclid'=>sanitize_text_field(wp_unslash($_POST['fbclid']??''))]; foreach($fields as $k=>$v) update_post_meta($id,'pioneer_'.$k,$v); $body="New website enquiry\n\nName: {$name}\nMobile: {$mobile}\nEmail: {$email}\nPickup: {$from}\nDestination: {$to}\nService: {$service}\nMoving date: {$date}\nMessage: {$message}\nLead ID: {$id}"; wp_mail(get_option('admin_email'),'New moving enquiry #'.$id.' | Pioneer Packers & Movers',$body); $thanks=get_page_by_path('thank-you'); wp_safe_redirect(add_query_arg('quote','sent',$thanks?get_permalink($thanks->ID):home_url('/'))); exit; } add_shortcode('pioneer_quote_form', function () { $status=sanitize_key($_GET['quote']??''); $notice=''; if($status==='sent') $notice='