/**
  * Returns a singleton of this class
  *
  * @return Tribe__Events__Organizer
  */
 public static function instance()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Example #2
0
 /**
  * Get organizer post type args
  *
  * @return array
  */
 public function getOrganizerPostTypeArgs()
 {
     return Tribe__Events__Organizer::instance()->post_type_args;
 }
 /**
  * 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;
 }
Example #4
0
 /**
  * Saves organizer meta
  *
  * @param int   $organizer_id The organizer ID.
  * @param array $data        The organizer data.
  *
  */
 private static function saveOrganizerMeta($organizer_id, $data)
 {
     return Tribe__Events__Organizer::instance()->save_meta($organizer_id, $data);
 }