379 lines
11 KiB
PHP
379 lines
11 KiB
PHP
<?php
|
|
|
|
/**
|
|
* JSONAPI controller.
|
|
*
|
|
* @author Ivan Filipov <ivan@web-lab.solutions>
|
|
*
|
|
* @version 1.0
|
|
* @date updated Feb 28, 2023
|
|
*/
|
|
|
|
namespace wlurep;
|
|
|
|
class JSONAPI {
|
|
|
|
private static $_instance = null;
|
|
private $_types = [];
|
|
private $_contractTypes = [];
|
|
private $_cities = [];
|
|
|
|
/**
|
|
* JSONAPI constructor.
|
|
*
|
|
* Initializing Register json API.
|
|
*
|
|
* @since 1.0.0
|
|
* @access private
|
|
*/
|
|
private function __construct() {
|
|
add_action('rest_api_init', [$this, 'init']);
|
|
}
|
|
|
|
public function init() {
|
|
register_rest_route('urep', '/update', [
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'update'],
|
|
'permission_callback' => '__return_true'
|
|
]);
|
|
// if we have to use post method
|
|
// register_rest_route('my/v1', '/projects_search', [
|
|
// 'methods' => 'POST',
|
|
// 'callback' => [$this, 'post_projects_search']
|
|
// ]);
|
|
}
|
|
|
|
public function update($params) {
|
|
$pluginDir = realpath(dirname(__FILE__) );
|
|
|
|
ini_set('post_max_size', '2048M');
|
|
ini_set('upload_max_filesize', "200M");
|
|
ini_set('max_input_time', 259200);
|
|
ini_set('max_execution_time', 259200);
|
|
ini_set('memory_limit', "1024M");
|
|
error_reporting(E_ALL & ~E_DEPRECATED);
|
|
ini_set('log_errors', true);
|
|
ini_set('error_log', $pluginDir . '/ErrorLog.txt');
|
|
set_time_limit(0);
|
|
$start = (int)microtime(true);
|
|
|
|
// Use this only if you have big Big BIG ... muscle!
|
|
set_time_limit(0);
|
|
ignore_user_abort(true);
|
|
ini_set('memory_limit', '-1');
|
|
|
|
$estateID = $params->get_param('estateid');
|
|
$page = $params->get_param('page');
|
|
$perPage = $params->get_param('perpage');
|
|
$disable_param = $params->get_param('deleted');
|
|
$disable = (!empty($disable_param))?true:false;
|
|
|
|
if(!$disable){
|
|
$apiURL = wl_get_option('api_url', '');
|
|
$apiToken = wl_get_option('api_key', '');
|
|
if($apiURL === ''){
|
|
$response = new \WP_REST_Response(["result" => 'failed', 'message' => __('API url is missing').'.']);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
if($apiToken === ''){
|
|
$response = new \WP_REST_Response(["result" => 'failed', 'message' => __('API token is missing').'.']);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
$apiParams = array(
|
|
'token' => $apiToken
|
|
);
|
|
if (!empty($estateID)) {
|
|
$apiParams['estateID'] = $estateID;
|
|
}
|
|
$info = json_decode(\wlurep\SimpleCurl::get($apiURL, $apiParams), true)['properties'];
|
|
$this->_types = $this->getTerms('property-type');
|
|
$this->_contractTypes = $this->getTerms('property-contract-type');
|
|
$this->_cities = $this->getTerms('city-type');
|
|
|
|
$istart = 0;
|
|
$iend = count($info);
|
|
if (isset($page)) {
|
|
if (empty($perPage)) {
|
|
$perPage = 10;
|
|
}
|
|
if(($page*$perPage)>$istart && ($page*$perPage)<$iend){
|
|
$istart = (int)$page*$perPage;
|
|
}
|
|
if((($page+1)*$perPage)<$iend && (($page+1)*$perPage)>$istart){
|
|
$iend = (int)(($page+1)*$perPage);
|
|
|
|
}
|
|
}
|
|
|
|
for ($i = $istart; $i < $iend; $i++) {
|
|
$propery = $info[$i];
|
|
$propertyData = array(
|
|
'estateid' => $propery['uid'],
|
|
'title' => (!empty($propery['titleBG']))?$propery['titleBG']:mb_substr($propery['DescriptionBG'], 0, 20),
|
|
'description' => $propery['DescriptionBG'],
|
|
'property-contract-type' => $propery['listing_type_name'], // taxonomy
|
|
'property-type' => $propery['estate_type_name'], // taxonomy
|
|
'city-type' => $propery['region_name'], // taxonomy
|
|
'imic_property_site_address' => $propery['subregion_name'],
|
|
'imic_property_price' => $propery['price'],
|
|
'imic_property_area' => $propery['space_m2'],
|
|
'imic_property_beds' => $propery['bedroomCount'],
|
|
'imic_property_baths' => (!empty($propery['bathCount']) && (int)$propery['bathCount']>0)?$propery['bathCount']:((!empty($propery['bathToiletCount']) && (int)$propery['bathToiletCount']>0)?$propery['bathCount']:0),
|
|
'photos' => $propery['photos']
|
|
);
|
|
$this->updateProperty($propertyData);
|
|
}
|
|
}else{
|
|
if(!empty($estateID)){
|
|
$this->disableProperty($estateID);
|
|
}
|
|
}
|
|
|
|
$end = (int)microtime(true);
|
|
$spend = (int)($end - $start);
|
|
|
|
$response = new \WP_REST_Response(["result" => 'done', 'start' => date('d.m.Y H:i:s', $start), 'end' => date('d.m.Y H:i:s', $end), 'total time' => $spend]);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param array $propertyData
|
|
*/
|
|
private function updateProperty($propertyData = []) {
|
|
if (!is_array($propertyData) || count($propertyData) < 1) {
|
|
return;
|
|
}
|
|
|
|
$propertyID = $this->getPropertyID($propertyData['estateid'], $propertyData['title'], $propertyData['description']);
|
|
$typeID = $this-> getTermID('types', $propertyData['property-type'], 'property-type');
|
|
$contractTypesID = $this-> getTermID('contractTypes', $propertyData['property-contract-type'], 'property-contract-type');
|
|
$cityID = $this-> getTermID('cities', $propertyData['city-type'], 'city-type');
|
|
|
|
//update post
|
|
$post = array(
|
|
'ID' => $propertyID,
|
|
'post_title' => $propertyData['title'],
|
|
'post_content' => $propertyData['description'],
|
|
'post_author' => 1,
|
|
'post_status' => 'publish'
|
|
);
|
|
wp_update_post($post);
|
|
|
|
// update terms
|
|
wp_set_post_terms( $propertyID, array( (int) $typeID ), 'property-type', false );
|
|
wp_set_post_terms( $propertyID, array( (int) $contractTypesID ), 'property-contract-type', false );
|
|
wp_set_post_terms( $propertyID, array( (int) $cityID ), 'city-type', false );
|
|
|
|
// update metadata
|
|
update_post_meta($propertyID, 'imic_property_site_address', $propertyData['imic_property_site_address']);
|
|
update_post_meta($propertyID, 'imic_property_price', $propertyData['imic_property_price']);
|
|
update_post_meta($propertyID, 'imic_property_area', $propertyData['imic_property_area']);
|
|
update_post_meta($propertyID, 'imic_property_beds', $propertyData['imic_property_beds']);
|
|
update_post_meta($propertyID, 'imic_property_baths', $propertyData['imic_property_baths']);
|
|
update_post_meta($propertyID, 'imic_featured_property', 0);
|
|
|
|
// update photos
|
|
$this->updatePhotos($propertyID, $propertyData['photos']);
|
|
}
|
|
|
|
private function disableProperty($estateid) {
|
|
$property = $this->getProperty($estateid);
|
|
if(!empty($property)){
|
|
// change visibility to Private
|
|
$post = array(
|
|
'ID' => $property[0]->ID,
|
|
'post_status' => 'private'
|
|
);
|
|
wp_update_post($post);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $estateid
|
|
* @return object or null
|
|
*/
|
|
private function getProperty($estateid) {
|
|
$property = get_posts(array(
|
|
'post_type' => 'property',
|
|
'post_status' => array('private', 'publish'),
|
|
'posts_per_page' => 1,
|
|
'meta_query' => array(
|
|
array(
|
|
'key' => 'estateid',
|
|
'value' => $estateid,
|
|
'compare' => '='
|
|
)
|
|
)
|
|
));
|
|
return $property;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param int $estateid
|
|
* @param string $title
|
|
* @param string $description
|
|
* @return int
|
|
*/
|
|
private function getPropertyID($estateid = null, $title = '', $description = '') {
|
|
if (!$estateid) {
|
|
return;
|
|
}
|
|
$property = $this->getProperty($estateid);
|
|
if(empty($property)){
|
|
$propertyID = $this->createProperty($estateid, $title, $description);
|
|
}else{
|
|
$propertyID = $property[0]->ID;
|
|
}
|
|
return $propertyID;
|
|
}
|
|
/**
|
|
*
|
|
* @param int $estateid
|
|
* @param string $title
|
|
* @param string $description
|
|
* @return int
|
|
*/
|
|
|
|
private function createProperty($estateid = null, $title = '', $description = '') {
|
|
if (!$estateid) {
|
|
return;
|
|
}
|
|
$post = array(
|
|
'post_name' => $title,
|
|
'post_title' => $title,
|
|
'post_content' => $description,
|
|
'post_status' => 'publish',
|
|
'post_type' => 'property'
|
|
);
|
|
$property_id = wp_insert_post($post);
|
|
update_post_meta($property_id, 'estateid', $estateid);
|
|
return $property_id;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $taxonomiName
|
|
* @return array
|
|
*/
|
|
private function getTerms($taxonomiName) {
|
|
$terms = [];
|
|
$termsObj = get_terms($taxonomiName);
|
|
foreach ($termsObj as $term) {
|
|
$terms[$term->slug] = array(
|
|
'term_id' => $term->term_id,
|
|
'term_name' => $term->name
|
|
);
|
|
}
|
|
return $terms;
|
|
}
|
|
/**
|
|
*
|
|
* @param string $termType
|
|
* @param string $termName
|
|
* @param string $taxonomyName
|
|
* @return integer
|
|
*/
|
|
private function getTermID($termType, $termName, $taxonomyName = '') {
|
|
$termSlug = sanitize_title($termName);
|
|
$terms = $this->{'_'.$termType};
|
|
if(in_array($termSlug, $terms)){
|
|
$termID = $terms[$termSlug]['term_id'];
|
|
}else{
|
|
$termID = $this->insertTerm($taxonomyName, $termName);
|
|
}
|
|
return $termID;
|
|
}
|
|
/**
|
|
*
|
|
* @param string $termType
|
|
* @param string $termName
|
|
* @return type integer
|
|
*/
|
|
private function insertTerm($termType, $termName){
|
|
if(!function_exists('wp_create_term')){
|
|
require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
|
|
}
|
|
$newTerm = \wp_create_term( $termName, $termType );
|
|
|
|
$termID = $newTerm['term_id'];
|
|
|
|
return $termID;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param type $propertyID
|
|
* @param type $photos
|
|
*/
|
|
private function updatePhotos($propertyID, $photos) {
|
|
if(!is_array($photos)){
|
|
return;
|
|
}
|
|
// Delete old photos
|
|
$this->deleteOldPhotos($propertyID);
|
|
|
|
$url0 = $photos[0]['url'];
|
|
$post_title = get_the_title($propertyID);
|
|
if(empty($post_title) || $post_title == ''){
|
|
$post_title = basename($url0);
|
|
}
|
|
$attachment_data = array(
|
|
'title' => $post_title,
|
|
'caption' => $post_title,
|
|
'alt_text' => $post_title,
|
|
'description' => $post_title,
|
|
);
|
|
$download_remote_image = new \wlurep\WlDownloadRemoteImage( $url0, $attachment_data );
|
|
$attachment_id = $download_remote_image->download();
|
|
|
|
|
|
if ( ! $attachment_id ) {
|
|
return false;
|
|
}
|
|
|
|
set_post_thumbnail( $propertyID, $attachment_id );
|
|
unset($photos[0]);
|
|
|
|
|
|
foreach ($photos as $phpto) {
|
|
$url = $phpto['url'];
|
|
$download_remote_image = new \wlurep\WlDownloadRemoteImage( $url, $attachment_data );
|
|
$attachment_id = $download_remote_image->download();
|
|
add_post_meta($propertyID, 'imic_property_sights', $attachment_id);
|
|
}
|
|
}
|
|
|
|
private function deleteOldPhotos($propertyID) {
|
|
$oldPhotos = get_post_meta($propertyID , 'imic_property_sights');
|
|
$featuredImageID = get_post_thumbnail_id( $propertyID );
|
|
if(!empty($featuredImageID)){
|
|
$oldPhotos[] = $featuredImageID;
|
|
}
|
|
foreach ($oldPhotos as $oldPhotoID) {
|
|
wp_delete_attachment( $oldPhotoID, true );
|
|
}
|
|
delete_post_meta( $propertyID, 'imic_property_sights', null );
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return \wlurep\JSONAPI
|
|
*/
|
|
public static function getInstance() {
|
|
if (self::$_instance == null) {
|
|
self::$_instance = new self;
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
|
|
}
|
|
|