/**
 * Media Vault internal Activation function for a single
 * blog install or for each blog site
 * in network activation mode
 *
 * @since 0.8.5
 *
 * @uses mgjp_mv_default_options()
 * @uses mgjp_mv_load_placeholder_image()
 */
function _mgjp_mv_activate_local($blog_id = 0)
{
    // register Media Vault options to the local options table
    add_option('mgjp_mv_default_permission', 'logged-in', '', 'yes');
    add_option('mgjp_mv_options', mgjp_mv_default_options(), '', 'no');
    add_option('mgjp_mv_ir', array('is_on' => true), '', 'no');
    mgjp_mv_load_placeholder_image();
    do_action('mgjp_mv_activated_local', $blog_id);
}
 /**
  * Updates plugin data from pre-version 0.8 to version 0.8
  * Loads the Media Vault place-holder image
  * which is used from 0.8 onwards to replace
  * images a user does not have permission to view.
  * Modifies the db replacing all references to the deprecated
  * `mgjp_mv_meta` post meta key with the new `_mgjp_mv_permission`
  * post meta key.
  *
  * @since 0.8
  *
  * @uses mgjp_mv_load_placeholder_image()
  */
 function update_08()
 {
     // edit existing options
     $default_permission = get_option('mgjp_mv_default_permission', 'logged-in');
     delete_option('mgjp_mv_default_permission');
     add_option('mgjp_mv_default_permission', $default_permission, '', 'yes');
     // add options
     add_option('mgjp_mv_ir', array('is_on' => true), '', 'no');
     // load place-holder image into Media Library
     mgjp_mv_load_placeholder_image();
     /** correctly replace old `mgjp_mv_meta` meta with new `_mgjp_mv_permission` meta **/
     global $wpdb;
     // get all posts with the old 'mgjp_mv_meta' meta key
     $old = $wpdb->get_results($wpdb->prepare("\n        SELECT      meta_id, meta_value\n        FROM        {$wpdb->postmeta}\n        WHERE       meta_key = %s\n        ", 'mgjp_mv_meta'), ARRAY_A);
     if (empty($old)) {
         return;
     }
     // distill the meta values which have custom
     // permissions set on them
     foreach ($old as $columns) {
         $meta = unserialize($columns['meta_value']);
         if (!isset($meta['permission'])) {
             continue;
         }
         $ids[] = $columns['meta_id'];
         $data[$meta['permission']][] = $columns['meta_id'];
     }
     if (!isset($data)) {
         return delete_post_meta_by_key('mgjp_mv_meta');
     }
     // build the sql update query to convert the old meta system
     // using 'mgjp_mv_meta' to the new meta system using '_mgjp_mv_permission'
     $sql_update[] = $wpdb->prepare("UPDATE `{$wpdb->postmeta}` SET `meta_key` = %s, `meta_value` = CASE", '_mgjp_mv_permission');
     foreach ($data as $meta_value => $meta_ids) {
         if (isset($meta_ids[1])) {
             $sql_update[] = $wpdb->prepare("WHEN `meta_id` IN (" . implode(', ', array_fill(0, count($meta_ids), '%d')) . ") THEN %s", array_merge($meta_ids, array($meta_value)));
         } else {
             $sql_update[] = $wpdb->prepare("WHEN `meta_id` = %d THEN %s", $meta_ids[0], $meta_value);
         }
     }
     $sql_update[] = $wpdb->prepare("ELSE %s END WHERE `meta_id` IN (" . implode(', ', array_fill(0, count($ids), '%d')) . ")", array_merge(array('logged-in'), $ids));
     // run the update query
     $wpdb->query(implode(' ', $sql_update));
     // and delete all other references to 'mgjp_mv_meta'
     delete_post_meta_by_key('mgjp_mv_meta');
 }