function fbes_send_events($events) { $eids = array(); $query = new WP_Query(array('post_type' => 'tribe_events', 'posts_per_page' => '-1')); foreach ($query->posts as $post) { if (!empty($post->to_ping)) { $segments = fbes_segments($post->to_ping); $eid = array_pop($segments); $eids[$eid] = $post->ID; } //if you're reading this and you want to delete all those duplicate events, uncomment this temporarially. Note, it will also delete all manually made events since June 13 //http://codex.wordpress.org/Version_3.4 - June 13, 2012 //depending on many duplicates you had, you might end up re-loading this script a bunch of times after it times out. Me, I had 14k duplicates. Had to run the script like 10 times. /* else { $post_date = trim(substr($post->post_date, 0, 10)); if($post->post_date > '2012-06-12') wp_delete_post($post->ID); } */ } //file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fbevent.log', print_r(array(time(),$events,$eids),1)."\n".str_repeat('=',40)."\n", FILE_APPEND); foreach ($events as $event) { $args['post_title'] = $event['name']; $offset = get_option('gmt_offset') * 3600; $offsetStart = $event['start_time'] + $offset; $offsetEnd = $event['end_time'] + $offset; //don't update or insert events from the past. if ($offsetStart > time()) { $args['EventStartDate'] = date("Y-m-d", $offsetStart); $args['EventStartHour'] = date("H", $offsetStart); $args['EventStartMinute'] = date("i", $offsetStart); $args['EventEndDate'] = date("Y-m-d", $offsetEnd); $args['EventEndHour'] = date("H", $offsetEnd); $args['EventEndMinute'] = date("i", $offsetEnd); $args['post_content'] = $event['description']; $args['Venue']['Venue'] = $event['location']; $args['post_status'] = "Publish"; $args['post_type'] = "tribe_events"; //$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4 $args['to_ping'] = 'https://www.facebook.com/events/' . $event['id'] . '/'; $inserting = $post_id = false; if (!array_key_exists($event['id'], $eids)) { //double check $already_exists = false; foreach ($query->posts as $post) { if ($post->to_ping == $args['to_ping'] || trim($post->pinged) == $args['to_ping']) { $already_exists = true; } } if (!$already_exists) { file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/fbevent.log', print_r(array(time(), 'creating', $args, $eids, $query->posts), 1) . "\n" . str_repeat('=', 40) . "\n", FILE_APPEND); $post_id = tribe_create_event($args); echo "<br />Inserting: " . $post_id; $inserting = true; } } if (!$inserting) { $post_id = $eids[$event['id']]; echo "{$post_id}"; tribe_update_event($post_id, $args); echo "<br />Updating: " . $eids[$event['id']]; } if ($post_id) { update_metadata('post', $post_id, 'fb_event_obj', $event); } //eid, name, start_time, end_time, location, description } if ($eids) { reset($eids); } } }
/** * Imports the schedule. * * @access public * @since 1.0.0 */ public function import_wordcamp_schedule() { global $wpdb; // There's no point if TEC doesn't exist if (!function_exists('tribe_create_event')) { return false; } // Get the schedule if (($response = wp_remote_get('https://central.wordcamp.org/wp-json/posts?type=wordcamp&filter[posts_per_page]=30')) && ($body = wp_remote_retrieve_body($response)) && ($schedule = json_decode($body))) { // Get current WordCamp IDs so we don't duplicate events $wordcamp_ids = $wpdb->get_col("SELECT meta.meta_value FROM {$wpdb->postmeta} meta INNER JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id AND posts.post_type = 'tribe_events' WHERE meta.meta_key = '_wordcamp_id'"); // Set datetime format $date_format = 'Y-m-d'; // H:i:s'; foreach ($schedule as $event) { // Get post ID $event_post_id = in_array($event->ID, $wordcamp_ids) ? $wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wordcamp_id' AND meta_value = '{$event->ID}'") : false; // Setup event args $event_args = array('post_title' => $event->title, 'post_content' => $event->content, 'post_status' => 'publish', 'EventAllDay' => true, 'EventTimezone' => $event->date_tz); // Set timezone $event_timezone = new DateTimeZone($event->date_tz); // Will hold venue info $event_venue = array(); // Process all the meta foreach ($event->post_meta as $meta) { // Get meta value $meta_value = $meta->value; // Get start date if (preg_match('/^Start\\sDate/i', $meta->key)) { // We must have a value if ($meta_value) { // Create DateTime $start_date = new DateTime(date('Y-m-d', $meta_value), $event_timezone); // Store for event $event_args['EventStartDate'] = $start_date->format($date_format); } else { // Change status to draft since no start date $event_args['post_status'] = 'draft'; // No start date $event_args['EventStartDate'] = null; } } else { if (preg_match('/^End\\sDate/i', $meta->key)) { // We must have a value if ($meta_value) { // Create DateTime $end_date = new DateTime(date('Y-m-d', $meta_value), $event_timezone); // Store for event $event_args['EventEndDate'] = $end_date->format($date_format); } } else { if ($meta_value) { switch ($meta->key) { case 'URL': $event_args['_EventURL'] = $meta_value; break; case 'Venue Name': $event_venue['Venue'] = $meta_value; break; } } } } } // If we have event info if (!empty($event_venue)) { $event_args['Venue'] = $event_venue; } // If we have event info... if (!empty($event_args)) { // Make sure we have an end date if (!$event_args['EventEndDate']) { $event_args['EventEndDate'] = $event_args['EventStartDate']; } // Update the event if (in_array($event->ID, $wordcamp_ids)) { // Update the event if ($event_post_id > 0) { // @TODO Don't update the post content // until I figure out how to not override changes unset($event_args['post_content']); tribe_update_event($event_post_id, $event_args); } } else { if ($event_post_id = tribe_create_event($event_args)) { // Store the WordCamp ID if ($event_post_id > 0) { add_post_meta($event_post_id, '_wordcamp_id', $event->ID, true); } } } // Make sure the category is set if ($event_post_id > 0) { wp_set_object_terms($event_post_id, 'wordcamps', 'tribe_events_cat', true); } } } } }
/** * Inserts events, venues, and organizers for the Import Record * * @param array $data Dummy data var to allow children to optionally react to passed in data * * @return array|WP_Error */ public function insert_posts($items = array()) { add_filter('tribe-post-origin', array(Tribe__Events__Aggregator__Records::instance(), 'filter_post_origin'), 10); // Creates an Activity to log what Happened $activity = new Tribe__Events__Aggregator__Record__Activity(); $args = array('post_status' => $this->meta['post_status']); $unique_field = $this->get_unique_field(); $existing_ids = $this->get_existing_ids_from_import_data($items); //cache $possible_parents = array(); $found_organizers = array(); $found_venues = array(); //if we have no non recurring events the message may be different $non_recurring = false; $show_map_setting = Tribe__Events__Aggregator__Settings::instance()->default_map($this->meta['origin']); $update_authority_setting = Tribe__Events__Aggregator__Settings::instance()->default_update_authority($this->meta['origin']); $unique_inserted = array(); foreach ($items as $item) { $event = Tribe__Events__Aggregator__Event::translate_service_data($item); // set the event ID if it can be set if ($unique_field && isset($event[$unique_field['target']]) && isset($existing_ids[$event[$unique_field['target']]])) { $event['ID'] = $existing_ids[$event[$unique_field['target']]]->post_id; } // only set the post status if there isn't an ID if (empty($event['ID'])) { $event['post_status'] = $args['post_status']; } /** * Should events that have previously been imported be overwritten? * * By default this is turned off (since it would reset the post status, description * and any other fields that have subsequently been edited) but it can be enabled * by returning true on this filter. * * @var bool $overwrite * @var int $event_id */ if (!empty($event['ID']) && 'retain' === $update_authority_setting) { // Log this Event was Skipped $activity->add('event', 'skipped', $event['ID']); continue; } if ($show_map_setting) { $event['EventShowMap'] = $show_map_setting; $event['EventShowMapLink'] = $show_map_setting; } if (empty($event['recurrence'])) { $non_recurring = true; } // set the parent if (!empty($event['ID']) && ($id = wp_get_post_parent_id($event['ID']))) { $event['post_parent'] = $id; } elseif (!empty($event['parent_uid']) && ($k = array_search($event['parent_uid'], $possible_parents))) { $event['post_parent'] = $k; } //if we should create a venue or use existing if (!empty($event['Venue']['Venue'])) { $v_id = array_search($event['Venue']['Venue'], $found_venues); if (false !== $v_id) { $event['EventVenueID'] = $v_id; } elseif ($venue = get_page_by_title($event['Venue']['Venue'], 'OBJECT', Tribe__Events__Main::VENUE_POST_TYPE)) { $found_venues[$venue->ID] = $event['Venue']['Venue']; $event['EventVenueID'] = $venue->ID; } else { $event['Venue']['ShowMap'] = $show_map_setting; $event['Venue']['ShowMapLink'] = $show_map_setting; $event['EventVenueID'] = Tribe__Events__Venue::instance()->create($event['Venue'], $this->meta['post_status']); // Log this Venue was created $activity->add('venue', 'created', $event['EventVenueID']); } // Remove the Venue to avoid duplicates unset($event['Venue']); } //if we should create an organizer or use existing if (!empty($event['Organizer']['Organizer'])) { $o_id = array_search($event['Organizer']['Organizer'], $found_organizers); if (false !== $o_id) { $event['EventOrganizerID'] = $o_id; } elseif ($organizer = get_page_by_title($event['Organizer']['Organizer'], 'OBJECT', Tribe__Events__Main::ORGANIZER_POST_TYPE)) { $found_organizers[$organizer->ID] = $event['Organizer']['Organizer']; $event['EventOrganizerID'] = $organizer->ID; } else { $event['EventOrganizerID'] = Tribe__Events__Organizer::instance()->create($event['Organizer'], $this->meta['post_status']); // Log this Organizer was created $activity->add('organizer', 'created', $event['EventOrganizerID']); } // Remove the Organizer to avoid duplicates unset($event['Organizer']); } $event['post_type'] = Tribe__Events__Main::POSTTYPE; /** * Filters the event data before any sort of saving of the event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_save_event', $event, $this); if (!empty($event['ID'])) { if ('preserve_changes' === $update_authority_setting) { $event = Tribe__Events__Aggregator__Event::preserve_changed_fields($event); } add_filter('tribe_aggregator_track_modified_fields', '__return_false'); /** * Filters the event data before updating event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_update_event', $event, $this); $event['ID'] = tribe_update_event($event['ID'], $event); // since the Event API only supports the _setting_ of these meta fields, we need to manually // delete them rather than relying on Tribe__Events__API::saveEventMeta() if (isset($event['EventShowMap']) && (empty($event['EventShowMap']) || 'no' === $event['EventShowMap'])) { delete_post_meta($event['ID'], '_EventShowMap'); } if (isset($event['EventShowMapLink']) && (empty($event['EventShowMapLink']) || 'no' === $event['EventShowMapLink'])) { delete_post_meta($event['ID'], '_EventShowMapLink'); } remove_filter('tribe_aggregator_track_modified_fields', '__return_false'); // Log that this event was updated $activity->add('event', 'updated', $event['ID']); } else { /** * Filters the event data before inserting event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_insert_event', $event, $this); $event['ID'] = tribe_create_event($event); // Log this event was created $activity->add('event', 'created', $event['ID']); } Tribe__Events__Aggregator__Records::instance()->add_record_to_event($event['ID'], $this->id, $this->origin); //add post parent possibility if (empty($event['parent_uid'])) { $possible_parents[$event['ID']] = $event[$unique_field['target']]; } if (!empty($event[$unique_field['target']])) { update_post_meta($event['ID'], "_{$unique_field['target']}", $event[$unique_field['target']]); } //Save the meta data in case of updating to pro later on if (!empty($event['EventRecurrenceRRULE'])) { update_post_meta($event['ID'], '_EventRecurrenceRRULE', $event['EventRecurrenceRRULE']); } $terms = array(); if (!empty($event['categories'])) { foreach ($event['categories'] as $cat) { if (!($term = term_exists($cat, Tribe__Events__Main::TAXONOMY))) { $term = wp_insert_term($cat, Tribe__Events__Main::TAXONOMY); if (!is_wp_error($term)) { $terms[] = (int) $term['term_id']; // Track that we created a Term $activity->add('cat', 'created', $term['term_id']); } } else { $terms[] = (int) $term['term_id']; } } } // if we are setting all events to a category specified in saved import if (!empty($this->meta['category'])) { $terms[] = (int) $this->meta['category']; } wp_set_object_terms($event['ID'], $terms, Tribe__Events__Main::TAXONOMY, false); // If we have a Image Field from Service if (!empty($event['image'])) { // Attempt to grab the event image $image_import = Tribe__Events__Aggregator::instance()->api('image')->get($event['image']->id); /** * Filters the returned event image url * * @param array|bool $image Attachment information * @param array $event Event array */ $image = apply_filters('tribe_aggregator_event_image', $image_import, $event); // If there was a problem bail out if (false === $image) { continue; } // Verify for more Complex Errors if (is_wp_error($image)) { continue; } if (isset($image->status) && 'created' === $image->status) { // Set as featured image $featured_status = set_post_thumbnail($event['ID'], $image->post_id); if ($featured_status) { // Log this attachment was created $activity->add('attachment', 'created', $image->post_id); } } } } remove_filter('tribe-post-origin', array(Tribe__Events__Aggregator__Records::instance(), 'filter_post_origin'), 10); return $activity; }