<?php
/**
 * Class for create page when plugin install
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
class fbbttmPageconfig{


	public function fbbttm_newall_create_pages()
	{

		// WordPress sets fresh_site to 0 after a page gets published.
		// Prevent fresh_site option from being set to 0 so that we can use it for further customizations.
		remove_action( 'publish_page', '_delete_option_fresh_site', 0 );

		$shoppage_code = 'fbbttm-fooditem';
		$cart_page_content ='fbbttm-cart';
		$checkout_page_content = 'fbbttm-checkout';
		$my_account_shortcode = 'fbbttm-myaccount';
		$order_received = 'fbbttm-order-received';

		$pages = apply_filters(
			'fbbttm_create_pages',
			array(
				'fbbttm-shop'           => array(
					'name'    => _x( 'fbbttm-shop', 'Page slug', 'foodboxbooker' ),
					'title'   => _x( 'Shop', 'Page title', 'foodboxbooker' ),
					'content' => '<!-- wp:shortcode -->[' . $shoppage_code. ']<!-- /wp:shortcode -->',
				),
				'fbbttm-cart'           => array(
					'name'    => _x( 'fbbttm-cart', 'Page slug', 'foodboxbooker' ),
					'title'   => _x( 'Cart', 'Page title', 'foodboxbooker' ),
					'content' => '<!-- wp:shortcode -->[' . $cart_page_content. ']<!-- /wp:shortcode -->',
				),
				'fbbttm-checkout'       => array(
					'name'    => _x( 'fbbttm-checkout', 'Page slug', 'foodboxbooker' ),
					'title'   => _x( 'Checkout', 'Page title', 'foodboxbooker' ),
					'content' => '<!-- wp:shortcode -->[' . $checkout_page_content. ']<!-- /wp:shortcode -->',
				),
				'fbbttm-myaccount'      => array(
					'name'    => _x( 'fbbttm-my-account', 'Page slug', 'foodboxbooker' ),
					'title'   => _x( 'My account', 'Page title', 'foodboxbooker' ),
					'content' => '<!-- wp:shortcode -->[' . $my_account_shortcode . ']<!-- /wp:shortcode -->',
				),
				'fbbttm-order-received'      => array(
					'name'    => _x( 'fbbttm-order-received', 'Page slug', 'foodboxbooker' ),
					'title'   => _x( 'Order Confirmation', 'Page title', 'foodboxbooker' ),
					'content' => '<!-- wp:shortcode -->[' . $order_received . ']<!-- /wp:shortcode -->',
				),
			)
		);


		foreach ( $pages as $key => $page ) {

			
			$this->fbbttm_create_page(
				esc_sql( $page['name'] ),
				'fbbttm_' . $key . '_page_id',
				$page['title'],
				$page['content'],
				! empty( $page['parent'] ) ? wc_get_page_id( $page['parent'] ) : '',
				! empty( $page['post_status'] ) ? $page['post_status'] : 'publish'
			);
		}

		// Restore the locale to the default locale.
		$this->fbbttm_restore_locale();

	}


	public function fbbttm_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0, $post_status = 'publish' ) {

		global $wpdb;

		$option_value = get_option( $option );
		
		if ( $option_value > 0 ) {

			$page_object = get_post( $option_value );

			if ( $page_object && 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ), true ) ) {
				// Valid page is already in place.
				return $page_object->ID;
			}
		}

		if ( strlen( $page_content ) > 0 ) {
			// Search for an existing page with the specified page content (typically a shortcode).
			$shortcode        = str_replace( array( '<!-- wp:shortcode -->', '<!-- /wp:shortcode -->' ), '', $page_content );
			//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$shortcode}%" ) );
		} else {
			// Search for an existing page with the specified page slug.
			//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
		}


		
		$valid_page_found = apply_filters( 'fbbttm_create_page_id', $valid_page_found, $slug, $page_content );

	
		if ( $valid_page_found ) {
			if ( $option ) {
				update_option( $option, $valid_page_found );
			}
			return $valid_page_found;
		}

		// Search for a matching valid trashed page.
		if ( strlen( $page_content ) > 0 ) {
			//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			// Search for an existing page with the specified page content (typically a shortcode).
			$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
		} else {
			//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			// Search for an existing page with the specified page slug.
			$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
		}

		if ( $trashed_page_found ) {
			$page_id   = $trashed_page_found;
			$page_data = array(
				'ID'          => $page_id,
				'post_status' => $post_status,
			);
			wp_update_post( $page_data );
		} else {
			$page_data = array(
				'post_status'    => $post_status,
				'post_type'      => 'page',
				'post_author'    => 1,
				'post_name'      => $slug,
				'post_title'     => $page_title,
				'post_content'   => $page_content,
				'post_parent'    => $post_parent,
				'comment_status' => 'closed',
			);
		$page_id   = wp_insert_post( $page_data );
		
		//do_action( 'fbbttm_page_created', $page_id, $page_data );
	}

	if ( $option ) {
		update_option( $option, $page_id );
	}
	return $page_id;
  }

	public function fbbttm_restore_locale(){
		global $wp_locale_switcher;
			if ( function_exists( 'restore_previous_locale' ) && isset( $wp_locale_switcher ) ) {
				restore_previous_locale();
				// Remove filter.
				remove_filter( 'plugin_locale', 'get_locale' );
			}
		}
 }
?>