| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
class PS_Migrator { |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle the migration from v3 to v4 in Passster. |
| 9 |
* |
| 10 |
* @return void |
| 11 |
*/ |
| 12 |
public static function migrate() { |
| 13 |
// Reset new settings first. |
| 14 |
$options = array(); |
| 15 |
|
| 16 |
// Start migration. |
| 17 |
$options = self::migrate_customizer_settings( $options ); |
| 18 |
$options = self::migrate_options( $options ); |
| 19 |
|
| 20 |
update_option( 'passster', $options ); |
| 21 |
|
| 22 |
// Now we can delete the old options. |
| 23 |
self::delete_options(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Migrates customizer settings to option. |
| 28 |
* |
| 29 |
* @param $options |
| 30 |
* |
| 31 |
* @return mixed |
| 32 |
*/ |
| 33 |
public static function migrate_customizer_settings( $options ) { |
| 34 |
$customizer_data = array( |
| 35 |
'headline' => get_theme_mod( 'passster_form_instructions_headline' ), |
| 36 |
'instruction' => get_theme_mod( 'passster_form_instructions_text' ), |
| 37 |
'placeholder' => get_theme_mod( 'passster_form_instructions_placeholder' ), |
| 38 |
'error' => get_theme_mod( 'passster_form_error_text' ), |
| 39 |
'button_label' => get_theme_mod( 'passster_form_button_label' ), |
| 40 |
'show_password' => get_theme_mod( 'passster_form_instructions_password_typing' ), |
| 41 |
'form_background_color' => get_theme_mod( 'passster_form_general_background_color' ), |
| 42 |
'headline_font_color' => get_theme_mod( 'passster_form_instructions_headline_color' ), |
| 43 |
'headline_font_size' => get_theme_mod( 'passster_form_instructions_headline_font_size' ), |
| 44 |
'headline_font_weight' => get_theme_mod( 'passster_form_instructions_headline_font_weight' ), |
| 45 |
'instruction_font_color' => get_theme_mod( 'passster_form_instructions_text_color' ), |
| 46 |
'instruction_font_size' => get_theme_mod( 'passster_form_instructions_text_font_size' ), |
| 47 |
'instruction_font_weight' => get_theme_mod( 'passster_form_instructions_text_font_weight' ), |
| 48 |
'button_background_color' => get_theme_mod( 'passster_form_button_background_color' ), |
| 49 |
'button_font_color' => get_theme_mod( 'passster_form_button_text_color' ), |
| 50 |
'button_font_size' => '16px', |
| 51 |
'button_background_color_hover' => get_theme_mod( 'passster_form_button_background_hover_color' ), |
| 52 |
'button_font_color_hover' => get_theme_mod( 'passster_form_button_text_hover_color' ), |
| 53 |
'form_max_width' => '700px', |
| 54 |
'form_border_radius' => 0, |
| 55 |
'form_padding' => array( |
| 56 |
'top' => '20px', |
| 57 |
'right' => '20px', |
| 58 |
'bottom' => '20px', |
| 59 |
'left' => '20px' |
| 60 |
), |
| 61 |
'form_margin' => array( |
| 62 |
'top' => '0px', |
| 63 |
'right' => '0px', |
| 64 |
'bottom' => '0px', |
| 65 |
'left' => '0px' |
| 66 |
), |
| 67 |
'button_padding' => array( |
| 68 |
'top' => '10px', |
| 69 |
'right' => '10px', |
| 70 |
'bottom' => '10px', |
| 71 |
'left' => '10px' |
| 72 |
), |
| 73 |
'button_margin' => array( |
| 74 |
'top' => '0px', |
| 75 |
'right' => '0px', |
| 76 |
'bottom' => '0px', |
| 77 |
'left' => '0px' |
| 78 |
), |
| 79 |
'button_border_radius' => 0, |
| 80 |
); |
| 81 |
|
| 82 |
$default_data = array( |
| 83 |
'headline' => __( 'Protected Area', 'content-protector' ), |
| 84 |
'instruction' => __( 'This content is password-protected. Please verify with a password to unlock the content.', 'content-protector' ), |
| 85 |
'placeholder' => __( 'Enter your password..', 'content-protector' ), |
| 86 |
'error' => __( 'Sorry, there was an error.', 'content-protector' ), |
| 87 |
'button_label' => __( 'Unlock', 'content-protector' ), |
| 88 |
'show_password' => false, |
| 89 |
'form_background_color' => '#FAFAFA', |
| 90 |
'headline_font_color' => '#6804cc', |
| 91 |
'headline_font_size' => 24, |
| 92 |
'headline_font_weight' => 500, |
| 93 |
'instruction_font_color' => '#000', |
| 94 |
'instruction_font_size' => 16, |
| 95 |
'instruction_font_weight' => 300, |
| 96 |
'button_background_color' => '#6804cc', |
| 97 |
'button_font_color' => '#fff', |
| 98 |
'button_font_size' => 16, |
| 99 |
'button_background_color_hover' => '#000', |
| 100 |
'button_font_color_hover' => '#fff', |
| 101 |
'form_border_radius' => 0, |
| 102 |
'button_border_radius' => 0, |
| 103 |
); |
| 104 |
|
| 105 |
foreach ( $customizer_data as $option => $value ) { |
| 106 |
if ( ! empty( $value ) ) { |
| 107 |
$options[ $option ] = $value; |
| 108 |
} else { |
| 109 |
$options[ $option ] = $default_data[ $option ]; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $options; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Migrate old options to new option. |
| 118 |
* |
| 119 |
* @param $options |
| 120 |
* |
| 121 |
* @return mixed |
| 122 |
*/ |
| 123 |
public static function migrate_options( $options ) { |
| 124 |
// Migrate general settings. |
| 125 |
$general_settings = get_option( 'passster_general_settings' ); |
| 126 |
|
| 127 |
if ( ! is_array( $general_settings ) ) { |
| 128 |
return $options; |
| 129 |
} |
| 130 |
|
| 131 |
if ( 'on' !== $general_settings['toggle_cookie'] ) { |
| 132 |
$options['disable_cookie'] = false; |
| 133 |
} |
| 134 |
|
| 135 |
if ( 'on' === $general_settings['toggle_ajax'] ) { |
| 136 |
$options['unlock_mode'] = true; |
| 137 |
} |
| 138 |
|
| 139 |
if ( 'on' === $general_settings['passster_activate_concurrent'] ) { |
| 140 |
$options['use_concurrent'] = true; |
| 141 |
} |
| 142 |
|
| 143 |
if ( 'on' === $general_settings['passster_activate_concurrent'] ) { |
| 144 |
$options['use_concurrent'] = true; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! empty( $general_settings['passster_cookie_duration'] ) ) { |
| 148 |
$options['cookie_duration'] = $general_settings['passster_cookie_duration']; |
| 149 |
} else { |
| 150 |
$options['cookie_duration'] = "1"; |
| 151 |
} |
| 152 |
|
| 153 |
$options['third_party_shortcodes'] = $general_settings['third_party_shortcodes']; |
| 154 |
$options['number_of_concurrents'] = $general_settings['passster_number_of_concurrent_logins']; |
| 155 |
|
| 156 |
update_option( 'passster', $options ); |
| 157 |
|
| 158 |
// Migrate advanced settings. |
| 159 |
$advanced_settings = get_option( 'passster_advanced_settings' ); |
| 160 |
|
| 161 |
if ( ! is_array( $advanced_settings ) ) { |
| 162 |
return $options; |
| 163 |
} |
| 164 |
|
| 165 |
$options['recaptcha_version'] = $advanced_settings['passster_recaptcha_type']; |
| 166 |
$options['recaptcha_site_key'] = $advanced_settings['passster_recaptcha_site_key']; |
| 167 |
$options['recaptcha_secret'] = $advanced_settings['passster_recaptcha_secret']; |
| 168 |
$options['recaptcha_language'] = $advanced_settings['passster_recaptcha_language']; |
| 169 |
$options['bitly_token'] = $advanced_settings['passster_bitly_access_key']; |
| 170 |
|
| 171 |
// Migrate Global Protection settings. |
| 172 |
$options['global_protection_id'] = get_option( 'passster_global_id' ); |
| 173 |
|
| 174 |
// Also add new options to avoid missing keys on migration. |
| 175 |
$options['password_length'] = "6"; |
| 176 |
$options['include_uppercase'] = false; |
| 177 |
$options['include_numbers'] = false; |
| 178 |
$options['cookie_duration_unit'] = 'days'; |
| 179 |
|
| 180 |
return $options; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Delete old options. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public static function delete_options() { |
| 189 |
// Delete theme mods. |
| 190 |
remove_theme_mod( 'passster_form_instructions_headline' ); |
| 191 |
remove_theme_mod( 'passster_form_instructions_text' ); |
| 192 |
remove_theme_mod( 'passster_form_instructions_placeholder' ); |
| 193 |
remove_theme_mod( 'passster_form_error_text' ); |
| 194 |
remove_theme_mod( 'passster_form_button_label' ); |
| 195 |
remove_theme_mod( 'passster_form_instructions_password_typing' ); |
| 196 |
remove_theme_mod( 'passster_form_general_background_color' ); |
| 197 |
remove_theme_mod( 'passster_form_instructions_headline_font_size' ); |
| 198 |
remove_theme_mod( 'passster_form_instructions_headline_font_weight' ); |
| 199 |
remove_theme_mod( 'passster_form_instructions_text_color' ); |
| 200 |
remove_theme_mod( 'passster_form_instructions_text_font_size' ); |
| 201 |
remove_theme_mod( 'passster_form_instructions_text_font_weight' ); |
| 202 |
remove_theme_mod( 'passster_form_button_background_color' ); |
| 203 |
remove_theme_mod( 'passster_form_button_text_color' ); |
| 204 |
remove_theme_mod( 'passster_form_button_background_hover_color' ); |
| 205 |
remove_theme_mod( 'passster_form_button_text_hover_color' ); |
| 206 |
|
| 207 |
// Delete old options. |
| 208 |
delete_option( 'passster_general_settings' ); |
| 209 |
delete_option( 'passster_advanced_settings' ); |
| 210 |
delete_option( 'passster_global_id' ); |
| 211 |
} |
| 212 |
} |
| 213 |
|