| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages Polylang upgrades |
| 8 |
* |
| 9 |
* @since 1.2 |
| 10 |
*/ |
| 11 |
class PLL_Upgrade { |
| 12 |
/** |
| 13 |
* Stores the plugin options. |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
public $options; |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
* |
| 22 |
* @since 1.2 |
| 23 |
* |
| 24 |
* @param array $options Polylang options |
| 25 |
*/ |
| 26 |
public function __construct( &$options ) { |
| 27 |
$this->options = &$options; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Check if upgrade is possible otherwise die to avoid activation |
| 32 |
* |
| 33 |
* @since 1.2 |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function can_activate() { |
| 38 |
if ( ! $this->can_upgrade() ) { |
| 39 |
ob_start(); |
| 40 |
$this->admin_notices(); // FIXME the error message is displayed two times |
| 41 |
die( ob_get_contents() ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Upgrades if possible otherwise returns false to stop Polylang loading |
| 47 |
* |
| 48 |
* @since 1.2 |
| 49 |
* |
| 50 |
* @return bool true if upgrade is possible, false otherwise |
| 51 |
*/ |
| 52 |
public function upgrade() { |
| 53 |
if ( ! $this->can_upgrade() ) { |
| 54 |
add_action( 'all_admin_notices', array( $this, 'admin_notices' ) ); |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
add_action( 'admin_init', array( $this, '_upgrade' ) ); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Check if we the previous version is not too old |
| 65 |
* Upgrades if OK |
| 66 |
* /!\ never start any upgrade before admin_init as it is likely to conflict with some other plugins |
| 67 |
* |
| 68 |
* @since 1.2 |
| 69 |
* |
| 70 |
* @return bool true if upgrade is possible, false otherwise |
| 71 |
*/ |
| 72 |
public function can_upgrade() { |
| 73 |
// Don't manage upgrade from version < 1.8 |
| 74 |
return version_compare( $this->options['version'], '1.8', '>=' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Displays a notice when ugrading from a too old version |
| 79 |
* |
| 80 |
* @since 1.0 |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function admin_notices() { |
| 85 |
load_plugin_textdomain( 'polylang' ); |
| 86 |
printf( |
| 87 |
'<div class="error"><p>%s</p><p>%s</p></div>', |
| 88 |
esc_html__( 'Polylang has been deactivated because you upgraded from a too old version.', 'polylang' ), |
| 89 |
sprintf( |
| 90 |
/* translators: %1$s and %2$s are Polylang version numbers */ |
| 91 |
esc_html__( 'Before upgrading to %2$s, please upgrade to %1$s.', 'polylang' ), |
| 92 |
'<strong>2.9</strong>', |
| 93 |
POLYLANG_VERSION // phpcs:ignore WordPress.Security.EscapeOutput |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Upgrades the plugin depending on the previous version |
| 100 |
* |
| 101 |
* @since 1.2 |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function _upgrade() { |
| 106 |
foreach ( array( '2.0.8', '2.1', '2.7', '2.8.1' ) as $version ) { |
| 107 |
if ( version_compare( $this->options['version'], $version, '<' ) ) { |
| 108 |
$method_to_call = array( $this, 'upgrade_' . str_replace( '.', '_', $version ) ); |
| 109 |
if ( is_callable( $method_to_call ) ) { |
| 110 |
call_user_func( $method_to_call ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$this->options['previous_version'] = $this->options['version']; // Remember the previous version of Polylang since v1.7.7 |
| 116 |
$this->options['version'] = POLYLANG_VERSION; |
| 117 |
update_option( 'polylang', $this->options ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Upgrades if the previous version is < 2.0.8 |
| 122 |
* Changes the user meta 'user_lang' to 'locale' to match WP 4.7 choice |
| 123 |
* |
| 124 |
* @since 2.0.8 |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected function upgrade_2_0_8() { |
| 129 |
global $wpdb; |
| 130 |
$wpdb->update( $wpdb->usermeta, array( 'meta_key' => 'locale' ), array( 'meta_key' => 'user_lang' ) ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Upgrades if the previous version is < 2.1. |
| 135 |
* Moves strings translations from polylang_mo post_content to post meta _pll_strings_translations. |
| 136 |
* |
| 137 |
* @since 2.1 |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
protected function upgrade_2_1() { |
| 142 |
$posts = get_posts( |
| 143 |
array( |
| 144 |
'post_type' => 'polylang_mo', |
| 145 |
'post_status' => 'any', |
| 146 |
'numberposts' => -1, |
| 147 |
'nopaging' => true, |
| 148 |
) |
| 149 |
); |
| 150 |
|
| 151 |
if ( is_array( $posts ) ) { |
| 152 |
foreach ( $posts as $post ) { |
| 153 |
$meta = get_post_meta( $post->ID, '_pll_strings_translations', true ); |
| 154 |
|
| 155 |
if ( empty( $meta ) ) { |
| 156 |
$strings = maybe_unserialize( $post->post_content ); |
| 157 |
if ( is_array( $strings ) ) { |
| 158 |
update_post_meta( $post->ID, '_pll_strings_translations', $strings ); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Upgrades if the previous version is < 2.7 |
| 167 |
* Replace numeric keys by hashes in WPML registered strings |
| 168 |
* Dismiss the wizard notice for existing sites |
| 169 |
* |
| 170 |
* @since 2.7 |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
protected function upgrade_2_7() { |
| 175 |
$strings = get_option( 'polylang_wpml_strings' ); |
| 176 |
if ( is_array( $strings ) ) { |
| 177 |
$new_strings = array(); |
| 178 |
|
| 179 |
foreach ( $strings as $string ) { |
| 180 |
$context = $string['context']; |
| 181 |
$name = $string['name']; |
| 182 |
|
| 183 |
$key = md5( "$context | $name" ); |
| 184 |
$new_strings[ $key ] = $string; |
| 185 |
} |
| 186 |
update_option( 'polylang_wpml_strings', $new_strings ); |
| 187 |
} |
| 188 |
|
| 189 |
PLL_Admin_Notices::dismiss( 'wizard' ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Upgrades if the previous version is < 2.8.1 |
| 194 |
* |
| 195 |
* Deletes language cache due to: |
| 196 |
* - 'redirect_lang' option removed for subdomains and multiple domains in 2.2 |
| 197 |
* - W3C and Facebook locales added to PLL_Language objects in 2.3 |
| 198 |
* - flags moved to a different directory in Polylang Pro 2.8 |
| 199 |
* - bug of flags url returning html fixed in 2.8.1 |
| 200 |
* |
| 201 |
* @since 2.8.1 |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
protected function upgrade_2_8_1() { |
| 206 |
delete_transient( 'pll_languages_list' ); |
| 207 |
} |
| 208 |
} |
| 209 |
|