<commit_before><?php
namespace WordpressLib\Theme;

use WordpressLib\Posts\CustomTaxonomy;

class Admin {

	public function __construct($slug, $title, $pluginDir, $templateVars=[]) {
		$this->slug = $slug;
		$this->title = $title;
		$this->pluginDir = $pluginDir;
		$this->templateVars = $templateVars;
		add_filter('admin_body_class', [$this, 'bodyClass']);
		add_action('init', [$this, 'init'], 20);
		add_action('admin_init', [$this, 'adminInit'], 20);
	}

	public function init() {
	}

	public function adminInit() {
	}

	public function isEmbedded() {
		return isset($_GET['embedded']);
	}

	public function isThemeScreen() {
		global $current_screen;
		return strpos($current_screen->base, $this->slug) !== FALSE || $this->isEmbedded();
	}

	public function isEmbedScreen() {
		global $current_screen;
		return (
			preg_match("/$this->slug-(.*)-settings$/", $current_screen->id, $matches)
			&& in_array($matches[1], ['page','gallery','users'])
		);
	}

	protected function getBodyClass() {
		return $this->slug;
	}

	public function bodyClass($classes) {
		$c = [];
		if ($this->isThemeScreen()) {
			$c[] = $this->getBodyClass();
			if ($this->isEmbedScreen()) $c[] = "$this->slug--embed";
		}
		if ($this->isEmbedded()) $c[] = "$this->slug--embedded";
		return empty($c) ? $classes : $classes.' '.implode(' ', $c);
	}

	protected function getSubMenus() {
		return [];
	}

	public function getScreenPostTypeMap() {
		return [];
	}

	public function getCapabilityFromScreen() {
		return $this->optionsCapability;
	}

	public function getPostTypeFromScreen() {
		$map = $this->getScreenPostTypeMap();
		return get_post_type_object(isset($map[$this->screenSlug]) ? $map[$this->screenSlug] : $this->screenSlug);
	}

	public function getScreenFromPostType($post_type) {
		$name = is_object($post_type) ? $post_type->name : "$post_type";
		return ($screen = array_search($name, $this->getScreenPostTypeMap())) !== FALSE ? $screen : $name;
	}

	public function getScreenTemplateMap() {
		$map = [];
		foreach (['page','post'] as $s) $map[$s] = 'listing';
		return $map;
	}

	public function getScreenTemplate() {
		$map = $this->getScreenTemplateMap();
		return isset($map[$this->screenSlug]) ? $map[$this->screenSlug] : 'embed';
	}

	public function addOptions($capability, $options=[]) {
		$this->optionsCapability = $capability;
		add_action('admin_menu', function() use ($options, $capability) {
			$handle = "$this->slug-settings";
			add_menu_page("$this->title Settings", $this->title, $capability, $handle, [$this, 'optionsHTML']);
			foreach($this->getSubMenus() as $submenu) {
				extract($submenu);
				add_submenu_page($handle, "$this->title $title", $title, $capability, "$this->slug-$slug-settings", [$this, 'optionsHTML']);
			}
		});
	}

	public function optionsHTML() {
		global $current_screen;
		if (strpos($current_screen->id, "$this->slug-settings") !== FALSE) {
			$this->screenSlug = 'page';
		}
		elseif (preg_match("/$this->slug-(.*)-settings$/", $current_screen->id, $matches)) {
			$this->screenSlug = $matches[1];
		}
		else throw new \InvalidArgumentException("Admin screen '$current_screen->id' is invalid.");
		if (!current_user_can($this->getCapabilityFromScreen())) {
			wp_die(__('You do not have sufficient permissions to access this page.'));
		}
		$this->template($this->getScreenTemplate(), TRUE);<commit_msg>Exception on missing screen template<commit_after><?php
namespace WordpressLib\Theme;

use WordpressLib\Posts\CustomTaxonomy;

class Admin {

	public function __construct($slug, $title, $pluginDir, $templateVars=[]) {
		$this->slug = $slug;
		$this->title = $title;
		$this->pluginDir = $pluginDir;
		$this->templateVars = $templateVars;
		add_filter('admin_body_class', [$this, 'bodyClass']);
		add_action('init', [$this, 'init'], 20);
		add_action('admin_init', [$this, 'adminInit'], 20);
	}

	public function init() {
	}

	public function adminInit() {
	}

	public function isEmbedded() {
		return isset($_GET['embedded']);
	}

	public function isThemeScreen() {
		global $current_screen;
		return strpos($current_screen->base, $this->slug) !== FALSE || $this->isEmbedded();
	}

	public function isEmbedScreen() {
		global $current_screen;
		return (
			preg_match("/$this->slug-(.*)-settings$/", $current_screen->id, $matches)
			&& in_array($matches[1], ['page','gallery','users'])
		);
	}

	protected function getBodyClass() {
		return $this->slug;
	}

	public function bodyClass($classes) {
		$c = [];
		if ($this->isThemeScreen()) {
			$c[] = $this->getBodyClass();
			if ($this->isEmbedScreen()) $c[] = "$this->slug--embed";
		}
		if ($this->isEmbedded()) $c[] = "$this->slug--embedded";
		return empty($c) ? $classes : $classes.' '.implode(' ', $c);
	}

	protected function getSubMenus() {
		return [];
	}

	public function getScreenPostTypeMap() {
		return [];
	}

	public function getCapabilityFromScreen() {
		return $this->optionsCapability;
	}

	public function getPostTypeFromScreen() {
		$map = $this->getScreenPostTypeMap();
		return get_post_type_object(isset($map[$this->screenSlug]) ? $map[$this->screenSlug] : $this->screenSlug);
	}

	public function getScreenFromPostType($post_type) {
		$name = is_object($post_type) ? $post_type->name : "$post_type";
		return ($screen = array_search($name, $this->getScreenPostTypeMap())) !== FALSE ? $screen : $name;
	}

	public function getScreenTemplateMap() {
		$map = [];
		foreach (['page','post'] as $s) $map[$s] = 'listing';
		return $map;
	}

	public function getScreenTemplate() {
		$map = $this->getScreenTemplateMap();
		if (isset($map[$this->screenSlug])) {
			return $map[$this->screenSlug];
		}
		throw new \BadMethodCallException("Missing template for the section '$this->screenSlug'.");
	}

	public function addOptions($capability, $options=[]) {
		$this->optionsCapability = $capability;
		add_action('admin_menu', function() use ($options, $capability) {
			$handle = "$this->slug-settings";
			add_menu_page("$this->title Settings", $this->title, $capability, $handle, [$this, 'optionsHTML']);
			foreach($this->getSubMenus() as $submenu) {
				extract($submenu);
				add_submenu_page($handle, "$this->title $title", $title, $capability, "$this->slug-$slug-settings", [$this, 'optionsHTML']);
			}
		});
	}

	public function optionsHTML() {
		global $current_screen;
		if (strpos($current_screen->id, "$this->slug-settings") !== FALSE) {
			$this->screenSlug = 'page';
		}
		elseif (preg_match("/$this->slug-(.*)-settings$/", $current_screen->id, $matches)) {
			$this->screenSlug = $matches[1];
		}
		else throw new \InvalidArgumentException("Admin screen '$current_screen->id' is invalid.");
		if (!current_user_can($this->getCapabilityFromScreen())) {
			wp_die(__('You do not have sufficient permissions to access this page.'));
		}
		$this->template($this->getScreenTemplate(), TRUE);
{'fraction_non_alphanumeric': 0.1918732580313921, 'fraction_numerical': 0.001760305119554056, 'mean_word_length': 5.049689440993789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 90, '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': '191535', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2259, 'n_words': 449}