/**
  * Adds al the elements to the page.
  * I took this out of the factory so that it's done only if the page is clicked
  */
 private function add_renderables_to_page()
 {
     // These are the tabs
     $tabs_ids_description = array('general' => __('General', AI1EC_PLUGIN_NAME), 'table' => __('Tables', AI1EC_PLUGIN_NAME), 'buttons' => __('Buttons', AI1EC_PLUGIN_NAME), 'forms' => __('Forms', AI1EC_PLUGIN_NAME), 'calendar' => __('Calendar general', AI1EC_PLUGIN_NAME), 'posterboard' => __('Posterboard view', AI1EC_PLUGIN_NAME), 'stream' => __('Stream view', AI1EC_PLUGIN_NAME), 'month' => __('Month/week/day view', AI1EC_PLUGIN_NAME), 'agenda' => __('Agenda view', AI1EC_PLUGIN_NAME));
     // Create the tab layout
     $bootstrap_tabs_layout = Ai1ec_Helper_Factory::create_bootstrap_tabs_layout_instance();
     $bootstrap_tabs_layout->set_layout('left');
     $less_variables = Ai1ec_Lessphp_Controller::get_saved_variables(Ai1ec_Adapters_Factory::create_db_adapter_instance());
     // Inizialize the array of tabs that will be added to the layout
     $bootstrap_tabs_to_add = array();
     // initialize the array of tab bodyes that will be added to the tabs
     $tabs_bodies = array();
     foreach ($tabs_ids_description as $id => $description) {
         $bootstrap_tabs_to_add["ai1ec-{$id}"] = Ai1ec_Helper_Factory::create_bootstrap_tab_instance($id, $description);
         $bootstrap_tabs_to_add["ai1ec-{$id}"]->add_class('form-horizontal');
         // create the main div that will hold all the variables
         $div = Ai1ec_Helper_Factory::create_generic_html_tag('div');
         $tabs_bodies["ai1ec-{$id}"] = $div;
     }
     foreach ($less_variables as $variable_id => $variable_attributes) {
         $variable_attributes['id'] = $variable_id;
         $less_variable = Ai1ec_Less_Factory::create_less_variable($variable_attributes['type'], $variable_attributes);
         $tabs_bodies["ai1ec-{$variable_attributes['tab']}"]->add_renderable_children($less_variable);
     }
     foreach ($tabs_bodies as $tab => $div) {
         $bootstrap_tabs_to_add[$tab]->add_renderable_children($div);
     }
     foreach ($bootstrap_tabs_to_add as $tab) {
         $bootstrap_tabs_layout->add_renderable_children($tab);
     }
     $this->add_renderable_children($bootstrap_tabs_layout);
     $input = Ai1ec_Helper_Factory::create_input_instance();
     $input->set_type('submit');
     $input->set_value(__('Save Options', AI1EC_PLUGIN_NAME));
     $input->set_name(Ai1ec_Less_Variables_Editing_Page::FORM_SUBMIT_NAME);
     $input->add_class("button-primary");
     $reset_theme = Ai1ec_Helper_Factory::create_input_instance();
     $reset_theme->set_type('submit');
     $reset_theme->set_value(__('Reset to defaults', AI1EC_PLUGIN_NAME));
     $reset_theme->set_name(Ai1ec_Less_Variables_Editing_Page::FORM_SUBMIT_RESET_THEME);
     $reset_theme->add_class("button");
     $reset_theme->set_id('ai1ec-reset-variables');
     $this->add_renderable_children($input);
     $this->add_renderable_children($reset_theme);
 }
 /**
  * Called by the Update Calendar Themes page. Removes the core theme files
  * under wp-content/themes-ai1ec and replaces them with fresh versions.
  */
 function update_core_themes()
 {
     global $ai1ec_view_helper;
     $src_dir = trailingslashit(AI1EC_PATH . DIRECTORY_SEPARATOR . AI1EC_THEMES_FOLDER);
     $dest_dir = trailingslashit(AI1EC_THEMES_ROOT);
     // List of core themes.
     $folders = array('gamma', 'plana', 'umbra', 'vortex');
     // Array to hold error notifications to the user while updating the themes.
     $delete_errors = array();
     $copy_errors = array();
     // WP_Filesystem figures it out by itself, but the filesystem method may be
     // overriden here.
     $method = '';
     $url = wp_nonce_url(AI1EC_UPDATE_THEMES_BASE_URL, AI1EC_PLUGIN_NAME . '-theme-updater');
     $creds = request_filesystem_credentials($url, $method, false, false);
     if (false === $creds) {
         // If we get here, then we don't have credentials yet,
         // but have just produced a form for the user to fill in,
         // so stop processing for now.
         return false;
         // Stop the normal page form from displaying.
     }
     // Now we have some credentials, try to get the wp_filesystem running.
     if (!WP_Filesystem($creds)) {
         // Our credentials were no good, ask the user for them again.
         request_filesystem_credentials($url, $method, true, false);
         return false;
     }
     global $wp_filesystem;
     // 1. Remove old folders.
     foreach ($folders as $folder) {
         $folder = $dest_dir . $folder;
         // Check if folder exists.
         if ($wp_filesystem->is_dir($folder)) {
             // Try to delete it recusively.
             if (false === $wp_filesystem->delete($folder, true)) {
                 // If delete failed, chmod folder recursively to 0644 and try again.
                 $wp_filesystem->chmod($folder, 0644, $recursive);
                 if (false === $wp_filesystem->delete($folder, true)) {
                     // We were not able to remove the folder; notify the user.
                     $delete_errors[] = $folder;
                 }
             }
         }
     }
     // 2. Copy fresh versions of folders.
     foreach ($folders as $folder) {
         $src_folder = $src_dir . $folder;
         $dest_folder = $dest_dir . $folder;
         // Try to copy the folder.
         $result = copy_dir($src_dir, $dest_dir);
         if (is_wp_error($result)) {
             // We were not able to copy the folder; notify the user.
             $copy_errors[] = $src_folder;
         }
     }
     $errors = array();
     if ($delete_errors) {
         $error = '<div class="error"><p><strong>';
         $error .= __('There was an error while removing outdated core themes from your themes folder.', AI1EC_PLUGIN_NAME);
         $error .= '</strong> ';
         $error .= __('Please FTP to your web server and manually delete:', AI1EC_PLUGIN_NAME);
         $error .= '</p><blockquote><div><code>';
         $error .= implode('</code></div><div><code>', $delete_errors);
         $error .= '</code></div></blockquote></div>';
         $errors[] = $error;
     }
     if ($copy_errors) {
         $error = '<div class="error"><p><strong>';
         $error .= __('There was an error while copying core themes from the plugin into the themes folder.', AI1EC_PLUGIN_NAME);
         $error .= '</strong> ';
         $error .= __('Please FTP to your web server and manually copy the folders:', AI1EC_PLUGIN_NAME);
         $error .= '</p><blockquote><div><code>';
         $error .= implode('</code></div><div><code>', $copy_errors);
         $error .= '</code></div></blockquote>';
         $error .= '<p>' . __('to', AI1EC_PLUGIN_NAME) . ' <code>' . $dest_dir . '</code></p></div>';
         $errors[] = $error;
     }
     // We have updated the files, now let's update LESS variables.
     $lessphp_controller = Ai1ec_Less_Factory::create_lessphp_controller();
     $lessphp_controller->update_less_variables_on_theme_update();
     // After the update we invalidate the cache and recompile
     $css_controller = Ai1ec_Less_Factory::create_css_controller_instance();
     if (false === $css_controller->invalidate_cache(null, true)) {
         $errors[] = '<div class="error"><p>' . __('<strong>An error occurred while compiling the theme’s CSS after updating files.</strong> Please visit <strong>Events</strong> &gt; <strong>Theme Options</strong> and click <strong>Save Options</strong> for more detail about the error.', AI1EC_PLUGIN_NAME) . '</p></div>';
     }
     // Unsuccessful core theme file update.
     if ($errors) {
         array_unshift($errors, __('<div id="message" class="error"><h3>Errors occurred while we tried to update your core Calendar Themes</h3><p><strong>Please follow any instructions listed below or your calendar may malfunction:</strong></p></div>', AI1EC_PLUGIN_NAME));
     } else {
         // Update theme version
         update_option('ai1ec_themes_version', AI1EC_THEMES_VERSION);
         $msg = '<div id="message" class="updated"><h3>';
         $msg .= __('Your core Calendar Themes were updated successfully', AI1EC_PLUGIN_NAME);
         $msg .= '</h3><p>';
         $msg .= __('Be sure to <strong>reload your browser</strong> when viewing your site to make sure the most current scripts are used.', AI1EC_PLUGIN_NAME);
         $msg .= '</p></div>';
     }
     $args = array('msg' => $msg, 'errors' => $errors);
     $ai1ec_view_helper->display_admin('themes-updated.php', $args);
 }
    }
    // Delete table events
    $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
    // Delete table event instances
    $table_name = $wpdb->prefix . 'ai1ec_event_instances';
    $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
    // Delete table event feeds
    $table_name = $wpdb->prefix . 'ai1ec_event_feeds';
    $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
    // Delete table category colors
    $table_name = $wpdb->prefix . 'ai1ec_event_category_colors';
    $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
    // Delete themes folder
    if (is_object($wp_filesystem) && !is_wp_error($wp_filesystem->errors)) {
        // Get the base plugin folder
        $themes_dir = $wp_filesystem->wp_content_dir() . AI1EC_THEMES_FOLDER;
        if (!empty($themes_dir)) {
            $themes_dir = trailingslashit($themes_dir);
            $wp_filesystem->delete($themes_dir, true);
        }
    }
    // Let the plugin run their uninstall procedures
    $ai1ec_importer_plugin_helper->run_uninstall_procedures();
    // Delete cached css from persistence
    $css_controller = Ai1ec_Less_Factory::create_css_controller_instance();
    $css_controller->invalidate_cache();
    // Delete last updated time for css parsing
    delete_option(Ai1ec_Css_Controller::GET_VARIBALE_NAME);
    // Delete parsed variables for css generation
    delete_option(Ai1ec_Lessphp_Controller::DB_KEY_FOR_LESS_VARIABLES);
}
 /**
  * @param string $default_theme_url
  */
 public static function set_default_theme_url($default_theme_url)
 {
     Ai1ec_Less_Factory::$default_theme_url = $default_theme_url;
 }
 /**
  * Constructor
  *
  * Default constructor - application initialization
  **/
 private function __construct($preview_mode)
 {
     global $wpdb, $wp_locale, $wp_scripts, $ai1ec_app_helper, $ai1ec_view_helper, $ai1ec_events_controller, $ai1ec_events_helper, $ai1ec_importer_controller, $ai1ec_exporter_controller, $ai1ec_settings_controller, $ai1ec_settings, $ai1ec_themes_controller, $ai1ec_calendar_controller, $ai1ec_calendar_helper, $ai1ec_importer_plugin_helper, $ai1ec_requirejs_controller, $ai1ec_rss_feed, $ai1ec_duplicate_controller;
     $aie1c_admin_notices_helper = Ai1ec_Admin_Notices_Helper::get_instance();
     $ai1ec_duplicate_controller->set_admin_notice_helper($aie1c_admin_notices_helper);
     $css_controller = Ai1ec_Less_Factory::create_css_controller_instance();
     // register_activation_hook
     register_activation_hook(AI1EC_PLUGIN_NAME . '/' . AI1EC_PLUGIN_NAME . '.php', array(&$this, 'activation_hook'));
     // Configure MySQL to operate in GMT time
     $wpdb->query("SET time_zone = '+0:00'");
     // Load plugin text domain
     $this->load_textdomain();
     // Install/update database schema as necessary
     $this->install_schema();
     // Enable stats collection
     $this->install_n_cron();
     // Enable plugins for importing events from external sources
     $this->install_plugins();
     $lessphp_controller = Ai1ec_Less_Factory::create_lessphp_controller();
     // Update less variables in the db
     if (isset($_POST[Ai1ec_Less_Variables_Editing_Page::FORM_SUBMIT_NAME]) || isset($_POST[Ai1ec_Less_Variables_Editing_Page::FORM_SUBMIT_RESET_THEME])) {
         $css_controller->handle_less_variables_page_form_post();
     }
     // Adds the image field only if we are on event categories page
     $this->add_image_field_to_event_categories();
     // Enable checking for cron updates
     $this->install_u_cron();
     // Continue loading hooks only if themes are installed. Otherwise display a
     // notification on the backend with instructions how to install themes.
     if (!$ai1ec_themes_controller->are_themes_available()) {
         // Enables the hidden themes installer page
         add_action('admin_menu', array(&$ai1ec_themes_controller, 'register_theme_installer'), 10);
         // Redirects the user to install theme page
         add_action('admin_menu', array(&$this, 'check_themes'), 2);
         return;
     }
     if (false === $preview_mode && !$ai1ec_themes_controller->are_themes_outdated()) {
         // Create the less variables if they are not set, but only if we are not in preview mode.
         // this is because there is an edge case when you activate a theme and then you preview another
         // the variables of the other theme are set.
         $lessphp_controller->initialize_less_variables_if_not_set(Ai1ec_Less_Factory::create_less_file_instance(Ai1ec_Less_File::USER_VARIABLES_FILE));
     }
     // Check for legacy format themes when viewing WP dashboard; do not perform
     // the check when switching themes or if current theme files are outdated.
     if (is_admin() && !isset($_GET['ai1ec_template']) && !$ai1ec_themes_controller->are_themes_outdated()) {
         $ai1ec_themes_controller->generate_notice_if_legacy_theme_installed();
     }
     // ===========
     // = ACTIONS =
     // ===========
     // Very early on in WP bootstrap, prepare to do any requested theme preview.
     add_action('setup_theme', array(&$ai1ec_themes_controller, 'preview_theme'));
     // Calendar theme initialization
     add_action('after_setup_theme', array(&$ai1ec_themes_controller, 'setup_theme'));
     // Create custom post type
     add_action('init', array(&$ai1ec_app_helper, 'create_post_type'));
     // Handle ICS export requests
     add_action('init', array(&$this, 'parse_standalone_request'));
     // RSS Feed
     add_action('init', array($ai1ec_rss_feed, 'add_feed'));
     // Add the link for CSS generation
     if (!is_admin()) {
         add_action('init', array($css_controller, 'add_link_to_html_for_frontend'), 1);
     }
     // Load plugin text domain
     add_action('init', array(&$this, 'load_textdomain'));
     // Load back-end javascript files
     add_action('init', array($ai1ec_requirejs_controller, 'load_admin_js'));
     // Load the scripts for the backend for wordpress version < 3.3
     add_action('admin_footer', array($ai1ec_requirejs_controller, 'print_admin_script_footer_for_wordpress_32'));
     // Load the scripts for the frontend for wordpress version < 3.3
     add_action('wp_footer', array($ai1ec_requirejs_controller, 'print_frontend_script_footer_for_wordpress_32'));
     // Set an action to load front-end javascript
     add_action('ai1ec_load_frontend_js', array($ai1ec_requirejs_controller, 'load_frontend_js'), 10, 1);
     // Check if themes are installed
     add_action('init', array(&$ai1ec_themes_controller, 'check_themes'));
     // Register The Event Calendar importer
     add_action('admin_init', array(&$ai1ec_importer_controller, 'register_importer'));
     // Install admin menu items.
     add_action('admin_menu', array(&$this, 'admin_menu'), 9);
     // Enable theme updater page if last version of core themes is older than
     // current version.
     if ($ai1ec_themes_controller->are_themes_outdated()) {
         add_action('admin_menu', array(&$ai1ec_themes_controller, 'register_theme_updater'), 10);
     }
     // Add Event counts to dashboard.
     add_action('right_now_content_table_end', array(&$ai1ec_app_helper, 'right_now_content_table_end'));
     // add content for our custom columns
     add_action('manage_ai1ec_event_posts_custom_column', array(&$ai1ec_app_helper, 'custom_columns'), 10, 2);
     // Add filtering dropdowns for event categories and tags
     add_action('restrict_manage_posts', array(&$ai1ec_app_helper, 'taxonomy_filter_restrict_manage_posts'));
     // Trigger display of page in front-end depending on request
     add_action('template_redirect', array(&$this, 'route_request'));
     // Add meta boxes to event creation/edit form.
     add_action('add_meta_boxes', array(&$ai1ec_app_helper, 'add_meta_boxes'));
     add_action('show_user_profile', array(&$ai1ec_app_helper, 'add_profile_boxes'));
     add_action('personal_options_update', array(&$ai1ec_app_helper, 'save_user_profile'), 10, 1);
     // Save event data when post is saved
     add_action('save_post', array(&$ai1ec_events_controller, 'save_post'), 10, 2);
     // Delete event data when post is deleted
     add_action('delete_post', array(&$ai1ec_events_controller, 'delete_post'));
     add_action('delete_term', array($ai1ec_settings, 'term_deletion'), 10, 3);
     // Notification cron job hook
     add_action('ai1ec_n_cron', array(&$ai1ec_exporter_controller, 'n_cron'));
     // Updates cron job hook
     add_action('ai1ec_u_cron', array(&$ai1ec_settings_controller, 'u_cron'));
     // Category colors
     add_action('events_categories_add_form_fields', array(&$ai1ec_events_controller, 'events_categories_add_form_fields'));
     add_action('events_categories_edit_form_fields', array(&$ai1ec_events_controller, 'events_categories_edit_form_fields'));
     add_action('created_events_categories', array(&$ai1ec_events_controller, 'created_events_categories'));
     add_action('edited_events_categories', array(&$ai1ec_events_controller, 'edited_events_categories'));
     add_action('admin_notices', array(&$ai1ec_app_helper, 'admin_notices'));
     // The new object that handles notices.
     add_action('admin_notices', array(&$aie1c_admin_notices_helper, 'render'));
     // Scripts/styles for settings and widget admin screens.
     add_action('admin_enqueue_scripts', array(&$ai1ec_app_helper, 'admin_enqueue_scripts'));
     // Widgets
     add_action('widgets_init', create_function('', "return register_widget( 'Ai1ec_Agenda_Widget' );"));
     // Modify WP admin bar
     add_action('admin_bar_menu', array(&$ai1ec_app_helper, 'modify_admin_bar'));
     // ===========
     // = FILTERS =
     // ===========
     if (is_admin() && 'admin-ajax.php' !== basename($_SERVER['SCRIPT_NAME'])) {
         add_filter('the_title', array(&$ai1ec_view_helper, 'the_title_admin'), 1, 2);
     }
     add_filter('posts_orderby', array(&$ai1ec_app_helper, 'orderby'), 10, 2);
     // add custom column names and change existing columns
     add_filter('manage_ai1ec_event_posts_columns', array(&$ai1ec_app_helper, 'change_columns'));
     // filter the post lists by custom filters
     add_filter('parse_query', array(&$ai1ec_app_helper, 'taxonomy_filter_post_type_request'));
     // Override excerpt filters for proper event display in excerpt form
     add_filter('get_the_excerpt', array(&$ai1ec_events_controller, 'event_excerpt'), 11);
     add_filter('the_excerpt', array(&$ai1ec_events_controller, 'event_excerpt_noautop'), 11);
     remove_filter('the_excerpt', 'wpautop', 10);
     // Update event post update messages
     add_filter('post_updated_messages', array(&$ai1ec_events_controller, 'post_updated_messages'));
     // Sort the custom columns
     add_filter('manage_edit-ai1ec_event_sortable_columns', array(&$ai1ec_app_helper, 'sortable_columns'));
     add_filter('map_meta_cap', array(&$ai1ec_app_helper, 'map_meta_cap'), 10, 4);
     // Inject event categories, only in front-end, depending on setting
     if ($ai1ec_settings->inject_categories && !is_admin()) {
         add_filter('get_terms', array(&$ai1ec_app_helper, 'inject_categories'), 10, 3);
         add_filter('wp_list_categories', array(&$ai1ec_app_helper, 'selected_category_link'), 10, 2);
     }
     // Rewrite event category URLs to point to calendar page.
     add_filter('term_link', array(&$ai1ec_app_helper, 'calendar_term_link'), 10, 3);
     // Add a link to settings page on the plugin list page.
     add_filter('plugin_action_links_' . AI1EC_PLUGIN_BASENAME, array(&$ai1ec_settings_controller, 'plugin_action_links'));
     // Add a link to donate page on plugin list page.
     add_filter('plugin_row_meta', array(&$ai1ec_settings_controller, 'plugin_row_meta'), 10, 2);
     add_filter('post_type_link', array(&$ai1ec_events_helper, 'post_type_link'), 10, 3);
     add_filter('ai1ec_template_root_path', array(&$ai1ec_themes_controller, 'template_root_path'));
     add_filter('ai1ec_template_root_url', array(&$ai1ec_themes_controller, 'template_root_url'));
     // ========
     // = AJAX =
     // ========
     // RRule to Text
     add_action('wp_ajax_ai1ec_rrule_to_text', array(&$ai1ec_events_helper, 'convert_rrule_to_text'));
     // Display Repeat Box
     add_action('wp_ajax_ai1ec_get_repeat_box', array(&$ai1ec_events_helper, 'get_repeat_box'));
     add_action('wp_ajax_ai1ec_get_date_picker_box', array(&$ai1ec_events_helper, 'get_date_picker_box'));
     // Disable notifications
     add_action('wp_ajax_ai1ec_disable_notification', array(&$ai1ec_settings_controller, 'disable_notification'));
     add_action('wp_ajax_ai1ec_disable_intro_video', array(&$ai1ec_settings_controller, 'disable_intro_video'));
     // Front-end event creation
     add_action('wp_ajax_ai1ec_front_end_create_event_form', array(&$ai1ec_events_helper, 'get_front_end_create_event_form'));
     add_action('wp_ajax_ai1ec_front_end_submit_event', array(&$ai1ec_events_helper, 'submit_front_end_create_event_form'));
     if ($ai1ec_settings->allow_anonymous_submissions) {
         add_action('wp_ajax_nopriv_ai1ec_front_end_create_event_form', array(&$ai1ec_events_helper, 'get_front_end_create_event_form'));
         add_action('wp_ajax_nopriv_ai1ec_front_end_submit_event', array(&$ai1ec_events_helper, 'submit_front_end_create_event_form'));
     }
     // Invalid license status warning.
     add_action('wp_ajax_ai1ec_set_license_warning', array(&$ai1ec_settings_controller, 'set_license_warning'));
     // ==============
     // = Shortcodes =
     // ==============
     add_shortcode('ai1ec', array(&$ai1ec_events_helper, 'shortcode'));
 }
    /**
     * Renders everything that's needed for the web widget
     *
     */
    public function render_web_widget()
    {
        header('Content-Type: application/javascript');
        // Aggressive caching to save future requests from the same client.
        $etag = '"' . md5(__FILE__ . AI1EC_VERSION) . '"';
        header('ETag: ' . $etag);
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
        header('Cache-Control: public, max-age=31536000');
        if (empty($_SERVER['HTTP_IF_NONE_MATCH']) || $etag !== stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) {
            $ccs_controller = Ai1ec_Less_Factory::create_css_controller_instance();
            $require_main = AI1EC_ADMIN_THEME_JS_URL . '/require.js';
            $data_main = AI1EC_ADMIN_THEME_JS_URL . '/main_widget.js';
            $translation = $this->get_frontend_translation_data();
            $permalink = get_permalink($this->settings->calendar_page_id);
            $css_url = $ccs_controller->get_css_url();
            $translation['calendar_url'] = $permalink;
            $translation_module = $this->create_require_js_module(self::FRONTEND_CONFIG_MODULE, $translation);
            $config = $this->create_require_js_module('ai1ec_config', $this->get_translation_data());
            // get jquery
            $jquery = $this->get_jquery_version_based_on_browser($_SERVER['HTTP_USER_AGENT']);
            echo <<<JS
\t\t\t/******** Called once Require.js has loaded ******/
\t\t\t// This needs to be global
\t\t\tfunction timely_scriptLoadHandler() {
\t\t\t\t// Load translations modules
\t\t\t\t{$translation_module}
\t\t\t\t{$config}
\t\t\t\t{$jquery}
\t\t\t}
\t\t\t(function() {
\t\t\t\tif( typeof timely === 'undefined' ) {
\t\t\t\t\tvar timely_script_tag = document.createElement( 'script' );
\t\t\t\t\ttimely_script_tag.setAttribute( "type","text/javascript" );
\t\t\t\t\ttimely_script_tag.setAttribute( "src", "{$require_main}" );
\t\t\t\t\ttimely_script_tag.setAttribute( "data-main", "{$data_main}" );
\t\t\t\t\ttimely_script_tag.async = true;
\t\t\t\t\tif ( timely_script_tag.readyState ) {
\t\t\t\t\t\ttimely_script_tag.onreadystatechange = function () { // For old versions of IE
\t\t\t\t\t\t\tif ( this.readyState == 'complete' || this.readyState == 'loaded' ) {
\t\t\t\t\t\t\t\ttimely_scriptLoadHandler();
\t\t\t\t\t\t\t}
\t\t\t\t\t\t};
\t\t\t\t\t} else { // Other browsers
\t\t\t\t\t\ttimely_script_tag.onload = timely_scriptLoadHandler;
\t\t\t\t\t}
\t\t\t\t\t( document.getElementsByTagName( "head" )[0] || document.documentElement ).appendChild( timely_script_tag );
\t\t\t\t} else {
\t\t\t\t\ttimely.require( ['main_widget'] );
\t\t\t\t\ttimely_scriptLoadHandler();
\t\t\t\t}
\t\t\t\tvar timely_css = document.createElement( 'link' );
\t\t\t\ttimely_css.setAttribute( "type", "text/css" );
\t\t\t\ttimely_css.setAttribute( "rel", "stylesheet" );
\t\t\t\ttimely_css.setAttribute( "href", "{$css_url}" );
\t\t\t\t( document.getElementsByTagName( "head" )[0] || document.documentElement ).appendChild( timely_css );
\t\t\t})(); // We call our anonymous function immediately
JS;
        } else {
            // Not modified!
            status_header(304);
        }
        exit;
    }
 /**
  * a static method to get variables
  *
  * @param Ai1ec_Db_Adapter $db_adapter
  * @return array
  */
 public static function get_saved_variables(Ai1ec_Db_Adapter $db_adapter)
 {
     $variables = $db_adapter->get_data_from_config(self::DB_KEY_FOR_LESS_VARIABLES);
     $variables_with_description = self::get_less_variable_data_from_config_file(Ai1ec_Less_Factory::create_less_file_instance(Ai1ec_Less_File::USER_VARIABLES_FILE));
     // Add the description at runtime so that it can get the translation
     foreach ($variables as $name => $attrs) {
         if (isset($variables_with_description[$name]['description'])) {
             $variables[$name]['description'] = $variables_with_description[$name]['description'];
         }
     }
     return $variables;
 }
 /**
  * Try to get the CSS from cache.
  * If it's not there re-generate it and save it to cache
  * If we are in preview mode, recompile the css using the theme present in the url.
  *
  */
 private function get_compiled_css()
 {
     try {
         // If we want to force a recompile, we throw an exception.
         if ($this->preview_mode === true || self::PARSE_LESS_FILES_AT_EVERY_REQUEST === true) {
             throw new Ai1ec_Cache_Not_Set_Exception();
         } else {
             // This throws an exception if the key is not set
             $css = $this->persistance_context->get_data_from_persistence();
             return $css;
         }
     } catch (Ai1ec_Cache_Not_Set_Exception $e) {
         // If we are in preview mode we force a recompile and we pass the variables.
         if ($this->preview_mode) {
             return $this->lessphp_controller->parse_less_files($this->lessphp_controller->get_less_variable_data_from_config_file(Ai1ec_Less_Factory::create_less_file_instance(Ai1ec_Less_File::USER_VARIABLES_FILE)));
         } else {
             $css = $this->lessphp_controller->parse_less_files();
         }
         try {
             $this->update_persistence_layer($css);
             return $css;
         } catch (Ai1ec_Cache_Write_Exception $e) {
             // If something is really broken, still return the css.
             // This means we parse it every time. This should never happen.
             return $css;
         }
     }
 }