PluginProbe
Polylang / 3.1.2
Polylang v3.1.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / install / upgrade.php

upgrade.php in Polylang 3.1.2, at install/upgrade.php

206 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 call_user_func( array( $this, 'upgrade_' . str_replace( '.', '_', $version ) ) );
109 }
110 }
111
112 $this->options['previous_version'] = $this->options['version']; // Remember the previous version of Polylang since v1.7.7
113 $this->options['version'] = POLYLANG_VERSION;
114 update_option( 'polylang', $this->options );
115 }
116
117 /**
118 * Upgrades if the previous version is < 2.0.8
119 * Changes the user meta 'user_lang' to 'locale' to match WP 4.7 choice
120 *
121 * @since 2.0.8
122 *
123 * @return void
124 */
125 protected function upgrade_2_0_8() {
126 global $wpdb;
127 $wpdb->update( $wpdb->usermeta, array( 'meta_key' => 'locale' ), array( 'meta_key' => 'user_lang' ) );
128 }
129
130 /**
131 * Upgrades if the previous version is < 2.1.
132 * Moves strings translations from polylang_mo post_content to post meta _pll_strings_translations.
133 *
134 * @since 2.1
135 *
136 * @return void
137 */
138 protected function upgrade_2_1() {
139 $posts = get_posts(
140 array(
141 'post_type' => 'polylang_mo',
142 'post_status' => 'any',
143 'numberposts' => -1,
144 'nopaging' => true,
145 )
146 );
147
148 if ( is_array( $posts ) ) {
149 foreach ( $posts as $post ) {
150 $meta = get_post_meta( $post->ID, '_pll_strings_translations', true );
151
152 if ( empty( $meta ) ) {
153 $strings = maybe_unserialize( $post->post_content );
154 if ( is_array( $strings ) ) {
155 update_post_meta( $post->ID, '_pll_strings_translations', $strings );
156 }
157 }
158 }
159 }
160 }
161
162 /**
163 * Upgrades if the previous version is < 2.7
164 * Replace numeric keys by hashes in WPML registered strings
165 * Dismiss the wizard notice for existing sites
166 *
167 * @since 2.7
168 *
169 * @return void
170 */
171 protected function upgrade_2_7() {
172 $strings = get_option( 'polylang_wpml_strings' );
173 if ( is_array( $strings ) ) {
174 $new_strings = array();
175
176 foreach ( $strings as $string ) {
177 $context = $string['context'];
178 $name = $string['name'];
179
180 $key = md5( "$context | $name" );
181 $new_strings[ $key ] = $string;
182 }
183 update_option( 'polylang_wpml_strings', $new_strings );
184 }
185
186 PLL_Admin_Notices::dismiss( 'wizard' );
187 }
188
189 /**
190 * Upgrades if the previous version is < 2.8.1
191 *
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
197 *
198 * @since 2.8.1
199 *
200 * @return void
201 */
202 protected function upgrade_2_8_1() {
203 delete_transient( 'pll_languages_list' );
204 }
205 }
206