: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @package AdvancedAds\Framework
* @author Advanced Ads <info@wpadvancedads.com>
namespace AdvancedAds\Framework;
use InvalidArgumentException;
defined( 'ABSPATH' ) || exit;
* Updates that need to be run.
private $option_name = null;
* Retrieve main instance.
* Ensure only one instance is loaded or can be loaded.
public static function get() {
if ( null === $instance && ! ( $instance instanceof Updates ) ) {
$instance = new Updates();
* @throws InvalidArgumentException When folder not defined.
* @throws InvalidArgumentException When version not defined.
* @throws InvalidArgumentException When option name not defined.
public function hooks() {
if ( empty( $this->folder ) ) {
throw new InvalidArgumentException( 'Please set the folder path for update files.' );
if ( empty( $this->version ) ) {
throw new InvalidArgumentException( 'Please set the plugin version number.' );
if ( empty( $this->option_name ) ) {
throw new InvalidArgumentException( 'Please set option name to save version in database.' );
add_action( 'admin_init', [ $this, 'do_updates' ] );
* @param string $folder Folder path to look for updates.
public function set_folder( $folder ) {
$this->folder = trailingslashit( $folder );
* Set plugin version number
* @param string $version Plugin version.
public function set_version( $version ) {
$this->version = $version;
* Set plugin option name.
* @param string $name Plugin option name.
public function set_option_name( $name ) {
$this->option_name = $name;
* @param array $updates Array of updates to be run.
public function add_updates( array $updates ) {
$this->updates = $updates;
* Check if need any update
public function do_updates() {
if ( ! current_user_can( 'update_plugins' ) ) {
$installed_version = get_option( $this->option_name );
// Maybe it's the first install.
if ( ! $installed_version ) {
if ( version_compare( $installed_version, $this->version, '<' ) ) {
$this->perform_updates();
public function perform_updates() {
$installed_version = get_option( $this->option_name );
foreach ( $this->updates as $version => $path ) {
if ( version_compare( $installed_version, $version, '<' ) ) {
include $this->folder . $path;
$this->save_version( $version );
* @param string $version Version number to save.
private function save_version( $version = false ) {
if ( empty( $version ) ) {
$version = $this->version;
update_option( $this->option_name, $this->version );