function ikit_event_internal_save_post($post_id) { $post = get_post($post_id); if ($post->post_type == IKIT_POST_TYPE_IKIT_EVENT_INTERNAL) { ikit_add_or_update_post_meta($post->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_INTERNAL, true); ikit_add_or_update_post_meta($post->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, 'Live', true); global $wpdb; $event_internal_table_name = $wpdb->prefix . IKIT_EVENT_INTERNAL_TABLE_NAME; $start_date = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, true); $end_date = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, true); $location_name = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_NAME, true); $location_address_1 = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_ADDRESS_1, true); $location_address_2 = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_ADDRESS_2, true); $location_city = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_CITY, true); $location_state_province = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_STATE_PROVINCE, true); $location_postal_code = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_POSTAL_CODE, true); $location_country = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_LOCATION_COUNTRY, true); $url = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_URL, true); $url_name = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_URL_NAME, true); $start_time_hour = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_START_TIME_HOUR, true); $start_time_minute = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_START_TIME_MINUTE, true); $end_time_hour = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_END_TIME_HOUR, true); $end_time_minute = get_post_meta($post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_INTERNAL_END_TIME_MINUTE, true); // Create the start and end times based on the repeater fields $start_time = null; if ($start_time_hour != 'null' && $start_time_minute != 'null' && empty($start_time_hour) == false && empty($start_time_minute) == false) { $start_time = $start_time_hour . ':' . $start_time_minute . ':00'; } $end_time = null; if ($end_time_hour != 'null' && $end_time_minute != 'null' && empty($end_time_hour) == false && empty($end_time_minute) == false) { $end_time = $end_time_hour . ':' . $end_time_minute . ':00'; } if (empty($start_date) == false && empty($end_date) == false) { // Create or update the associated database record $event_data = array('id' => $post_id, 'start_date' => $start_date, 'end_date' => $end_date, 'service' => IKIT_EVENT_SERVICE_INTERNAL, 'start_time' => $start_time, 'end_time' => $end_time, 'location_name' => $location_name, 'location_address_1' => $location_address_1, 'location_address_2' => $location_address_2, 'location_city' => $location_city, 'location_state_province' => $location_state_province, 'location_postal_code' => $location_postal_code, 'location_country' => $location_country, 'url' => $url, 'url_name' => $url_name); $rows_affected = $wpdb->query("select id from {$event_internal_table_name} where id = '{$post_id}'"); if ($rows_affected == 0) { $rows_affected = $wpdb->insert($event_internal_table_name, $event_data); } else { $rows_affected = $wpdb->update($event_internal_table_name, $event_data, array('id' => $post_id)); } } } }
function ikit_job_remote_fetch_save($jobs) { global $wpdb; $job_table_name = $wpdb->prefix . IKIT_JOB_TABLE_NAME; foreach ($jobs as $active_job) { $active_job_id = (string) $active_job->id; $job_data = array('id' => $active_job_id, 'title' => $active_job->title, 'description' => ikit_strip_cdata_tags($active_job->description), 'company_name' => ikit_strip_cdata_tags($active_job->company_name), 'contact_information' => ikit_strip_cdata_tags($active_job->contact_information), 'expertise_area' => $active_job->expertise_area, 'experience_length' => $active_job->experience_length, 'city' => $active_job->city, 'state' => $active_job->state, 'country' => $active_job->country, 'other_skills' => $active_job->other_skills, 'apply_online_email' => $active_job->apply_online_email, 'date_approved' => $active_job->date_approved, 'expiration_date' => $active_job->expiration_date, 'job_level' => $active_job->job_level, 'application_url' => ikit_strip_cdata_tags($active_job->application_url), 'application_url_text' => ikit_strip_cdata_tags($active_job->application_url_text), 'submission_details' => ikit_strip_cdata_tags($active_job->submission_details), 'job_functions' => ikit_strip_cdata_tags($active_job->job_functions)); $rows_affected = $wpdb->query("select id from {$job_table_name} where id = {$active_job_id}"); if ($rows_affected == 0) { $rows_affected = $wpdb->insert($job_table_name, $job_data); } else { $rows_affected = $wpdb->update($job_table_name, $job_data, array('id' => $active_job_id)); } // Each job has an associated WordPress post to aid with custom fields $associated_post = array('post_title' => (string) $active_job->title, 'post_type' => IKIT_POST_TYPE_IKIT_JOB, 'post_status' => 'publish', 'post_content' => (string) ikit_strip_cdata_tags($active_job->description), 'post_date' => (string) $active_job->date_approved); $existing_associated_post = ikit_get_post_by_meta(IKIT_CUSTOM_FIELD_IKIT_JOB_ID, $active_job_id, IKIT_POST_TYPE_IKIT_JOB); if ($existing_associated_post == null) { $new_associated_post_id = wp_insert_post($associated_post); if ($new_associated_post_id > 0) { // Add the job id to link this post with the job add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_ID, $active_job_id, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_CITY, (string) $active_job->city, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_STATE, (string) $active_job->state, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_LEVEL, (string) $active_job->job_level, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_EXPERTISE_AREA, (string) $active_job->expertise_area, true); } } else { $associated_post['ID'] = $existing_associated_post->ID; $updated_associated_post_id = wp_update_post($associated_post); if ($updated_associated_post_id > 0) { ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_CITY, (string) $active_job->city, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_STATE, (string) $active_job->state, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_LEVEL, (string) $active_job->job_level, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_JOB_EXPERTISE_AREA, (string) $active_job->expertise_area, true); } } } }
/** * Reads in the pages RSS feed as determined by the settings * and adds/updates/deletes based on that feed, also sets * the external source for the item so that it doesn't get deleted * or edited by the user. The page content can then be placed * within other pages using the ikit page shortcode. */ function ikit_national_remote_fetch_page() { $page_feed_url = IKIT_NATIONAL_FEED_PAGE_URL; // Use SimplePie to parse the feeds // note that we turn off cacheing, because this fetch // is called at the proper interval and should always // be freshest $feed = new SimplePie(); $feed->set_feed_url($page_feed_url); $feed->enable_cache(false); $feed->strip_attributes(false); $feed->init(); $feed->handle_content_type(); $feed_page_slugs = array(); foreach ($feed->get_items() as $item) { $page_title = $item->get_title(); $page_description = $item->get_description(); // Extract the slug $page_slug_el = $item->get_item_tags(IKIT_XML_NAMESPACE_URI, 'pageSlug'); $page_slug = $page_slug_el[0]['data']; $feed_page_slugs[$page_slug] = true; // Add or update existing sponsor $associated_post = array('post_title' => $page_title, 'post_name' => $page_slug, 'post_type' => 'page', 'post_status' => 'publish', 'post_content' => $page_description); $existing_associated_post = ikit_get_post_by_slug($page_slug, 'page'); if ($existing_associated_post == null) { $new_associated_post_id = wp_insert_post($associated_post); if ($new_associated_post_id > 0) { // Add custom fields add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, IKIT_EXTERNAL_SOURCE_NATIONAL, true); } } else { $associated_post['ID'] = $existing_associated_post->ID; $updated_associated_post_id = wp_update_post($associated_post); if ($updated_associated_post_id > 0) { ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, IKIT_EXTERNAL_SOURCE_NATIONAL, true); } } } if (count($feed_page_slugs) > 0 && count($feed->get_items()) == count($feed_page_slugs)) { // Safety check to not just delete randomly, should be at least one page in the feed // Delete any non-existing sponsors marked with same external source $args = array('post_type' => 'page', 'meta_key' => IKIT_CUSTOM_FIELD_GENERIC_EXTERNAL_SOURCE, 'meta_value' => IKIT_EXTERNAL_SOURCE_NATIONAL); $pages = get_posts($args); foreach ($pages as $page) { if (array_key_exists($page->post_name, $feed_page_slugs) == false) { // Delete wp_delete_post($page->ID, true); } } } }
/** * Assumes the meta value is an attachment ID, adds or updates attachment, will * do a test for the file name, if it's the same won't update as is unneccesary */ function ikit_media_sideload_image_meta($post_id, $image_url, $image_url_meta_key) { $existing_attachment_id = get_post_meta($post_id, $image_url_meta_key, true); if (isset($existing_attachment_id)) { $existing_attachment_url = wp_get_attachment_url($existing_attachment_id); if (basename($existing_attachment_url) != basename($image_url)) { wp_delete_attachment($existing_attachment_id); $attachment_id = ikit_media_sideload_image($image_url, $post_id); ikit_add_or_update_post_meta($post_id, $image_url_meta_key, $attachment_id, true); } } else { $attachment_id = ikit_media_sideload_image($image_url, $post_id); ikit_add_or_update_post_meta($post_id, $image_url_meta_key, $attachment_id, true); } }
function ikit_updates() { // 20150608, update the eyeondesign feed if (!get_option('ikit_update_eyeondesign_feed_20150608')) { update_option('ikit_update_eyeondesign_feed_20150608', 1); ikit_social_remote_fetch_eyeondesign_rss(); } // 20150505, change the page template of "events" to use the table of contents // this is okay for ikit one and two themes if (!get_option('ikit_update_events_template_20150505')) { update_option('ikit_update_events_template_20150505', 1); $events_page = get_page_by_path('events'); if ($events_page != null) { print_r($events_page); update_post_meta($events_page->ID, '_wp_page_template', 'section_toc.php'); } } // 20141030, update the membership rates page template if (!get_option('ikit_update_membershiprates_template_20141030')) { update_option('ikit_update_membershiprates_template_20141030', 1); $membership_rates_page = get_page_by_path(IKIT_SLUG_PAGE_MEMBERSHIP . '/' . IKIT_SLUG_PAGE_MEMBERSHIP_RATES); if ($membership_rates_page != null) { update_post_meta($membership_rates_page->ID, '_wp_page_template', 'membership_rates.php'); } } // 20120809, update the menu items if (!get_option('ikit_update_menu_items_20120809')) { update_option('ikit_update_menu_items_20120809', 1); $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { // Correct the link for Living Principles (it has an extra http// in it) if ($nav_menu_item->title == 'The Living Princples') { $post = array(); $post['ID'] = $nav_menu_item->ID; $post['post_title'] = 'The Living Principles'; wp_update_post($post); ikit_add_or_update_post_meta($nav_menu_item->ID, '_menu_item_url', 'http://www.livingprinciples.org/', true); } } } // 20120621, add a one time flush rewrite as permalink // structure changed for event single and job single. if (!get_option('ikit_update_flush_rewrite_rules_20120621')) { update_option('ikit_update_flush_rewrite_rules_20120621', 1); flush_rewrite_rules(); } // 20120710, update the menu items if (!get_option('ikit_update_menu_items_20120710')) { update_option('ikit_update_menu_items_20120710', 1); $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { // Update events archive to past events if ($nav_menu_item->title == 'Events Archive') { $post = array(); $post['ID'] = $nav_menu_item->ID; $post['post_title'] = 'Past Events'; wp_update_post($post); } // Delete Design Forum; it no longer exists. if ($nav_menu_item->title == 'Design Forum') { wp_delete_post($nav_menu_item->ID); } // Change the link for Design & Business to http://www.aiga.org/Resources/ if ($nav_menu_item->title == 'Design & Business') { ikit_add_or_update_post_meta($nav_menu_item->ID, '_menu_item_url', 'http://www.aiga.org/resources/', true); } // Change Design Salary Survey to AIGA | Aquent Survey of Design Salaries if ($nav_menu_item->title == 'Design Salary Survey') { $post = array(); $post['ID'] = $nav_menu_item->ID; $post['post_title'] = 'AIGA | Aquent Survey of Design Salaries'; wp_update_post($post); } // Correct the link for Living Principles (it has an extra http// in it) if ($nav_menu_item->title == 'Living Principles') { ikit_add_or_update_post_meta($nav_menu_item->ID, '_menu_item_url', 'http://www.livingprinciples.org/', true); } } } // 20130128, delete all acf groups, we will use the code instead to register them so they are easily updateable if (!get_option('ikit_update_acf_20130128')) { update_option('ikit_update_acf_20130128', 1); ikit_delete_posts('acf'); } // 20130203, update the slug of the featured image gallery if not set properly if (!get_option('ikit_update_image_gallery_featured_20130203')) { update_option('ikit_update_image_gallery_featured_20130203', 1); $args = array('post_type' => IKIT_POST_TYPE_IKIT_IMAGE_GALLERY); $posts = get_posts($args); foreach ($posts as $post) { if ($post->post_name = 'featured') { $post->post_name = IKIT_SLUG_IKIT_IMAGE_GALLERY_FEATURED; wp_update_post($post); break; } } } // 20130206, add member directory page, and then assign to about us menu if (!get_option('ikit_update_member_directory_20130206')) { global $g_options; // Only perform is an imus chapter is set, otherwise // we would display to users a blank members page $imus_chapter = null; if (ikit_isset_not_empty($g_options[IKIT_PLUGIN_OPTION_FILTER_IKIT_IMUS_CHAPTER])) { update_option('ikit_update_member_directory_20130206', 1); $member_directory_page = ikit_get_post_by_slug(IKIT_SLUG_PAGE_MEMBER_DIRECTORY, 'page'); if ($member_directory_page == null) { // Create member directory page $member_directory_page['post_type'] = 'page'; $member_directory_page['post_status'] = 'publish'; $member_directory_page['post_title'] = 'Member Directory'; $member_directory_page['post_name'] = IKIT_SLUG_PAGE_MEMBER_DIRECTORY; // Assign about us as parent $membership_page = ikit_get_post_by_slug(IKIT_SLUG_PAGE_MEMBERSHIP, 'page'); $member_directory_page['post_parent'] = $membership_page->ID; $member_directory_page_id = wp_insert_post($member_directory_page); if ($member_directory_page_id == 0) { // If error, try again next time update_option('ikit_update_member_directory_20130206', 0); } else { // Assign member directory template update_post_meta($member_directory_page_id, '_wp_page_template', 'member_directory.php'); // Add to about us menu item $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { if ($nav_menu_item->title == 'Membership') { wp_update_nav_menu_item($nav_menu->term_id, 0, array('menu-item-title' => __('Member Directory'), 'menu-item-parent-id' => $nav_menu_item->ID, 'menu-item-object' => 'page', 'menu-item-object-id' => $member_directory_page_id, 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish')); break; } } // Force pull of member feed // ikit_national_remote_fetch_member_feed(); } } } } // 20130212, update member directory if (!get_option('ikit_update_member_directory_20130212')) { update_option('ikit_update_member_directory_20130212', 1); // Force pull of member feed // ikit_national_remote_fetch_member_feed(); } // 20130213, update member directory post NimbleUser fix if (!get_option('ikit_update_member_directory_20130213')) { update_option('ikit_update_member_directory_20130213', 1); // Force pull of member feed // ikit_national_remote_fetch_member_feed(); } // 20130219, update member directory as 999 limit was too small for certain chapters if (!get_option('ikit_update_member_directory_20130219')) { update_option('ikit_update_member_directory_20130219', 1); // Force pull of member feed // ikit_national_remote_fetch_member_feed(); } // 20130227, update about description to be 67 chapters if (!get_option('ikit_update_about_description_20130227')) { update_option('ikit_update_about_description_20130227', 1); $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { // Update events archive to past events if ($nav_menu_item->title == 'About Us') { $updated_post_content = str_replace('66', '67', $nav_menu_item->post_content); $nav_menu_item->post_content = $updated_post_content; wp_update_post($nav_menu_item); break; } } } // 20130408, update member directory as there is now a new database for it if (!get_option('ikit_update_member_database_20130408')) { update_option('ikit_update_member_database_20130408', 1); ikit_member_remote_fetch(); } // 20130521, recover from error with datagram where can't access chapters.aiga.org portfolio feed, so truncated portfolios, this update checks if no portfolios, and if so repopulates if (!get_option('ikit_update_portfolios_if_empty_20130521')) { update_option('ikit_update_portfolios_if_empty_20130521', 1); global $wpdb; $project_table_name = $wpdb->prefix . IKIT_PORTFOLIO_PROJECT_TABLE_NAME; $rows_affected = $wpdb->query("select * from {$project_table_name}"); if ($rows_affected == 0) { ikit_portfolio_remote_fetch(); } } // 20130624, set the status of published events to 'Live', future pulls will auto-set from etouches // note that we know published events are either 'Live' or 'Sold Out', either of which are fine to display if (!get_option('ikit_update_event_status_published_to_live_20130624')) { update_option('ikit_update_event_status_published_to_live_20130624', 1); // Get the sponsors $args = array('numberposts' => 999, 'offset' => 0, 'order' => 'ASC', 'post_type' => IKIT_POST_TYPE_IKIT_EVENT, 'post_status' => 'publish'); $events = get_posts($args); foreach ($events as $event) { ikit_add_or_update_post_meta($event->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, 'Live', true); } } // 20131017, save the post orderings, this is a special meta field which holds // a special ordering field that can be used to order events and posts, the // new display priority field will appear for admins for events and posts if (!get_option('ikit_update_save_post_orderings_20131017')) { update_option('ikit_update_save_post_orderings_20131017', 1); ikit_save_post_orderings(); } // 20131101, update wp_ikit_event entries with service eventbrite if (!get_option('ikit_update_eventdb_setservice_20131101')) { update_option('ikit_update_eventdb_setservice_20131101', 1); global $wpdb; $event_table_name = $wpdb->prefix . IKIT_EVENT_ETOUCHES_TABLE_NAME; $wpdb->query("update {$event_table_name} set service = 'etouches'"); } // 20131101, update event service meta for existing events if (!get_option('ikit_update_eventservicemeta_20131101')) { update_option('ikit_update_eventservicemeta_20131101', 1); $events = get_posts(array('post_type' => array(IKIT_POST_TYPE_IKIT_EVENT), 'posts_per_page' => 9999)); foreach ($events as $event) { ikit_add_or_update_post_meta($event->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_ETOUCHES, true); } } // 20131105, update the member id to a varchar instead of int, as it has leading zeroes, so refresh the feed if (!get_option('ikit_update_updatedmemberid_20131105')) { update_option('ikit_update_updatedmemberid_20131105', 1); ikit_member_remote_fetch(); } // 20130227, update about description to be 67 chapters if (!get_option('ikit_update_about_description_20140712')) { update_option('ikit_update_about_description_20140712', 1); $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { // Update events archive to past events if ($nav_menu_item->title == 'About Us') { $updated_post_content = str_replace('67', '68', $nav_menu_item->post_content); $nav_menu_item->post_content = $updated_post_content; wp_update_post($nav_menu_item); break; } } } // 20141119, update about description to be 69 chapters if (!get_option('ikit_update_about_description_20141119')) { update_option('ikit_update_about_description_20141119', 1); $nav_menu_locations = get_nav_menu_locations(); $nav_menu = wp_get_nav_menu_object($nav_menu_locations['nav-menu-main']); $nav_menu_items = wp_get_nav_menu_items($nav_menu->term_id); foreach ((array) $nav_menu_items as $key => $nav_menu_item) { // Update events archive to past events if ($nav_menu_item->title == 'About Us') { $updated_post_content = str_replace('67', '69', $nav_menu_item->post_content); $nav_menu_item->post_content = $updated_post_content; wp_update_post($nav_menu_item); $updated_post_content = str_replace('68', '69', $nav_menu_item->post_content); $nav_menu_item->post_content = $updated_post_content; wp_update_post($nav_menu_item); break; } } } }
function ikit_save_post_orderings() { $persons = get_posts(array('post_type' => array(IKIT_POST_TYPE_IKIT_PERSON), 'posts_per_page' => 9999)); $posts = get_posts(array('post_type' => array('post', IKIT_POST_TYPE_IKIT_POST_EXTERNAL), 'posts_per_page' => 9999)); $events = get_posts(array('post_type' => array(IKIT_POST_TYPE_IKIT_EVENT, IKIT_POST_TYPE_IKIT_EVENT_EXTERNAL, IKIT_POST_TYPE_IKIT_EVENT_INTERNAL), 'posts_per_page' => 9999)); // By display order, admins optionally set a display priority on events and news items, this value // is then multiplied by the post date as an integer value, that value then represents the priority field // which can then be used to order the events or news postings $impossible_future_date = strtotime('2200-01-01'); $impossible_past_date = strtotime('1900-01-01'); foreach ($persons as $person) { $ordering_display_priority = null; $display_priority = get_post_meta($person->ID, IKIT_CUSTOM_FIELD_IKIT_PERSON_DISPLAY_PRIORITY, true); if (empty($display_priority)) { // Normally will order by the last name, which is whatever we find after splitting by spaces in the last index $post_title_split = explode(' ', $person->post_title); $ordering_display_priority = strtolower($post_title_split[count($post_title_split) - 1]); } else { // Period to start will always appear before any letter, then we use a numeric value to generate a pseudo string that will order correctly... $ordering_display_priority = '.' . ($display_priority * 60 * 60 * 24 - $impossible_future_date); } ikit_add_or_update_post_meta($person->ID, IKIT_CUSTOM_FIELD_GENERIC_ORDERING_DISPLAY_PRIORITY, $ordering_display_priority, true); } foreach ($posts as $post) { $ordering_display_priority = null; $display_priority = get_post_meta($post->ID, IKIT_CUSTOM_FIELD_POST_DISPLAY_PRIORITY, true); if (empty($display_priority)) { $ordering_display_priority = strtotime($post->post_date); } else { $ordering_display_priority = $impossible_future_date + $display_priority * 60 * 60 * 24; // Take the impossible future date and append the number of days of display priority } ikit_add_or_update_post_meta($post->ID, IKIT_CUSTOM_FIELD_GENERIC_ORDERING_DISPLAY_PRIORITY, $ordering_display_priority, true); } foreach ($events as $event) { $event_start_date = get_post_meta($event->ID, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, true); $ordering_display_priority = null; $display_priority = get_post_meta($event->ID, IKIT_CUSTOM_FIELD_POST_DISPLAY_PRIORITY, true); if (empty($display_priority)) { $ordering_display_priority = strtotime($event_start_date); } else { $ordering_display_priority = $impossible_past_date - $display_priority * 60 * 60 * 24; // Take the impossible future date and append the number of days of display priority } ikit_add_or_update_post_meta($event->ID, IKIT_CUSTOM_FIELD_GENERIC_ORDERING_DISPLAY_PRIORITY, $ordering_display_priority, true); } }
/** * 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); } } }
/** * Save event feed, saves or updates events in the database */ function ikit_event_etouches_remote_fetch_save($events) { global $wpdb; $event_table_name = $wpdb->prefix . IKIT_EVENT_ETOUCHES_TABLE_NAME; foreach ($events as $event) { $event_id = (string) $event->eventid; // Do not include error conditioned events or those without live status if ($event_id <= 0) { continue; } $event_data = array('id' => $event_id, 'timezone_id' => $event->timezoneid, 'name' => $event->name, 'code' => $event->code, 'start_date' => $event->startdate, 'end_date' => $event->enddate, 'status' => $event->status, 'description' => $event->description, 'program_manager' => $event->programmanager, 'default_language' => $event->defaultlanguage, 'creation_date' => $event->createddatetime, 'last_modified_date' => $event->modifieddatetime, 'location_name' => $event->location->name, 'location_address1' => $event->location->address1, 'location_address2' => $event->location->address2, 'location_city' => $event->location->city, 'location_state' => $event->location->state, 'location_country' => $event->location->country, 'location_phone' => $event->location->phone, 'location_email' => $event->location->email, 'url' => $event->url, 'start_time' => $event->starttime, 'end_time' => $event->endtime, 'close_date' => $event->closedate, 'close_time' => $event->closetime, 'homepage_url' => $event->homepage, 'timezone_description' => $event->timezone, 'folder_id' => $event->folderid, 'folder_name' => $event->foldername, 'client_contact' => $event->clientcontact, 'service' => IKIT_EVENT_SERVICE_ETOUCHES); // Allow for null postcodes if (empty($event->location->postcode) == false) { $event_data['location_postcode'] = $event->location->postcode; } $rows_affected = $wpdb->query("select id from {$event_table_name} where id = {$event_id}"); if ($rows_affected == 0) { $rows_affected = $wpdb->insert($event_table_name, $event_data); } else { $rows_affected = $wpdb->update($event_table_name, $event_data, array('id' => $event_id)); } $post_status = 'publish'; if ($event->status != 'Live' && $event->status != 'Sold Out' && $event->status != 'Closed') { $post_status = 'draft'; } // 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); $associated_post = array('post_title' => (string) $event->name, 'post_type' => IKIT_POST_TYPE_IKIT_EVENT, 'post_status' => $post_status, 'post_content' => (string) $event->description, 'post_date_gmt' => $yesterday_gmt, 'post_date' => $yesterday_local, 'edit_date' => true); $existing_associated_post = ikit_get_post_by_meta(IKIT_CUSTOM_FIELD_IKIT_EVENT_ID, $event_id, IKIT_POST_TYPE_IKIT_EVENT); if ($existing_associated_post == null) { $new_associated_post_id = wp_insert_post($associated_post); if ($new_associated_post_id > 0) { // Add the event id to link this post with the event add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_ID, $event_id, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, (string) $event->enddate, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, (string) $event->startdate, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, (string) $event->status, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_ETOUCHES, true); // If in the no reg national folder, auto set to disabled reg if ($event_data['folder_id'] == IKIT_EVENT_FOLDER_ID_NATIONAL_REGISTRATION_TYPE_DISABLED) { add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_REGISTRATION_TYPE, IKIT_CUSTOM_FIELD_IKIT_EVENT_REGISTRATION_TYPE_DISABLED, true); } } } else { $associated_post['ID'] = $existing_associated_post->ID; // If admin explicity sent event to trash, leave it there if ($existing_associated_post->post_status == 'trash') { $associated_post['post_status'] = 'trash'; } $updated_associated_post_id = wp_update_post($associated_post); if ($updated_associated_post_id > 0) { ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, (string) $event->enddate, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, (string) $event->startdate, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, (string) $event->status, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_ETOUCHES, true); // If in the no reg national folder, auto set to disabled reg if ($event_data['folder_id'] == IKIT_EVENT_FOLDER_ID_NATIONAL_REGISTRATION_TYPE_DISABLED) { ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_REGISTRATION_TYPE, IKIT_CUSTOM_FIELD_IKIT_EVENT_REGISTRATION_TYPE_DISABLED, true); } } } } }
function ikit_event_eventbrite_remote_fetch_save($events) { global $wpdb; $event_eventbrite_table_name = $wpdb->prefix . IKIT_EVENT_EVENTBRITE_TABLE_NAME; foreach ($events as $event) { $event = $event->event; $event_id = (string) $event->id; $event_start_date = ikit_date_without_time($event->start_date); $event_end_date = ikit_date_without_time($event->end_date); $event_start_time = ikit_time_without_date($event->start_date); $event_end_time = ikit_time_without_date($event->end_date); $event_data = array('id' => $event_id, 'url' => $event->url, 'start_date' => $event_start_date, 'end_date' => $event_end_date, 'description' => $event->description, 'status' => $event->status, 'title' => $event->title, 'venue_name' => $event->venue->name, 'venue_address' => $event->venue->address, 'venue_address2' => $event->venue->address2, 'venue_city' => $event->venue->city, 'venue_region' => $event->venue->region, 'venue_postal_code' => $event->venue->postal_code, 'venue_country' => $event->venue->country, 'venue_country_code' => $event->venue->country_code, 'venue_longitude' => $event->venue->longitude, 'venue_latitude' => $event->venue->latitude, 'logo' => $event->logo, 'service' => IKIT_EVENT_SERVICE_EVENTBRITE, 'start_time' => $event_start_time, 'end_time' => $event_end_time); $rows_affected = $wpdb->query("select id from {$event_eventbrite_table_name} where id = {$event_id}"); if ($rows_affected == 0) { $rows_affected = $wpdb->insert($event_eventbrite_table_name, $event_data); } else { $rows_affected = $wpdb->update($event_eventbrite_table_name, $event_data, array('id' => $event_id)); } $post_status = 'publish'; if ($event->status != 'Live' && $event->status != 'Completed' && $event->status != 'Started') { $post_status = 'draft'; } // 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); $associated_post = array('post_title' => (string) $event->title, 'post_type' => IKIT_POST_TYPE_IKIT_EVENT, 'post_status' => $post_status, 'post_content' => (string) $event->description, 'post_date_gmt' => $yesterday_gmt, 'post_date' => $yesterday_local, 'edit_date' => true); $event_end_date = ikit_date_without_time($event->end_date); $event_start_date = ikit_date_without_time($event->start_date); $existing_associated_post = ikit_get_post_by_meta(IKIT_CUSTOM_FIELD_IKIT_EVENT_EVENTBRITE_ID, $event_id, IKIT_POST_TYPE_IKIT_EVENT); if ($existing_associated_post == null) { $new_associated_post_id = wp_insert_post($associated_post); if ($new_associated_post_id > 0) { // Add the event id to link this post with the event add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_EVENTBRITE_ID, $event_id, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_EVENTBRITE, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, (string) $event->status, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, $event_end_date, true); add_post_meta($new_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, $event_start_date, true); } } else { $associated_post['ID'] = $existing_associated_post->ID; $updated_associated_post_id = wp_update_post($associated_post); if ($updated_associated_post_id > 0) { ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_SERVICE, IKIT_EVENT_SERVICE_EVENTBRITE, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_STATUS, (string) $event->status, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_END_DATE, $event_end_date, true); ikit_add_or_update_post_meta($updated_associated_post_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_START_DATE, $event_start_date, true); } } } }
/** * 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)); } } }