function on_init()
 {
     $this->metaboxes = xtreme_build_metaboxes(apply_filters('xtreme_metaboxes_default', array()), $this);
     $this->version = get_option(XF_VERSION_FIELD);
     $this->options = get_option(XF_OPTIONS, array());
     $this->layouts = get_option(XF_LAYOUTS, array());
     //templates done at different page but checked for consistency
     $templates = get_option(XF_TEMPLATES, array());
     if (empty($templates)) {
         $templates = array();
     }
     $files = xtreme_load_templates();
     if (count($templates) !== count($files)) {
         foreach ($files as $file => $data) {
             if (!array_key_exists($file, $templates)) {
                 $templates[$file] = 'xf_layout-default';
             }
         }
         update_option(XF_TEMPLATES, $templates);
     }
     $this->apply_options($this->options);
     add_action('wp_ajax_xtreme_check_user_credentials', array(&$this, 'on_check_user_credentials'));
     if (current_theme_supports('post-thumbnails')) {
         add_filter('manage_posts_columns', array(&$this, 'on_thumbnail_column'));
         add_filter('manage_pages_columns', array(&$this, 'on_thumbnail_column'));
         add_action('manage_posts_custom_column', array(&$this, 'on_thumbnail_column_value'), 10, 2);
         add_action('manage_pages_custom_column', array(&$this, 'on_thumbnail_column_value'), 10, 2);
     }
     if (is_admin()) {
         wp_enqueue_style('xtreme-hooks', XF_ADMIN_URL . '/css/xtreme-hooked.css');
     }
     global $wp_version;
     // advanced gallery legacy support
     if (current_theme_supports('xtreme-advanced-wpgallery-legacy') && version_compare($wp_version, '3.5', '>=')) {
         add_action('wp_enqueue_media', array($this, 'wp_enqueue_media_advanced_gallery_legacy'));
         add_action('print_media_templates', array($this, 'print_media_templates_advanced_gallery_legacy'));
     }
     //advanced tinymce support
     if (current_theme_supports('xtreme-advanced-tinymce')) {
         add_editor_style('css/screen/editor-style.css');
         add_filter('mce_css', array(&$this, 'on_mce_css'), 999);
         add_filter('mce_buttons', array(&$this, 'on_mce_buttons'), 999);
     }
     //advanced color style support
     if (current_theme_supports('xtreme-color-styles')) {
         add_filter('after_theme_css', array(&$this, 'after_theme_css'));
     }
     //css compression
     add_action('admin_notices', array(&$this, 'on_admin_notice'));
 }
 function on_admin_init()
 {
     $this->metaboxes = xtreme_build_metaboxes(apply_filters('xtreme_metaboxes_layout', array()), $this);
     $this->error = array();
     $redirect = admin_url('admin.php?page=xtreme_layouts');
     add_action('wp_ajax_xtreme_check_user_credentials', array(&$this, 'on_check_user_credentials'));
     if (isset($_GET['error'])) {
         switch ((int) $_GET['error']) {
             case 1:
                 $this->error[] = __('No files applied to the new layout.', XF_TEXTDOMAIN);
                 break;
             case 2:
                 $this->error[] = __('You can not apply all files to a new Layout.', XF_TEXTDOMAIN);
                 break;
             case 3:
                 $this->error[] = __('You can not remove the only one remaining file from layout.', XF_TEXTDOMAIN);
                 break;
         }
     }
     if (isset($_GET['mode'])) {
         $options = get_option(XF_LAYOUTS);
         $files = xtreme_load_templates();
         $temp_options['xc_templayout'] = $options['xf_layout-default'];
         $temp_options['xc_templayout']['nicename']['value'] = 'new Layout';
         foreach ($files as $file => $value) {
             $field = substr($value['metavalue'], 0, -4);
             $temp_options['xc_templayout'][$field]['value'] = false;
         }
         $temp_options['xc_templayout']['mode']['value'] = 'add';
         $this->apply_options($temp_options);
     }
     if (isset($_GET['mode']) && isset($_GET['layout']) && 'edit_layout' == $_GET['mode']) {
         $layout = strip_tags($_GET['layout']);
         $options = get_option(XF_LAYOUTS);
         $templates = get_option(XF_TEMPLATES);
         $files = xtreme_load_templates();
         if (array_key_exists($layout, $options)) {
             $temp_options['xc_templayout'] = $options[$layout];
             foreach ($templates as $filename => $layoutname) {
                 $field = substr($files[$filename]['metavalue'], 0, -4);
                 if ($layoutname === $layout) {
                     $temp_options['xc_templayout'][$field]['value'] = true;
                 } else {
                     $temp_options['xc_templayout'][$field]['value'] = false;
                 }
             }
             $temp_options['xc_templayout']['mode']['value'] = 'edit';
             $this->apply_options($temp_options);
         }
     }
     if (isset($_GET['mode']) && isset($_GET['layout']) && 'delete_layout' == $_GET['mode'] && $_GET['layout'] !== 'xf_layout-default') {
         $layout = strip_tags($_GET['layout']);
         $options = get_option(XF_LAYOUTS);
         $templates = get_option(XF_TEMPLATES);
         if (array_key_exists($layout, $options)) {
             unset($options[$layout]);
             foreach ($templates as $filename => $layoutname) {
                 if ($layoutname === $layout) {
                     $templates[$filename] = 'xf_layout-default';
                 }
             }
             update_option(XF_LAYOUTS, $options);
             update_option(XF_TEMPLATES, $templates);
             $this->generate_theme();
             $redirect = $redirect . '&xtreme_msg=layout_deleted&layout=' . $layout;
             wp_redirect($redirect);
         }
     }
 }