/**
  * Loads thePlatform plugin options from
  * the database into their respective arrays. Uses
  * array_merge to merge with default values if they're
  * missing.
  */
 function load_options()
 {
     // Get existing options, or empty arrays if no options exist
     $this->account_options = get_option(TP_ACCOUNT_OPTIONS_KEY, array());
     $this->preferences_options = get_option(TP_PREFERENCES_OPTIONS_KEY, array());
     $this->metadata_options = get_option(TP_METADATA_OPTIONS_KEY, array());
     $this->upload_options = get_option(TP_UPLOAD_OPTIONS_KEY, array());
     // Initialize option defaults
     $this->account_options = array_merge(TP_ACCOUNT_OPTIONS_DEFAULTS(), $this->account_options);
     $this->preferences_options = array_merge(TP_PREFERENCES_OPTIONS_DEFAULTS(), $this->preferences_options);
     if (!$this->upload_options) {
         update_option(TP_UPLOAD_OPTIONS_KEY, TP_UPLOAD_FIELDS_DEFAULTS());
     }
     if (!$this->metadata_options) {
         update_option(TP_METADATA_OPTIONS_KEY, array());
     }
     $this->account_is_verified = $this->tp_api->internal_verify_account_settings();
     if ($this->account_is_verified) {
         $this->region_is_verified = $this->tp_api->internal_verify_account_region();
     } else {
         $this->region_is_verified = FALSE;
         if ($this->account_options['mpx_username'] != 'mpx/') {
             echo '<div id="message" class="error">';
             echo '<p><strong>Sign in to thePlatform failed, please check your account settings.</strong></p>';
             echo '</div>';
         }
     }
 }
 /**
  * Checks if the plugin has been updated and performs any necessary updates.
  */
 function check_plugin_update()
 {
     $oldVersion = $this->plugin_version_changed();
     if (false === $oldVersion) {
         return;
     }
     $newVersion = TP_PLUGIN_VERSION();
     // On any version, update defaults that didn't previously exist
     $newPreferences = array_merge(TP_PREFERENCES_OPTIONS_DEFAULTS(), get_option(TP_PREFERENCES_OPTIONS_KEY, array()));
     $newPreferences['plugin_version'] = TP_PLUGIN_VERSION;
     update_option(TP_PREFERENCES_OPTIONS_KEY, $newPreferences);
     update_option(TP_ACCOUNT_OPTIONS_KEY, array_merge(TP_ACCOUNT_OPTIONS_DEFAULTS(), get_option(TP_ACCOUNT_OPTIONS_KEY, array())));
     update_option(TP_BASIC_METADATA_OPTIONS_KEY, array_merge(TP_BASIC_METADATA_OPTIONS_DEFAULTS(), get_option(TP_BASIC_METADATA_OPTIONS_KEY, array())));
     // We had a messy update with 1.2.2/1.3.0, let's clean up
     if ($oldVersion['major'] == '1' && $oldVersion['minor'] == '2' && $oldVersion['patch'] == '2' || $oldVersion['major'] == '1' && $oldVersion['minor'] == '3' && $oldVersion['patch'] == '0') {
         $basicMetadataFields = get_option(TP_BASIC_METADATA_OPTIONS_KEY, array());
         $customMetadataFields = get_option(TP_CUSTOM_METADATA_OPTIONS_KEY, array());
         update_option(TP_BASIC_METADATA_OPTIONS_KEY, array_diff_assoc($basicMetadataFields, $customMetadataFields));
     }
     // Move account settings from preferences (1.2.0)
     if ($oldVersion['major'] == '1' && $oldVersion['minor'] < '2' && ($newVersion['major'] > '1' || $newVersion['major'] >= '1' && $newVersion['minor'] >= '2')) {
         $preferences = get_option(TP_PREFERENCES_OPTIONS_KEY, array());
         if (array_key_exists('mpx_account_id', $preferences)) {
             $accountSettings = TP_ACCOUNT_OPTIONS_DEFAULTS();
             foreach ($preferences as $key => $value) {
                 if (array_key_exists($key, $accountSettings)) {
                     $accountSettings[$key] = $preferences[$key];
                 }
             }
             update_option(TP_ACCOUNT_OPTIONS_KEY, $accountSettings);
         }
     }
 }
/**
 * Checks if the plugin has been updated and performs any necessary updates.
 */
function theplatform_check_plugin_update()
{
    $oldVersion = theplatform_plugin_version_changed();
    if (FALSE === $oldVersion) {
        return;
    }
    $newVersion = TP_PLUGIN_VERSION();
    // On any version, update defaults that didn't previously exist and update the plugin version
    $newPreferences = array_merge(TP_PREFERENCES_OPTIONS_DEFAULTS(), get_option(TP_PREFERENCES_OPTIONS_KEY, array()));
    $newPreferences['plugin_version'] = TP_PLUGIN_VERSION;
    update_option(TP_PREFERENCES_OPTIONS_KEY, $newPreferences);
    update_option(TP_ACCOUNT_OPTIONS_KEY, array_merge(TP_ACCOUNT_OPTIONS_DEFAULTS(), get_option(TP_ACCOUNT_OPTIONS_KEY, array())));
    update_option(TP_UPLOAD_OPTIONS_KEY, array_merge(TP_UPLOAD_FIELDS_DEFAULTS(), get_option(TP_UPLOAD_OPTIONS_KEY, array())));
    // Set the default embed hook and media embed types (1.2.2 ~ 1.3.2)
    if ($oldVersion['major'] == '1' && $oldVersion['minor'] == '2' && $oldVersion['patch'] == '2' || $oldVersion['major'] == '1' && $oldVersion['minor'] == '3' && $oldVersion['patch'] < '3') {
        if (defined('WPCOM_IS_VIP_ENV')) {
            $newPreferences['embed_hook'] = 'tinymce';
            // VIP Only
        }
        $newPreferences['media_embed_type'] = 'release';
        update_option(TP_PREFERENCES_OPTIONS_KEY, $newPreferences);
    }
    // We had a messy update with 1.2.2/1.3.0, let's clean up
    if ($oldVersion['major'] == '1' && $oldVersion['minor'] == '2' && $oldVersion['patch'] == '2' || $oldVersion['major'] == '1' && $oldVersion['minor'] == '3' && $oldVersion['patch'] == '0') {
        $basicMetadataFields = get_option(TP_UPLOAD_OPTIONS_KEY, array());
        $customMetadataFields = get_option(TP_METADATA_OPTIONS_KEY, array());
        update_option(TP_UPLOAD_OPTIONS_KEY, array_diff_assoc($basicMetadataFields, $customMetadataFields));
    }
    // Move account settings from preferences (1.2.0)
    if ($oldVersion['major'] == '1' && $oldVersion['minor'] < '2' && ($newVersion['major'] > '1' || $newVersion['major'] >= '1' && $newVersion['minor'] >= '2')) {
        $preferences = get_option(TP_PREFERENCES_OPTIONS_KEY, array());
        if (array_key_exists('mpx_account_id', $preferences)) {
            $accountSettings = TP_ACCOUNT_OPTIONS_DEFAULTS();
            foreach ($preferences as $key => $value) {
                if (array_key_exists($key, $accountSettings)) {
                    $accountSettings[$key] = $preferences[$key];
                }
            }
            update_option(TP_ACCOUNT_OPTIONS_KEY, $accountSettings);
        }
    }
}