Skip to content

Instantly share code, notes, and snippets.

@M4hd1BD
Created May 1, 2022 09:17
Show Gist options
  • Save M4hd1BD/1d1c79a1e7779626c49a39c16bef0e26 to your computer and use it in GitHub Desktop.
Save M4hd1BD/1d1c79a1e7779626c49a39c16bef0e26 to your computer and use it in GitHub Desktop.
Import Google Images with Directorist
// Import Google Content
function directorist_import_googlecontent($url = "", $post_id = 0)
{
if (!empty($url)) {
$data = directorist_file_get_contents_curl($url);
$wp_upload_dir = wp_upload_dir();
$attachment_id = 0;
$fp = $wp_upload_dir['basedir'] . '/logo-3.jpg';
$info = file_put_contents($fp, $data);
if ($info > 0) {
$url = $wp_upload_dir['baseurl'] . '/logo-3.jpg';
$attachment_id = directorist_save_image_as_attachment($url, $post_id);
}
unlink($fp);
return $attachment_id;
}
return 0;
}
function directorist_file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function directorist_save_image_as_attachment($url = '', $post_id = 0)
{
// Need to require these files
if (!function_exists('media_handle_upload')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$tmp = download_url($url);
if (is_wp_error($tmp)) {
// download failed, handle error
}
$post_id = 0;
$desc = "The WordPress Logo";
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
return $id;
}
add_action('directorist_listing_imported', function ($post_id, $post) {
$preview_image = isset($_POST['listing_img']) ? sanitize_text_field($_POST['listing_img']) : '';
$preview_url = !empty($post[$preview_image]) ? explode(',', trim($post[$preview_image])) : '';
if ($preview_url) {
$attachment_ids = [];
foreach ($preview_url as $_url_index => $_url) {
$attachment_id = directorist_import_googlecontent($_url);
if ($_url_index < 1) {
update_post_meta($post_id, '_listing_prv_img', $attachment_id);
} else {
$attachment_ids[] = $attachment_id;
}
}
update_post_meta($post_id, '_listing_img', $attachment_ids);
}
// Update post status to publish
wp_update_post(array('ID' => $post_id, 'post_status' => 'publish'));
}, 10, 2);
// Import Limit per cycle
add_filter('atbdp_listing_import_limit_per_cycle', function($cycle){
return 5;
});
// Admin CSS
add_action('admin_head', function(){
?>
<style>
.atbdp-importer-mapping-table-name code {
line-break: anywhere;
}
</style>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment