Home / WP Read/UnRead status checking code helper

WP Read/UnRead status checking code helper

<?php


// Start of class build by PrismICT.com
class isRead{
	function _construct(){

	}

	private $postType   = 'status_check';
	private $statusName = '_status';
	private $defStatus  = 'new';

	private function IsExists($data = []){
		// userID, postID, status
		if($data){
			extract($data);
		}
		$args = array(
			'posts_per_page' => 1,
			'title'          => $postID,
			'post_type'      => $this->postType,
			'post_author'    => $userID,
			'post_status'    => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit')

		);
		$CheckExists = get_posts( $args );
		// echo '<PRE>'; print_r($CheckExists); echo '</PRE>';

		if($CheckExists){
			if(count($CheckExists)){
				$InfoID = $CheckExists[0]->ID;
			}
		}
		return $InfoID;
	}

	public function setStatus($data = []){
		// userID, postID, status
		if($data){
			extract($data);
		}

		if(!$error){
			// Check if exist the post with author
			$InfoID = $this->IsExists($data);

			// Insert new item if not exists
			if(!$InfoID){
				$InfoID = wp_insert_post([
					'post_title'   => $postID,
					'post_author'  => $userID,
					'post_type'    => $this->postType,
				]);
			};

			// Set the status as new status
			if($InfoID){
				update_post_meta($InfoID, $this->statusName, $status);
			}
			
			// return new status
			return get_post_meta($InfoID, $this->statusName, true);
		}
	}

	public function Status($data = []){
		// user, postID
		if($data){
			extract($data);
		}

		if(!$error){
			$InfoID = $this->IsExists($data);
		}

		if($InfoID){
			$NewStatus = get_post_meta($InfoID, $this->statusName, true);
		}

		$NewStatus = ($NewStatus)?$NewStatus:$this->defStatus;
		return $NewStatus;
	}
}
// Start of class build by PrismICT.com


// Call the Class
$isRead = new isRead();

// Set status for new entry
$isRead->setStatus([
		'userID' => get_current_user_id(),
		'postID' => '75',
		'status' => 'Read',
	]);

// Get Status
$setStatus = $isRead->Status([
		'userID' => get_current_user_id(),
		'postID' => '75'
	]);

?>

all rights reserved