<?php

namespace FSPoster\App\Controller\Ajax;

use FSPoster\App\Providers\CronJob;
use FSPoster\App\Providers\Date;
use FSPoster\App\Providers\DB;
use FSPoster\App\Providers\Helper;
use FSPoster\App\Providers\Request;

trait Schedule
{

	public function schedule_save ()
	{
		$id         = Request::post( 'id', '0', 'int' );
		$title      = Request::post( 'title', '', 'string' );
		$start_date = Request::post( 'start_date', '', 'string' );
		$start_time = Request::post( 'start_time', '', 'string' );
		$interval   = Request::post( 'interval', '0', 'num' );
		$share_time = Request::post( 'share_time', '', 'string' );

		$post_type_filter                = Request::post( 'post_type_filter', '', 'string' );
		$dont_post_out_of_stock_products = Request::post( 'dont_post_out_of_stock_products', '0', 'string', [ '0', '1' ] );
		$category_filter                 = Request::post( 'category_filter', '0', 'int' );
		$post_sort                       = Request::post( 'post_sort', 'random', 'string', [ 'random', 'random2', 'old_first', 'new_first' ] );
		$post_date_filter                = Request::post( 'post_date_filter', 'all', 'string', [ 'all', 'this_week', 'previously_week', 'this_month', 'previously_month', 'this_year', 'last_30_days', 'last_60_days' ] );
		$post_ids_p                      = Request::post( 'post_ids', '', 'string' );

		$custom_messages = Request::post( 'custom_messages', '', 'string' );
		$accounts_list   = Request::post( 'accounts_list', '', 'string' );

		$sleep_time_start = Request::post( 'sleep_time_start', '', 'string' );
		$sleep_time_end   = Request::post( 'sleep_time_end', '', 'string' );

		if ( $id > 0 )
		{
			$scheduleInf = DB::fetch( 'schedules', $id );

			if ( ! $scheduleInf )
			{
				Helper::response( FALSE );
			}
		}

		$post_ids = [];

		$post_ids_p = explode( ',', str_replace( ' ', '', $post_ids_p ) );
		foreach ( $post_ids_p as $postId )
		{
			if ( is_numeric( $postId ) && $postId > 0 )
			{
				$post_ids[] = (int) $postId;
			}
		}

		if ( count( $post_ids ) > 250 )
		{
			Helper::response( FALSE, 'Too many post selected! You can select maximum 250 posts!' );
		}

		$post_ids = implode( ',', $post_ids );
		$post_ids = empty( $post_ids ) ? NULL : $post_ids;

		if ( empty( $sleep_time_start ) || empty( $sleep_time_end ) )
		{
			$sleep_time_start = NULL;
			$sleep_time_end   = NULL;
		}
		else
		{
			$sleep_time_start = Date::timeSQL( $sleep_time_start );
			$sleep_time_end   = Date::timeSQL( $sleep_time_end );
		}

		$_custom_messages = [];
		if ( ! empty( $custom_messages ) )
		{
			$custom_messages = json_decode( $custom_messages, TRUE );
			$custom_messages = is_array( $custom_messages ) ? $custom_messages : [];

			foreach ( $custom_messages as $socialNetwork => $message1 )
			{
				if ( in_array( $socialNetwork, [ 'fb', 'instagram', 'instagram_h', 'linkedin', 'twitter', 'pinterest', 'vk', 'ok', 'tumblr', 'reddit', 'google_b', 'telegram', 'medium', 'wordpress' ] ) && is_string( $message1 ) )
				{
					$_custom_messages[$socialNetwork] = $message1;
				}
			}
		}
		$_custom_messages = empty( $_custom_messages ) ? NULL : json_encode( $_custom_messages );

		$_accounts_list = [];
		if ( ! empty( $accounts_list ) )
		{
			$accounts_list = json_decode( $accounts_list, TRUE );
			$accounts_list = is_array( $accounts_list ) ? $accounts_list : [];

			foreach ( $accounts_list as $socialAccount )
			{
				if ( is_string( $socialAccount ) )
				{
					$socialAccount = explode( ':', $socialAccount );
					if ( ! ( count( $socialAccount ) == 2 && is_numeric( $socialAccount[1] ) ) )
					{
						continue;
					}

					$_accounts_list[] = ( $socialAccount[0] == 'account' ? 'account' : 'node' ) . ':' . $socialAccount[1];
				}
			}
		}

		if ( empty( $_accounts_list ) )
		{
			Helper::response( FALSE, esc_html__( 'No account or coummunications are selected!', 'fs-poster' ) );
		}

		$_accounts_list = implode( ',', $_accounts_list );

		// sanitize post types array...
		$allowedPostTypes = explode( '|', Helper::getOption( 'allowed_post_types', '' ) );

		if ( ! in_array( $post_type_filter, $allowedPostTypes ) )
		{
			$post_type_filter = '';
		}

		if ( empty( $title ) )
		{
			Helper::response( FALSE, esc_html__( 'The name must not be empty!', 'fs-poster' ) );
		}

		if ( empty( $start_date ) || empty( $start_time ) )
		{
			Helper::response( FALSE, esc_html__( 'Please select the start date and time!', 'fs-poster' ) );
		}

		if ( ! is_numeric( $interval ) | $interval <= 0 )
		{
			Helper::response( FALSE, esc_html__( 'Please type the interval!', 'fs-poster' ) );
		}

		$start_date = Date::dateSQL( $start_date );
		$start_time = Date::timeSQL( $start_time );

		$cronStartTime = $start_date . ' ' . $start_time;

		$sqlArr = [
			'blog_id'           => Helper::getBlogId(),
			'title'             => $title,
			'start_date'        => $start_date,
			'interval'          => $interval,
			'status'            => 'active',
			'insert_date'       => Date::dateTimeSQL(),
			'user_id'           => get_current_user_id(),
			'share_time'        => $start_time,
			'next_execute_time' => $cronStartTime,

			'post_ids'      => $post_ids,
			'save_post_ids' => $post_ids,

			'post_type_filter'                => $post_type_filter,
			'dont_post_out_of_stock_products' => $dont_post_out_of_stock_products,
			'category_filter'                 => $category_filter > 0 ? $category_filter : NULL,
			'post_sort'                       => $post_sort,
			'post_date_filter'                => $post_date_filter,

			'custom_post_message' => $_custom_messages,
			'share_on_accounts'   => $_accounts_list,

			'sleep_time_start' => $sleep_time_start,
			'sleep_time_end'   => $sleep_time_end
		];

		if ( $id > 0 && $scheduleInf['status'] != 'finished' )
		{
			unset( $sqlArr['status'] );
			DB::DB()->update( DB::table( 'schedules' ), $sqlArr, [ 'id' => $id ] );
		}
		else
		{
			DB::DB()->insert( DB::table( 'schedules' ), $sqlArr );
		}

		Helper::response( TRUE );
	}

	public function wp_native_schedule_save ()
	{
		$post_id         = 0;
		$info            = Request::post( 'info', '', 'string' );
		$custom_messages = Request::post( 'custom_messages', '', 'string' );
		$accounts_list   = Request::post( 'accounts_list', '', 'string' );

		if ( ! empty( $info ) )
		{
			$info    = json_decode( $info, TRUE );
			$info    = is_array( $info ) ? $info : [];
			$post_id = $info['post_id'];
		}

		if ( ! ( $post_id > 0 ) )
		{
			Helper::response( FALSE );
		}

		$feeds = DB::fetchAll( 'feeds', [
			'post_id'   => $post_id,
			'is_sended' => 0,
			'blog_id'   => Helper::getBlogId()
		] );

		if ( ! $feeds )
		{
			Helper::response( FALSE );
		}

		$defaultNodeIds = [];

		foreach ( $feeds as $feed )
		{
			$defaultNodeIds[] = $feed['node_id'];
		}

		$_custom_messages = [];

		if ( ! empty( $custom_messages ) )
		{
			$custom_messages = json_decode( $custom_messages, TRUE );
			$custom_messages = is_array( $custom_messages ) ? $custom_messages : [];

			foreach ( $custom_messages as $socialNetwork => $message1 )
			{
				if ( in_array( $socialNetwork, [ 'fb', 'instagram', 'instagram_h', 'linkedin', 'twitter', 'pinterest', 'vk', 'ok', 'tumblr', 'reddit', 'google_b', 'telegram', 'medium', 'wordpress' ] ) && is_string( $message1 ) )
				{
					$_custom_messages[$socialNetwork] = $message1;
				}
			}
		}

		$_accounts_list = [
			'accounts' => [],
			'nodes'    => []
		];

		if ( ! empty( $accounts_list ) )
		{
			$accounts_list = json_decode( $accounts_list, TRUE );
			$accounts_list = is_array( $accounts_list ) ? $accounts_list : [];

			foreach ( $accounts_list as $socialAccount )
			{
				if ( is_string( $socialAccount ) )
				{
					$socialAccount = explode( ':', $socialAccount );
					if ( ! ( count( $socialAccount ) == 2 && is_numeric( $socialAccount[1] ) ) )
					{
						continue;
					}

					if ( $socialAccount[0] === 'account' )
					{
						$_accounts_list['accounts'][] = $socialAccount[1];
					}
					else
					{
						$_accounts_list['nodes'][] = $socialAccount[1];
					}
				}
			}
		}

		$accountIDs = implode( ',', $_accounts_list['accounts'] );
		$nodeIDs    = implode( ',', $_accounts_list['nodes'] );

		if ( ! empty( $accountIDs ) )
		{
			$accounts = DB::DB()->get_results(
				DB::DB()->prepare( "
				SELECT 
					tb2.*, IFNULL(tb1.filter_type, 'no') AS filter_type, tb1.categories, (SELECT GROUP_CONCAT(`name`) FROM " . DB::WPtable( 'terms', TRUE ) . " WHERE FIND_IN_SET(term_id,tb1.categories) ) AS categories_name,'account' AS node_type 
				FROM " . DB::table( 'accounts' ) . " tb2
				LEFT JOIN " . DB::table( 'account_status' ) . " tb1 ON tb2.id=tb1.account_id AND tb1.user_id=%d
				WHERE (tb2.user_id=%d OR is_public=1) AND tb2.blog_id=%d AND tb2.id IN ({$accountIDs})
				ORDER BY name", [ get_current_user_id(), get_current_user_id(), Helper::getBlogId() ] )
				, ARRAY_A
			);
		}
		else
		{
			$accounts = [];
		}

		if ( ! empty( $nodeIDs ) )
		{
			$activeNodes = DB::DB()->get_results(
				DB::DB()->prepare( "
				SELECT 
					tb2.*, IFNULL(tb1.filter_type, 'no') AS filter_type, tb1.categories, (SELECT GROUP_CONCAT(`name`) FROM " . DB::WPtable( 'terms', TRUE ) . " WHERE FIND_IN_SET(term_id,tb1.categories) ) AS categories_name 
				FROM " . DB::table( 'account_nodes' ) . " tb2
				LEFT JOIN " . DB::table( 'account_node_status' ) . " tb1 ON tb2.id=tb1.node_id AND tb1.user_id=%d
				WHERE (tb2.user_id=%d OR tb2.is_public=1) AND tb2.blog_id=%d AND tb2.id IN ({$nodeIDs})
				ORDER BY (CASE node_type WHEN 'ownpage' THEN 1 WHEN 'group' THEN 2 WHEN 'page' THEN 3 END), name", [ get_current_user_id(), get_current_user_id(), Helper::getBlogId() ] )
				, ARRAY_A
			);
		}
		else
		{
			$activeNodes = [];
		}

		$activeNodes = array_merge( $accounts, $activeNodes );

		if ( empty( $activeNodes ) )
		{
			Helper::response( FALSE, esc_html__( 'No account or coummunications are selected!', 'fs-poster' ) );
		}

		DB::DB()->query(
			DB::DB()->prepare(
				'DELETE FROM ' . DB::table( 'feeds' ) . ' WHERE post_id = %d AND is_sended = 0 AND blog_id = %d',
				[ $post_id, Helper::getBlogId() ]
			)
		);

		$new_feeds = [];

		foreach ( $activeNodes as $node )
		{
			$new_feeds[] = '(' . intval( $post_id ) . ', ' . intval( $node['id'] ) . ', "' . esc_sql( $node['node_type'] ) . '", "' . esc_sql( $node['driver'] ) . '", "' . esc_sql( $info['send_time'] ) . '", 0, 0, "' . mb_substr( esc_sql( $_custom_messages[$node['driver']] ), 0, 2000 ) . '", 1, ' . Helper::getBlogId() . ', 0)';
		}

		$new_feeds_sql = 'INSERT INTO ' . DB::table( 'feeds' ) . ' (`post_id`, `node_id`, `node_type`, `driver`, `send_time`, `interval`, `driver_post_id`, `custom_post_message`, `share_on_background`, `blog_id`, `is_seen`) VALUES ' . implode( ',', $new_feeds ) . '';
		$inserted_rows = DB::DB()->query( $new_feeds_sql );

		if ( ! $inserted_rows )
		{
			Helper::response( TRUE );
		}

		Helper::response( TRUE );
	}

	public function schedule_posts ()
	{
		$plan_date  = Request::post( 'plan_date', '', 'string' );
		$post_ids_p = Request::post( 'post_ids', [], 'array' );
		$interval   = Request::post( 'interval', '0', 'num' );

		if ( ! ( $interval > 0 ) )
		{
			Helper::response( FALSE, esc_html__( 'Validation error', 'fs-poster' ) );
		}

		if ( empty( $plan_date ) )
		{
			Helper::response( FALSE, 'Schedule date is empty!' );
		}
		else
		{
			if ( Date::epoch( $plan_date, '-1 year' ) > Date::epoch() )
			{
				Helper::response( FALSE, 'Plan date or time is not valid!' );
			}
			else
			{
				if ( Date::epoch( $plan_date, '+5 minutes' ) < Date::epoch() )
				{
					Helper::response( FALSE, 'Plan date or time is not valid!<br>Please select Schedule date/time according to your server time. <br>Your server time is: ' . Date::dateTime() );
				}
			}
		}

		$plan_date = Date::dateTimeSQL( $plan_date );

		$post_ids = [];

		foreach ( $post_ids_p as $postId )
		{
			if ( is_numeric( $postId ) && $postId > 0 )
			{
				$post_ids[] = (int) $postId;
			}
		}

		if ( empty( $post_ids ) )
		{
			Helper::response( FALSE, 'Please select at least one post.' );
		}
		else
		{
			if ( count( $post_ids ) > 250 )
			{
				Helper::response( FALSE, 'Too many post selected! You can select maximum 250 posts!' );
			}
		}

		$custom_messages = Request::post( 'custom_messages', '', 'string' );
		$accounts_list   = Request::post( 'accounts_list', '', 'string' );

		$_custom_messages = [];
		if ( ! empty( $custom_messages ) )
		{
			$custom_messages = json_decode( $custom_messages, TRUE );
			$custom_messages = is_array( $custom_messages ) ? $custom_messages : [];

			foreach ( $custom_messages as $socialNetwork => $message1 )
			{
				if ( in_array( $socialNetwork, [ 'fb', 'instagram', 'instagram_h', 'linkedin', 'twitter', 'pinterest', 'vk', 'ok', 'tumblr', 'reddit', 'google_b', 'telegram', 'medium', 'wordpress' ] ) && is_string( $message1 ) )
				{
					$_custom_messages[$socialNetwork] = $message1;
				}
			}
		}
		$_custom_messages = empty( $_custom_messages ) ? NULL : json_encode( $_custom_messages );

		$_accounts_list = [];
		if ( ! empty( $accounts_list ) )
		{
			$accounts_list = json_decode( $accounts_list, TRUE );
			$accounts_list = is_array( $accounts_list ) ? $accounts_list : [];

			foreach ( $accounts_list as $socialAccount )
			{
				if ( is_string( $socialAccount ) )
				{
					$socialAccount = explode( ':', $socialAccount );
					if ( ! ( count( $socialAccount ) == 2 && is_numeric( $socialAccount[1] ) ) )
					{
						continue;
					}

					$_accounts_list[] = ( $socialAccount[0] == 'account' ? 'account' : 'node' ) . ':' . $socialAccount[1];
				}
			}
		}
		$_accounts_list = empty( $_accounts_list ) ? NULL : implode( ',', $_accounts_list );

		$postsCount = count( $post_ids );

		if ( $postsCount == 1 )
		{
			$onePostId  = reset( $post_ids );
			$onePostInf = get_post( $onePostId, ARRAY_A );

			$title = 'Scheduled post: "' . Helper::cutText( ! empty( $onePostInf['post_title'] ) ? $onePostInf['post_title'] : $onePostInf['post_content'] ) . '"';
		}
		else
		{
			$title = 'Schedule ( ' . $postsCount . ' posts )';
		}

		$post_ids = implode( ',', $post_ids );

		$start_date = Date::dateSQL( $plan_date );
		$end_date   = Date::dateSQL( $plan_date, '+' . ( ( $postsCount - 1 ) * $interval ) . ' minutes' );
		$share_time = Date::timeSQL( $plan_date );

		$post_type_filter = '';
		$category_filter  = '';
		$post_sort        = $postsCount == 1 ? 'new_first' : Request::post( 'post_sort', 'old_first', 'string', [ 'old_first', 'random', 'new_first' ] );
		$post_date_filter = 'all';

		DB::DB()->insert( DB::table( 'schedules' ), [
			'blog_id'     => Helper::getBlogId(),
			'title'       => $title,
			'start_date'  => $start_date,
			'end_date'    => $end_date,
			'interval'    => $interval,
			'status'      => 'active',
			'insert_date' => Date::dateTimeSQL(),
			'user_id'     => get_current_user_id(),
			'share_time'  => $share_time,

			'post_type_filter' => $post_type_filter,
			'category_filter'  => $category_filter,
			'post_sort'        => $post_sort,
			'post_date_filter' => $post_date_filter,

			'post_ids'          => $post_ids,
			'save_post_ids'     => $post_ids,
			'next_execute_time' => $plan_date,

			'custom_post_message' => $_custom_messages,
			'share_on_accounts'   => $_accounts_list
		] );

		Helper::response( TRUE );
	}

	public function delete_schedule ()
	{
		$id = Request::post( 'id', 0, 'num' );
		if ( $id <= 0 )
		{
			Helper::response( FALSE );
		}

		$checkSchedule = DB::fetch( 'schedules', $id );
		if ( ! $checkSchedule )
		{
			Helper::response( FALSE, esc_html__( 'Schedule not found!', 'fs-poster' ) );
		}
		else
		{
			if ( $checkSchedule['user_id'] != get_current_user_id() )
			{
				Helper::response( FALSE, esc_html__( 'You do not have a permission to delete this schedule!', 'fs-poster' ) );
			}
		}

		DB::DB()->delete( DB::table( 'schedules' ), [ 'id' => $id ] );

		Helper::response( TRUE );
	}

	public function delete_schedules ()
	{
		$ids = Request::post( 'ids', [], 'array' );
		if ( count( $ids ) == 0 )
		{
			Helper::response( FALSE, 'No schedule selected!' );
		}

		foreach ( $ids as $id )
		{
			if ( is_numeric( $id ) && $id > 0 )
			{
				$checkSchedule = DB::fetch( 'schedules', $id );
				if ( ! $checkSchedule )
				{
					Helper::response( FALSE, esc_html__( 'Schedule not found!', 'fs-poster' ) );
				}

				else
				{
					if ( $checkSchedule['user_id'] != get_current_user_id() )
					{
						Helper::response( FALSE, esc_html__( 'You do not have a permission to delete this schedule!', 'fs-poster' ) );
					}
				}

				DB::DB()->delete( DB::table( 'schedules' ), [ 'id' => $id ] );
			}
		}

		Helper::response( TRUE );
	}

	public function schedule_change_status ()
	{
		$id = Request::post( 'id', 0, 'num' );

		if ( $id <= 0 )
		{
			Helper::response( FALSE );
		}

		$checkSchedule = DB::fetch( 'schedules', $id );
		if ( ! $checkSchedule )
		{
			Helper::response( FALSE, esc_html__( 'Schedule not found!', 'fs-poster' ) );
		}
		else
		{
			if ( $checkSchedule['user_id'] != get_current_user_id() )
			{
				Helper::response( FALSE, esc_html__( 'You do not have a permission to Pause/Play this schedule!', 'fs-poster' ) );
			}
		}

		if ( $checkSchedule['status'] != 'paused' && $checkSchedule['status'] != 'active' )
		{
			Helper::response( FALSE, esc_html__( 'This schedule has finished!', 'fs-poster' ) );
		}

		$newStatus = $checkSchedule['status'] == 'active' ? 'paused' : 'active';

		$updateArr = [ 'status' => $newStatus ];

		if ( $newStatus != 'paused' )
		{
			$locTime         = Date::epoch();
			$scheduleStarted = Date::epoch( $checkSchedule['start_date'] . ' ' . $checkSchedule['share_time'] );

			$dif = $locTime - $scheduleStarted;

			$interval = $checkSchedule['interval'] * 60;

			$nextExecTime = ( $dif % $interval ) === 0 ? $locTime : $locTime + $interval - ( $dif % $interval );

			$updateArr['next_execute_time'] = Date::dateTimeSQL( $nextExecTime );
		}

		DB::DB()->update( DB::table( 'schedules' ), $updateArr, [ 'id' => $id ] );

		Helper::response( TRUE );
	}

	public function schedule_get_calendar ()
	{
		$month = (int) Request::post( 'month', Date::month(), 'int', [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] );
		$year  = (int) Request::post( 'year', Date::year(), 'int' );

		if ( $year > Date::year() + 4 || $year < Date::year() - 4 )
		{
			Helper::response( FALSE, 'Loooooooooooooooolll :)' );
		}

		$firstDate = Date::datee( "{$year}-{$month}-01" );
		$lastDate  = Date::lastDateOfMonth( $year, $month );
		$myId      = (int) get_current_user_id();

		if ( Date::epoch( $firstDate ) < Date::epoch( Date::dateSQL() ) )
		{
			$firstDate = Date::datee();
		}

		$getPlannedDays = DB::DB()->get_results( "SELECT * FROM `" . DB::table( 'schedules' ) . "` WHERE `start_date`<='$lastDate' AND `status`='active' AND user_id='$myId' AND `blog_id`='" . Helper::getBlogId() . "'", ARRAY_A );

		$days = [];

		foreach ( $getPlannedDays as $planInf )
		{
			$scheduleId = (int) $planInf['id'];
			$planStart  = Date::epoch( $planInf['start_date'] );
			$planEnd    = Date::epoch( $lastDate );
			$interval   = (int) $planInf['interval'] > 0 ? (int) $planInf['interval'] : 1;

			$postCount = empty( $planInf['post_ids'] ) ? -1 : count( explode( ',', $planInf['post_ids'] ) );

			if ( $planStart < Date::epoch( $firstDate ) )
			{
				while ( $planStart < Date::epoch( $firstDate ) )
				{
					$planStart += 60 * $interval;
				}
			}

			if ( $planInf['post_sort'] != 'random' && $planInf['post_sort'] != 'random2' )
			{
				$filterQuery = Helper::scheduleFilters( $planInf );
				$calcLimit   = 1 + (int) ( ( $planEnd - $planStart ) / 60 / $interval );

				$calcLimit = $calcLimit > 0 ? $calcLimit : 1;

				$getRandomPost = DB::DB()->get_results( "SELECT * FROM " . DB::WPtable( 'posts', TRUE ) . " tb1 WHERE post_status='publish' {$filterQuery} LIMIT " . $calcLimit, ARRAY_A );
			}

			if ( empty( $planInf['share_time'] ) )
			{
				$getLastShareTime      = DB::DB()->get_row( "SELECT MAX(send_time) AS max_share_time FROM " . DB::table( 'feeds' ) . " WHERE schedule_id='$scheduleId'", ARRAY_A );
				$planInf['share_time'] = Date::timeSQL( $getLastShareTime['max_share_time'] );
			}

			$cursorDayTimestamp = Date::epoch( Date::dateSQL( $planStart ) . ' ' . $planInf['share_time'] );
			$planEnd            = Date::epoch( Date::dateSQL( $planEnd ) . ' 23:59:59' );

			while ( $cursorDayTimestamp <= $planEnd )
			{
				$currentDate = Date::dateSQL( $cursorDayTimestamp );
				$time        = Date::time( $cursorDayTimestamp );

				$cursorDayTimestamp += 60 * $interval;

				if ( Date::epoch( $currentDate . ' ' . $time ) < Date::epoch() )
				{
					continue;
				}

				if ( $postCount === 0 )
				{
					break;
				}

				if ( $planInf['post_sort'] == 'random' || $planInf['post_sort'] == 'random2' )
				{
					$postDetails = 'Will select randomly';
					$postId      = NULL;
				}
				else
				{
					$thisPostInf = current( $getRandomPost );
					next( $getRandomPost );

					if ( $thisPostInf )
					{
						$postDetails = '<b>Post ID:</b> ' . $thisPostInf['ID'] . "<br><b>Title:</b> " . htmlspecialchars( Helper::cutText( $thisPostInf['post_title'] ) . '<br><br><i>Click to get the post page</i>' );
						$postId      = $thisPostInf['ID'];
					}
					else
					{
						$postDetails = 'Post not found with your filters for this date!';
						$postId      = NULL;
					}
				}

				$days[] = [
					'id'        => $planInf['id'],
					'title'     => htmlspecialchars( Helper::cutText( $planInf['title'], 22 ) ),
					'post_data' => $postDetails,
					'post_id'   => $postId,
					'date'      => $currentDate,
					'time'      => $time
				];

				$postCount--;
			}

		}

		Helper::response( TRUE, [ 'days' => $days ] );
	}

	public function calcualte_post_count ()
	{
		$post_type_filter                = Request::post( 'post_type_filter', '', 'string' );
		$dont_post_out_of_stock_products = Request::post( 'dont_post_out_of_stock_products', '0', 'string', [ '1', '0' ] );
		$category_filter                 = Request::post( 'category_filter', '', 'int' );
		$post_date_filter                = Request::post( 'post_date_filter', '', 'string' );
		$post_ids                        = Request::post( 'post_ids', '', 'string' );

		$scheduleInf = [
			'id'                              => 0,
			'post_type_filter'                => $post_type_filter,
			'dont_post_out_of_stock_products' => $dont_post_out_of_stock_products,
			'category_filter'                 => $category_filter,
			'post_date_filter'                => $post_date_filter,
			'post_ids'                        => $post_ids
		];

		$filterQuery = Helper::scheduleFilters( $scheduleInf );

		$getRandomPost = DB::DB()->get_row( "SELECT count(0) AS `post_count` FROM `" . DB::WPtable( 'posts', TRUE ) . "` tb1 WHERE (`post_status`='publish' OR `post_type`='attachment') {$filterQuery}", ARRAY_A );
		$postsCount    = (int) $getRandomPost['post_count'];

		Helper::response( TRUE, [
			'count' => $postsCount
		] );
	}

}