<?php
/**
 * Post Class
 *
 * @package Page_Generator_Pro
 * @author WP Zinc
 */

/**
 * Registers a metabox on Posts, Pages and public facing Custom Post Types
 * that do not use the block editor, saving settings when the Post is saved
 * in the WordPress Administration interface.
 *
 * @package Page_Generator_Pro
 * @author  WP Zinc
 */
class Page_Generator_Pro_Post {

	/**
	 * Holds the base object.
	 *
	 * @since   5.5.5
	 *
	 * @var     object
	 */
	public $base;

	/**
	 * Holds the Post's settings.
	 *
	 * @since   5.5.5
	 *
	 * @var     array
	 */
	private $settings = array();

	/**
	 * Constructor
	 *
	 * @since   5.5.5
	 *
	 * @param   object $base    Base Plugin Class.
	 */
	public function __construct( $base ) {

		$this->base = $base;

		add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
		add_action( 'save_post', array( $this, 'save_post' ) );

	}

	/**
	 * Adds a meta box for the given Post Type.
	 *
	 * @since   5.5.5
	 *
	 * @param   string  $post_type  Post Type.
	 * @param   WP_Post $post       Post.
	 */
	public function add_meta_boxes( $post_type, $post ) {

		// Don't register the meta box if this Post Type isn't supported.
		if ( ! in_array( $post_type, array_keys( $this->base->get_class( 'common' )->get_post_types_key_value_array() ), true ) ) {
			return;
		}

		// Don't register the meta box if the block editor is being used, as register_post_meta() handles saving post meta.
		if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post ) ) {
			return;
		}

		// Don't register the meta box if the Page/Post/CPT wasn't generated by Page Generator Pro.
		if ( ! get_post_meta( $post->ID, '_page_generator_pro_group', true ) ) {
			return;
		}

		// Register Meta Box.
		add_meta_box( $this->base->plugin->name . '-meta-box', $this->base->plugin->displayName, array( $this, 'display_meta_box' ), $post_type, 'normal' );

		// Enqueue CSS.
		wp_enqueue_style( $this->base->plugin->name . '-admin', $this->base->plugin->url . 'assets/css/admin.css', array(), $this->base->plugin->version );

	}

	/**
	 * Outputs the meta box.
	 *
	 * @since   5.5.5
	 *
	 * @param   WP_Post $post   The Post being edited.
	 */
	public function display_meta_box( $post ) {

		// Fetch latitude and longitude for the given Post ID.
		$latitude_longitude = $this->base->get_class( 'geo' )->get( $post->ID );

		// Fetch settings.
		$this->settings = array(
			'exclude'   => get_post_meta( $post->ID, '_page_generator_pro_exclude', true ),
			'latitude'  => is_array( $latitude_longitude ) ? $latitude_longitude['latitude'] : '',
			'longitude' => is_array( $latitude_longitude ) ? $latitude_longitude['longitude'] : '',
		);

		// Load metabox view.
		include $this->base->plugin->folder . 'views/admin/post-meta-box.php';

	}

	/**
	 * Saves Post Settings when either editing a Post/Page or using the Quick Edit functionality.
	 *
	 * @since   5.5.5
	 *
	 * @param   int $post_id    Post ID.
	 */
	public function save_post( $post_id ) {

		// Bail if this is an autosave.
		if ( wp_is_post_autosave( $post_id ) ) {
			return;
		}

		// Bail if this is a post revision.
		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		// Bail if no nonce field exists.
		if ( ! isset( $_POST[ $this->base->plugin->name . '-save-meta-nonce' ] ) ) {
			return;
		}

		// Bail if the nonce verification fails.
		if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ $this->base->plugin->name . '-save-meta-nonce' ] ) ), $this->base->plugin->name . '-save-meta' ) ) {
			return;
		}

		// Bail if no Page Generator Pro settings were posted.
		if ( ! isset( $_POST[ $this->base->plugin->name ] ) ) {
			return;
		}

		// Save Post's settings.
		// Exclude from regeneration.
		if ( array_key_exists( 'exclude', $_POST[ $this->base->plugin->name ] ) ) {
			update_post_meta( $post_id, '_page_generator_pro_exclude', 1 );
		} else {
			delete_post_meta( $post_id, '_page_generator_pro_exclude' );
		}

		// Latitude and Longitude.
		if ( empty( $_POST[ $this->base->plugin->name ]['latitude'] ) && empty( $_POST[ $this->base->plugin->name ]['longitude'] ) ) {
			// Delete.
			$this->base->get_class( 'geo' )->delete( $post_id );
		} else {
			// Update.
			$this->base->get_class( 'geo' )->update(
				$post_id,
				get_post_meta( $post_id, '_page_generator_pro_group', true ),
				sanitize_text_field( wp_unslash( $_POST[ $this->base->plugin->name ]['latitude'] ) ),
				sanitize_text_field( wp_unslash( $_POST[ $this->base->plugin->name ]['longitude'] ) )
			);
		}

	}

}
