/** * Saves the posts from a remote fetch, assumes posts objects are of a particular format */ function ikit_post_external_remote_fetch_save($external_source, $external_source_display_name, $external_source_link_url, $items) { // Find all existing external posts for this source, delete any that are no longer in the feed $existing_posts = ikit_get_posts_by_meta(IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, IKIT_POST_TYPE_IKIT_POST_EXTERNAL, 999999); $existing_post_exists_by_id = array(); foreach ($existing_posts as $existing_post) { $existing_post_exists_by_id[get_post_meta($existing_post->ID, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_ID, true)] = 0; } foreach ($items as $item) { $title = $item->title; $post_date = $item->post_date; $link_url = $item->link_url; $description = $item->description; $author = $item->author; $image_url = $item->image_url; $post_id = $item->post_id; $post = array('post_title' => $title, 'post_type' => IKIT_POST_TYPE_IKIT_POST_EXTERNAL, 'post_content' => $description, 'post_date' => $post_date, 'edit_date' => true); $existing_post_exists_by_id[$post_id] = 1; $existing_post = ikit_get_post_by_meta(IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_ID, $post_id, IKIT_POST_TYPE_IKIT_POST_EXTERNAL); if ($existing_post == null) { $post['post_status'] = 'draft'; $new_post_id = wp_insert_post($post); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_ID, $post_id, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_DISPLAY_NAME, $external_source_display_name, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_POST_ATTRIBUTION, $author, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_IMAGE, $image_url, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_LINK_URL, $external_source_link_url, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_LINK_URL, $link_url, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_DATE, $post_date, true); } else { $post['ID'] = $existing_post->ID; $updated_post_id = wp_update_post($post); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_DISPLAY_NAME, $external_source_display_name, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_POST_ATTRIBUTION, $author, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_IMAGE, $image_url, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_LINK_URL, $external_source_link_url, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_LINK_URL, $link_url, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_DATE, $post_date, true); } } // Delete posts no longer in feed foreach ($existing_posts as $existing_post) { $exists = $existing_post_exists_by_id[get_post_meta($existing_post->ID, IKIT_CUSTOM_FIELD_IKIT_POST_EXTERNAL_ID, true)]; if ($exists == 0) { wp_delete_post($existing_post->ID, true); } } }
/** * Return a post by a meta key value */ function ikit_get_post_by_meta($meta_key, $meta_value, $post_type) { $posts = ikit_get_posts_by_meta($meta_key, $meta_value, $post_type, 1); if (count($posts) > 0) { return $posts[0]; } return null; }
/** * Saves the posts from a remote fetch, assumes posts objects are of a particular format */ function ikit_event_external_remote_fetch_save($external_source, $external_source_display_name, $external_source_link_url, $items) { global $wpdb; $event_external_table_name = $wpdb->prefix . IKIT_EVENT_EXTERNAL_TABLE_NAME; // Find all existing external events for this chapter, delete any that are no longer in the feed $existing_posts = ikit_get_posts_by_meta(IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, IKIT_POST_TYPE_IKIT_EVENT_EXTERNAL, 999999); $existing_post_exists_by_id = array(); foreach ($existing_posts as $existing_post) { $existing_post_exists_by_id[get_post_meta($existing_post->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_EXTERNAL_ID, true)] = 0; } foreach ($items as $item) { $title = $item->title; $link_url = $item->link_url; $image_url = $item->image_url; $description = $item->description; $start_date = $item->start_date; $end_date = $item->end_date; $start_time = $item->start_time; $end_time = $item->end_time; $image_url = $item->image_url; $location = $item->location; $post_id = $item->post_id; // Create an event entry $event_data = array('id' => $post_id, 'url' => $link_url, 'image_url' => $image_url, 'start_date' => $start_date, 'end_date' => $end_date, 'service' => IKIT_EVENT_SERVICE_EXTERNAL, 'start_time' => $start_time, 'end_time' => $end_time, 'location' => $location); $existing_post_exists_by_id[$post_id] = 1; $rows_affected = $wpdb->query("select id from {$event_external_table_name} where id = '{$post_id}'"); if ($rows_affected == 0) { $rows_affected = $wpdb->insert($event_external_table_name, $event_data); } else { $rows_affected = $wpdb->update($event_external_table_name, $event_data, array('id' => $post_id)); } // Each event has an associated WordPress post to aid with custom fields // searching and an auto-generated admin interface for the event $yesterday_gmt = date("Y-m-d H:i:s", time() - 60 * 60 * 24); $yesterday_local = date("Y-m-d H:i:s", time() - 60 * 60 * 24 + get_option('gmt_offset') * 3600); $post = array('post_title' => $title, 'post_type' => IKIT_POST_TYPE_IKIT_EVENT_EXTERNAL, 'post_content' => $description, 'post_date_gmt' => $yesterday_gmt, 'post_date' => $yesterday_local, 'edit_date' => true); $existing_post = ikit_get_post_by_meta(IKIT_CUSTOM_FIELD_IKIT_EVENT_EXTERNAL_ID, $post_id, IKIT_POST_TYPE_IKIT_EVENT_EXTERNAL); if ($existing_post == null) { $post['post_status'] = 'draft'; $new_post_id = wp_insert_post($post); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_EXTERNAL_ID, $post_id, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, $end_date, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, $start_date, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, 'Live', true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_EXTERNAL, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_DISPLAY_NAME, $external_source_display_name, true); add_post_meta($new_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_LINK_URL, $external_source_link_url, true); } else { $post['ID'] = $existing_post->ID; $updated_post_id = wp_update_post($post); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, $end_date, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, $start_date, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, 'Live', true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_EXTERNAL, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, $external_source, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_DISPLAY_NAME, $external_source_display_name, true); ikit_add_or_update_post_meta($updated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE_LINK_URL, $external_source_link_url, true); } } foreach ($existing_posts as $existing_post) { $external_id = get_post_meta($existing_post->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_EXTERNAL_ID, true); $exists = $existing_post_exists_by_id[$external_id]; if ($exists == 0) { wp_delete_post($existing_post->ID, true); // Delete associated database entry global $wpdb; $table_name = $wpdb->prefix . IKIT_EVENT_EXTERNAL_TABLE_NAME; $wpdb->query(sprintf("delete from {$table_name} where id = '%s'", $external_id)); } } }