switch_to_blog() public method

Helper to switch to a Multisite blog - If the site is MS - If the blog is not the current blog defined
public switch_to_blog ( integer | boolean $blog_id = false )
$blog_id integer | boolean
 /**
  * Purge URL from cache
  *
  * @param string   $url
  * @param bool|int $blog_id
  */
 public function purge_from_cache($url, $blog_id = false)
 {
     global $wpdb;
     if (false !== $blog_id) {
         $this->as3cf->switch_to_blog($blog_id);
     }
     // Purge postmeta cache
     $sql = $wpdb->prepare("\n \t\t\tDELETE FROM {$wpdb->postmeta}\n \t\t\tWHERE meta_key = %s\n \t\t\tAND meta_value LIKE %s;\n \t\t", 'amazonS3_cache', '%"' . $url . '"%');
     $wpdb->query($sql);
     // Purge option cache
     $sql = $wpdb->prepare("\n \t\t\tDELETE FROM {$wpdb->options}\n \t\t\tWHERE option_name = %s\n \t\t\tAND option_value LIKE %s;\n \t\t", 'amazonS3_cache', '%"' . $url . '"%');
     $wpdb->query($sql);
     if (false !== $blog_id) {
         $this->as3cf->restore_current_blog();
     }
 }
示例#2
0
 /**
  * Cron job to update the region of the bucket in s3 metadata
  */
 public function do_upgrade()
 {
     // Check if the cron should even be running
     if ($this->get_saved_upgrade_id() >= $this->upgrade_id || $this->get_upgrade_status() !== self::STATUS_RUNNING) {
         $this->unschedule();
         return;
     }
     // set the batch size limit for the query
     $limit = apply_filters('as3cf_update_' . $this->upgrade_name . '_batch_size', 500);
     $all_limit = $limit;
     $finish = time() + apply_filters('as3cf_update_' . $this->upgrade_name . '_time_limit', 20);
     $session = $this->get_session();
     // find the blog IDs that have been processed so we can skip them
     $processed_blog_ids = isset($session['processed_blog_ids']) ? $session['processed_blog_ids'] : array();
     $this->error_count = isset($session['error_count']) ? $session['error_count'] : 0;
     $offset = isset($session['offset']) ? $session['offset'] : false;
     // get the table prefixes for all the blogs
     $table_prefixes = $this->as3cf->get_all_blog_table_prefixes($processed_blog_ids);
     $all_items = array();
     $all_count = 0;
     foreach ($table_prefixes as $blog_id => $table_prefix) {
         $items = $this->get_items_to_process($table_prefix, $limit, $offset);
         $count = count($items);
         if (0 === $count) {
             // No more items, record the blog ID to skip next time
             $session['offset'] = false;
             $processed_blog_ids[] = $blog_id;
         } else {
             $all_count += $count;
             $all_items[$blog_id] = $items;
         }
         if ($all_count >= $all_limit) {
             break;
         }
         $limit = $limit - $count;
     }
     if (0 === $all_count) {
         $this->upgrade_finished();
         return;
     }
     // loop through and update s3 meta with region
     foreach ($all_items as $blog_id => $items) {
         $this->as3cf->switch_to_blog($blog_id);
         foreach ($items as $item) {
             if ($this->error_count >= $this->error_threshold) {
                 $this->upgrade_error($session);
                 return;
             }
             // Do the actual upgrade to the item
             $this->upgrade_item($item);
             if (time() >= $finish || $this->as3cf->memory_exceeded('as3cf_update_' . $this->upgrade_name . '_memory_exceeded')) {
                 // Batch limits reached
                 $this->as3cf->restore_current_blog();
                 break 2;
             }
         }
         $this->as3cf->restore_current_blog();
     }
     $session['offset'] = isset($item) ? $item : false;
     $session['processed_blog_ids'] = $processed_blog_ids;
     $session['error_count'] = $this->error_count;
     $this->save_session($session);
 }
 /**
  * Cron jon to update the region of the bucket in s3 metadata
  */
 function do_upgrade()
 {
     // Check if the cron should even be running
     if ($this->as3cf->get_setting($this->settings_key, 0) >= $this->upgrade_id || $this->get_upgrade_status() !== self::STATUS_RUNNING) {
         $this->as3cf->clear_scheduled_event($this->cron_hook);
         return;
     }
     // set the batch size limit for the query
     $limit = apply_filters('as3cf_update_' . $this->upgrade_name . '_batch_size', 500);
     $all_limit = $limit;
     // only process the loop for a certain amount of time
     $minutes = $this->cron_interval_in_minutes * 60;
     // smaller time limit so won't run into another instance of cron
     $minutes = $minutes * 0.8;
     $finish = time() + $minutes;
     $session = $this->get_session();
     // find the blog IDs that have been processed so we can skip them
     $processed_blog_ids = isset($session['processed_blog_ids']) ? $session['processed_blog_ids'] : array();
     $this->error_count = isset($session['error_count']) ? $session['error_count'] : 0;
     // get the table prefixes for all the blogs
     $table_prefixes = $this->as3cf->get_all_blog_table_prefixes($processed_blog_ids);
     $all_attachments = array();
     $all_count = 0;
     foreach ($table_prefixes as $blog_id => $table_prefix) {
         $attachments = $this->get_attachments_to_process($table_prefix, $limit);
         $count = count($attachments);
         if (0 === $count) {
             // no more attachments, record the blog ID to skip next time
             $processed_blog_ids[] = $blog_id;
         } else {
             $all_count += $count;
             $all_attachments[$blog_id] = $attachments;
         }
         if ($all_count >= $all_limit) {
             break;
         }
         $limit = $limit - $count;
     }
     if (0 === $all_count) {
         $this->upgrade_finished();
         return;
     }
     // loop through and update s3 meta with region
     foreach ($all_attachments as $blog_id => $attachments) {
         $this->as3cf->switch_to_blog($blog_id);
         foreach ($attachments as $attachment) {
             if ($this->error_count >= $this->error_threshold) {
                 $this->upgrade_error($session);
                 return;
             }
             // Do the actual upgrade to the attachment
             $this->upgrade_attachment($attachment);
             if (time() >= $finish || $this->as3cf->memory_exceeded('as3cf_update_' . $this->upgrade_name . '_memory_exceeded')) {
                 // Batch limits reached
                 break 2;
             }
         }
     }
     $this->as3cf->restore_current_blog($blog_id);
     $session['processed_blog_ids'] = $processed_blog_ids;
     $session['error_count'] = $this->error_count;
     $this->save_session($session);
 }