public function ph_auto_email_match()
 {
     global $post;
     // Auto emails enabled in settings
     // Auto emails not disabled in applicant record
     // Property added more recently that setting enabled in settings
     // Property not already previously sent
     // Valid email address
     // 'Do not email' not selected
     $auto_property_match_enabled = get_option('propertyhive_auto_property_match', '');
     if ($auto_property_match_enabled == '') {
         return false;
     }
     $auto_property_match_enabled_date = get_option('propertyhive_auto_property_match_enabled_date', '');
     if ($auto_property_match_enabled_date == '') {
         return false;
     }
     include_once dirname(__FILE__) . '/admin/class-ph-admin-matching-properties.php';
     $ph_admin_matching_properties = new PH_Admin_Matching_Properties();
     // Get all contacts that have a type of applicant
     $args = array('post_type' => 'contact', 'nopaging' => true, 'meta_query' => array(array('key' => '_contact_types', 'value' => 'applicant', 'compare' => 'LIKE')), 'fields' => 'ids');
     $contact_query = new WP_Query($args);
     if ($contact_query->have_posts()) {
         $default_subject = get_option('propertyhive_property_match_default_email_subject', '');
         $default_body = get_option('propertyhive_property_match_default_email_body', '');
         while ($contact_query->have_posts()) {
             $contact_query->the_post();
             $contact_id = get_the_ID();
             // invalid email address
             if (strpos(get_post_meta($contact_id, '_email_address', TRUE), '@') === FALSE) {
                 continue;
             }
             // email in the list of forbidden contact methods
             $forbidden_contact_methods = get_post_meta($contact_id, '_forbidden_contact_methods', TRUE);
             if (is_array($forbidden_contact_methods) && in_array('email', $forbidden_contact_methods)) {
                 continue;
             }
             $applicant_profiles = get_post_meta($contact_id, '_applicant_profiles', TRUE);
             if ($applicant_profiles != '' && $applicant_profiles > 0) {
                 $dismissed_properties = get_post_meta($contact_id, '_dismissed_properties', TRUE);
                 for ($i = 0; $i < $applicant_profiles; ++$i) {
                     $applicant_profile = get_post_meta($contact_id, '_applicant_profile_' . $i, TRUE);
                     if ($applicant_profile == '' || !is_array($applicant_profile) || !isset($applicant_profile['department'])) {
                         continue;
                     }
                     if (isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == '') {
                         continue;
                     }
                     if (isset($applicant_profile['auto_match_disabled']) && $applicant_profile['auto_match_disabled'] == 'yes') {
                         continue;
                     }
                     $matching_properties = $ph_admin_matching_properties->get_matching_properties($contact_id, $i, $auto_property_match_enabled_date);
                     if (!empty($matching_properties)) {
                         $already_sent_properties = get_post_meta($contact_id, '_applicant_profile_' . $i . '_match_history', TRUE);
                         // Check properties haven't already been sent and not marked as 'not interested'
                         $new_matching_properties = array();
                         foreach ($matching_properties as $matching_property) {
                             if (!isset($already_sent_properties[$matching_property->id]) && !in_array($matching_property->id, $dismissed_properties)) {
                                 $new_matching_properties[] = $matching_property->id;
                             }
                         }
                         if (!empty($new_matching_properties)) {
                             $subject = str_replace("[property_count]", count($new_matching_properties) . ' propert' . (count($new_matching_properties) != 1 ? 'ies' : 'y'), $default_subject);
                             $body = str_replace("[contact_name]", get_the_title($contact_id), $default_body);
                             $body = str_replace("[property_count]", count($new_matching_properties) . ' propert' . (count($new_matching_properties) != 1 ? 'ies' : 'y'), $body);
                             if (strpos($body, '[properties]') !== FALSE) {
                                 ob_start();
                                 if (!empty($new_matching_properties)) {
                                     foreach ($new_matching_properties as $email_property_id) {
                                         $property = new PH_Property((int) $email_property_id);
                                         ph_get_template('emails/applicant-match-property.php', array('property' => $property));
                                     }
                                 }
                                 $body = str_replace("[properties]", ob_get_clean(), $body);
                             }
                             $ph_admin_matching_properties->send_emails($contact_id, $i, $new_matching_properties, get_bloginfo('name'), get_option('admin_email'), $subject, $body);
                         }
                     }
                 }
             }
         }
     }
     wp_reset_postdata();
 }
 /**
  * Init the applicant matching properties page
  */
 public function matching_properties_page()
 {
     include_once 'class-ph-admin-matching-properties.php';
     $ph_admin_matching_properties = new PH_Admin_Matching_Properties();
     $ph_admin_matching_properties->output();
 }