| @@ -2,8 +2,10 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | +use WP_Syntex\Polylang\Options\Options; | |
| 7 | + | |
| 6 | 8 | /** |
| 7 | 9 | * Manages Polylang upgrades |
| 8 | 10 | * |
| 9 | 11 | * @since 1.2 |
| @@ -54,8 +56,9 @@ | ||
| 54 | 56 | add_action( 'all_admin_notices', array( $this, 'admin_notices' ) ); |
| 55 | 57 | return false; |
| 56 | 58 | } |
| 57 | 59 | |
| 60 | + delete_transient( 'pll_languages_list' ); | |
| 58 | 61 | add_action( 'admin_init', array( $this, '_upgrade' ) ); |
| 59 | 62 | return true; |
| 60 | 63 | } |
| 61 | 64 | |
| @@ -102,17 +105,26 @@ | ||
| 102 | 105 | * |
| 103 | 106 | * @return void |
| 104 | 107 | */ |
| 105 | 108 | public function _upgrade() { |
| 106 | - foreach ( array( '2.0.8', '2.1', '2.7', '2.8.1' ) as $version ) { | |
| 109 | + foreach ( array( '2.0.8', '2.1', '2.7', '3.4', '3.7' ) as $version ) { | |
| 107 | 110 | if ( version_compare( $this->options['version'], $version, '<' ) ) { |
| 108 | - call_user_func( array( $this, 'upgrade_' . str_replace( '.', '_', $version ) ) ); | |
| 111 | + $method_to_call = array( $this, 'upgrade_' . str_replace( '.', '_', $version ) ); | |
| 112 | + if ( is_callable( $method_to_call ) ) { | |
| 113 | + call_user_func( $method_to_call ); | |
| 114 | + } | |
| 109 | 115 | } |
| 110 | 116 | } |
| 111 | 117 | |
| 118 | + /** | |
| 119 | + * Fires after Polylang has been upgraded and before the new version is saved in options. | |
| 120 | + * | |
| 121 | + * @since 3.7 | |
| 122 | + */ | |
| 123 | + do_action( 'pll_upgrade' ); | |
| 124 | + | |
| 112 | 125 | $this->options['previous_version'] = $this->options['version']; // Remember the previous version of Polylang since v1.7.7 |
| 113 | 126 | $this->options['version'] = POLYLANG_VERSION; |
| 114 | - update_option( 'polylang', $this->options ); | |
| 115 | 127 | } |
| 116 | 128 | |
| 117 | 129 | /** |
| 118 | 130 | * Upgrades if the previous version is < 2.0.8 |
| @@ -186,20 +198,165 @@ | ||
| 186 | 198 | PLL_Admin_Notices::dismiss( 'wizard' ); |
| 187 | 199 | } |
| 188 | 200 | |
| 189 | 201 | /** |
| 190 | - * Upgrades if the previous version is < 2.8.1 | |
| 202 | + * Upgrades if the previous version is < 3.4.0. | |
| 191 | 203 | * |
| 192 | - * Deletes language cache due to: | |
| 193 | - * - 'redirect_lang' option removed for subdomains and multiple domains in 2.2 | |
| 194 | - * - W3C and Facebook locales added to PLL_Language objects in 2.3 | |
| 195 | - * - flags moved to a different directory in Polylang Pro 2.8 | |
| 196 | - * - bug of flags url returning html fixed in 2.8.1 | |
| 204 | + * @since 3.4 | |
| 197 | 205 | * |
| 198 | - * @since 2.8.1 | |
| 206 | + * @return void | |
| 207 | + */ | |
| 208 | + protected function upgrade_3_4() { | |
| 209 | + $this->migrate_locale_fallback_to_language_description(); | |
| 210 | + | |
| 211 | + $this->migrate_strings_translations(); | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Upgrades if the previous version is < 3.7. | |
| 216 | + * Hides the "The language is set from content" option if it isn't the one selected. | |
| 217 | + * Cleans up strings translations so we don't store translations duplicated from the source. | |
| 199 | 218 | * |
| 219 | + * @since 3.7 | |
| 220 | + * | |
| 200 | 221 | * @return void |
| 201 | 222 | */ |
| 202 | - protected function upgrade_2_8_1() { | |
| 203 | - delete_transient( 'pll_languages_list' ); | |
| 223 | + protected function upgrade_3_7() { | |
| 224 | + $this->allow_to_hide_language_from_content(); | |
| 225 | + $this->empty_duplicated_strings_translations(); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Moves strings translations from post meta to term meta _pll_strings_translations. | |
| 230 | + * | |
| 231 | + * @since 3.4 | |
| 232 | + * | |
| 233 | + * @return void | |
| 234 | + */ | |
| 235 | + protected function migrate_strings_translations() { | |
| 236 | + $posts = get_posts( | |
| 237 | + array( | |
| 238 | + 'post_type' => 'polylang_mo', | |
| 239 | + 'post_status' => 'any', | |
| 240 | + 'numberposts' => -1, | |
| 241 | + 'nopaging' => true, | |
| 242 | + ) | |
| 243 | + ); | |
| 244 | + | |
| 245 | + if ( ! is_array( $posts ) ) { | |
| 246 | + return; | |
| 247 | + } | |
| 248 | + | |
| 249 | + foreach ( $posts as $post ) { | |
| 250 | + $meta = get_post_meta( $post->ID, '_pll_strings_translations', true ); | |
| 251 | + | |
| 252 | + $term_id = (int) substr( $post->post_title, 12 ); | |
| 253 | + | |
| 254 | + // Do not delete post metas in case a user needs to rollback to Polylang < 3.4. | |
| 255 | + | |
| 256 | + if ( empty( $meta ) || ! is_array( $meta ) ) { | |
| 257 | + continue; | |
| 258 | + } | |
| 259 | + | |
| 260 | + update_term_meta( $term_id, '_pll_strings_translations', wp_slash( $meta ) ); | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Migrate locale fallback to language term description. | |
| 266 | + * | |
| 267 | + * @since 3.4 | |
| 268 | + * | |
| 269 | + * @return void | |
| 270 | + */ | |
| 271 | + protected function migrate_locale_fallback_to_language_description() { | |
| 272 | + // Migrate locale fallbacks from term metas to language term description. | |
| 273 | + $terms = get_terms( | |
| 274 | + array( | |
| 275 | + 'taxonomy' => 'language', | |
| 276 | + 'hide_empty' => false, | |
| 277 | + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | |
| 278 | + array( | |
| 279 | + 'key' => 'fallback', | |
| 280 | + 'compare_key' => 'EXISTS', | |
| 281 | + ), | |
| 282 | + ), | |
| 283 | + ) | |
| 284 | + ); | |
| 285 | + | |
| 286 | + if ( ! is_array( $terms ) ) { | |
| 287 | + return; | |
| 288 | + } | |
| 289 | + | |
| 290 | + foreach ( $terms as $term ) { | |
| 291 | + $fallbacks = get_term_meta( $term->term_id, 'fallback', true ); | |
| 292 | + | |
| 293 | + delete_term_meta( $term->term_id, 'fallback' ); | |
| 294 | + | |
| 295 | + if ( empty( $fallbacks ) || ! is_array( $fallbacks ) ) { | |
| 296 | + // Empty or invalid value, should not happen. | |
| 297 | + continue; | |
| 298 | + } | |
| 299 | + | |
| 300 | + $description = maybe_unserialize( $term->description ); | |
| 301 | + $description = is_array( $description ) ? $description : array(); | |
| 302 | + | |
| 303 | + $description['fallbacks'] = $fallbacks; | |
| 304 | + /** @var string */ | |
| 305 | + $description = maybe_serialize( $description ); | |
| 306 | + | |
| 307 | + wp_update_term( $term->term_id, 'language', array( 'description' => $description ) ); | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Hides the language from content option if it is not the one selected. | |
| 313 | + * | |
| 314 | + * @since 3.7 | |
| 315 | + * | |
| 316 | + * @return void | |
| 317 | + */ | |
| 318 | + private function allow_to_hide_language_from_content() { | |
| 319 | + update_option( | |
| 320 | + 'pll_language_from_content_available', | |
| 321 | + 0 === $this->options['force_lang'] ? 'yes' : 'no' | |
| 322 | + ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | + * Cleans up strings translations so we don't store translations duplicated from the source. | |
| 327 | + * | |
| 328 | + * @since 3.7 | |
| 329 | + * | |
| 330 | + * @return void | |
| 331 | + */ | |
| 332 | + private function empty_duplicated_strings_translations() { | |
| 333 | + $terms = get_terms( | |
| 334 | + array( | |
| 335 | + 'taxonomy' => 'language', | |
| 336 | + 'hide_empty' => false, | |
| 337 | + ) | |
| 338 | + ); | |
| 339 | + | |
| 340 | + if ( ! is_array( $terms ) ) { | |
| 341 | + return; | |
| 342 | + } | |
| 343 | + | |
| 344 | + foreach ( $terms as $term ) { | |
| 345 | + $strings = get_term_meta( $term->term_id, '_pll_strings_translations', true ); | |
| 346 | + | |
| 347 | + if ( empty( $strings ) || ! is_array( $strings ) ) { | |
| 348 | + continue; | |
| 349 | + } | |
| 350 | + | |
| 351 | + foreach ( $strings as $i => $tuple ) { | |
| 352 | + if ( $tuple[0] !== $tuple[1] ) { | |
| 353 | + continue; | |
| 354 | + } | |
| 355 | + | |
| 356 | + $strings[ $i ][1] = ''; | |
| 357 | + } | |
| 358 | + | |
| 359 | + update_term_meta( $term->term_id, '_pll_strings_translations', $strings ); | |
| 360 | + } | |
| 204 | 361 | } |
| 205 | 362 | } |