<?php
defined('ABSPATH') || exit;

/**
 * Obtain the page slug generated by the channelize live shopping plugin..
 *
 * @return page-slug
 */


function chls_get_main_stream_page_details()
{
	$post_slug = '';
	$args = array(
		'post_type'              => array('page'),
		'meta_query' => array(
			array(
				'key'       => 'created_by_channelize_live_shopping',
				'value'     => '1',
			),
		),
	);

	// The Query
	$query = new WP_Query($args);
	if ($query->have_posts()) {
		$post_slug = get_post_field('post_name', $query->post->ID);
	}
	return $post_slug;
}

/*page creations.*/
function chls_create_custom_page()
{

	if (!(chls_get_post_id_by_meta_key_and_value('created_by_channelize_live_shopping', '1'))) {
		$options  = get_option('channelize_live_shopping');

		$new_page_id = wp_insert_post(
			array(
				'post_title' => 'Streams',
				'post_type'     => 'page',
				'post_name'  => 'streams',
				'comment_status' => 'closed',
				'ping_status' => 'closed',
				'post_content' => " <div  id='channelize-livebroadcast-profile'></div>
										<div  id='channelize-livebroadcast-list'></div>",
				'post_status' => 'publish',
				'post_author' => 1,
				'menu_order' => 0
			)
		);
		$template_file = 'channelize_live_shopping_stream_template.php';


		update_post_meta($new_page_id, '_wp_page_template', $template_file);
		update_post_meta($new_page_id, 'created_by_channelize_live_shopping', '1');
	}
}

/**
 * 

 * @param [type] $key
 * @param [type] $value
 * @return bool page exixt or not 
 */
function chls_get_post_id_by_meta_key_and_value($key, $value)
{


	$args = array(
		'post_type'              => array('page'),
		'meta_query' => array(
			array(
				'key'       => $key,
				'value'     => $value,
			),
		),
	);

	// The Query
	$query = new WP_Query($args);
	if ($query->have_posts()) {
		return true;
	} else {
		return false;
	}
}
