<?php
namespace Jet_Engine\Modules\Profile_Builder;

class Restrictions {

	/**
	 * A reference to an instance of this class.
	 *
	 * @since  1.0.0
	 * @access private
	 * @var    object
	 */
	private static $instance = null;

	private $_latest_message = false;

	/**
	 * Normalize post type slug used by restrictions.
	 *
	 * @param mixed $post_type Post type to validate.
	 *
	 * @return string|false
	 */
	private function normalize_post_type( $post_type = null ) {

		if ( ! is_scalar( $post_type ) ) {
			return false;
		}

		$post_type = sanitize_key( (string) $post_type );

		if ( empty( $post_type ) || ! post_type_exists( $post_type ) ) {
			return false;
		}

		return $post_type;

	}

	public function current_user_can_submit_posts( $post_type = false ) {
		
		$res = true;
		$restrictions = Module::instance()->settings->get( 'posts_restrictions' );
		$post_type    = $this->normalize_post_type( $post_type );

		if ( empty( $restrictions ) ){
			return $res;
		}

		$found_restriction = false;

		if ( ! is_user_logged_in() ) {
			$roles = array( 'jet-engine-guest' );
		} else {
			$user  = wp_get_current_user();
			$roles = array_values( $user->roles );
		}

		$found_restrictions = array();
		$find_by_post_type  = false;

		foreach ( $restrictions as $restriction ) {

			if ( empty( $restriction['role'] ) ) {
				continue;
			}

			$intersect = array_intersect( $roles, $restriction['role'] );

			if ( ! empty( $intersect ) ) {
				$_post_type = ! empty( $restriction['post_type'] ) ? $restriction['post_type'] : 'all';

				if ( is_array( $_post_type ) ) {
					foreach ( $_post_type as $_p_type ) {
						$_p_type = $this->normalize_post_type( $_p_type );

						if ( $_p_type ) {
							$found_restrictions[ $_p_type ] = $restriction;
						}
					}
				} else {
					if ( 'all' === $_post_type ) {
						$found_restrictions['all'] = $restriction;
					} else {
						$_post_type = $this->normalize_post_type( $_post_type );

						if ( $_post_type ) {
							$found_restrictions[ $_post_type ] = $restriction;
						}
					}
				}
			}
		}

		if ( isset( $found_restrictions[ $post_type ] ) ) {
			$found_restriction = $found_restrictions[ $post_type ];
			$find_by_post_type = $post_type;
		} else if ( isset( $found_restrictions['all'] ) ) {
			$found_restriction = $found_restrictions['all'];
		}

		$limit = ! empty( $found_restriction['limit'] ) ? absint( $found_restriction['limit'] ) : 0;

		if ( ! $limit ) {
			return $res;
		}

		$user_id    = isset( $user ) ? $user->ID : 0;
		$user_posts = $this->get_user_posts( $user_id, $find_by_post_type );

		if ( $user_posts >= $limit ) {
			$message = ! empty( $found_restriction['error_message'] ) ? $found_restriction['error_message'] : __( 'Posts limit reached', 'jet-engine' );
			$this->_latest_message = $message;
			$res = false;
		}

		return $res;

	}

	public function get_latest_message() {
		return $this->_latest_message;
	}

	/**
	 * Get user posts count
	 */
	public function get_user_posts( $user_id = null, $post_type = null ) {

		global $wpdb;

		$posts     = $wpdb->posts;
		$user_id   = absint( $user_id );
		$post_type = $this->normalize_post_type( $post_type );

		if ( $post_type ) {
			$query = $wpdb->prepare(
				"SELECT COUNT(*) FROM $posts WHERE post_author = %d AND post_type = %s AND post_status IN ( 'draft', 'publish', 'trash' );",
				$user_id,
				$post_type
			);
		} else {
			$query = $wpdb->prepare(
				"SELECT COUNT(*) FROM $posts WHERE post_author = %d AND post_status IN ( 'draft', 'publish', 'trash' );",
				$user_id
			);
		}

		$posts_num = $wpdb->get_var( $query );

		return absint( $posts_num );

	}

	/**
	 * Returns the instance.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return object
	 */
	public static function instance() {
		// If the single instance hasn't been set, set it now.
		if ( null == self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}

}
