<?php
/**
 * Created by PhpStorm.
 * User: Hani
 * Date: 3/25/2019
 * Time: 1:35 PM
 */

class woo2app_Reviews {

	public function create_reviews()
	{
		if(!empty($_POST["in"]))
		{
			ob_start();
			global $wpdb;

			$in = $_POST['in'];
			$slashless = stripcslashes($in);
			$url_json = urldecode($slashless);
			$json = (array)  json_decode($url_json);
			$comment_author       = ! isset( $json['comment_author'] )       ? '' : $json['comment_author'];
			$comment_author_email = ! isset( $json['comment_author_email'] ) ? '' : $json['comment_author_email'];
			$comment_author_url   = ! isset( $json['comment_author_url'] )   ? '' : $json['comment_author_url'];
			$comment_author_IP    = ! isset( $json['comment_author_IP'] )    ? '' : $json['comment_author_IP'];
			$comment_date     = ! isset( $json['comment_date'] )     ? current_time( 'mysql' )            : $json['comment_date'];
			$comment_date_gmt = ! isset( $json['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $json['comment_date_gmt'];
			$comment_post_id  = ! isset( $json['comment_post_id'] )  ? '' : $json['comment_post_id'];
			$comment_content  = ! isset( $json['comment_content'] )  ? '' : $json['comment_content'];
			$comment_karma    = ! isset( $json['comment_karma'] )    ? 0  : $json['comment_karma'];
			$comment_approved = ! isset( $json['comment_approved'] ) ? 0  : $json['comment_approved'];
			$comment_agent    = ! isset( $json['comment_agent'] )    ? '' : $json['comment_agent'];
			$comment_type     = ! isset( $json['comment_type'] )     ? '' : $json['comment_type'];
			$comment_parent   = ! isset( $json['comment_parent'] )   ? 0  : $json['comment_parent'];
			$commentdata   = ! isset( $json['comment_meta'] )   ? array()  : $json['comment_meta'];
			$user_id  = ! isset( $json['user_id'] ) ? 0 : $json['user_id'];
			$compacted = compact( 'comment_post_id', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' );
			if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
				return false;
			}else{
				ob_clean();
				return array("result"=>1);
			}
			$id = (int) $wpdb->insert_id;
			if ( $comment_approved == 1 ) {
				wp_update_comment_count( $comment_post_id );
			}
			$comment = get_comment( $id );
			// If metadata is provided, store it.
			var_dump($commentdata);
			if ( isset( $commentdata ) && !empty( $commentdata ) ) {
				foreach ( $commentdata as $meta_key => $meta_value ) {
					add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
				}
			}
		}
		else
		{
			return "not found page";
		}
	}

	function get_reviews()
	{
		if(!empty($_GET["in"]))
		{
			ob_start();
			$in = $_GET['in'];
			$slashless = stripcslashes($in);
			$url_json = urldecode($slashless);
			$json = (array)  json_decode($url_json);
			$count = (int) $json["Count"];
			$last_count = (int) $json["LastCount"];
			$post = (int) $json["Post_id"];
			$num= $count + $last_count;
			$args = array(
				'number' =>$num,
				'post_id' => $post,
			);
			$records = get_comments($args );
			$array_p = array();
			$i=1;
			foreach($records as $record)
			{
				if ($record->comment_approved == 1) {
					if($i>$last_count){
						$array_p['comments'][] = array(
							'Comment_id'=>$record->comment_ID,
							'Comment_author'=>$record->comment_author,
							'Comment_date'=>$record->comment_date,
							'Comment_content'=>$record->comment_content,
							'Comment_parent'=>$record->comment_parent,
							'rating' => get_comment_meta($record->comment_ID, 'rating', true)
						);
					}
					$i++;
				}
			}
			//ob_clean();
			return $array_p;
		}
		else
		{
			return "not found page";
		}
	}
}