<?php
	/**
	 * Plugin Name: Theme Legal Pages
	 * Description: Adds Privacy, Terms and Cookie pages to Reading settings.
	 * Version: 1.0
	 */

	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	/**
	 * Remove default Privacy settings page
	 */
	add_action( 'admin_menu', function () {

		remove_submenu_page(
			'options-general.php',
			'options-privacy.php'
		);

	}, 999 );

	/**
	 * Reset default WP privacy page
	 */
	add_action( 'admin_init', function () {

		if ( get_option( 'wp_page_for_privacy_policy' ) ) {
			update_option( 'wp_page_for_privacy_policy', 0 );
		}

	}, 1 );

	/**
	 * Register legal pages settings
	 */
	add_action( 'admin_init', function () {

		/**
		 * Section
		 */
		add_settings_section(
			'theme_legal_pages_section',
			__( 'Legal Pages', 'theme' ),
			'__return_false',
			'reading'
		);

		/**
		 * Privacy Policy Page
		 */
		register_setting(
			'reading',
			'wp_page_for_privacy',
			[
				'type'              => 'integer',
				'sanitize_callback' => 'absint',
				'default'           => 0,
			]
		);

		add_settings_field(
			'wp_page_for_privacy',
			__( 'Privacy Policy Page', 'theme' ),
			function () {

				wp_dropdown_pages(
					[
						'name'              => 'wp_page_for_privacy',
						'selected'          => get_option( 'wp_page_for_privacy' ),
						'show_option_none'  => __( '&mdash; Select &mdash;' ),
						'option_none_value' => 0,
					]
				);

			},
			'reading',
			'theme_legal_pages_section'
		);

		/**
		 * Terms & Conditions Page
		 */
		register_setting(
			'reading',
			'wp_page_for_terms',
			[
				'type'              => 'integer',
				'sanitize_callback' => 'absint',
				'default'           => 0,
			]
		);

		add_settings_field(
			'wp_page_for_terms',
			__( 'Terms & Conditions Page', 'theme' ),
			function () {

				wp_dropdown_pages(
					[
						'name'              => 'wp_page_for_terms',
						'selected'          => get_option( 'wp_page_for_terms' ),
						'show_option_none'  => __( '&mdash; Select &mdash;' ),
						'option_none_value' => 0,
					]
				);

			},
			'reading',
			'theme_legal_pages_section'
		);

		/**
		 * Cookie Policy Page
		 */
		register_setting(
			'reading',
			'wp_page_for_cookie',
			[
				'type'              => 'integer',
				'sanitize_callback' => 'absint',
				'default'           => 0,
			]
		);

		add_settings_field(
			'wp_page_for_cookie',
			__( 'Cookie Policy Page', 'theme' ),
			function () {

				wp_dropdown_pages(
					[
						'name'              => 'wp_page_for_cookie',
						'selected'          => get_option( 'wp_page_for_cookie' ),
						'show_option_none'  => __( '&mdash; Select &mdash;' ),
						'option_none_value' => 0,
					]
				);

			},
			'reading',
			'theme_legal_pages_section'
		);

	}, 30 );

	/**
	 * Add labels in Pages list
	 */
	add_filter( 'display_post_states', function ( $post_states, $post ) {

		$privacy_page = (int) get_option( 'wp_page_for_privacy' );
		$terms_page   = (int) get_option( 'wp_page_for_terms' );
		$cookie_page  = (int) get_option( 'wp_page_for_cookie' );

		if ( $privacy_page === $post->ID ) {
			$post_states['theme_privacy_page'] = __( 'Privacy Policy Page', 'theme' );
		}

		if ( $terms_page === $post->ID ) {
			$post_states['theme_terms_page'] = __( 'Terms Page', 'theme' );
		}

		if ( $cookie_page === $post->ID ) {
			$post_states['theme_cookie_page'] = __( 'Cookie Policy Page', 'theme' );
		}

		return $post_states;

	}, 10, 2 );