Beispiel #1
0
 function __destruct()
 {
     if ($this->parsed) {
         if ($this->mem) {
             if (!$this->keep && isset($this->open_file)) {
                 unlink($this->filename);
             }
         } else {
             flcose($this->fcont);
         }
     }
     unset($this->open_file);
     unset($this->keep);
     unset($this->fcont);
     unset($this->mem);
     unset($this->file_ok);
     unset($this->parsed);
     unset($this->test_func);
 }
 function zoo_save_classified_posts_from_xml($classified_array)
 {
     if (is_array($classified_array)) {
         $meta_prefix = '_zoo_classified_';
         $administrator_id = zoo_get_administrator_id() ? zoo_get_administrator_id() : 1;
         foreach ($classified_array as $single_classified) {
             // at first we will see the categories
             // insert or update classified categories
             $category_ids = array();
             if ($single_classified['categories'] && sizeof($single_classified) > 0) {
                 $categories = $single_classified['categories'];
                 foreach ($categories as $category) {
                     $parent_term_id = null;
                     // parent category
                     if ($category_name = $category['parent']) {
                         // check if this category exists
                         $check_term = get_term_by('name', $category_name, 'classified-category');
                         if ($check_term && sizeof($check_term) > 0) {
                             // category found
                             $category_ids[] = $check_term->term_id;
                             $parent_term_id = $check_term->term_id;
                         } else {
                             // category not found so insert it
                             $new_category_args = array();
                             $new_category = wp_insert_term($category_name, 'classified-category');
                             // store the newly created category ids
                             if (!is_wp_error($new_category) && sizeof($new_category) > 0) {
                                 $category_ids[] = $new_category['term_id'];
                                 $parent_term_id = $new_category['term_id'];
                             }
                         }
                     }
                     // child category
                     if ($category_name = $category['child']) {
                         // check if this category exists
                         $check_term = get_term_by('name', $category_name, 'classified-category');
                         if ($check_term && sizeof($check_term) > 0) {
                             // category found
                             $category_ids[] = $check_term->term_id;
                         } elseif ($parent_term_id) {
                             // category not found so insert it
                             $new_category_args = array('parent' => $parent_term_id);
                             $new_category = wp_insert_term($category_name, 'classified-category', $new_category_args);
                             // store the newly created category ids
                             if (!is_wp_error($new_category) && sizeof($new_category) > 0) {
                                 $category_ids[] = $new_category['term_id'];
                             }
                         }
                     }
                 }
             }
             // we have to define post author
             $classified_data = array('post_type' => 'classified-ad', 'post_title' => $single_classified['title'], 'post_content' => $single_classified['description'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => $administrator_id);
             $classified_info = array('unique_id' => $single_classified['uniqueID'], 'status' => $single_classified['status'], 'price' => $single_classified['price'], 'address' => $single_classified['address']);
             if ($single_classified['price_view']) {
                 $classified_info['price_view'] = $single_classified['price_view'];
             }
             // immediately delete classified if its status is withdrawn
             if ($single_classified['status'] == 'withdrawn') {
                 $query = array('post_type' => 'classified-ad', 'numberposts' => 1, 'meta_query' => array(array('key' => $meta_prefix . 'unique_id', 'value' => $single_classified['uniqueID'], 'compare' => '=')));
                 $withdrawn_classified = get_posts($query);
                 if (is_array($withdrawn_classified) && sizeof($withdrawn_classified)) {
                     $withdrawn_classified_id = $withdrawn_classified[0]->ID;
                     wp_delete_post($withdrawn_classified_id, true);
                 }
             }
             // classified agents
             if ($single_classified['agents'] && sizeof($single_classified['agents']) > 0) {
                 $classified_info['classified_agents'] = $single_classified['agents'];
             }
             $existing_query = array('post_type' => 'classified-ad', 'numberposts' => 1, 'meta_query' => array(array('key' => $meta_prefix . 'unique_id', 'value' => $single_classified['uniqueID'], 'compare' => '=')));
             $existing_classified = get_posts($existing_query);
             if (is_array($existing_classified) && sizeof($existing_classified) > 0) {
                 // we will update this classified
                 $exist_classified = true;
             } else {
                 // we will insert this classified
                 $exist_classified = false;
             }
             // update classified
             if (is_array($existing_classified) && sizeof($existing_classified) > 0 && $exist_classified === true) {
                 // echo 'update';
                 $classified_data['ID'] = $existing_classified[0]->ID;
                 $new_classified_id = wp_update_post($classified_data);
             } else {
                 // echo 'insert';
                 $new_classified_id = wp_insert_post($classified_data, true);
             }
             // apply categories
             wp_set_object_terms($new_classified_id, $category_ids, 'classified-category');
             if (!is_wp_error($new_classified_id) && $new_classified_id != 0) {
                 // update post meta
                 foreach ($classified_info as $meta_key => $meta_value) {
                     add_post_meta($new_classified_id, $meta_prefix . $meta_key, $meta_value, true) or update_post_meta($new_classified_id, $meta_prefix . $meta_key, $meta_value);
                 }
             }
             // figures out where the uploads folder lives
             $wp_upload_dir = wp_upload_dir();
             if ($single_classified['images'] && is_array($single_classified['images'])) {
                 $new_classified_image_paths = array();
                 $image_gallery_sources = array();
                 foreach ($single_classified['images'] as $single_image_data) {
                     $single_image_data_explode = explode('|', $single_image_data);
                     $format = $single_image_data_explode[1];
                     $source = $single_image_data_explode[0];
                     $parsed_url = parse_url($source);
                     $pathinfo = pathinfo($parsed_url['path']);
                     // figure out where we putting this image
                     // $dest_file_name = wp_unique_filename($wp_upload_dir['path'], $pathinfo['basename']);
                     $dest_file_name = $pathinfo['basename'] . '.' . $format;
                     $dest_path = $wp_upload_dir['path'] . '/' . $dest_file_name;
                     $dest_url = $wp_upload_dir['url'] . '/' . $dest_file_name;
                     // download the image to our server
                     if (!file_exists($dest_path)) {
                         if (ini_get('allow_url_fopen')) {
                             copy($source, $dest_path);
                             // @copy($source, $dest_path);
                         } elseif (function_exists('curl_init')) {
                             $ch = curl_init($source);
                             $fb = fopen($dest_path, 'wb');
                             $options = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 60);
                             curl_setopt_array($ch, $options);
                             curl_exec($ch);
                             $http_status = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
                             curl_close($ch);
                             flcose($fb);
                             // delete the file if the download was unsuccessful
                             if ($http_status != 200) {
                                 unlink($dest_path);
                             }
                         }
                     }
                     $new_classified_image_paths[] = array('path' => $dest_path, 'url' => $dest_url, 'source' => $source);
                 }
                 foreach ($new_classified_image_paths as $index => $dest_path_info) {
                     // check for duplicate images
                     $existing_attachment_query = array('post_type' => 'attachment', 'numberposts' => 1, 'meta_query' => array(array('key' => '_import_source', 'value' => $dest_path_info['source'], 'compare' => '=')));
                     $existing_attachment = get_posts($existing_attachment_query);
                     if (is_array($existing_attachment) && sizeof($existing_attachment) > 0) {
                         // we will update this attachment
                         $exist_attachment = true;
                     } else {
                         // we will insert this attachment
                         $exist_attachment = false;
                     }
                     if (file_exists($dest_path_info['path'])) {
                         // if attachment exists we won't attach and update attachment meta
                         if ($exist_attachment === false) {
                             $dest_url = str_ireplace(ABSPATH, home_url('/'), $dest_path_info['path']);
                             $path_parts = pathinfo($dest_path_info['path']);
                             // add a post of type 'attachment' so this item shows up in the WP Media Library
                             // our imported classified will be the post's parent.
                             $wp_file_type = wp_check_filetype($dest_path_info['path']);
                             $attachment = array('guid' => $dest_url, 'post_mime_type' => $wp_file_type['type'], 'post_title' => $path_parts['filename'], 'post_content' => '', 'post_status' => 'inherit', 'post_author' => $administrator_id);
                             $attachment_id = wp_insert_attachment($attachment, $dest_path_info['path'], $new_classified_id);
                             // we must first include the image.php file
                             // for the function wp_generate_attachment_metadata() to work
                             require_once ABSPATH . 'wp-admin/includes/image.php';
                             $attach_data = wp_generate_attachment_metadata($attachment_id, $dest_path_info['path']);
                             wp_update_attachment_metadata($attachment_id, $attach_data);
                             // keep track of where attachment came from so we don't import duplicates later
                             add_post_meta($attachment_id, '_import_source', $dest_path_info['source'], true) or update_post_meta($attachment_id, '_import_source', $dest_path_info['source']);
                         } else {
                             $attachment_id = $existing_attachment[0]->ID;
                         }
                         // set the images for the gallery
                         $image_gallery_sources[$attachment_id] = $dest_path_info['url'];
                     }
                 }
                 // set gallery images for the imported classified
                 if (sizeof($image_gallery_sources) > 0) {
                     add_post_meta($new_classified_id, $meta_prefix . 'gallery_images', $image_gallery_sources, true) or update_post_meta($new_classified_id, $meta_prefix . 'gallery_images', $image_gallery_sources);
                 }
             }
         }
         return true;
     }
     return false;
 }
Beispiel #3
0
flcose($handle);
// reading csv
$handler = fopen($pathToFile, 'r+');
$lines = array();
while (($data = fgetcsv($handle)) !== false) {
    $lines[] = $data;
}
fclose($handler);
// updating csv
$handler = fopen($pathToFile, 'r+');
$lines = array();
$line6 = array('column1', 'column2');
$line7 = array('column1', 'column2');
while (($data = fgetcsv($handle)) !== false) {
    $lines[] = $data;
}
fclose($handler);
// consider that the array has 10 items
$newLines = array_chunk($lines, 5);
array_push($newLines[1], $line6, $line7);
$lines = $newLines[0] + $newLines[1];
// now writting the new values
$handle = fopen($pathToFile, 'w+');
// w+ will reset the file
foreach ($lines as $index => $line) {
    fputcsv($handle, $line);
}
flcose($handle);
// deleting
$pathToFile = '/tmp/newTextFile.csv';
unlink($pathToFile);
Beispiel #4
0
        <h4>Request Token</h4>
        <pre><?php 
var_dump($request_token);
?>
</pre>
        <h4>Auth link</h4>
        <a href="<?php 
echo $url;
?>
"><?php 
echo $url;
?>
</a>


        <?php 
if (isset($_REQUEST['oauth_token']) && isset($_SESSION['oauth_token'])) {
    $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_REQUEST['oauth_verifier']]);
    echo '<h4>Access Token</h4><pre>';
    var_dump($access_token);
    echo '</pre>';
    $sf = fopen('tokens/' . $access_token['screen_name'] . '.json', 'w');
    fwrite($sf, json_encode($access_token, JSON_PRETTY_PRINT));
    flcose($sf);
    session_destroy();
}
?>
</body>

</html>