Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sudarshann/bdf96f9d2d15e05629087429e59f24d3 to your computer and use it in GitHub Desktop.

Select an option

Save sudarshann/bdf96f9d2d15e05629087429e59f24d3 to your computer and use it in GitHub Desktop.

Revisions

  1. sudarshann revised this gist May 3, 2021. 1 changed file with 37 additions and 29 deletions.
    66 changes: 37 additions & 29 deletions wordpress-download-google-drive-image.php
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,6 @@
    class downloadGoogleDriveImage {
    public static function export_google_url($url) {
    if (strpos($url, "https://drive.google.com/file/d/") === false) {
    return $url;
    }
    $unique_id = str_replace(array("https://drive.google.com/file/d/", "/view?usp=sharing"), array('', ''), $url);
    return "https://drive.google.com/uc?export=download&id=" . $unique_id;
    }

    public static function get_file_name_from_disposition($header){
    if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
    return $matches[1];
    }
    if (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    return rawurldecode($matches[1]);
    }

    return false;
    }

    public static function download_external_url_by_post_id($post_id, $meta_keys){

    public static function download_external_url_by_post_id($post_id, $meta_keys){

    $result = [];

    @@ -41,8 +23,29 @@ public static function download_external_url_by_post_id($post_id, $meta_keys){

    return $result;
    }

    public static function export_google_url($url) {
    if (strpos($url, "https://drive.google.com/file/d/") === false) {
    return $url;
    }
    $unique_id = str_replace(array("https://drive.google.com/file/d/", "/view?usp=sharing"), array('', ''), $url);
    return "https://drive.google.com/uc?export=download&id=" . $unique_id;
    }

    public static function download_image($url){
    private static function get_file_name_from_disposition($header){
    if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
    return $matches[1];
    }
    if (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    return rawurldecode($matches[1]);
    }

    return false;
    }



    public static function download_image($url, $path = ''){
    $image_url = self::export_google_url($url);

    $ch = curl_init();
    @@ -60,19 +63,24 @@ public static function download_image($url){

    $header = substr($res, 0, $header_size);
    $body = substr($res, $header_size);

    $filename = self::get_file_name_from_disposition($header);


    $filename = self::get_file_name_from_disposition($header);
    if(empty($filename)){
    return false;
    }

    $upload_dir = wp_upload_dir();

    if (wp_mkdir_p($upload_dir['path'])) {
    $file = $upload_dir['path'] . '/' . $filename;
    $upload_dir = "";
    if(!empty($path)){
    $upload_dir = $path;
    } else{
    $upload_dir = wp_upload_dir()['path'];
    }

    $file = '';
    if (wp_mkdir_p($upload_dir)) {
    $file = $upload_dir . '/' . $filename;
    } else {
    $file = $upload_dir['basedir'] . '/' . $filename;
    $file = $upload_dir . '/' . $filename;
    }

    file_put_contents($file, $body);
  2. sudarshann revised this gist May 3, 2021. 1 changed file with 17 additions and 15 deletions.
    32 changes: 17 additions & 15 deletions wordpress-download-google-drive-image.php
    Original file line number Diff line number Diff line change
    @@ -18,26 +18,28 @@ public static function get_file_name_from_disposition($header){
    return false;
    }

    public static function after_Import_Save_Image_url() {

    $_posts = new \WP_Query(
    [
    'post__in' => array(15932),
    'posts_per_page' => '-1'
    ]
    );

    while ($_posts->have_posts()) {
    public static function download_external_url_by_post_id($post_id, $meta_keys){

    $result = [];

    foreach($meta_keys as $meta_key){
    $url = trim(get_post_meta($post_id, $meta_key, true));

    $_posts->the_post();
    if(empty($url)){
    $result[$meta_key] = false;
    continue;
    }

    global $post;
    if(substr( $url, 0, 4 ) !== "http"){
    $result[$meta_key] = false;
    continue;
    }

    $url = get_post_meta(get_the_ID(), 'url_logo', true);

    $logo_attachment_id = self::download_image($url);

    $result[$meta_key] = self::download_image($url);
    }

    return $result;
    }

    public static function download_image($url){
  3. sudarshann created this gist May 3, 2021.
    94 changes: 94 additions & 0 deletions wordpress-download-google-drive-image.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    class downloadGoogleDriveImage {
    public static function export_google_url($url) {
    if (strpos($url, "https://drive.google.com/file/d/") === false) {
    return $url;
    }
    $unique_id = str_replace(array("https://drive.google.com/file/d/", "/view?usp=sharing"), array('', ''), $url);
    return "https://drive.google.com/uc?export=download&id=" . $unique_id;
    }

    public static function get_file_name_from_disposition($header){
    if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
    return $matches[1];
    }
    if (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    return rawurldecode($matches[1]);
    }

    return false;
    }

    public static function after_Import_Save_Image_url() {

    $_posts = new \WP_Query(
    [
    'post__in' => array(15932),
    'posts_per_page' => '-1'
    ]
    );

    while ($_posts->have_posts()) {

    $_posts->the_post();

    global $post;

    $url = get_post_meta(get_the_ID(), 'url_logo', true);

    $logo_attachment_id = self::download_image($url);

    }
    }

    public static function download_image($url){
    $image_url = self::export_google_url($url);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $image_url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);

    $res = curl_exec($ch);

    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    curl_close($ch);

    $header = substr($res, 0, $header_size);
    $body = substr($res, $header_size);

    $filename = self::get_file_name_from_disposition($header);

    if(empty($filename)){
    return false;
    }

    $upload_dir = wp_upload_dir();

    if (wp_mkdir_p($upload_dir['path'])) {
    $file = $upload_dir['path'] . '/' . $filename;
    } else {
    $file = $upload_dir['basedir'] . '/' . $filename;
    }

    file_put_contents($file, $body);

    $wp_filetype = wp_check_filetype($filename, null);

    $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => sanitize_file_name($filename),
    'post_content' => '',
    'post_status' => 'inherit'
    );

    $attach_id = wp_insert_attachment($attachment, $file);
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    wp_update_attachment_metadata($attach_id, $attach_data);

    return $attach_id;
    }

    }