/**
  * 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>';
         }
     }
 }
 /**
  * Validate mpx Account Settings for invalid input
  *
  * @param array $input Passed by Wordpress, an Array of mpx options
  *
  * @return array A cleaned up copy of the array, invalid values will be cleared.
  */
 function theplatform_account_options_validate($input)
 {
     require_once dirname(__FILE__) . '/thePlatform-API.php';
     $tp_api = new ThePlatform_API();
     $defaults = TP_ACCOUNT_OPTIONS_DEFAULTS();
     if (!is_array($input) || $input['mpx_username'] === 'mpx/') {
         return $defaults;
     }
     $account_is_verified = $tp_api->verify_account_settings();
     if ($account_is_verified) {
         if (strpos($input['mpx_account_id'], '|') !== false) {
             $ids = explode('|', $input['mpx_account_id']);
             $input['mpx_account_id'] = $ids[0];
             $input['mpx_account_pid'] = $ids[1];
         }
         if (strpos($input['mpx_region'], '|') !== false) {
             $ids = explode('|', $input['mpx_region']);
             $input['mpx_region'] = $ids[0];
         }
     }
     foreach ($input as $key => $value) {
         $input[$key] = sanitize_text_field($value);
     }
     // If username, account id, or region have changed, reset settings to default
     $old_preferences = get_option(TP_ACCOUNT_OPTIONS_KEY);
     if ($old_preferences) {
         $updates = false;
         // If the username changes, reset all preferences except user/pass
         if ($this->theplatform_setting_changed('mpx_username', $old_preferences, $input)) {
             $input['mpx_region'] = $defaults['mpx_region'];
             $input['mpx_account_pid'] = $defaults['mpx_account_pid'];
             $input['mpx_account_id'] = $defaults['mpx_account_id'];
             $updates = true;
         }
         // If the region changed, reset all preferences, but keep the new account settings
         if ($this->theplatform_setting_changed('mpx_region', $old_preferences, $input)) {
             $updates = true;
         }
         // If the account changed, reset all preferences, but keep the new account settings
         if ($this->theplatform_setting_changed('mpx_account_id', $old_preferences, $input)) {
             $updates = true;
         }
         // Clear old options
         if ($updates) {
             delete_option(TP_PREFERENCES_OPTIONS_KEY);
             delete_option(TP_CUSTOM_METADATA_OPTIONS_KEY);
             delete_option(TP_BASIC_METADATA_OPTIONS_KEY);
             delete_option(TP_TOKEN_OPTIONS_KEY);
         }
     }
     return $input;
 }
/**
 * 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);
        }
    }
}