/** * Static Singleton Factory Method * * @return Tribe__Events__Aggregator__Service */ public static function instance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; }
public static function get_auth_url($args = array()) { $service = Tribe__Events__Aggregator__Service::instance(); $url = $service->api()->domain . 'facebook/' . $service->api()->key; $defaults = array('referral' => urlencode(home_url()), 'type' => 'new', 'lang' => get_bloginfo('language')); $args = wp_parse_args($args, $defaults); $url = add_query_arg($args, $url); return $url; }
public function handle_facebook_credentials() { /** * Verify that we are dealing with a FB token Request */ if (!isset($_GET['ea-fb-token'])) { return false; } /** * @todo include a way to handle errors on the Send back URL */ $api = Tribe__Events__Aggregator__Service::instance()->api(); $response = Tribe__Events__Aggregator__Service::instance()->get_facebook_token(); $type = $_GET['ea-fb-token']; if (is_wp_error($response)) { return false; } if (empty($response->data)) { return false; } if (empty($response->data->expires) || empty($response->data->token) || empty($response->data->scopes)) { return false; } $url_map = array('new' => Tribe__Events__Aggregator__Page::instance()->get_url(array('tab' => $this->get_slug(), 'ea-auth' => 'facebook')), 'settings' => Tribe__Settings::instance()->get_url(array('tab' => 'addons', 'ea-auth' => 'facebook'))); if (!isset($url_map[$type])) { return false; } // Calculate when will this Token Expire $expires = absint(trim(preg_replace('/[^0-9]/', '', $response->data->expires))); $expires += time(); // Save the Options tribe_update_option('fb_token', trim(preg_replace('/[^a-zA-Z0-9]/', '', $response->data->token))); tribe_update_option('fb_token_expires', $expires); tribe_update_option('fb_token_scopes', trim(preg_replace('/[^a-zA-Z0-9\\,_-]/', '', $response->data->scopes))); // Send it back to the Given Url wp_redirect($url_map[$type]); exit; }
/** * Verifies if the service is active * * @return boolean */ public static function is_service_active() { return !is_wp_error(Tribe__Events__Aggregator__Service::instance()->api()); }
<table class="event-aggregator-status"> <thead> <tr class="table-heading"> <th colspan="4"><?php esc_html_e('Import Services', 'the-events-calendar'); ?> </th> </tr> </thead> <tbody> <?php $indicator = 'good'; $notes = ' '; $ea_server = Tribe__Events__Aggregator__Service::instance()->api()->domain; $up = Tribe__Events__Aggregator__Service::instance()->get('status/up'); if (!$up || is_wp_error($up)) { $indicator = 'bad'; /* translators: %s: Event Aggregator Server URL */ $text = sprintf(__('Not connected to %s', 'the-events-calendar'), $ea_server); $notes = esc_html__('The server is not currently responding', 'the-events-calendar'); } elseif (is_object($up) && is_object($up->data) && isset($up->data->status) && 400 <= $up->data->status) { // this is a rare condition that should never happen // An example case: the route is not defined on the EA server $indicator = 'warning'; /* translators: %s: Event Aggregator Server URL */ $text = sprintf(__('Not connected to %s', 'the-events-calendar'), $ea_server); $notes = __('The server is responding with an error:', 'the-events-calendar'); $notes .= '<pre>'; $notes .= esc_html($up->message); $notes .= '</pre>';
private function action_run_import($records = array()) { $service = Tribe__Events__Aggregator__Service::instance(); $record_obj = Tribe__Events__Aggregator__Records::instance()->get_post_type(); $records = array_filter((array) $records, 'is_numeric'); $success = array(); $errors = array(); foreach ($records as $record_id) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($record_id); if (is_wp_error($record)) { $errors[$record_id] = $record; continue; } // if there are no remaining imports for today, log that and skip if ($service->is_over_limit(true)) { $errors[$record->id] = new WP_Error('error:usage-limit-exceeded', $service->get_service_message('error:usage-limit-exceeded', Tribe__Events__Aggregator::instance()->get_daily_limit())); $record->update_meta('last_import_status', 'error:usage-limit-exceeded'); continue; } $child = $record->create_child_record(); $child->update_meta('interactive', true); $status = $child->queue_import(); if (is_wp_error($status)) { $errors[$record->id] = $status; $record->update_meta('last_import_status', 'error:import-failed'); continue; } $record->update_meta('last_import_status', 'success:queued'); $child->finalize(); $child->process_posts(); $success[$record->id] = $record; } return array($success, $errors); }
/** * Fetches the status message for the last import attempt on (scheduled) records * * @param string $type Type of message to fetch * * @return string */ public function get_last_import_status($type = 'error') { $status = empty($this->meta['last_import_status']) ? null : $this->meta['last_import_status']; if (!$status) { return; } if (0 !== strpos($status, $type)) { return; } if ('error:usage-limit-exceeded' === $status) { return __('When this import was last scheduled to run, the daily limit for your Event Aggregator license had already been reached.', 'the-events-calendar'); } return Tribe__Events__Aggregator__Service::instance()->get_service_message($status); }
/** * Checks if any Child Record needs to be created, this will run on the Cron every 15m * * @since 4.3 * @return void */ public function verify_child_record_creation() { // if the service isn't active, don't do anything if (!Tribe__Events__Aggregator::instance()->is_service_active()) { return; } $records = Tribe__Events__Aggregator__Records::instance(); $service = Tribe__Events__Aggregator__Service::instance(); $query = $records->query(array('post_status' => Tribe__Events__Aggregator__Records::$status->schedule, 'posts_per_page' => -1)); if (!$query->have_posts()) { $this->log('debug', 'No Records Scheduled, skipped creating childs'); return; } foreach ($query->posts as $post) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($post); if (!$record->is_schedule_time()) { $this->log('debug', sprintf('Record (%d) skipped, not scheduled time', $record->id)); continue; } if ($record->get_child_record_by_status('pending')) { $this->log('debug', sprintf('Record (%d) skipped, has pending childs', $record->id)); continue; } // if there are no remaining imports for today, log that and skip if ($service->is_over_limit(true)) { $this->log('debug', sprintf($service->get_service_message('error:usage-limit-exceeded') . ' (%1$d)', $record->id)); $record->update_meta('last_import_status', 'error:usage-limit-exceeded'); continue; } // Creating the child records based on this Parent $child = $record->create_child_record(); if (!is_wp_error($child)) { $this->log('debug', sprintf('Record (%d), was created as a child', $child->id)); // Creates on the Service a Queue to Fetch the events $response = $child->queue_import(); if (!empty($response->status)) { $this->log('debug', sprintf('%s — %s (%s)', $response->status, $response->message, $response->data->import_id)); $record->update_meta('last_import_status', 'success:queued'); } else { $this->log('debug', 'Could not create Queue on Service'); $record->update_meta('last_import_status', 'error:import-failed'); } } else { $this->log('debug', $child->get_error_message()); $record->update_meta('last_import_status', 'error:import-failed'); } } }
/** * Allows us to Prevent too many of our Requests to be fired at on single Cron Job * * @param boolean $run Shouldn't trigger the call * @param array $request The Request that was made * @param string $url To which URL * * @return boolean|array|object */ public function filter_check_http_limit($run = false, $request, $url) { // We bail if it's not a CRON job if (!defined('DOING_CRON') || !DOING_CRON) { return $run; } // If someone changed this already we bail, it's not going to be fired if (false !== $run) { return $run; } // Bail if it wasn't done inside of the Actual Cron task if (true !== $this->is_running) { return $run; } $service = Tribe__Events__Aggregator__Service::instance(); // If the Domain is not we just keep the same answer if (0 !== strpos($url, $service->api()->domain)) { return $run; } // If we already reached 0 we throw an error if ($this->limit <= 0) { // Schedule a Cron Event to happen ASAP, and flag it for searching and we need to make it unique // By default WordPress won't allow more than one Action to happen twice in 10 minutes wp_schedule_single_event(time(), self::$single_action); return tribe_error('core:aggregator:http_request-limit', array('request' => $request, 'url' => $url)); } // Lower the Limit $this->limit--; // Return false to make the Actual Request Run return $run; }
public function __construct() { $this->service = Tribe__Events__Aggregator__Service::instance(); }