/**
  * 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);
 }
 /**
  * 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'));
 }