Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- /**
- * На голой бд сначала запустить import - импортировать гуиды терминов.
- * Затем запустить import_fields - импортировать гуиды названий полей.
- * после import_logical_fields
- * На крон вешать ./integration --cmd=load
- */
- //error_reporting(E_ALL | ~E_WARNING);
- //setup global $_SERVER variables to keep WP from trying to redirect
- $_SERVER = array(
- "HTTP_HOST" => "http://ordo.local:81",
- "SERVER_NAME" => "http://ordo.local:81",
- "REQUEST_URI" => "/",
- "REQUEST_METHOD" => "GET"
- );
- $username = 'CrmIntegration';
- $password = 'crmordogroup';
- $serviceUrl = 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/';
- $pageSize = 40;
- define('ANS_YES', 'да');
- define('ANS_NO', 'нет');
- define('DEV', false);
- ini_set('memory_limit', '512M');
- $DOCUMENT_ROOT = dirname(__FILE__);
- //require the WP bootstrap
- require_once(dirname(__FILE__).'/wp-load.php');
- $timestamp = get_option('update_timestamp');
- $lastUpdate = date('Y-m-d\TH:i:s', $timestamp);
- function load_handbooks()
- {
- global $pageSize,$lastUpdate;
- $listings = array(
- 'property-status' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwListingTypeCollection',
- 'agents' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwOwnerCollection',
- 'currencies' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwCurrencyCollection',
- 'property-type' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwPropertyCategoryCollection',
- 'property-city' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwCityCollection',
- 'property-country' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwCountryCollection',
- 'property-new' => 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwRealtySaleTypeCollection',
- );
- $currencies_name = array(
- 'RUR' => 'Рубль',
- 'EUR' => 'Евро',
- 'USD' => 'Доллар',
- 'CHF' => 'Швейцарский франк',
- 'GBP' => 'Фунт стерлингов',
- );
- foreach ($listings as $key => $url) {
- $page = 0;
- do {
- $next = false;
- $method = $url . '?$top=' . $pageSize . '&$skip=' . $page * $pageSize;
- $method .= '&$filter=ModifiedOn%20gt%20DateTime\'' . $lastUpdate . '\'';
- $xml = get_result($method);
- foreach ($xml->children() as $child) {
- if ($child->getName() != 'entry') {
- continue;
- } else {
- $next = true;
- }
- $id = null;
- $properties = $child->content->children('m', true)->children('d', true);
- $guid = (string)$properties->Id;
- $name = (string)$properties->Name;
- if ($option = get_option($guid)) {
- $data = unserialize($option);
- $term = get_term_by('id', $data['id'], $data['taxonomy']);
- if ($term->name != $name) {
- wp_update_term($data['id'], $data['taxonomy'], array('name' => $name));
- }
- } elseif ($key == 'agents') {
- // Создаём объект записи
- $my_post = array(
- 'post_title' => $name,
- 'post_content' => '',
- 'post_type' => 'agent',
- 'post_status' => 'publish',
- 'post_author' => 1,
- );
- // Вставляем запись в базу данных
- $id = wp_insert_post($my_post);
- } elseif ($key != 'currencies') {
- $term = wp_insert_term($name, $key);
- if (!$term instanceof WP_Error) {
- $id = $term['term_id'];
- } else {
- $errors = $term->errors;
- if (array_key_exists('term_exists', $errors)) {
- $term = get_term_by('name', $name, $key);
- $id = $term->term_id;
- } else {
- print_r($name . ' error' . "\n");
- print_r($term->errors);
- }
- }
- } else {
- if (($cur = array_search($name, $currencies_name)) !== false) {
- $id = $cur;
- }
- }
- if (!empty($id)) {
- $data = serialize(array('taxonomy' => $key, 'id' => $id));
- update_option('guid_' . $key . $id, $guid);
- update_option($guid, $data);
- }
- }
- unset($xml);
- $page++;
- } while($next);
- }
- }
- function load_gallery()
- {
- $url = 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwListingGalleryImageCollection';
- global $username, $password, $serviceUrl, $pageSize, $lastUpdate;
- $context = stream_context_create(array(
- 'http' => array(
- 'header' => "Authorization: Basic " . base64_encode("$username:$password")
- )
- ));
- $uploads = wp_upload_dir();
- if (!is_dir($uploads['path'])) {
- wp_mkdir_p($uploads['path']);
- }
- $postsGallery = array();
- $page = 0;
- do {
- $next = false;
- $method = $url . '?$top=' . $pageSize . '&$skip=' . $page * $pageSize;
- $method .= '&$filter=ModifiedOn%20gt%20DateTime\'' . $lastUpdate . '\'';
- print_r($method . "\n");
- $xml = get_result($method);
- foreach ($xml->children() as $child) {
- try {
- if ($child->getName() != 'entry') {
- continue;
- } else {
- $next = true;
- }
- $properties = $child->content->children('m', true)->children('d', true);
- $href = null;
- foreach ($child->children() as $field) {
- if ($field->getName() == 'link' && $field->attributes()->title == 'Data') {
- $href = $serviceUrl . $field->attributes()->href;
- }
- }
- if (!$href) {
- continue;
- }
- $fileContent = file_get_contents($href, false, $context);
- $imageName = (string) $properties->Name;
- $imageName = !empty($imageName) ? time() . '_' . $imageName : md5((string) $properties->Id . time()) . '.jpg';
- $imagePath = $uploads['path'] . '/' . $imageName;
- file_put_contents($imagePath, $fileContent);
- unset($fileContent);
- $objectId = (string) $properties->ListingId;
- $position = (int) $properties->Position;
- $data = unserialize(get_option($objectId));
- if (!is_array($data) || empty($data['taxonomy']) || $data['taxonomy'] != 'property') {
- continue;
- }
- $galleryData = unserialize(get_option((string) $properties->Id));
- $gallery = get_post_meta($data['id'], 'REAL_HOMES_property_images');
- //удаляем изображения не из crm
- foreach ($gallery as $key => $imageId) {
- if (!($img = get_option('guid_gallery' . $imageId))) {
- wp_delete_attachment($imageId, true);
- unset($gallery[$key]);
- }
- }
- if ( ($key = array_search($galleryData['id'], $gallery)) !== false) {
- unset($gallery[$key]);
- wp_delete_attachment($galleryData['id'], true);
- delete_option('guid_gallery' . $galleryData['id']);
- delete_option((string) $properties->Id);
- }
- delete_post_meta($data['id'], 'REAL_HOMES_property_images');
- foreach ($gallery as $imageId) {
- add_post_meta($data['id'], 'REAL_HOMES_property_images', $imageId);
- }
- $attachment = array(
- 'guid' => $imagePath,
- 'post_mime_type' => 'image/jpeg',
- 'post_title' => preg_replace('/\.[^.]+$/', '', $imageName),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $imageId = wp_insert_attachment($attachment, $imagePath, $data['id']);
- update_post_meta($imageId, 'sort', $position);
- if (!isset($postsGallery[$data['id']]) && $position == 0) {
- $postsGallery[$data['id']] = array(
- 'id' => $imageId,
- 'path' => $imagePath,
- );
- }
- add_post_meta($data['id'], 'REAL_HOMES_property_images', $imageId);
- update_option('guid_gallery' . $imageId, (string)$properties->Id);
- $data = serialize(array('type' => 'gallery', 'id' => $imageId, 'post_id' => $data['id']));
- update_option((string)$properties->Id, $data);
- } catch (Exception $e) {
- print_r($e->getMessage());
- }
- }
- unset($xml);
- $page++;
- } while($next);
- echo "gallery images: \n";
- print_r($postsGallery);
- foreach ($postsGallery as $post_id => $image) {
- if (!empty($image['id']) && !empty($image['path'])) {
- set_object_thumbnail($post_id, $image['id'], $image['path']);
- }
- }
- }
- function load_files()
- {
- $url = 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwListingFileCollection';
- global $username, $password, $serviceUrl, $pageSize, $lastUpdate;
- $context = stream_context_create(array(
- 'http' => array(
- 'header' => "Authorization: Basic " . base64_encode("$username:$password")
- )
- ));
- $uploads = wp_upload_dir();
- if (!is_dir($uploads['path'])) {
- wp_mkdir_p($uploads['path']);
- }
- $page = 0;
- do {
- $next = false;
- $method = $url . '?$top=' . $pageSize . '&$skip=' . $page * $pageSize;
- $method .= '&$filter=ModifiedOn%20gt%20DateTime\'' . $lastUpdate . '\'';
- $xml = get_result($method);
- foreach ($xml->children() as $child) {
- if ($child->getName() != 'entry') {
- continue;
- } else {
- $next = true;
- }
- $properties = $child->content->children('m', true)->children('d', true);
- $href = null;
- foreach ($child->children() as $field) {
- if ($field->getName() == 'link' && $field->attributes()->title == 'Data') {
- $href = $serviceUrl . $field->attributes()->href;
- }
- }
- if (!$href) {
- continue;
- }
- $objectId = (string) $properties->ListingId;
- $data = unserialize(get_option($objectId));
- if (!is_array($data) || empty($data['taxonomy']) || $data['taxonomy'] != 'property') {
- continue;
- }
- $fileContent = file_get_contents($href, false, $context);
- $fileName = time() . $properties->Name;
- $filePath = $uploads['path'] . '/' . $fileName;
- file_put_contents($filePath, $fileContent);
- unset($fileContent);
- $attachmentData = unserialize(get_option((string) $properties->Id));
- $attachments = get_post_meta($data['id'], 'REAL_HOMES_attachments');
- foreach ($attachments as $key => $fileId) {
- if (!($img = get_option('guid_attachment' . $fileId))) {
- wp_delete_attachment($fileId, true);
- unset($attachments[$key]);
- }
- }
- if ( ($key = array_search($attachmentData['id'], $attachments)) !== false) {
- unset($attachments[$key]);
- wp_delete_attachment($attachmentData['id'], true);
- delete_option('guid_attachment' . $attachmentData['id']);
- delete_option((string) $properties->Id);
- }
- delete_post_meta($data['id'], 'REAL_HOMES_attachments');
- foreach ($attachments as $fileId) {
- add_post_meta($data['id'], 'REAL_HOMES_attachments', $fileId);
- }
- $type = @mime_content_type($filePath);
- $attachment = array(
- 'guid' => $filePath,
- 'post_mime_type' => $type,
- 'post_title' => preg_replace('/\.[^.]+$/', '', $fileName),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $fileId = wp_insert_attachment($attachment, $filePath, $data['id']);
- add_post_meta($data['id'], 'REAL_HOMES_attachments', $fileId);
- update_option('guid_attachment' . $fileId, (string)$properties->Id);
- $data = serialize(array('type' => 'attachment', 'id' => $fileId, 'post_id' => $data['id']));
- update_option((string)$properties->Id, $data);
- }
- unset($xml);
- $page++;
- } while($next);
- }
- function load_objects_amenity()
- {
- global $pageSize, $lastUpdate, $serviceUrl;
- $url = 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwAmenityInObjectCollection';
- $page = 0;
- $arrayToSave = array();
- $notFoundFields = array();
- $notFoundObjects = array();
- do {
- $next = false;
- $method = $url . '?$top=' . $pageSize . '&$skip=' . $page * $pageSize;
- $method .= '&$filter=ModifiedOn%20gt%20DateTime\'' . $lastUpdate . '\'';
- print_r($method . "\n");
- $xml = get_result($method);
- foreach ($xml->children() as $child) {
- if ($child->getName() != 'entry') {
- continue;
- } else {
- $next = true;
- }
- $id = null;
- $properties = $child->content->children('m', true)->children('d', true);
- $objectGuid = (string) $properties->ListingId;
- $fieldGuid = (string) $properties->AmenityId;
- $value = (string) $properties->Value;
- $lookupValue = (string) $properties->LookupValueId;
- $objectSerialized = get_option($objectGuid);
- $objectData = unserialize($objectSerialized);
- if (!is_array($objectData) || !isset($objectData['id'])) {
- print_r($objectGuid . " object not found \n");
- print_r($objectData);
- if (!in_array($objectGuid, $notFoundObjects)) {
- $notFoundObjects[] = $objectGuid;
- }
- continue;
- }
- $fieldSerialized = get_option($fieldGuid);
- $fieldData = unserialize($fieldSerialized);
- if (!is_array($fieldData)) {
- print_r($fieldData." | " .$fieldSerialized. " field not found \n");
- print_r($fieldGuid . " field not found \n");
- if (!in_array($fieldGuid, $notFoundFields)) {
- $notFoundFields[] = $fieldGuid;
- }
- continue;
- }
- //Текстовые свойства сразу сохраняем
- if (isset($fieldData['type']) && $fieldData['type'] == 'field') {
- if($fieldGuid == "3db05362-e6e3-4b3f-9c6a-42c3d9b9dbd0"
- || $fieldGuid == "ADCADD5A-A83E-407D-AAB7-90970CD3E795"){
- $valueIn = get_post_meta($objectData['id'], $fieldData['name'], true);
- print_r("sq: ".$valueIn);
- if($value != "" && $value != $valueIn){
- update_post_meta($objectData['id'], $fieldData['name'], $value);
- }
- }else{
- update_post_meta($objectData['id'], $fieldData['name'], $value);
- }
- } elseif (isset($fieldData['type']) && $fieldData['type'] == 'taxonomy' && !isGuidEmpty($lookupValue)) {
- $termSerialized = get_option($lookupValue);
- $termData = unserialize($termSerialized);
- if (is_array($termData)){
- $arrayToSave[$objectData['id']][$fieldData['name']][] = $termData['id'];
- } else {
- $href = null;
- foreach ($child->children() as $field) {
- if ($field->getName() == 'link' && $field->attributes()->title == 'LookupValue') {
- $href = $serviceUrl . $field->attributes()->href;
- }
- }
- if (!$href) {
- continue;
- }
- $term_id = add_new_taxonomy_term($fieldData['name'], $href);
- if (!is_null($term_id)) {
- $arrayToSave[$objectData['id']][$fieldData['name']][] = $term_id;
- }
- }
- } elseif (isset($fieldData['taxonomy']) && !empty($fieldData['id'])) {
- if ($value == ANS_YES) {
- $arrayToSave[$objectData['id']][$fieldData['taxonomy']][] = $fieldData['id'];
- } elseif (!isset($arrayToSave[$objectData['id']][$fieldData['taxonomy']])) {
- $arrayToSave[$objectData['id']][$fieldData['taxonomy']] = array();
- }
- }
- }
- unset($xml);
- $page++;
- } while($next);
- print_r("array to save: \n");
- print_r($arrayToSave);
- print_r("not found objects: \n");
- print_r($notFoundObjects);
- print_r("not found fields: \n");
- print_r($notFoundFields);
- foreach ($arrayToSave as $objectId => $taxonomies) {
- foreach ($taxonomies as $taxonomy => $terms) {
- wp_set_post_terms($objectId, $terms, $taxonomy);
- }
- }
- }
- /**
- * @deprecated
- * @param $objectId
- * @param $xml
- * @return bool
- */
- function load_object_thumbnail($objectId, $xml)
- {
- global $username, $password, $serviceUrl;
- $context = stream_context_create(array(
- 'http' => array(
- 'header' => "Authorization: Basic " . base64_encode("$username:$password")
- )
- ));
- $href = null;
- foreach ($xml->children() as $field) {
- if ($field->getName() == 'link' && $field->attributes()->title == 'Photo') {
- $href = $serviceUrl . $field->attributes()->href;
- }
- }
- if (!$href) {
- return false;
- }
- $photoXml = get_result($href);
- $photoHref = null;
- foreach ($photoXml->children() as $field) {
- if ($field->getName() == 'link' && $field->attributes()->title == 'Data') {
- $photoHref = $serviceUrl . $field->attributes()->href;
- }
- }
- if (!$photoHref) {
- return false;
- }
- $uploads = wp_upload_dir();
- $properties = $photoXml->content->children('m', true)->children('d', true);
- $fileContent = file_get_contents($photoHref, false, $context);
- if (empty($fileContent)) {
- print_r($photoHref . " preview is empty\n");
- return false;
- }
- $imageName = md5((string) $properties->Id . time()) . '.jpg';
- $imagePath = $uploads['path'] . '/' . $imageName;
- file_put_contents($imagePath, $fileContent);
- unset($fileContent);
- $attachment = array(
- 'guid' => $imagePath,
- 'post_mime_type' => 'image/jpeg',
- 'post_title' => preg_replace('/\.[^.]+$/', '', $imageName),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $attach_id = wp_insert_attachment( $attachment, $imagePath, $objectId );
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $attach_data = wp_generate_attachment_metadata( $attach_id, $imagePath );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- set_post_thumbnail( $objectId, $attach_id );
- return true;
- }
- /**
- * Получает изменения с момента $lastUpdate
- */
- function load_posts()
- {
- mail("zhirnovvlad@gmail.com,irina.sorokina@ordogroup.ru", "ordogroup.ru: Началось обнолвение объектов.", "");
- clearTmpImportIds();
- $time_start = time();
- global $pageSize, $lastUpdate;
- load_handbooks();
- print_r($lastUpdate . "\n");
- echo "handbooks ready\n";
- $url = 'https://ordoag.bpmonline.com/0/ServiceModel/EntityDataService.svc/VwListingExportCollection';
- $siteCurrencies = currencies_array();
- $page = 0;
- do {
- $next = false;
- $method = $url . '?$top=' . $pageSize . '&$skip=' . $page * $pageSize;
- $method .= '&$filter=ModifiedOn%20gt%20DateTime\'' . $lastUpdate . '\'';
- $xml = get_result($method);
- try {
- if(is_object($xml) && is_object($xml->children())){
- foreach ($xml->children() as $child) {
- try {
- if ($child->getName() != 'entry') {
- continue;
- } else {
- $next = true;
- }
- $postId = null;
- $properties = $child->content->children('m', true)->children('d', true);
- if (!isGuidEmpty((string) $properties->Id) && $data = get_option((string) $properties->Id)) {
- $data = unserialize($data);
- if ($data['taxonomy'] == 'property') {
- $postId = $data['id'];
- }
- }
- $id = (string) $properties->UsrNumberOfListing;
- insertTmpImportId($id);
- $load = (string) $properties->IsExportOnSite;
- $status = $load == 'true' ? 'publish' : 'draft';
- $post = null;
- if (!empty($postId)) {
- $post = get_post($postId);
- }
- $new = false;
- if (!$post instanceof WP_Post) {
- $args = array(
- 'post_type' => 'property',
- 'posts_per_page' => -1,
- 'meta_query' => array(
- array(
- 'key' => 'REAL_HOMES_property_id',
- 'value' => $id,
- 'compare' => '=',
- )
- )
- );
- $query = new WP_Query($args);
- if ($query->have_posts()) {
- $post = $query->post;
- } else {
- if ($status == "draft") {
- continue;
- }
- $my_post = array(
- 'post_title' => (string)$properties->Name,
- 'post_content' => (string)$properties->Description,
- 'post_type' => 'property',
- 'post_status' => $status,
- 'post_author' => 1,
- );
- // Вставляем запись в базу данных
- $postId = wp_insert_post($my_post);
- if (!($postId instanceof WP_Error)) {
- $post = get_post($postId);
- echo "NEW: ".$id;
- } else {
- echo "Error adding post: ".$id." - ".$postId->get_error_message();
- continue;
- }
- $new = true;
- }
- }
- update_option('guid_property' . $post->ID, (string) $properties->Id);
- $data = serialize(array('taxonomy' => 'property', 'id' => $post->ID));
- update_option((string) $properties->Id, $data);
- $post->post_title = (string) $properties->Name;
- $post->post_content = (string) $properties->Description;
- $post->post_status = $status;
- update_post_option($post->ID, (string) $properties->PropertyCategoryId);
- update_post_option($post->ID, (string) $properties->ListingTypeId);
- update_post_option($post->ID, (string) $properties->CityId);
- update_post_option($post->ID, (string) $properties->CountryId);
- update_post_option($post->ID, (string) $properties->UsrRealtySaleTypeId);
- update_post_option($post->ID, (string) $properties->OwnerId);
- update_post_meta($post->ID, 'REAL_HOMES_property_address', (string) $properties->Address);
- update_post_meta($post->ID, 'REAL_HOMES_property_card_legend', (string) $properties->UsrDescription2);
- update_post_meta($post->ID, 'REAL_HOMES_price_request', ((string) $properties->IsRequestPrice == 'true'));
- update_post_meta($post->ID, 'REAL_HOMES_show_on_main', ((string) $properties->IsShowOnMain == 'true'));
- update_post_meta($post->ID, 'REAL_HOMES_property_location', (string) $properties->Latitude . ',' . (string) $properties->Longitude);
- update_post_meta($post->ID, 'REAL_HOMES_property_id', $id);
- $curr = (string) $properties->CurrencyId;
- if ($new) {
- update_post_meta($post->ID, 'REAL_HOMES_gallery_slider_type', 'thumb-on-bottom');
- }
- if (!isGuidEmpty($curr) && $currencyData = get_option($curr)) {
- $currencyData = unserialize($currencyData);
- if ($currencyData['taxonomy'] == 'currencies') {
- $main_currency = $currencyData['id'];
- $id = array_search($main_currency, $siteCurrencies);
- if ($id !== false && $main_currency) {
- update_post_meta($post->ID, 'REAL_HOMES_property_main_currency', $id);
- update_post_meta($post->ID, 'REAL_HOMES_property_' . strtolower($main_currency) . '_price', (string) $properties->Price);
- }
- }
- }
- wp_update_post($post);
- if ($new) {
- print_r($post->ID . " created, guid: " . (string) $properties->Id . " \n");
- } else {
- print_r($post->ID . " updated, guid: " . (string) $properties->Id . " \n");
- }
- } catch (Exception $e) {
- print_r($e->getMessage());
- }
- }
- }
- } catch (Exception $exc) {
- echo $exc->getTraceAsString ();
- }
- unset($xml);
- $page++;
- } while($next);
- //die();
- load_objects_amenity();
- if(!DEV){
- load_gallery();
- mail("zhirnovvlad@gmail.com", "ordogroup.ru: Выгрузка завершена галерея.", "");
- load_files();
- }
- mail("zhirnovvlad@gmail.com", "ordogroup.ru: Выгрузка завершена.", "");
- update_option('update_timestamp', $time_start);
- }
- function get_result($url) {
- global $username, $password;
- if ($ch = curl_init($url)) {
- curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- $result = preg_replace('/&#x[0-1]?[0-9A-E]/', ' ', $result);
- return simplexml_load_string(stripInvalidXml($result));
- }
- return false;
- }
- function stripInvalidXml($value)
- {
- $ret = "";
- $current;
- if (empty($value))
- {
- return $ret;
- }
- $length = strlen($value);
- for ($i=0; $i < $length; $i++)
- {
- $current = ord($value{$i});
- if (($current == 0x9) ||
- ($current == 0xA) ||
- ($current == 0xD) ||
- (($current >= 0x20) && ($current <= 0xD7FF)) ||
- (($current >= 0xE000) && ($current <= 0xFFFD)) ||
- (($current >= 0x10000) && ($current <= 0x10FFFF)))
- {
- $ret .= chr($current);
- }
- else
- {
- $ret .= " ";
- }
- }
- return $ret;
- }
- $args = parseArgs($argv);
- $importer = new WP_Command_Line();
- $importer->main($args);
- function parseArgs($argv){
- array_shift($argv);
- $out = array();
- foreach ($argv as $arg){
- if (substr($arg,0,2) == '--'){
- $eqPos = strpos($arg,'=');
- if ($eqPos === false){
- $key = substr($arg,2);
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- } else {
- $key = substr($arg,2,$eqPos-2);
- $out[$key] = substr($arg,$eqPos+1);
- }
- } else if (substr($arg,0,1) == '-'){
- if (substr($arg,2,1) == '='){
- $key = substr($arg,1,1);
- $out[$key] = substr($arg,3);
- } else {
- $chars = str_split(substr($arg,1));
- foreach ($chars as $char){
- $key = $char;
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- }
- }
- } else {
- $out[] = $arg;
- }
- }
- return $out;
- }
Add Comment
Please, Sign In to add comment