<?php
  class GetAppsReviews_Functions extends WP_Review_Pro_Admin_Hooks {
	
	//============================================================
	//functions for scraping reviews from the get-apps page
	//--------------------------
	//
	/**
	 * Called from admin class-wp-review-slider-pro-admin_hooks.php > wprp_getapps_getrevs_ajax_go
	 * @access  public
	 * @since   11.3.7
	 * @return  void
	 
	 */
	//for calling dataforseo api to submit new task or poll task
	public function wprp_getapps_getrevs_page_dfs_submitnewtask($sitetype,$listedurl,$pagenum,$perpage,$savedpageid,$sortoption,$fid,$blockstoinsert,$dfsflag){
		$result['ack']='success';
		$result['ackmsg']='';
		$result['reviews']='';
		$result['total']='';
		$result['avg']='';
		//submit new task
		//call dataforseo api to submit new task, get the task id and save to database table for this form
			
		if (empty($listedurl)) {
			if (!defined('DOING_CRON') || !DOING_CRON) {
				wp_send_json_error(array('message' => 'Search terms are required'));
			}
			return array('ack' => 'error', 'ackmsg' => 'Search terms are required');
		}
		
		// Prepare request to external crawler server
		$ip_server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : urlencode(get_site_url());
		$siteurl = get_site_url();
		$user_id = get_current_user_id();

		// Since this is a new task, clear any previous dfs_task_status immediately
		if (!empty($fid)) {
			global $wpdb;
			$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
		$wpdb->update(
			$table_name,
			array('dfs_task_status' => '', 'dfs_task_id' => '', 'dfs_task_timestamp' => time()),
			array('id' => (int) $fid),
			array('%s','%s','%d'),
			array('%d')
		);
		}

		//make sure we have a $listedurl if not return error
		if(empty($listedurl)){
			return array('ack'=>'error','ackmsg'=>'Search terms or placeid are required');
		}
		
		$crawler_url = 'https://crawl.ljapps.com/crawlrevs?' . http_build_query(array(
			'rip' => $ip_server,
			'surl' => $siteurl,
			'stype' => 'dataforseo',
			'scrapequery' => $listedurl,
			'limit' =>  $blockstoinsert,
			'blocks' => $blockstoinsert,
			'pagenum' => 1,
			'locationtype' => '',
			'nhful' => $sortoption,
			'tempbusinessname' => '',
			'user_id' => $user_id,
			'sfp' => 'pro',
			'nobot' => 1
		));
		
		// Debug: Log the crawler URL (removed for focus)
		//echo $crawler_url;
		//die();
		
		// Make request to external server
		$response = wp_remote_get($crawler_url, array(
			'timeout' => 60,
			'sslverify' => false,
			'headers' => array(
				'Content-Type' => 'application/json',
				'Accept' => 'application/json'
			)
		));

		if (is_wp_error($response)) {
			$error_msg = 'Failed to contact crawler server: ' . $response->get_error_message();
			if (!defined('DOING_CRON') || !DOING_CRON) {
				wp_send_json_error(array('message' => $error_msg));
			}
			return array('ack' => 'error', 'ackmsg' => $error_msg);
		}
		
		$body = wp_remote_retrieve_body($response);
		$results = json_decode($body, true);

		$results['here1'] = "here1";
		$serverresponse = $response['body']; // testing response for block
		//testing only
		//$serverresponse = "Please wait while your request is being verified";
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
			
			//this site is greylisted by imunify360 on cloudways, call backup digital ocean server
			$crawler_url_ocean = 'https://ocean.ljapps.com/crawlrevs.php?' . http_build_query(array(
				'rip' => $ip_server,
				'surl' => $siteurl,
				'stype' => 'dataforseo',
				'scrapequery' => $listedurl,
				'limit' =>  $blockstoinsert,
				'blocks' => $blockstoinsert,
				'pagenum' => 1,
				'locationtype' => '',
				'nhful' => $sortoption,
				'tempbusinessname' => '',
				'user_id' => $user_id,
				'sfp' => 'pro',
				'nobot' => 1
			));
			$response = wp_remote_get($crawler_url_ocean, array(
				'timeout' => 45,
				'sslverify' => false,
				'headers' => array(
					'Content-Type' => 'application/json',
					'Accept' => 'application/json'
				)
			));
			$body = wp_remote_retrieve_body($response);
			$results = json_decode($body, true);
		 }
		 //=========================
		
		if ($results && isset($results['ack']) && $results['ack'] === 'crawling') {
			
			// Check if task_id is in the main results or nested in result array
			$task_id = null;
			if (isset($results['task_id'])) {
				$task_id = $results['task_id'];
			} elseif (isset($results['result']['task_id'])) {
				$task_id = $results['result']['task_id'];
			}
			
			// Update dfs task info in DB for this form
			if (!empty($fid)) {
				global $wpdb;
				$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
				$status_to_set = isset($results['ack']) ? $results['ack'] : 'crawling';
				$update_data = array(
					'dfs_task_status' => $status_to_set,
					'dfs_task_timestamp' => time(),
				);
				$update_format = array('%s','%d');
				if (!empty($task_id)) {
					$update_data['dfs_task_id'] = $task_id;
					$update_format[] = '%s';
				}
				$wpdb->update(
					$table_name,
					$update_data,
					array('id' => (int) $fid),
					$update_format,
					array('%d')
				);
			}
			
			$response_data = array(
				'task_id' => $task_id,
				'daily_remaining' => isset($results['daily_remaining']) ? $results['daily_remaining'] : 100
			);
			if (!defined('DOING_CRON') || !DOING_CRON) {
				wp_send_json_success($response_data);
			}
			return array_merge(array('ack' => 'crawling'), $response_data);

		} elseif ($results && isset($results['ack']) && $results['ack'] === 'success') {
			// If we get immediate success, update status and task id if present
			$task_id = null;
			if (isset($results['task_id'])) {
				$task_id = $results['task_id'];
			} elseif (isset($results['result']['task_id'])) {
				$task_id = $results['result']['task_id'];
			}
			if (!empty($fid)) {
				global $wpdb;
				$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
				$update_data = array(
					'dfs_task_status' => 'success',
					'dfs_task_timestamp' => time(),
				);
				$update_format = array('%s','%d');
				if (!empty($task_id)) {
					$update_data['dfs_task_id'] = $task_id;
					$update_format[] = '%s';
				}
				$wpdb->update(
					$table_name,
					$update_data,
					array('id' => (int) $fid),
					$update_format,
					array('%d')
				);
			}
			$response_data = array(
				'task_id' => $task_id,
				'daily_remaining' => isset($results['daily_remaining']) ? $results['daily_remaining'] : 100
			);
			if (!defined('DOING_CRON') || !DOING_CRON) {
				wp_send_json_success($response_data);
			}
			return array_merge(array('ack' => 'success'), $response_data);
		} else {
			// Check for error message in different possible fields
			$error_msg = 'Unknown error occurred';
			if (isset($results['ackmessage'])) {
				$error_msg = $results['ackmessage'];
			} elseif (isset($results['ackmsg'])) {
				$error_msg = $results['ackmsg'];
			} elseif (isset($results['message'])) {
				$error_msg = $results['message'];
			}
			
			if (!defined('DOING_CRON') || !DOING_CRON) {
				wp_send_json_error(array('message' => $error_msg, 'results' => $results));
			}
			return array('ack' => 'error', 'ackmsg' => $error_msg);
		}

		return $results;
	}
	 	 
	// Poll DataForSEO task and, when complete, return reviews and save status
	public function wprp_getapps_getrevs_page_dfs_checktask($sitetype,$listedurl,$pagenum,$perpage,$savedpageid,$sortoption,$fid,$blockstoinsert,$dfsflag,$taskid=''){
		$result = array();
		$result['ack']='success';
		$result['ackmsg']='';
		$result['reviews']=array();
		$result['total']='';
		$result['avg']='';

		if(empty($taskid)){
			// Attempt to load from DB if not provided
			if(!empty($fid)){
				global $wpdb;
				$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
				$row = $wpdb->get_row( $wpdb->prepare("SELECT dfs_task_id FROM {$table_name} WHERE id=%d", (int)$fid) );
				if($row && isset($row->dfs_task_id)){
					$taskid = $row->dfs_task_id;
				}
			}
		}
		if(empty($taskid)){
			return array('ack'=>'error','ackmsg'=>'Missing task id');
		}

		$ip_server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : urlencode(get_site_url());
		$siteurl = urlencode(get_site_url());
		$user_id = get_current_user_id();

		$crawler_url = 'https://crawl.ljapps.com/crawlrevs?' . http_build_query(array(
			'rip' => $ip_server,
			'surl' => $siteurl,
			'user_id' => $user_id,
			'stype' => 'dataforseo_poll',
			'task_id' => $taskid,
			'sfp' => 'pro',
			'nobot' => 1
		));

		//return array('ack'=>'error','ackmsg'=>'crawler url: ' . $crawler_url);
		//echo $crawler_url;
		//die();

		$response = wp_remote_get($crawler_url, array(
			'timeout' => 45,
			'sslverify' => false,
			'headers' => array(
				'Content-Type' => 'application/json',
				'Accept' => 'application/json'
			)
		));

		if (is_wp_error($response)) {
			return array('ack'=>'error','ackmsg'=>'Failed to contact crawler server: ' . $response->get_error_message());
		}
		$body = wp_remote_retrieve_body($response);
		$results = json_decode($body, true);

		$serverresponse = $response['body']; // testing response for block
		//testing only
		//$serverresponse = "Please wait while your request is being verified";
		
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
			//this site is greylisted by imunify360 on cloudways, call backup digital ocean server
			$crawler_url_ocean = 'https://ocean.ljapps.com/crawlrevs.php?' . http_build_query(array(
				'rip' => $ip_server,
				'surl' => $siteurl,
				'user_id' => $user_id,
				'stype' => 'dataforseo_poll',
				'task_id' => $taskid,
				'sfp' => 'pro',
				'nobot' => 1
			));
			$response = wp_remote_get($crawler_url_ocean, array(
				'timeout' => 45,
				'sslverify' => false,
				'headers' => array(
					'Content-Type' => 'application/json',
					'Accept' => 'application/json'
				)
			));
			$body = wp_remote_retrieve_body($response);
			$results = json_decode($body, true);
		 }
		 //=========================



		if(!$results || !isset($results['ack'])){
			return array('ack'=>'error','ackmsg'=>'Invalid response from crawler');
		}

		// Update status in DB
		if (!empty($fid)) {
			global $wpdb;
			$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
			$status_to_set = isset($results['ack']) ? $results['ack'] : '';
			//if ackmsg contains Task Handed or Task In Queue then set status to processing
			if(isset($results['ackmsg']) && ($results['ackmsg'] != '') && (strpos($results['ackmsg'], 'Task Handed') !== false || strpos($results['ackmsg'], 'Task In Queue') !== false)){
				$status_to_set = 'processing';
			}
			
			$wpdb->update(
				$table_name,
				array('dfs_task_status' => $status_to_set, 'dfs_task_id' => $taskid),
				array('id' => (int)$fid),
				array('%s','%s'),
				array('%d')
			);
		}

		if ($results['ack'] === 'crawling'){
			// not done yet
			$completed = false;
			if (isset($results['completed']) && $results['completed']) { $completed = true; }
			if (isset($results['result']['completed']) && $results['result']['completed']) { $completed = true; }
			if(!$completed){
				return array('ack'=>'processing','success'=>true,'data'=>array('completed'=>false,'status'=>isset($results['status']) ? $results['status'] : 'Processing...'));
			}
		}

		if ($results['ack'] === 'success' || (isset($results['completed']) && $results['completed']) || (isset($results['result']['completed']) && $results['result']['completed'])){
			// Extract business info and reviews
			$business_info = array();
			if (isset($results['business_info'])){ $business_info = $results['business_info']; }
			if (isset($results['result']['business_info'])){ $business_info = $results['result']['business_info']; }
			$reviews = array();
			if (isset($results['reviews']) && is_array($results['reviews'])){ $reviews = $results['reviews']; }
			if (isset($results['result']['reviews']) && is_array($results['result']['reviews'])){ $reviews = $results['result']['reviews']; }
			
			// Compute total and avg from available fields
			$place_total_reviews = '';
			if (isset($business_info['totalreviews'])) {
				$place_total_reviews = $business_info['totalreviews'];
			} else if (isset($results['total_reviews'])) {
				$place_total_reviews = $results['total_reviews'];
			} else if (isset($results['result']['total_reviews'])) {
				$place_total_reviews = $results['result']['total_reviews'];
			} else if (!empty($reviews)) {
				$place_total_reviews = count($reviews);
			}
			$place_avg_rating = '';
			if (isset($business_info['rating'])) {
				$place_avg_rating = $business_info['rating'];
			} else if (isset($results['avg'])) {
				$place_avg_rating = $results['avg'];
			} else if (isset($results['result']['avg'])) {
				$place_avg_rating = $results['result']['avg'];
			}

			// Convert to expected format used by existing insert flow
			$reviewsarray = array();
			foreach ($reviews as $review) {
				$reviewsarray[] = array(
					'reviewer_name' => isset($review['reviewer_name']) ? $review['reviewer_name'] : 'Google User',
					'reviewer_id' => isset($review['reviewer_id']) ? $review['reviewer_id'] : '',
					'reviewer_email' => '',
					'userpic' => isset($review['userpic']) ? $review['userpic'] : '',
					'rating' => isset($review['rating']) ? $review['rating'] : '',
					'updated' => isset($review['created_time']) ? $review['created_time'] : '',
					'review_text' => isset($review['review_text']) ? $review['review_text'] : '',
					'review_title' => isset($review['review_title']) ? $review['review_title'] : '',
					'from_url_review' => isset($review['from_url']) ? $review['from_url'] : '',
					'language_code' => isset($review['language_code']) ? $review['language_code'] : '',
					'unique_id' => isset($review['unique_id']) ? $review['unique_id'] : '',
					'location' => isset($review['location']) ? $review['location'] : '',
					'recommendation_type' => '',
					'company_title' => '',
					'company_url' => '',
					'company_name' => '',
					'mediaurlsarrayjson' => isset($review['mediaurlsarrayjson']) ? $review['mediaurlsarrayjson'] : '',
					'owner_response' => isset($review['owner_response']) ? $review['owner_response'] : '',
					'from_url' => isset($business_info['googleurl']) ? $business_info['googleurl'] : '',
					'meta_data' => isset($review['meta_data']) ? $review['meta_data'] : ''
				);
			}

			// Mark task as completed in DB now that reviews are ready
			if (!empty($fid)) {
				global $wpdb;
				$table_name = $wpdb->prefix . 'wpfb_getapps_forms';
				$wpdb->update(
					$table_name,
					array('dfs_task_status' => 'completed', 'dfs_task_id' => $taskid, 'page_id' => $business_info['foundplaceid']),
					array('id' => (int)$fid),
					array('%s','%s','%s'),
					array('%d')
				);
			}

			return array(
				'ack' => 'success',
				'reviews' => $reviewsarray,
				'total' => $place_total_reviews,
				'avg' => $place_avg_rating,
				'business_info' => $business_info
			);
		}

		// Fallback
		return array('ack'=>'error','ackmsg'=> isset($results['ackmsg']) ? $results['ackmsg'] : 'Unknown error');
	}
	//for calling Google Places API
	public function wprp_getapps_getrevs_page_googleplacesapi($sitetype,$listedurl,$pagenum,$perpage,$savedpageid,$sortoption,$fid,$blockstoinsert,$nextpageurl,$langcode){
		$result['ack'] = '';
		$result['ackmsg'] = '';
		$reviewsarray = Array();
		
		//api key
		$google_api_key = get_option('wprevpro_googleplacesapikey_val');
		
		//sortoption, relevant, newest, both
		$temprsort='';
		$jumpnum=1;
		if($sortoption=='relevant'){
			$rsort='most_relevant';
		} else if($sortoption=='newest'){
			$rsort='newest';
		} else if($sortoption=='both'){
			$temprsort='both';
			$rsort='most_relevant';
		}


		//if rsort is both then we need to jump back here and run again with other sort option adding to reviews array.
		a:
		if($temprsort=='both' && $jumpnum == 2){
			$rsort='newest';
		}

		$google_places_url = add_query_arg(
			array(
				'placeid' => trim($savedpageid),
				'key'     => trim($google_api_key),
				'language' => trim($langcode),
				'reviews_sort'=> trim($rsort),
				'reviews_no_translations'=> "true"	
			),
			'https://maps.googleapis.com/maps/api/place/details/json'
		);
		
		//Sanitize the URL
		$url = esc_url_raw( $google_places_url );
		
		//echo $url;
		
		$data = wp_remote_get($url, array(
		  'timeout' => 20,
		  'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0'
		));

		if ( is_wp_error( $data ) ) 
		{
			$result['ack'] ='error';
			$result['ackmsg'] =__('Error gpa_0001: Something happened. Msg: ','wp-review-slider-pro').$data->get_error_message();
			//$result = json_encode($result);
			//echo $result;
			return $result;
			die();
		}
		$response = json_decode( $data['body'], true );
		
		//print_r($response);
		//die();
		//catch non OK status status
		if( !isset( $response['status'])  ||  $response['status']!='OK' ) {
			$result['ack'] ='error';
			$result['ackmsg'] =__('Error gpa_0002: Error from Google: ','wp-review-slider-pro').$response['status'].", Full Response: ".$data['body'];
			//$result = json_encode($result);
			//echo $result;
			return $result;
			die();
		}


		if( ! ( isset( $response['result']['reviews'] ) ||  empty( $response['result']['reviews'] ) ) ){
			$result['ack'] ='error';
			$result['ackmsg'] =__('Error gpa_0003: No Google Reviews Found. Please check your api key and place id. ','wp-review-slider-pro');
			//$result = json_encode($result);
			//echo $result;
			return $result;
			die();
		}
		
		
			//need totals and avg for this place
			$result['total']='';
			$result['avg']='';
			if(isset($response['result']['rating'])){
				$result['avg']=$response['result']['rating'];
			}
			if(isset($response['result']['user_ratings_total'])){
				$result['total']=$response['result']['user_ratings_total'];
			}
			//print_r($response['result']['reviews']);
			foreach( $response['result']['reviews'] as $item){
			
				if(!isset($item['language']) || $item['language']=="NULL"){
							$item['language']='';
				}
				$intreviewer_id = filter_var($item['author_url'], FILTER_SANITIZE_NUMBER_INT);
				
				$updatedtimestring = date( "Y-m-d H:i:s", $item['time'] );
		
				$reviewsarray[] = [
				 'reviewer_name' => $item['author_name'],
				 'reviewer_id' => $intreviewer_id,
				 'reviewer_email' => '',
				 'userpic' => $item['profile_photo_url'],
				 'rating' => $item['rating'],
				 'updated' => $updatedtimestring,
				 'review_text' => $item['text'],
				 'review_title' => '',
				 'from_url' => $response['result']['url'],
				 'from_url_review' => '',
				 'from_url_author' => $item['author_url'],
				 'language_code' => $item['language'],
				 'location' => '',
				 'recommendation_type' => '',
				 'company_title' =>  '',
				 'company_url' => '',
				 'company_name' => '',
				 'mediaurlsarrayjson' => '',
				 ];

			}

			//jump back up and run again if using both sort option
			if($temprsort=='both' && $jumpnum ==1){
				$jumpnum = 2;
				goto a;
			}
			
			//print_r($reviewsarray);
			//die();
			
			if($temprsort=='both'){
				//remove possible duplicates
				$reviewsarray = array_map("unserialize", array_unique(array_map("serialize", $reviewsarray)));
			}
			
			$result['reviews'] = $reviewsarray;
			
			
			
			//only calling once since this is google.
			$result['stoploop'] ='stop';
		
		return $result;
	}
	


	//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
	public function wprp_getapps_getrevs_page_google($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		
		//echo $savedpageid;
		//die();
		
			//Google crawl method to actually download reviews
			if($savedpageid!=''){
				$gplaceid = $savedpageid;
			} else {
				//error no place id.
				$result['ack'] = 'error';
				$result['ackmsg'] =esc_html__('Error 103: Please enter your search terms or place id above and click the Save & Test button.', 'wp-review-slider-pro');
			}
			$checkdetails = json_decode(get_option('wprev_google_crawl_check'),true);
			//print_r($checkdetails);
			//die();
			$tempkey = trim(sanitize_text_field($listedurl));
			//strip \' from url 
			$tempkey = str_replace("\'","",$tempkey);
			
			if($checkdetails[$tempkey]['idorquery'] == 'query' && $checkdetails[$tempkey]['enteredterms']!=''){
				$tempbusinessname=$checkdetails[$tempkey]['enteredterms'];
			} else {
				$tempbusinessname=$checkdetails[$tempkey]['businessname'];
			}

			if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
				$ip_server = $_SERVER['SERVER_ADDR'];
			} else {
				//get url of site.
				$ip_server = urlencode(get_site_url());
			}
			$locationtype = '';
			if($checkdetails[$tempkey]['placetype']=='hotel'){
				$locationtype = 'hotel';
			} else if($checkdetails[$tempkey]['placetype']=='product'){
				$locationtype = 'product';
				
			}
			//print_r($checkdetails);
			$siteurl = urlencode(get_site_url());
			
			$tempurlvalue = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&blocks='.$blockstoinsert.'&surl='.$siteurl.'&stype=google&sfp=pro&nobot=1&nobot=1&nhful='.$nhful.'&locationtype='.$locationtype.'&scrapequery='.urlencode($gplaceid).'&tempbusinessname='.urlencode($tempbusinessname);

			//echo $tempurlvalue;
			//die();
			
			$serverresponse='';

			//try remote_get if we dont' have serverresponse yet
			$args = array(
				'timeout'     => 55,
				'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				) 
			); 
			$response = wp_remote_get( $tempurlvalue, $args );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			} else {
				//must have been an error
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001ag: trouble contacting crawling server with remote_get. Make sure wp_remote_get is allowed on your server. Please try again or contact support.'.$response->get_error_message();
				$results = json_encode($results);
				echo $results;
				die();
			}

		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
		   //this site is greylisted by imunify360 on cloudways, call backup digital ocean server
		   $response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&blocks='.$blockstoinsert.'&surl='.$siteurl.'&stype=google&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype='.$locationtype.'&scrapequery='.urlencode($gplaceid).'&tempbusinessname='.urlencode($tempbusinessname), array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			}
		}
		//=========================
			
			$serverresponsearray = json_decode($serverresponse, true);

			if($serverresponse=='' || !is_array($serverresponsearray)){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch limit error
			if($serverresponsearray['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002: '.$serverresponsearray['result']['ackmessage']." : ".$serverresponsearray['ackmessage'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch error
			if($serverresponsearray['result']['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0003: Please try again. '.$serverresponsearray['result']['ackmessage'];
				$results = json_encode($results);
				echo $results;
				die();
			}

	//print_r($serverresponsearray);
	//die();
			//made it this far assume we have reviews.
			$crawlerresultarray = $serverresponsearray['result'];
			
			if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0004: trouble finding reviews. Contact support with this error code and the search terms or place id you are using. ';
				$results = json_encode($results);
				echo $results;
				die();
			}
			$crawlerreviewsarray = $crawlerresultarray['reviews'];

			//need totals and avg for this place $getreviewsarray['total']
			$result['total']='';
			$result['avg']='';
			if(isset($crawlerresultarray['avg'])){
				$result['avg']=$crawlerresultarray['avg'];
			}
			if(isset($crawlerresultarray['total'])){
				$result['total']=$crawlerresultarray['total'];
			}
			
			//find link to google reviews.
			if (strpos($gplaceid, ' ') !== false) {
				$orgreviewurlvalue = 'https://www.google.com/search?q='.urlencode($gplaceid);
			} else {
				$orgreviewurlvalue = "https://search.google.com/local/reviews?placeid=".urlencode($gplaceid);
			}
			
			$x=0;
			$numreturned = count($crawlerreviewsarray);
			
			//print_r($crawlerreviewsarray);
	
			foreach ($crawlerreviewsarray as $review) {
				// Find user_name
				$results[$x]['user_name']=$review['user_name'];
				
				//created time
				$results[$x]['created_time']=$review['created_time'];
				$results[$x]['created_time_stamp']=$review['created_time_stamp'];
			
						
				//find reviewer_id from maps link		//https://www.google.com/maps/contrib/117800412986895302631?hl=en-US&sa=X&ved=2ahUKEwit9_W6s-PyAhWFTTABHZaUBeIQvvQBegQIARAh
				$results[$x]['user_link']=$review['user_link'];
				$results[$x]['reviewer_id']=$review['reviewer_id'];

				//find review rating span Fam1ne EBe2gf
				$results[$x]['rating']=$review['rating'];
				
				//find review text
				$results[$x]['review_text']=$review['review_text'];
				$results[$x]['reviewlength']=$review['reviewlength'];
				
				//find mediajson
				$results[$x]['mediaurlsarrayjson']=$review['mediaurlsarrayjson'];
				
				//find metadata
				$results[$x]['meta_data']=$review['meta_data'];
				
				//find owner res
				$results[$x]['owner_response']=$review['owner_response'];
								
				//find user image
				$results['userpic'] = $review['userpic'];
				
				if($review['from_url_review']!=''){
					$tempfrom_url_review=$review['from_url_review'];
				} else {
					$tempfrom_url_review=$orgreviewurlvalue;
				}
				
				
					$updatedtimestring = date( "Y-m-d H:i:s", $results[$x]['created_time_stamp']  );
					
					$from_url_val = $checkdetails[$tempkey]['googleurl']; 
					
					$reviewsarray[] = [
					 'reviewer_name' => $results[$x]['user_name'],
					 'reviewer_id' => $results[$x]['reviewer_id'],
					 'reviewer_email' => '',
					 'userpic' => $results['userpic'],
					 'rating' => $results[$x]['rating'],
					 'updated' => $updatedtimestring,
					 'review_text' => $results[$x]['review_text'],
					 'review_title' => '',
					 'from_url' => $orgreviewurlvalue,
					 'from_url_review' => $tempfrom_url_review,
					 'language_code' => '',
					 'location' => '',
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $results[$x]['mediaurlsarrayjson'],
					 'meta_data' => $results[$x]['meta_data'],
					 'owner_response' => $results[$x]['owner_response']
					 ];
				
				
				$x++;
			}

			$result['reviews'] = $reviewsarray;
			
			
			//only calling once since this is google.
			$result['stoploop'] ='stop';
			
			//print_r($result);
		
		return $result;
	}
	
	public function wprp_browser_curl($url){

		$ch=curl_init($url);
		curl_setopt_array($ch,array(
				CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',
				CURLOPT_ENCODING=>'gzip, deflate',
				CURLOPT_RETURNTRANSFER=>1,
				CURLOPT_HTTPHEADER=>array(
						'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
						'Accept-Language: en-US,en;q=0.5',
						'Connection: keep-alive',
						'Upgrade-Insecure-Requests: 1',
				),
		));

		$result=curl_exec($ch);
		
		curl_close($ch);
		
		return $result;
	}
	
	//for downloading TripAdvisor reviews using local server.
	public function wprp_getapps_getrevs_page_tripadvisorlocal($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron,$crawlserver){
		$result['ack']='success';
		set_time_limit(120);
		ini_set('memory_limit','400M');
		$errormsg='';
		$reviewsarraytemp = Array();
		$totalreviews = '';
		$avgrating = '';
		$result['total']='';
		$result['avg']='';
		$currenturl = $listedurl;
		$nexturl = '';
		$vactionrental = false;
		$attractionreviews=false;
		
		if($pagenum>1 && $nextpageurl!=''){
			$currenturl = $nextpageurl;
		}
		
		$parseurl = parse_url($listedurl);
		$baseurl = $parseurl['scheme'].'://'.$parseurl['host'];

		$attractionproductreviews=false;
		$vactionrental = false;
		$showuserreviewpage=false;
		$orgregularpage = false;
		$hotelreview = false;
		$attractionreviews=false;
		$usephantomsimple = false;
		
		if (strpos($currenturl, 'VacationRentalReview') !== false) {
			//this is a vactionrental
			$vactionrental = true;
			$usephantomsimple = true;
		} else if (strpos($currenturl, 'ShowUserReviews') !== false) {
			$showuserreviewpage=true;
		} else if (strpos($currenturl, 'Hotel_Review') !== false) {
			$hotelreview = true;
		} else if (strpos($currenturl, 'Attraction_Review') !== false) {
			$attractionreviews=true;
		} else if (strpos($currenturl, 'AttractionProductReview') !== false) {
			$attractionproductreviews=true;
		} else {
			$orgregularpage = true;
		}
		if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
			
			if($pagenum==1){
				if ($showuserreviewpage === false && $attractionproductreviews === false && $attractionreviews === false ) {
					$urlarray = $this->wprevpro_download_tripadvisor_showuserreviews_url($currenturl);
					$currenturl =$urlarray['page1'];
					$nexturl =$urlarray['page2'];
					$totalreviews = $urlarray['totalreviews'];
					$avgrating = $urlarray['avgrating'];
					sleep(1);
					$showuserreviewpage=true;
					//print_r($urlarray);
				}
			}

			//loop to grab pages
			$reviews = [];
			$reviewobject = [];
			$n=1;
			//foreach ($tripadvisorurl as $urlvalue) {
			$urlvalue = $currenturl;
			$localorphantom = "local";
			$phatomtry = 1;
			phantom:
			
			if($phatomtry > 1){
				$apitoken = 'a-demo-key-with-low-quota-per-ip-address';
				$phantomtempurlvalue = "https://phantomjscloud.com/api/browser/v2/".$apitoken."/?request={url:%22".urlencode($urlvalue)."%22,renderType:%22html%22,requestSettings:{doneWhen:[{event:%22domReady%22}]}}";
			}

				$args = array(
					'timeout'     => 60,
					'sslverify' => false
				); 
				
				if($localorphantom =="local"){
					//echo "local";
						//echo $urlvalue;
					$response = wp_remote_get( $urlvalue,$args );
					$results['urlvalue'] =$urlvalue;
					
					//$fileurlcontents = $this->wprp_browser_curl($urlvalue);
					
					//echo $fileurlcontents;
					//die();
				} else {
					//echo "phatnom";
						//echo $phantomtempurlvalue;
					$response = wp_remote_get( $phantomtempurlvalue,$args );
					$results['urlvalue'] =$phantomtempurlvalue;
				}
				if ( is_array( $response ) ) {
				  $header = $response['headers']; // array of http header lines
				  $fileurlcontents = $response['body']; // use the content
				} else {
						if($phatomtry<2){
							$phatomtry = 2;
							$localorphantom="phantom";
							//goto phantom;
						}
						$results['ack'] ='error';
						$results['ackmsg'] = esc_html__('Error 103a: Unable to read TripAdvisor page. Please contact support, the Review Funnel page, or change the crawl server to Remote.', 'wp-review-slider-pro');
						$results = json_encode($results);
						echo $results;
						die();
				}
				
				
				//fix for lazy load base64 ""
				//$fileurlcontents = str_replace('=="', '', $fileurlcontents);
				
				$html = wppro_str_get_html($fileurlcontents);
				
				//echo $fileurlcontents;
				//die();
					if($attractionproductreviews || $attractionreviews ){
						
						if($html->find('div.LbPSX', 0)){
							//another attraction review
							$reviewcontainerdiv = $html->find('div[data-automation=reviewCard]');
						
						}

						//go ahead and look for next page url.
						$nextlinkgrab = '';
						if($pagenum==1){
							if($html->find("a[class=BrOJk u j z _F wSSLS tIqAi iNBVo]", 0)){
								$nextlinkgrab = $html->find("a[class=BrOJk u j z _F wSSLS tIqAi unMkR]", 0)->href;
							}
						} else {
							if($html->find("a[class=BrOJk u j z _F wSSLS tIqAi iNBVo]", 1)){
								$nextlinkgrab = $html->find("a[class=BrOJk u j z _F wSSLS tIqAi unMkR]", 1)->href;
							}
						}
						
						

						if($nextlinkgrab!=''){
						$nexturl = $parseurl['scheme'].'://'.$parseurl['host'].$nextlinkgrab;
						}
							
					} else {
						if($html->find('div.reviewSelector', 0)){
							$reviewcontainerdiv = $html->find('div.reviewSelector');
							//echo "here1";
						} else if($html->find('div.review-container', 0)){
							$reviewcontainerdiv = $html->find('div.review-container');
							//echo "here2";
						} else if($html->find('div._1c8_1ITO', 0)){
							//print_r($html->find('div._1c8_1ITO', 0));
							$reviewcontainerdiv = $html->find('div._1c8_1ITO', 0)->children;
						} else if($html->find("div[class=Dq9MAugU T870kzTX LnVzGwUB]",0)){
							//echo "here3";
								//another attraction review
								$reviewcontainerdiv = $html->find("div[class=Dq9MAugU T870kzTX LnVzGwUB]");
						} else if($html->find('div[class=eVykL Gi z cPeBe MD cwpFC]',0)){
								//another attraction review
								//eVykL Gi z cPeBe MD cwpFC
								//echo "here4";
								$reviewcontainerdiv = $html->find('div[class=eVykL Gi z cPeBe MD cwpFC]');
						} else if($html->find('div.bPhtn', 0)){
								//another attraction review
								$reviewcontainerdiv = $html->find('div.bPhtn', 0)->children;
							
						} else if($html->find('div.dHjBB', 0)){
								//another attraction review
								$reviewcontainerdiv = $html->find('div.dHjBB', 0)->children;
						} else if($html->find('div.LbPSX', 0)){
								//another attraction review
								$reviewcontainerdiv = $html->find('div.LbPSX', 0)->children;
						}
						
						//if this is page 1 check to see if on first page of tripadvisor.
						//echo $html;
						//die();
						//echo "pagenum:".$pagenum;
						$onfirstpage ='';
						if($pagenum==1){
							//check if we are not on first page
							if($html->find('a[class=pageNum first current]', 0)){
								$onfirstpage = 'yes';
							} else if($html->find('a[class=pageNum first]', 0)){
								$onfirstpage = 'no';
								$rtitlelink = $html->find('a[class=pageNum first]', 0)->href;
								$nexturl = $parseurl['scheme'].'://'.$parseurl['host'].$nextlinkgrab;
							}
						}
						//echo "nexturl:".$nexturl;
						//echo "onfirstpage:".$onfirstpage;
						//die();
						
					}
					
					if(!isset($reviewcontainerdiv) || count($reviewcontainerdiv)<1){
						if($phatomtry<3){
							$phatomtry = 3;
							$localorphantom="phantom";
							goto phantom;
						}
					}
					if(!isset($reviewcontainerdiv) || count($reviewcontainerdiv)<1){
						$results['ack'] ='error';
						$results['ackmsg'] = esc_html__('Error 103b: Unable to read TripAdvisor page. Please change the crawl server to Remote, or use the Review Funnel page. If you still have trouble contact support.', 'wp-review-slider-pro');
						$results = json_encode($results);
						echo $results;
						die();
					}
					
				
				//in case we couldn't get it from first page search string.
				if($pagenum==1){
					if($totalreviews<1 || $avgrating<1){
						$avgtotalarray = $this->tripadvisorgettotalavg($html,"");
						//print_r($avgtotalarray);
						$totalreviews = $avgtotalarray['totalreviews'];
						$avgrating = $avgtotalarray['avgrating'];
					}
					
					//if they aren't find try getting them from the original listed url.
					if($totalreviews<1 || $avgrating<1){
						//echo "<br>getting from listedurl<br>";
						$avgtotalarray = $this->tripadvisorgettotalavg("",$listedurl);
						$totalreviews = $avgtotalarray['totalreviews'];
						$avgrating = $avgtotalarray['avgrating'];
						//print_r($avgtotalarray);
					}
				}
				if($totalreviews>0){
					$result['total']=$totalreviews;
				}
				if($avgrating>0){
					$result['avg']=$avgrating;
				}
				
				//for user avatars
				$startstringpos = stripos("$html","var lazyImgs = [") + 16;
				$choppedstr = substr("$html", $startstringpos);
				$endstringpos = stripos("$choppedstr","]");
				$finalstring = trim(substr("$html", $startstringpos, $endstringpos));
				$finalstring =str_replace(":true",':"true"',$finalstring);
				$finalstring ="[".str_replace(":false",':"false"',$finalstring)."]";
				$jsonlazyimg  = json_decode($finalstring, true);

				// Find 20 reviews
				$i = 1;
				
				
				foreach ($reviewcontainerdiv as $review) {
					
					//echo $review->dump();
				//	die();
					
						if ($i > 21) {
								break;
						}
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						// Find user_name
						if($review->find('div.username', 0)){
							$user_name = $review->find('div.username', 0)->plaintext;
						}
						if($user_name==''){
							if($review->find('div.info_text', 0)){
								$user_name = $review->find('div.info_text', 0)->find('div', 0)->plaintext;
							}
						}
						//for attraction
						if($user_name==''){
							if($review->find("span[class=DrjyGw-P _1SRa-qNz NGv7A1lw _2yS548m8 _2cnjB3re _1TAWSgm1 _1Z1zA2gh _2-K8UW3T _2AAjjcx8]", 0)){
								$user_name = $review->find("span[class=DrjyGw-P _1SRa-qNz NGv7A1lw _2yS548m8 _2cnjB3re _1TAWSgm1 _1Z1zA2gh _2-K8UW3T _2AAjjcx8]", 0)->plaintext;
							}
						}
						//for attraction 2
						if($user_name==''){
							if($review->find("a[class=ui_header_link _1r_My98y]", 0)){
								$user_name = $review->find("a[class=ui_header_link _1r_My98y]", 0)->plaintext;
							}
						}
						if($user_name==''){
							if($review->find("a[class=ui_header_link bPvDb]", 0)){
								$user_name = $review->find("a[class=ui_header_link bPvDb]", 0)->plaintext;
							}
						}
						//for attraction 2
						if($user_name==''){
							if($review->find("a[class=iPqaD _F G- ddFHE eKwUx btBEK fUpii]", 0)){
								$user_name = $review->find("a[class=iPqaD _F G- ddFHE eKwUx btBEK fUpii]", 0)->plaintext;
							}
						}
						//for attraction 2 biGQs _P fiohW fOtGX
						if($user_name==''){
							if($review->find("span[class=biGQs _P fiohW fOtGX]", 0)){
								$user_name = $review->find("span[class=biGQs _P fiohW fOtGX]", 0)->plaintext;
							}
						}
				
							
							
							// Find userimage ui_avatar, need to pull from lazy load varible
							if($review->find('div.ui_avatar', 0)){
								if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)){
									$userimageid = $review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->id;
									//strip id from 
									$userimageid = strrchr ($userimageid , "_" );
									//loop through array and return url
									if (isset($jsonlazyimg) && is_array($jsonlazyimg)){
									for ($x = 0; $x <= count($jsonlazyimg); $x++) {
										//get temp id
										$tempid = $jsonlazyimg[$x]['id'];
										$tempid = strrchr ($tempid , "_" );
										if($userimageid==$tempid){
											$userimage = $jsonlazyimg[$x]['data'];
											$x = count($jsonlazyimg) + 1;
										}
									} 
									}
								}
							}

							//if userimage not found check
							$checkstringpos =  strpos($userimage, 'base64');
							if($userimage =='' || $checkstringpos>0){
								if($review->find('div.ui_avatar', 0)){
									if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)){
										if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->{'data-lazyurl'}){
											$userimage =$review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->{'data-lazyurl'};
										} else {
											$userimage =$review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->src;
										}
									}
								}
								
							}
							//echo "<br>userimage2:".$userimage;
							//another fix
							if($userimage ==''){
								if($review->find('div.avatar', 0)){
									if($review->find('div.avatar', 0)->find('img.avatar', 0)){
										$userimage =$review->find('div.avatar', 0)->find('img.avatar', 0)->{'src'};
									}
								}
							}
							//echo "<br>userimage3:".$userimage;
							//one more try for hotels
							if($userimage =='' && $review->find('div.ui_avatar', 0)){
								if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)){
										$userimage =$review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->src;
								}
							}
							//echo "<br>userimage4:".$userimage;
							//if userimage not found check
							if($userimage =='' && $review->find('div.ui_avatar', 0)){
								if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)){
									if($review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->{'data-lazyurl'}){
										$userimage =$review->find('div.ui_avatar', 0)->find('img.basicImg', 0)->{'data-lazyurl'};
									}
								}
								//echo "<br>userimage:".$userimage;
							}
							//echo "<br>userimage5:".$userimage;
							//another check for activities userimage
							if($userimage =='' && $review->find('picture._2f-Th360', 0)){
								if($review->find('picture._2f-Th360', 0)->find('img', 0)){
										$userimage =$review->find('picture._2f-Th360', 0)->find('img', 0)->src;
								}
							}
							//echo "<br>userimage6:".$userimage;
							//another check for activities userimage
							if($userimage =='' && $review->find("a[class=_3x5_awTA ui_social_avatar inline]", 0)){
								if($review->find("a[class=_3x5_awTA ui_social_avatar inline]", 0)->find('img', 0)){
										$userimage =$review->find("a[class=_3x5_awTA ui_social_avatar inline]", 0)->find('img', 0)->src;
								}
							}
							//echo "<br>userimage7:".$userimage;
							//another check for activities userimage
							if($userimage =='' && $review->find("a[class=ui_social_avatar inline]", 0)){
								if($review->find("a[class=ui_social_avatar inline]", 0)->find('img', 0)){
										$userimage =$review->find("a[class=ui_social_avatar inline]", 0)->find('img', 0)->src;
								}
							}
							//echo "<br>userimage8:".$userimage;
							//bugwz I ui_social_avatar inline
							if($userimage =='' && $review->find("a[class=bugwz I ui_social_avatar inline]", 0)){
								if($review->find("a[class=bugwz I ui_social_avatar inline]", 0)->find('img', 0)){
										$userimage =$review->find("a[class=bugwz I ui_social_avatar inline]", 0)->find('img', 0)->src;
								}
							}
							//echo "<br>userimage9:".$userimage;
							//another check for activities userimage
							if($userimage =='' && $review->find("a[class=iPqaD _F G- ddFHE eKwUx]", 0)){
								if($review->find("a[class=iPqaD _F G- ddFHE eKwUx]", 0)->find('img', 0)){
										$userimage =$review->find("a[class=iPqaD _F G- ddFHE eKwUx]", 0)->find('img', 0)->src;
								}
							}
							//echo "<br>userimage10:".$userimage;
							//another check for activities userimage
							if($userimage =='' && $review->find("div[class=FGwzt PaRlG]", 0)){
								if($review->find("div[class=FGwzt PaRlG]", 0)->find('img', 0)){
										$userimage =$review->find("div[class=FGwzt PaRlG]", 0)->find('img', 0)->src;
								}
							}



						// find rating
						if($review->find('span.ui_bubble_rating', 0)){
							$temprating = $review->find('span.ui_bubble_rating', 0)->class;
							$int = filter_var($temprating, FILTER_SANITIZE_NUMBER_INT);
							//echo $int."<br>";
							$rating = str_replace(0,"",$int);
						}
						//rating for activities
						$temprating ='';
						if($rating ==''){
							if($review->find('.zWXXYhVR', 0)){
							$temprating = $review->find('.zWXXYhVR', 0)->title;
							} else if($review->find("svg[class=RWYkj d H0]", 0)){
							$temprating = $review->find("svg[class=RWYkj d H0]", 0)->title;	
							} else if($review->find("svg[class=UctUV d H0]", 0)){
							$temprating = $review->find("svg[class=UctUV d H0]", 0)->getAttribute('aria-label');
							}
							if($temprating==''){
									if($review->find("svg[class=UctUV d H0]", 0)){
										if($review->find("svg[class=UctUV d H0]", 0)->find("title", 0)){
											$temprating = $review->find("svg[class=UctUV d H0]", 0)->find("title", 0)->plaintext;
										}
									}
								}
							$temprating = str_replace('.0 of 5 bubbles',"",$temprating);
							$temprating = str_replace('Punteggio ',"",$temprating);
							$temprating = str_replace(',0 su 5',"",$temprating);
							$temprating = str_replace(',0 sur 5',"",$temprating);
							$rating = filter_var($temprating, FILTER_SANITIZE_NUMBER_INT);
							$rating = $rating[0];
						}
						
						if($rating ==''){
							$temprating = $review->find('span.ui_star_rating', 0)->class;
							$int = filter_var($temprating, FILTER_SANITIZE_NUMBER_INT);
							//echo $int."<br>";
							$rating = str_replace(0,"",$int);
						}
						
						// find date
						if($review->find('span.ratingDate', 0)){
							//if($vactionrental==false){
							//	$datesubmitted = $review->find('span.ratingDate', 0)->title;
							//	$datesubmitted = preg_replace("(<([a-z]+)>.*?</\\1>)is","",$datesubmitted);
							//} else {
								if($review->find('span.ratingDate', 0)->title){
									$datesubmitted = $review->find('span.ratingDate', 0)->title;
									$datesubmitted = preg_replace("(<([a-z]+)>.*?</\\1>)is","",$datesubmitted);
								} else {
								$datesubmitted = $review->find('span.ratingDate', 0)->innertext;
								$datesubmitted = preg_replace("(<([a-z]+)>.*?</\\1>)is","",$datesubmitted);
								$datesubmitted = str_replace("Reviewed","",$datesubmitted);
								$datesubmitted = str_replace("Beoordeeld","",$datesubmitted);
								$datesubmitted = str_replace("op","",$datesubmitted);
								$datesubmitted = str_replace('Recensito il',"",$datesubmitted);
								
								//$datesubmitted = date('d-m-Y H:i:s', strtotime($datesubmitted, 1324189035));
								}
							//}
						}
						//for activities 2 _2fxQ4TOx
						if($datesubmitted ==''){
							if($review->find("div[class=_2fxQ4TOx]", 0)){
								$datesubmitted = $review->find("div[class=_2fxQ4TOx]", 0)->plaintext;
							}
						}
						
						//for activities 2
						if($datesubmitted ==''){
							if($review->find("span[class=_34Xs-BQm]", 0)){
								$datesubmitted = $review->find("span[class=_34Xs-BQm]", 0)->plaintext;
							}
						}
						//for activities 2 DrjyGw-P _26S7gyB4 _1z-B2F-n _1dimhEoy
						if($datesubmitted ==''){
							if($review->find("div[class=DrjyGw-P _26S7gyB4 _1z-B2F-n _1dimhEoy]", 0)){
								$datesubmitted = $review->find("div[class=DrjyGw-P _26S7gyB4 _1z-B2F-n _1dimhEoy]", 0)->plaintext;
							}
						}
						//for activities 2 euPKI _R Me S4 H3
						if($datesubmitted ==''){
							if($review->find("span[class=euPKI _R Me S4 H3]", 0)){
								$datesubmitted = $review->find("span[class=euPKI _R Me S4 H3]", 0)->plaintext;
							}
						}
						//for activities
						if($datesubmitted ==''){
							if($review->find("div[class=WlYyy diXIH cspKb bQCoY]", 0)){
								$datesubmitted = $review->find("div[class=WlYyy diXIH cspKb bQCoY]", 0)->plaintext;
							}
						}
						//for activities italian
						if($datesubmitted ==''){
							if($review->find("div[class=bcaHz]", 0)){
								$datesubmitted = $review->find("div[class=bcaHz]", 0)->plaintext;
							}
						}
						//for activities  biGQs _P pZUbB ncFvv osNWb
						if($datesubmitted ==''){
							if($review->find("div[class=biGQs _P pZUbB ncFvv osNWb]", 0)){
								$datesubmitted = $review->find("div[class=biGQs _P pZUbB ncFvv osNWb]", 0)->plaintext;
							}
						}
						
						//echo 'date:'.$datesubmitted;
						$datesubmitted = $this->formatdatestring($datesubmitted,$user_name);
						//echo 'dateformat:'.$datesubmitted;
						
						//Escrita el 6 de agosto de 2024
						
						
						//$timestamp = $this->myStrtotime($datesubmitted);
						//echo "---<br>".$timestamp;
						//die();
						
						// find text
						$rtext='';
						//if($vactionrental==false){
							if($review->find('div.prw_reviews_text_summary_hsx', 0)){
								$rtext = $review->find('div.prw_reviews_text_summary_hsx', 0)->find('p', 0)->plaintext;
							}
							//if this is the first review then handle differently, it is at top of showuserreview page
							if($i==1){
								if($review->find('div.prw_reviews_resp_sur_review_text', 0)){
									//echo 'here1';
									$rtext = $review->find('div.prw_reviews_resp_sur_review_text', 0)->find('p', 0)->plaintext;
								} else if($review->find('div.prw_reviews_resp_sur_review_text_expanded', 0)){
									$rtext = $review->find('div.prw_reviews_resp_sur_review_text_expanded', 0)->find('p', 0)->plaintext;
									//echo 'here2';
									//echo 'rtext-'.$rtext;
									//print_r($review->find('div.prw_reviews_resp_sur_review_text_expanded', 0)->find('p', 0));
								}
								
								
							}
						//} else {
						//	if($review->find('div.entry', 0)){
						//		$rtext = $review->find('div.entry', 0)->find('p', 0)->plaintext;
						//	}
						//}
						//if rtext is blank try one more time, used to get top review on hotels
						if($rtext==''){
							if($review->find('div.entry', 0)){
								$rtext = $review->find('div.entry', 0)->find('p', 0)->plaintext;
							}
						}
						//try again for activities
						if($rtext==''){
							if($review->find("div[class=DrjyGw-P _26S7gyB4 _2nPM5Opx]", 0)){
								$rtext = $review->find("div[class=DrjyGw-P _26S7gyB4 _2nPM5Opx]", 0)->plaintext;
							}
						}
						//try again for activities
						if($rtext==''){
							if($review->find("div[class=cPQsENeY]", 0)){
								$rtext = $review->find("div[class=cPQsENeY]", 0)->plaintext;
							}
						}
						//try again for activities
						if($rtext==''){
							if($review->find("q[class=XllAv H4 _a]", 0)){
								$rtext = $review->find("q[class=XllAv H4 _a]", 0)->plaintext;
								$rtext = mb_substr($rtext, 0, -10);
								$rtext = str_replace("…","",$rtext);
							}
						}
						//try again for activities
						if($rtext==''){
							if($review->find("div[class=WlYyy diXIH dDKKM]", 0)){
								$rtext = $review->find("div[class=WlYyy diXIH dDKKM]", 0)->plaintext;
								$rtext = mb_substr($rtext, 0, -10);
								$rtext = str_replace("…","",$rtext);
							}
						}
						//try again for activities
						if($rtext==''){
							if($review->find("div[class=biGQs _P pZUbB KxBGd]", 0)){
								$rtext = $review->find("div[class=biGQs _P pZUbB KxBGd]", 0)->plaintext;
								//$rtext = mb_substr($rtext, 0, -10);
								$rtext = str_replace("…","",$rtext);
							}
						}
						
						$rtext =str_replace("...More","...",$rtext);
						
						$rtext = trim($rtext);

						//find title if set on options page
						//-may be used in future version--------
							$rtitle = '';
							if($vactionrental==false){
								if($review->find('span.noQuotes', 0)){
									$rtitle = $review->find('span.noQuotes', 0)->plaintext;
								}
							} else {
								if($review->find('div.quote', 0)){
									$rtitle = $review->find('div.quote', 0)->plaintext;
								}									
							}
							//if rtitle
							if($rtitle==''){
								if($review->find('div.quote', 0)){
									$rtitle = $review->find('div.quote', 0)->plaintext;
								}
							}
							//for activity
							if($rtitle==''){
								if($review->find('span._2tsgCuqy', 0)){
									$rtitle = $review->find('span._2tsgCuqy', 0)->plaintext;
								}
							}
							//for activity
							if($rtitle==''){
								if($review->find('div.glasR4aX', 0)){
									$rtitle = $review->find('div.glasR4aX', 0)->plaintext;
								}
							}
							//fpMxB MC _S b S6 H5 _a
							if($rtitle==''){
								if($review->find("div[class=fpMxB MC _S b S6 H5 _a]", 0)){
									$rtitle = $review->find("div[class=fpMxB MC _S b S6 H5 _a]", 0)->plaintext;
								}
							}
							if($rtitle==''){
								if($review->find("div[class=WlYyy cPsXC bLFSo cspKb dTqpp]", 0)){
									$rtitle = $review->find("div[class=WlYyy cPsXC bLFSo cspKb dTqpp]", 0)->plaintext;
								}
							}
							if($rtitle==''){
								if($review->find("div[class=biGQs _P fiohW qWPrE ncFvv fOtGX]", 0)){
									$rtitle = $review->find("div[class=biGQs _P fiohW qWPrE ncFvv fOtGX]", 0)->plaintext;
								}
							}
							
							
						//-----------------------------------
						$fromlink = $listedurl;
						
						if($attractionproductreviews){

							
						} else {
						
							if($review->find('div.quote', 0)){
								if($review->find('div.quote', 0)->find('a',0)){
								$fromlinkgrab = $review->find('div.quote', 0)->find('a',0)->href;
								$parseurl = parse_url($urlvalue);
								$fromlink= $parseurl['scheme'].'://'.$parseurl['host'].$fromlinkgrab;
									if($showuserreviewpage && $pagenum>1){
										$nexturl = $fromlink;
									}
								} else {
								$fromlink = $listedurl;
								}
							} else {
								$fromlink = $listedurl;
							}
						}
						
						if($rating>0 && $rtext!=''){
			
							$review_length = substr_count($rtext, ' ');

							$pos = strpos($userimage, 'default_avatars');
							if ($pos === false) {
								$userimage = str_replace("60s.jpg","120s.jpg",$userimage);
							}

							//echo "<br>".$datesubmitted;
							$timestamp = $this->myStrtotime($datesubmitted);
							//echo "---<br>".$timestamp;
							//die();
							$unixtimestamp = $timestamp;
							$timestamp = date("Y-m-d H:i:s", $timestamp);
							$hideme = 'no';
							
							$review_length = str_word_count($rtext);
							if (extension_loaded('mbstring')) {
								$review_length_char = mb_strlen($rtext);
							} else {
								$review_length_char = strlen($rtext);
							}
							if($review_length_char>0 && $review_length<1){
								$review_length = 1;
							}
							
							//mediaurlsarrayjson, max of 8 images
							//--------------
							$mediaurlsarrayjson='';
							$mediaurlsarray = Array();
							
							if($attractionproductreviews || $attractionreviews){
								if($review->find('button[class=ajoIU _S B-]',0)){
									//have at least one image
									$imagesobject = $review->find('button[class=ajoIU _S B-]');
								} else if($review->find('button[class=UikNM _G B- _S _W _T c G_ wSSLS wnNQG]',0)){
									$imagesobject = $review->find('button[class=UikNM _G B- _S _W _T c G_ wSSLS wnNQG]');
								}
								if($imagesobject){
									//echo $imagesobject[0]->find('img', 0)->src;
									//die();
									foreach ($imagesobject as $imageobj) {
										if($imageobj->find('img', 0)){
											$tempurl= $imageobj->find('img', 0)->src;
											if($tempurl!='' && strpos($tempurl, "base64") == false){
												//get bigger image.
												$imgsizes = array("w=400", "w=300", "w=200", "w=100");
												$mediaurlsarray[]=str_replace($imgsizes,"w=500",$tempurl);
											}
											if(count($mediaurlsarray)>7){
												break;
											}
											$mediaimg='';
											$tempurl='';
										}
									}
									$mediaurlsarrayjson = json_encode($mediaurlsarray);
									unset($mediaurlsarray);
								}
							} else {
							
								if($review->find('div[class=photoContainer]',0)){
									//have at least one image
									$imagesobject = $review->find('div[class=photoContainer]');
									foreach ($imagesobject as $imageobj) {
										if($imageobj->find('img', 1)){
											$tempurl= $imageobj->find('img', 1)->src;
											if($tempurl!='' && strpos($tempurl, "base64") == false){
												//get bigger image.
												$mediaurlsarray[]=str_replace("/photo-l/","/photo-s/",$tempurl);
											}
											if(count($mediaurlsarray)>7){
												break;
											}
											$mediaimg='';
											$tempurl='';
										} else if($imageobj->find('img', 0)){
											$tempurl= $imageobj->find('img', 0)->src;
											if($tempurl!='' && strpos($tempurl, "base64") == false){
												//get bigger image.
												$mediaurlsarray[]=str_replace("/photo-l/","/photo-s/",$tempurl);
											}
											if(count($mediaurlsarray)>7){
												break;
											}
											$mediaimg='';
											$tempurl='';
										}
									}
									$mediaurlsarrayjson = json_encode($mediaurlsarray);
									unset($mediaurlsarray);
								}
							}
							//look for owner response
							//owner_response {"id":71320417,"name":"Response from the owner","date":"2020-06-05","comment":"Thank You will Matsch "}
							$owner_response_encode ='';
							$owner['id'] = '';
							$owner['name'] = '';
							$owner['comment'] = '';
							$owner['date'] = '';
							//mgrRspnInline
							if($attractionproductreviews || $attractionreviews){
								if($review->find('div[class=hjJJO PJ]',0)){
									//must be a response, look for details.
									$responsediv = $review->find('div[class=hjJJO PJ]',0);

									if($responsediv->find('span[class=biGQs _P fiohW fOtGX]', 0)){
										$owner['name']= $responsediv->find('span[class=biGQs _P fiohW fOtGX]', 0)->plaintext;
									} else {
										$owner['name'] = 'Response from the owner';
									}
									
									if($responsediv->find('div[class=biGQs _P pZUbB xUqsL ncFvv osNWb]', 0)){
										//responseDate
										$tempresdate = $responsediv->find('div[class=biGQs _P pZUbB xUqsL ncFvv osNWb]', 0)->plaintext;
										$tempdate = $this->formatdatestring($tempresdate,'');
										$tempdate = $this->myStrtotime($tempdate);
										if($tempdate==''){
											//use review date.
											$tempdate = $unixtimestamp;
										}
										$owner['date'] = date('Y-m-d', $tempdate);
									}
									if($responsediv->find('span[class=JguWG]', 0)){
										$owner['comment']= $responsediv->find('span[class=JguWG]', 0)->plaintext;
									}
								}
							} else {
								if($review->find('div[class=mgrRspnInline]',0)){
									//must be a response, look for details.
									$responsediv = $review->find('div[class=mgrRspnInline]',0);
									if($responsediv->find('div[class=prw_rup prw_reviews_response_header]', 0)){
										$owner['name']= $responsediv->find('div[class=prw_rup prw_reviews_response_header]', 0)->plaintext;
										//need to subtract date.
										if($responsediv->find('span[class=responseDate]', 0)){
											$tempresdate = $responsediv->find('span[class=responseDate]', 0)->plaintext;
											$owner['name'] = str_replace($tempresdate,"",$owner['name']);
										}
									} else {
										$owner['name'] = 'Response from the owner';
									}
									if($responsediv->find('span[class=responseDate]', 0)){
										//responseDate
										$tempresdate = $responsediv->find('span[class=responseDate]', 0)->plaintext;
										$tempdate = $this->formatdatestring($tempresdate,'');
										$tempdate = $this->myStrtotime($tempdate);
										if($tempdate==''){
											//use review date.
											$tempdate = $unixtimestamp;
										}
										$owner['date'] = date('Y-m-d', $tempdate);
									}
									if($responsediv->find('p[class=partial_entry]', 0)){
										$owner['comment']= $responsediv->find('p[class=partial_entry]', 0)->plaintext;
									}
								}
							}
							if($owner['comment']!=''){
								$owner_response_encode = json_encode($owner);
							}
							//end owner response

		

							//check to see if in database already
							$reviewindb = 'no';
							$user_name = trim($user_name);
							$reviews[] = [
									'reviewer_name' => $user_name,
									'pagename' => '',
									'pageid' => '',
									'userpic' => $userimage,
									'rating' => $rating,
									'created_time' => $timestamp,
									'created_time_stamp' => $unixtimestamp,
									'review_text' => trim($rtext),
									'review_title' => trim($rtitle),
									'hide' => $hideme,
									'type' => 'TripAdvisor',
									'from_url' => trim($listedurl),
									'from_url_review' => $fromlink,
									'review_length_char' => $review_length_char,
									'mediaurlsarrayjson' => $mediaurlsarrayjson,
									'owner_response' => $owner_response_encode,
							];

						}
				 
						$i++;
				}
				
				$n++;
				
				//var_dump($reviews);
				// clean up memory

			//}
			 
			// print_r($reviews);
			// die();
			 //echo "<br>";

			$reviews = array_unique($reviews, SORT_REGULAR);
			//$tempArr = array_unique(array_column($reviews, 'value'));
			//$reviews = array_intersect_key($array, $tempArr);

			//remove duplicates
			$reviewernames = [];
			$reviewlength = [];
			$insertreviews = [];
			foreach ( $reviews as $stat ){
				if (!in_array($stat['reviewer_name'], $reviewernames) || !in_array($stat['review_length_char'], $reviewlength)) {
					$insertreviews[] = $stat;
				}
				$reviewernames[] = $stat['reviewer_name'];
				$reviewlength[] = $stat['review_length_char'];
			}
			

			$x=0;
			$reviewsarray= Array();
			$numreturned = count($insertreviews);
			
			foreach ($insertreviews as $review) {
				
				$tempownerres='';
				if(isset($review['owner_response']) && $review['owner_response']!=''){
					$tempownerres = $review['owner_response'];
				}
				$templocation ='';
				if(isset($review['location']) && $review['location']!=''){
					$templocation = $review['location'];
				}	
				$tempmediaurlsarrayjson ='';
				if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
					$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
				}					
				
				$reviewsarray[] = [
				 'reviewer_name' => $review['reviewer_name'],
				 'reviewer_id' => '',
				 'reviewer_email' => '',
				 'userpic' => $review['userpic'],
				 'rating' => $review['rating'],
				 'updated' => $review['created_time'],
				 'review_text' => $review['review_text'],
				 'review_title' => $review['review_title'],
				 'from_url' => $listedurl,
				 'from_url_review' => $review['from_url_review'],
				 'language_code' => '',
				 'location' => $templocation,
				 'recommendation_type' => '',
				 'company_title' =>  '',
				 'company_url' => '',
				 'company_name' => '',
				 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
				 'owner_response' => $tempownerres,
				 ];
				
				$x++;
			}

			
			
			//pass back URL used
			if(isset($urlvalue)){
				$result['callurl']=$urlvalue;
			}
			//pass back next URL used
			if(isset($nexturl) && $nexturl!=''){
				$result['nextpageurl']=$nexturl;
			}
			
			if (!empty($html)) {
				$html->clear();
				unset($html);
			}
				
			//pass back stoploop if set
			$result['stoploop']='';
			
			$result['crawlserver'] ='local';
			
			$result['reviews'] = $reviewsarray;
			
		}
			
		return $result;
	}
	
	//for calling remote crawler for Amazon reviews - always uses remote server (no local option)
	public function wprp_getapps_getrevs_page_amazon($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron){
		$result['ack']='success';
		set_time_limit(150);
		$errormsg='';
		$reviewsarraytemp = Array();
		
		if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
			
			//strip any query parameters from URL
			$stripvariableurl = stripslashes($listedurl);
			$listedurl = strtok($stripvariableurl, '?');
			
			if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
				$ip_server = $_SERVER['SERVER_ADDR'];
			} else {
				$ip_server = urlencode(get_site_url());
			}
			$siteurl = urlencode(get_site_url());
			
			//build crawler server URL - always using remote for Amazon
			$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=amazon&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron;

			//echo $tempurlval;
			//die();
			$serverresponse='';
			$args = array(
				'timeout'     => 120,
				'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				)
			); 
			$response = wp_remote_get( $tempurlval, $args );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers'];
				$serverresponse    = $response['body'];
			} else {
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
				$results = json_encode($results);
				echo $results;
				die();
			}

			//check for block or timeout - try backup server
			if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
				//call backup digital ocean server
				$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=amazon&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron, array( 'sslverify' => false, 'timeout' => 60 ) );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers'];
					$serverresponse    = $response['body'];
				}
			}
			
			$serverresponsearray = json_decode($serverresponse, true);

			if($serverresponse=='' || !is_array($serverresponsearray)){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch limit error
			if($serverresponsearray['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the Amazon product URL you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch error
			if($serverresponsearray['result']['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			//made it this far assume we have reviews
			$crawlerresultarray = $serverresponsearray['result'];
			
			if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0004 Amazon: trouble finding reviews. Contact support with this error code and the Amazon product URL you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			$reviewsarray = $crawlerresultarray['reviews'];
			$result['total'] = $crawlerresultarray['total'];
			$result['avg'] = $crawlerresultarray['avg'];
			
			//store rating breakdown if available
			if(isset($crawlerresultarray['rating_breakdown'])){
				$result['rating_breakdown'] = $crawlerresultarray['rating_breakdown'];
			}
			
		} else {
			$result['ack'] ='error';
			$result['ackmsg'] = esc_html__('Error 100: Please enter a valid Amazon product URL.', 'wp-review-slider-pro');
		}
		
		$result['crawlserver'] ='remote';


		//map Amazon crawler response to plugin format
		$reviewsarrayfinal = array();
		foreach( $reviewsarray as $item){
			
			//extract reviewer_id from review_id if available
			$intreviewer_id = '';
			if(isset($item['review_id']) && $item['review_id']!=''){
				$intreviewer_id = filter_var($item['review_id'], FILTER_SANITIZE_NUMBER_INT);
			}
			
			//convert timestamp to datetime string
			$updatedtimestring = '';
			if(isset($item['created_time_stamp']) && $item['created_time_stamp']!=''){
				$updatedtimestring = date( "Y-m-d H:i:s", $item['created_time_stamp'] );
			} else if(isset($item['created_time']) && $item['created_time']!=''){
				//fallback to parsing created_time string
				$updatedtimestring = date( "Y-m-d H:i:s", strtotime($item['created_time']) );
			}
			
		//prepare media URLs JSON if review_images exist
		$mediaurlsarrayjson = '';
		if(isset($item['review_images']) && $item['review_images']!=''){
			//review_images is comma-separated string, convert to JSON array
			$imagesarray = explode(',', $item['review_images']);
			$mediaurlsarrayjson = json_encode($imagesarray);
		}
		
		//build metadata JSON with Amazon-specific fields
		$metadata = array();
		if(isset($item['verified_purchase']) && $item['verified_purchase']!=''){
			$metadata['verified_purchase'] = $item['verified_purchase'];
		}
		if(isset($item['helpful_votes']) && $item['helpful_votes']!=''){
			$metadata['helpful_votes'] = $item['helpful_votes'];
		}
		$metadatajson = !empty($metadata) ? json_encode($metadata) : '';
		
		//map verified_purchase to verified_order field
		$verified_order = '';
		if(isset($item['verified_purchase']) && $item['verified_purchase']=='yes'){
			$verified_order = 'yes';
		}
		
		//build review record
		$reviewsarrayfinal[] = [
		 'reviewer_name' => isset($item['reviewer_name']) ? $item['reviewer_name'] : '',
		 'reviewer_id' => $intreviewer_id,
		 'reviewer_email' => '',
		 'userpic' => isset($item['userpic']) ? $item['userpic'] : '',
		 'rating' => isset($item['rating']) ? $item['rating'] : 0,
		 'updated' => $updatedtimestring,
		 'review_text' => isset($item['review_text']) ? $item['review_text'] : '',
		 'review_title' => '',
		 'from_url' => $listedurl,
		 'from_url_review' => '',
		 'from_url_author' => '',
		 'language_code' => '',
		 'location' => '',
		 'recommendation_type' => '',
		 'company_title' =>  '',
		 'company_url' => '',
		 'company_name' => isset($item['pagename']) ? $item['pagename'] : 'Amazon',
		 'mediaurlsarrayjson' => $mediaurlsarrayjson,
		 'meta_data' => $metadatajson,
		 'verified_order' => $verified_order,
		 'unique_id' => isset($item['review_id']) ? $item['review_id'] : ''
		 ];

		}

		$result['reviews'] = $reviewsarrayfinal;

		
		return $result;
	}
	
	//for calling remote get and returning array of reviews to insert for Etsy
	public function wprp_getapps_getrevs_page_etsy($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron){
		$result['ack']='success';
		set_time_limit(150);
		$errormsg='';
		$reviewsarray = Array(); // Initialize $reviewsarray here
		$nhful='new'; // Default for Etsy
		
		if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
			$stripvariableurl = stripslashes($listedurl);
			$listedurl = strtok($stripvariableurl, '?'); //remove all parameters
			
			if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
				$ip_server = $_SERVER['SERVER_ADDR'];
			} else {
				$ip_server = urlencode(get_site_url());
			}
			$siteurl = urlencode(get_site_url());
			
		//build crawler server URL - always using remote for Etsy
		//pass blocks parameter to control how many review pages to scrape
		$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=etsy&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron;

		$serverresponse='';
			$args = array(
				'timeout'     => 120,
				'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				)
			); 
			$response = wp_remote_get( $tempurlval, $args );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers'];
				$serverresponse    = $response['body'];
			} else {
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
				$results = json_encode($results);
				echo $results;
				die();
			}

		//check for block or timeout - try backup server
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
			//call backup digital ocean server
			$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=etsy&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron, array( 'sslverify' => false, 'timeout' => 60 ) );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers'];
					$serverresponse    = $response['body'];
				}
			}
			
			$serverresponsearray = json_decode($serverresponse, true);

			if($serverresponse=='' || !is_array($serverresponsearray)){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch limit error
			if($serverresponsearray['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the Etsy product URL you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch error
			if($serverresponsearray['result']['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			//made it this far assume we have reviews
			$crawlerresultarray = $serverresponsearray['result'];
			
			if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0004 Etsy: trouble finding reviews. Contact support with this error code and the Etsy product URL you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
	$reviewsarray = $crawlerresultarray['reviews'];
	$result['total'] = $crawlerresultarray['total'];
	$result['avg'] = $crawlerresultarray['avg'];
	
	// Store rating breakdown if available from Etsy modal histogram
	if(isset($crawlerresultarray['rating_breakdown'])){
		$result['rating_breakdown'] = $crawlerresultarray['rating_breakdown'];
	}
	
	//pass back stoploop if set by crawler
		if(isset($crawlerresultarray['stoploop'])){
			$result['stoploop'] = $crawlerresultarray['stoploop'];
		}
		
	} else {
		$result['ack'] ='error';
		$result['ackmsg'] = esc_html__('Error 100: Please enter a valid Etsy product URL.', 'wp-review-slider-pro');
	}
		
		$result['crawlserver'] ='remote';
		
		//map Etsy crawler response to plugin format
		$reviewsarrayfinal = array();
		foreach( $reviewsarray as $item){
			
			//extract reviewer_id from review_id if available
			$intreviewer_id = '';
			if(isset($item['review_id']) && $item['review_id']!=''){
				$intreviewer_id = filter_var($item['review_id'], FILTER_SANITIZE_NUMBER_INT);
			}
			
			//convert timestamp to datetime string
			$updatedtimestring = '';
			if(isset($item['created_time_stamp']) && $item['created_time_stamp']!=''){
				$updatedtimestring = date( "Y-m-d H:i:s", $item['created_time_stamp'] );
			} else if(isset($item['created_time']) && $item['created_time']!=''){
				//fallback to parsing created_time string
				$updatedtimestring = date( "Y-m-d H:i:s", strtotime($item['created_time']) );
			}
			
			//use media URLs JSON directly from crawler (already formatted)
			$mediaurlsarrayjson = isset($item['mediaurlsarrayjson']) ? $item['mediaurlsarrayjson'] : '';
			
			//use metadata JSON directly from crawler (already formatted)
			$metadatajson = isset($item['meta_data']) ? $item['meta_data'] : '';
			
			//map verified_purchase to verified_order field
			$verified_order = '';
			if(isset($item['verified_purchase']) && $item['verified_purchase']=='yes'){
				$verified_order = 'yes';
			}
			
			//build review record
			$reviewsarrayfinal[] = [
			 'reviewer_name' => isset($item['reviewer_name']) ? $item['reviewer_name'] : '',
			 'reviewer_id' => $intreviewer_id,
			 'reviewer_email' => '',
			 'userpic' => isset($item['userpic']) ? $item['userpic'] : '',
			 'rating' => isset($item['rating']) ? $item['rating'] : 0,
			 'updated' => $updatedtimestring,
			 'review_text' => isset($item['review_text']) ? $item['review_text'] : '',
			 'review_title' => isset($item['review_title']) ? $item['review_title'] : '',
			 'from_url' => $listedurl,
			 'from_url_review' => '',
			 'from_url_author' => '',
			 'language_code' => '',
			 'location' => '',
			 'recommendation_type' => '',
			 'company_title' =>  '',
			 'company_url' => '',
			 'company_name' => isset($item['pagename']) ? $item['pagename'] : 'Etsy',
			 'mediaurlsarrayjson' => $mediaurlsarrayjson,
			 'meta_data' => $metadatajson,
			 'verified_order' => $verified_order,
			 'unique_id' => isset($item['review_id']) ? $item['review_id'] : ''
			 ];

		}

	$result['reviews'] = $reviewsarrayfinal;
	$result['stoploop']='stop';
	return $result;
}

//for calling remote get and returning array of reviews to insert for Redfin
public function wprp_getapps_getrevs_page_redfin($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron){
	$result['ack']='success';
	set_time_limit(150);
	$errormsg='';
	$reviewsarray = Array();
	$nhful='new';
	
	if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
		$stripvariableurl = stripslashes($listedurl);
		$listedurl = strtok($stripvariableurl, '?');
		
		if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
			$ip_server = $_SERVER['SERVER_ADDR'];
		} else {
			$ip_server = urlencode(get_site_url());
		}
		$siteurl = urlencode(get_site_url());
		
		//build crawler server URL - always using remote for Redfin
		$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=redfin&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron;

		$serverresponse='';
		$args = array(
			'timeout'     => 120,
			'sslverify' => false,
			'headers' => array( 
				'Content-Type' => ' application/json',
				'Accept'=> 'application/json'
			)
		); 
		$response = wp_remote_get( $tempurlval, $args );
		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
			$headers = $response['headers'];
			$serverresponse    = $response['body'];
		} else {
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
			$results = json_encode($results);
			echo $results;
			die();
		}

		//check for block or timeout - try backup server
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
			//call backup digital ocean server
			$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=redfin&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers'];
				$serverresponse    = $response['body'];
			}
		}
		
		$serverresponsearray = json_decode($serverresponse, true);

		if($serverresponse=='' || !is_array($serverresponsearray)){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		//catch limit error
		if($serverresponsearray['ack']=='error'){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
			$results = json_encode($results);
			echo $results;
			die();
		}
		if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the Redfin agent URL you are using.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		//catch error
		if($serverresponsearray['result']['ack']=='error'){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
			$results = json_encode($results);
			echo $results;
			die();
		}
		//made it this far assume we have reviews
		$crawlerresultarray = $serverresponsearray['result'];
		
		if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0004 Redfin: trouble finding reviews. Contact support with this error code and the Redfin agent URL you are using.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		
	$reviewsarray = $crawlerresultarray['reviews'];
	$result['total'] = $crawlerresultarray['total'];
	$result['avg'] = $crawlerresultarray['avg'];
	
	// No rating breakdown for Redfin - will be calculated from downloaded reviews
	
	//pass back stoploop if set by crawler
		if(isset($crawlerresultarray['stoploop'])){
			$result['stoploop'] = $crawlerresultarray['stoploop'];
		}
		
	} else {
		$result['ack'] ='error';
		$result['ackmsg'] = esc_html__('Error 100: Please enter a valid Redfin agent URL.', 'wp-review-slider-pro');
	}
	
	$result['crawlserver'] ='remote';
	
	//map Redfin crawler response to plugin format
	$reviewsarrayfinal = array();
	foreach( $reviewsarray as $item){
		
		//extract reviewer_id from review_id if available
		$intreviewer_id = '';
		if(isset($item['review_id']) && $item['review_id']!=''){
			$intreviewer_id = filter_var($item['review_id'], FILTER_SANITIZE_NUMBER_INT);
		}
		
		//convert timestamp to datetime string
		$updatedtimestring = '';
		if(isset($item['created_time_stamp']) && $item['created_time_stamp']!=''){
			$updatedtimestring = date( "Y-m-d H:i:s", $item['created_time_stamp'] );
		} else if(isset($item['created_time']) && $item['created_time']!=''){
			//fallback to parsing created_time string
			$updatedtimestring = date( "Y-m-d H:i:s", strtotime($item['created_time']) );
		}
		
		//use media URLs JSON directly from crawler (already formatted)
		$mediaurlsarrayjson = isset($item['mediaurlsarrayjson']) ? $item['mediaurlsarrayjson'] : '';
		
		//use metadata JSON directly from crawler (already formatted)
		$metadatajson = isset($item['meta_data']) ? $item['meta_data'] : '';
		
		//build review record
		$reviewsarrayfinal[] = [
		 'reviewer_name' => isset($item['reviewer_name']) ? $item['reviewer_name'] : '',
		 'reviewer_id' => $intreviewer_id,
		 'reviewer_email' => '',
		 'userpic' => isset($item['userpic']) ? $item['userpic'] : '',
		 'rating' => isset($item['rating']) ? $item['rating'] : 0,
		 'updated' => $updatedtimestring,
		 'review_text' => isset($item['review_text']) ? $item['review_text'] : '',
		 'review_title' => isset($item['review_title']) ? $item['review_title'] : '',
		 'from_url' => $listedurl,
		 'from_url_review' => '',
		 'from_url_author' => '',
		 'language_code' => '',
		 'location' => '',
		 'recommendation_type' => '',
		 'company_title' =>  '',
		 'company_url' => '',
		 'company_name' => isset($item['pagename']) ? $item['pagename'] : 'Redfin',
		 'mediaurlsarrayjson' => $mediaurlsarrayjson,
		 'meta_data' => $metadatajson,
		 'verified_order' => '',
		 'unique_id' => isset($item['review_id']) ? $item['review_id'] : ''
		 ];

	}

	$result['reviews'] = $reviewsarrayfinal;
	$result['stoploop']='stop';
	return $result;
}

//for calling remote get and returning array of reviews to insert for Fiverr
public function wprp_getapps_getrevs_page_fiverr($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron){
	$result['ack']='success';
	set_time_limit(150);
	$errormsg='';
	$reviewsarray = Array();
	$nhful='new';
	
	if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
		$stripvariableurl = stripslashes($listedurl);
		$listedurl = strtok($stripvariableurl, '?');
		
		if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
			$ip_server = $_SERVER['SERVER_ADDR'];
		} else {
			$ip_server = urlencode(get_site_url());
		}
		$siteurl = urlencode(get_site_url());
		
		$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=fiverr&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron;

		$serverresponse='';
		$args = array(
			'timeout'     => 120,
			'sslverify' => false,
			'headers' => array( 
				'Content-Type' => ' application/json',
				'Accept'=> 'application/json'
			)
		); 
		$response = wp_remote_get( $tempurlval, $args );
		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
			$headers = $response['headers'];
			$serverresponse    = $response['body'];
		} else {
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001a: trouble contacting crawling server. '.$response->get_error_message();
			$results = json_encode($results);
			echo $results;
			die();
		}

		//check for block or timeout - try backup server
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='') {
			$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=fiverr&sfp=pro&nobot=1&nhful='.$nhful.'&blocks='.$blockstoinsert.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers'];
				$serverresponse    = $response['body'];
			}
		}
		
		$serverresponsearray = json_decode($serverresponse, true);

		if($serverresponse=='' || !is_array($serverresponsearray)){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: trouble contacting crawling server.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		if($serverresponsearray['ack']=='error'){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002b: trouble finding reviews.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		if($serverresponsearray['result']['ack']=='error'){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0003: '.$serverresponsearray['result']['ackmsg'];
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$crawlerresultarray = $serverresponsearray['result'];
		
		if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0004 Fiverr: trouble finding reviews.';
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$reviewsarray = $crawlerresultarray['reviews'];
		$result['total'] = $crawlerresultarray['total'];
		$result['avg'] = $crawlerresultarray['avg'];
		
		//store rating breakdown if available
		if(isset($crawlerresultarray['rating_breakdown'])){
			$result['rating_breakdown'] = $crawlerresultarray['rating_breakdown'];
		}
		
		if(isset($crawlerresultarray['stoploop'])){
			$result['stoploop'] = $crawlerresultarray['stoploop'];
		}
		
	} else {
		$result['ack'] ='error';
		$result['ackmsg'] = esc_html__('Error 100: Please enter a valid Fiverr gig URL.', 'wp-review-slider-pro');
	}
	
	$result['crawlserver'] ='remote';

	//map Fiverr crawler response to plugin format
	$reviewsarrayfinal = array();
	foreach( $reviewsarray as $item){
		$intreviewer_id = '';
		if(isset($item['review_id']) && $item['review_id']!=''){
			$intreviewer_id = filter_var($item['review_id'], FILTER_SANITIZE_NUMBER_INT);
		}
		
		$updatedtimestring = '';
		if(isset($item['created_time_stamp']) && $item['created_time_stamp']!=''){
			$updatedtimestring = date( "Y-m-d H:i:s", $item['created_time_stamp'] );
		} else if(isset($item['created_time']) && $item['created_time']!=''){
			$updatedtimestring = date( "Y-m-d H:i:s", strtotime($item['created_time']) );
		}
		
		$mediaurlsarrayjson = isset($item['mediaurlsarrayjson']) ? $item['mediaurlsarrayjson'] : '';
		$metadatajson = isset($item['meta_data']) ? $item['meta_data'] : '';
		
		$reviewsarrayfinal[] = [
		 'reviewer_name' => isset($item['reviewer_name']) ? $item['reviewer_name'] : '',
		 'reviewer_id' => $intreviewer_id,
		 'reviewer_email' => '',
		 'userpic' => isset($item['userpic']) ? $item['userpic'] : '',
		 'rating' => isset($item['rating']) ? $item['rating'] : 0,
		 'updated' => $updatedtimestring,
		 'review_text' => isset($item['review_text']) ? $item['review_text'] : '',
		 'review_title' => '',
		 'from_url' => $listedurl,
		 'from_url_review' => '',
		 'from_url_author' => '',
		 'language_code' => '',
		 'location' => isset($item['country']) ? $item['country'] : '',
		 'recommendation_type' => '',
		 'company_title' =>  '',
		 'company_url' => '',
		 'company_name' => '',
		 'type' => 'Fiverr',
		 'mediaurlsarrayjson' => $mediaurlsarrayjson,
		 'meta_data' => $metadatajson,
		 'verified_order' => 'yes',
		 'unique_id' => isset($item['review_id']) ? $item['review_id'] : ''
		 ];
	}

	$result['reviews'] = $reviewsarrayfinal;
	$result['stoploop']='stop';
	return $result;
}

//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
public function wprp_getapps_getrevs_page_tripadvisor($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron){
		$result['ack']='success';
		set_time_limit(150);
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			
			if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
					
				$stripvariableurl = stripslashes($listedurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				
				if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
					$ip_server = $_SERVER['SERVER_ADDR'];
				} else {
					//get url of site.
					$ip_server = urlencode(get_site_url());
				}
				$siteurl = urlencode(get_site_url());
				
				//scrapeurl
				$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=tripadvisor&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron;
				
				//https://crawl.ljapps.com/crawlrevs?rip=127.0.0.1&surl=https%3A%2F%2Fwptest.ljapps.com&scrapeurl=https://www.tripadvisor.com/ShowUserReviews-g187371-d217169-r855068628-Hotel_Lyskirchen_Cologne-Cologne_North_Rhine_Westphalia.html&stype=tripadvisor&sfp=pro&nhful=new&locationtype=&scrapequery=&tempbusinessname=&pagenum=1&nextpageurl=&iscron=no
				
				//echo $tempurlval;
				//die();
				
				$serverresponse='';
				$args = array(
					'timeout'     => 120,
					'sslverify' => false,
					'headers' => array( 
						'Content-Type' => ' application/json',
						'Accept'=> 'application/json'
					)
				); 
				$response = wp_remote_get( $tempurlval, $args );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$serverresponse    = $response['body']; // use the content
				} else {
					//must have been an error
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
					$results = json_encode($results);
					echo $results;
					die();
				}

		//check for block or timeout
			//====================
			if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
			//this site is greylisted by imunify360 on cloudways, call backup digital ocean server
			$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=tripadvisor&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.$iscron, array( 'sslverify' => false, 'timeout' => 60 ) );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$serverresponse    = $response['body']; // use the content
				}
			}
			//=========================
		
				$serverresponsearray = json_decode($serverresponse, true);

				if($serverresponse=='' || !is_array($serverresponsearray)){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch limit error
				if($serverresponsearray['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch error
				if($serverresponsearray['result']['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				//made it this far assume we have reviews.
				$crawlerresultarray = $serverresponsearray['result'];
				
				//pass back forceloop if set
				$result['forceloop']='';
				if(isset($crawlerresultarray['forceloop'])){
					$result['forceloop']=$crawlerresultarray['forceloop'];
				}
				
				if($result['forceloop']!='yes'){
					if(!isset($crawlerresultarray['reviews']) || !is_array($crawlerresultarray['reviews'])){
						$results['ack'] ='error';
						$results['ackmsg'] ='Error 0004 Trip: trouble finding reviews. Contact support with this error code and the search terms or place id you are using. ';
						$results = json_encode($results);
						echo $results;
						die();
					}
				}
				
				//need totals and avg for this place $getreviewsarray['total']
				$result['total']='';
				$result['avg']='';
				if(isset($crawlerresultarray['avg'])){
					$result['avg']=$crawlerresultarray['avg'];
				}
				if(isset($crawlerresultarray['total'])){
					$result['total']=$crawlerresultarray['total'];
				}
				
				//pass back URL used
				if(isset($crawlerresultarray['callurl'])){
					$result['callurl']=$crawlerresultarray['callurl'];
				}
				//pass back next URL used
				if(isset($crawlerresultarray['nextpageurl'])){
					$result['nextpageurl']=$crawlerresultarray['nextpageurl'];
				}
				//pass back stoploop if set
				if(isset($crawlerresultarray['stoploop'])){
					$result['stoploop']=$crawlerresultarray['stoploop'];
				}
				//pass back jumpnum if set
				$result['jumpnum']='';
				if(isset($crawlerresultarray['jumpnum'])){
					$result['jumpnum']=$crawlerresultarray['jumpnum'];
				}
				//pass back proxy if set
				$result['proxy']='';
				if(isset($crawlerresultarray['proxy'])){
					$result['proxy']=$crawlerresultarray['proxy'];
				}
				
				$x=0;
				$reviewsarray= Array();
				$crawlerreviewsarray = Array();
				if(isset($crawlerresultarray['reviews']) && is_array($crawlerresultarray['reviews'])){
					$crawlerreviewsarray = $crawlerresultarray['reviews'];
				}
				$numreturned = count($crawlerreviewsarray);
				
				foreach ($crawlerreviewsarray as $review) {
					
					$tempownerres='';
					if(isset($review['owner_response']) && $review['owner_response']!=''){
						$tempownerres = $review['owner_response'];
					}
					$templocation ='';
					if(isset($review['location']) && $review['location']!=''){
						$templocation = $review['location'];
					}	
					$tempmediaurlsarrayjson ='';
					if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
						$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
					}					
					
					$reviewsarray[] = [
					 'reviewer_name' => $review['user_name'],
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $review['userpic'],
					 'rating' => $review['rating'],
					 'updated' => $review['created_time'],
					 'review_text' => $review['review_text'],
					 'review_title' => $review['review_title'],
					 'from_url' => $listedurl,
					 'from_url_review' => $review['user_link'],
					 'language_code' => '',
					 'location' => $templocation,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
					 'owner_response' => $tempownerres,
					 ];
					
					$x++;
				}

			$result['reviews'] = $reviewsarray;

			}
		return $result;
	}
	
	
	//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
	public function wprp_getapps_getrevs_page_greatrecruiter_local($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		
		set_time_limit(120);
		
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			$reviewsarray= Array();
			
		if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
			
			$stripvariableurl = strtok($listedurl, '?');
			$urlvalue = $stripvariableurl;
			
			$parseurl = parse_url($stripvariableurl);
			$baseurl = $parseurl['scheme'].'://'.$parseurl['host'];
			
			if($pagenum > 1){
				$urlvalue = $stripvariableurl.'?page='.$pagenum;
			}
			
			$fileurlcontents = $this->wprp_browser_curl($urlvalue);

			if($pagenum==1){
				$result['avg'] = $this->get_string_between($fileurlcontents, '<span class="score  star">','</span>');
				$result['total'] = $this->get_string_between($fileurlcontents, "<h6 class='profile-text-style'>from ", 'reviews</h6>');
				$result['avg'] = str_replace(' ','',$result['avg']);
				$result['total'] = str_replace(' ','',$result['total']);

			}
			
			$html = wppro_str_get_html($fileurlcontents);
			
			
			$i = 1;
			$reviews = [];
			//print_r($html->find('div.review--with-sidebar'));
			//die();
			//look for review div.
			$reviewdivs = new stdClass();
			if($html->find('div[class=reviews]')){
				$reviewdivs = $html->find('div[class=reviews]');
			}

			
			if(count( (array)$reviewdivs) <1){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble finding reviews. Please try again or contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			
			foreach ($reviewdivs as $review) {
				if ($i > 21) {
						break;
				}
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$location='';
				// Find user_name
				if($review->find('span[class=review-candidate-name]', 0)){
					$user_name = $review->find('span[class=review-candidate-name]', 0)->plaintext;
				}
					
				//find user profile link if we can. this link is for the linking the avatar on the review.
				$from_url_review='';
				$location='';

				
				// find rating 
				if($review->find('span[class=score  star]', 0)){
					$rating = $review->find('span[class=score  star]', 0)->plaintext;
					$rating = intval($rating);
				}
				// find date
				if($review->find('p[class=light]', 0)){
					$datesubmitted = $review->find('p[class=light]', 0)->plaintext;

				}
				
				// find text
				if($review->find('div.review-information', 0)){
					$rtext = $review->find('div.review-information', 0)->plaintext;
				}
				
				$company_title="";
				if($review->find('span[class=review-candidate]', 0)){
					$company_title = $review->find('span[class=review-candidate]', 0)->plaintext;
				}
				
/*
				echo "<br><br>";
				echo "<br>--".$user_name;
				echo "<br>--".$userimage;
				echo "<br>--".$rating;
				echo "<br>--".$datesubmitted;
				echo "<br>--".$rtext;
				echo "<br>--".$company_title;
*/
				
				if($rating>0 && $user_name!=''){
					$review_length = str_word_count($rtext);
					$timestamp = strtotime($datesubmitted);
					$timestamp = date("Y-m-d H:i:s", $timestamp);

					$reviewsarray[] = [
					 'reviewer_name' => $user_name,
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $userimage,
					 'rating' => $rating,
					 'updated' => $timestamp,
					 'review_text' => $rtext,
					 'review_title' => '',
					 'from_url' => $stripvariableurl,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' => $company_title,
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => "",
					 'owner_response' => "",
					 ];
					$review_length ='';
				}
		 
				$i++;
			}
				
				
		//if we find less than 10 then do not loop again.
			if(count($reviewsarray)<10){
				$result['stoploop']='stop';
			}

			$result['reviews'] = $reviewsarray;

		}
			if (!empty($html)) {
				$html->clear();
				unset($html);
			}
			
		return $result;
		
	}
	
	
	//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
	public function wprp_getapps_getrevs_page_yelp_local($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		
		set_time_limit(120);
		
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			$reviewsarray= Array();
			
		if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
			
			$stripvariableurl = strtok($listedurl, '?');
			$urlvalue = $stripvariableurl.'?sort_by=date_desc';
			
			$parseurl = parse_url($stripvariableurl);
			$baseurl = $parseurl['scheme'].'://'.$parseurl['host'];
			
			if($pagenum > 1){
				$pagenumtemp = $pagenum-1;
				$urlvalue = $stripvariableurl.'?start='.$pagenumtemp.'0&sort_by=date_desc';
			}
			
			if (ini_get('allow_url_fopen') == true) {
				$fileurlcontents=file_get_contents($urlvalue);
			} else if (function_exists('curl_init')) {
				$fileurlcontents=$this->file_get_contents_curl($urlvalue);
			} else {
				$fileurlcontents='<html><body>fopen is not allowed on this host.</body></html>';
				$errormsg = $errormsg . ' <p style="color: #A00;">fopen is not allowed on this host and cURL did not work either. Please ask your hosting provided to turn fopen on or fix cURL.</p>';
				$this->errormsg = $errormsg;
				die();
			}
			
			//echo $fileurlcontents;
			//die();
			
			if($pagenum==1){
				$result['total'] = $this->get_string_between($fileurlcontents, '"reviewCount":', '}');
				$result['avg'] = $this->get_string_between($fileurlcontents, '"AggregateRating","ratingValue":', ',');
				if($result['avg'] ==""){
					if($html->find('div[class=five-stars--large__09f24__Waiqf]', 0)){
						$result['avg'] = $html->find('div[class=five-stars--large__09f24__Waiqf]', 0)->{'aria-label'};
						$result['avg'] = preg_replace("/[^0-9\.]/", "",$result['avg']);
					}
				}
				if($result['total'] ==""){
					if($html->find('span[class=css-1x9ee72]', 0)){
						$result['total'] = $html->find('span[class=css-1x9ee72]', 0)->plaintext;
						$result['total'] = preg_replace("/[^0-9\.]/", "",$result['total']);
					}
				}
				$result['avg'] = str_replace('"','',$result['avg']);
				$result['total'] = str_replace('"','',$result['total']);

			}

			$html = wppro_str_get_html($fileurlcontents);
			
			
			$i = 1;
			$reviews = [];
			//print_r($html->find('div.review--with-sidebar'));
			//die();
			//look for review div.
			$reviewdivs = new stdClass();
			if($html->find('div[class=review__09f24__oHr9V]')){
				$reviewdivs = $html->find('div[class=review__09f24__oHr9V]');
			}
			if(count( (array)$reviewdivs) <1){
				if($html->find('li[class=css-1q2nwpv]')){
					$reviewdivs = $html->find('li[class=css-1q2nwpv]');
				}
			}

			
			//another change on 4/26
			if(count( (array)$reviewdivs) <1){
				if($html->find('li[class=yelp-emotion-1jp2syp]')){
					$reviewdivs = $html->find('li[class=yelp-emotion-1jp2syp]');
				}
			}
			
			//another change on 5/17  y-css-1jp2syp
					if(count( (array)$reviewdivs) <1){
						//echo "shere";
						if($html->find('li[class=y-css-1jp2syp]')){
							$reviewdivs = $html->find('li[class=y-css-1jp2syp]');
						}
					}
					
					//another change on 9/23  y-css-mu4kr5
					if(count( (array)$reviewdivs) <1){
						//echo "shere";
						if($html->find('li[class=y-css-mu4kr5]')){
							$reviewdivs = $html->find('li[class=y-css-mu4kr5]');
						}
					}
			
			if(count( (array)$reviewdivs) <1){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble finding reviews. Please try again, switch to remote crawl, or contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			
			foreach ($reviewdivs as $review) {
				if ($i > 21) {
						break;
				}
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$location='';
				// Find user_name
				if($review->find('a.user-display-name', 0)){
					$user_name = $review->find('a.user-display-name', 0)->plaintext;
				}
				if($user_name ==''){
					if($review->find('span[class=fs-block css-ux5mu6]', 0)){
						$user_name = $review->find('span[class=fs-block css-ux5mu6]', 0)->find('a', 0)->plaintext;
					}
				}
				
				
				if($user_name ==''){
					if($review->find('div[class=user-passport-info]', 0)){
						$user_name = $review->find('div[class=user-passport-info]', 0)->find('a', 0)->plaintext;
					}
				}
				
				if($user_name ==''){
					if($review->find('span[class=fs-block yelp-emotion-1m3btbh]', 0)){
						$user_name = $review->find('span[class=fs-block yelp-emotion-1m3btbh]', 0)->find('a', 0)->plaintext;
					}
				}
				if($user_name ==''){
					if($review->find('span[class=y-css-w3ea6v]', 0)){
						$user_name = $review->find('span[class=y-css-w3ea6v]', 0)->find('a', 0)->plaintext;
					}
				}
				
				//find user profile link if we can. this link is for the linking the avatar on the review.
				$from_url_review='';
				if($review->find('a[class=css-1fkqezt]', 0)){
					$from_url_review = $baseurl.$review->find('a[class=css-1fkqezt]', 0)->href;
				}
				if($from_url_review ==''){
					if($review->find('yelp-emotion-idvn5q', 0)){
						$from_url_review = $baseurl.$review->find('yelp-emotion-idvn5q', 0)->href;
					}
				}
				
				$location='';
				if($review->find('div[class=responsive-hidden-small__09f24__qQFtj]', 0)){
					$location = $review->find('div[class=responsive-hidden-small__09f24__qQFtj]', 0)->plaintext;
				}
				if($location ==''){
					if($review->find('div[class=yelp-emotion-12kfwpw]', 0)){
						$location = $review->find('div[class=yelp-emotion-12kfwpw]', 0)->plaintext;
					}
				}
				 
				
				//die();
				
				// Find userimage  y-css-1k4vfmo
				if($review->find('img.photo-box-img', 0)){
					$userimage = $review->find('img.photo-box-img', 0)->src;
				}
				if($userimage ==''){
					if($review->find('img[class=y-css-1k4vfmo]', 0)){
						$userimage = $review->find('img[class=y-css-1k4vfmo]', 0)->src;
					}
				}
				if($userimage ==''){
					if($review->find('img[class=lemon--img__373c0__3GQUb photo-box-img__373c0__O0tbt]', 0)){
						$userimage = $review->find('img[class=lemon--img__373c0__3GQUb photo-box-img__373c0__O0tbt]', 0)->src;
					}
				}
				if($userimage ==''){
					if($review->find('img[class=css-1pz4y59]', 0)){
						$userimage = $review->find('img[class=css-1pz4y59]', 0)->src;
					}
				}
				if($userimage ==''){
					if($review->find('img[class=yelp-emotion-1k4vfmo]', 0)){
						$userimage = $review->find('img[class=yelp-emotion-1k4vfmo]', 0)->src;
					}
				}
				
				if($userimage==""){
					$userimage='"https://s3-media0.fl.yelpcdn.com/assets/srv0/yelp_styleguide/514f6997a318/assets/img/default_avatars/user_60_square.png';
					
				}
				
				// find rating
				if($review->find('div.rating-large', 0)){
					$rating = $review->find('div.rating-large', 0)->title;
					$rating = intval($rating);
				}
				if($rating ==''){
					if($review->find("div[class*=i-stars--regular-]", 0)){
						$rating = $review->find("div[class*=i-stars--regular-]", 0)->{'title'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=five-stars__09f24__mBKym]", 0)){
						$rating = $review->find("div[class=five-stars__09f24__mBKym]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=css-14g69b3]", 0)){
						$rating = $review->find("div[class=css-14g69b3]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=css-14g69b3]", 0)){
						$rating = $review->find("div[class=css-14g69b3]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=yelp-emotion-9tnml4]", 0)){
						$rating = $review->find("div[class=yelp-emotion-9tnml4]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=y-css-9tnml4]", 0)){
						$rating = $review->find("div[class=y-css-9tnml4]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}
				if($rating ==''){
					if($review->find("div[class=y-css-1jwbncq]", 0)){
						$rating = $review->find("div[class=y-css-1jwbncq]", 0)->{'aria-label'};
						$rating = intval($rating);
					}
				}

				// find date
				if($review->find('span.rating-qualifier', 0)){
					$datesubmitted = $review->find('span.rating-qualifier', 0)->plaintext;
					$datesubmitted = str_replace(array("Updated", "review"), "", $datesubmitted);
				}
				if($datesubmitted ==''){
					if($review->find('span[class=lemon--span__373c0__3997G text__373c0__2pB8f text-color--mid__373c0__3G312 text-align--left__373c0__2pnx_]', 0)){
						$datesubmitted = $review->find('span[class=lemon--span__373c0__3997G text__373c0__2pB8f text-color--mid__373c0__3G312 text-align--left__373c0__2pnx_]', 0)->plaintext;
					}
				}
				if($datesubmitted ==''){
					if($review->find('span[class=css-chan6m]', 0)){
						$datesubmitted = $review->find('span[class=css-chan6m]', 0)->plaintext;
					}
				}
				if($datesubmitted ==''){
					if($review->find('span[class=yelp-emotion-v293gj]', 0)){
						$datesubmitted = $review->find('span[class=yelp-emotion-v293gj]', 0)->plaintext;
					}
				}
				if($datesubmitted ==''){
					if($review->find('span[class=y-css-wfbtsu]', 0)){
						$datesubmitted = $review->find('span[class=y-css-wfbtsu]', 0)->plaintext;
					}
				}

				
				// find text
				if($review->find('div.review-content', 0)){
					$rtext = $review->find('div.review-content', 0)->find('p', 0)->plaintext;
				}
				if($rtext ==''){
					if($review->find('p.comment__373c0__3EKjH', 0)){
					$rtext = $review->find('p.comment__373c0__3EKjH', 0)->plaintext;
					}
				}
				if($rtext ==''){
					if($review->find('p[class=comment__09f24__gu0rG]', 0)){
					$rtext = $review->find('p[class=comment__09f24__gu0rG]', 0)->plaintext;
					}
				}
				if($rtext ==''){
					if($review->find('p[class=comment__09f24__D0cxf css-qgunke]', 0)){
					$rtext = $review->find('p[class=comment__09f24__D0cxf css-qgunke]', 0)->plaintext;
					}
				}
				if($rtext ==''){
					if($review->find('span[class=raw__09f24__T4Ezm]', 0)){
					$rtext = $review->find('span[class=raw__09f24__T4Ezm]', 0)->plaintext;
					}
				}
				
				//look for owner comments. block-quote__09f24__qASfJ css-kjl932
				//block-quote__09f24__qASfJ yelp-emotion-cckqp0
				$ownerresponsearrayjson ='';
				if($review->find('div[class=block-quote__09f24__qASfJ]', 0)){
					
					$ownerresponsediv = $review->find('div[class=block-quote__09f24__qASfJ]', 0);
					
					//make sure this isn't a previous review, must not have the stars.
					if($ownerresponsediv->find('div[class=five-stars__09f24__mBKym]', 0)){
						//could be previous review so we skip.
						$ownerresponsearray ='';
					} else {
						$ownerresponsearray = [];
						$ownerresponsearray['id']='';
						
						$ownerresponsearray['name']='';
						if($ownerresponsediv->find('p[class=css-ux5mu6"]', 0)){
							$ownerresponsearray['name']=$ownerresponsediv->find('p[class=css-ux5mu6"]', 0)->plaintext;
						}
						if($ownerresponsediv->find('p[class=css-chan6m"]', 0)){
							$ownerresponsearray['name']=$ownerresponsearray['name'].', '.$ownerresponsediv->find('p[class=css-chan6m"]', 0)->plaintext;
						}
						// yelp-emotion-1m3btbh
						if($ownerresponsearray['name']==''){
							if($ownerresponsediv->find('p[class=yelp-emotion-1m3btbh"]', 0)){
								$ownerresponsearray['name']=$ownerresponsediv->find('p[class=yelp-emotion-1m3btbh"]', 0)->plaintext;
							}
						}
						if($ownerresponsearray['name']==''){
							$ownerresponsearray['name']='Owner';
						}
						$ownerresponsearray['date']='';
						if($ownerresponsediv->find('p[class=css-chan6m"]', 1)){
							$ownerresponsearray['date']=$ownerresponsediv->find('p[class=css-chan6m"]', 1)->plaintext;
						}
						if($ownerresponsearray['date']==''){
							if($ownerresponsediv->find('div[class=yelp-emotion-1j7tr06"]', 0)){
								$ownerresponsearray['date']=$ownerresponsediv->find('div[class=yelp-emotion-1j7tr06"]', 0)->plaintext;
							}
						}

						$ownerresponsearray['comment']='';
						if($ownerresponsediv->find('span[class=raw__09f24__T4Ezm]', 0)){
							$ownerresponsearray['comment']=$ownerresponsediv->find('span[class=raw__09f24__T4Ezm]', 0)->plaintext;
						}

						if($ownerresponsearray['comment']!=''){
						$ownerresponsearrayjson = json_encode($ownerresponsearray);
						}

					}
				}

				
				//----------------
				//add review images here, like google and tripadvisor   ->find('img[class=css-xlzvdl]')
				//mediaurlsarrayjson, max of 8 images,  css-xlzvdl
				//--------------
				$mediaurlsarrayjson='';
				$mediaurlsarray = Array();
				
				if($review->find('div[class=css-1ek5ind]',0)){
					if($review->find('div[class=css-1ek5ind]',0)->find('img[class=css-xlzvdl]')){
						//have at least one image
						$ahrefimagesobject = $review->find('div[class=css-1ek5ind]');
						foreach ($ahrefimagesobject as $imageobj) {
								$mediaimg = $imageobj->find('img[class=css-xlzvdl]',0)->src;
								if($mediaimg!=''){
								$mediaurlsarray[]=$mediaimg;
								}
								if(count($mediaurlsarray)>7){
									break;
								}
							$mediaimg='';
						}
						if(count($mediaurlsarray)>0){
							$mediaurlsarrayjson = json_encode($mediaurlsarray);
						}
						unset($mediaurlsarray);
					}
				}
				
				if($mediaurlsarrayjson=='' || count($mediaurlsarray)<1 ){
					if($review->find('div[class=yelp-emotion-1ax7kgc]',0)) {
					if($review->find('div[class=yelp-emotion-1ax7kgc]',0)->find('img[class=yelp-emotion-dy9j94]',0)){
						$ahrefimagesobject = $review->find('div[class=yelp-emotion-1ax7kgc]',0)->find('img[class=yelp-emotion-dy9j94]');
						foreach ($ahrefimagesobject as $imageobj) {
								$mediaimg = $imageobj->src;
								if($mediaimg!=''){
								$mediaurlsarray[]=$mediaimg;
								}
								if(count($mediaurlsarray)>7){
									break;
								}
							$mediaimg='';
						}
						if(count($mediaurlsarray)>0){
							$mediaurlsarrayjson = json_encode($mediaurlsarray);
						}
						unset($mediaurlsarray);
					}
					}
					
				}
				if($mediaurlsarrayjson=='' || count($mediaurlsarray)<1 ){
					if($review->find('div[class=y-css-1azhvrn]',0)) {
					if($review->find('div[class=y-css-1azhvrn]',0)->find('img',0)){
						$ahrefimagesobject = $review->find('div[class=y-css-1azhvrn]');
						foreach ($ahrefimagesobject as $imageobj) {
								$mediaimg = $imageobj->find('img',0)->src;
								if($mediaimg!=''){
								$mediaurlsarray[]=$mediaimg;
								}
								if(count($mediaurlsarray)>7){
									break;
								}
							$mediaimg='';
						}
						if(count($mediaurlsarray)>0){
							$mediaurlsarrayjson = json_encode($mediaurlsarray);
						}
						unset($mediaurlsarray);
					}
					}
				}
				if($mediaurlsarrayjson=='' || count($mediaurlsarray)<1 ){
					if($review->find('div[class=y-css-1lel115]',0)) {
					if($review->find('div[class=y-css-1lel115]',0)->find('img',0)){
						$ahrefimagesobject = $review->find('div[class=y-css-1lel115]');
						foreach ($ahrefimagesobject as $imageobj) {
								$mediaimg = $imageobj->find('img',0)->src;
								if($mediaimg!=''){
								$mediaurlsarray[]=$mediaimg;
								}
								if(count($mediaurlsarray)>7){
									break;
								}
							$mediaimg='';
						}
						if(count($mediaurlsarray)>0){
							$mediaurlsarrayjson = json_encode($mediaurlsarray);
						}
						unset($mediaurlsarray);
					}
					}
				}
				
			
				
/*
				echo "<br><br>";
				echo "<br>--".$user_name;
				echo "<br>--".$userimage;
				echo "<br>--".$rating;
				echo "<br>--".$datesubmitted;
				echo "<br>--".$rtext;
				die();
				*/
				if($rating>0 && $user_name!=''){
					$review_length = str_word_count($rtext);
					$pos = strpos($userimage, 'default_avatars');
					if ($pos === false) {
						$userimage = str_replace("60s.jpg","120s.jpg",$userimage);
					}
					$timestamp = strtotime($datesubmitted);
					$timestamp = date("Y-m-d H:i:s", $timestamp);
					//check option to see if this one has been hidden
					//pull array from options table of yelp hidden
					$yelphidden = get_option( 'wpyelp_hidden_reviews' );
					if(!$yelphidden){
						$yelphiddenarray = array('');
					} else {
						$yelphiddenarray = json_decode($yelphidden,true);
					}
					$this_yelp_val = trim($user_name)."-".strtotime($datesubmitted)."-".$review_length."-Yelp-".$rating;
					if (in_array($this_yelp_val, $yelphiddenarray)){
						$hideme = 'yes';
					} else {
						$hideme = 'no';
					}

					$reviewsarray[] = [
					 'reviewer_name' => $user_name,
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $userimage,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => $rtext,
					 'review_title' => '',
					 'from_url' => $stripvariableurl,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $mediaurlsarrayjson,
					 'owner_response' => $ownerresponsearrayjson,
					 ];
					$review_length ='';
				}
		 
				$i++;
			}
				
				
		//if we find less than 10 then do not loop again.
			if(count($reviewsarray)<10){
				$result['stoploop']='stop';
			}

			$result['reviews'] = $reviewsarray;

		}
			if (!empty($html)) {
				$html->clear();
				unset($html);
			}
			
		return $result;
		
	}	
	
	
	//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
	public function wprp_getapps_getrevs_page_yelp($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron=''){
		$result['ack']='success';
		
		set_time_limit(120);
		
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			$reviewsarray= Array();
			
			if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
					
				if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
					$ip_server = $_SERVER['SERVER_ADDR'];
				} else {
					//get url of site.
					$ip_server = urlencode(get_site_url());
				}
				$siteurl = urlencode(get_site_url());
				
				//scrapeurl
				$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=yelp&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&iscron='.urlencode($iscron);
				
				//echo $tempurlval;
				//die();
				
				$serverresponse='';
				
				$args = array(
					'timeout'     => 120,
					'sslverify' => false,
					'headers' => array( 
						'Content-Type' => ' application/json',
						'Accept'=> 'application/json'
					)
				); 
				$response = wp_remote_get( $tempurlval, $args );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$serverresponse    = $response['body']; // use the content
				} else {
					//must have been an error
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support. '.$response->get_error_message();
					$results = json_encode($results);
					echo $results;
					die();
				}
				
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
		   //this site is greylisted by imunify360 on cloudways, call backup digital ocean server
		   $response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=yelp&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			}
		}
		//=========================
		
				$serverresponsearray = json_decode($serverresponse, true);
				
				//print_r($serverresponsearray);

				if($serverresponse=='' || !is_array($serverresponsearray)){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch limit error
				if($serverresponsearray['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch error
				if($serverresponsearray['result']['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				//made it this far assume we have reviews.
				$crawlerresultarray = $serverresponsearray['result'];
				

				//need totals and avg for this place $getreviewsarray['total']
				$result['total']='';
				$result['avg']='';
				if(isset($crawlerresultarray['avg'])){
					$result['avg']=$crawlerresultarray['avg'];
				}
				if(isset($crawlerresultarray['total'])){
					$result['total']=$crawlerresultarray['total'];
				}
				
				//pass back URL used
				if(isset($crawlerresultarray['callurl'])){
					$result['callurl']=$crawlerresultarray['callurl'];
				}
				//pass back next URL used
				if(isset($crawlerresultarray['nextpageurl'])){
					$result['nextpageurl']=$crawlerresultarray['nextpageurl'];
				}
				//pass back proxy
				if(isset($crawlerresultarray['proxy'])){
					$result['proxy']=$crawlerresultarray['proxy'];
				}
				
				$x=0;
				$crawlerreviewsarray = $crawlerresultarray['reviews'];
				$numreturned = count($crawlerreviewsarray);	
				
				foreach ($crawlerreviewsarray as $review) {
					
					$tempownerres='';
					if(isset($review['owner_response']) && $review['owner_response']!=''){
						$tempownerres = $review['owner_response'];
					}
					$templocation ='';
					if(isset($review['location']) && $review['location']!=''){
						$templocation = $review['location'];
					}	
					$tempmediaurlsarrayjson ='';
					if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
						$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
					}					
					
					$reviewsarray[] = [
					 'reviewer_name' => $review['user_name'],
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $review['userimage'],
					 'rating' => $review['rating'],
					 'updated' => $review['datesubmitted'],
					 'review_text' => $review['rtext'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => $review['from_url_review'],
					 'language_code' => '',
					 'location' => $templocation,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
					 'owner_response' => $tempownerres,
					 ];
					
					$x++;
				}
				
				//if we find less than 10 then do not loop again.
					if(count($reviewsarray)<10){
						$result['stoploop']='stop';
					}

			$result['reviews'] = $reviewsarray;

			}
		return $result;
	}

	//for calling remote crawl server and returning array of Clutch reviews
	public function wprp_getapps_getrevs_page_clutch($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl,$iscron=''){
		$result['ack']='success';

		set_time_limit(120);

		$errormsg='';
		$reviewsarray= Array();

		// Strip all query params and fragments from the entered URL, rebuild clean base URL
		$parsed = parse_url($listedurl);
		$clutchbaseurl = '';
		if($parsed && isset($parsed['scheme']) && isset($parsed['host'])){
			$clutchbaseurl = $parsed['scheme'].'://'.$parsed['host'];
			if(isset($parsed['path'])){
				$clutchbaseurl .= rtrim($parsed['path'], '/');
			}
		}

		if($clutchbaseurl == '' || !filter_var($clutchbaseurl, FILTER_VALIDATE_URL)){
			$result['ack'] = 'error';
			$result['ackmsg'] = 'Error 101: Please enter a valid Clutch profile URL.';
			return $result;
		}

		if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
			$ip_server = $_SERVER['SERVER_ADDR'];
		} else {
			$ip_server = urlencode(get_site_url());
		}
		$siteurl = urlencode(get_site_url());

		$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.urlencode($clutchbaseurl).'&stype=clutch&sfp=pro&nobot=1&nhful=new&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl=&iscron='.urlencode($iscron);

		//echo $tempurlval;
		//die();


		$args = array(
			'timeout'   => 120,
			'sslverify' => false,
			'headers'   => array(
				'Content-Type' => 'application/json',
				'Accept'       => 'application/json'
			)
		);
		$response = wp_remote_get( $tempurlval, $args );
		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
			$serverresponse = $response['body'];
		} else {
			$result['ack']    = 'error';
			$result['ackmsg'] = 'Error 0001a: trouble contacting crawling server. Please try again or contact support. '.$response->get_error_message();
			return $result;
		}

		// Fallback to backup server on bot-block
		if(strpos($serverresponse, 'Please wait while your request is being verified') !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, 'Access denied by Imunify360 bot-protection.') !== false || strpos($serverresponse, '403 Forbidden') !== false){
			$response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.urlencode($clutchbaseurl).'&stype=clutch&sfp=pro&nobot=1&nhful=new&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl=', array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$serverresponse = $response['body'];
			}
		}

		$serverresponsearray = json_decode($serverresponse, true);

		if($serverresponse=='' || !is_array($serverresponsearray)){
			$result['ack']    = 'error';
			$result['ackmsg'] = 'Error 0001: trouble contacting crawling server. Please try again or contact support.';
			return $result;
		}
		if($serverresponsearray['ack']=='error'){
			$result['ack']    = 'error';
			$result['ackmsg'] = 'Error 0002: '.$serverresponsearray['ackmessage'];
			return $result;
		}
		if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
			$result['ack']    = 'error';
			$result['ackmsg'] = 'Error 0002b: trouble finding reviews. Contact support with this error code.';
			return $result;
		}
		if($serverresponsearray['result']['ack']=='error'){
			$result['ack']    = 'error';
			$result['ackmsg'] = 'Error 0003: '.$serverresponsearray['result']['ackmsg'];
			return $result;
		}

		$crawlerresultarray = $serverresponsearray['result'];

		$result['total'] = '';
		$result['avg']   = '';
		if(isset($crawlerresultarray['avg'])){   $result['avg']   = $crawlerresultarray['avg']; }
		if(isset($crawlerresultarray['total'])){ $result['total'] = $crawlerresultarray['total']; }
		if(isset($crawlerresultarray['callurl'])){ $result['callurl'] = $crawlerresultarray['callurl']; }

		$crawlerreviewsarray = isset($crawlerresultarray['reviews']) ? $crawlerresultarray['reviews'] : array();
		$numreturned = count($crawlerreviewsarray);

		foreach($crawlerreviewsarray as $review){
			$reviewsarray[] = [
				'reviewer_name'       => $review['user_name'],
				'reviewer_id'         => '',
				'reviewer_email'      => '',
				'userpic'             => $review['userimage'],
				'rating'              => $review['rating'],
				'updated'             => $review['datesubmitted'],
				'review_text'         => $review['rtext'],
				'review_title'        => '',
				'from_url'            => $listedurl,
				'from_url_review'     => $review['from_url_review'],
				'language_code'       => '',
				'location'            => isset($review['location']) ? $review['location'] : '',
				'recommendation_type' => '',
				'company_title'       => '',
				'company_url'         => '',
				'company_name'        => '',
				'mediaurlsarrayjson'  => '',
				'owner_response'      => '',
			];
		}

		// Stop looping when fewer than 10 reviews are returned (end of reviews)
		if($numreturned < 10){
			$result['stoploop'] = 'stop';
		}

		// Cron: only fetch the first page to save credits
		if($iscron == 'yes'){
			$result['stoploop'] = 'stop';
		}

		$result['reviews'] = $reviewsarray;

		return $result;
	}

	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_birdeye($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();
		
		$businessId = $listedurl;
		$sindex = 0;
		$apikey = get_option('wprevpro_birdeyeapikey_val');
		$result['avg']='';
		$result['total']='';
			
		if($businessId=='' || $apikey==''){
			//must have been an error
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Make sure to enter your API Key and Business ID from Birdeye.';
			$results = json_encode($results);
			echo $results;
			die();
		}

		if($pagenum >1){
			$sindex = $pagenum*100;
		}
		
		//get the total and avg first
		if($pagenum <2){
			//https://api.birdeye.com/resources/v1/review/businessid/businessId/summary?api_key=abcdefgh&statuses=ad
			$callurl = "https://api.birdeye.com/resources/v1/review/businessid/".$businessId."/summary?api_key=".$apikey."&statuses=ad";
			$response = wp_remote_get( $callurl, array( 'timeout' => 10,
					'headers' => array( 'Content-Type' => 'application/json',
                               'Accept'=> 'application/json' ) 
					));
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$body    = $response['body']; // use the content
			} else {
				$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
			}
			$reviewsummary = json_decode($body,true);
			
			if($reviewsummary['sources'] && is_array($reviewsummary['sources'])){
				foreach ($reviewsummary['sources'] as $valuearray) {
					if(isset($valuearray['sourceName'])){
						if($valuearray['sourceName']=='Birdeye'){
							$result['avg']=$valuearray['avgRating'];
							$result['total']=$valuearray['reviewCount'];
						}
					}
				}
			}

		}
		
		//$result = $type.':'.$listedurl.':'.$pagenum.':'.$perpage.':'.$savedpageid.':'.$nhful.':'.$fid.':'.$blockstoinsert;
		//Birdeye:158025200485823:1:100:::324:20
		//https://api.birdeye.com/resources/v1/review/businessId/158025200485823?sindex=0&count=100&api_key=EW36B9hWVECe05pBB96p8WDtNwQktKnx&includeNonAggregatedReviews=false&businessId=158025200485823
			
		$endpoint = 'https://api.birdeye.com/resources/v1/review/businessId/'.$businessId.'?sindex='.$sindex.'&count=100&api_key=EW36B9hWVECe05pBB96p8WDtNwQktKnx&includeNonAggregatedReviews=false&businessId=158025200485823';

		$body = '{
			"sources":["our_website","direct_feedback","birdeye"],
			"allChild":"true",
			"fetchExtraParams":false,
			"needCustomerInfo":false
		}';

		$options = [
			'body'        => $body,
			'headers'     => [
				'Content-Type' => 'application/json',
			],
			'timeout'     => 60,
			'redirection' => 5,
			'blocking'    => true,
			'httpversion' => '1.0',
			'sslverify'   => false,
			'data_format' => 'body',
		];

		$response = wp_remote_post( $endpoint, $options );
		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$fileurlcontents = $response['body'];	
			
		$reviewcontainerdiv = json_decode($fileurlcontents,true);
		
		foreach ($reviewcontainerdiv as $review) {
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$from_url_review='';
				$company_title='';
				$unique_id='';
				$reviewer_id='';
				$title='';
				$reviewer_email='';
				$userpic='';
				$from_url_review='';
				$from_url='';
				$location='';
			
			// Find unique id
			if($review['reviewId']){
				$unique_id=$review['reviewId'];
			}
			
			// Find user_name
			if($review['reviewer']['nickName']){
				$user_name=$review['reviewer']['nickName'];
			}
			if($review['reviewer']['customerId']){
				$reviewer_id=$review['reviewer']['customerId'];
			}
			if($review['reviewer']['emailId']){
				$reviewer_email=$review['reviewer']['emailId'];
			}
			if($review['reviewer']['thumbnailUrl']){
				$userpic=$review['reviewer']['thumbnailUrl'];
			}

			//find rating
			if($review['rating']){
				$rating=$review['rating'];
			}
			
			//find date created_at
			if($review['reviewDate']){
				$datesubmitted=$review['reviewDate'];
			}
			
			//find title
			if($review['title']){
				$title=$review['title'];
			}
			
			//find text
			if($review['comments']){
				$rtext=$review['comments'];
			}
			
			if($review['reviewUrl']){
				$from_url=$review['reviewUrl'];
			}
			
			if($review['uniqueReviewUrl']){
				$from_url_review=$review['uniqueReviewUrl'];
			}
			$citylocation ='';
			$statelocation='';
			if($review['reviewer']['city']){
				$citylocation = $review['reviewer']['city'];
			}
			if($review['reviewer']['state']){
				$statelocation = $review['reviewer']['state'];
			}
			if($citylocation !='' && $statelocation !=''){
				$location = $citylocation.', '.$statelocation;
			} else if($citylocation !='' && $statelocation ==''){
				$location = $citylocation;
			} else if($citylocation =='' && $statelocation !=''){
				$location = $statelocation;
			}
			
			//owner_response
			$owner_response_encode ='';
			$owner['id'] = '';
			$owner['name'] = '';
			$owner['comment'] = '';
			$owner['date'] = '';
			//mgrRspnInline
			if($review['response']){
				//must be a response
				$owner['name'] = 'Business Response';

				//responseDate
				if(isset($review['responseDate'])){
				 $tempdate = $this->myStrtotime($review['responseDate']);	
				} else {
				 $tempdate = $this->myStrtotime($datesubmitted);
				}
				$owner['date'] = date('Y-m-d', $tempdate);
				$owner['comment'] = $review['response'];
			}
			if($owner['comment']!=''){
				$owner_response_encode = json_encode($owner);
			}

			if($rating>0){
				$reviewsarray[] = [
					 'reviewer_name' => trim($user_name),
					 'reviewer_id' => trim($reviewer_id),
					 'reviewer_email' => $reviewer_email,
					 'userpic' => $userpic,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => trim($rtext),
					 'review_title' => trim($title),
					 'from_url' => $from_url,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'unique_id' => $unique_id,
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => $owner_response_encode,
					 ];
			}
		}

		$result['reviews'] = $reviewsarray;
				
		//die();

		return $result;
	}
	
	
	//new Nextdoor 4-6-2024
	
	public function wprp_getapps_getrevs_page_nextdoor($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$rectostar){
			
			
			//try to find page slug. https://nextdoor.com/pages/il-mulino-fort-lauderdale-fl/
			$pageslug = $this->get_string_between($listedurl, '/pages/', '/');

			$endpoint = "https://nextdoor.com/api/gql/OrgPagesRecFeedLogout";
			

			$body = '{"operationName":"OrgPagesRecFeedLogout","variables":{"pagedCommentsMode":"FEED","slug":"'.$pageslug.'","recommendationFeedArgs":{"feedRequestId":"5E07FF34-A2DC-489F-9F6D-B5AC007647E4","nextPage":"eyJwYWdpbmdfdG9rZW4iOiAzLCAic3RhcnRfb2Zmc2V0IjogMywgIm1hdGVyaWFsaXplZF92aWV3X2lkIjogImQ4OGE4NGJkLWE2NDUtNDUyOC04ODU4LTc5OGM0NDAyYTU0YiJ9","pageSize":20},"timeZone":"America/Chicago"},"query":"queryOrgPagesRecFeedLogout($slug:String!,$pagedCommentsMode:PagedCommentsMode=FEED,$pinnedCommentId:NextdoorID,$timeZone:String!,$recommendationFeedArgs:OrgPagesRecommendationsFeedArgs!){\norgPagesFeed(\norgPagesFeedArgs:{businessSlug:$slug,limitResponsesTo:FeedItemOrgPagesRecommendations}\n){\nfeedItems{\nfeedItemType\nlegacyAnalyticsId\ntrackingId\ncontentId\ncontentType\nanalyticsPayload\n...onFeedItemOrgPagesRecommendations{\nmoduleTitleText{\n...styled_text_styledTextFragment\n__typename\n}\nrecommendationsFeed(feedArgs:$recommendationFeedArgs){\nnextPage\nfeedItems{\ncontentId\nfeedItemType\ntrackingId\n...onFeedItemRecommendationsItem{\nfeedItemType\nauthor{\n...logout_businessProfile_AnonymousAuthorFragment\n__typename\n}\nbodyText{\n...styled_text_styledTextFragment\n__typename\n}\nsecondaryText{\n...styled_text_styledTextFragment\n__typename\n}\nsourceId\nsourceLegacyId\nsourceType\nshouldShowDeleteAction\nsourceContextText{\n...styled_text_styledTextFragment\n__typename\n}\nrecFeedPost{\nid\nauthor{\n...logout_businessProfile_AnonymousAuthorFragment\n__typename\n}\nmediaAttachments{\n...logout_businessProfile_MediaAttachmentsFragment\n__typename\n}\nbody\ncomments(mode:$pagedCommentsMode,pinnedCommentId:$pinnedCommentId){\npagedComments{\n...logout_businessProfile_CommentsFragment\n__typename\n}\n__typename\n}\ncreatedAt{\nepochMillis\nasDateTime(timeZone:$timeZone){\nrelativeTime\n__typename\n}\n__typename\n}\neditedAt{\nasDateTime(timeZone:$timeZone){\nrelativeTime\n__typename\n}\n__typename\n}\nfeedItemType\nid\nimageCropType\nlegacyPostId\nphotos{\nurl\n__typename\n}\npostType\nreactionSummaries{\n...feed_post_reactionSummariesFragment\n__typename\n}\nmarkdownBody\nstyledBody{\n...styled_text_styledTextFragment\n__typename\n}\nsubject\n__typename\n}\n__typename\n}\n__typename\n}\nfeedRenderId\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n}\n\nfragmentstyled_text_styledTextFragmentonStyledText{\ntext\nmaxLines\nstyles{\nstart\nlength\nattributes{\nstandardColor{\n...standard_color_standardColorFragment\n__typename\n}\nstandardFont\nisStrikethrough\nstandardIconV2{\n...standard_icon_standardIconV2Fragment\n__typename\n}\nstyledTextUrl\npillColor{\n...standard_color_standardColorFragment\n__typename\n}\naction{\n...standard_action_standardActionFragment\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n\nfragmentstandard_color_standardColorFragmentonStandardColor{\ncolorName\nfallbackColorHEX\n__typename\n}\n\nfragmentstandard_icon_standardIconV2FragmentonStandardIconV2{\nicon\ntint{\n...standard_color_standardColorFragment\n__typename\n}\n__typename\n}\n\nfragmentstandard_action_standardActionFragmentonStandardAction{\nstandardActionType\ntrackingEvent\ntrackingMetadata\n...onAppealsAction{\nnextStep\n__typename\n}\n...onAppealSuspensionAction{\nrestrictionId\nrestrictionReason\n__typename\n}\n...onBlockUserAction{\nblock\nuserId\nuserName\n__typename\n}\n...onLinkAction{\nurl\nurlPath\nnewWindow\nlinkActionType\ntrackingEvent\ntrackingMetadata\nadsTrackingMetadata{\n...ads_adsTrackingMetadataFragment\n__typename\n}\n__typename\n}\n...onShowModerationChoicePageActionV1{\ncontentId\n__typename\n}\n...onShowModerationEventsSummaryActionV1{\neventsSummaryType\n__typename\n}\n...onShowModerationSummaryActionV1{\nviewMode\n__typename\n}\n...onRecommendBusinessPageActionV1{\nbusinessId\nisRecommend\nmodalUrlPath\n__typename\n}\n...onShareInAppFlowAction{\nurl\n__typename\n}\n...onShowUserGroupDetailPostComposerAction{\nuserGroupId\nuserGroupSecureId\nuserGroupName\nuserGroupAbout\npostSubject\npromoName\n__typename\n}\n...onShareGroupAction{\nuserGroupSecureId\nuserGroupName\npromoName\n__typename\n}\n...onUploadCoverPhotoAction{\nuserGroupSecureId\npromoName\n__typename\n}\n...onChangeGroupMembershipAction{\nuserGroupSecureId\nmembershipAction\n__typename\n}\n...onClickToCallAction{\nphoneNumber\ndescription\n__typename\n}\n...onShareGroupToFeedAction{\nuserGroupSecureId\npostSubject\npostMessage\npromoName\n__typename\n}\n...onShowUserGroupCategoriesAction{\nuserGroupSecureId\npromoName\n__typename\n}\n...onContentCreationAction{\nuserGroupSecureId\npostSubject\npostType\npromoName\n__typename\n}\n...onAcceptConnectionRequestAction{\nuserProfile{\nid\n__typename\n}\n__typename\n}\n...onSendConnectionRequestAction{\nuserProfile{\nid\n__typename\n}\n__typename\n}\n...onMessageConnectionAction{\nuserProfile{\nid\nlegacyUserId\ncanonicalChatId\nname{\ndisplayName\n__typename\n}\n__typename\n}\n__typename\n}\n...onMessageAction{\nnextdoorId\ndisplayName\nlegacyId\n__typename\n}\n...onDeleteNotificationAction{\nnotificationId\n__typename\n}\n...onUpdateUserMuteStatusAction{\nuserProfile{\nid\nname{\ngivenName\n__typename\n}\n__typename\n}\nmute\n__typename\n}\n...onMutePostAction{\npostId\nmute\n__typename\n}\n...onFeedItemToggleNotificationAction{\npostId\npostShareId\ntoggle\n__typename\n}\n...onFeedItemToggleMuteUserAction{\ntoggle\nuserId\n__typename\n}\n...onWaveAction{\nuserProfile{\nid\nname{\ngivenName\n__typename\n}\n__typename\n}\n__typename\n}\n...onDiscoverSendUserConnectionRequestAction{\nuserProfile{\nid\nname{\ndisplayName\n__typename\n}\n__typename\n}\n__typename\n}\n...onDiscoverCancelUserConnectionRequestAction{\nuserProfile{\nid\nname{\ndisplayName\n__typename\n}\n__typename\n}\n__typename\n}\n...onDiscoverChangeGroupMembershipAction{\nuserGroupSecureId\nmembershipAction\n__typename\n}\n...onFeedItemAddReactionAction{\ncontentId\nreactionName\n__typename\n}\n...onFeedItemRemoveReactionAction{\ncontentId\nreactionId\n__typename\n}\n...onDeleteBookmarkAndRerender{\ncontentId\ncontentType\nuiComponentId\n__typename\n}\n...onAddVirtualGiftAction{\ncontentId\nvirtualGiftName\n__typename\n}\n...onRemoveVirtualGiftAction{\ncontentId\nvirtualGiftId\n__typename\n}\n...onCopyToClipboardAction{\nvalue\n__typename\n}\n...onOrganizationConnectAction{\norganizationId\nactionToTake\n__typename\n}\n...onSetLocalEventResponseToInterestedAction{\neventId\nuserResponse\n__typename\n}\n...onOpenLocalEventResponsePickerAction{\neventId\nuserResponse\n__typename\n}\n...onSeasonalEventUserResponseAction{\npinVariant\nresponseActionType\nresponseText\nmapType\n__typename\n}\n...onMapRollupPinAction{\npinId\nsheet{\nvariant\n__typename\n}\n__typename\n}\n...onMapCardsPopupAction{\nreferenceCardId\n__typename\n}\n...onReportNodeSelectedAction{\nnode\nreportContentId:contentId\n__typename\n}\n...onReportContentSubmitAction{\ncontentId\ncontentActionReason\ncontentActionType\n__typename\n}\n...onReportUserSubmitAction{\nuserId\nreason\n__typename\n}\n...onReportDeleteAction{\ncontentId\ncontentActionType\n__typename\n}\n...onReportMuteAuthorAction{\ncontentId\nmute\nnextNode\n__typename\n}\n...onReportMutePostAction{\npostId\nmute\nnextNode\n__typename\n}\n...onReportMuteUserAction{\nuserId\nmute\nnextNode\n__typename\n}\n...onOpenPostShareSheetAction{\npost{\nid\nshareId\nlocalEventInfo{\nlocalEvent{\nshareId\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n...onMapOpenPostComposerAction{\nmapType\npinPost:post{\nid\n__typename\n}\n__typename\n}\n...onSocialShareAction{\nshareMetadata{\ntitle\nbody\nurl\n__typename\n}\n__typename\n}\n...onSetLocalEventResponseToGoingAction{\neventId\nuserResponse\n__typename\n}\n...onShowAppDownloadModalAction{\ndeeplinkUrl\nmodalTitle\n__typename\n}\n...onOpenComposerAction{\ncomposerProperties{\ninitSource\nredirectOnSuccess\nsubflow\n__typename\n}\n__typename\n}\n...onClassifiedMessageAction{\nrecipientId\nrecipientDisplayName\nclassifiedId\nlegacyClassifiedId\nsubject\n__typename\n}\n...onUseAISuggestionAction{\nvariant\n__typename\n}\n...onGroupPromoLinkAction{\ngroupSecureId\nurl\nurlPath\nnewWindow\npromoName\nlinkActionType\n__typename\n}\n...onMapFilterContentUpdateAction{\nfilterActionType\nfilterProperty\nmapTypeName\n__typename\n}\n...onDeactivationFlowNextPageAction{\npage\n__typename\n}\n...onUnsuspendUserAction{\nentryPoint\n__typename\n}\n...onHideStreamChannelAction{\nchannelId\n__typename\n}\n...onMuteStreamChannelAction{\nchannelId\nmute\n__typename\n}\n...onLeaveLocalCommunityAction{\ncommunityShareId\n__typename\n}\n...onReportUserAction{\nuserId\nuserSecureId\n__typename\n}\n...onUserFeedbackResponseAction{\nresponseActionNextStep:nextStep\n__typename\n}\n...onUserFeedbackSubmitAction{\nsubmitActionNextStep:nextStep\n__typename\n}\n...onLeaveStreamChannelAction{\nchannelId\n__typename\n}\n...onViewStreamChannelMembersAction{\nchannelId\n__typename\n}\n...onEditStreamChannelAction{\nchannelId\nchannelName\n__typename\n}\n...onDeleteStreamChannelAction{\nchannelId\n__typename\n}\n...onShowGeneralizedFlowAction{\nflowType\nview\n__typename\n}\n...onAddUsersToCommunityStreamChatAction{\nchannelCid\ncommunityShareId\n__typename\n}\n...onEditLocalCommunityAction{\ncommunityShareId\n__typename\n}\n__typename\n}\n\nfragmentads_adsTrackingMetadataFragmentonAdsTrackingMetadata{\nadUnitID\nadFormatID\nproductContext\nmetadata{\nadRequestId\ntrackingId\nlineItemId\ncreativeId\ndemandSource\npassthroughKv\nmetadataType\n...ads_impressionMetadataFragment\n...ads_clickMetadataFragment\n...ads_mediaMetadataFragment\n...ads_actionMetadataFragment\n__typename\n}\n__typename\n}\n\nfragmentads_impressionMetadataFragmentonImpressionMetadata{\nadImpressionId\nadRequestId\nadvertiserID\nbidResponseEntityId\ncreativeId\ndemandSource\nextraData\nimpressionType\nlineItemId\nmetadataType\norderId\npageId\npassthroughKv\nsponsor\ntrackingId\n__typename\n}\n\nfragmentads_clickMetadataFragmentonClickMetadata{\nadRequestId\nadvertiserID\nclickDestinationType\nclickId\ncreativeId\ndemandSource\nextraData\nlineItemId\nmetadataType\norderId\npageId\npassthroughKv\nsponsor\ntrackingId\nuiElementName\n__typename\n}\n\nfragmentads_mediaMetadataFragmentonMediaMetadata{\nadImpressionId\nadMediaId\nadMediaType\nadRequestId\nadvertiserID\ncreativeId\ndemandSource\nextraData\nlineItemId\nmetadataType\norderId\npassthroughKv\ntrackingId\n__typename\n}\n\nfragmentads_actionMetadataFragmentonActionMetadata{\nadActionId\nadCampaignId\nadEventType\nadFunnelId\nadRequestId\nadvertiserID\ncreativeId\ndemandSource\nextraData\nlineItemId\nmetadataType\npassthroughKv\ntrackingId\n__typename\n}\n\nfragmentfeed_post_reactionSummariesFragmentonReactionSummaries{\nsummaries{\ncolor\ncount\ndefaultResources{\ntitle\nsvgUrl\npng42Url\nanimationUrl\n__typename\n}\nname\npopupResources{\ntitle\nsvgUrl\npng42Url\nanimationUrl\n__typename\n}\nselectedResources{\ntitle\nsvgUrl\npng42Url\nanimationUrl\n__typename\n}\nunselectedResources{\ntitle\nsvgUrl\npng42Url\nanimationUrl\n__typename\n}\nuserReactionId\n__typename\n}\n__typename\n}\n\nfragmentlogout_businessProfile_AnonymousAuthorFragmentonAuthor{\navatar{\nurl\n__typename\n}\nawardIcon{\n...AwardIconFragment\n__typename\n}\ndisplayName\n...onAnonymizedAuthor{\nlocationHint\n__typename\n}\n__typename\n}\n\nfragmentAwardIconFragmentonAwardIcon{\nicon{\n...standard_icon_standardIconV2Fragment\n__typename\n}\nbackgroundColor{\n...standard_color_standardColorFragment\n__typename\n}\nborderColor{\n...standard_color_standardColorFragment\n__typename\n}\n__typename\n}\n\nfragmentlogout_businessProfile_CommentsFragmentonPagedCommentsConnection{\nedges{\nnode{\ncomment{\n...logout_businessProfile_CommentFragment\n__typename\n}\nreplies{\nedges{\nnode{\ncomment{\n...logout_businessProfile_CommentFragment\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n__typename\n}\n\nfragmentlogout_businessProfile_MediaAttachmentsFragmentonMediaAttachment{\ntype\nurl\n...onImageMediaAttachment{\nimageSize{\nurl\nwidthPx\nheightPx\n__typename\n}\nimage{\n...images_imageFragment\n__typename\n}\nresourceId\ns3BucketKey\ns3Path\norder\n__typename\n}\n...onVideoMediaAttachment{\ntype\nurl\nresourceId\nvideo{\nvideoMetadata{\nvideoDimensions{\nheightPx\nwidthPx\n__typename\n}\n__typename\n}\nvideoHlsInfo{\nurl\nplaybackUrlParams{\nkey\nvalue\n__typename\n}\n__typename\n}\n__typename\n}\npreviewImage{\n...images_imageFragment\n__typename\n}\norder\n__typename\n}\n...onDocumentMediaAttachment{\ntype\nurl\nresourceId\ntitle\n__typename\n}\n__typename\n}\n\nfragmentimages_imageFragmentonImage{\nurl\nimageMetadata{\naltText\nimageDimensions{\nwidthPx\nheightPx\n__typename\n}\nimageFocalArea{\nleft\ntop\nright\nbottom\n__typename\n}\n__typename\n}\n__typename\n}\n\nfragmentlogout_businessProfile_CommentFragmentonComment{\ncreatedAt{\nepochMillis\nasDateTime(timeZone:$timeZone){\nrelativeTime\n__typename\n}\n__typename\n}\nid\nlegacyCommentId\nbody\nauthor{\n...logout_businessProfile_AnonymousAuthorFragment\n__typename\n}\nreactionSummaries{\nsummaries{\ncount\n__typename\n}\n__typename\n}\nphotos{\nurl\n__typename\n}\nmediaAttachments{\n...logout_businessProfile_MediaAttachmentsFragment\n__typename\n}\n__typename\n}\n"}';
			
			$loop = 1;
			startover:	//will end up here if no reviews found on first loop;
			
				if($loop ==1){
					$endpoint = "https://nextdoor.com/api/gql/OrgPagesFeed?";
					$body = '{"operationName":"OrgPagesFeed","variables":{"pagedCommentsMode":"FEED","includeModerationInfo":true,"activityFeedArgs":{"feedRequestId":"85F016E6-C11B-4BD4-ACFE-DACAAF4C4B46","nextPage":"eyJwb3N0X3RzIjogIjE2NzQzNDk2Nzk4NzIwMDAiLCAicHJvbW9fcGFnZV9kYXRhIjogbnVsbCwgImZlZWRfdHlwZSI6ICIwIiwgIm9yZGVyaW5nX21vZGUiOiAtMn0=","pageSize":10},"slug":"'.$pageslug.'","timeZone":"America/Chicago","feedRequestId":"985E03CC-A31C-4C3B-9667-AB7EC612946B","recNextPage":"","recPageSize":3,"experienceType":"NEIGHBOR_VIEW"},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"372fa180714b0c07a38afe5d420941a446e0a65da5fcc3b4309e958dae03a5c9"}}}';
				}

			$options = [
				'body'        => $body,
				'headers'     => [
					'Content-Type' => 'application/json',
					'Cookie' => 'csrftoken=kfA8vVrdNaEII9og5mXVT3IpG1rfEVNWt1ayOkd69iLt85T8sMfQP59XFl7lHuq3; WE=412a2e8e-52d2-4a91-bcf0-c2462dd8969a240328; _gcl_au=1.1.1248135096.1711588936; _ga=GA1.1.902398086.1711588936; _ga_L2ES4MTTT0=GS1.1.1711588936.1.1.1711588936.60.0.0; ndp_session_id=7dc7821c-bf27-430a-88fa-aea1d77fb44c; _uetsid=9d424780eca111ee808905f097ea6dba; _uetvid=9d424da0eca111eea9c57f9fca9c43f0; FPID=FPID2.2.bfUma0l6WLAAr0fjLUTYElbMHMll41tannLSI49qpMU%3D.1711588936; FPLC=%2FuDsZyEdsYSQ4ArxbtXzlouYLsugIy3umnI7bjbTFBjnR%2FzCrdu76lJB65f8CFG7zE0BrdFLKdav2teufXtrzDDrTdtGtDi40vQmDsn4kSeiJw2gpfisKCsghqUBlQ%3D%3D; _fbp=fb.1.1711588936858.1156574007; OptanonConsent=isGpcEnabled=0&datestamp=Wed+Mar+27+2024+20%3A22%3A16+GMT-0500+(Central+Daylight+Time)&version=202303.2.0&browserGpcFlag=0&isIABGlobal=false&hosts=&consentId=fd359058-5a27-4feb-8d72-30f74e1448b0&interactionCount=0&landingPath=https%3A%2F%2Fnextdoor.com%2Fpages%2F'.$pageslug.'%2F&groups=C0001%3A1%2CC0003%3A1%2CC0004%3A1%2CC0005%3A1%2CC0002%3A1%2CC0007%3A1; _dd_s=logs=1&id=76a691b8-c975-46aa-9451-dd6e57d6b7bc&created=1711588937062&expire=1711589849865&rum=0',
					'Origin' => 'https://nextdoor.com',
					'Referer' => $listedurl,
					'X-Csrftoken' => 'kfA8vVrdNaEII9og5mXVT3IpG1rfEVNWt1ayOkd69iLt85T8sMfQP59XFl7lHuq3'
				],
				'timeout'     => 60,
				'redirection' => 5,
				'blocking'    => true,
				'httpversion' => '1.0',
				'sslverify'   => false,
				'data_format' => 'body',
			];

			$response = wp_remote_post( $endpoint, $options );

			if ( ! is_wp_error( $response ) ) {
				$body = json_decode( wp_remote_retrieve_body( $response ), true );
				//print_r($body);
				//die();
			} else {
				$error_message = $response->get_error_message();
				//throw new Exception( $error_message );
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
				$results = json_encode($results);
				echo $results;
				die();
			}
			//recommendationsFeed
			
			//loop through feedItems and test for [feedItemType] => ORG_PAGES_HEADER
			$feedItems = $body['data']['orgPagesFeed']['feedItems'];
			foreach ($feedItems as $fitem) {
				if($fitem['feedItemType']=="ORG_PAGES_RECOMMENDATIONS"){
					$feedItem = $fitem;
				}
			}
			
			if(!isset($feedItem['recommendationsFeed']['feedItems'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0003: Something went wrong. Can not find the reviews. Please check your URL and contact support. ';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			$reviewsarraytemp = $feedItem['recommendationsFeed']['feedItems'];

			//print_r($reviewsarraytemp);
			//die();
			
			//if nothing found try the other api endpoint.
			//if(count($reviewsarraytemp)<1 && $loop==1){
			//	$loop = 2;
			//	goto startover;
			//}
			
			//loop reviews and build new array of just what we need
			foreach ($reviewsarraytemp as $item) {
				
				//check for comments first if not there then grab top level post.
				if(isset($item['recFeedPost']['comments']['pagedComments']['edges'][0]['node']['comment']['id'])){
					$isreplycomment = true;
					$reviewdataarray = $item['recFeedPost']['comments']['pagedComments']['edges'][0]['node']['comment'];
					//echo "<br><br>here<br>";
					//echo $reviewdataarray;
					//print_r($reviewdataarray);
					//die();
				} else {
					$isreplycomment = false;
					$reviewdataarray = $item['recFeedPost'];
					//echo "<br><br>here1<br>";
					//echo $reviewdataarray;
				//	print_r($reviewdataarray);
					//die();
				}
				$isreplycomment = false;
					$reviewdataarray = $item['recFeedPost'];

				$tempuserpic = '';
				if($reviewdataarray['author']['avatar']['url']){
					$tempuserpic = $reviewdataarray['author']['avatar']['url'];
				}
				$subject = '';
				if($isreplycomment==false){
					if($reviewdataarray['subject']){
						$subject = $reviewdataarray['subject'];
					}
				}
				
				$updatedtimestring = '';
				if($reviewdataarray['createdAt']['epochMillis']){
					$updatedtimestring = $reviewdataarray['createdAt']['epochMillis'];
				}
				
				//echo "<br>timestamp".$updatedtimestring;
				
				//$updatedtimestring = date("Y-m-d H:i:s", $updatedtimestring);
				$updatedtimestring = date("Y-m-d H:i:s", substr($updatedtimestring, 0, 10));
				
				//echo "<br>time".$updatedtimestring;
				
				$rating="";
				if(isset($rectostar) && $rectostar =='1'){
						$rating=5;
				}
				
				$location = "";
				if(isset($reviewdataarray['author']['locationHint'])){
					$location = $reviewdataarray['author']['locationHint'];
				}
				
				 $reviewsarray[] = [
				 'reviewer_name' => trim($reviewdataarray['author']['displayName']),
				 'reviewer_id' => '',
				 'reviewer_email' => '',
				 'userpic' => $tempuserpic,
				 'rating' => $rating,
				 'updated' => $updatedtimestring,
				 'review_title' => $subject,
				 'review_text' => $reviewdataarray['body'],
				 'from_url_review' => '',
				 'language_code' => '',
				 'location' => $location,
				 'recommendation_type' => 'positive',
				 'company_title' =>  '',
				 'company_url' => '',
				 'company_name' => '',
				 ];
			}

			$result['reviews'] = $reviewsarray;
				
		//die();
		$result['stoploop']='stop';

		return $result;
	}	
	

		
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_socialclimb($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();
		
		//get apikey andn survey id from input
		$listedurl = str_replace(" ","",$listedurl);
		$idarray = explode(",",$listedurl);
		
		$apikey = $idarray[0];
		$surveyid = $idarray[1];
		$result['avg']='';
		$result['total']='';
			
		if($apikey==''){
			//must have been an error
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Make sure to enter your API Key and Business ID from Birdeye.';
			$results = json_encode($results);
			echo $results;
			die();
		}

	
		//get the total and avg first===?????anyway to get this?

		$endpoint = 'https://api.socialclimb.com/v1/survey-responses?survey_id='.$surveyid;

		$options = [
			'body'        => '',
			'headers'     => [
				'Content-Type' => 'application/json',
				'api-token' => $apikey,
			],
			'timeout'     => 60,
			'redirection' => 5,
			'blocking'    => true,
			'httpversion' => '1.0',
			'sslverify'   => false,
		];

		$response = wp_remote_get( $endpoint, $options );
		
		//echo "here";
		//print_r($response);
		
		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$fileurlcontents = json_decode($response['body'],true);	
			
		$reviewcontainerdiv = $fileurlcontents['items'];
		
		//print_r($reviewcontainerdiv);
		
		foreach ($reviewcontainerdiv as $review) {
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$from_url_review='';
				$company_title='';
				$unique_id='';
				$reviewer_id='';
				$title='';
				$reviewer_email='';
				$userpic='';
				$from_url_review='';
				$from_url='';
				$location='';
			
			$user_name=$review['user'];
			
			// Find user_name  reviewer_name
			if($review['user']){
				$name = ucwords(strtolower($review['user']));
			}

			if($review['contact']){
				$reviewer_email=$review['contact'];
			}

			//find rating
			if($review['overall']){
				$rating=$review['overall'];
			}
			
			//find date created_at
			if($review['survey_submit_date']){
				$datesubmitted=$review['survey_submit_date'];
			}
			if($datesubmitted==""){
				if($review['survey_date']){
					$datesubmitted=$review['survey_date'];
				}
			}
			
		
			//find text
			if($review['comments']){
				$rtext=$review['comments'];
				$rtext=str_replace("N/A","",$rtext);
			}
			
			if($review['location']){
				$location=$review['location'];
			}
			
			
			$meta_json ="";
			$meta_data = Array();
			if($review['appointment_date']){
				$meta_data['appointment_date'] = sanitize_text_field($review['appointment_date']);
			}

			if($review['provider']){
				$meta_data['provider'] = sanitize_text_field($review['provider']);
			}
			if($review['delays']){
				$meta_data['delays'] = sanitize_text_field($review['delays']);
			}
			if($review['explanation']){
				$meta_data['explanation'] = sanitize_text_field($review['explanation']);
			}
			if($review['home_care']){
				$meta_data['home_care'] = sanitize_text_field($review['home_care']);
			}
			if($review['information']){
				$meta_data['information'] = sanitize_text_field($review['information']);
			}
			if($review['nurses']){
				$meta_data['nurses'] = sanitize_text_field($review['nurses']);
			}
			if($review['physician']){
				$meta_data['physician'] = sanitize_text_field($review['physician']);
			}
			if($review['recommendation']){
				$meta_data['recommendation'] = sanitize_text_field($review['recommendation']);
			}
			if($review['registration']){
				$meta_data['registration'] = sanitize_text_field($review['registration']);
			}
			if($review['response']){
				$meta_data['response'] = sanitize_text_field($review['response']);
			}
			if(count($meta_data)>0){
				$meta_json = json_encode($meta_data);
			}
			
			

			if($rating>0){
				$reviewsarray[] = [
					 'reviewer_name' => $name,
					 'reviewer_id' => $reviewer_id,
					 'reviewer_email' => $reviewer_email,
					 'userpic' => $userpic,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => trim($rtext),
					 'review_title' => trim($title),
					 'from_url' => $from_url,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'unique_id' => $unique_id,
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => '',
					 'meta_data' => $meta_json,
					 'owner_response' => '',
					 ];
			}
			//print_r($reviewsarray);
			//die();
		}

		$result['reviews'] = $reviewsarray;
				
		//print_r($result['reviews']);
		//die();

		return $result;
	}

	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_reviewsio($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		
			$errormsg='';
			$callurl = $listedurl;
			$reviewsarraytemp = Array();
			
			if (filter_var($callurl, FILTER_VALIDATE_URL)) {
					
				$stripvariableurl = stripslashes($callurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				//https://www.reviews.co.uk/company-reviews/store/simplylendingsolutions-co-uk-
				//next page is https://www.reviews.co.uk/company-reviews/store/simplylendingsolutions-co-uk-/1
				//does not work with product reviews
				
				$callurl = $listedurl;
				
				//modify page url if this is $pagenum >1;
				if($pagenum >1){
					if($nextpageurl!=''){
						$callurl =$nextpageurl;
					} else {
						$subtractpage = $pagenum - 1;
						$callurl = $listedurl."/".$subtractpage;
					}
				}

				//echo $callurl;
				$result['callurl'] =$callurl;
				$response = wp_remote_get( $callurl );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
				}
				
				$fileurlcontents = $response['body'];
				$html = wppro_str_get_html($fileurlcontents);
				
				//actually try to get next page, fall back to using above.
				$nexthref='';
				$result['nextpageurl']='';
				if($pagenum >1){
					if($html->find('ul[class=pagination]', 0)){
						$nexthref = $html->find('ul[class=pagination]', 0) -> lastChild() -> find('a', 0)-> href;
					}
				}
				if($nexthref!=''){
					$result['nextpageurl']= $nexthref;
				}
				
				//echo $html;
				//die();
				
				$jsonschema = $this->get_string_between($fileurlcontents, '<script type="application/ld+json">', '</script>');
				$jsonschemaarray = json_decode($jsonschema,true);
				//use array to find values
				
				//print_r($jsonschemaarray);
				$result['avg']='';
				$result['total']='';
				
				if(!is_array($jsonschemaarray)){
					$result['ack']=esc_html__('Error: Can not find review information. Contact support.', 'wp-review-slider-pro');
					return $result;
					die();
				}
				
				
				//find the average with simplehtmldom
				if($html->find('span[class*=js-reviewsio-avg-rating] strong', 0)){
					$result['avg']= $html->find('span[class*=js-reviewsio-avg-rating] strong', 0)->plaintext;
					$result['avg']=trim($result['avg']);
				}
				//find total from json schema
				if(isset($jsonschemaarray['aggregateRating'])){
					if(isset($jsonschemaarray['aggregateRating']['reviewCount'])){
						$result['total']=$jsonschemaarray['aggregateRating']['reviewCount'];
					}
				}
				
				$reviewcontainerdiv = $jsonschemaarray['review'];
				
				foreach ($reviewcontainerdiv as $review) {
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						$from_url_review='';
						$company_title='';
						
					// Find user_name
					if($review['author']['name']){
						$user_name=$review['author']['name'];
					}

					//find rating
					if($review['reviewRating']['ratingValue']){
						$rating=$review['reviewRating']['ratingValue'];
					}
					
					//find date created_at
					if($review['datePublished']){
						$datesubmitted=$review['datePublished'];
					}
					
					//find text
					if($review['reviewBody']){
						$rtext=$review['reviewBody'];
					}

					$recommend="";
					$meta_json ="";
					$meta_data = Array();
					
					if($rating>0){
						$reviewsarraytemp[] = [
								'reviewer_name' => trim($user_name),
								'rating' => $rating,
								'date' => $datesubmitted,
								'review_text' => trim($rtext),
								'type' => $type,
								'company_title' => $company_title
						];
					}
				}
	
					//loop reviews and build new array of just what we need
				$reviewsarrayfinal = Array();
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => '',
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => '',
					 'language_code' =>'',
					 'location' => '',
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 ];
				}

				$result['reviews'] = $reviewsarrayfinal;
				
			}
		
		return $result;
	}
    	
	//for calling remote get and returning array of reviews to insert, used for wordpress
	public function wprp_getapps_getrevs_page_wordpress($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		$result['ack']='success';
		ini_set('memory_limit','500M');
			$errormsg='';
			$callurl = $listedurl;
			$reviewsarraytemp = Array();
			
			if (filter_var($callurl, FILTER_VALIDATE_URL)) {
					
				$stripvariableurl = stripslashes($callurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				//https://wordpress.org/support/plugin/wp-tripadvisor-review-slider/reviews/
				//get totals and averages from first page, then everything else from detail page, url is title
				//next page is https://wordpress.org/support/plugin/wp-tripadvisor-review-slider/reviews/page/2/
				//make sure works for themes as well.
				
				$callurl = $listedurl;
				
				//modify page url if this is $pagenum >1;
				if($pagenum >1){
					$callurl = $listedurl."/page/".$pagenum."/";
				}

				//echo $callurl;
				$result['callurl'] =$callurl;
				$response = wp_remote_get( $callurl );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
				}
				
				$fileurlcontents = $response['body'];
				$html = wppro_str_get_html($fileurlcontents);
				
				//echo $fileurlcontents;
				//die();
				
				$result['avg']='';
				$result['total']='';
				
				if($html->find('div[class=reviews-total-count]', 0)){
					$result['total']= $html->find('div[class=reviews-total-count]', 0)->plaintext;
					$result['total'] = str_replace(" reviews", "", $result['total']);
					$result['total']=intval($result['total']);
				}
				
				if($html->find('span[class=counter-count]', 0)){
						$numstar[5] = intval($html->find('span[class=counter-count]', 0)->plaintext);
						$numstar[4] = intval($html->find('span[class=counter-count]', 1)->plaintext);
						$numstar[3] = intval($html->find('span[class=counter-count]', 2)->plaintext);
						$numstar[2] = intval($html->find('span[class=counter-count]', 3)->plaintext);
						$numstar[1] = intval($html->find('span[class=counter-count]', 4)->plaintext);
						if($result['total']>0){
							$possiblesumtotal = $result['total']*5;
							$ratedtotal = $numstar[5]*5+$numstar[4]*4+$numstar[3]*3+$numstar[2]*2+$numstar[1]*1;
							$average = ($ratedtotal/$possiblesumtotal)*5;
							$result['avg']=round($average,1);
						}
				}
				
				//need an array of URLs to loop to get actual data
				$reviewurlarray = Array();
				if($html->find('a[class=bbp-topic-permalink]', 0)){
					$reviewurlarraytemp = $html->find('a[class=bbp-topic-permalink]');
					foreach ($reviewurlarraytemp as $reviewurl) {
						$reviewurlarray[]=$reviewurl->href;
					}
				} else {
					//can't find reviews.
					$result['ack']=esc_html__('Error: Can not find reviews for this URL. Contact support.', 'wp-review-slider-pro').' '.$callurl;
				}
				//remove blanks
				$reviewurlarray=array_filter($reviewurlarray);
				$reviewsarraytemp=Array();
				//check count of urls
				if(count($reviewurlarray)>0){
					//if count of reviewurlarray is greater than what we want then slice it to speed things up.array_slice($input, 0, 3);
					if(count($reviewurlarray)>$blockstoinsert){
						$reviewurlarray = array_slice($reviewurlarray, 0, $blockstoinsert);
					}
					//if we find less than 30 then do not loop again.
					if(count($reviewurlarray)<30){
						$result['stoploop']='stop';
					}
					//okay to continue going to each page and getting info.
					foreach ($reviewurlarray as $url) {
						usleep(500000);
						$responsereview = wp_remote_get( $url );
						if ( is_array( $responsereview ) && ! is_wp_error( $responsereview ) ) {
							$headers = $responsereview['headers']; // array of http header lines
							$body    = $responsereview['body']; // use the content
						} else {
							$result['ack']=esc_html__('Error: Can not use remote get on this review url:', 'wp-review-slider-pro').' '.$url;
						}
						$fileurlcontentsreview = $responsereview['body'];
						$htmlreview = wppro_str_get_html($fileurlcontentsreview);
				
						//now should be on individual page. add data to array.
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						$from_url_review=$url;
						
						// Find user_name
						if($htmlreview->find('span[class=bbp-author-name]', 0)){
							$user_name= $htmlreview->find('span[class=bbp-author-name]', 0)->plaintext;
						}
						// Find image
						if($htmlreview->find('img[class=avatar avatar-100 photo]', 0)){
							$userpic= $htmlreview->find('img[class=avatar avatar-100 photo]', 0)->src;
							//remove url paramaters and add this... ?s=100&d=retro&r=g
							$userpic = strtok($userpic, '?')."?s=100&d=retro&r=g";
						}
						//find rating
						if($htmlreview->find('div[class=wporg-ratings]', 0)){
								$rating = $htmlreview->find('div[class=wporg-ratings]', 0)->title;
								$rating = str_replace(" out of 5 stars", "", $rating);
								$rating = intval($rating);
						}
						//find date created_at
						if($htmlreview->find('a[class=bbp-topic-permalink]', 0)){
								$datesubmitted= $htmlreview->find('a[class=bbp-topic-permalink]', 0)->title;
								$datesubmitted = substr($datesubmitted, 0, strpos($datesubmitted, "at"));
						}
						//find text
						if($htmlreview->find('div[class=bbp-topic-content]', 0)){
							if($htmlreview->find('div[class=bbp-topic-content]', 0)->find('p',0)){
							$rtext= $htmlreview->find('div[class=bbp-topic-content]', 0)->find('p',0)->plaintext;
							}
						} 
						
						$review_title = '';
						if($htmlreview->find('span[class=bbp-breadcrumb-current]', 0)){
							$review_title= $htmlreview->find('span[class=bbp-breadcrumb-current]', 0)->plaintext;
						} 
						
						$reviewsarraytemp[] = [
								'reviewer_name' => trim($user_name),
								'userpic' => $userpic,
								'rating' => $rating,
								'date' => $datesubmitted,
								'review_title' => trim($review_title),
								'review_text' => trim($rtext),
								'type' => $type,
								'from_url_review' => $from_url_review
						];
						unset($htmlreview);

					}
				}

				//loop reviews and build new array of just what we need
				$reviewsarrayfinal = Array();
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $item['userpic'],
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => $item['review_title'],
					 'from_url' => $listedurl,
					 'from_url_review' => $item['from_url_review'],
					 'language_code' =>'',
					 'location' => '',
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				
			}
		
		return $result;
	}
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_sourceforge($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid){
		$result['ack']='success';
		
			$errormsg='';
			$callurl = $listedurl;
			$reviewsarraytemp = Array();
			
			if (filter_var($callurl, FILTER_VALIDATE_URL) && $pagenum=1) {
					
				$stripvariableurl = stripslashes($callurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				//https://sourceforge.net/software/product/Visual-Visitor/
				//or
				//https://sourceforge.net/projects/portableapps/reviews/
				//changes depending on software or project.
							
				//$temppage = ($pagenum - 1)*10;
				
				$callurl = $listedurl;

				//echo $callurl;
				$result['callurl'] =$callurl;
				$response = wp_remote_get( $callurl );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
				}
				
				$fileurlcontents = $response['body'];
				
				
				$html = wppro_str_get_html($fileurlcontents);
				
				$result['avg']='';
				$result['total']='';
				
				if($html->find('div[itemprop=aggregateRating]', 0)){
						$result['avg']= $html->find('div[itemprop=aggregateRating]', 0)->find('span[itemprop=ratingValue]',0)->plaintext;
						$result['total']= $html->find('div[itemprop=aggregateRating]', 0)->find('span[itemprop=reviewCount]',0)->plaintext;
						$result['total']=intval($result['total']);
				}
				//if not set then check for project page
				if($result['avg']=='' && $result['total']==''){
					if($html->find('span[itemprop=ratingValue]', 0)){
						$result['avg']= $html->find('span[itemprop=ratingValue]', 0)->plaintext;
					}
					if($html->find('a[class=count]', 0)){
						$result['total']= $html->find('a[class=count]', 0)->plaintext;
						$result['total']=intval($result['total']);
					}
					
				}

				//print_r($result);
				//echo $html;
				//die();
				
				$reviewcontainerdiv = Array();
				//get the array of review container class
				if($html->find('div[class=m-review]', 0)){
					$reviewcontainerdiv = $html->find('div[class=m-review]');
				}

				foreach ($reviewcontainerdiv as $review) {
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						$from_url_review='';
						$company_title='';
						
					// Find user_name
					if($review->find('div[class=ext-review-meta]', 0)){
						$user_name= $review->find('div[class=ext-review-meta]', 0)->find('div',0)->plaintext;
					}
					if($user_name==''){
						//probably on a project page
						if($review->find('span[class=author-name]', 0)){
							$user_name= $review->find('span[class=author-name]', 0)->plaintext;
						}
					}
					//company title ,company_title
					if($review->find('div[class=ext-review-meta]', 0)){
						$company_title= $review->find('div[class=ext-review-meta]', 0)->find('div[class=value-label]',0)->plaintext;
					}

					//find rating
					if($review->find('meta[itemprop=ratingValue]', 0)){
							$rating = $review->find('meta[itemprop=ratingValue]', 0)->content;
							$rating = str_replace(" ", "", $rating);
							$rating = $rating;
					}
					
					//find date created_at
					if($review->find('span[class=created-date]', 0)){
							$datesubmitted= $review->find('span[class=created-date]', 0)->plaintext;
							$datesubmitted = str_replace("Posted", "", $datesubmitted);
							$datesubmitted = str_replace(" ", "", $datesubmitted);
					}
					//find text, look for expandable text first $rtext
					if($review->find('div[class=ext-review-content]', 0)){
						
						$rtext_pros= $review->find('div[class=ext-review-content]', 0)->find('p',0)->innertext;
						$rtext_cons= $review->find('div[class=ext-review-content]', 0)->find('p',1)->innertext;
						$rtext_overall= $review->find('div[class=ext-review-content]', 0)->find('p',2)->innertext;
						$rtext=$rtext_pros."<br><br>".$rtext_cons."<br><br>".$rtext_overall."<br><br>";
						//need to delete all html except for br and b3
						$rtext=strip_tags($rtext,'<b><br>');
					} 
					if($rtext==''){
						//probably on a project page
						if($review->find('div[class=review-txt]', 0)){
							$rtext= $review->find('div[class=review-txt]', 0)->plaintext;
						}
					}

					$recommend="";
					$meta_json ="";
					$meta_data = Array();
					

					if($rating>0){
						$reviewsarraytemp[] = [
								'reviewer_name' => trim($user_name),
								'rating' => $rating,
								'date' => $datesubmitted,
								'review_text' => trim($rtext),
								'type' => $type,
								'company_title' => $company_title
						];
					}
				}
				//print_r($reviewsarraytemp);
				//die();
				
				//loop reviews and build new array of just what we need
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => '',
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => '',
					 'language_code' =>'',
					 'location' => '',
					 'recommendation_type' => '',
					 'company_title' =>  $item['company_title'],
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				
			}
		
		return $result;
	}
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_guildquality($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid){
		$result['ack']='success';
		
			$errormsg='';
			$callurl = $listedurl;
			$reviewsarraytemp = Array();
			
			if (filter_var($callurl, FILTER_VALIDATE_URL) && $pagenum=1) {
					
				$stripvariableurl = stripslashes($callurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				//https://www.guildquality.com/pro/model-remodel?tab=reviews
				//only getting first page. 50 reviews max.
				
				$callurl = $listedurl.'?tab=reviews';

				//echo $callurl;
				$result['callurl'] =$callurl;
				$args = array(
					'timeout'     => 15,
					'sslverify' => false
				); 
				$response = wp_remote_get($callurl,$args);
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
				}
				
				$fileurlcontents = $response['body'];
				
				
				$html = wppro_str_get_html($fileurlcontents);
				
				$result['avg']='';
				$result['total']='';
				
				if($html->find('meta[itemprop=reviewCount]', 0)){
						$result['total']= $html->find('meta[itemprop=reviewCount]', 0)->content;
						$result['total']=intval($result['total']);
				}
				if($html->find('meta[itemprop=ratingValue]', 0)){
						$result['avg']= $html->find('meta[itemprop=ratingValue]', 0)->content;
				}
				
				
				$reviewcontainerdiv = Array();
				//get the array of review container class
				if($html->find('div[itemprop=review]', 0)){
					$reviewcontainerdiv = $html->find('div[itemprop=review]');
				}

				foreach ($reviewcontainerdiv as $review) {
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						$from_url_review='';
						$location='';
						
					// Find user_name
					if($review->find('span[itemprop=name]', 0)){
						$user_name= $review->find('span[itemprop=name]', 0)->plaintext;
					}

					//location
					if($review->find('div[class=mat-meta]', 0)){
						$location= $review->find('div[class=mat-meta]', 0)->innertext;
						//remove everything inside <>
						$location= preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $location);
						$location = str_replace("&middot;", "", $location);
						$location = str_replace("·", "", $location);
						$location=trim($location);
					}

					//find rating
					if($review->find('meta[itemprop=ratingValue]', 0)){
							$rating = $review->find('meta[itemprop=ratingValue]', 0)->content;
							$rating = str_replace(" ", "", $rating);
							$rating = $rating;
					}
					
					//find date created_at
					if($review->find('span[itemprop=datePublished]', 0)){
							$datesubmitted= $review->find('span[itemprop=datePublished]', 0)->datetime;
					}
					//find text, look for expandable text first $rtext
					if($review->find('p[itemprop=reviewBody]', 0)){
						$rtext= $review->find('p[itemprop=reviewBody]', 0)->plaintext;
					} 
					
					//----------------
					//add review images here
					//mediaurlsarrayjson, max of 8 images
					//--------------
					$mediaurlsarrayjson='';
					$mediaurlsarray = Array();
					if($review->find('a[class=mat-card_review-img]')){
						//have at least one image
						$imagesobject = $review->find('a[class=mat-card_review-img]');
						foreach ($imagesobject as $imageobj) {
							$mediaimg= $imageobj->style;
							$tempurl = $this->get_string_between($mediaimg, "background-image: url('", "');");
							if($tempurl!=''){
							$mediaurlsarray[]=$this->get_string_between($mediaimg, "background-image: url('", "');");
							}
							if(count($mediaurlsarray)>7){
								break;
							}
							$mediaimg='';
							$tempurl='';
						}
						$mediaurlsarrayjson = json_encode($mediaurlsarray);
						unset($mediaurlsarray);
					}
					//========loop to find all media links up to 8.


					$recommend="";
					$meta_json ="";
					$meta_data = Array();
					
					if($rating>0){
						$reviewsarraytemp[] = [
								'reviewer_name' => trim($user_name),
								'rating' => $rating,
								'date' => $datesubmitted,
								'review_text' => trim($rtext),
								'type' => $type,
								'location' => $location,
								'mediaurlsarrayjson' => $mediaurlsarrayjson,
						];
					}
				}
				
				//loop reviews and build new array of just what we need
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => '',
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => '',
					 'language_code' =>'',
					 'location' => $item['location'],
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 'mediaurlsarrayjson' => $item['mediaurlsarrayjson'],
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				
			}
		
		return $result;
	}
	
	//for calling remote get and returning array of reviews to insert, calling Crawler now crawl.ljapps.com
	public function wprp_getapps_getrevs_page_vrbo($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			$reviewsarray= Array();
			
			if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
					
				if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
					$ip_server = $_SERVER['SERVER_ADDR'];
				} else {
					//get url of site.
					$ip_server = urlencode(get_site_url());
				}
				$siteurl = urlencode(get_site_url());
				
				//scrapeurl
				$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=vrbo&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl;
				
				//echo $tempurlval;
				//die();
				
				$serverresponse='';
				
				$args = array(
					'timeout'     => 120,
					'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				)
				); 
				$response = wp_remote_get( $tempurlval, $args );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$serverresponse    = $response['body']; // use the content
				} else {
					//must have been an error
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support. '.$response->get_error_message();
					$results = json_encode($results);
					echo $results;
					die();
				}
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
		   //this site is greylisted by imunify360 on cloudways, call backup digital ocean server
		   $response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=vrbo&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			}
		}
		//=========================
				$serverresponsearray = json_decode($serverresponse, true);

				if($serverresponse=='' || !is_array($serverresponsearray)){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch limit error
				if($serverresponsearray['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch error
				if($serverresponsearray['result']['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				//made it this far assume we have reviews.
				$crawlerresultarray = $serverresponsearray['result'];
				

				//need totals and avg for this place $getreviewsarray['total']
				$result['total']='';
				$result['avg']='';
				if(isset($crawlerresultarray['avg'])){
					$result['avg']=$crawlerresultarray['avg'];
				}
				if(isset($crawlerresultarray['total'])){
					$result['total']=$crawlerresultarray['total'];
				}
				
				//pass back URL used
				if(isset($crawlerresultarray['callurl'])){
					$result['callurl']=$crawlerresultarray['callurl'];
				}
				//pass back next URL used
				if(isset($crawlerresultarray['nextpageurl'])){
					$result['nextpageurl']=$crawlerresultarray['nextpageurl'];
				}
				
				$x=0;
				$crawlerreviewsarray = $crawlerresultarray['reviews'];
				$numreturned = count($crawlerreviewsarray);	
				
				foreach ($crawlerreviewsarray as $review) {
					
					$tempownerres='';
					if(isset($review['owner_response']) && $review['owner_response']!=''){
						$tempownerres = $review['owner_response'];
					}
					$templocation ='';
					if(isset($review['location']) && $review['location']!=''){
						$templocation = $review['location'];
					}	
					$tempmediaurlsarrayjson ='';
					if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
						$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
					}					
					
					$reviewsarray[] = [
					 'reviewer_name' => $review['user_name'],
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => '',
					 'rating' => $review['rating'],
					 'updated' => $review['created_time'],
					 'review_text' => $review['review_text'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => '',
					 'language_code' => '',
					 'location' => $templocation,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
					 'owner_response' => $tempownerres,
					 ];
					
					$x++;
				}
				
				//if we find less than 10 then do not loop again.
					if(count($reviewsarray)<10){
						$result['stoploop']='stop';
					}

			$result['reviews'] = $reviewsarray;

			}
		return $result;
	}

	
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_vrbo_oldDELETE($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		$stripvariableurl = strtok($listedurl, '?');
		$reviewsarraytemp = Array();

		$callurl = "https://phantomjscloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/?request=%7Burl:%22".$stripvariableurl."%22,renderType:%22html%22,scripts:%7BdomReady:%5B%22setInterval%28function%28%29%7Bvar%20lastY=window.scrollY;window.scrollBy%280,200%29;if%28lastY===window.scrollY%29%7Bwindow._pjscMeta.forceFinish=true;%7D%7D,50%29;%22%5D%7D%7D";
		
		//echo $callurl;
		//die();
		
		$args = array(
				'timeout'     => 30,
				'sslverify' => false
			); 
		$data = wp_remote_get( $callurl,$args );
		if ( is_wp_error( $data ) ) 
		{
			$response['error_message'] 	= $data->get_error_message();
			$reponse['status'] 		= $data->get_error_code();
			print_r($response);
			die();
		}
		$fileurlcontents = $data['body'];
		
		//check for phantom out of credits.
		//dailySubscriptionBalance
		if (strpos($fileurlcontents, "dailySubscriptionBalance") !== false) {
			$results['ack'] ='error';
			$results['ackmsg'] = esc_html__('Error 1001: Out of credits.', 'wp-review-slider-pro');
			$results = json_encode($results)." - ".$fileurlcontents;
			echo $results;
			die();
		}
					
		
		$html = wppro_str_get_html($fileurlcontents);
		
		if($html->find('div.review__content')){
			$reviewobject = $html->find('div.review__content');
		}
		if(!isset($reviewobject)){
			if($html->find('article[itemprop=review]')){
				$reviewobject = $html->find('article[itemprop=review]');
			}
		}
		if(!isset($reviewobject) || count($reviewobject)<1){
			echo $html;
			$results['ack'] ='error';
			$results['ackmsg'] = esc_html__('Error 103: Unable to read VRBO page. Please contact support or use the Review Funnel page.', 'wp-review-slider-pro');
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		
		foreach ($reviewobject as $review) {
			
			$user_name='';
			$userimage='';
			$rating='';
			$datesubmitted='';
			$rtext='';
			$from_url_review='';
			$location='';
			$language_code='';
			$review_title ='';
			
			// Find user_name
			if($review->find('span[class=review__author-name]', 0)){
				$user_name = $review->find('span[class=review__author-name]', 0)->plaintext;
			}
			if($user_name==''){
				if($review->find('h5[itemprop=author]', 0)){
					$user_name = $review->find('h5[itemprop=author]', 0)->plaintext;
				}
			}
			
			
			// Find location
			if($review->find('span[class=review__author-location]', 0)){
				$location = $review->find('span[class=review__author-location]', 0)->plaintext;
			}


			// find rating rating__score text-muted small
			if($review->find('span[class=rating__score text-muted small]', 0)){
				$rating = $review->find('span[class=rating__score text-muted small]', 0)->plaintext;
				$rating = str_replace("/5","",$rating);
			}
			if($rating==''){
				if($review->find('span[itemprop=ratingValue]', 0)){
					$rating = $review->find('span[itemprop=ratingValue]', 0)->plaintext;
					$rating = str_replace("/5","",$rating);
					$rating = intval($rating);
				}
			}
			
			// find date created_at
			if($review->find('span[class=review__submitted-details]', 0)){
				$datesubmitted = $review->find('span[class=review__submitted-details]', 0)->plaintext;
			}
			if($datesubmitted==''){
				if($review->find('span[itemprop=datePublished]', 0)){
					$datesubmitted = $review->find('span[itemprop=datePublished]', 0)->plaintext;
				}
			}
			
			// find headline
			//review__headline
			if($review->find('h4[class=review__headline]', 0)){
				$review_title = $review->find('h4[class=review__headline]', 0)->plaintext;
			}
			if($review_title==''){
				if($review->find('h5[data-stid=review_section_title]', 0)){
					$review_title = $review->find('h5[data-stid=review_section_title]', 0)->plaintext;
				}
			}
			
			//review--expanded
			if($review->find('div[class=review--expanded]', 0)){
				$rtext = $review->find('div[class=review--expanded]', 0)->plaintext;
			}
			if($rtext==''){
				if($review->find('span[itemprop=description]', 0)){
					$rtext = $review->find('span[itemprop=description]', 0)->plaintext;
				}
			}
			

			if($review->find('div[class=review__response]', 0)){
				$responobj = $review->find('div[class=review__response]', 0);
				if($responobj->find('span[class=review__response-body]', 0)){
					$ownerresponsearray = [];
					$ownerresponsearray['id']='';
					$ownerresponsearray['name']='Owner';
					$ownerresponsearray['date']='';
					$ownerresponsearray['comment']=$responobj->find('span[class=review__response-body]', 0)->plaintext;
					$ownerresponsearray = json_encode($ownerresponsearray);
				} else {
					$ownerresponsearray ='';
				}
				
			} else {
				$ownerresponsearray ='';
			}
			
			if($rating>0){
				$review_length = str_word_count($rtext);
				if (extension_loaded('mbstring')) {
					$review_length_char = mb_strlen($rtext);
				} else {
					$review_length_char = strlen($rtext);
				}
				if($review_length_char>0 && $review_length<1){
								$review_length = 1;
							}
				

				$reviewsarraytemp[] = [
							'reviewer_name' => trim($user_name),
							'reviewer_id' => '',
							'userpic' => $userimage,
							'rating' => $rating,
							'date' => $datesubmitted,
							'review_text' => trim($rtext),
							'review_title' => $review_title,
							'type' => $type,
							'language_code' => $language_code,
							'location' => $location,
							'from_url_review' => $from_url_review,
							'owner_response' => $ownerresponsearray,
				];
	
				
				$review_length ='';
				$review_length_char='';
			}
		}
		
				
				//loop reviews and build new array of just what we need
				$reviewsarrayfinal = Array();
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => trim($item['reviewer_id']),
					 'reviewer_email' => '',
					 'userpic' => $item['userpic'],
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => $item['review_title'],
					 'from_url' => $listedurl,
					 'from_url_review' => $item['from_url_review'],
					 'language_code' =>$item['language_code'],
					 'location' => $item['location'],
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => $item['owner_response'],
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				//we can currently only get one page of reviews so we send stop command back
				$result['stoploop']='stop';
			
  
 		return $result;
	}
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_vrbo_old($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		$stripvariableurl = strtok($listedurl, '?');
		$stripvariableurl = $stripvariableurl."?pwaDialogNested=PropertyDetailsReviewsBreakdownDialog";
		$reviewsarraytemp = Array();
		
		echo $stripvariableurl;
		die();
		
		//https://phantomjscloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/?request=%7Burl:%22https://www.vrbo.com/741538%22,renderType:%22jpg%22,scripts:%7BdomReady:%5B%22setInterval%28function%28%29%7Bvar%20lastY=window.scrollY;window.scrollBy%280,200%29;if%28lastY===window.scrollY%29%7Bwindow._pjscMeta.forceFinish=true;%7D%7D,50%29;%22%5D%7D%7D
		
		$args = array(
				'timeout'     => 30,
				'sslverify' => false
			); 
		$data = wp_remote_get( $stripvariableurl,$args );
		if ( is_wp_error( $data ) ) 
		{
			$response['error_message'] 	= $data->get_error_message();
			$reponse['status'] 		= $data->get_error_code();
			print_r($response);
			die();
		}
	
		$fileurlcontents = $data['body'];
		
		echo $fileurlcontents;
		die();
		
		$result['total'] = intval($this->get_string_between($fileurlcontents, '"reviewCount": ', '}'));
		$result['avg'] = $this->get_string_between($fileurlcontents, '"ratingValue": ', ',');
		
		//echo 'total:'.$result['total'];
		//echo 'avg:'.$result['avg'];
		
		$parsed = $this->get_string_between($fileurlcontents, '"reviews":[', '],');
		$parsed = "[".$parsed."]";

		$pagedata = json_decode( $parsed, true );
		
		print_r($pagedata);
		die();
		
		foreach ($pagedata as $review) {
			
			$user_name='';
			$userimage='';
			$rating='';
			$datesubmitted='';
			$rtext='';
			$reviewer_id='';
			$from_url_review='';
			$location='';
			$language_code='';
			$review_title ='';
			
			// Find user_name
			if($review['reviewer']['nickname']){
				$user_name = $review['reviewer']['nickname'];
			}
			
			// Find userimage ui_avatar
			if(isset($review['reviewer']['profileImage']) && $review['reviewer']['profileImage']!=''){
				$userimage = $review['reviewer']['profileImage'];
			}
			
			// Find language_code
			if($review['reviewLanguage']){
				$language_code = $review['reviewLanguage'];
			}
			
			// Find user_name
			if($review['reviewer']['location'] && $review['reviewer']['location']!='null'){
				$location = $review['reviewer']['location'];
			}

			// find rating
			if($review['rating']){
				$rating = $review['rating'];
			}

			// find date created_at
			if($review['datePublished']){
				$datesubmitted = $review['datePublished'];
			}
			
			// find headline
			
			if($review['headline']){
				$review_title = $review['headline'];
			}
			if($review['body']){
				$rtext = $review['body'];
			}
			
			//owner response
			//{ "id":12630808, "name":"Response from the owner", "date":"2018-08-24", "comment":"Raul - this is a very bad example of how the trip went. Sorry you feel that way, and hope you have better luck with another charter..." }
			//{"id":16369073,"name":"Response from the owner","date":"2014-05-29","comment":"Thank You - Jennifer.  Your family is always a pleasure to have aboard.  Fish On!!"}
			
			if(isset($review['response']) && $review['response']['body']){
				$ownerresponsearray = [];
				$responsebody = $review['response']['body'];
				$ownerresponsearray['id']='';
				$ownerresponsearray['name']='Owner';
				$ownerresponsearray['date']='';
				$ownerresponsearray['comment']=$responsebody;
				$ownerresponsearray = json_encode($ownerresponsearray);
			} else {
				$ownerresponsearray ='';
			}
			
			
			if($rating>0){
				$review_length = str_word_count($rtext);
				if (extension_loaded('mbstring')) {
					$review_length_char = mb_strlen($rtext);
				} else {
					$review_length_char = strlen($rtext);
				}
				if($review_length_char>0 && $review_length<1){
								$review_length = 1;
							}
				

				$furlrev = 'https://www.vrbo.com/users/show/'.trim($reviewer_id);
				$reviewsarraytemp[] = [
							'reviewer_name' => trim($user_name),
							'reviewer_id' => trim($reviewer_id),
							'userpic' => $userimage,
							'rating' => $rating,
							'date' => $datesubmitted,
							'review_text' => trim($rtext),
							'review_title' => $review_title,
							'type' => $type,
							'language_code' => $language_code,
							'location' => $location,
							'from_url_review' => $from_url_review,
							'owner_response' => $ownerresponsearray,
				];
	
				
				$review_length ='';
				$review_length_char='';
			}
		}
		
				
				//loop reviews and build new array of just what we need
				$reviewsarrayfinal = Array();
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => trim($item['reviewer_id']),
					 'reviewer_email' => '',
					 'userpic' => $item['userpic'],
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => $item['review_title'],
					 'from_url' => $listedurl,
					 'from_url_review' => $item['from_url_review'],
					 'language_code' =>$item['language_code'],
					 'location' => $item['location'],
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => $item['owner_response'],
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				//we can currently only get one page of reviews so we send stop command back
				$result['stoploop']='stop';
			
  
 		return $result;
	}
	
	//used for Zillow and Realtor remote crawls and InnStyle and Airbnb and DocFinder
	public function wprp_getapps_getrevs_page_multi_remote($sitetype,$listedurl,$pagenum,$perpage,$savedpageid,$sortoption,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
	
		$errormsg='';
		$reviewsarraytemp = Array();
		$nhful='new';
		$reviewsarray= Array();
		$crawlerreviewsarray= Array();
		
		
		if (filter_var($listedurl, FILTER_VALIDATE_URL) || $sitetype=="InnStyle") {
				
			if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
				$ip_server = $_SERVER['SERVER_ADDR'];
			} else {
				//get url of site.
				$ip_server = urlencode(get_site_url());
			}
			$siteurl = urlencode(get_site_url());
			
			//scrapeurl
			$sitetypelower = strtolower($sitetype);

			//if this is checkatrade then lets url encode the listedurl
			if($sitetypelower=='checkatrade'){
				$listedurl = urlencode($listedurl."?size=20&orderDesc=createdAt&attachments=true");
			}
			
			$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype='.$sitetypelower.'&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl.'&blocks='.$blockstoinsert;
			
//echo $tempurlval;
//die();
			
			if(strpos($listedurl, '/lender-profile/') !== false){
				//for lender profile
				$results['ack'] ='error';
				$results['ackmsg'] ='Sorry, this does not currently work for a lender profile url. You can download Lender Reviews with the Review Funnels tab above.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			$serverresponse='';
			
			$args = array(
				'timeout'     => 120,
				'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				)
			); 
			$response = wp_remote_get( $tempurlval, $args );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			} else {
				//must have been an error
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
				$results = json_encode($results);
				echo $results;
				die();
			}
			
			//print_r($serverresponse);
			
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false || strpos($serverresponse, "403 Forbidden") !== false) {
		   //this site is greylisted by imunify360 on cloudways, call backup digital ocean server
		   $response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype='.$sitetypelower.'&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			}
		}
		//=========================	
			$serverresponsearray = json_decode($serverresponse, true);
			
			//print_r($serverresponsearray );
			//die();

			if($serverresponse=='' || !is_array($serverresponsearray)){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch limit error
			if($serverresponsearray['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
				$results = json_encode($results);
				echo $results;
				die();
			}
			//catch error
			if($serverresponsearray['result']['ack']=='error'){
				$results['ack'] ='error';
				$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
				$results = json_encode($results);
				echo $results;
				die();
			}
			//made it this far assume we have reviews.
			$crawlerresultarray = $serverresponsearray['result'];
			

			//need totals and avg for this place $getreviewsarray['total']
			$result['total']='';
			$result['avg']='';
			if(isset($crawlerresultarray['avg'])){
				$result['avg']=$crawlerresultarray['avg'];
			}
			if(isset($crawlerresultarray['total'])){
				$result['total']=$crawlerresultarray['total'];
			}
			
			//pass back URL used
			if(isset($crawlerresultarray['callurl'])){
				$result['callurl']=$crawlerresultarray['callurl'];
			}
			//pass back next URL used
			if(isset($crawlerresultarray['nextpageurl'])){
				$result['nextpageurl']=$crawlerresultarray['nextpageurl'];
			}
			
			$x=0;
			if(isset($crawlerresultarray['reviews'])){
			$crawlerreviewsarray = $crawlerresultarray['reviews'];
			}
			
			foreach ($crawlerreviewsarray as $review) {
				
				$tempownerres='';
				if(isset($review['owner_response']) && $review['owner_response']!=''){
					$tempownerres = $review['owner_response'];
				}
				$templocation ='';
				if(isset($review['location']) && $review['location']!=''){
					$templocation = $review['location'];
				}	
				$tempmediaurlsarrayjson ='';
				if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
					$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
				}
				
				$tempimg = "";	
				if($review['userimage']!=""){
					$tempimg = $review['userimage'];
				}
				
				$fromurl = "";	
				if($review['from_url_review']!=""){
					$fromurl = $review['from_url_review'];
				}
				$review_title = "";	
				if($review['review_title']!=""){
					$review_title = $review['review_title'];
				}

				$language_code = "";
				if($review['language_code']!=""){
					$language_code = $review['language_code'];
				}
				
			
				$reviewsarray[] = [
				 'reviewer_name' => $review['reviewer_name'],
				 'reviewer_id' => $review['reviewer_id'],
				 'reviewer_email' => '',
				 'userpic' => $tempimg,
				 'rating' => $review['rating'],
				 'updated' => $review['date'],
				 'review_text' => $review['review_text'],
				 'review_title' => $review_title,
				 'from_url_review' => $fromurl,
				 'language_code' => $language_code,
				 'unique_id' => $review['unique_id'],
				 'location' => $templocation,
				 'recommendation_type' => '',
				 'company_title' =>  '',
				 'company_url' => '',
				 'company_name' => '',
				 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
				 'owner_response' => $tempownerres,
				 'meta_data' => $review['meta_data'],
				 ];
				
				$x++;
			}
			
			//if we find less than 10 then do not loop again.$sitetype unless zillow is 5
			if(count($reviewsarray)<10 && $sitetypelower!="zillow"){
				$result['stoploop']='stop';
			}
			if($sitetypelower=="realtor"){
				$result['stoploop']='stop';
			}
			if(count($reviewsarray)<5 && $sitetypelower!="zillow"){
				$result['stoploop']='stop';
			}
			
			//just crawling once for zillow and airbnb
			if($sitetypelower=="airbnb" ){
				$result['stoploop']='stop';
			}

		$result['reviews'] = $reviewsarray;

		}
		return $result;

	}
	
	
		//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_zillow($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();
		
		$businessId = $listedurl;
		$sindex = 0;
		//$apikey = get_option('wprevpro_birdeyeapikey_val');
		$result['avg']='';
		$result['total']='';
			

		//https://www.zillow.com/reviews/write/?s=X1-ZUvu3i2bzw4m4p_46au8
		//https://www.zillow.com/profile-page/api/public/v1/reviews?encodedZuid=X1-ZUvu3i2bzw4m4p_46au8&profileTypeIds=1%2C2%2C12%2C16&page=1&size=5&sortType=2&sortOrder=2


		$getreviews = false;
		$errormsg='';
		$callurl = $listedurl;
		$reviewsarraytemp = Array();
		$reviewsarrayfinal = Array();
		if (filter_var($callurl, FILTER_VALIDATE_URL)) {
			$stripvariableurl = strtok($callurl, '?');
			$stripvariableurl = stripslashes($stripvariableurl);
			//check url to find out what kind of review page this is
			if (strpos($stripvariableurl, '/write/') !== false) {
				
				//find the id if this is the first page
				if($pagenum==1){
					$url_components = parse_url($callurl);
					parse_str($url_components['query'], $urldetails);
					
					//print_r($urldetails);
					$id = $urldetails['s'];
					update_option( 'wprevpro_zillowid', $id, false );
				} else {
					$id = get_option('wprevpro_zillowid');
				}
				//$rurl ="https://www.zillow.com/ajax/review/ReviewDisplayJSONGetPage.htm?id=".$id."&size=50&page=".$pagenum."&page_type=received&moderator_actions=0&reviewee_actions=0&reviewer_actions=0&proximal_buttons=1&hasImpersonationPermission=0&service=&sort=1";
				
				
				$rurl ="https://www.zillow.com/profile-page/api/public/v1/reviews?encodedZuid=".$id."&profileTypeIds=1%2C2%2C12%2C16&page=".$pagenum."&size=5&sortType=2&sortOrder=2";
				
				//echo $rurl;
				//die();
				
				$urlvalue = esc_url_raw($rurl);
				
				//if this is a realtor page
				if(isset($id) && $id!=''){
					$getreviews = true;
				} else {
					$errormsg = $errormsg . __(' Unable to find the Zillow reviews URL. Contact support or try using a Review Funnel.','wp-review-slider-pro');
					$this->errormsg = $errormsg;
				}
			
			$result['callurl'] =$urlvalue;
							
			if($getreviews){
				
				//now actually get the reviews
				
				$data = wp_remote_get( $urlvalue );
				if ( is_wp_error( $data ) ) 
				{
					$response['error_message'] 	= $data->get_error_message();
					$reponse['status'] 		= $data->get_error_code();
					print_r($response);
					die();
				}
				//print_r($data);
				//die();
				
				if ( is_array( $data ) ) {
				  $header = $data['headers']; // array of http header lines
				  $body = $data['body']; // use the content
				}
					
				$pagedata = json_decode( $body, true );
				if(!is_array($pagedata) || count($pagedata)<1){
					$fileurlcontents =$this->file_get_contents_curl_browser($urlvalue,'');
					//echo $fileurlcontents;
					$html = wppro_str_get_html($fileurlcontents);
					$pagedata = json_decode( $html, true );
				}
				
				//print_r($pagedata);
				//die();
				
				if(isset($pagedata['filters'][0]['count'])){
						$result['total']=$pagedata['filters'][0]['count'];
				}
				
				if(!isset($pagedata['reviews'])){
					$result['ack'] = __(' Unable to find reviews. Please check the URL. You can also try switching the Crawl Server to Remote.','wp-review-slider-pro');
					return $result;
					die();
				}
				
				// Find reviews
				$reviewsarray = $pagedata['reviews'];
				


				foreach ($reviewsarray as $review) {
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';

						$from_url_review='';
						$company_title='';
						$unique_id='';
						$reviewer_id='';
						$title='';
						$reviewer_email='';
						$userpic='';
						$from_url_review='';
						$from_url='';
						$location='';
						
						// Find user_name
						if($review['reviewer']['screenName']){
							$user_name = $review['reviewer']['screenName'];
						}
						
						// Find userimage ui_avatar
						$userimage = '';

						// find rating
						if($review['rating']){
							$rating = intval($review['rating']);
						}

						// find date created_at
						if($review['createDate']){
							//11/14/2018
							$datesubmitted = $review['createDate'];
						}
						
						// find text
						if($review['reviewComment']){
							$rtext = $review['reviewComment'];
						}
						
						// find reviewer_id
						if($review['reviewer']['encodedZuid']){
							$unique_id = $review['reviewer']['encodedZuid'];
						}
						
						// find text
						if($review['reviewId']){
							$unique_id = $review['reviewId'];
						}
						
						$meta_json ="";
						$meta_data = Array();
						if($review['subRatings']){
							$meta_data['subRatings'] = json_encode($review['subRatings']);
						}
						if($review['workDescription']){
							$meta_data['workDescription'] = $review['workDescription'];
						}
						if(count($meta_data)>0){
							$meta_json = json_encode($meta_data);
						}
					
						if($rating>0){
							$reviewsarraytemp[] = [
									'reviewer_name' => trim($user_name),
									'rating' => $rating,
									'date' => $datesubmitted,
									'review_text' => trim($rtext),
									'reviewer_id' => trim($reviewer_id),
									'unique_id' => trim($unique_id),
									'meta_data' => $meta_json,
									'type' => 'Zillow'
							];
						}
				}
			}
			//print_r($reviewsarraytemp);
			//die();
			//loop reviews and build new array of just what we need
			
			foreach ($reviewsarraytemp as $item) {
				 $reviewsarrayfinal[] = [
				 'reviewer_name' => trim($item['reviewer_name']),
				 'reviewer_id' => $item['reviewer_id'],
				 'reviewer_email' => '',
				 'userpic' => '',
				 'rating' => $item['rating'],
				 'updated' => $item['date'],
				 'review_text' => $item['review_text'],
				 'review_title' => '',
				 'from_url_review' => '',
				 'language_code' => '',
				 'unique_id' => $item['unique_id'],
				 'location' => '',
				 'recommendation_type' => '',
				 'company_title' =>  '',
				 'company_url' => '',
				 'company_name' => '',
				 'meta_data' => $item['meta_data'],
				 ];
			}

			//print_r($reviewsarrayfinal);
			//die();
			
			$result['reviews'] = $reviewsarrayfinal;
			} else if(strpos($stripvariableurl, '/lender-profile/') !== false){

				//for lender profile
				//$stripvariableurl
				$errormsg = $errormsg . __(' Sorry, this does not currently work for a lender profile url. You can download Lender Reviews with the Review Funnels tab above.','wp-review-slider-pro');
				$this->errormsg = $errormsg;
					
					
			}
		} else {
			$errormsg='Please enter a valid URL.';
		}
		
		$result['ack'] =$errormsg;
		if(count($reviewsarrayfinal)<5){
			//no need to loop again.
			$result['stoploop'] = "stop";
		}
		

		return $result;
	}
	
	
	
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_yotpo($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();
		
		$client_id = $listedurl;
		$sindex = 0;
		$clientsecret = get_option('wprevpro_yotposecretkey_val');
		$usertoken = get_option('wprevpro_yotpousertoken');
		$result['avg']='';
		$result['total']='';
			
		if($client_id=='' || $clientsecret==''){
			//must have been an error
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Make sure to enter your API Key and Business ID from Birdeye.';
			$results = json_encode($results);
			echo $results;
			die();
		}

		if($pagenum >1){
			$sindex = $pagenum*100;
		}
		
		//first make call to get user token if on page 1 or if not set yet.
		if($pagenum<2 || $usertoken==''){
			//https://api.yotpo.com/oauth/token?grant_type=client_credentials&client_id=2396MKzUZbAPHI0daFTEmeMfIa8hpLgu7e899k4l&client_secret=3iD4fxhnXZwhgIjztEyVxniBOM25pSQw36AIHfYE
			
			$callurl = "https://api.yotpo.com/oauth/token?grant_type=client_credentials&client_id=".$client_id."&client_secret=".$clientsecret;
			
			$response = wp_remote_post( $callurl, array(
					'method'      => 'POST',
					'timeout'     => 45,
					'redirection' => 5,
					'httpversion' => '1.0',
					'blocking'    => true,
					'headers'     => array(),
					'body'        => array(),
					'cookies'     => array()
					)
				);
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$body    = $response['body']; // use the content
			} else {
				$result['ack']=esc_html__('Error: Can not use remote post on this url:', 'wp-review-slider-pro').' '.$callurl;
			}
			$usertokenarray = json_decode($body,true);
			
			if(isset($usertokenarray['access_token'])){
				$usertoken = $usertokenarray['access_token'];
				update_option('wprevpro_yotpousertoken',$usertoken);
			}
			
			//also need to find avg and total here since this is first page.
			$callurl = "https://api.yotpo.com/products/".$client_id."/yotpo_site_reviews/bottomline";
			$response = wp_remote_get( $callurl, array( 'timeout' => 10,
					'headers' => array( 'Content-Type' => 'application/json',
                               'Accept'=> 'application/json' ) 
					));
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$body    = $response['body']; // use the content
			} else {
				$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
			}
			$reviewsummary = json_decode($body,true);

			if($reviewsummary['response']['bottomline']){
				$result['avg']=$reviewsummary['response']['bottomline']['average_score'];
				$result['total']=$reviewsummary['response']['bottomline']['total_reviews'];
			}
			
		}
		if ( $usertoken=='') {
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Can not find user token. ';
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$endpoint = "https://api.yotpo.com/v1/apps/".$client_id."/reviews?utoken=".$usertoken."&count=50&page=".$pagenum;

		$args = array(
					'timeout'     => 50,
					'sslverify' => false
				); 
		$response = wp_remote_get( $endpoint, $args );
				
		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$fileurlcontents = $response['body'];	
			
		$reviewcontainerdiv = json_decode($fileurlcontents,true);
		
		foreach ($reviewcontainerdiv['reviews'] as $review) {
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$from_url_review='';
				$company_title='';
				$unique_id='';
				$reviewer_id='';
				$title='';
				$reviewer_email='';
				$userpic='';
				$from_url_review='';
				$from_url='';
				$location='';
			
			// Find unique id
			if($review['id']){
				$unique_id=$review['id'];
			}
			
			// Find user_name
			if($review['name']){
				$user_name=$review['name'];
			}

			if($review['email']){
				$reviewer_email=$review['email'];
			}

			//find rating
			if($review['score']){
				$rating=$review['score'];
			}
			
			//find date created_at
			if($review['created_at']){
				$datesubmitted=$review['created_at'];
			}
			
			//find title
			if($review['title']){
				$title=$review['title'];
			}
			
			//find text
			if($review['content']){
				$rtext=$review['content'];
			}
			
			$meta_json ="";
			$meta_data = Array();
			if($review['votes_up']){
				$meta_data['votes_up'] = json_encode($review['votes_up']);
			}
			if($review['votes_down']){
				$meta_data['votes_down'] = json_encode($review['votes_down']);
			}
			if($review['sentiment']){
				$meta_data['sentiment'] = json_encode($review['sentiment']);
			}
			if($review['sku']){
				$meta_data['sku'] = json_encode($review['sku']);
			}
			if($review['reviewer_type']){
				$meta_data['reviewer_type'] = json_encode($review['reviewer_type']);
			}
			if(count($meta_data)>0){
				$meta_json = json_encode($meta_data);
			}


			if($rating>0){
				$reviewsarray[] = [
					 'reviewer_name' => trim($user_name),
					 'reviewer_id' => trim($reviewer_id),
					 'reviewer_email' => $reviewer_email,
					 'userpic' => $userpic,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => trim($rtext),
					 'review_title' => trim($title),
					 'from_url' => $from_url,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'unique_id' => $unique_id,
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => '',
					 'meta_data' => $meta_json,
					 ];
			}
		}

		$result['reviews'] = $reviewsarray;
				
		//die();

		return $result;
	}
		//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_angi($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert){
			
				$result['ack']='success';
		
			$errormsg='';
			$reviewsarraytemp = Array();
			$nhful='new';
			$reviewsarray= Array();
			$nextpageurl='';
			
			if (filter_var($listedurl, FILTER_VALIDATE_URL)) {
					
				if(isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=''){
					$ip_server = $_SERVER['SERVER_ADDR'];
				} else {
					//get url of site.
					$ip_server = urlencode(get_site_url());
				}
				$siteurl = urlencode(get_site_url());
				
				//scrapeurl
				$tempurlval = 'https://crawl.ljapps.com/crawlrevs?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=angi&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl;
				
				//echo $tempurlval;
				//die();
				
				$serverresponse='';
				
				$args = array(
					'timeout'     => 50,
					'sslverify' => false,
				'headers' => array( 
					'Content-Type' => ' application/json',
					'Accept'=> 'application/json'
				)
				); 
				$response = wp_remote_get( $tempurlval, $args );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$serverresponse    = $response['body']; // use the content
				} else {
					//must have been an error
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001a: trouble contacting crawling server with remote_get. Please try again or contact support.'.$response->get_error_message();
					$results = json_encode($results);
					echo $results;
					die();
				}
		//check for block or timeout
		//====================
		if (strpos($serverresponse, "Please wait while your request is being verified") !== false || !isset($serverresponse) || $serverresponse=='' || strpos($serverresponse, "Access denied by Imunify360 bot-protection.") !== false|| strpos($serverresponse, "403 Forbidden") !== false) {
		   //this site is greylisted by imunify360 on cloudways, call backup digital ocean server
		   $response = wp_remote_get( 'https://ocean.ljapps.com/crawlrevs.php?rip='.$ip_server.'&surl='.$siteurl.'&scrapeurl='.$listedurl.'&stype=angi&sfp=pro&nobot=1&nhful='.$nhful.'&locationtype=&scrapequery=&tempbusinessname=&pagenum='.$pagenum.'&nextpageurl='.$nextpageurl, array( 'sslverify' => false, 'timeout' => 60 ) );
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$serverresponse    = $response['body']; // use the content
			}
		}
		//=========================	
				$serverresponsearray = json_decode($serverresponse, true);
				
				//print_r($serverresponsearray);

				if($serverresponse=='' || !is_array($serverresponsearray)){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0001: trouble contacting crawling server. Please try again or contact support.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch limit error
				if($serverresponsearray['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002: '.$serverresponsearray['ackmessage'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				if(!isset($serverresponsearray['result']) || !is_array($serverresponsearray['result'])){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0002b: trouble finding reviews. Contact support with this error code and the search terms or place id you are using.';
					$results = json_encode($results);
					echo $results;
					die();
				}
				//catch error
				if($serverresponsearray['result']['ack']=='error'){
					$results['ack'] ='error';
					$results['ackmsg'] ='Error 0003: '.$serverresponsearray['ackmessage'].' : '.$serverresponsearray['result']['ackmsg'];
					$results = json_encode($results);
					echo $results;
					die();
				}
				//made it this far assume we have reviews.
				$crawlerresultarray = $serverresponsearray['result'];
				

				//need totals and avg for this place $getreviewsarray['total']
				$result['total']='';
				$result['avg']='';
				if(isset($crawlerresultarray['avg'])){
					$result['avg']=floatval($crawlerresultarray['avg']);
				}
				if(isset($crawlerresultarray['total'])){
					$result['total']=$crawlerresultarray['total'];
				}
				
				//pass back URL used
				if(isset($crawlerresultarray['callurl'])){
					$result['callurl']=$crawlerresultarray['callurl'];
				}
				//pass back next URL used
				if(isset($crawlerresultarray['nextpageurl'])){
					$result['nextpageurl']=$crawlerresultarray['nextpageurl'];
				}
				
				$x=0;
				$crawlerreviewsarray = $crawlerresultarray['reviews'];
				$numreturned = count($crawlerreviewsarray);	
				
				foreach ($crawlerreviewsarray as $review) {
					
					$tempownerres='';
					if(isset($review['owner_response']) && $review['owner_response']!=''){
						$tempownerres = $review['owner_response'];
					}
					$templocation ='';
					if(isset($review['location']) && $review['location']!=''){
						$templocation = $review['location'];
					}	
					$tempmediaurlsarrayjson ='';
					if(isset($review['mediaurlsarrayjson']) && $review['mediaurlsarrayjson']!=''){
						$tempmediaurlsarrayjson = $review['mediaurlsarrayjson'];
					}					
					
					$reviewsarray[] = [
					 'reviewer_name' => $review['user_name'],
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => $review['userimage'],
					 'rating' => $review['rating'],
					 'updated' => $review['datesubmitted'],
					 'review_text' => $review['rtext'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => $review['from_url_review'],
					 'language_code' => '',
					 'location' => $templocation,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => $tempmediaurlsarrayjson,
					 'owner_response' => $tempownerres,
					 ];
					
					$x++;
				}
				
				//if we find less than 10 then do not loop again.
					if(count($reviewsarray)<10){
						$result['stoploop']='stop';
					}

			$result['reviews'] = $reviewsarray;

			}
			
			//print_r($result);
			
		return $result;
				
	}
	
		//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_fresha($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();

		$result['avg']='';
		$result['total']='';
			
		if($listedurl=='' || filter_var($listedurl, FILTER_VALIDATE_URL) === FALSE){
			//must have been an error
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Make sure to enter the correct Fresha page URL.';
			$results = json_encode($results);
			echo $results;
			die();
		}

		
		//get the total and avg first
		if($pagenum <2){
			$response = wp_remote_get( $listedurl, array( 'timeout' => 30,
					'headers' => array( 'Content-Type' => 'application/json',
                               'Accept'=> 'application/json' ) 
					));
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$body    = $response['body']; // use the content
			} else {
				$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
			}
			
			$avg = $this->get_string_between($body, ',"ratingValue":', ',');
			$total = $this->get_string_between($body, '"reviewCount":', ',');
			$avg = (float)$avg;
			if(intval($total)>0){
				$result['total']=$total;
			}
			if($avg>0){
				$result['avg']=$avg;
			}
		}
		
		//print_r($result);
		//die();
		
		//grab the slug from the listedurl
		//https://www.fresha.com/a/morgan-company-hair-beauty-nottingham-unit-2-riverbank-business-park-uk-w3xcsptr
		$slug = $this->get_string_between($listedurl, 'fresha.com/a/', '#');
			
		$endpoint = 'https://b2c-api-gateway.fresha.com/graphql';
		$nextid = "";
		if($nextpageurl!=''){
			$nextid = $nextpageurl;
		}

		$body = '{"query":"\n query locationReviewsModal($slug: String!, $reviews: Int!, $id: ID, $ratings: [RatingValue!], $sortingType: ReviewSortingType) {\n  location(slug: $slug) {\n reviews(\n first: $reviews\n after: $id\n ratings: $ratings\n sortingType: $sortingType\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n author {\n avatar {\n url(class: THUMB)\n }\n name\n }\n date {\n longDate\n }\n id\n rating\n text\n reply {\n isOnlyVisibleToAuthor\n text\n date {\n longDate\n }\n author {\n avatar {\n url(class: THUMB)\n }\n name\n }\n }\n }\n }\n totalCount\n }\n  }\n}\n",
				   "variables":{
					  "id":"'.$nextid.'",
					  "reviews":5,
					  "ratings":[],
					  "slug":"'.$slug.'",
					  "sortingType":"LATEST"
				   },
				   "operationName":"locationReviewsModal",
				   "extensions":{
					  "platform":"web",
					  "version":"2.8.829"
				   }
				}';

		$options = [
			'body'        => $body,
			'headers'     => [
				'Content-Type' => 'application/json',
			],
			'timeout'     => 60,
			'redirection' => 5,
			'blocking'    => true,
			'httpversion' => '1.0',
			'sslverify'   => false,
			'data_format' => 'body',
		];

		$response = wp_remote_post( $endpoint, $options );
		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$fileurlcontents = $response['body'];	
			
		$reviewjsonarray = json_decode($fileurlcontents,true);
		
		//print_r($reviewjsonarray);
		
		if(is_array($reviewjsonarray) && isset($reviewjsonarray['data']['location']['reviews']['edges'])){
			$reviewcontainerarray = $reviewjsonarray['data']['location']['reviews']['edges'];
		} else {
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0003: Unable to find reviews. ';
			$results = json_encode($results);
			echo $results;
			die();
		}
		

		foreach ($reviewcontainerarray as $review) {
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$from_url_review='';
				$company_title='';
				$unique_id='';
				$reviewer_id='';
				$title='';
				$reviewer_email='';
				$userpic='';
				$from_url_review='';
				$from_url='';
				$location='';
				
			//for finding next page of reviews.
			if($review['cursor'] && $review['cursor']!=''){
			$result['nextpageurl']=$review['cursor'];
			}
			
			// Find unique id
			if($review['node']['id']){
				$unique_id=$review['node']['id'];
			}
			
			// Find user_name
			if($review['node']['author']['name']){
				$user_name=$review['node']['author']['name'];
			}


			if($review['node']['author']['avatar']){
				$userpic=$review['node']['author']['avatar']['url'];
			}

			//find rating
			if($review['node']['rating']){
				$rating=$review['node']['rating'];
			}
			
			//find date created_at
			if($review['node']['date']['longDate']){
				$datesubmitted=$review['node']['date']['longDate'];
				$datesubmitted= substr($datesubmitted, 0, strpos($datesubmitted, ","));
			}
			
			//find text
			if($review['node']['text']){
				$rtext=$review['node']['text'];
			}
			
			//owner_response
			$owner_response_encode ='';
			$owner['id'] = '';
			$owner['name'] = '';
			$owner['comment'] = '';
			$owner['date'] = '';
			//mgrRspnInline
			if(isset($review['node']['reply']) && $review['node']['reply']['text']){
				$replyarray=$review['node']['reply'];
				//must be a response
				$owner['name'] = $replyarray['author']['name'];

				//responseDate
				$ownerdate = $replyarray['date']['longDate'];
				$tempdate = $this->myStrtotime($ownerdate);

				$owner['date'] = date('Y-m-d', $tempdate);
				$owner['comment'] = $replyarray['text'];
			}
			if($owner['comment']!=''){
				$owner_response_encode = json_encode($owner);
			}

			if($rating>0){
				$reviewsarray[] = [
					 'reviewer_name' => trim($user_name),
					 'reviewer_id' => trim($reviewer_id),
					 'reviewer_email' => $reviewer_email,
					 'userpic' => $userpic,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => trim($rtext),
					 'review_title' => trim($title),
					 'from_url' => $from_url,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'unique_id' => $unique_id,
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => $owner_response_encode,
					 ];
			}
		}

		$result['reviews'] = $reviewsarray;
		
		//print_r($reviewsarray);
				
		//die();

		return $result;
	}
	

	//-----------------------------
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_creativemarket($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();

		$result['avg']='';
		$result['total']='';
		
		$apikey = "a-demo-key-with-low-quota-per-ip-address";
			
		if($listedurl=='' || filter_var($listedurl, FILTER_VALIDATE_URL) === FALSE){
			//must have been an error
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0001: Make sure to enter the correct Creative Market page URL.';
			$results = json_encode($results);
			echo $results;
			die();
		}

		$tempphantomurl = 'https://phantomjscloud.com/api/browser/v2/'.$apikey.'/?request={url:%22'.$listedurl.'%22,renderType:%22html%22,requestSettings:{doneWhen:[{event:%22domReady%22}]}}';

		//get the total and avg first
		if($pagenum <2){
			$response = wp_remote_get( $tempphantomurl, array( 'timeout' => 30,
					'headers' => array( 'Content-Type' => 'application/json',
                               'Accept'=> 'application/json' ) 
					));
			if ( is_array( $response ) && ! is_wp_error( $response ) ) {
				$headers = $response['headers']; // array of http header lines
				$body    = $response['body']; // use the content
			} else {
				$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
			}
			
			//print_r($response);
			//echo "here";
			
			$avg = $this->get_string_between($body, ',"averageReviewRating":', ',');
			$total = $this->get_string_between($body, ',"numReviewRatings":', ',');
			$avg = (float)$avg;
			if(intval($total)>0){
				$result['total']=$total;
			}
			if($avg>0){
				$result['avg']=$avg;
			}
		}
		
		
		//grab the id from the listedurl and then call the api url to get json of reviews.
		//https://creativemarket.com/mila.garret/6817168-Harlow-Serif-Script-Modern-Font-Duo
		$start = "/";
		$end = "-";
		$string = " ".$listedurl;
		$ini = strrpos($string,$start);
		//if ($ini == 0) return "";
		$ini += strlen($start);
		$len = strpos($string,$end,$ini) - $ini;
		$prodid = substr($string,$ini,$len);

		
		$newurl = 'https://creativemarket.com/product/'.$prodid.'/reviews/10/'.$pagenum.'?sort=date&sortBy=desc&contextID='.$prodid.'&limitPage=10&page='.$pagenum.'&sorting=%7B%22id%22:1,%22text%22:%22Most+Recent%22,%22key%22:%22date%22,%22type%22:%22desc%22%7D';
		
		$newurl = urlencode($newurl);
		
		$newurlphantom = 'https://phantomjscloud.com/api/browser/v2/'.$apikey.'/?request={url:%22'.$newurl.'%22,renderType:%22html%22,requestSettings:{doneWhen:[{event:%22domReady%22}]}}';
		
		//echo $newurlphantom;
		//die();
		sleep(3);
		
		$response = wp_remote_get( $newurlphantom, array( 'timeout' => 30,
					'headers' => array( 'Content-Type' => 'application/json',
                               'Accept'=> 'application/json' ) 
					));
		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0002: Something went wrong. '.$error_message;
			$results = json_encode($results);
			echo $results;
			die();
		}
		
		$fileurlcontents = trim($response['body']);
		
		$fileurlcontents = $this->get_string_between($fileurlcontents, '<pre style="word-wrap: break-word; white-space: pre-wrap;">', '</pre>');
		

		$reviewjsonarray = json_decode($fileurlcontents,true);

//print_r($reviewjsonarray);
//die();
		
		if(is_array($reviewjsonarray) && isset($reviewjsonarray['reviews'])){
			$reviewcontainerarray = $reviewjsonarray['reviews'];
		} else {
			$results['ack'] ='error';
			$results['ackmsg'] ='Error 0003: Unable to find reviews. ';
			$results = json_encode($results);
			echo $results;
			die();
		}
		

		foreach ($reviewcontainerarray as $review) {
				$user_name='';
				$userimage='';
				$rating='';
				$datesubmitted='';
				$rtext='';
				$from_url_review='';
				$company_title='';
				$unique_id='';
				$reviewer_id='';
				$title='';
				$reviewer_email='';
				$userpic='';
				$from_url_review='';
				$from_url='';
				$location='';

			
			// Find unique id
			if($review['id']){
				$unique_id=$review['id'];
			}
			
			// Find user_name
			if($review['user']['fullName']){
				$user_name=$review['user']['fullName'];
			}


			if($review['user']['avatar']['l']){
				$userpic=$review['user']['avatar']['l'];
			}

			//find rating
			if($review['rating']){
				$rating=$review['rating'];
			}
			
			//find date created_at title=\"
			if($review['timeSince']){
				$temptime = $this->get_string_between($review['timeSince'], '&gt;', '&lt;');
				$datesubmitted=$temptime;
			}

			
			//find text
			if($review['reviewText']){
				$rtext=$review['reviewText'];
			}
			
			//owner_response
			$owner_response_encode ='';
			$owner['id'] = '';
			$owner['name'] = '';
			$owner['comment'] = '';
			$owner['date'] = '';
			//mgrRspnInline
			if(isset($review['reply']) && $review['reply']['replyText']){
				$replyarray=$review['reply'];
				//must be a response
				$owner['name'] = "Owner Response";

				//responseDate
				$ownerdate = $this->get_string_between($review['timeSince'], '&gt;', '&lt;');
				$tempdate = $this->myStrtotime($ownerdate);

				$owner['date'] = date('Y-m-d', $tempdate);
				$owner['comment'] = $replyarray['replyText'];
			}
			if($owner['comment']!=''){
				$owner_response_encode = json_encode($owner);
			}

			if($rating>0){
				$reviewsarray[] = [
					 'reviewer_name' => trim($user_name),
					 'reviewer_id' => trim($reviewer_id),
					 'reviewer_email' => $reviewer_email,
					 'userpic' => $userpic,
					 'rating' => $rating,
					 'updated' => $datesubmitted,
					 'review_text' => trim($rtext),
					 'review_title' => trim($title),
					 'from_url' => $from_url,
					 'from_url_review' => $from_url_review,
					 'language_code' => '',
					 'unique_id' => $unique_id,
					 'location' => $location,
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'mediaurlsarrayjson' => '',
					 'owner_response' => $owner_response_encode,
					 ];
			}
		}

		$result['reviews'] = $reviewsarray;
		
		if(count($reviewcontainerarray)<10){
			//no need to loop again.
			$result['stoploop'] = "stop";
		}
				
		//die();

		return $result;
	}
	
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_realtor($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid,$blockstoinsert,$nextpageurl){
		$result['ack']='success';
		$errormsg='';
		$reviewsarray = Array();

		$result['avg']='';
		$result['total']='';

				//echo $callurl;
				$result['listedurl'] =$listedurl;
				$response = wp_remote_get( $listedurl );
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$listedurl;
				}
				
				$fileurlcontents = $response['body'];

				//echo $fileurlcontents;
				//die();
				
				//get the review schema from html string
				$schemastring = $this->get_string_between($fileurlcontents, '<script strategy="afterInteractive" type="application/ld+json">', '</script>');
				
				$pagedata = json_decode( $schemastring, true );
				//print_r($pagedata);
				//die();

				$result['total']=$pagedata[0]['aggregateRating']['reviewCount'];
				$result['avg']=$pagedata[0]['aggregateRating']['ratingValue'];
				
				$reviewdata = "[".$this->get_string_between($fileurlcontents, '"feedbackData":[', ']')."]";
				$reviewdataarray = json_decode( $reviewdata, true );
				
				$recommendationdata = "[".$this->get_string_between($fileurlcontents, '"recommendationData":[', ']')."]";

				$recommendationdataarray = json_decode( $recommendationdata, true );

				$allrevsarray = array_merge($reviewdataarray,$recommendationdataarray);
				
				//loop reviews and build new array of just what we need
				foreach ($allrevsarray as $review) {
					
					//print_r($review);
					//die();
					$user_name='';
					$userimage='';
					$rating='';
					$datesubmitted='';
					$rtext='';
					$from_url_review='';
					$location='';
					$meta_json ="";
					$meta_data = Array();

					if(isset($review['display_name'])){
						$user_name=$review['display_name'];
					}
					if(isset($review['comment'])){
						$rtext=$review['comment'];
					}
					if(isset($review['rating'])){
						$rating=$review['rating'];
						$meta_data['rtype']="review";
					}
					if($rating==''){
						//must be recommendation just give it a 5 star
						$rating=5;
						$meta_data['rtype']="recommendation";
					}
					
					if(isset($review['location'])){
						$location=$review['location'];
					}
					if($review['started_timestamp']){
						$datesubmitted=$review['started_timestamp'];
					}
						
					
					if(isset($review['transaction_date'])){
						$meta_data['transaction_date'] = $review['transaction_date'];
					}
					if(isset($review['trixel_url'])){
						$meta_data['trixel_url'] = $review['trixel_url'];
					}
					if(isset($review['describe_yourself'])){
						$meta_data['describe_yourself'] = $review['describe_yourself'];
					}
					if(isset($review['responsiveness'])){
						$meta_data['responsiveness'] = $review['responsiveness'];
					}
					if(isset($review['negotiation_skills'])){
						$meta_data['negotiation_skills'] = $review['negotiation_skills'];
					}
					if(isset($review['professionalism_communication'])){
						$meta_data['professionalism_communication'] = $review['professionalism_communication'];
					}
					if(isset($review['market_expertise'])){
						$meta_data['market_expertise'] = $review['market_expertise'];
					}

					if(isset($review['link'])){
						$meta_data['link'] = $review['link'];
					}
					if(count($meta_data)>0){
						$meta_json = json_encode($meta_data);
					}
					
					$owner_response_encode ='';
					$owner['id'] = '';
					$owner['name'] = '';
					$owner['comment'] = '';
					$owner['date'] = '';
					$reviewer_email = '';
					if(isset($review['reply']) and is_array($review['reply'])){
						$owner['comment'] = $review['reply']['message'];
						$owner['date'] = $review['reply']['reply_date'];
						$owner['name'] = $review['reply']['personName'];
						$reviewer_email = $review['reply']['reviewer_email'];
					}
					if($owner['comment']!=''){
						$owner_response_encode = json_encode($owner);
					}
					
						
					 $reviewsarrayfinal[] = [
						 'reviewer_name' => $user_name,
						 'reviewer_id' => '',
						 'reviewer_email' => $reviewer_email,
						 'userpic' => '',
						 'rating' => $rating,
						 'updated' => $datesubmitted,
						 'review_text' => $rtext,
						 'review_title' => '',
						 'from_url' => $listedurl,
						 'from_url_review' => '',
						 'language_code' =>'',
						 'location' => $location,
						 'recommendation_type' => '',
						 'company_title' =>  '',
						 'company_url' => '',
						 'company_name' => '',
						 'meta_data' => $meta_json ,
						 'owner_response' => $owner_response_encode,
						 ];
				}

				$result['reviews'] = $reviewsarrayfinal;

		return $result;
	}
	
	
	
	//for calling remote get and returning array of reviews to insert
	public function wprp_getapps_getrevs_page_planway($type,$listedurl,$pagenum,$perpage,$savedpageid,$nhful,$fid){
		//define( 'MAX_FILE_SIZE', 10000000 );
		ini_set('memory_limit','500M');
		$result['ack']='success';
		
			$errormsg='';
			$callurl = $listedurl;
			$reviewsarraytemp = Array();
			
			if (filter_var($callurl, FILTER_VALIDATE_URL) && $pagenum=1) {
					
				$stripvariableurl = stripslashes($callurl);
				$listedurl = strtok($stripvariableurl, '?');	//remove all parameters
				
				//https://hvorfor-gaa-og-have-ondt.planway.com/widget/reviews
				//only getting first page. 50 reviews max.
				
				$callurl = $listedurl;

				//echo $callurl;
				$result['callurl'] =$callurl;
				$args = array(
					'timeout'     => 60,
					'sslverify' => false
				); 
				
				$response = wp_remote_get($callurl,$args);
				if ( is_array( $response ) && ! is_wp_error( $response ) ) {
					$headers = $response['headers']; // array of http header lines
					$body    = $response['body']; // use the content
				} else {
					$result['ack']=esc_html__('Error: Can not use remote get on this url:', 'wp-review-slider-pro').' '.$callurl;
				}
				
				$fileurlcontents = $response['body'];
				$html = wppro_str_get_html($fileurlcontents,true,false);
				
				//echo dirname(__FILE__);
				
				//file_put_contents(dirname(__FILE__)."/tempplanwayrevs.txt", $fileurlcontents);
				
				//echo $fileurlcontents;
				//die();
				
				//sleep(2);
				//$fileurlcontents =file_get_contents(dirname(__FILE__)."/tempplanwayrevs.txt");
				//$html = wppro_str_get_html($fileurlcontents);
				
				//$html = wppro_file_get_html(dirname(__FILE__)."/tempplanwayrevs.txt");
				
				//$html = file_get_contents(dirname(__FILE__)."/tempplanwayrevs.txt");   
				
				//echo $html;
				//die();
				
				//$html = wppro_str_get_html($html,true,false);
				
				$result['avg']='';
				$result['total']='';
				/*
				if($html->find('meta[itemprop=reviewCount]', 0)){
						$result['total']= $html->find('meta[itemprop=reviewCount]', 0)->content;
						$result['total']=intval($result['total']);
				}
				if($html->find('meta[itemprop=ratingValue]', 0)){
						$result['avg']= $html->find('meta[itemprop=ratingValue]', 0)->content;
				}
				*/
				
				$reviewcontainerdiv = Array();
				//get the array of review container class
				if($html->find('div[class=review-box]', 0)){
					$reviewcontainerdiv = $html->find('div[class=review-box]');
				}

				//print_r($reviewcontainerdiv);
				//die();

				foreach ($reviewcontainerdiv as $review) {
						$user_name='';
						$userimage='';
						$rating='';
						$datesubmitted='';
						$rtext='';
						$from_url_review='';
						$location='';
						
					// Find user_name
					if($review->find('div[class=name]', 0)){
						$user_name= $review->find('div[class=name]', 0)->plaintext;
					}

					//find rating
					if($review->find('div[class=stars]', 0)){
							$rating = count($review->find('i[class=fas fa-star]'));
					}
					
					//find date created_at
					if($review->find('div[class=datebox]', 0)){
							$datesubmitted= $review->find('div[class=datebox]', 0)->plaintext;
							$datesubmitted=str_replace("."," ",$datesubmitted);
							
					}
					//find text, look for expandable text first $rtext
					if($review->find('div[class=text]', 0)){
						$rtext= $review->find('div[class=text]', 0)->plaintext;
					} 
					
					
					$recommend="";
					$meta_json ="";
					$meta_data = Array();
					
					if($rating>0){
						$reviewsarraytemp[] = [
								'reviewer_name' => trim($user_name),
								'rating' => $rating,
								'date' => $datesubmitted,
								'review_text' => trim($rtext),
								'type' => $type,
								'location' => $location
						];
					}
				}
				//print_r($reviewsarraytemp);
				
				//loop reviews and build new array of just what we need
				foreach ($reviewsarraytemp as $item) {
					 $reviewsarrayfinal[] = [
					 'reviewer_name' => trim($item['reviewer_name']),
					 'reviewer_id' => '',
					 'reviewer_email' => '',
					 'userpic' => '',
					 'rating' => $item['rating'],
					 'updated' => $item['date'],
					 'review_text' => $item['review_text'],
					 'review_title' => '',
					 'from_url' => $listedurl,
					 'from_url_review' => '',
					 'language_code' =>'',
					 'location' => $item['location'],
					 'recommendation_type' => '',
					 'company_title' =>  '',
					 'company_url' => '',
					 'company_name' => '',
					 'meta_data' => '',
					 'mediaurlsarrayjson' => '',
					 ];
				}
				//print_r($reviewsarrayfinal);
				//die();
				$result['reviews'] = $reviewsarrayfinal;
				
			}
		
		return $result;
	}


	
	
	
	//used for getting avg and total from TripAdvisor showuserreview page
	public function tripadvisorgettotalavg($html,$listedurl){
		//find total and average number here
			$avgrating ='';
			$totalreviews = '';
			
			if($html=='' && $listedurl!=''){
				$args = array(
					'timeout'     => 15,
					'sslverify' => false
				); 
				$response = wp_remote_get( $listedurl,$args );
				if ( is_array( $response ) ) {
				  $header = $response['headers']; // array of http header lines
				  $fileurlcontents = $response['body']; // use the content
				} else {
					echo esc_html__('Error 01: Error finding reviews. Please contact plugin support.', 'wp-review-slider-pro');
					die();
				}

				$fileurlcontents = str_replace('=="', '', $fileurlcontents);
					
				$html = wppro_str_get_html($fileurlcontents);
				//echo "listedurl:".$listedurl;
			}
			
			
			//works for hotels
			if($html->find('div.ratingContainer', 0)){
				//echo "<br>avg:".$avgrating;
				//echo "<br>totalreviews:".$totalreviews;
				if($html->find('div.ratingContainer', 0)->find('span.ui_bubble_rating', 0)){
				$avgrating = $html->find('div.ratingContainer', 0)->find('span.ui_bubble_rating', 0)->alt;
				$avgrating = str_replace(" of 5 bubbles","",$avgrating);
				$avgrating = str_replace(" de 5 burbujas","",$avgrating);
				$avgrating = str_replace(",",".",$avgrating);
				$avgrating = (float)$avgrating;
				}
				if($html->find('div.ratingContainer', 0)->find('span.reviewCount', 0)){
				$totalreviews = $html->find('div.ratingContainer', 0)->find('span.reviewCount', 0)->plaintext;
				$totalreviews = str_replace(",","",$totalreviews);
				$totalreviews = intval($totalreviews);
				}
			}
			//works for resturants US
			if($html->find('div.userRating', 0)){
				//echo "<br>avg:".$avgrating;
				//echo "<br>totalreviews:".$totalreviews;
				if($html->find('div.userRating', 0)->find('span.ui_bubble_rating', 0)){
				$avgrating = $html->find('div.userRating', 0)->find('span.ui_bubble_rating', 0)->alt;
				$avgrating = str_replace(" of 5 bubbles","",$avgrating);
				$avgrating = str_replace(" de 5 burbujas","",$avgrating);
				$avgrating = str_replace(",",".",$avgrating);
				$avgrating = (float)$avgrating;
				}

				if($html->find('div.userRating', 0)->find('div.rating', 0)){
				$totalreviews = $html->find('div.userRating', 0)->find('div.rating', 0)->plaintext;
				$totalreviews = str_replace(",","",$totalreviews);
				$totalreviews = intval($totalreviews);
				}

			}
			
			//backup method for hotels
			if($avgrating==''){
					if($html->find('span.hotels-hotel-review-about-with-photos-Reviews__overallRating--vElGA', 0)){
					$avgrating = $html->find('span.hotels-hotel-review-about-with-photos-Reviews__overallRating--vElGA', 0)->plaintext;
					$avgrating = str_replace(",",".",$avgrating);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
					//$avgrating = $avgrating/10;
					}
					if($html->find('span.hotels-hotel-review-about-with-photos-Reviews__seeAllReviews--3PpLR', 0)){
					$totalreviews = $html->find('span.hotels-hotel-review-about-with-photos-Reviews__seeAllReviews--3PpLR', 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = intval($totalreviews);
					}
			}
			
			//if not found try backup method, currently used for resturants
			if($avgrating==''){
				if($html->find('div.rating_and_popularity', 0)){
					if($html->find('div.rating_and_popularity', 0)->find('span.ui_bubble_rating', 0)){
					$avgrating = $html->find('div.rating_and_popularity', 0)->find('span.ui_bubble_rating', 0)->alt;
					$avgrating = str_replace(" of 5 bubbles","",$avgrating);
					//fix for comma
					$avgrating = str_replace(",",".",$avgrating);
					$avgrating = (float)$avgrating;
					}
					if($html->find('div.rating_and_popularity', 0)->find('div.rating', 0)){
					$totalreviews = $html->find('div.rating_and_popularity', 0)->find('div.rating', 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = intval($totalreviews);
					}
				}
			}
			//finally one more try for vacationrental
			if($avgrating==''){
				if($html->find('div.ratingSystem', 0)){
					if($html->find('div.ratingSystem', 0)->find('span.ui_bubble_rating', 0)){
					$avgrating = $html->find('div.ratingSystem', 0)->find('span.ui_bubble_rating', 0)->class;
					$avgrating = str_replace(",",".",$avgrating);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
					$avgrating = $avgrating/10;
					}
					if($html->find('div.ratingSystem', 0)->find('span.based-on-n-reviews', 0)){
					$totalreviews = $html->find('div.ratingSystem', 0)->find('span.based-on-n-reviews', 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = str_replace("-","",$totalreviews);
					$totalreviews = str_replace("based on ","",$totalreviews);
					$totalreviews = preg_replace('/[^0-9.]+/', '', $totalreviews);
					}
				}
			}
			//finally one more try for attraction
			if($avgrating=='' || $totalreviews==''){
				if($html->find('div.ui_poi_review_rating ', 0)){
					if($html->find('div.ui_poi_review_rating ', 0)->find('span.ui_bubble_rating', 0)){
					$avgrating = $html->find('div.ui_poi_review_rating', 0)->find('span.ui_bubble_rating', 0)->class;
					$avgrating = str_replace(",",".",$avgrating);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
					$avgrating = $avgrating/10;
					}
					if($avgrating==''){
						if($html->find('div.ui_poi_review_rating ', 0)->find('span.ui_star_rating', 0)){
						$avgrating = $html->find('div.ui_poi_review_rating', 0)->find('span.ui_star_rating', 0)->class;
						$avgrating = str_replace(",",".",$avgrating);
						$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
						$avgrating = $avgrating/10;
						}
					}
					
					if($html->find('div.ui_poi_review_rating', 0)->find('span.reviewCount', 0)){
					$totalreviews = $html->find('div.ui_poi_review_rating', 0)->find('span.reviewCount', 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = str_replace("-","",$totalreviews);
					$totalreviews = str_replace("based on ","",$totalreviews);
					$totalreviews = preg_replace('/[^0-9.]+/', '', $totalreviews);
					}
				}
			}
			//another try for attraction
			if($avgrating=='' || $totalreviews==''){
				if($html->find("div[class=zTTYS8QR]", 0)){
					$avgrating = $html->find("div[class=zTTYS8QR]", 0)->{'aria-label'};
					$avgrating = substr($avgrating, 0, 3);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
				}
				if($html->find("span[class=DrjyGw-P _26S7gyB4 _14_buatE _2nPM5Opx]", 0)){
					$totalreviews = $html->find("span[class=DrjyGw-P _26S7gyB4 _14_buatE _2nPM5Opx]", 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = preg_replace('/[^0-9.]+/', '', $totalreviews);
				}
			}
			//another try for attraction
			if($avgrating=='' || $totalreviews==''){
				if($html->find("div[class=zTTYS8QR]", 0)){
					$avgrating = $html->find("div[class=zTTYS8QR]", 0)->{'aria-label'};
					$avgrating = substr($avgrating, 0, 3);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
				}
				if($html->find("span[class=_1yuvE2vR]", 0)){
					$totalreviews = $html->find("span[class=_1yuvE2vR]", 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = preg_replace('/[^0-9.]+/', '', $totalreviews);
				}
			}
			//another try for attraction
			if($avgrating=='' || $totalreviews==''){
				if($html->find("div[class=WlYyy cPsXC fksET cMKSg]", 0)){
					$avgrating = $html->find("div[class=WlYyy cPsXC fksET cMKSg]", 0)->plaintext;
					$avgrating = substr($avgrating, 0, 3);
					$avgrating = preg_replace('/[^0-9.]+/', '', $avgrating);
				}
				if($html->find("span[class=WlYyy diXIH bGusc dDKKM]", 0)){
					$totalreviews = $html->find("span[class=WlYyy diXIH bGusc dDKKM]", 0)->plaintext;
					$totalreviews = str_replace(",","",$totalreviews);
					$totalreviews = preg_replace('/[^0-9.]+/', '', $totalreviews);
				}
			}
			if($avgrating==''){
				
				
			}
			
		
		
		$response =array("avgrating"=>$avgrating,"totalreviews"=>$totalreviews);
		return $response;
	}
	
	public function wprevpro_download_tripadvisor_showuserreviews_url($currenturl) {
		ini_set('memory_limit','500M');

		if (strpos($currenturl, 'VacationRentalReview') !== false) {
			//this is a vactionrental
			$vactionrental = true;
		} else {
			$vactionrental = false;
		}
					

		$args = array(
			'timeout'     => 15,
			'sslverify' => false
		); 
		$response = wp_remote_get( $currenturl,$args );
		if ( is_array( $response ) ) {
		  $header = $response['headers']; // array of http header lines
		  $fileurlcontents = $response['body']; // use the content
		} else {
			echo esc_html__('Error finding reviews. Please contact plugin support. Error: ', 'wp-tripadvisor-review-slider');
			if( is_wp_error( $response ) ) {
				echo $response->get_error_message();
			}
			die();
		}
		
		
			//fix for lazy load base64 ""
			//$fileurlcontents = str_replace('=="', '', $fileurlcontents);
			
			//echo $fileurlcontents;
			//die();
			
			$html = wppro_str_get_html($fileurlcontents);
			//die();
			//unset($fileurlcontents);
			
			//find total and average number here
			$avgrating ='';
			$totalreviews = '';
			
			$reviewobject5 ="";
			$page2url ="";
			$rtitlelink ="";
			$nextbtnlink="";
			//echo $currenturl;
			//echo "<br><br>";
			

			//check to see if on vacation rental or regular page
			if($vactionrental==true){
				if($html->find('div.reviewSelector')){
					$reviewobject = $html->find('div.reviewSelector',0);
				} else {
					echo esc_html__('Error 102a: Unable to read Vacation Rental TripAdvisor page. Please contact support.', 'wp-tripadvisor-review-slider');
					die();	
				}
			} else {
				if($html->find('div.review-container')){
						$reviewobject = $html->find('div.review-container',0);
						$reviewobject5 = $html->find('div.review-container',5);
				}
			}
			
			//need to get the links
			if($html->find("div[class*=ReviewTitle]", 0)){
				$rtitlelink = $html->find("div[class*=ReviewTitle]", 0)->find('a',0)->href;

				if($html->find("div[class*=ReviewTitle]", 4)){
					$nextbtnlink = $html->find("div[class*=ReviewTitle]", 4)->find('a',0)->href;
				}
				//echo $rtitlelink;
				//echo 'h1';
			
			} else if($html->find("div[data-test-target*=review-title]", 0)){

				$rtitlelink = $html->find("div[data-test-target*=review-title]", 0)->find('a',0)->href;

				if($html->find("div[data-test-target*=review-title]", 4)){
					$nextbtnlink = $html->find("div[data-test-target*=review-title]", 4)->find('a',0)->href;
				}
				//echo $rtitlelink;
				//echo 'h2';
			
			} else if($html->find("div.glasR4aX", 0)){
				$rtitlelink = $html->find("div.glasR4aX", 0)->find('a',0)->href;

				if($html->find("div.glasR4aX", 4)){
					$nextbtnlink = $html->find("div.glasR4aX", 4)->find('a',0)->href;
				}
				//echo $rtitlelink;
				//echo 'h3';
			
			} else {
				//try to find links using string search here, had to add for italian languages
				$urlmatches = $this->getBetween($fileurlcontents, "/ShowUserReviews-", '"');

				//print_r($urlmatches2);
				//echo '<br><br>';
				if(isset($urlmatches[0]) && $urlmatches[0]!=''){
					$rtitlelink = "/ShowUserReviews-".$urlmatches['1'];
					$nextbtnlink = "/ShowUserReviews-".$urlmatches['6'];
					//echo $rtitlelink;
				//echo 'h4';
				} else if(isset($urlmatches[1]) && $urlmatches[1]!=''){
					$rtitlelink = "/ShowUserReviews-".$urlmatches['0'];
					$nextbtnlink = "/ShowUserReviews-".$urlmatches['5'];
					//echo $rtitlelink;
				//echo 'h4';
					
					
				} else {

					echo esc_html__('Error 103a: Unable to read TripAdvisor page. Please contact support.', 'wp-tripadvisor-review-slider');
					//echo $html;
					die();	
				}
			}

			
			//print_r($reviewobject);
			//echo "<br><br>";
			//print_r(reviewobject5);		
			
			if(isset($reviewobject) && $reviewobject!="" && $rtitlelink==''){
				$rtitlelink = $reviewobject->find('div.quote', 0)->find('a',0)->href;
				//echo $rtitlelink;
				//echo 'h5';
			}
			
			if($reviewobject5!="" && $nextbtnlink==''){
				$nextbtnlink = $reviewobject5->find('div.quote', 0)->find('a',0)->href;
			}
			
			$parseurl = parse_url($currenturl);
			$newurl = $parseurl['scheme'].'://'.$parseurl['host'].$rtitlelink; ;
			if($nextbtnlink!=''){
			$page2url = $parseurl['scheme'].'://'.$parseurl['host'].$nextbtnlink;
			}
			
			//$response =array("page1"=>$newurl,"page2"=>$page2url);
			
			$response =array("page1"=>$newurl,"page2"=>$page2url,"totalreviews"=>$totalreviews,"avgrating"=>$avgrating);
			$html->clear(); 
			unset($html);
			
			//print_r($response);
			//die();

			//create new link based on $currenturl and $rtitlelink
		return $response;
	}

	
  }
?>