<commit_before><?php

// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

/**
 * Papi Admin.
 *
 * @package Papi
 */

final class Papi_Admin {

	/**
	 * The page type.
	 *
	 * @var Papi_Page_Type
	 */

	private $page_type;

	/**
	 * The page type id.
	 *
	 * @var string
	 */

	private $page_type_id;

	/**
	 * The post type.
	 *
	 * @var string
	 */

	private $post_type;

	/**
	 * The instance of Papi Core.
	 *
	 * @var object
	 */

	private static $instance;

	/**
	 * Papi Admin instance.
	 *
	 * @return object
	 */

	public static function instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self;
			self::$instance->load_files();
			self::$instance->setup_globals();
			self::$instance->setup_actions();
			self::$instance->setup_filters();
		}

		return self::$instance;
	}

	/**
	 * The constructor.
	 */

	private function __construct() {}

	/**
	 * Cloning is forbidden.
	 */

	public function __clone() {
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '1.0.0' );
	}

	/**
	 * Unserializing instances of this class is forbidden.
	 */

	public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '1.0.0' );
	}

	/**
	 * Admin init.
	 *
	 * Setup the page type.
	 */

	public function admin_init() {
		if ( ! $this->setup_papi() ) {
			return;
		}

		$this->page_type->setup();
	}

	/**
	 * Add style to admin head.
	 */

	public function admin_head() {
		wp_enqueue_media();
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_style( 'papi-main', dirname( PAPI_PLUGIN_URL ) . '/dist/css/style.min.css', false, null );
	}

	/**
	 * Enqueue script into admin footer.
	 */

	public function admin_enqueue_scripts() {
		// WordPress will override window.papi on plugins page,
		// so don't include Papi JavaScript on plugins page.
		if ( strpos( $_SERVER['REQUEST_URI'], 'plugins.php' ) !== false ) {
			return;
		}

		wp_enqueue_script( 'papi-main', dirname( PAPI_PLUGIN_URL ) . '/dist/js/main.min.js', [
			'json2',
			'jquery',
			'jquery-ui-core',
			'jquery-ui-sortable',
			'wp-color-picker'
		], '', true );

		wp_localize_script( 'papi-main', 'papiL10n', [
			'remove'        => __( 'Remove', 'papi' ),
			'requiredError' => __( 'This fields are required:', 'papi' ),
		] );
	}

	/**
	 * Add custom body class when it's a page type.
	 *
	 * @param string $classes
	 *
	 * @return string
	 */

	public function admin_body_class( $classes ) {
		$post_type = papi_get_post_type();

		if ( ! in_array( $post_type, papi_get_post_types() ) ) {
			return $classes;
		}

		if ( count( get_page_templates() ) ) {
			$classes .= 'papi-hide-cpt';
		}

		return $classes;
	}

	/**
	 * Output Papi page type hidden field.
	 *
	 * This will only output on a post type page.
	 */

	public function edit_form_after_title() {
		wp_nonce_field( 'papi_save_data', 'papi_meta_nonce' );
		?>
		<input type="hidden" name="<?php esc_attr_e( PAPI_PAGE_TYPE_KEY ); ?>" value="<?php esc_attr_e( papi_get_page_type_id() ); ?>"/>
		<?php
	}

	/**
	 * Output hidden meta boxes.
	 */

	public function hidden_meta_boxes() {
		global $_wp_post_type_features;
		if ( ! isset( $_wp_post_type_features[$this->post_type]['editor'] ) ) {
			add_meta_box( 'papi-hidden-editor', 'Papi hidden editor', [ $this, 'hidden_meta_box_editor' ], $this->post_type );
		}
	}

	/**
	 * Output hidden WordPress editor.
	 */

	public function hidden_meta_box_editor() {
		wp_editor( '', 'papiHiddenEditor' );
	}

	/**
	 * Load admin files that are not loaded by the autoload.
	 */

	private function load_files() {
		require_once __DIR__ . '/class-papi-admin-ajax.php';
		require_once __DIR__ . '/class-papi-admin-data-handler.php';
		require_once __DIR__ . '/class-papi-admin-management-pages.php';
		require_once __DIR__ . '/class-papi-admin-post-handler.php';
		require_once __DIR__ . '/class-papi-admin-option-handler.php';
	}

	/**
	 * Load post new action
	 * Redirect to right url if no page type is set.
	 */

	public function load_post_new() {
		$request_uri = $_SERVER['REQUEST_URI'];
		$post_types = papi_get_post_types();
		$post_type  = papi_get_post_type();

		if ( in_array( $post_type, $post_types ) && strpos( $request_uri, 'page_type=' ) === false && strpos( $request_uri, 'papi-bypass=true' ) === false ) {
			$parsed_url = parse_url( $request_uri );

			$only_page_type = papi_filter_settings_only_page_type( $post_type );

			// Check if we should show one post type or not and create the right url for that.
			if ( ! empty( $only_page_type ) ) {
				$url = papi_get_page_new_url( $only_page_type, false );
			} else {
				$page = 'page=papi-add-new-page,' . $post_type;

				if ( $post_type !== 'post' ) {
					$page = '&' . $page;
				}

				$url = 'edit.php?' . $parsed_url['query'] . $page;
			}

			wp_safe_redirect( $url );
			exit;
		}
	}

	/**
	 * Add custom table header to page type.
	 *
	 * @param array $defaults
	 *
	 * @return array
	 */

	public function manage_page_type_posts_columns( $defaults ) {
		$defaults['page_type'] = papi_filter_settings_page_type_column_title( $this->post_type );
		return $defaults;
	}

	/**
	 * Add custom table column to page type.
	 *
	 * @param string $column_name
	 * @param int $post_id
	 */

	public function manage_page_type_posts_custom_column( $column_name, $post_id ) {
		if ( $column_name === 'page_type' ) {
			$page_type = papi_get_page_type_by_post_id( $post_id );
			if ( ! is_null( $page_type ) ) {
				esc_html_e( $page_type->name );
			} else {
				esc_html_e( papi_filter_settings_standard_page_name( papi_get_post_type() ) );
			}
		}
	}

	/**
	 * Menu callback that loads right view depending on what the `page` query string says.
	 */
<commit_msg>Fixed issue #72<commit_after><?php

// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

/**
 * Papi Admin.
 *
 * @package Papi
 */

final class Papi_Admin {

	/**
	 * The page type.
	 *
	 * @var Papi_Page_Type
	 */

	private $page_type;

	/**
	 * The page type id.
	 *
	 * @var string
	 */

	private $page_type_id;

	/**
	 * The post type.
	 *
	 * @var string
	 */

	private $post_type;

	/**
	 * The instance of Papi Core.
	 *
	 * @var object
	 */

	private static $instance;

	/**
	 * Papi Admin instance.
	 *
	 * @return object
	 */

	public static function instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self;
			self::$instance->load_files();
			self::$instance->setup_globals();
			self::$instance->setup_actions();
			self::$instance->setup_filters();
		}

		return self::$instance;
	}

	/**
	 * The constructor.
	 */

	private function __construct() {}

	/**
	 * Cloning is forbidden.
	 */

	public function __clone() {
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '1.0.0' );
	}

	/**
	 * Unserializing instances of this class is forbidden.
	 */

	public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '1.0.0' );
	}

	/**
	 * Admin init.
	 *
	 * Setup the page type.
	 */

	public function admin_init() {
		if ( ! $this->setup_papi() ) {
			return;
		}

		$this->page_type->setup();
	}

	/**
	 * Add style to admin head.
	 */

	public function admin_head() {
		wp_enqueue_media();
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_style( 'papi-main', dirname( PAPI_PLUGIN_URL ) . '/dist/css/style.min.css', false, null );
	}

	/**
	 * Enqueue script into admin footer.
	 */

	public function admin_enqueue_scripts() {
		// WordPress will override window.papi on plugins page,
		// so don't include Papi JavaScript on plugins page.
		if ( strpos( $_SERVER['REQUEST_URI'], 'plugins.php' ) !== false ) {
			return;
		}

		wp_enqueue_script( 'papi-main', dirname( PAPI_PLUGIN_URL ) . '/dist/js/main.min.js', [
			'json2',
			'jquery',
			'jquery-ui-core',
			'jquery-ui-sortable',
			'wp-color-picker'
		], '', true );

		wp_localize_script( 'papi-main', 'papiL10n', [
			'remove'        => __( 'Remove', 'papi' ),
			'requiredError' => __( 'This fields are required:', 'papi' ),
		] );
	}

	/**
	 * Add custom body class when it's a page type.
	 *
	 * @param string $classes
	 *
	 * @return string
	 */

	public function admin_body_class( $classes ) {
		$post_type = papi_get_post_type();

		if ( ! in_array( $post_type, papi_get_post_types() ) ) {
			return $classes;
		}

		if ( count( get_page_templates() ) ) {
			$classes .= 'papi-hide-cpt';
		}

		return $classes;
	}

	/**
	 * Output Papi page type hidden field.
	 *
	 * This will only output on a post type page.
	 */

	public function edit_form_after_title() {
		wp_nonce_field( 'papi_save_data', 'papi_meta_nonce' );
		?>
		<input type="hidden" name="<?php esc_attr_e( PAPI_PAGE_TYPE_KEY ); ?>" value="<?php esc_attr_e( papi_get_page_type_id() ); ?>"/>
		<?php
	}

	/**
	 * Output hidden meta boxes.
	 */

	public function hidden_meta_boxes() {
		global $_wp_post_type_features;
		if ( ! isset( $_wp_post_type_features[$this->post_type]['editor'] ) ) {
			add_meta_box( 'papi-hidden-editor', 'Papi hidden editor', [ $this, 'hidden_meta_box_editor' ], $this->post_type );
		}
	}

	/**
	 * Output hidden WordPress editor.
	 */

	public function hidden_meta_box_editor() {
		wp_editor( '', 'papiHiddenEditor' );
	}

	/**
	 * Load admin files that are not loaded by the autoload.
	 */

	private function load_files() {
		require_once __DIR__ . '/class-papi-admin-ajax.php';
		require_once __DIR__ . '/class-papi-admin-data-handler.php';
		require_once __DIR__ . '/class-papi-admin-management-pages.php';
		require_once __DIR__ . '/class-papi-admin-post-handler.php';
		require_once __DIR__ . '/class-papi-admin-option-handler.php';
	}

	/**
	 * Load post new action
	 * Redirect to right url if no page type is set.
	 */

	public function load_post_new() {
		$request_uri = $_SERVER['REQUEST_URI'];
		$post_types = papi_get_post_types();
		$post_type  = papi_get_post_type();

		if ( in_array( $post_type, $post_types ) && strpos( $request_uri, 'page_type=' ) === false && strpos( $request_uri, 'papi-bypass=true' ) === false ) {
			$parsed_url = parse_url( $request_uri );

			$only_page_type = papi_filter_settings_only_page_type( $post_type );

			// Check if we should show one post type or not and create the right url for that.
			if ( ! empty( $only_page_type ) ) {
				$url = papi_get_page_new_url( $only_page_type, false );
			} else {
				$page = 'page=papi-add-new-page,' . $post_type;

				if ( $post_type !== 'post' ) {
					$page = '&' . $page;
				}

				$url = 'edit.php?' . $parsed_url['query'] . $page;
			}

			wp_safe_redirect( $url );
			exit;
		}
	}

	/**
	 * Add custom table header to page type.
	 *
	 * @param array $defaults
	 *
	 * @return array
	 */

	public function manage_page_type_posts_columns( $defaults ) {
		$post_type  = papi_get_post_type();
		$post_types = papi_get_post_types();

		if ( in_array( $post_type, $post_types ) ) {
			$defaults['page_type'] = papi_filter_settings_page_type_column_title( $this->post_type );
		}

		return $defaults;
	}

	/**
	 * Add custom table column to page type.
	 *
	 * @param string $column_name
	 * @param int $post_id
	 */

	public function manage_page_type_posts_custom_column( $column_name, $post_id ) {
		if ( $column_name === 'page_type' ) {
			$page_type = papi_get_page_type_by_post_id( $post_id );
			if ( ! is_null( $page_type ) ) {
				esc_html_e( $page_type->name );
			} else {
				esc_html_e( papi_filter_settings_standard_page_name( papi_get_post_type() ) );
			}
		}
	}

	/**
	 * Menu callback that loads right view depending on what the `page` query string says.
	 */

{'fraction_non_alphanumeric': 0.15627439077269967, 'fraction_numerical': 0.003122018905558928, 'mean_word_length': 3.2149122807017543, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, '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': '5381894', 'n_tokens_mistral': 4644, 'n_tokens_neox': 4143, 'n_words': 916}