Exemplo n.º 1
0
 /**
  * Queues the import on the Aggregator service
  *
  * @return mixed
  */
 public function queue_import($args = array())
 {
     $aggregator = Tribe__Events__Aggregator::instance();
     $is_previewing = !empty($_GET['action']) && ('tribe_aggregator_create_import' === $_GET['action'] || 'tribe_aggregator_preview_import' === $_GET['action']);
     $error = null;
     $defaults = array('type' => $this->meta['type'], 'origin' => $this->meta['origin'], 'source' => $this->meta['source'], 'callback' => $is_previewing ? null : site_url('/event-aggregator/insert/?key=' . urlencode($this->meta['hash'])));
     if (!empty($this->meta['frequency'])) {
         $defaults['frequency'] = $this->meta['frequency'];
     }
     if (!empty($this->meta['file'])) {
         $defaults['file'] = $this->meta['file'];
     }
     if (!empty($this->meta['keywords'])) {
         $defaults['keywords'] = $this->meta['keywords'];
     }
     if (!empty($this->meta['location'])) {
         $defaults['location'] = $this->meta['location'];
     }
     if (!empty($this->meta['start'])) {
         $defaults['start'] = $this->meta['start'];
     }
     if (!empty($this->meta['radius'])) {
         $defaults['radius'] = $this->meta['radius'];
     }
     if ($is_previewing) {
         $defaults['preview'] = true;
     }
     $args = wp_parse_args($args, $defaults);
     // create the import on the Event Aggregator service
     $response = $aggregator->api('import')->create($args);
     // if the Aggregator API returns a WP_Error, set this record as failed
     if (is_wp_error($response)) {
         $error = $response;
         return $this->set_status_as_failed($error);
     }
     // if the Aggregator response has an unexpected format, set this record as failed
     if (empty($response->message_code)) {
         return $this->set_status_as_failed(tribe_error('core:aggregator:invalid-service-response'));
     }
     // if the Import creation was unsuccessful, set this record as failed
     if ('success:create-import' != $response->message_code && 'queued' != $response->message_code) {
         /**
          * @todo Allow overwriting the message
          */
         $error = new WP_Error($response->message_code, Tribe__Events__Aggregator__Errors::build(esc_html__($response->message, 'the-events-calendar'), empty($response->data->message_args) ? array() : $response->data->message_args));
         return $this->set_status_as_failed($error);
     }
     // if the Import creation didn't provide an import id, the response was invalid so mark as failed
     if (empty($response->data->import_id)) {
         return $this->set_status_as_failed(tribe_error('core:aggregator:invalid-service-response'));
     }
     // only set as pending if we aren't previewing the record
     if (!$is_previewing) {
         // if we get here, we're good! Set the status to pending
         $this->set_status_as_pending();
     }
     // store the import id
     update_post_meta($this->id, self::$meta_key_prefix . 'import_id', $response->data->import_id);
     return $response;
 }