/** * Parses the XML response, deletes existing CPTs, and imports CCB data * * @param mixed $full_response * @since 0.9.0 * @access protected * @return void */ protected function import_cpts($full_response) { global $wpdb; // temporarily disable counting for performance wp_defer_term_counting(true); wp_defer_comment_counting(true); // temporarily disable autocommit $wpdb->query('SET autocommit = 0;'); // GROUP PROFILES if ($this->enabled_apis['group_profiles'] == true && isset($full_response['group_profiles']->response->groups->group) && !empty($full_response['group_profiles']->response->groups->group)) { $groups_taxonomy_map = CCB_Core_CPTs::get_groups_taxonomy_map(); $groups_custom_fields_map = CCB_Core_CPTs::get_groups_custom_fields_map(); // delete the existing taxonomy terms foreach ($groups_taxonomy_map as $taxonomy_name => $taxonomy) { $terms = get_terms($taxonomy_name, array('fields' => 'ids', 'hide_empty' => false)); if (!empty($terms)) { foreach ($terms as $term_value) { wp_delete_term($term_value, $taxonomy_name); } } } // delete existing custom posts $custom_posts = get_posts(array('post_type' => $this->plugin_name . '-groups', 'posts_per_page' => -1)); foreach ($custom_posts as $custom_post) { wp_delete_post($custom_post->ID, true); } // commit the deletes now $wpdb->query('COMMIT;'); foreach ($full_response['group_profiles']->response->groups->group as $group) { // only allow publicly listed and active groups to be imported if ($group->inactive == 'false' && $group->public_search_listed == 'true') { $group_id = 0; foreach ($group->attributes() as $key => $value) { if ($key == 'id') { $group_id = (int) $value; break; } } // insert group post $group_post_atts = array('post_title' => $group->name, 'post_name' => $group->name, 'post_content' => $group->description, 'post_status' => 'publish', 'post_type' => $this->plugin_name . '-groups'); $post_id = wp_insert_post($group_post_atts); // insert hierarchial taxonomy values (categories) and non-hierarchial taxonomy values (tags) $taxonomy_atts = $this->get_taxonomy_atts($group, $groups_taxonomy_map); if (!empty($taxonomy_atts)) { foreach ($taxonomy_atts as $taxonomy_attribute) { wp_set_post_terms($post_id, $taxonomy_attribute['terms'], $taxonomy_attribute['taxonomy'], true); } } // insert custom fields $custom_fields_atts = $this->get_custom_fields_atts($group, $groups_custom_fields_map); if (!empty($custom_fields_atts)) { foreach ($custom_fields_atts as $custom_fields_attribute) { add_post_meta($post_id, $custom_fields_attribute['field_name'], $custom_fields_attribute['field_value']); } } } } // commit the inserts now $wpdb->query('COMMIT;'); } // PUBLIC CALENDAR LISTING if ($this->enabled_apis['public_calendar_listing'] == true && isset($full_response['public_calendar_listing']->response->items->item) && !empty($full_response['public_calendar_listing']->response->items->item)) { $calendar_taxonomy_map = CCB_Core_CPTs::get_calendar_taxonomy_map(); $calendar_custom_fields_map = CCB_Core_CPTs::get_calendar_custom_fields_map(); // delete the existing taxonomy terms foreach ($calendar_taxonomy_map as $taxonomy_name => $taxonomy) { $terms = get_terms($taxonomy_name, array('fields' => 'ids', 'hide_empty' => false)); if (!empty($terms)) { foreach ($terms as $term_value) { wp_delete_term($term_value, $taxonomy_name); } } } // delete existing custom posts $custom_posts = get_posts(array('post_type' => $this->plugin_name . '-calendar', 'posts_per_page' => -1)); foreach ($custom_posts as $custom_post) { wp_delete_post($custom_post->ID, true); } // commit the deletes now $wpdb->query('COMMIT;'); foreach ($full_response['public_calendar_listing']->response->items->item as $event) { // insert event post $event_post_atts = array('post_title' => $event->event_name, 'post_name' => $event->event_name, 'post_content' => $event->event_description, 'post_status' => 'publish', 'post_type' => $this->plugin_name . '-calendar'); $post_id = wp_insert_post($event_post_atts); // insert hierarchial taxonomy values (categories) and non-hierarchial taxonomy values (tags) $taxonomy_atts = $this->get_taxonomy_atts($event, $calendar_taxonomy_map); if (!empty($taxonomy_atts)) { foreach ($taxonomy_atts as $taxonomy_attribute) { wp_set_post_terms($post_id, $taxonomy_attribute['terms'], $taxonomy_attribute['taxonomy'], true); } } // insert custom fields $custom_fields_atts = $this->get_custom_fields_atts($event, $calendar_custom_fields_map); if (!empty($custom_fields_atts)) { foreach ($custom_fields_atts as $custom_fields_attribute) { add_post_meta($post_id, $custom_fields_attribute['field_name'], $custom_fields_attribute['field_value']); } } } // commit the inserts now $wpdb->query('COMMIT;'); } // re-enable autocommit $wpdb->query('SET autocommit = 1;'); // re-enable counting wp_defer_term_counting(false); wp_defer_comment_counting(false); }