Skip to content

Commit 643353e

Browse files
ouunstklcode
andauthored
update deprecated WPMS hooks (#246)
Conditionally use "wp_initialize_site" and "wp_delete_site" on sites running on WP 5.1 and above in favor of the deprecated "delete_blog" and "wpmu_new_blog" hooks. Co-authored-by: Stefan Kalscheuer <[email protected]>
1 parent 34fad2f commit 643353e

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec
66
Requires PHP 5.6 and WordPress 4.7 or above
77

88
* Enhance: adjust styling for setup instructions (#215, props timse201)
9+
* Enhance: update hooks for Multisite initialization in WordPress 5.1 and above (#246, props ouun)
910

1011

1112
## 2.3.2

inc/class-cachify.php

+20-15
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@ public function __construct() {
132132

133133
if ( is_admin() ) {
134134
/* Backend */
135-
add_action( 'wpmu_new_blog', array( __CLASS__, 'install_later' ) );
136-
137-
add_action( 'delete_blog', array( __CLASS__, 'uninstall_later' ) );
135+
if ( version_compare( get_bloginfo( 'version' ), '5.1', '<' ) ) {
136+
// The following hooks are deprecated since WP 5.1 (#246).
137+
add_action( 'wpmu_new_blog', array( __CLASS__, 'install_later' ) );
138+
add_action( 'delete_blog', array( __CLASS__, 'uninstall_later' ) );
139+
} else {
140+
add_action( 'wp_initialize_site', array( __CLASS__, 'install_later' ) );
141+
add_action( 'wp_delete_site', array( __CLASS__, 'uninstall_later' ) );
142+
}
138143

139144
add_action( 'admin_init', array( __CLASS__, 'register_textdomain' ) );
140145

@@ -210,21 +215,21 @@ public static function on_activation() {
210215
}
211216

212217
/**
213-
* Plugin installation on new MU blog.
218+
* Plugin installation on new WPMS site.
214219
*
215-
* @since 1.0
216-
* @change 1.0
220+
* @since 1.0
221+
* @since 2.4 supports WP_Site argument
217222
*
218-
* @param integer $id Blog ID.
223+
* @param int|WP_Site $new_site New site ID or object.
219224
*/
220-
public static function install_later( $id ) {
225+
public static function install_later( $new_site ) {
221226
/* No network plugin */
222227
if ( ! is_plugin_active_for_network( CACHIFY_BASE ) ) {
223228
return;
224229
}
225230

226231
/* Switch to blog */
227-
switch_to_blog( $id );
232+
switch_to_blog( is_int( $new_site ) ? $new_site : $new_site->blog_id );
228233

229234
/* Install */
230235
self::_install_backend();
@@ -281,21 +286,21 @@ public static function on_uninstall() {
281286
}
282287

283288
/**
284-
* Uninstalling of the plugin for MU and network.
289+
* Uninstalling of the plugin for WPMS site.
285290
*
286-
* @since 1.0
287-
* @change 1.0
291+
* @since 1.0
292+
* @since 2.4 supports WP_Site argument
288293
*
289-
* @param integer $id Blog ID.
294+
* @param int|WP_Site $old_site Old site ID or object.
290295
*/
291-
public static function uninstall_later( $id ) {
296+
public static function uninstall_later( $old_site ) {
292297
/* No network plugin */
293298
if ( ! is_plugin_active_for_network( CACHIFY_BASE ) ) {
294299
return;
295300
}
296301

297302
/* Switch to blog */
298-
switch_to_blog( $id );
303+
switch_to_blog( is_int( $old_site ) ? $old_site : $old_site->blog_id );
299304

300305
/* Install */
301306
self::_uninstall_backend();

0 commit comments

Comments
 (0)