Ejemplo n.º 1
0
 /**
  * A loop that syncs files to the Google Drive
  * @since 1.0
  */
 public function db_sync()
 {
     //if( ! isset( $_REQUEST['wpbgdc_nonce'] ) ) return;
     // nonces are always attached to a user
     // therefore nonces will not work because there is no user that can have a nonce
     //if( ! wp_verify_nonce( $_REQUEST['wpbgdc_nonce'], 'wpbgdc_sync_nonce' ) ) return;
     global $wpdb;
     if (!$wpdb instanceof wpdb) {
         return;
     }
     // starting sync
     update_option('wpbgdc_currently_syncing', time());
     // delete old caching entries if any
     WPB_Google_Drive_Cdn_Service_Helper::remove_old_entries();
     // @see http://codex.wordpress.org/Function_Reference/wp_upload_dir
     //$upload_dir = wp_upload_dir();
     //$content_url = trailingslashit( $upload_dir['baseurl'] ); // this is wp-content/uploads/
     $content_url = trailingslashit(content_url());
     // this is wp-content/
     //$upload_dir  = trailingslashit( $upload_dir['basedir'] );
     $content_dir = trailingslashit(WP_CONTENT_DIR);
     $service_helper = new WPB_Google_Drive_Cdn_Service_Helper($this);
     /**
      * If the "Upload new files only" option is set, the plugin will not check if the file has changed on Google Drive
      * Instead it just uploads the files that have not yet been uploaded
      */
     $options = get_option('wpbgdc');
     if (!isset($options['upload_new_files_only'])) {
         $options['upload_new_files_only'] = 0;
     }
     $upload_only = (bool) $options['upload_new_files_only'];
     /***
      * CHECK FOR NEW OR UPDATED FILES AND UPLOAD OR UPDATE THEM
      */
     if ($upload_only) {
         $db_files = $wpdb->get_results('SELECT * FROM `' . $wpdb->prefix . 'wpbgdc_files` WHERE `file_synced` = "0" AND `file_drive_id` = ""', OBJECT);
     } else {
         $db_files = $wpdb->get_results('SELECT * FROM `' . $wpdb->prefix . 'wpbgdc_files` WHERE `file_synced` = "0"', OBJECT);
     }
     /**
      * check if the script should sleep after a file upload
      */
     $sleeping_time = WPB_Google_Drive_Cdn_Settings::get_setting('sleep_time');
     $sleeping_time = intval($sleeping_time);
     $sleeping_time = max(0, $sleeping_time);
     foreach ($db_files as $db_file) {
         $url = $db_file->file_live_url;
         $is_content_url = false !== strpos($url, $content_url) ? true : false;
         /**
          * Workaround for SSL-Sites
          * This sets the $content_url to a new value when the file could not be found on https:// but on http
          * or the other way around
          */
         if (!$is_content_url) {
             // are we on ssl?
             if (false !== strpos($content_url, 'https://')) {
                 // yes, we are on SSL
                 // check if the file was found on non ssl-url
                 $is_content_url = false !== stripos($url, str_replace('https://', 'http://', $content_url)) ? true : false;
                 if ($is_content_url) {
                     $content_url = str_replace('https://', 'http://', $content_url);
                 }
             } else {
                 // no, we are not on SSL
                 $is_content_url = false !== stripos($url, str_replace('http://', 'https://', $content_url)) ? true : false;
                 if ($is_content_url) {
                     $content_url = str_replace('http://', 'https://', $content_url);
                 }
             }
         }
         if (!$is_content_url) {
             // set a flag in the db entry to prevent doing the same operation again
             $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id));
             continue;
         }
         // get the path to the file
         $file = str_replace($content_url, $content_dir, $url);
         // if the file does not exist on the server, return the url = stop here
         if (!is_file($file)) {
             // set a flag in the db entry to prevent doing the same operation again
             $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id));
             continue;
         }
         // count file uploads. set to 0 if not yet set
         if (!isset($this->file_upload_count)) {
             $this->file_upload_count = 0;
         }
         // stop if 3 files have been uploaded because otherwise it's too much for the webserver
         if ($this->file_upload_count >= 3) {
             // redirect will not work here because this was invoked by wp_remote_post and this function does not follow redirects to an unlimited length
             // wp_redirect( admin_url( 'admin-ajax.php' ) . '?action=wpbgdc_db_sync&wpbgdc_stop_ping=1&wpbgdc_nonce=' . wp_create_nonce( 'wpbgdc_sync_nonce' ), 301 );
             // so we start doing another ping, but at first we have to stop the current sync process to restart it later (in the ping_sync() function)
             update_option('wpbgdc_currently_syncing', 0);
             if (isset($_REQUEST['wpbgdc_stop_ping'])) {
                 unset($_REQUEST['wpbgdc_stop_ping']);
             }
             $this->ping_sync();
             // then die
             die;
         }
         // upload the file and update the database entry
         $service_helper->file_upload($file, $url);
         sleep($sleeping_time);
         // only count if a file has been uploaded
         if ($service_helper->_file_uploaded) {
             $this->file_upload_count++;
         }
         // mark as synced anyway (it doesn't really matter if the file has been uploaded or not
         // the only thing that matters if there has been the approach to upload it
         $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id));
     }
     // stop syncing
     update_option('wpbgdc_currently_syncing', 0);
     // reset the synced flag within all database entries
     $wpdb->query('UPDATE `' . $wpdb->prefix . 'wpbgdc_files` SET `file_synced`= 0');
 }
 /**
  * @return bool
  * @since 1.0
  */
 public function delete_folder()
 {
     $service = new Google_Service_Drive($this->_google_drive_cdn->get_google_client());
     // get the folder id
     $folder_id = WPB_Google_Drive_Cdn_Settings::get_setting('folder_id', 'wpbgdc_folders');
     try {
         $service->files->trash($folder_id);
         // deleting the files directly will result in a bug @see http://stackoverflow.com/questions/16573970/google-drive-api-delete-method-bug
         // $service->files->delete( $folder_id );
     } catch (Exception $e) {
         $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: delete_folder 1)', false);
         return false;
     }
     return true;
 }
 /**
  * Outputs the metabox content for the information
  * @return void
  * @since 1.0
  */
 public function info()
 {
     $folder_name = WPB_Google_Drive_Cdn_Settings::get_setting('folder_name', 'wpbgdc_folders');
     $folder_link = WPB_Google_Drive_Cdn_Settings::get_setting('folder_link', 'wpbgdc_folders');
     echo '<p><strong>', sprintf(__('You are now ready to go! The plugin has created a folder named %1$s on your Google Drive. Click here to view all files: %2$s', $this->_google_drive_cdn->get_textdomain()), $folder_name, '<a href="' . $folder_link . '" target="_blank">' . $folder_link . '</a>'), '</strong></p>';
     echo '<p>', __('The plugin works like this:', $this->_google_drive_cdn->get_textdomain()), '</p>';
     echo '<ol>';
     echo '<li>' . __('If anyone visits a page of your website, the plugin searches for files that can be uploaded to Google Drive.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('If the file is not yet uploaded to Google Drive the plugin will put it down into the cache-database for a later upload.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('Every hour the plugin reads all these entries from the cache-database to sync files to your Google Drive that have not yet been synced.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('Synced files can then be served directly from your Google Drive.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('You can manually start syncing the files that have been written to the cache-database by clicking the "Sync local CDN"-button.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('From time to time every single file gets located and uploaded to Google Drive. Because of this process the speed of your website should improve continuously.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '</ol>';
     echo '<p>', __('Please note:', $this->_google_drive_cdn->get_textdomain()), '</p>';
     echo '<ol>';
     echo '<li>' . __('It is recommended to use a "caching plugin" like WP-Super-Cache to significantly improve page load time.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('You can start sync your local media library to Google Drive using the "Sync my media library now"-button. Depending on the size of your media library this will take some time.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('Google Drive delivers all files via secure SSL. If you are not using SSL on your domain you should remove CSS files from the file extensions field below. This is because it can lead to the "Mixed Content Blocker" problem some browsers have when your CSS file tries to import Non-SSL files via the @import function. The plugin will automatically stop referring to CSS files on Google Drive if it detects that your domain does not support SSL.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '<li>' . __('As per default this plugin syncs new or updated files every hour. This can cause problems if you\'re using a WordPress theme which change files on the file system. Try to manually sync files after changing any theme-related things.', $this->_google_drive_cdn->get_textdomain()) . '</li>';
     echo '</ol>';
 }