/**
  * Import Facilities
  *
  * @since    1.0.0
  */
 public function import_facilities()
 {
     global $wpdb;
     ini_set('max_execution_time', 600);
     $options = get_option('pressbuilt_display_locator_settings');
     if (!isset($options['import_facilities_last_run'])) {
         $options['import_facilities_last_run'] = strtotime("Yesterday 12:01 AM");
         $options['import_facilities_last_run_lock'] = false;
         update_option('pressbuilt_display_locator_settings', $options);
     }
     if (time() - $options['import_facilities_last_run'] > 86400 && $options['import_facilities_last_run_lock'] === false) {
         $options['import_facilities_last_run_lock'] = true;
         update_option('pressbuilt_display_locator_settings', $options);
         $wpdb->query("UPDATE `wp_posts` SET `post_status` = 'draft' WHERE `post_type` = 'facilities'");
         $facilities = Pressbuilt_Place_Locator::fetch_facilities();
         $insurance_plans = Pressbuilt_Place_Locator::fetch_insurance_plans();
         $services = Pressbuilt_Place_Locator::fetch_services();
         $facility_insurances = Pressbuilt_Place_Locator::fetch_facility_insurance_list();
         $facility_services = Pressbuilt_Place_Locator::fetch_facility_service_list();
         foreach ($facilities as $id => $facility) {
             $post_status = strtolower($facility['Active']) == '1' ? 'publish' : 'draft';
             $post_title = str_replace(' ', '', $facility['FacilityName']);
             $post_args = array('post_type' => 'facilities', 'post_title' => trim($post_title), 'post_content' => '', 'post_status' => $post_status, 'post_author' => 1, 'comment_status' => 'closed', 'ping_status' => 'closed');
             $search_args = array('post_type' => 'facilities', 'meta_query' => array(array('key' => 'pressbuilt_place_locator_facility_id', 'value' => $facility['FacilityID'])));
             $post_query = new WP_Query($search_args);
             if ($post_query->have_posts()) {
                 $post_args['ID'] = $post_query->post->ID;
             }
             $post_id = wp_insert_post($post_args);
             if ($post_id) {
                 if (!empty($facility['Address'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_address1', $facility['Address']);
                 }
                 if (!empty($facility['Address2'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_address2', $facility['Address2']);
                 }
                 if (!empty($facility['CLIANumber'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_clia_number', $facility['CLIANumber']);
                 }
                 if (!empty($facility['City'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_city', $facility['City']);
                 }
                 if (!empty($facility['CorpDate'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_corp_date', $facility['CorpDate']);
                 }
                 if (!empty($facility['CorpID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_corp_id', $facility['CorpID']);
                 }
                 if (!empty($facility['County'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_county', $facility['County']);
                 }
                 if (!empty($facility['CreatedBy'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_created_by', $facility['CreatedBy']);
                 }
                 if (!empty($facility['CreatedDate'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_created_date', $facility['CreatedDate']);
                 }
                 if (!empty($facility['Email'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_email', $facility['Email']);
                 }
                 if (!empty($facility['FacilityID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_facility_id', $facility['FacilityID']);
                 }
                 if (!empty($facility['Fax1'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_fax1', $facility['Fax1']);
                 }
                 if (!empty($facility['Fax2'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_fax2', $facility['Fax2']);
                 }
                 if (!empty($facility['Fax3'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_fax2', $facility['Fax3']);
                 }
                 if (!empty($facility['LastModifiedBy'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_last_modified_by', $facility['LastModifiedBy']);
                 }
                 if (!empty($facility['LastModifiedDate'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_last_modified_date', $facility['LastModifiedDate']);
                 }
                 if (!empty($facility['LegalName'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_legal_name', $facility['LegalName']);
                 }
                 if (!empty($facility['MedicaidID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_medicaid_id', $facility['MedicaidID']);
                 }
                 if (!empty($facility['MedicareID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_medicare_id', $facility['MedicareID']);
                 }
                 if (!empty($facility['NPI'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_npi', $facility['NPI']);
                 }
                 if (!empty($facility['NumBeds'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_num_beds', $facility['NumBeds']);
                 }
                 if (!empty($facility['Phone'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_phone', $facility['Phone']);
                 }
                 if (!empty($facility['State'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_state', $facility['State']);
                 }
                 if (!empty($facility['StateCorp'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_state_corp', $facility['StateCorp']);
                 }
                 if (!empty($facility['StateID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_state_id', $facility['StateID']);
                 }
                 if (!empty($facility['TaxID'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_tax_id', $facility['TaxID']);
                 }
                 if (!empty($facility['Zip'])) {
                     update_post_meta($post_id, 'pressbuilt_place_locator_zip', $facility['Zip']);
                 }
                 $county_terms = array();
                 if (!empty($facility['State'])) {
                     $state_slug = sanitize_title($facility['State']);
                     $state_cat = get_term_by('slug', $state_slug, 'county');
                     if ($state_cat->term_id) {
                         $county_terms[] = $state_cat->term_id;
                     }
                 }
                 if (!empty($facility['County'])) {
                     $county_slug = sanitize_title($facility['State'] . '-' . $facility['County']);
                     $county_cat = get_term_by('slug', $county_slug, 'county');
                     if ($county_cat->term_id) {
                         $county_terms[] = $county_cat->term_id;
                     }
                 }
                 if (!empty($county_terms)) {
                     $county_terms = array_map('intval', $county_terms);
                     $county_terms = array_unique($county_terms);
                     wp_set_object_terms($post_id, $county_terms, 'county');
                 }
                 $fac_insurance_plan_ids = array();
                 if (array_key_exists($id, $facility_insurances)) {
                     foreach ($facility_insurances[$id] as $insurance_id) {
                         if (!empty($insurance_plans[$insurance_id])) {
                             $tag_slug = sanitize_title($insurance_plans[$insurance_id]);
                             $tag = get_term_by('slug', $tag_slug, 'insurance');
                             if ($tag->term_id) {
                                 $fac_insurance_plan_ids[] = $tag->term_id;
                             }
                         }
                     }
                     if (!empty($fac_insurance_plan_ids)) {
                         $fac_insurance_plan_ids = array_map('intval', $fac_insurance_plan_ids);
                         $fac_insurance_plan_ids = array_unique($fac_insurance_plan_ids);
                         wp_set_object_terms($post_id, $fac_insurance_plan_ids, 'insurance');
                     }
                 }
                 $fac_service_ids = array();
                 if (array_key_exists($id, $facility_services)) {
                     foreach ($facility_services[$id] as $service_id) {
                         if (!empty($services[$service_id])) {
                             $tag_slug = sanitize_title($services[$service_id]);
                             $tag = get_term_by('slug', $tag_slug, 'service');
                             if ($tag->term_id) {
                                 $fac_service_ids[] = $tag->term_id;
                             }
                         }
                     }
                     if (!empty($fac_service_ids)) {
                         $fac_service_ids = array_map('intval', $fac_service_ids);
                         $fac_service_ids = array_unique($fac_service_ids);
                         wp_set_object_terms($post_id, $fac_service_ids, 'service');
                     }
                 }
             }
         }
         $options['import_facilities_last_run'] = time();
         $options['import_facilities_last_run_lock'] = false;
         update_option('pressbuilt_display_locator_settings', $options);
     }
 }
 public function shortcode_locator_display($atts)
 {
     $facilities = Pressbuilt_Place_Locator::fetch_facilities();
     $insurance_plans = Pressbuilt_Place_Locator::fetch_insurance_plans();
     $services = Pressbuilt_Place_Locator::fetch_services();
     $counties = $this->fetch_counties();
     ob_start();
     require dirname(__FILE__) . '/partials/shortcode-locator-display.php';
     return ob_get_clean();
 }
/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_pressbuilt_place_locator()
{
    $plugin = new Pressbuilt_Place_Locator();
    $plugin->run();
}