| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { |
| 14 |
|
| 15 |
die( 'Do. Or do not. There is no try.' ); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'WpssoRegister' ) ) { |
| 19 |
|
| 20 |
class WpssoRegister { |
| 21 |
|
| 22 |
private $p; // Wpsso class object. |
| 23 |
|
| 24 |
public function __construct( &$plugin ) { |
| 25 |
|
| 26 |
$this->p =& $plugin; |
| 27 |
|
| 28 |
register_activation_hook( WPSSO_FILEPATH, array( $this, 'network_activate' ) ); |
| 29 |
register_deactivation_hook( WPSSO_FILEPATH, array( $this, 'network_deactivate' ) ); |
| 30 |
|
| 31 |
if ( is_multisite() ) { |
| 32 |
|
| 33 |
add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog' ), 10, 6 ); |
| 34 |
add_action( 'wpmu_activate_blog', array( $this, 'wpmu_activate_blog' ), 10, 5 ); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/* |
| 39 |
* Fires immediately after a new site is created. |
| 40 |
*/ |
| 41 |
public function wpmu_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { |
| 42 |
|
| 43 |
switch_to_blog( $blog_id ); |
| 44 |
|
| 45 |
$this->activate_plugin(); |
| 46 |
|
| 47 |
restore_current_blog(); |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* Fires immediately after a site is activated (not called when users and sites are created by a Super Admin). |
| 52 |
*/ |
| 53 |
public function wpmu_activate_blog( $blog_id, $user_id, $password, $signup_title, $meta ) { |
| 54 |
|
| 55 |
switch_to_blog( $blog_id ); |
| 56 |
|
| 57 |
$this->activate_plugin(); |
| 58 |
|
| 59 |
restore_current_blog(); |
| 60 |
} |
| 61 |
|
| 62 |
public function network_activate( $sitewide ) { |
| 63 |
|
| 64 |
self::do_multisite( $sitewide, array( $this, 'activate_plugin' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
public function network_deactivate( $sitewide ) { |
| 68 |
|
| 69 |
self::do_multisite( $sitewide, array( $this, 'deactivate_plugin' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* uninstall.php defines constants before calling network_uninstall(). |
| 74 |
*/ |
| 75 |
public static function network_uninstall() { |
| 76 |
|
| 77 |
$sitewide = true; |
| 78 |
|
| 79 |
/* |
| 80 |
* Uninstall from the individual blogs first. |
| 81 |
*/ |
| 82 |
self::do_multisite( $sitewide, array( __CLASS__, 'uninstall_plugin' ) ); |
| 83 |
|
| 84 |
$opts = get_site_option( WPSSO_SITE_OPTIONS_NAME, array() ); |
| 85 |
|
| 86 |
if ( ! empty( $opts[ 'plugin_clean_on_uninstall' ] ) ) { |
| 87 |
|
| 88 |
delete_site_option( WPSSO_SITE_OPTIONS_NAME ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* See WpssoAdmin->activated_plugin(). |
| 94 |
* See WpssoRegister->network_activate() |
| 95 |
* See WpssoRegister->network_deactivate() |
| 96 |
* See WpssoRegister->network_uninstall() |
| 97 |
*/ |
| 98 |
public static function do_multisite( $sitewide, $method, $args = array() ) { |
| 99 |
|
| 100 |
if ( is_multisite() && $sitewide ) { |
| 101 |
|
| 102 |
global $wpdb; |
| 103 |
|
| 104 |
$query = 'SELECT blog_id FROM ' . $wpdb->blogs; |
| 105 |
|
| 106 |
$blog_ids = $wpdb->get_col( $query ); |
| 107 |
|
| 108 |
foreach ( $blog_ids as $blog_id ) { |
| 109 |
|
| 110 |
switch_to_blog( $blog_id ); |
| 111 |
|
| 112 |
call_user_func_array( $method, array( $args ) ); |
| 113 |
} |
| 114 |
|
| 115 |
restore_current_blog(); |
| 116 |
|
| 117 |
} else { |
| 118 |
|
| 119 |
call_user_func_array( $method, array( $args ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private function activate_plugin() { |
| 124 |
|
| 125 |
$this->p->init_textdomain(); |
| 126 |
|
| 127 |
$this->check_required( WpssoConfig::$cf ); |
| 128 |
|
| 129 |
$this->p->set_config( $activate = true ); // Force a refresh of the plugin config. |
| 130 |
$this->p->set_options( $activate = true ); // Read / create options and site_options. |
| 131 |
$this->p->set_objects( $activate = true ); // Load all the class objects. |
| 132 |
|
| 133 |
/* |
| 134 |
* Returns the event timestamp, or false if the event has not been registered. |
| 135 |
*/ |
| 136 |
$new_install = WpssoUtilReg::get_ext_event_time( 'wpsso', 'install' ) ? false : true; |
| 137 |
|
| 138 |
/* |
| 139 |
* Add the "person" role for all WpssoUser::get_public_ids(). |
| 140 |
*/ |
| 141 |
if ( $new_install ) { |
| 142 |
|
| 143 |
$this->p->user->schedule_add_person_role(); |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* Register plugin install, activation, update times. |
| 148 |
*/ |
| 149 |
$version = WpssoConfig::get_version(); |
| 150 |
|
| 151 |
WpssoUtilReg::update_ext_version( 'wpsso', $version ); |
| 152 |
|
| 153 |
/* |
| 154 |
* Refresh cache on activate. |
| 155 |
*/ |
| 156 |
$user_id = get_current_user_id(); |
| 157 |
|
| 158 |
$this->p->util->cache->schedule_refresh( $user_id ); |
| 159 |
} |
| 160 |
|
| 161 |
private function deactivate_plugin() { |
| 162 |
|
| 163 |
$this->reset_admin_checks(); |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* See WpssoAdmin->activated_plugin(). |
| 168 |
* See WpssoAdmin->after_switch_theme(). |
| 169 |
* See WpssoAdmin->upgrader_process_complete(). |
| 170 |
*/ |
| 171 |
public function reset_admin_checks() { |
| 172 |
|
| 173 |
delete_option( WPSSO_POST_CHECK_COUNT_NAME ); |
| 174 |
delete_option( WPSSO_TMPL_HEAD_CHECK_NAME ); |
| 175 |
delete_option( WPSSO_WP_CONFIG_CHECK_NAME ); |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* uninstall.php defines constants before calling network_uninstall(), which calls do_multisite(), and then calls |
| 180 |
* uninstall_plugin(). |
| 181 |
*/ |
| 182 |
private static function uninstall_plugin() { |
| 183 |
|
| 184 |
$blog_id = get_current_blog_id(); |
| 185 |
$opts = get_option( WPSSO_OPTIONS_NAME, array() ); |
| 186 |
|
| 187 |
if ( $dh = @opendir( WPSSO_CACHE_DIR ) ) { |
| 188 |
|
| 189 |
while ( $file_name = @readdir( $dh ) ) { |
| 190 |
|
| 191 |
$cache_file = WPSSO_CACHE_DIR . $file_name; |
| 192 |
|
| 193 |
if ( ! preg_match( '/^(\..*|index\.php)$/', $file_name ) && is_file( $cache_file ) ) { |
| 194 |
|
| 195 |
@unlink( $cache_file ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
closedir( $dh ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( ! empty( $opts[ 'plugin_clean_on_uninstall' ] ) ) { |
| 203 |
|
| 204 |
delete_option( WPSSO_REG_TS_NAME ); |
| 205 |
delete_option( WPSSO_OPTIONS_NAME ); |
| 206 |
|
| 207 |
$col_meta_keys = WpssoAbstractWpMeta::get_column_meta_keys(); |
| 208 |
|
| 209 |
foreach ( array( 'comment', 'post', 'term', 'user' ) as $obj_name ) { |
| 210 |
|
| 211 |
foreach ( $col_meta_keys as $col_key => $meta_key ) { |
| 212 |
|
| 213 |
delete_metadata( $obj_name, $obj_id = null, $meta_key, $meta_value = null, $delete_all = true ); |
| 214 |
} |
| 215 |
|
| 216 |
foreach ( array( |
| 217 |
'WPSSO_META_NAME' => '_wpsso_meta', |
| 218 |
'WPSSO_META_ATTACHED_NAME' => '_wpsso_meta_attached', |
| 219 |
'WPSSO_PREF_NAME' => '_wpsso_pref', |
| 220 |
'WPSSORAR_META_ALLOW_RATINGS' => '_wpsso_allow_ratings', |
| 221 |
'WPSSORAR_META_AVERAGE_RATING' => '_wpsso_average_rating', |
| 222 |
'WPSSORAR_META_RATING_COUNTS' => '_wpsso_rating_counts', |
| 223 |
'WPSSORAR_META_REVIEW_COUNT' => '_wpsso_review_count', |
| 224 |
) as $const_name => $def_meta_key ) { |
| 225 |
|
| 226 |
$meta_key = SucomUtil::get_const( $const_name, $def_meta_key ); |
| 227 |
|
| 228 |
delete_metadata( $obj_name, $obj_id = null, $meta_key, $meta_value = null, $delete_all = true ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
while ( $result = SucomUtilWP::get_users_ids( $blog_id, $role = '', $limit = 1000 ) ) { // Get a maximum of 1000 user IDs at a time. |
| 233 |
|
| 234 |
foreach ( $result as $user_id ) { |
| 235 |
|
| 236 |
delete_user_option( $user_id, WPSSO_DISMISS_NAME, $global = false ); |
| 237 |
delete_user_option( $user_id, WPSSO_NOTICES_NAME, $global = false ); |
| 238 |
|
| 239 |
WpssoUser::delete_metabox_prefs( $user_id ); |
| 240 |
WpssoUser::remove_role_by_id( $user_id, $role = 'person' ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
remove_role( 'person' ); |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* Delete plugin transients. |
| 249 |
*/ |
| 250 |
global $wpdb; |
| 251 |
|
| 252 |
$prefix = '_transient_'; |
| 253 |
$query = 'SELECT option_name FROM ' . $wpdb->options . ' WHERE option_name LIKE \'' . $prefix . 'wpsso_%\';'; |
| 254 |
$result = $wpdb->get_col( $query ); |
| 255 |
|
| 256 |
foreach( $result as $option_name ) { |
| 257 |
|
| 258 |
$transient_name = str_replace( $prefix, '', $option_name ); |
| 259 |
|
| 260 |
if ( ! empty( $transient_name ) ) { |
| 261 |
|
| 262 |
delete_transient( $transient_name ); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
private static function check_required( $cf ) { |
| 268 |
|
| 269 |
$plugin_name = $cf[ 'plugin' ][ 'wpsso' ][ 'name' ]; |
| 270 |
$plugin_short = $cf[ 'plugin' ][ 'wpsso' ][ 'short' ]; |
| 271 |
$plugin_version = $cf[ 'plugin' ][ 'wpsso' ][ 'version' ]; |
| 272 |
|
| 273 |
foreach ( array( 'wp', 'php' ) as $key ) { |
| 274 |
|
| 275 |
if ( empty( $cf[ $key ][ 'min_version' ] ) ) { |
| 276 |
|
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
switch ( $key ) { |
| 281 |
|
| 282 |
case 'wp': |
| 283 |
|
| 284 |
global $wp_version; |
| 285 |
|
| 286 |
$app_version = $wp_version; |
| 287 |
|
| 288 |
break; |
| 289 |
|
| 290 |
case 'php': |
| 291 |
|
| 292 |
$app_version = phpversion(); |
| 293 |
|
| 294 |
break; |
| 295 |
} |
| 296 |
|
| 297 |
$app_label = $cf[ $key ][ 'label' ]; |
| 298 |
$min_version = $cf[ $key ][ 'min_version' ]; |
| 299 |
$version_url = $cf[ $key ][ 'version_url' ]; |
| 300 |
|
| 301 |
if ( version_compare( $app_version, $min_version, '>=' ) ) { |
| 302 |
|
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
if ( ! function_exists( 'deactivate_plugins' ) ) { |
| 307 |
|
| 308 |
require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/plugin.php'; |
| 309 |
} |
| 310 |
|
| 311 |
deactivate_plugins( WPSSO_PLUGINBASE, $silent = true ); |
| 312 |
|
| 313 |
if ( method_exists( 'SucomUtil', 'safe_error_log' ) ) { |
| 314 |
|
| 315 |
$error_pre = sprintf( __( '%s warning:', 'wpsso' ), __METHOD__ ); |
| 316 |
|
| 317 |
$error_msg = sprintf( __( '%1$s requires %2$s version %3$s or higher and has been deactivated.', |
| 318 |
'wpsso' ), $plugin_name, $app_label, $min_version ); |
| 319 |
|
| 320 |
SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg ); |
| 321 |
} |
| 322 |
|
| 323 |
wp_die( |
| 324 |
'<p>' . sprintf( __( 'You are using %1$s version %2$s - <a href="%3$s">this %1$s version is outdated, unsupported, possibly insecure</a>, and may lack important updates and features.', 'wpsso' ), $app_label, $app_version, $version_url ) . '</p>' . |
| 325 |
'<p>' . sprintf( __( '%1$s requires %2$s version %3$s or higher and has been deactivated.', |
| 326 |
'wpsso' ), $plugin_name, $app_label, $min_version ) . '</p>' . |
| 327 |
'<p>' . sprintf( __( 'Please upgrade %1$s before trying to re-activate the %2$s plugin.', |
| 328 |
'wpsso' ), $app_label, $plugin_name ) . '</p>' |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
public static function register_taxonomy_page_tag() { |
| 334 |
|
| 335 |
$labels = array( |
| 336 |
'name' => __( 'Page Tags', 'wpsso' ), |
| 337 |
'singular_name' => __( 'Page Tag', 'wpsso' ), |
| 338 |
'menu_name' => _x( 'Page Tags', 'admin menu name', 'wpsso' ), |
| 339 |
'all_items' => __( 'All Page Tags', 'wpsso' ), |
| 340 |
'edit_item' => __( 'Edit Page Tag', 'wpsso' ), |
| 341 |
'view_item' => __( 'View Page Tag', 'wpsso' ), |
| 342 |
'update_item' => __( 'Update Page Tag', 'wpsso' ), |
| 343 |
'add_new_item' => __( 'Add New Page Tag', 'wpsso' ), |
| 344 |
'new_item_name' => __( 'New Page Tag Name', 'wpsso' ), |
| 345 |
'parent_item' => __( 'Parent Page Tag', 'wpsso' ), |
| 346 |
'parent_item_colon' => __( 'Parent Page Tag:', 'wpsso' ), |
| 347 |
'search_items' => __( 'Search Page Tags', 'wpsso' ), |
| 348 |
'popular_items' => __( 'Popular Page Tags', 'wpsso' ), |
| 349 |
'separate_items_with_commas' => __( 'Separate page tags with commas', 'wpsso' ), |
| 350 |
'add_or_remove_items' => __( 'Add or remove page tags', 'wpsso' ), |
| 351 |
'choose_from_most_used' => __( 'Choose from the most used', 'wpsso' ), |
| 352 |
'not_found' => __( 'No page tags found.', 'wpsso' ), |
| 353 |
'back_to_items' => __( '← Back to page tags', 'wpsso' ), |
| 354 |
); |
| 355 |
|
| 356 |
$args = array( |
| 357 |
'label' => _x( 'Page Tags', 'taxonomy label', 'wpsso' ), |
| 358 |
'labels' => $labels, |
| 359 |
'public' => false, |
| 360 |
'publicly_queryable' => false, |
| 361 |
'show_ui' => true, |
| 362 |
'show_in_menu' => true, |
| 363 |
'show_in_nav_menus' => true, |
| 364 |
'show_admin_column' => true, |
| 365 |
'show_in_quick_edit' => true, |
| 366 |
'show_in_rest' => true, // Show this taxonomy in the block editor. |
| 367 |
'show_tagcloud' => false, |
| 368 |
'description' => _x( 'Tags for the page post type.', 'taxonomy description', 'wpsso' ), |
| 369 |
'hierarchical' => false, |
| 370 |
); |
| 371 |
|
| 372 |
register_taxonomy( WPSSO_PAGE_TAG_TAXONOMY, array( 'page' ), $args ); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|