+/**
+ * Start preview theme output buffer.
+ *
+ * Will only preform task if the user has permissions and template and preview
+ * query variables exist.
+ *
+ * @since 2.6.0
+ */
+function preview_theme() {
+ if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
+ return;
+
+ if ( !current_user_can( 'switch_themes' ) )
+ return;
+
+ $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
+
+ if ( validate_file($_GET['template']) )
+ return;
+
+ add_filter( 'template', '_preview_theme_template_filter' );
+
+ if ( isset($_GET['stylesheet']) ) {
+ $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
+ if ( validate_file($_GET['stylesheet']) )
+ return;
+ add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
+ }
+
+ // Prevent theme mods to current theme being used on theme being previewed
+ add_filter( 'pre_option_mods_' . get_current_theme(), create_function( '', "return array();" ) );
+
+ ob_start( 'preview_theme_ob_filter' );
+}
+add_action('setup_theme', 'preview_theme');
+
+/**
+ * Private function to modify the current template when previewing a theme
+ *
+ * @since 2.9.0
+ * @access private
+ *
+ * @return string
+ */
+function _preview_theme_template_filter() {
+ return isset($_GET['template']) ? $_GET['template'] : '';
+}
+
+/**
+ * Private function to modify the current stylesheet when previewing a theme
+ *
+ * @since 2.9.0
+ * @access private
+ *
+ * @return string
+ */
+function _preview_theme_stylesheet_filter() {
+ return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
+}
+
+/**
+ * Callback function for ob_start() to capture all links in the theme.
+ *
+ * @since 2.6.0
+ * @access private
+ *
+ * @param string $content
+ * @return string
+ */
+function preview_theme_ob_filter( $content ) {
+ return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
+}
+
+/**
+ * Manipulates preview theme links in order to control and maintain location.
+ *
+ * Callback function for preg_replace_callback() to accept and filter matches.
+ *
+ * @since 2.6.0
+ * @access private
+ *
+ * @param array $matches
+ * @return string
+ */
+function preview_theme_ob_filter_callback( $matches ) {
+ if ( strpos($matches[4], 'onclick') !== false )
+ $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if its escaped by \ to prevent breaking mid-attribute.
+ if (
+ ( false !== strpos($matches[3], '/wp-admin/') )
+ ||
+ ( false !== strpos($matches[3], '://') && 0 !== strpos($matches[3], get_option('home')) )
+ ||
+ ( false !== strpos($matches[3], '/feed/') )
+ ||
+ ( false !== strpos($matches[3], '/trackback/') )
+ )
+ return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
+
+ $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
+ if ( 0 === strpos($link, 'preview=1') )
+ $link = "?$link";
+ return $matches[1] . esc_attr( $link ) . $matches[4];
+}
+
+/**
+ * Switches current theme to new template and stylesheet names.
+ *
+ * @since unknown
+ * @uses do_action() Calls 'switch_theme' action on updated theme display name.
+ *
+ * @param string $template Template name
+ * @param string $stylesheet Stylesheet name.
+ */
+function switch_theme($template, $stylesheet) {
+ update_option('template', $template);
+ update_option('stylesheet', $stylesheet);
+ delete_option('current_theme');
+ $theme = get_current_theme();
+ do_action('switch_theme', $theme);
+}
+
+/**
+ * Checks that current theme files 'index.php' and 'style.css' exists.
+ *
+ * Does not check the 'default' theme. The 'default' theme should always exist
+ * or should have another theme renamed to that template name and directory
+ * path. Will switch theme to default if current theme does not validate.
+ * You can use the 'validate_current_theme' filter to return FALSE to
+ * disable this functionality.
+ *
+ * @since 1.5.0
+ *
+ * @return bool
+ */