: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Class for Admin Upgrades
* @copyright Copyright (c) 2023, Code Atlantic LLC
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
class PUM_Admin_Upgrades {
* @var PUM_Admin_Upgrades The one true PUM_Admin_Upgrades
private static $instance;
public $upgrade_args = [];
public $doing_upgrades = false;
public $required_cap = 'manage_options';
public $current_routine = null;
public $next_routine = null;
* Initialize the actions needed to process upgrades.
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof PUM_Admin_Upgrades ) ) {
self::$instance = new PUM_Admin_Upgrades();
* Initialize the actions needed to process upgrades.
$this->update_plugin_version();
$this->required_cap = apply_filters( 'pum_upgrade_required_cap', 'manage_options' );
// bail if this plugin data doesn't need updating
if ( pum_get_db_ver() >= Popup_Maker::$DB_VER ) {
add_action( 'admin_menu', [ $this, 'register_pages' ] );
add_action( 'network_admin_menu', [ $this, 'register_pages' ] );
add_action( 'admin_init', [ $this, 'process_upgrade_args' ] );
add_action( 'wp_ajax_pum_trigger_upgrades', [ $this, 'trigger_upgrades' ] );
add_action( 'admin_notices', [ $this, 'show_upgrade_notices' ] );
public function update_plugin_version() {
$current_ver = get_option( 'pum_ver', false );
$deprecated_ver = get_site_option( 'popmake_version', false );
$current_ver = $deprecated_ver ? $deprecated_ver : Popup_Maker::$VER;
add_option( 'pum_ver', Popup_Maker::$VER );
if ( version_compare( $current_ver, Popup_Maker::$VER, '<' ) ) {
// Save Upgraded From option
update_option( 'pum_ver_upgraded_from', $current_ver );
update_option( 'pum_ver', Popup_Maker::$VER );
* Registers the pum-upgrades admin page.
public function register_pages() {
global $pum_upgrades_page;
if ( function_exists( 'is_network_admin' ) && is_network_admin() ) {
__( 'Popup Maker', 'popup-maker' ),
__( 'Popup Maker', 'popup-maker' ),
'manage_network_plugins',
$this->page = add_submenu_page(
__( 'Popup Maker Upgrades', 'popup-maker' ),
__( 'Popup Maker Upgrades', 'popup-maker' ),
[ $this, 'upgrades_screen' ]
$pum_upgrades_page = $this->page;
public function process_upgrade_args() {
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'pum_trigger_upgrades' === $_REQUEST['action'] ) && 'pum-upgrades' !== $page ) {
$this->doing_upgrades = true;
$action = isset( $_REQUEST['pum-upgrade'] ) ? sanitize_text_field( $_REQUEST['pum-upgrade'] ) : $this->get_pum_db_ver() + 1;
$step = isset( $_REQUEST['step'] ) ? absint( $_REQUEST['step'] ) : 1;
$total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : false;
$custom = isset( $_REQUEST['custom'] ) ? absint( $_REQUEST['custom'] ) : 0;
$number = isset( $_REQUEST['number'] ) ? absint( $_REQUEST['number'] ) : 100;
$completed = isset( $_REQUEST['completed'] ) ? absint( $_REQUEST['completed'] ) : false;
$steps = ceil( $total / $number );
// Prevent a weird case where the estimate was off. Usually only a couple.
'page' => 'pum-upgrades',
'pum-upgrade' => $action,
'completed' => $completed,
update_option( 'pum_doing_upgrade', $this->upgrade_args );
public function set_arg( $key, $value = null ) {
$this->upgrade_args[ $key ] = $value;
if ( 'number' === $key || 'total' === $key ) {
$this->upgrade_args['steps'] = ceil( $this->upgrade_args['total'] / $this->upgrade_args['number'] );
if ( $this->upgrade_args['step'] > $this->upgrade_args['steps'] ) {
// Prevent a weird case where the estimate was off. Usually only a couple.
$this->upgrade_args['steps'] = $this->upgrade_args['step'];
} elseif ( $this->upgrade_args['step'] * $this->upgrade_args['steps'] ) {
update_option( 'pum_doing_upgrade', $this->upgrade_args );
public function get_arg( $key = null ) {
if ( ! isset( $this->upgrade_args[ $key ] ) ) {
return $this->upgrade_args[ $key ];
public function get_args() {
return $this->upgrade_args;
public function doing_upgrades() {
return $this->doing_upgrades;
* Display Upgrade Notices
public function show_upgrade_notices() {
$screen = get_current_screen();
if ( $screen->id === $this->page ) {
return; // Don't show notices on the upgrades page
if ( ! $this->has_upgrades() ) {
// Sequential Orders was the first stepped upgrade, so check if we have a stalled upgrade
$resume_upgrade = $this->maybe_resume_upgrade();
if ( ! empty( $resume_upgrade ) ) {
$resume_url = add_query_arg( $resume_upgrade, admin_url( 'index.php' ) );
'<div class="error"><p>' . __( 'Popup Maker needs to complete a database upgrade that was previously started, click <a href="%s">here</a> to resume the upgrade.', 'popup-maker' ) . '</p></div>',
'<div class="error"><p><strong>%s:</strong> <span class="dashicons dashicons-warning" style="color: #dc3232;"></span> %s %s %s</p></div>',
__( 'Popup Maker', 'popup-maker' ),
__( 'Important', 'popup-maker' ),
__( 'Database upgrades required.', 'popup-maker' ),
__( 'Please click %1$shere%2$s to complete these changes now.', 'popup-maker' ),
'<a href="' . esc_url( admin_url( 'options.php?page=pum-upgrades' ) ) . '">',
* Triggers all upgrade functions
* This function is usually triggered via AJAX
public function trigger_upgrades() {
if ( ! current_user_can( $this->required_cap ) ) {
wp_die( __( 'You do not have permission to do upgrades', 'popup-maker' ), __( 'Error', 'popup-maker' ), [ 'response' => 403 ] );
$deprecated_ver = get_site_option( 'popmake_version', false );
$current_ver = get_option( 'pum_ver', $deprecated_ver );
// Save Upgraded From option
update_option( 'pum_ver_upgraded_from', $current_ver );
update_option( 'pum_ver', Popup_Maker::$VER );
$this->process_upgrades();
'<strong>%s</strong><br/>%s',
__( 'Upgrades have been completed successfully.', 'popup-maker' ),
sprintf( 'You will automatically be redirected in %s seconds', '<span id="pum-countdown">5</span>' )
'redirect' => admin_url( 'edit.php?post_type=popup' ),
); // Let AJAX know that the upgrade is complete
* Updates the pum_db_ver to the passed $version.
* If no $version is passed a default value will be established.
public function set_pum_db_ver( $version = null ) {
$version = preg_replace( '/[^0-9.].*/', '', $version );
update_option( 'pum_db_ver', $version );
$upgraded_from = get_option( 'pum_ver_upgraded_from', false );
// this is the current database schema version number
$current_db_ver = pum_get_db_ver();
// If no current db version, but prior install detected, set db version correctly.
if ( ! $current_db_ver ) {
if ( version_compare( $upgraded_from, '1.3.0', '<' ) ) {
$current_db_ver = Popup_Maker::$DB_VER;
add_option( 'pum_db_ver', $current_db_ver );
* Gets the pum_db_ver or sets and returns the correct one.
* @see PUM_Utils_Upgrades::set_pum_db_ver()
public function get_pum_db_ver() {
if ( ! isset( $pum_db_ver ) ) {
// this is the current database schema version number
$pum_db_ver = pum_get_db_ver();
$pum_db_ver = pum_get_db_ver();
return preg_replace( '/[^0-9.].*/', '', $pum_db_ver );
* Process upgrades in a stepped succession.
* Starts with the current version and loops until reaching the target version.
public function process_upgrades() {
// this is the target version that we need to reach
$target_db_ver = Popup_Maker::$DB_VER;
// this is the current database schema version number
$current_db_ver = $this->get_pum_db_ver();
// Run upgrade routine until target version reached.
while ( $current_db_ver < $target_db_ver ) {
// increment the current db_ver by one
$this->current_routine = $current_db_ver;
$this->next_routine = $current_db_ver === $target_db_ver ? null : $current_db_ver + 1;
if ( file_exists( POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php" ) ) {
require_once POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php";
$func = "PUM_Admin_Upgrade_Routine_{$current_db_ver}::run";
if ( is_callable( $func ) ) {
public function current_routine() {
return $this->current_routine;
public function next_routine() {
return $this->next_routine;
* Process upgrades in a stepped succession.
* Starts with the current version and loops until reaching the target version.
public function get_upgrades() {
// this is the target version that we need to reach
$target_db_ver = Popup_Maker::$DB_VER;
// this is the current database schema version number
$current_db_ver = $this->get_pum_db_ver();
// Run upgrade routine until target version reached.
while ( $current_db_ver < $target_db_ver ) {
// increment the current db_ver by one
if ( file_exists( POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php" ) ) {
require_once POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php";
$func = "PUM_Admin_Upgrade_Routine_{$current_db_ver}::description";
if ( is_callable( $func ) ) {
$upgrades[ $current_db_ver ] = call_user_func( $func );
public function get_upgrade( $version = null ) {
$upgrades = $this->get_upgrades();
if ( isset( $upgrades[ $version ] ) ) {
return $upgrades[ $version ];
* Returns true if there are unprocessed upgrades.
public function has_upgrades() {
return boolval( count( $this->get_upgrades() ) );
* For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
* @return mixed When nothing to resume returns false, otherwise starts the upgrade where it left off
public function maybe_resume_upgrade() {
$doing_upgrade = get_option( 'pum_doing_upgrade', false );
if ( empty( $doing_upgrade ) ) {
* Adds an upgrade action to the completed upgrades array
* @param string $upgrade_action The action to add to the competed upgrades array
* @return bool If the function was successfully added
public function set_upgrade_complete( $upgrade_action = '' ) {
if ( empty( $upgrade_action ) ) {
$completed_upgrades = $this->get_completed_upgrades();
$completed_upgrades[] = $upgrade_action;
// Remove any blanks, and only show uniques