Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Import Properties by XML
- Plugin URI:
- description: A Custom plugin for Import Properties by XML
- Version: 1
- Author: Mrs. R.Kalra
- Author URI:
- License: GPL2
- */
- function wpb_image_editor_default_to_gd( $editors ) {
- $gd_editor = 'WP_Image_Editor_GD';
- $editors = array_diff( $editors, array( $gd_editor ) );
- array_unshift( $editors, $gd_editor );
- return $editors;
- }
- add_filter( 'wp_image_editors', 'wpb_image_editor_default_to_gd' );
- // Register Custom Taxonomy
- function custom_taxonomy() {
- $labels = array(
- 'name' => _x( 'Taxonomiesxx', 'Taxonomy General Name', 'text_domain' ),
- 'singular_name' => _x( 'Taxonomyxx', 'Taxonomy Singular Name', 'text_domain' ),
- 'menu_name' => __( 'Taxonomy', 'text_domain' ),
- 'all_items' => __( 'All Items', 'text_domain' ),
- 'parent_item' => __( 'Parent Item', 'text_domain' ),
- 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
- 'new_item_name' => __( 'New Item Name', 'text_domain' ),
- 'add_new_item' => __( 'Add New Item', 'text_domain' ),
- 'edit_item' => __( 'Edit Item', 'text_domain' ),
- 'update_item' => __( 'Update Item', 'text_domain' ),
- 'view_item' => __( 'View Item', 'text_domain' ),
- 'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
- 'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
- 'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
- 'popular_items' => __( 'Popular Items', 'text_domain' ),
- 'search_items' => __( 'Search Items', 'text_domain' ),
- 'not_found' => __( 'Not Found', 'text_domain' ),
- 'no_terms' => __( 'No items', 'text_domain' ),
- 'items_list' => __( 'Items list', 'text_domain' ),
- 'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
- );
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => false,
- 'public' => true,
- 'show_ui' => true,
- 'show_admin_column' => true,
- 'show_in_nav_menus' => true,
- 'show_tagcloud' => true,
- );
- register_taxonomy( 'taxonomyxxx', array( 'post' ), $args );
- }
- add_action( 'init', 'custom_taxonomy', 0 );
- add_action( 'admin_menu', 'prop_admin_menu' );
- function prop_admin_menu() {
- add_menu_page( 'Import Properties by XML', 'Import Properties by XML', 'manage_options', 'imp-prop-byxml', 'impor_prop_byxml_callback');
- }
- function impor_prop_byxml_callback()
- {
- ?>
- <div class="wrap"><h2>Import Properties by XML URL:</h2><br/>
- <form action="" method="post">
- <input type="hidden" name="action" value="xmlimpo" />
- <input type="Submit" value="Click to Import New Properties!" />
- </form>
- <p>Please dont close the browser untill you get message - "properties are successfully imported", please refresh the url if you get any internal server error!</p>
- <?php
- if(isset($_GET['loop']) && $_GET['loop']!='')
- {
- $crloop = $_GET['loop'];
- $nextloop = $_GET['loop'] + 1;
- $url = 'https://mmzr.mycrm.com/feed/privatesite/71a35b4940147de1dda45876020b103b';
- $geturl = file_get_contents($url);
- $xmlcont = simplexml_load_string($geturl, 'SimpleXMLElement', LIBXML_NOCDATA);
- $tcount = 1;
- $totproperty = count($xmlcont->property);
- if($nextloop > $totproperty)
- {
- echo '<br/>Properties are successfull imported!';
- }
- else
- {
- foreach($xmlcont->property as $prop)
- {
- if($tcount == $nextloop)
- {
- $p_title = $prop->title_en;
- $p_desc = $prop->description_en;
- global $wpdb;
- $newpstable = $wpdb->prefix.'posts';
- $newpropresult = $wpdb->get_results( "SELECT ID FROM $newpstable WHERE post_title = '".$p_title."' and post_type like 'property' and post_status like 'publish' ", OBJECT );
- if(empty($newpropresult))
- {
- $post_id = wp_insert_post(array (
- 'post_type' => 'property',
- 'post_title' => $p_title,
- 'post_content' => $p_desc,
- 'post_status' => 'publish',
- 'comment_status' => 'closed', // if you prefer
- 'ping_status' => 'closed', // if you prefer
- ));
- $photoarr = (array) $prop->photo->url;
- unset($photoarr['@attributes']);
- }
- if ($post_id) {
- $parent_term = term_exists($prop->city, 'property_city' ); // array is returned if taxonomy is given
- if(!empty($parent_term))
- {
- $set_city_termid = $parent_term['term_id'];
- }
- else
- {
- $city_termid = wp_insert_term(
- $prop->city, // the term
- 'property_city', // the taxonomy
- array(
- 'description'=> ''
- )
- );
- $set_city_termid = $city_termid->term_id;
- }
- $posted = wp_set_post_terms( $post_id, $set_city_termid, 'property_city',true );
- update_post_meta($post_id, 'fave_featured', '1');
- update_post_meta($post_id, 'fave_property_id', ''.$prop->reference_number.'');
- update_post_meta($post_id, 'fave_property_price', ''.$prop->price.'');
- update_post_meta($post_id, 'fave_property_address', ''.$prop->sub_community.', '.$prop->community.','.$prop->city.'');
- update_post_meta($post_id, 'fave_property_size', ''.$prop->size.'');
- update_post_meta($post_id, 'fave_property_size_prefix', 'Sq Ft');
- update_post_meta($post_id, 'fave_property_bedrooms', ''.$prop->bedroom.'');
- update_post_meta($post_id, 'fave_property_bathrooms', ''.$prop->bathroom.'');
- update_post_meta($post_id, 'fave_single_content_area', 'v4');
- update_post_meta($post_id, 'fave_single_top_area', 'v4');
- if($prop->amenities != '')
- {
- $amennewarr = array();
- $amen = $prop->amenities;
- $amenexp = explode(',',$amen);
- foreach($amenexp as $amenval)
- {
- $amenval = strtolower($amenval);
- $term = get_term_by('slug', $amenval, 'propamenities');
- $amennewarr[] = $term->name;
- }
- $impamen = implode(',',$amennewarr);
- update_post_meta($post_id, 'fave_additional_features_enable', 'enable');
- $returnValue = array(array('fave_additional_feature_title' => 'Amenities','fave_additional_feature_value' => ''.$impamen.'' ));
- update_post_meta($post_id, 'additional_features',$returnValue);
- }
- $loc = $prop->geopoints;
- $locexp = explode(',',$loc);
- update_post_meta($post_id, 'fave_property_location', ''.$locexp[1].','.$locexp[0].'');
- if($prop->geopoints!='')
- {
- update_post_meta($post_id, 'fave_property_map', '1');
- update_post_meta($post_id, 'fave_property_map_address',''.$prop->sub_community.', '.$prop->community.','.$prop->city.'');
- $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?api=AIzaSyBjJwEX78xOROeRLbwpF24fqWQtUM8M3mY&latlng='.trim($locexp[1]).','.trim($locexp[0]).'&sensor=false');
- $output = json_decode($geocodeFromLatLong);
- $status = $output->status;
- if($status=="OK")
- {
- $locountry = $output->results[1]->address_components[4]->short_name;
- if($locountry == 'AE')
- {
- $locountry = 'UAE';
- }
- update_post_meta($post_id, 'fave_property_country', $locountry);
- }
- }
- global $wpdb;
- $pstable = $wpdb->prefix.'posts';
- $agentresult = $wpdb->get_results( "SELECT * FROM $pstable WHERE post_title like '".$prop->agent->name."'", OBJECT );
- if(empty($agentresult))
- {
- $ag_post_id = wp_insert_post(array (
- 'post_type' => 'houzez_agent',
- 'post_title' => $prop->agent->name,
- 'post_status' => 'publish',
- 'comment_status' => 'closed', // if you prefer
- 'ping_status' => 'closed', // if you prefer
- ));
- if ($ag_post_id) {
- update_post_meta($ag_post_id, 'fave_agent_email', ''.$prop->agent->email.'');
- update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($post_id, 'fave_agents', ''.$ag_post_id.'');
- update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');
- }
- }
- else
- {
- update_post_meta($agentresult[0]->ID, 'fave_agent_email', ''.$prop->agent->email.'');
- update_post_meta($agentresult[0]->ID, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($post_id, 'fave_agents', ''.$agentresult[0]->ID.'');
- update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');
- }
- $rentcond = ''.$prop->rental_period.'';
- if($rentcond == 'Y')
- {
- $rtid = 33;
- }
- else
- {
- $rtid = 34;
- }
- wp_set_post_terms( $post_id, $rtid, 'property_status');
- $proptype = ''.$prop->property_type.'';
- if($proptype != '')
- {
- $proptype = strtolower($proptype);
- if($proptype == 'ap')
- {
- $proptype = 'apartment';
- }
- $setermid = get_term_by( 'slug', $proptype, 'property_type');
- wp_set_post_terms( $post_id, $setermid->term_id, 'property_type');
- }
- /*******************images******************************/
- //$featrdpath = 'http://shivayeweb.com/wp-content/uploads/2018/05/zpo97vYOzupYJ2JM_.jpg';
- $featrdpath = $photoarr[0];
- Generate_Featured_Image($featrdpath, $post_id);
- foreach($photoarr as $gall)
- {
- Generate_Img_propgllery($gall, $post_id);
- }
- /*******************images******************************/
- }
- echo '<script>window.location.href="?page=imp-prop-byxml&loop='.$nextloop.'"</script>';
- exit;
- }
- $tcount++;
- }
- }
- }
- if(isset($_POST['action']) && $_POST['action']=='xmlimpo')
- {
- $url = 'https://mmzr.mycrm.com/feed/privatesite/71a35b4940147de1dda45876020b103b';
- $geturl = file_get_contents($url);
- $xmlcont = simplexml_load_string($geturl, 'SimpleXMLElement', LIBXML_NOCDATA);
- $tcount = 1;
- foreach($xmlcont->property as $prop)
- {
- if($tcount > 1)
- {
- echo '<script>window.location.href="?page=imp-prop-byxml&loop=1"</script>';
- exit;
- }
- $p_title = $prop->title_en;
- $p_desc = $prop->description_en;
- global $wpdb;
- $newpstable = $wpdb->prefix.'posts';
- $newpropresult = $wpdb->get_results( "SELECT ID FROM $newpstable WHERE post_title = '".$p_title."' and post_type like 'property' and post_status like 'publish' ", OBJECT );
- if(empty($newpropresult))
- {
- $post_id = wp_insert_post(array (
- 'post_type' => 'property',
- 'post_title' => $p_title,
- 'post_content' => $p_desc,
- 'post_status' => 'publish',
- 'comment_status' => 'closed', // if you prefer
- 'ping_status' => 'closed', // if you prefer
- ));
- $photoarr = (array) $prop->photo->url;
- unset($photoarr['@attributes']);
- }
- if ($post_id) {
- $parent_term = term_exists($prop->city, 'property_city' );
- if(!empty($parent_term))
- {
- $set_city_termid = $parent_term['term_id'];
- }
- else
- {
- $city_termid = wp_insert_term(
- $prop->city, // the term
- 'property_city', // the taxonomy
- array(
- 'description'=> ''
- )
- );
- $set_city_termid = $city_termid->term_id;
- }
- $posted = wp_set_post_terms( $post_id, $set_city_termid, 'property_city',true );
- update_post_meta($post_id, 'fave_featured', '1');
- update_post_meta($post_id, 'fave_property_id', ''.$prop->reference_number.'');
- update_post_meta($post_id, 'fave_property_price', ''.$prop->price.'');
- update_post_meta($post_id, 'fave_property_address', ''.$prop->sub_community.', '.$prop->community .','.$prop->city.'');
- update_post_meta($post_id, 'fave_property_size', ''.$prop->size.'');
- update_post_meta($post_id, 'fave_property_size_prefix', 'Sq Ft');
- update_post_meta($post_id, 'fave_property_bedrooms', ''.$prop->bedroom.'');
- update_post_meta($post_id, 'fave_property_bathrooms', ''.$prop->bathroom.'');
- update_post_meta($post_id, 'fave_single_content_area', 'v2');
- update_post_meta($post_id, 'fave_single_top_area', 'v2');
- if($prop->amenities != '')
- {
- $amennewarr = array();
- $amen = $prop->amenities;
- $amenexp = explode(',',$amen);
- foreach($amenexp as $amenval)
- {
- $amenval = strtolower($amenval);
- $term = get_term_by('slug', $amenval, 'propamenities');
- $amennewarr[] = $term->name;
- }
- $impamen = implode(',',$amennewarr);
- update_post_meta($post_id, 'fave_additional_features_enable', 'enable');
- $returnValue = array(array('fave_additional_feature_title' => 'Amenities','fave_additional_feature_value' => ''.$impamen.'' ));
- update_post_meta($post_id, 'additional_features',$returnValue);
- }
- $loc = $prop->geopoints;
- $locexp = explode(',',$loc);
- update_post_meta($post_id, 'fave_property_location', ''.$locexp[1].','.$locexp[0].'');
- if($prop->geopoints!='')
- {
- update_post_meta($post_id, 'fave_property_map', '1');
- update_post_meta($post_id, 'fave_property_map_address',''.$prop->sub_community.', '.$prop->community .','.$prop->city.'');
- $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?api=AIzaSyBjJwEX78xOROeRLbwpF24fqWQtUM8M3mY&latlng='.trim($locexp[1]).','.trim($locexp[0]).'&sensor=false');
- $output = json_decode($geocodeFromLatLong);
- $status = $output->status;
- if($status=="OK")
- {
- $locountry = $output->results[1]->address_components[4]->short_name;
- if($locountry == 'AE')
- {
- $locountry = 'UAE';
- }
- update_post_meta($post_id, 'fave_property_country', $locountry);
- }
- }
- global $wpdb;
- $pstable = $wpdb->prefix.'posts';
- $agentresult = $wpdb->get_results( "SELECT * FROM $pstable WHERE post_title like '".$prop->agent->name."'", OBJECT );
- if(empty($agentresult))
- {
- $ag_post_id = wp_insert_post(array (
- 'post_type' => 'houzez_agent',
- 'post_title' => $prop->agent->name,
- 'post_status' => 'publish',
- 'comment_status' => 'closed', // if you prefer
- 'ping_status' => 'closed', // if you prefer
- ));
- if ($ag_post_id) {
- update_post_meta($ag_post_id, 'fave_agent_email', ''.$prop->agent->email.'');
- update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($ag_post_id, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($post_id, 'fave_agents', ''.$ag_post_id.'');
- update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');
- }
- }
- else
- {
- update_post_meta($agentresult[0]->ID, 'fave_agent_email', ''.$prop->agent->email.'');
- update_post_meta($agentresult[0]->ID, 'fave_agent_office_num', ''.$prop->agent->phone.'');
- update_post_meta($post_id, 'fave_agents', ''.$agentresult[0]->ID.'');
- update_post_meta($post_id, 'fave_agent_display_option', 'agent_info');
- }
- $rentcond = ''.$prop->rental_period.'';
- if($rentcond == 'Y')
- {
- $rtid = 33;
- }
- else
- {
- $rtid = 34;
- }
- wp_set_post_terms( $post_id, $rtid, 'property_status');
- $proptype = ''.$prop->property_type.'';
- if($proptype != '')
- {
- $proptype = strtolower($proptype);
- if($proptype == 'ap')
- {
- $proptype = 'apartment';
- }
- $setermid = get_term_by( 'slug', $proptype, 'property_type');
- wp_set_post_terms( $post_id, $setermid->term_id, 'property_type');
- }
- /*******************images******************************/
- //$featrdpath = 'http://shivayeweb.com/wp-content/uploads/2018/05/zpo97vYOzupYJ2JM_.jpg';
- $featrdpath = $photoarr[0];
- Generate_Featured_Image($featrdpath, $post_id);
- foreach($photoarr as $gall)
- {
- Generate_Img_propgllery($gall, $post_id);
- }
- /*******************images******************************/
- }
- $tcount++;
- }
- echo '<br/>Properties are successfull imported!';
- }
- ?>
- </div>
- <?php
- }
- function Generate_Featured_Image( $image_url, $post_id ){
- // Add Featured Image to Post
- $image_url = $image_url; // Define the image URL here
- $image_name = basename($image_url);
- $upload_dir = wp_upload_dir(); // Set upload folder
- $image_data = file_get_contents($image_url); // Get image data
- $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
- $filename = basename( $unique_file_name ); // Create image file name
- // Check folder permission and define file location
- if( wp_mkdir_p( $upload_dir['path'] ) ) {
- $file = $upload_dir['path'] . '/' . $filename;
- } else {
- $file = $upload_dir['basedir'] . '/' . $filename;
- }
- // Create the image file on the server
- file_put_contents( $file, $image_data );
- // Check image file type
- $wp_filetype = wp_check_filetype( $filename, null );
- // Set attachment data
- $attachment = array(
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => sanitize_file_name( $filename ),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- // Create the attachment
- $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
- // Include image.php
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- // Define attachment metadata
- $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
- // Assign metadata to attachment
- wp_update_attachment_metadata( $attach_id, $attach_data );
- // And finally assign featured image to post
- set_post_thumbnail( $post_id, $attach_id );
- }
- function Generate_Img_propgllery( $image_url, $post_id ){
- // Add Featured Image to Post
- $image_url = $image_url; // Define the image URL here
- $image_name = basename($image_url);
- $upload_dir = wp_upload_dir(); // Set upload folder
- $image_data = file_get_contents($image_url); // Get image data
- $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
- $filename = basename( $unique_file_name ); // Create image file name
- // Check folder permission and define file location
- if( wp_mkdir_p( $upload_dir['path'] ) ) {
- $file = $upload_dir['path'] . '/' . $filename;
- } else {
- $file = $upload_dir['basedir'] . '/' . $filename;
- }
- // Create the image file on the server
- file_put_contents( $file, $image_data );
- // Check image file type
- $wp_filetype = wp_check_filetype( $filename, null );
- // Set attachment data
- $attachment = array(
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => sanitize_file_name( $filename ),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- // Create the attachment
- $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
- // Include image.php
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- // Define attachment metadata
- $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
- // Assign metadata to attachment
- wp_update_attachment_metadata( $attach_id, $attach_data );
- // And finally assign featured image to post
- add_post_meta($post_id, 'fave_property_images', $attach_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement