: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<?php // phpcs:ignoreFile
defined( 'ABSPATH' ) || exit;
class Advanced_Ads_Admin_Licenses {
* Instance of this class.
protected static $instance = null;
* License API endpoint URL
const API_ENDPOINT = 'https://wpadvancedads.com/license-api/';
* Advanced_Ads_Admin_Licenses constructor.
private function __construct() {
if ( ! defined( 'DOING_AJAX' ) ) {
add_action( 'load-plugins.php', [ $this, 'check_plugin_licenses' ] );
add_action( 'plugins_loaded', [ $this, 'wp_plugins_loaded' ] );
// todo: check if this is loaded late enough and all add-ons are registered already.
add_filter( 'upgrader_pre_download', [ $this, 'addon_upgrade_filter' ], 10, 3 );
* Actions and filter available after all plugins are initialized
public function wp_plugins_loaded() {
// check for add-on updates.
add_action( 'admin_init', [ $this, 'add_on_updater' ], 1 );
// react on API update checks
add_action( 'http_api_debug', [ $this, 'update_license_after_version_info' ], 10, 5 );
* Return an instance of this class.
* @return self object A single instance of this class.
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
public function check_plugin_licenses() {
// gather all add-on plugin files.
$add_ons = apply_filters( 'advanced-ads-add-ons', [] );
foreach ( $add_ons as $_add_on ) {
if ( $this->get_license_status( $_add_on['options_slug'] ) !== 'valid' ) {
$plugin_file = plugin_basename( $_add_on['path'] );
add_action( 'after_plugin_row_' . $plugin_file, [ $this, 'add_plugin_list_license_notice' ], 10, 2 );
* Add a row below add-ons with an invalid license on the plugin list
* @param string $plugin_file Path to the plugin file, relative to the plugins directory.
* @param array $plugin_data An array of plugin data.
* @todo make this work on multisite as well
public function add_plugin_list_license_notice( $plugin_file, $plugin_data ) {
if ( is_null( $cols ) ) {
$cols = count( _get_list_table( 'WP_Plugins_List_Table' )->get_columns() );
'<tr class="advads-plugin-update-tr plugin-update-tr active"><td class="plugin-update colspanchange" colspan="%d"><div class="update-message notice inline notice-warning notice-alt"><p>%s</p></div></td></tr>',
/* Translators: 1: add-on name 2: admin URL to license page */
__( 'There might be a new version of %1$s. Please <strong>provide a valid license key</strong> in order to receive updates and support <a href="%2$s">on this page</a>.', 'advanced-ads' ),
admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' )
* @param string $addon string with add-on identifier.
* @param string $plugin_name name of the add-on.
* @param string $options_slug slug of the option in the database.
* @param string $license_key license key.
public function activate_license( $addon = '', $plugin_name = '', $options_slug = '', $license_key = '' ) {
if ( '' === $addon || '' === $plugin_name || '' === $options_slug ) {
return __( 'Error while trying to register the license. Please contact support.', 'advanced-ads' );
$license_key = esc_attr( trim( $license_key ) );
if ( '' === $license_key ) {
return __( 'Please enter a valid license key', 'advanced-ads' );
if ( has_filter( 'advanced_ads_license_' . $options_slug ) ) {
return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key );
* We need to remove the mltlngg_get_url_translated filter added by Multilanguage by BestWebSoft, https://wordpress.org/plugins/multilanguage/
* it causes the URL to look much different than it originally is
* we are adding it again later
remove_filter( 'home_url', 'mltlngg_get_url_translated' );
'edd_action' => 'activate_license',
'license' => $license_key,
'item_name' => urlencode( $plugin_name ),
* Re-add the filter removed from above
if ( function_exists( 'mltlngg_get_url_translated' ) ) {
add_filter( 'home_url', 'mltlngg_get_url_translated' );
$response = wp_remote_post(
// show license debug output if constant is set.
if ( defined( 'ADVANCED_ADS_SHOW_LICENSE_RESPONSE' ) ) {
return '<p><strong>' . esc_html__( 'The license status does not change as long as ADVANCED_ADS_SHOW_LICENSE_RESPONSE is enabled in wp-config.php.', 'advanced-ads' ) . '</strong></p>' .
'<pre>' . print_r( $response, true ) . '</pre>';
* Send the user to our support when his request is blocked by our firewall
if ( $error = $this->blocked_by_firewall( $response ) ) {
if ( is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
return __( 'License couldn’t be activated. Please try again later.', 'advanced-ads' ) . " (cURL {$curl['version']})";
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! empty( $license_data->license ) ) {
update_option( $options_slug . '-license-status', $license_data->license, false );
if ( ! empty( $license_data->expires ) ) {
update_option( $options_slug . '-license-expires', $license_data->expires, false );
// display activation problem.
if ( ! empty( $license_data->error ) ) {
// user friendly texts for errors.
'license_not_activable' => __( 'This is the bundle license key.', 'advanced-ads' ),
'item_name_mismatch' => __( 'This is not the correct key for this add-on.', 'advanced-ads' ),
'no_activations_left' => __( 'There are no activations left.', 'advanced-ads' )
/* translators: %1$s is a starting link tag, %2$s is the closing one. */
__( 'You can manage activations in %1$syour account%2$s.', 'advanced-ads' ),
'<a href="https://wpadvancedads.com/account/?utm_source=advanced-ads&utm_medium=link&utm_campaign=settings-licenses-activations-left" target="_blank">',
/* translators: %1$s is a starting link tag, %2$s is the closing one. */
__( '%1$sUpgrade%2$s for more activations.', 'advanced-ads' ),
'<a href="https://wpadvancedads.com/account/upgrades/?utm_source=advanced-ads&utm_medium=link&utm_campaign=settings-licenses-activations-left" target="_blank">',
$error = isset( $errors[ $license_data->error ] ) ? $errors[ $license_data->error ] : $license_data->error;
if ( 'expired' === $license_data->error ) {
if ( isset( $errors[ $license_data->error ] ) ) {
// translators: %s is a string containing information about the issue.
__( 'License is invalid. Reason: %s', 'advanced-ads' ),
// reset license_expires admin notification.
Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_expires' ); // this one is no longer added, but we keep the check here in case it is still in the queue for some users.
Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_expired' ); // this one is no longer added, but we keep the check here in case it is still in the queue for some users.
Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_invalid' );
$licenses = $this->get_licenses();
$licenses[ $addon ] = $license_key;
$this->save_licenses( $licenses );
* Check if a request was blocked by our firewall
* @param array $response response from license call.
* @return mixed message or false
public function blocked_by_firewall( $response ) {
$response_code = wp_remote_retrieve_response_code( $response );
if ( '403' == $response_code ) {
$blocked_information = '–';
if ( isset( $response['body'] ) ) {
// look for the IP address in this line: `<td><span>95.90.238.103</span></td>`.
$pattern = '/<span>([.0-9]*)<\/span>/';
preg_match( $pattern, $response['body'], $matches );
$ip = isset( $matches[1] ) ? $matches[1] : '–';
$blocked_information = 'IP: ' . $ip;
// translators: %s is a list of server information like IP address. Just keep it as is.
return sprintf( __( 'Your request was blocked by our firewall. Please send us the following information to unblock you: %s.', 'advanced-ads' ), $blocked_information );
* Check if a specific license key was already activated for the current page
* @param string $license_key license key.
* @param string $plugin_name name of the add-on.
* @param string $options_slug slug of the option in the database.
* @return bool true if already activated
* @deprecated since version 1.7.2 because it only checks if a key is valid, not if the url registered with that key
public function check_license( $license_key = '', $plugin_name = '', $options_slug = '' ) {
if ( has_filter( 'advanced_ads_license_' . $options_slug ) ) {
return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key );
'edd_action' => 'check_license',
'license' => $license_key,
'item_name' => urlencode( $plugin_name ),
$response = wp_remote_get(
add_query_arg( $api_params, 'https://wpadvancedads.com/' ),
if ( is_wp_error( $response ) ) {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
// if this license is still valid.
if ( 'valid' === $license_data->license ) {
update_option( $options_slug . '-license-expires', $license_data->expires, false );
update_option( $options_slug . '-license-status', $license_data->license, false );
* @param string $addon string with add-on identifier.
* @param string $plugin_name name of the add-on.
* @param string $options_slug slug of the option in the database.
public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) {
if ( '' === $addon || '' === $plugin_name || '' === $options_slug ) {
return __( 'Error while trying to disable the license. Please contact support.', 'advanced-ads' );
$licenses = $this->get_licenses();
$license_key = isset( $licenses[ $addon ] ) ? $licenses[ $addon ] : '';
if ( has_filter( 'advanced_ads_license_' . $options_slug ) ) {
return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key );
'edd_action' => 'deactivate_license',
'license' => $license_key,
'item_name' => urlencode( $plugin_name ),
// send the remote request.
$response = wp_remote_post(
// show license debug output if constant is set.
if ( defined( 'ADVANCED_ADS_SHOW_LICENSE_RESPONSE' ) ) {
return '<p><strong>' . esc_html__( 'The license status does not change as long as ADVANCED_ADS_SHOW_LICENSE_RESPONSE is enabled in wp-config.php.', 'advanced-ads' ) . '</strong></p>' .
'<pre>' . print_r( $response, true ) . '</pre>';
if ( is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
return __( 'License couldn’t be deactivated. Please try again later.', 'advanced-ads' );
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
* Send the user to our support when his request is blocked by our firewall
if ( $error = $this->blocked_by_firewall( $response ) ) {
if ( 'deactivated' === $license_data->license ) {
delete_option( $options_slug . '-license-status' );
delete_option( $options_slug . '-license-expires' );
} elseif ( 'failed' === $license_data->license ) {
update_option( $options_slug . '-license-expires', $license_data->expires, false );
update_option( $options_slug . '-license-status', $license_data->license, false );
return __( 'License couldn’t be deactivated. Please try again later.', 'advanced-ads' );
* Get license keys for all add-ons
public function get_licenses() {
return get_option( ADVADS_SLUG . '-licenses', [] );
* Save license keys for all add-ons
* @param array $licenses licenses.
public function save_licenses( $licenses = [] ) {
update_option( ADVADS_SLUG . '-licenses', $licenses );
* Get license status of an add-on
* @param string $slug slug of the add-on.
* @return string|false license status, "valid", "invalid" or false if option doesn't exist.
public function get_license_status( $slug = '' ) {
return get_option( $slug . '-license-status', false );
* If two or more add-ons use the same valid license this is probably an all-access customer
public function get_probably_all_access() {
return $this->get_license_status( ADVADS_SLUG . '-' . $key );
return [] !== $valid && max( array_count_values( $valid ) ) > 1;
* Return the licence expiry time if it is equal for more than one add-on. That indicates it is likely an All Access license
public function get_probably_all_access_expiry() {
* Get expiry dates of all add-ons.
* @param string $key Add-on key.
* @return string|false the expiration date or false.
$expiry_counts = array_count_values( array_map( function( $key ) {
return $this->get_license_expires( ADVADS_SLUG . '-' . $key );
}, array_keys( array_filter( $this->get_licenses() ) ) ) );
* Remove all licenses that are used only once.
* @param int $count the count from array_count_values_above
* @return bool whether the count is greater 1
$all_access = array_filter( $expiry_counts, function( $count ) {
// if there is an item in $all_access we can assume this is from All Access and return the expiry date.
return empty( $all_access ) ? null : key( $all_access );
* Get license expired value of an add-on
* @param string $slug slug of the add-on.
* @return string $date expiry date of an add-on, empty string if no option exists
public function get_license_expires( $slug = '' ) {
return get_option( $slug . '-license-expires', '' );
* Register the Updater class for every add-on, which includes getting version information
public function add_on_updater() {
// ignore, if not main blog or if updater was disabled
if ( ( is_multisite() && ! is_main_site() ) || ! apply_filters( 'advanced-ads-add-ons-updater', true ) ) {
* List of registered add ons