<commit_before><?php

/**
 * Main plugin class.
 */
class Toolbar_Theme_Switcher {

	/** @var WP_Theme $theme */
	public static $theme = false;

	/**
	 * Hooks that need to be set up early.
	 */
	public static function on_load() {

		add_action( 'setup_theme', array( __CLASS__, 'setup_theme' ) );
		add_action( 'init', array( __CLASS__, 'init' ) );
	}

	/**
	 * Loads cookie and sets up theme filters.
	 */
	public static function setup_theme() {

		global $pagenow;

		if ( ( is_admin() && 'themes.php' == $pagenow ) || ! self::can_switch_themes() ) {
			return;
		}

		self::check_reset();
		self::load_cookie();

		if ( empty( self::$theme ) ) {
			return;
		}

		add_filter( 'pre_option_template', array( self::$theme, 'get_template' ) );
		add_filter( 'pre_option_stylesheet', array( self::$theme, 'get_stylesheet' ) );
		add_filter( 'pre_option_stylesheet_root', array( self::$theme, 'get_theme_root' ) );
		$parent = self::$theme->parent();
		add_filter( 'pre_option_template_root', array( empty( $parent ) ? self::$theme : $parent, 'get_theme_root' ) );
		add_filter( 'pre_option_current_theme', '__return_false' );
	}

	/**
	 * Clear theme choice if reset variable is present in request.
	 */
	public static function check_reset() {

		if ( ! empty( filter_input( INPUT_GET, 'tts_reset' ) ) ) {
			setcookie( self::get_cookie_name(), '', 1 );
			nocache_headers();
			wp_safe_redirect( home_url() );
			die;
		}
	}

	/**
	 * If allowed to switch theme.
	 *
	 * @return boolean
	 */
	public static function can_switch_themes() {

		$capability = apply_filters( 'tts_capability', 'switch_themes' );

		return apply_filters( 'tts_can_switch_themes', current_user_can( $capability ) );
	}

	/**
	 * Sets if cookie is defined to non-default theme.
	 */
	public static function load_cookie() {

		$theme_name  = filter_input( INPUT_COOKIE, self::get_cookie_name() );

		if ( ! $theme_name ) {
			return;
		}

		$theme = wp_get_theme( $theme_name );

		if (
			$theme->exists()
			&& $theme->get( 'Name' ) !== get_option( 'current_theme' )
			&& $theme->is_allowed()
		) {
			self::$theme = $theme;
		}
	}

	/**
	 * Returns cookie name, based on home URL so it differs for sites in multisite.
	 *
	 * @return string
	 */
	public static function get_cookie_name() {

		static $hash;

		if ( empty( $hash ) ) {
			$hash = 'wordpress_tts_theme_' . md5( home_url( '', 'http' ) );
		}

		return $hash;
	}

	/**
	 * Retrieves allowed themes.
	 *
	 * @return WP_Theme[]
	 */
	public static function get_allowed_themes() {

		static $themes;

		if ( isset( $themes ) ) {
			return $themes;
		}

		$wp_themes = wp_get_themes( array( 'allowed' => true ) );

		/** @var WP_Theme $theme */
		foreach ( $wp_themes as $theme ) {

			// Make keys names (rather than slugs) for backwards compat.
			$themes[ $theme->get( 'Name' ) ] = $theme;
		}

		$themes = apply_filters( 'tts_allowed_themes', $themes );

		return $themes;
	}

	/**
	 * Sets up hooks that doesn't need to happen early.
	 */
	public static function init() {

		if ( self::can_switch_themes() ) {
			add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 90 );
			add_action( 'wp_ajax_tts_set_theme', array( __CLASS__, 'set_theme' ) );
		}

		load_plugin_textdomain( 'toolbar-theme-switcher', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
	}<commit_msg>Fixed path to translation files.<commit_after><?php

/**
 * Main plugin class.
 */
class Toolbar_Theme_Switcher {

	/** @var WP_Theme $theme */
	public static $theme = false;

	/**
	 * Hooks that need to be set up early.
	 */
	public static function on_load() {

		add_action( 'setup_theme', array( __CLASS__, 'setup_theme' ) );
		add_action( 'init', array( __CLASS__, 'init' ) );
	}

	/**
	 * Loads cookie and sets up theme filters.
	 */
	public static function setup_theme() {

		global $pagenow;

		if ( ( is_admin() && 'themes.php' == $pagenow ) || ! self::can_switch_themes() ) {
			return;
		}

		self::check_reset();
		self::load_cookie();

		if ( empty( self::$theme ) ) {
			return;
		}

		add_filter( 'pre_option_template', array( self::$theme, 'get_template' ) );
		add_filter( 'pre_option_stylesheet', array( self::$theme, 'get_stylesheet' ) );
		add_filter( 'pre_option_stylesheet_root', array( self::$theme, 'get_theme_root' ) );
		$parent = self::$theme->parent();
		add_filter( 'pre_option_template_root', array( empty( $parent ) ? self::$theme : $parent, 'get_theme_root' ) );
		add_filter( 'pre_option_current_theme', '__return_false' );
	}

	/**
	 * Clear theme choice if reset variable is present in request.
	 */
	public static function check_reset() {

		if ( ! empty( filter_input( INPUT_GET, 'tts_reset' ) ) ) {
			setcookie( self::get_cookie_name(), '', 1 );
			nocache_headers();
			wp_safe_redirect( home_url() );
			die;
		}
	}

	/**
	 * If allowed to switch theme.
	 *
	 * @return boolean
	 */
	public static function can_switch_themes() {

		$capability = apply_filters( 'tts_capability', 'switch_themes' );

		return apply_filters( 'tts_can_switch_themes', current_user_can( $capability ) );
	}

	/**
	 * Sets if cookie is defined to non-default theme.
	 */
	public static function load_cookie() {

		$theme_name  = filter_input( INPUT_COOKIE, self::get_cookie_name() );

		if ( ! $theme_name ) {
			return;
		}

		$theme = wp_get_theme( $theme_name );

		if (
			$theme->exists()
			&& $theme->get( 'Name' ) !== get_option( 'current_theme' )
			&& $theme->is_allowed()
		) {
			self::$theme = $theme;
		}
	}

	/**
	 * Returns cookie name, based on home URL so it differs for sites in multisite.
	 *
	 * @return string
	 */
	public static function get_cookie_name() {

		static $hash;

		if ( empty( $hash ) ) {
			$hash = 'wordpress_tts_theme_' . md5( home_url( '', 'http' ) );
		}

		return $hash;
	}

	/**
	 * Retrieves allowed themes.
	 *
	 * @return WP_Theme[]
	 */
	public static function get_allowed_themes() {

		static $themes;

		if ( isset( $themes ) ) {
			return $themes;
		}

		$wp_themes = wp_get_themes( array( 'allowed' => true ) );

		/** @var WP_Theme $theme */
		foreach ( $wp_themes as $theme ) {

			// Make keys names (rather than slugs) for backwards compat.
			$themes[ $theme->get( 'Name' ) ] = $theme;
		}

		$themes = apply_filters( 'tts_allowed_themes', $themes );

		return $themes;
	}

	/**
	 * Sets up hooks that doesn't need to happen early.
	 */
	public static function init() {

		if ( self::can_switch_themes() ) {
			add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 90 );
			add_action( 'wp_ajax_tts_set_theme', array( __CLASS__, 'set_theme' ) );
		}

		load_plugin_textdomain( 'toolbar-theme-switcher', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/lang' );
	}
{'fraction_non_alphanumeric': 0.15666069295101553, 'fraction_numerical': 0.0011947431302270011, 'mean_word_length': 3.586986301369863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5806844', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2315, 'n_words': 559}