$ai1ec_events_list_helper = Ai1ec_Events_List_Helper::get_instance();
if ('admin-ajax.php' === basename($_SERVER['SCRIPT_NAME']) && isset($_REQUEST['lang'])) {
    $ai1ec_localization_helper->set_language($_REQUEST['lang']);
}
// ====================================
// = Initialize and setup CONTROLLERS =
// ====================================
global $ai1ec_app_controller, $ai1ec_settings_controller, $ai1ec_events_controller, $ai1ec_calendar_controller, $ai1ec_importer_controller, $ai1ec_exporter_controller, $ai1ec_platform_controller, $ai1ec_duplicate_controller, $ai1ec_oauth_controller, $ai1ec_logging_controller;
$ai1ec_settings_controller = Ai1ec_Settings_Controller::get_instance();
$ai1ec_events_controller = Ai1ec_Events_Controller::get_instance();
$ai1ec_calendar_controller = Ai1ec_Calendar_Controller::get_instance();
$ai1ec_importer_controller = Ai1ec_Importer_Controller::get_instance();
$ai1ec_exporter_controller = Ai1ec_Exporter_Controller::get_instance();
$ai1ec_platform_controller = Ai1ec_Platform_Controller::get_instance();
$ai1ec_duplicate_controller = Ai1ec_Duplicate_Controller::get_instance();
$ai1ec_oauth_controller = Ai1ec_Oauth_Controller::get_instance();
$ai1ec_logging_controller = Ai1ec_Logging_Controller::get_instance();
// Initialize other global classes
global $ai1ec_requirejs_controller, $ai1ec_rss_feed, $ai1ec_tax_meta_class;
// Create the instance of the class that handles javascript loading
$ai1ec_requirejs_controller = new Ai1ec_Requirejs_Controller();
// Inject settings
$ai1ec_requirejs_controller->set_settings($ai1ec_settings);
// Inject calendar controller
$ai1ec_requirejs_controller->set_events_helper($ai1ec_events_helper);
// Se the themes controller
$ai1ec_requirejs_controller->set_ai1ec_themes_controller($ai1ec_themes_controller);
// ==================================
// = Add the hook to render the js  =
// ==================================
if (isset($_GET[Ai1ec_Requirejs_Controller::WEB_WIDGET_GET_PARAMETER])) {
 /**
  * Checks and sends message to Twitter
  *
  * Upon successfully sending message - updates meta to reflect status change
  *
  * @return bool Success
  *
  * @throws Ai1ec_Oauth_Exception In case of some error
  */
 protected function _send_twitter_message($event)
 {
     $format = '[title], [date] @ [venue], [link] [hashtags]';
     $oauth = Ai1ec_Oauth_Controller::get_instance();
     $status = Ai1ec_Meta::instance('Post')->get($event->post_id, '_ai1ec_post_twitter', array('not_requested'));
     if (is_array($status)) {
         $status = (string) current($status);
     }
     if ('pending' !== $status) {
         return false;
     }
     $terms = array_merge(wp_get_post_terms($event->post_id, 'events_categories'), wp_get_post_terms($event->post_id, 'events_tags'));
     $hashtags = array();
     foreach ($terms as $term) {
         $hashtags[] = '#' . implode('_', explode(' ', $term->name));
     }
     $hashtags = implode(' ', array_unique($hashtags));
     $link = get_permalink($event->post) . $event->instance_id;
     $message = str_replace(array('[title]', '[date]', '@ [venue]', '[link]', '[hashtags]'), array($event->post->post_title, $event->get_short_start_date(), $event->venue, $link, $hashtags), $format);
     $message = trim(preg_replace('/ ,/', ',', preg_replace('/\\s+/', ' ', $message)));
     $length = strlen($message);
     $link_length = strlen($link);
     if ($link_length > 20) {
         $length += 20 - $link_length;
     }
     if ($length > 140) {
         $message = substr($message, 0, strrpos($message, ' ', 140 - $length));
     }
     $status = $oauth->get_provider('twitter')->send_message($oauth->get_token($event->post->post_author, 'twitter'), $message);
     if (!$status) {
         return false;
     }
     return update_post_meta($event->post_id, '_ai1ec_post_twitter', array('status' => 'sent', 'twitter_status_id' => $status));
 }