<?php
/*
Plugin Name: Contact Form 7 Custom Functions
Description: Contact Form 7 Custom Functions
Author: Amer Saeed
Version: 0.1
*/

// shortcodes to work in Contact Form 7
add_filter( 'wpcf7_form_elements', 'mycustom_wpcf7_form_elements' );
function mycustom_wpcf7_form_elements( $form ) {
$form = do_shortcode( $form );
return $form;
}

// https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/
add_filter('wpcf7_form_tag_data_option', 'populate_cf7_select',10, 3);
function populate_cf7_select($n, $options, $args) {
  if (in_array('products', $options)){
    // $option_data = array(
    //   " - MAY 07 - NEW ORLEANS, LA",
    //   "- MAY 09 - AUSTIN, TX",
    //   "- MAY 12 - HOUSTON, TX"
    // );
    $rows = get_posts(
        array (
         'post_type' => 'product',
         'numberposts' => -1,
         'orderby' => 'title',
         'order' => 'ASC',
       ),    
    );  
    $option_data[] = "Select product";
    foreach ( $rows as $row ) {
        $option_data[] = $row->post_title;
    }
    return $option_data;
  }
  return $n;
}

// https://makemypost.com/how-to-use-contact-form-7-to-save-form-data-in-a-custom-post-with-acf-fields/
// https://wordpress.stackexchange.com/questions/328429/how-to-save-contact-form-7-data-in-custom-post-types-cpt
// add_action('wpcf7_mail_sent','save_my_form_data_to_my_cpt');
// add_action('wpcf7_mail_failed','save_my_form_data_to_my_cpt');

function save_my_form_data_to_my_cpt($contact_form){
    $submission = WPCF7_Submission::get_instance();
    if (!$submission){
        return;
    }
    $posted_data = $submission->get_posted_data();

    //The Sent Fields are now in an array
    //Let's say you got 4 Fields in your Contact Form
    //your-email, your-name, your-subject and your-message
    //you can now access them with $posted_data['your-email']
    //Do whatever you want like:
    $new_post = array();
    if(isset($posted_data['your-subject']) && !empty($posted_data['your-subject'])){
        $new_post['post_title'] = $posted_data['your-subject'];
    } else {
        $new_post['post_title'] = 'Message';
    }
    $new_post['post_type'] = 'lead'; //insert here your CPT
    if(isset($posted_data['your-message'])){
        $new_post['post_content'] = $posted_data['your-message'];
    } else {
        $new_post['post_content'] = 'No Message was submitted';
    }
    $new_post['post_status'] = 'publish';
    //you can also build your post_content from all of the fields of the form, or you can save them into some meta fields
    if(isset($posted_data['your-email']) && !empty($posted_data['your-email'])){
        $new_post['meta_input']['sender_email_address'] = $posted_data['your-email'];
    }
    if(isset($posted_data['your-name']) && !empty($posted_data['your-name'])){
        $new_post['meta_input']['sender_name'] = $posted_data['your-name'];
    }
    //When everything is prepared, insert the post into your Wordpress Database
    if ($contact_form -> id == 353){
      if($post_id = wp_insert_post($new_post)){
         //Everything worked, you can stop here or do whatever
      } else {
         //The post was not inserted correctly, do something (or don't ;) )
      }
    }
    return;
}



add_filter( 'wpcf7_form_tag', 'wc_services_dynamic_select_field_values', 10, 2);
function wc_services_dynamic_select_field_values ( $scanned_tag, $replace ) {

    if ( $scanned_tag['name'] != 'services' )
        return $scanned_tag;

    $rows = get_posts(
    	array (
	     'post_type' => 'product',
	     'numberposts' => -1,
	     'orderby' => 'title',
	     'order' => 'ASC',
       'tax_query' => array( array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => 'services',
          ) ),
        )
    );

    if ( ! $rows )
        return $scanned_tag;

    foreach ( $rows as $row ) {
        $scanned_tag['raw_values'][] = $row->post_title . '|' . $row->post_title;
    }

    $pipes = new WPCF7_Pipes($scanned_tag['raw_values']);

    $scanned_tag['values'] = $pipes->collect_befores();
    $scanned_tag['labels'] = $pipes->collect_afters();
    $scanned_tag['pipes'] = $pipes;

    return $scanned_tag;
}



add_filter( 'wpcf7_form_tag', 'wc_products_dynamic_select_field_values', 10, 2);
function wc_products_dynamic_select_field_values ( $scanned_tag, $replace ) {

    if ( $scanned_tag['name'] != 'products' )
        return $scanned_tag;

    $rows = get_posts(
    	array (
	     'post_type' => 'product',
	     'numberposts' => -1,
	     'orderby' => 'title',
	     'order' => 'ASC',
       'tax_query' => array( array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => 'products',
          ) ),
        )
    );

    if ( ! $rows )
        return $scanned_tag;

    foreach ( $rows as $row ) {
        $scanned_tag['raw_values'][] = $row->post_title . '|' . $row->post_title;
    }

    $pipes = new WPCF7_Pipes($scanned_tag['raw_values']);

    $scanned_tag['values'] = $pipes->collect_befores();
    $scanned_tag['labels'] = $pipes->collect_afters();
    $scanned_tag['pipes'] = $pipes;

    return $scanned_tag;
}




//add_filter( 'parse_query', 'prefix_parse_filter',10, 2 );
function  prefix_parse_filter($query) {

	 $current_page = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
   if ($current_page == "client"){
     $query->query_vars['meta_key']     = 'user';
      $query->query_vars['meta_value']   = 3;
      $query->query_vars['meta_compare'] = '=';

   }

   //global $typenow;
	 // if ( is_admin() && $typenow = 'product' ) {
		//  $query->query_vars['meta_key']     = 'vendor_id';
		//  $query->query_vars['meta_value']   = 2;
		//  $query->query_vars['meta_compare'] = '=';
	 // }
   // elseif ( is_admin() && $typenow = 'client' ) {
 		//  $query->query_vars['meta_key']     = 'user';
 		//  $query->query_vars['meta_value']   = 0;
 		//  $query->query_vars['meta_compare'] = '=';
 	 // }
}
?>
