/**
  * Uploads given file attachment onto CDN if it doesn't already exist
  */
 public function upload_file($file_path, $file_name = null, $existing_container = null, $post_id = null)
 {
     global $wpdb;
     // Check if file exists
     $check_file_name = isset($file_name) ? $file_name : basename($file_path);
     if (verify_exists($check_file_name)) {
         return true;
     } else {
         // Get ready to upload file to CDN
         $container = $this->container_object();
         $file = $this->file_object($container, $file_path, $file_name);
         // Upload object
         if ($this->opencloud_version == '1.10.0') {
             if ($container->uploadObject($file['file_name'], $file['file_content'])) {
                 return true;
             }
         } else {
             $content_type = get_content_type($file_path);
             if ($content_type !== false) {
                 if ($file->Create(array('content_type' => $content_type))) {
                     return true;
                 }
             } else {
                 if ($file->Create()) {
                     return true;
                 }
             }
         }
     }
     // Upload failed, remove local images
     if (stripos($file_path, 'http') == 0) {
         $upload_dir = wp_upload_dir();
         $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_path);
         unlink($file_path);
     } else {
         unlink($file_path);
     }
     // Upload failed, remove attachment from db
     if (isset($post_id)) {
         $wpdb->query("DELETE FROM {$wpdb->posts} WHERE ID='{$post_id}' AND post_type='attachment'");
         $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id='{$post_id}'");
     }
     return false;
 }
/**
 *	Set CDN path for image
 */
function set_cdn_path($attachment)
{
    // Get CDN object and settings
    $_SESSION['cdn'] = isset($_SESSION['cdn']) ? $_SESSION['cdn'] : new RS_CDN();
    // Uploads folder data
    $upload_data = wp_upload_dir();
    // Get public CDN URL
    try {
        if (isset($_SESSION['cdn']->api_settings->custom_cname) && trim($_SESSION['cdn']->api_settings->custom_cname) != '') {
            $cdn_url = $_SESSION['cdn']->api_settings->custom_cname;
        } else {
            $cdn_url = isset($_SESSION['cdn']->api_settings->use_ssl) ? get_cdn_url('ssl') : get_cdn_url();
        }
    } catch (Exception $e) {
        return $attachment;
    }
    // Rewrite URLs
    if (current_filter() == 'wp_get_attachment_url') {
        if (file_exists(str_replace($upload_data['baseurl'], $upload_data['basedir'], $attachment)) !== false) {
            $remote_file_url = str_replace($upload_data['baseurl'], $cdn_url, $attachment);
            if (verify_exists($remote_file_url)) {
                return $remote_file_url;
            } else {
                return $attachment;
            }
        } else {
            return str_replace($upload_data['baseurl'], $cdn_url, $attachment);
        }
    } else {
        preg_match_all('/\\"(http|https).*?\\/wp\\-content\\/.*?\\/\\d{4}+\\/\\d{2}+\\/.*?\\"/i', $attachment, $attachments);
        foreach ($attachments[0] as $cur_attachment) {
            // If local file does not exist, replace local URL with CDN URL
            $cur_attachment = trim($cur_attachment, '"');
            if (file_exists(str_replace($upload_data['baseurl'], $upload_data['basedir'], $cur_attachment)) === false) {
                $new_attachment = str_replace($upload_data['baseurl'], $cdn_url, $cur_attachment);
                if (verify_exists($new_attachment)) {
                    $attachment = str_replace($cur_attachment, $new_attachment, $attachment);
                }
            } else {
                // File exists locally, check if CDN file is there
                $new_attachment = str_replace($upload_data['baseurl'], $cdn_url, $cur_attachment);
                if (verify_exists($new_attachment)) {
                    $attachment = str_replace($cur_attachment, $new_attachment, $attachment);
                }
            }
        }
        return $attachment;
    }
    die;
}
/**
 *	Set CDN path for image
 */
function set_cdn_path($attachment)
{
    // Ensure CDN instance exists
    if (check_cdn() === false) {
        return $attachment;
    }
    // Uploads folder data
    $upload_data = wp_upload_dir();
    // If HTTPS is on, replace http with https URL
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
        $local_uploads_url = str_replace('http://', 'https://', $upload_data['baseurl']) . '/';
    } else {
        $local_uploads_url = $upload_data['baseurl'];
    }
    // Get public CDN URL
    try {
        if (isset($_SESSION['cdn']->api_settings->custom_cname) && trim($_SESSION['cdn']->api_settings->custom_cname) != '') {
            $cdn_url = $_SESSION['cdn']->api_settings->custom_cname;
        } else {
            $cdn_url = isset($_SESSION['cdn']->api_settings->use_ssl) ? get_cdn_url('ssl') : get_cdn_url();
        }
    } catch (Exception $e) {
        return $attachment;
    }
    // Get base URL defined for uploads
    $base_uploads_url = str_replace(array('http://', 'https://'), '(http|https)://', $upload_data['baseurl']);
    $base_uploads_url = str_replace('/', '\\/', $base_uploads_url);
    // Loop through attachments and rewrite with CDN URL if they are on the CDN
    preg_match_all('/' . $base_uploads_url . '\\/.*?\\.[a-z]{3}+/i', $attachment, $attachments);
    // Get attachments and check if on CDN
    if (count($attachments) > 0) {
        foreach ($attachments as $cur_attachments) {
            // Check if array
            foreach ($cur_attachments as $cur_attachment) {
                if ($cur_attachment != 'http' && $cur_attachment != 'https') {
                    // Verify attachment exists
                    $new_attachment = trim(str_replace($local_uploads_url, '', $cur_attachment), '/');
                    // If we are good to go, return the attachment
                    if (verify_exists($new_attachment)) {
                        $attachment = str_replace($cur_attachment, $cdn_url . '/' . $new_attachment, $attachment);
                    }
                }
            }
        }
    }
    // Return attachment
    return $attachment;
}