Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wp-security-audit-log/classes/Entities/Delete/class-delete-records.php
+++ b/wp-security-audit-log/classes/Entities/Delete/class-delete-records.php
@@ -29,16 +29,17 @@
class Delete_Records {
/**
- * Deletes records from the Occurrences amd Metadata tables.
+ * Deletes records from the Occurrences and Metadata tables.
*
* @param array $order - The order clause [ field -> order direction ].
* @param integer $limit - The limit for the query.
* @param array $condition - Condition to be used in the query.
- * @param WPDB $connection - WPDB connection to be used (if different from the currently default one).
+ * @param wpdb $connection - wpdb connection to be used (if different from the currently default one).
*
- * @return array - Number of deleted records (in Occurrences only) and the executed SQL as string.
+ * @return array|false|null - Deletion result and SQL, false on selection or metadata deletion failure, or null when no records match.
*
* @since 4.6.0
+ * @since 5.6.8 - Propagates selection and metadata deletion failures, deletes only metadata of matching occurrences and returns the prepared SQL.
*/
public static function delete( $order = array(), $limit = 0, $condition = array(), $connection = null ) {
if ( null !== $connection ) {
@@ -53,60 +54,77 @@
$sql = 'SELECT max(id) FROM ' . Occurrences_Entity::get_table_name( $_wpdb ) . ' WHERE 1 ';
if ( ! empty( $condition ) ) {
- $sql .= ' AND ' . array_key_first( $condition );
+ $sql .= ' AND ' . array_key_first( $condition );
}
/**
* Multi order is not supported currently.
*/
if ( ! empty( $order ) ) {
- $sql .= ' ORDER BY ' . array_key_first( $order ) . ' ' . reset( $order );
+ $sql .= ' ORDER BY ' . array_key_first( $order ) . ' ' . reset( $order );
}
if ( ! empty( $limit ) ) {
$sql .= ' LIMIT ' . $order;
}
+ $prepared_sql = $_wpdb->prepare( $sql, reset( $condition ) );
+
$_wpdb->suppress_errors( true );
- $biggest_id = (int) $_wpdb->get_var( $_wpdb->prepare( $sql, reset( $condition ) ) );
+
+ $biggest_id = (int) $_wpdb->get_var( $prepared_sql );
+
if ( '' !== $_wpdb->last_error ) {
if ( 1146 === Occurrences_Entity::get_last_sql_error( $_wpdb ) ) {
if ( Occurrences_Entity::create_table( $_wpdb ) ) {
- $biggest_id = (int) $_wpdb->get_var( $sql );
+ $biggest_id = (int) $_wpdb->get_var( $prepared_sql );
}
}
}
+
$_wpdb->suppress_errors( false );
- if ( $biggest_id > 0 ) {
- $delete_meta = 'DELETE FROM ' . Metadata_Entity::get_table_name( $_wpdb ) . ' WHERE occurrence_id <= %s';
+ if ( '' !== $_wpdb->last_error ) {
+ return false;
+ }
- Metadata_Entity::delete_query( $delete_meta, array( $biggest_id ), $_wpdb );
+ if ( 0 < $biggest_id ) {
+ $matching_occurrences_query = 'SELECT id FROM ' . Occurrences_Entity::get_table_name( $_wpdb ) . ' WHERE 1 ';
+
+ if ( ! empty( $condition ) ) {
+ $matching_occurrences_query .= ' AND ' . array_key_first( $condition );
+ }
+
+ // The ID range keeps the metadata index usable; the subquery limits deletion to matching occurrences.
+ $delete_meta = 'DELETE FROM ' . Metadata_Entity::get_table_name( $_wpdb ) . ' WHERE occurrence_id <= %d AND occurrence_id IN ( ' . $matching_occurrences_query . ' )';
+
+ if ( false === Metadata_Entity::delete_query( $delete_meta, array( $biggest_id, reset( $condition ) ), $_wpdb ) ) {
+ return false;
+ }
$delete_occurrence = 'DELETE FROM ' . Occurrences_Entity::get_table_name( $_wpdb ) . ' WHERE 1 ';
if ( ! empty( $condition ) ) {
- $delete_occurrence .= ' AND ' . array_key_first( $condition );
+ $delete_occurrence .= ' AND ' . array_key_first( $condition );
}
/**
* Multi order is not supported currently.
*/
if ( ! empty( $order ) ) {
- $delete_occurrence .= ' ORDER BY ' . array_key_first( $order ) . ' ' . reset( $order );
+ $delete_occurrence .= ' ORDER BY ' . array_key_first( $order ) . ' ' . reset( $order );
}
if ( ! empty( $limit ) ) {
$delete_occurrence .= ' LIMIT ' . $order;
}
+ $deleted_count = Occurrences_Entity::delete_query( $delete_occurrence, array( reset( $condition ) ), $_wpdb );
+ $executed_sql = $_wpdb->prepare( $delete_occurrence, reset( $condition ) );
+
return array(
- Occurrences_Entity::delete_query(
- $delete_occurrence,
- array( reset( $condition ) ),
- $_wpdb
- ),
- $delete_occurrence,
+ $deleted_count,
+ $executed_sql,
);
}
}
--- a/wp-security-audit-log/classes/Entities/class-occurrences-entity.php
+++ b/wp-security-audit-log/classes/Entities/class-occurrences-entity.php
@@ -757,37 +757,60 @@
/**
* Cleans up the database, based on pruning date selected and enabled.
*
- * @return void
+ * @return int|false - Deleted occurrence count, or false if any cleanup query failed.
*
* @since 5.0.0
+ * @since 5.6.8 - Reports cleanup results and logs successful deletions.
*/
public static function prune_records() {
$prune_enabled = Settings_Helper::get_boolean_option_value( 'pruning-date-e', false );
if ( ! $prune_enabled ) {
- return;
+ return 0;
}
- $now = time();
- $max_sdate = Plugin_Settings_Helper::get_pruning_date(); // Pruning date.
- $archiving = Settings_Helper::is_archiving_set_and_enabled();
+ $now = time();
+ $max_sdate = Plugin_Settings_Helper::get_pruning_date(); // Pruning date.
+ $connections = array();
+ $connections[] = self::get_connection();
+
// Calculate limit timestamp.
$max_stamp = $now - ( strtotime( $max_sdate ) - $now );
- $items = array();
-
- if ( $archiving ) {
- $connection_name = Settings_Helper::get_option_value( 'archive-connection' );
-
- $wsal_db = Connection::get_connection( $connection_name );
+ $count = 0;
+ $failed = false;
+ $queries = array();
+
+ foreach ( $connections as $connection ) {
+ $result = Delete_Records::delete( array(), 0, array( 'created_on <= %s' => intval( $max_stamp ) ), $connection );
+
+ if ( null === $result ) {
+ continue;
+ }
+
+ if ( false === $result || ! is_int( $result[0] ) ) {
+ $failed = true;
+
+ continue;
+ }
+
+ if ( 0 < $result[0] ) {
+ // Index 0: number of occurrences deleted from this connection.
+ $count += $result[0];
+
+ // Index 1: prepared DELETE query, stored in event 6000 as PruneQuery.
+ $queries[] = $result[1];
+ }
+ }
- $items = Delete_Records::delete( array(), 0, array( 'created_on <= %s' => intval( $max_stamp ) ), $wsal_db );
+ if ( 0 < $count ) {
+ do_action( 'wsal_prune', $count, implode( '; ', $queries ) );
}
- $main_items = Delete_Records::delete( array(), 0, array( 'created_on <= %s' => intval( $max_stamp ) ) );
+ return $failed ? false : $count;
}
/**
--- a/wp-security-audit-log/classes/Helpers/class-plugin-settings-helper.php
+++ b/wp-security-audit-log/classes/Helpers/class-plugin-settings-helper.php
@@ -15,6 +15,7 @@
use WSALHelpersSettings_Helper;
use WSALControllersAlert_Manager;
+use WSALLicensingLicensing_Factory;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
@@ -543,7 +544,7 @@
public static function get_plugin_settings() {
global $wpdb;
- return $wpdb->get_results("SELECT * FROM $wpdb->options WHERE option_name LIKE 'wsal_%'"); // phpcs:ignore
+ return $wpdb->get_results( "SELECT * FROM $wpdb->options WHERE option_name LIKE 'wsal_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- No WordPress function lists options by name prefix, and only the system information reads them.
}
/**
@@ -684,43 +685,22 @@
* Set the plugin in stealth mode for MainWP child sites.
*
* Following steps are taken in stealth mode:
- * 1. Freemius connection is skipped.
- * 2. Freemius notices are removed.
+ * 1. The licensing provider connection is skipped when supported.
+ * 2. Licensing provider notices are removed when supported.
* 3. WSAL's incognito mode is set.
* 4. Other site admins are restricted.
* 5. The current user is set as the sole editor of WSAL.
* 6. Stealth mode option is saved.
*
* @since 3.2.3.3
+ * @since 5.6.8 - Skips the licensing provider connection through Licensing_Factory.
*/
public static function set_mainwp_child_stealth_mode() {
if (
! Settings_Helper::get_boolean_option_value( 'mwp-child-stealth-mode', false ) // MainWP Child Stealth Mode is not already active.
&& WpSecurityAuditLog::is_mainwp_active() // And if MainWP Child plugin is installed & active.
) {
- // Check if freemius state is anonymous.
- if ( ! wsal_freemius()->is_premium() && 'anonymous' === Settings_Helper::get_option_value( 'freemius_state', 'anonymous' ) ) {
- // Update Freemius state to skipped.
- Settings_Helper::set_option_value( 'wsal_freemius_state', 'skipped', true );
-
- if ( ! WP_Helper::is_multisite() ) {
- wsal_freemius()->skip_connection(); // Opt out.
- } else {
- wsal_freemius()->skip_connection( null, true ); // Opt out for all websites.
- }
-
- // Connect account notice.
- if ( class_exists( 'FS_Admin_Notices' ) ) {
- FS_Admin_Notices::instance( 'wp-security-audit-log' )->remove_sticky( 'connect_account' );
- }
- }
-
- if ( ! wsal_freemius()->is_premium() ) {
- // Remove Freemius trial promotion notice.
- if ( class_exists( 'FS_Admin_Notices' ) ) {
- FS_Admin_Notices::instance( 'wp-security-audit-log' )->remove_sticky( 'trial_promotion' );
- }
- }
+ Licensing_Factory::prepare_mainwp_stealth_mode();
self::set_incognito( true ); // Incognito mode to hide WSAL on plugins page.
self::set_restrict_log_viewer( 'only_me' );
--- a/wp-security-audit-log/classes/Helpers/class-settings-helper.php
+++ b/wp-security-audit-log/classes/Helpers/class-settings-helper.php
@@ -14,6 +14,7 @@
use WSALUtilsMigration;
use WSALControllersConnection;
use WSALControllersAlert_Manager;
+use WSALLicensingLicensing_Factory;
use WSALHelpersPlugin_Settings_Helper;
// Exit if accessed directly.
@@ -1884,6 +1885,7 @@
* @return void
*
* @since 4.6.0
+ * @since 5.6.8 - Deletes local license data through Licensing_Factory.
*/
public static function delete_all_settings() {
global $wpdb;
@@ -1896,8 +1898,7 @@
wp_cache_flush();
- // Remove wsal specific Freemius entry.
- delete_site_option( 'fs_wsalp' );
+ Licensing_Factory::delete_local_license_data();
// Ensue entry is fully cleared.
delete_site_option( 'wsal_networkwide_tracker_cpts' );
--- a/wp-security-audit-log/classes/Helpers/class-uninstall-helper.php
+++ b/wp-security-audit-log/classes/Helpers/class-uninstall-helper.php
@@ -18,6 +18,7 @@
use WSALEntitiesReports_Entity;
use WSALHelpersSettings_Helper;
use WSALEntitiesMetadata_Entity;
+use WSALLicensingLicensing_Factory;
use WSALEntitiesOccurrences_Entity;
use WSALEntitiesGenerated_Reports_Entity;
use WSALEntitiesCustom_Notifications_Entity;
@@ -47,8 +48,28 @@
*
* @since 4.6.0
* @since 5.6.7 - Added custom notifications table removal.
+ * @since 5.6.8 - Frees the EDD license activations and removes the license data, whatever the "delete data" setting.
*/
public static function uninstall() {
+ $is_uninstalling = defined( 'WP_UNINSTALL_PLUGIN' ) || doing_action( 'uninstall_' . WSAL_BASE_NAME );
+
+ // The license is removed whatever the "delete data" setting is, but only when the plugin is really deleted.
+ if ( $is_uninstalling ) {
+ Licensing_Factory::remove_license_on_uninstall();
+ }
+
+ /**
+ * During uninstall, WordPress loads the main plugin file, which starts Action Scheduler,
+ * and then deletes the plugin files before the request ends. Its shutdown callback and recurring
+ * cron event are removed so they don't try to load files that no longer exist, which would
+ * cause a fatal error.
+ */
+ if ( $is_uninstalling && class_exists( 'ActionScheduler_QueueRunner', false ) ) {
+ ActionScheduler_QueueRunner::instance()->unhook_dispatch_async_request();
+
+ wp_clear_scheduled_hook( ActionScheduler_QueueRunner::WP_CRON_HOOK, array( 'WP Cron' ) );
+ }
+
if ( Settings_Helper::get_boolean_option_value( 'delete-data' ) ) {
Occurrences_Entity::drop_table();
Metadata_Entity::drop_table();
--- a/wp-security-audit-log/classes/Helpers/class-upgrade-notice.php
+++ b/wp-security-audit-log/classes/Helpers/class-upgrade-notice.php
@@ -47,7 +47,6 @@
if ( 'plugins' === $current_screen->id ) {
add_action( 'in_plugin_update_message-' . WpSecurityAuditLog::PREMIUM_VERSION_WHOLE_PLUGIN_NAME, array( __CLASS__, 'prefix_plugin_update_message' ), 10, 2 );
add_action( 'in_plugin_update_message-' . WpSecurityAuditLog::FREE_VERSION_WHOLE_PLUGIN_NAME, array( __CLASS__, 'prefix_plugin_update_message' ), 10, 2 );
- add_action( 'in_plugin_update_message-' . WpSecurityAuditLog::NOFS_VERSION_WHOLE_PLUGIN_NAME, array( __CLASS__, 'prefix_plugin_update_message' ), 10, 2 );
}
}
--- a/wp-security-audit-log/classes/Helpers/class-view-manager.php
+++ b/wp-security-audit-log/classes/Helpers/class-view-manager.php
@@ -17,6 +17,7 @@
use WSALHelpersSettings_Helper;
use WSALControllersAlert_Manager;
use WSALListAdminEventsList_Events;
+use WSALLicensingLicensing_Factory;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
@@ -53,6 +54,7 @@
* Method: Constructor.
*
* @since 1.0.0
+ * @since 5.6.8 - Uses Licensing_Factory for plan checks and moved the Freemius hooks to Freemius_Provider.
*/
public static function init() {
@@ -93,15 +95,6 @@
}
}
- // Stop Freemius from hiding the menu on sub sites under certain circumstances.
- add_filter(
- 'fs_should_hide_site_admin_settings_on_network_activation_mode_wp-security-audit-log',
- array(
- __CLASS__,
- 'bypass_freemius_menu_hiding',
- )
- );
-
// Add menus.
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menus' ) );
add_action( 'network_admin_menu', array( __CLASS__, 'add_admin_menus' ) );
@@ -126,8 +119,6 @@
}
- add_action( 'admin_head', array( __CLASS__, 'hide_freemius_sites_section' ) );
-
// Check if WFCM is running by seeing if we have the version defined.
if ( defined( 'WFCM_VERSION' ) && ( version_compare( WFCM_VERSION, '1.6.0', '<' ) ) ) {
add_action( 'admin_notices', array( __CLASS__, 'update_wfcm_notice' ) );
@@ -220,7 +211,7 @@
$main_view_menu_slug,
array( __CLASS__, 'render_view_body' ),
self::$views[0]->get_icon(),
- '2.5' // Right after dashboard.
+ '2.5' // Right after dashboard. A string, because WordPress before 6.0 turns a float 2.5 into 2, the Dashboard position.
)
);
@@ -288,6 +279,7 @@
* @param array $old_links - Array of old links.
*
* @since 5.0.0
+ * @since 5.6.8 - Uses Licensing_Factory for the Premium build check.
*/
public static function add_plugin_shortcuts( $old_links ) {
self::reorder_views();
@@ -298,7 +290,7 @@
if ( call_user_func( array( $view, 'has_plugin_shortcut_link' ) ) ) {
$new_links[] = '<a href="' . add_query_arg( 'page', call_user_func( array( $view, 'get_safe_view_name' ) ), network_admin_url( 'admin.php' ) ) . '">' . call_user_func( array( $view, 'get_name' ) ) . '</a>';
- if ( 1 === count( $new_links ) && ! wsal_freemius()->is__premium_only() ) {
+ if ( 1 === count( $new_links ) && ! Licensing_Factory::is_premium_build() ) {
// Trial link.
$trial_link = 'https://melapress.com/wordpress-activity-log/pricing/?utm_source=plugin&utm_medium=wsal&utm_campaign=plugins-page-get-premium';
$new_links[] = '<a style="font-weight:bold; color:#049443 !important" href="' . $trial_link . '" target="_blank">' . __( 'Get Premium!', 'wp-security-audit-log' ) . '</a>';
@@ -470,51 +462,6 @@
/**
- * Hide Freemius sites section on the account page
- * of a multisite WordPress network.
- *
- * @since 5.0.0
- */
- public static function hide_freemius_sites_section() {
- global $pagenow;
-
- // Return if not multisite.
- if ( ! is_multisite() ) {
- return;
- }
-
- // Return if multisite but not on the network admin.
- if ( ! is_network_admin() ) {
- return;
- }
-
- // Return if the pagenow is not on admin.php page.
- if ( 'admin.php' !== $pagenow ) {
- return;
- }
-
- // No nonce needed, this only reads the standard WP admin routing parameter for a strict string comparison.
- $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
-
- if ( 'wsal-auditlog-account' === $page ) {
- echo '<style type="text/css">#fs_sites {display:none;}</style>';
- }
- }
-
- /**
- * Bypasses Freemius hiding menu items.
- *
- * @param bool $should_hide Should allow Freemium to hide menu items.
- *
- * @return bool
- *
- * @since 5.0.0
- */
- public static function bypass_freemius_menu_hiding( $should_hide ) {
- return false;
- }
-
- /**
* Builds a relative asset path that takes SCRIPT_DEBUG constant into account.
*
* @param string $path Path relative to the plugin folder.
--- a/wp-security-audit-log/classes/Licensing/class-licensing-factory.php
+++ b/wp-security-audit-log/classes/Licensing/class-licensing-factory.php
@@ -0,0 +1,779 @@
+<?php
+/**
+ * Licensing factory for WP Activity Log.
+ *
+ * @package wsal
+ * @subpackage licensing
+ *
+ * @since 5.6.8
+ */
+
+declare(strict_types=1);
+
+namespace WSALLicensing;
+
+use WSALHelpersUninstall_Helper;
+use WSALLicensingProvidersLicense_Page;
+use WSALLicensingProvidersLicense_Notices;
+use WSALLicensingProvidersEDDEDD_Provider;
+use WSALLicensingProvidersFreemiusFreemius_Provider;
+
+// Exit if accessed directly.
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+if ( ! class_exists( 'WSALLicensingLicensing_Factory' ) ) {
+
+ /**
+ * Provides a provider-neutral entry point for licensing operations.
+ *
+ * @since 5.6.8
+ */
+ class Licensing_Factory {
+
+ /**
+ * Plan names from lowest to highest.
+ *
+ * @var string[]
+ *
+ * @since 5.6.8
+ */
+ public const PLANS = array( 'starter', 'professional', 'business', 'enterprise', 'premium', 'enterprisesupport' );
+
+ /**
+ * EDD product IDs and the plan from self::PLANS that each product grants.
+ *
+ * Each product maps to the Freemius plan sold under the same name, so EDD and Freemius
+ * customers get the same features.
+ *
+ * @var array
+ *
+ * @since 5.6.8
+ */
+ public const EDD_PRODUCT_PLANS = array(
+ 281 => 'premium',
+ 284 => 'enterprise',
+ 286 => 'enterprisesupport',
+ );
+
+ /**
+ * Site option storing the selected provider name.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const PROVIDER_OPTION = 'wsal_licensing_provider';
+
+ /**
+ * Prefix of Freemius license keys.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const FREEMIUS_KEY_PREFIX = 'sk_';
+
+ /**
+ * Prefix of EDD license keys.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const EDD_KEY_PREFIX = 'edd_';
+
+ /**
+ * Freemius provider name.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const FREEMIUS_PROVIDER_NAME = 'freemius';
+
+ /**
+ * EDD provider name.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const EDD_PROVIDER_NAME = 'edd';
+
+ /**
+ * Registered provider classes keyed by provider name, in fallback order.
+ *
+ * @var array
+ *
+ * @since 5.6.8
+ */
+ private const PROVIDERS = array(
+ self::FREEMIUS_PROVIDER_NAME => Freemius_Provider::class,
+ self::EDD_PROVIDER_NAME => EDD_Provider::class,
+ );
+
+ /**
+ * Site option Freemius stores the cached license state in. Its presence means Freemius was used on this site.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const FREEMIUS_LICENSE_OPTION = 'fs_wsalp';
+
+ /**
+ * Prefix of the site options the EDD provider stores the license in.
+ *
+ * @var string
+ *
+ * @since 5.6.8
+ */
+ public const EDD_OPTION_PREFIX = 'wsal_edd_';
+
+ /**
+ * Active provider class name.
+ *
+ * @var class-string<Licensing_Provider>|null
+ *
+ * @since 5.6.8
+ */
+ private static $provider = null;
+
+ /**
+ * Whether the active provider has been selected during this request.
+ *
+ * @var bool
+ *
+ * @since 5.6.8
+ */
+ private static $is_provider_resolved = false;
+
+ /**
+ * Whether this site is connected to Freemius, checked once per request.
+ *
+ * @var bool|null
+ *
+ * @since 5.6.8
+ */
+ private static $is_freemius_connected = null;
+
+ /**
+ * Returns a registered provider class that can be used in this build.
+ *
+ * @param string $provider_name - Provider name.
+ *
+ * @return string|null $provider - Provider class name, or null when the provider is unknown or unavailable.
+ *
+ * @since 5.6.8
+ */
+ private static function get_available_provider( string $provider_name ) {
+ $provider = self::PROVIDERS[ $provider_name ] ?? null;
+
+ // The Free build ships without the provider classes.
+ if ( null === $provider || ! class_exists( $provider ) || ! $provider::is_available() ) {
+ return null;
+ }
+
+ return $provider;
+ }
+
+ /**
+ * Checks whether this site is connected to Freemius.
+ *
+ * The Free build ships without the Freemius provider, so it is never connected there. The answer
+ * is kept until the end of the request, because the Freemius records are read without the option cache.
+ *
+ * @return bool $is_connected - True in the Premium build when Freemius can manage this site's license.
+ *
+ * @since 5.6.8
+ */
+ public static function is_freemius_connected(): bool {
+ if ( null === self::$is_freemius_connected ) {
+ self::$is_freemius_connected = self::is_premium_build() && Freemius_Provider::is_connected();
+ }
+
+ return self::$is_freemius_connected;
+ }
+
+ /**
+ * Selects the provider for this site.
+ *
+ * The stored provider choice wins. Without one, Freemius is used when it was used on this
+ * site before. A site with no license yet gets no provider, so it shows the license form
+ * instead of a provider's own connection screen.
+ *
+ * Freemius only counts while the site is still connected to it: its stored choice and cached
+ * license option survive a disconnect, and loading the SDK without a connection shows its
+ * registration screen, where an EDD key can't be entered.
+ *
+ * The Free build ships without providers, so it never gets one.
+ *
+ * @return string|null $provider - Provider class name, or null when no provider is available.
+ *
+ * @since 5.6.8
+ */
+ private static function resolve_provider() {
+ $stored_provider_name = self::get_stored_provider_name();
+
+ // If the saved provider is Freemius but the site is no longer connected to Freemius.
+ if ( self::FREEMIUS_PROVIDER_NAME === $stored_provider_name && ! self::is_freemius_connected() ) {
+ $stored_provider_name = '';
+ }
+
+ if ( '' !== $stored_provider_name ) {
+ $provider = self::get_available_provider( $stored_provider_name );
+
+ if ( null !== $provider ) {
+ return $provider;
+ }
+ }
+
+
+ // If the site was licensed with Freemius before the provider choice was saved, and is still connected to Freemius.
+ if ( self::has_freemius_license_data() && self::is_freemius_connected() ) {
+ return self::get_available_provider( self::FREEMIUS_PROVIDER_NAME );
+ }
+
+ return null;
+ }
+
+ /**
+ * Returns the stored provider name.
+ *
+ * @return string $provider_name - Stored provider name, or an empty string when none is stored.
+ *
+ * @since 5.6.8
+ */
+ public static function get_stored_provider_name(): string {
+ $provider_name = get_site_option( self::PROVIDER_OPTION, '' );
+
+ return is_string( $provider_name ) ? $provider_name : '';
+ }
+
+ /**
+ * Checks whether Freemius was used on this site, based on its cached license option.
+ *
+ * @return bool $has_data - True when the Freemius license option exists.
+ *
+ * @since 5.6.8
+ */
+ public static function has_freemius_license_data(): bool {
+ return null !== get_site_option( self::FREEMIUS_LICENSE_OPTION, null );
+ }
+
+ /**
+ * Checks whether an option holds license data, regardless of the option name's letter case.
+ *
+ * @param string $option_name - Option name.
+ *
+ * @return bool $is_license_option - True for the stored provider choice and the EDD license options.
+ *
+ * @since 5.6.8
+ */
+ public static function is_license_option( string $option_name ): bool {
+ // WordPress option names use a case-insensitive database collation by default.
+ $option_name = strtolower( $option_name );
+
+ if ( self::PROVIDER_OPTION === $option_name ) {
+ return true;
+ }
+
+ // True only when the option name starts with the EDD prefix.
+ return 0 === strpos( $option_name, self::EDD_OPTION_PREFIX );
+ }
+
+ /**
+ * Checks whether the Premium build has no licensing provider, so a license has to be activated first.
+ *
+ * @return bool $needs_activation - True when the license form must be shown instead of the plugin pages.
+ *
+ * @since 5.6.8
+ */
+ public static function needs_license_activation(): bool {
+ return self::is_premium_build() && null === self::get_provider();
+ }
+
+ /**
+ * Returns the name of the active provider.
+ *
+ * @return string $provider_name - Provider name, or an empty string when no provider is active.
+ *
+ * @since 5.6.8
+ */
+ public static function get_provider_name(): string {
+ $provider = self::get_provider();
+
+ return null !== $provider ? $provider::get_provider_name() : '';
+ }
+
+ /**
+ * Registers the uninstall callback with WordPress, unless Freemius handles the uninstall.
+ *
+ * WordPress keeps one uninstall callback per plugin. Freemius registers its own and runs the
+ * plugin's cleanup through its after_uninstall action, so it must not be replaced while the site
+ * is connected to Freemius. The stored options are read directly, because this runs before the
+ * migrations and must not cache the provider for the request.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function maybe_register_uninstall_hook() {
+ $stored_provider_name = self::get_stored_provider_name();
+ $uses_freemius = false;
+
+ if ( self::FREEMIUS_PROVIDER_NAME === $stored_provider_name ) {
+ $uses_freemius = true;
+ }
+
+ // Sites licensed with Freemius before the provider choice was stored.
+ if ( '' === $stored_provider_name && self::has_freemius_license_data() ) {
+ $uses_freemius = true;
+ }
+
+ if ( $uses_freemius && self::is_freemius_connected() ) {
+ return;
+ }
+
+ register_uninstall_hook( WSAL_BASE_NAME, array( Uninstall_Helper::class, 'uninstall' ) );
+ }
+
+ /**
+ * Frees the license activations and removes the license data when the plugin is uninstalled.
+ *
+ * A deleted plugin must not keep using activations or leave its license key behind. Only EDD
+ * licenses need it, and the Free build ships without the EDD provider.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function remove_license_on_uninstall() {
+ if ( ! self::is_premium_build() ) {
+ return;
+ }
+
+ // The stored choice is read, so the license is also removed while EDD is disabled with the wsal_edd_provider_available filter.
+ if ( self::EDD_PROVIDER_NAME !== self::get_stored_provider_name() ) {
+ return;
+ }
+
+ EDD_Provider::uninstall_cleanup();
+
+ delete_site_option( self::PROVIDER_OPTION );
+ }
+
+ /**
+ * Returns the active provider class.
+ *
+ * @param bool $force_refresh - Whether to select the provider again, checking the Freemius connection again too.
+ *
+ * @return string|null $provider - Provider class name, or null when no provider is available.
+ *
+ * @since 5.6.8
+ */
+ public static function get_provider( bool $force_refresh = false ) {
+ if ( $force_refresh ) {
+ self::$is_provider_resolved = false;
+ self::$is_freemius_connected = null;
+ }
+
+ if ( ! self::$is_provider_resolved ) {
+ self::$provider = self::resolve_provider();
+ self::$is_provider_resolved = true;
+ }
+
+ return self::$provider;
+ }
+
+ /**
+ * Registers the licensing hooks that must run on every request, front end included.
+ *
+ * The EDD provider activates the license of each new subsite, and WordPress can create subsites
+ * outside the admin, where the provider itself is not initialized.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function init_network_site_hooks() {
+ // The Free build never has a provider, so it always returns here.
+ if ( self::EDD_PROVIDER_NAME !== self::get_provider_name() ) {
+ return;
+ }
+
+ EDD_Provider::init_network_site_hooks();
+ }
+
+ /**
+ * Initializes the active licensing provider.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function init() {
+ $provider = self::get_provider();
+
+ if ( null !== $provider ) {
+ $provider::init();
+ }
+
+ // The license page ships in the Premium build only.
+ if ( self::is_premium_build() ) {
+ License_Page::init();
+ }
+ }
+
+ /**
+ * Returns the provider name a license key belongs to, based on its prefix.
+ *
+ * Freemius keys start with "sk_" and EDD keys start with "edd_".
+ *
+ * @param string $license_key - License key.
+ *
+ * @return string $provider_name - Provider name, or an empty string when the key format is not recognised.
+ *
+ * @since 5.6.8
+ */
+ public static function detect_provider_name( string $license_key ): string {
+ $license_key = trim( $license_key );
+
+ if ( 0 === strpos( $license_key, self::FREEMIUS_KEY_PREFIX ) ) {
+ return self::FREEMIUS_PROVIDER_NAME;
+ }
+
+ if ( 0 === strpos( $license_key, self::EDD_KEY_PREFIX ) ) {
+ return self::EDD_PROVIDER_NAME;
+ }
+
+ return '';
+ }
+
+ /**
+ * Checks whether this is the Premium build of the plugin.
+ *
+ * This is a property of the build, not of the license, so it does not depend on the provider.
+ *
+ * @return bool $is_premium_build - True in the Premium build.
+ *
+ * @since 5.6.8
+ */
+ public static function is_premium_build(): bool {
+ $is_premium_build = false;
+
+
+ return $is_premium_build;
+ }
+
+ /**
+ * Checks whether the active license is valid.
+ *
+ * @return bool $is_valid - True when the active license is valid.
+ *
+ * @since 5.6.8
+ */
+ public static function has_active_valid_license(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::has_active_valid_license();
+ }
+
+ /**
+ * Checks the cached license state without contacting the provider.
+ *
+ * @return bool $is_valid - True when the cached license state is valid.
+ *
+ * @since 5.6.8
+ */
+ public static function has_cached_valid_license(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::has_cached_valid_license();
+ }
+
+ /**
+ * Checks whether premium code may be used, including during a trial.
+ *
+ * @return bool $can_use - True when premium code may be used.
+ *
+ * @since 5.6.8
+ */
+ public static function can_use_premium_code(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::can_use_premium_code();
+ }
+
+ /**
+ * Checks whether premium settings are read-only while premium features keep running.
+ *
+ * @return bool $is_frozen - True when premium settings must not be edited.
+ *
+ * @since 5.6.8
+ */
+ public static function is_premium_frozen(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::is_premium_frozen();
+ }
+
+ /**
+ * Prints the notice that replaces premium settings while they are read-only.
+ *
+ * @return bool $is_rendered - True when premium settings are read-only and the notice was printed.
+ *
+ * @since 5.6.8
+ */
+ public static function maybe_render_frozen_notice(): bool {
+ $provider = self::get_provider();
+
+ if ( null === $provider ) {
+ return false;
+ }
+
+ $license_state = $provider::get_frozen_license_state();
+
+ if ( empty( $license_state ) ) {
+ return false;
+ }
+
+ License_Notices::render_frozen_notice( $license_state, true );
+
+ return true;
+ }
+
+ /**
+ * Checks whether the site has no paid, features-enabled license.
+ *
+ * @return bool $is_free - True when the site uses the free plan or no provider is available.
+ *
+ * @since 5.6.8
+ */
+ public static function is_free_plan(): bool {
+ $provider = self::get_provider();
+
+ return null === $provider || $provider::is_free_plan();
+ }
+
+ /**
+ * Checks whether a plan satisfies a required plan, using the order in self::PLANS.
+ *
+ * @param string $current_plan - Plan reported by the provider.
+ * @param string $required_plan - Plan required by the caller.
+ * @param bool $exact - Whether only the required plan itself matches.
+ *
+ * @return bool $matches - True when the current plan is the required plan, or a higher plan when $exact is false.
+ *
+ * @since 5.6.8
+ */
+ private static function plan_matches( string $current_plan, string $required_plan, bool $exact ): bool {
+ if ( '' === $current_plan ) {
+ return false;
+ }
+
+ $required_plan = strtolower( $required_plan );
+
+ if ( $current_plan === $required_plan ) {
+ return true;
+ }
+
+ if ( $exact ) {
+ return false;
+ }
+
+ $current_order = array_search( $current_plan, self::PLANS, true );
+ $required_order = array_search( $required_plan, self::PLANS, true );
+
+ if ( false === $current_order || false === $required_order ) {
+ return false;
+ }
+
+ return $current_order > $required_order;
+ }
+
+ /**
+ * Checks whether the active paid plan matches a plan.
+ *
+ * @param string $plan - Plan name from self::PLANS.
+ * @param bool $exact - Whether only the plan itself matches, not higher plans.
+ *
+ * @return bool $matches - True when the plan matches.
+ *
+ * @since 5.6.8
+ */
+ public static function is_plan( string $plan, bool $exact = false ): bool {
+ $provider = self::get_provider();
+
+ if ( null === $provider ) {
+ return false;
+ }
+
+ return self::plan_matches( $provider::get_plan_name(), $plan, $exact );
+ }
+
+ /**
+ * Checks whether the active paid plan or trial plan matches a plan.
+ *
+ * @param string $plan - Plan name from self::PLANS.
+ * @param bool $exact - Whether only the plan itself matches, not higher plans.
+ *
+ * @return bool $matches - True when the plan or trial plan matches.
+ *
+ * @since 5.6.8
+ */
+ public static function is_plan_or_trial( string $plan, bool $exact = false ): bool {
+ if ( self::is_plan( $plan, $exact ) ) {
+ return true;
+ }
+
+ $provider = self::get_provider();
+
+ if ( null === $provider ) {
+ return false;
+ }
+
+ return self::plan_matches( $provider::get_trial_plan_name(), $plan, $exact );
+ }
+
+ /**
+ * Refreshes the cached license state from the active provider.
+ *
+ * @return bool $is_synchronized - True when synchronization completes.
+ *
+ * @since 5.6.8
+ */
+ public static function sync_license(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::sync_license();
+ }
+
+ /**
+ * Deletes locally cached license data and the stored provider choice.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function delete_local_license_data() {
+ $provider = self::get_provider();
+
+ if ( null !== $provider ) {
+ $provider::delete_local_license_data();
+ }
+
+ delete_site_option( self::PROVIDER_OPTION );
+
+ self::$is_provider_resolved = false;
+ }
+
+ /**
+ * Activates a license key with the provider matching the key format.
+ *
+ * The provider choice is stored only after a successful activation, so a rejected key
+ * never changes the selected provider.
+ *
+ * @param string $license_key - License key to activate.
+ *
+ * @return bool|WP_Error $result - True when the license is activated, or the reason it failed.
+ *
+ * @since 5.6.8
+ */
+ public static function activate_license( string $license_key ) {
+ $license_key = trim( $license_key );
+
+ if ( '' === $license_key ) {
+ return new WP_Error( 'wsal_license_key_missing', __( 'Please enter a license key.', 'wp-security-audit-log' ) );
+ }
+
+ $provider_name = self::detect_provider_name( $license_key );
+
+ if ( '' === $provider_name ) {
+ return new WP_Error( 'wsal_license_key_invalid', __( 'This license key is not valid. Please check it and try again.', 'wp-security-audit-log' ) );
+ }
+
+ $provider = self::get_available_provider( $provider_name );
+
+ if ( null === $provider ) {
+ return new WP_Error( 'wsal_license_provider_unavailable', __( 'This type of license key is not supported by this version of WP Activity Log.', 'wp-security-audit-log' ) );
+ }
+
+ $result = $provider::activate_license( $license_key );
+
+ if ( is_wp_error( $result ) ) {
+ return $result;
+ }
+
+ if ( true !== $result ) {
+ return new WP_Error( 'wsal_license_activation_failed', __( 'The license could not be activated.', 'wp-security-audit-log' ) );
+ }
+
+ update_site_option( self::PROVIDER_OPTION, $provider_name );
+
+ self::$is_provider_resolved = false;
+
+ return true;
+ }
+
+ /**
+ * Deactivates the license through the active provider.
+ *
+ * @return bool $is_deactivated - True when deactivation completes.
+ *
+ * @since 5.6.8
+ */
+ public static function deactivate_license(): bool {
+ $provider = self::get_provider();
+
+ return null !== $provider && $provider::deactivate_license();
+ }
+
+ /**
+ * Registers the callback that removes plugin data when the plugin is uninstalled.
+ *
+ * @param callable $callback - Uninstall callback.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function register_uninstall_handler( callable $callback ) {
+ $provider = self::get_provider();
+
+ if ( null !== $provider ) {
+ $provider::register_uninstall_handler( $callback );
+ }
+ }
+
+ /**
+ * Returns the provider support contact form markup.
+ *
+ * @return string $form - Contact form markup, or an empty string when unavailable.
+ *
+ * @since 5.6.8
+ */
+ public static function get_support_contact_form(): string {
+ $provider = self::get_provider();
+
+ return null !== $provider ? $provider::get_support_contact_form() : '';
+ }
+
+ /**
+ * Applies provider-specific setup for MainWP stealth mode.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ public static function prepare_mainwp_stealth_mode() {
+ $provider = self::get_provider();
+
+ if ( null !== $provider ) {
+ $provider::prepare_mainwp_stealth_mode();
+ }
+ }
+ }
+}
--- a/wp-security-audit-log/classes/Licensing/class-licensing-helper.php
+++ b/wp-security-audit-log/classes/Licensing/class-licensing-helper.php
@@ -0,0 +1,312 @@
+<?php
+/**
+ * Licensing helper for WP Activity Log.
+ *
+ * @package wsal
+ * @subpackage licensing
+ *
+ * @since 5.6.8
+ */
+
+declare(strict_types=1);
+
+namespace WSALLicensing;
+
+// Exit if accessed directly.
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+if ( ! class_exists( 'WSALLicensingLicensing_Helper' ) ) {
+
+ /**
+ * Helper methods for licensing that work only on the values they receive.
+ *
+ * They read and store nothing, so licensing classes and tests can use them freely.
+ *
+ * @since 5.6.8
+ */
+ class Licensing_Helper {
+
+ /**
+ * Error codes of store requests that got no answer, or one that could not be read.
+ *
+ * @var string[]
+ *
+ * @since 5.6.8
+ */
+ private const UNANSWERED_ERROR_CODES = array(
+ 'store_unreachable',
+ 'store_invalid_response',
+ );
+
+ /**
+ * Returns the expiry time of a license.
+ *
+ * @param array $license_data - License data, with the expiry date in 'expires'.
+ *
+ * @return int|false $expires_at - Expiry time as a Unix timestamp, or false for lifetime licenses and missing or unreadable dates.
+ *
+ * @since 5.6.8
+ */
+ public static function get_expiry_timestamp( array $license_data ) {
+ $expires = (string) ( $license_data['expires'] ?? '' );
+
+ if ( '' === $expires || 'lifetime' === $expires ) {
+ return false;
+ }
+
+ return strtotime( $expires );
+ }
+
+ /**
+ * Checks whether the premium settings of a network must be read-only.
+ *
+ * An expired license, or a valid license that does not cover every site, keeps premium features
+ * running but makes their settings read-only. Every other status leaves premium code off.
+ *
+ * @param bool $has_license_key - Whether a license key is stored.
+ * @param string $status - License status reported by the store.
+ * @param bool $is_over_limit - Whether the network has more sites than the license covers.
+ *
+ * @return bool $is_frozen - True when premium settings must be read-only.
+ *
+ * @since 5.6.8
+ */
+ public static function is_premium_frozen_multisite( bool $has_license_key, string $status, bool $is_over_limit ): bool {
+ if ( ! $has_license_key ) {
+ return false;
+ }
+
+ if ( 'expired' === $status ) {
+ return true;
+ }
+
+ return 'valid' === $status && $is_over_limit;
+ }
+
+ /**
+ * Returns the URLs of the sites with an active activation.
+ *
+ * @param array $activations - Activation entries keyed by site URL.
+ *
+ * @return string[] $site_urls - Site URLs.
+ *
+ * @since 5.6.8
+ */
+ public static function get_active_site_urls_multisite( array $activations ): array {
+ $site_urls = array();
+
+ foreach ( $activations as $site_url => $activation ) {
+ if ( 'active' === ( $activation['status'] ?? '' ) ) {
+ $site_urls[] = (string) $site_url;
+ }
+ }
+
+ return $site_urls;
+ }
+
+ /**
+ * Returns the URLs of the network sites without an active activation.
+ *
+ * @param string[] $sites - Site URLs of the network.
+ * @param array $activations - Activation entries keyed by site URL.
+ *
+ * @return string[] $site_urls - Site URLs without an active activation, empty when every site is activated.
+ *
+ * @since 5.6.8
+ */
+ public static function get_unlicensed_site_urls_multisite( array $sites, array $activations ): array {
+ $site_urls = array();
+
+ foreach ( $sites as $site_url ) {
+ if ( 'active' !== ( $activations[ $site_url ]['status'] ?? '' ) ) {
+ $site_urls[] = $site_url;
+ }
+ }
+
+ return $site_urls;
+ }
+
+ /**
+ * Finds the stored activation URL of a site by its domain and path.
+ *
+ * The letter case of the domain and a trailing slash in the path are ignored.
+ *
+ * @param array $activations - Activation entries keyed by site URL.
+ * @param string $domain - Site domain.
+ * @param string $path - Site path.
+ *
+ * @return string $site_url - Matching activation URL, or an empty string when none matches.
+ *
+ * @since 5.6.8
+ */
+ public static function find_activation_url_multisite( array $activations, string $domain, string $path ): string {
+ $wanted = strtolower( $domain ) . untrailingslashit( $path );
+
+ foreach ( array_keys( $activations ) as $site_url ) {
+ $host = (string) wp_parse_url( (string) $site_url, PHP_URL_HOST );
+ $site_path = (string) wp_parse_url( (string) $site_url, PHP_URL_PATH );
+
+ if ( strtolower( $host ) . untrailingslashit( $site_path ) === $wanted ) {
+ return (string) $site_url;
+ }
+ }
+
+ return '';
+ }
+
+ /**
+ * Returns the sites a batch still has to activate.
+ *
+ * A site needs an activation when it has no active one, and is skipped when the batch already tried it.
+ *
+ * @param string[] $sites - Site URLs of the network.
+ * @param array $activations - Activation entries keyed by site URL.
+ * @param string $batch_id - ID of the batch.
+ *
+ * @return string[] $site_urls - Site URLs to activate.
+ *
+ * @since 5.6.8
+ */
+ public static function get_sites_to_activate_multisite( array $sites, array $activations, string $batch_id ): array {
+ $site_urls = array();
+
+ foreach ( $sites as $site_url ) {
+ $status = $activations[ $site_url ]['status'] ?? '';
+ $tried_in_batch = $activations[ $site_url ]['batch'] ?? '';
+
+ if ( 'active' === $status || $batch_id === $tried_in_batch ) {
+ continue;
+ }
+
+ $site_urls[] = $site_url;
+ }
+
+ return $site_urls;
+ }
+
+ /**
+ * Builds the stored activation entry for an activation result.
+ *
+ * @param bool|WP_Error $result - Result of EDD_Provider::try_activate_license().
+ * @param string $batch_id - ID of the batch that tried the site, empty outside a batch.
+ *
+ * @return array $entry - Activation entry with 'status', 'batch', and 'activated_at', or 'code' and 'error'.
+ *
+ * @since 5.6.8
+ */
+ public static function get_activation_entry_multisite( $result, string $batch_id = '' ): array {
+ if ( true === $result ) {
+ return array(
+ 'status' => 'active',
+ 'batch' => $batch_id,
+ 'activated_at' => time(),
+ );
+ }
+
+ return array(
+ 'status' => 'failed',
+ 'batch' => $batch_id,
+ 'code' => is_wp_error( $result ) ? $result->get_error_code() : '',
+ 'error' => is_wp_error( $result ) ? $result->get_error_message() : __( 'Unknown error', 'wp-security-audit-log' ),
+ );
+ }
+
+ /**
+ * Returns the sites whose last activation attempt got no answer from the store.
+ *
+ * The store may have activated these sites although its answer never arrived. Activating a site
+ * the store already counts uses no free activation, so they can be tried again at any time.
+ *
+ * @param array $activations - Activation entries keyed by site URL.
+ *
+ * @return string[] $site_urls - Site URLs.
+ *
+ * @since 5.6.8
+ */
+ public static function get_unanswered_site_urls_multisite( array $activations ): array {
+ $site_urls = array();
+
+ foreach ( $activations as $site_url => $activation ) {
+ if ( 'failed' === ( $activation['status'] ?? '' ) && in_array( $activation['code'] ?? '', self::UNANSWERED_ERROR_CODES, true ) ) {
+ $site_urls[] = (string) $site_url;
+ }
+ }
+
+ return $site_urls;
+ }
+
+ /**
+ * Returns the number of available activations from a store answer.
+ *
+ * @param array $license_data - License data returned by the store.
+ *
+ * @return int|false $activations - Available activations, PHP_INT_MAX for unlimited, or false when unknown.
+ *
+ * @since 5.6.8
+ */
+ public static function get_available_activations( array $license_data ) {
+ if ( isset( $license_data['activations_left'] ) ) {
+ if ( 'unlimited' === $license_data['activations_left'] ) {
+ return PHP_INT_MAX;
+ }
+
+ return (int) $license_data['activations_left'];
+ }
+
+ // A license limit of 0 means unlimited.
+ if ( isset( $license_data['license_limit'] ) && 0 === (int) $license_data['license_limit'] ) {
+ return PHP_INT_MAX;
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns the message shown when a network has more sites than the license has activations available.
+ *
+ * @param string $plugin_name - Plugin name shown to the user.
+ * @param int $site_count - Number of sites of the network.
+ * @param int $available_activations - Activations the license has available.
+ *
+ * @return string $message - Plain text message, escaped where it is printed.
+ *
+ * @since 5.6.8
+ */
+ public static function get_insufficient_activations_message_multisite( string $plugin_name, int $site_count, int $available_activations ): string {
+ return sprintf(
+ /* translators: 1: number of sites of the network, 2: plugin name, 3: available license activations */
+ _n( 'This network has %1$d sites, but your %2$s license only has %3$d activation available. Upgrade your license or remove sites from the network, then try again.', 'This network has %1$d sites, but your %2$s license only has %3$d activations available. Upgrade your license or remove sites from the network, then try again.', $available_activations, 'wp-security-audit-log' ),
+ $site_count,
+ $plugin_name,
+ $available_activations
+ );
+ }
+
+ /**
+ * Swaps the render and save callbacks of premium settings tabs, so they show the read-only notice and save nothing.
+ *
+ * @param array $tabs - Settings tabs keyed by tab ID, each with 'render' and 'save' callbacks.
+ * @param string[] $tab_ids - IDs of the premium tabs.
+ * @param callable $render_notice - Callback that prints the read-only notice.
+ *
+ * @return array $tabs - Settings tabs, with the premium tabs marked 'is_frozen'.
+ *
+ * @since 5.6.8
+ */
+ public static function make_tabs_read_only( array $tabs, array $tab_ids, callable $render_notice ): array {
+ foreach ( $tab_ids as $tab_id ) {
+ if ( ! isset( $tabs[ $tab_id ] ) ) {
+ continue;
+ }
+
+ $tabs[ $tab_id ]['render'] = $render_notice;
+ $tabs[ $tab_id ]['save'] = '__return_null';
+ $tabs[ $tab_id ]['is_frozen'] = true;
+ }
+
+ return $tabs;
+ }
+ }
+}
--- a/wp-security-audit-log/classes/Licensing/index.php
+++ b/wp-security-audit-log/classes/Licensing/index.php
@@ -0,0 +1,6 @@
+<?php
+/**
+ * Nothing to see here.
+ *
+ * @package wsal
+ */
--- a/wp-security-audit-log/classes/ListAdminEvents/class-list-events.php
+++ b/wp-security-audit-log/classes/ListAdminEvents/class-list-events.php
@@ -26,6 +26,7 @@
use WSALEntitiesMetadata_Entity;
use WSALControllersAlert_Manager;
use WSALEntitiesOccurrences_Entity;
+use WSALLicensingLicensing_Factory;
use WSALCopyEventDataCopy_Event_Data;
use WSALHelpersDateTime_Formatter_Helper;
@@ -159,6 +160,7 @@
* @return bool $is_premium_search_available - True when Premium search is available.
*
* @since 5.6.7
+ * @since 5.6.8 - Uses Licensing_Factory for the plan check.
*/
private static function is_premium_search_available(): bool {
$is_premium_search_available = false;
@@ -855,6 +857,7 @@
* Returns an associative array containing the bulk actions.
*
* @since 4.6.0
+ * @since 5.6.8 - Uses Licensing_Factory for the free plan check.
*
* @return array
*/
--- a/wp-security-audit-log/classes/MainWPAddon/class-mainwp-api.php
+++ b/wp-security-audit-log/classes/MainWPAddon/class-mainwp-api.php
@@ -18,6 +18,7 @@
use WSALEntitiesBase_Fields;
use WSALControllersAlert_Manager;
use WSALEntitiesOccurrences_Entity;
+use WSALLicensingLicensing_Factory;
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
@@ -73,6 +74,7 @@
* @return array
*
* @since 5.0.0
+ * @since 5.6.8 - Uses Licensing_Factory for the Premium build check.
*/
protected static function handle_wsal_info_check(): array {
$info = array();
--- a/wp-security-audit-log/classes/Migration/class-migration.php
+++ b/wp-security-audit-log/classes/Migration/class-migration.php
@@ -21,6 +21,7 @@
use WSALEntitiesReports_Entity;
use WSALHelpersSettings_Helper;
use WSALEntitiesMetadata_Entity;
+use WSALLicensingLicensing_Factory;
use WSALEntitiesOccurrences_Entity;
use WSALControllersPlugin_Extensions;
use WSALLoggersWSAL_Ext_MirrorLogger;
@@ -82,6 +83,7 @@
* @return void
*
* @since 4.4.0
+ * @since 5.6.8 - Stopped loading Freemius and the External DB extension during the migration.
*/
protected static function migrate_up_to_4420() {
@@ -98,7 +100,6 @@
global $wsal_class;
$wsal = $wsal_class;
- $wsal::load_freemius();
$wsal::load_defaults();
$disabled_alerts = WP_Helper::get_global_option( 'disabled-alerts', false );
@@ -185,12 +186,8 @@
// Delete dev options from the settings.
WP_Helper::delete_global_option( 'dev-options' );
- // phpcs:disable
- // phpcs:enable
if ( class_exists( 'WSAL_Extension_Manager' ) ) {
- WSAL_Extension_Manager::include_extension( 'external-db' );
-
// Delete cron jobs related to mirror scheduling.
// This was previously a constant in WSAL_Ext_Plugin, but we removed it in version 4.3.
@@ -641,8 +638,6 @@
*/
protected static function migrate_up_to_4430() {
- // phpcs:disable
- // phpcs:enable
if ( class_exists( 'WSALLoggersWSAL_Ext_MirrorLogger' ) && method_exists( 'WSALHelpersSettings_Helper', 'get_working_dir_path_static' ) ) {
@@ -1333,6 +1328,28 @@
}
/**
+ * Migration for version 5.6.8
+ *
+ * Stores Freemius as the licensing provider of sites that already used it. Sites without a
+ * stored provider show the license form, so existing Freemius customers would otherwise be
+ * asked for a license key again.
+ *
+ * @return void
+ *
+ * @since 5.6.8
+ */
+ protected static function migrate_up_to_5680() {
+ if ( '' !== Licensing_Factory::get_stored_provider_name() ) {
+ return;
+ }
+
+ // Only sites still connected to Freemius: a disconnected site shows the license form instead.
+ if ( Licensing_Factory::has_freemius_license_data() && Licensing_Factory::is_freemius_connected() ) {
+ update_site_option( Licensing_Factory::PROVIDER_OPTION, Licensing_Factory::FREEMIUS_PROVIDER_NAME );
+ }
+ }
+
+ /**
* Remove some very old data from the database.
*
* @return void
--- a/wp-security-audit-log/classes/Views/Help.php
+++ b/wp-security-audit-log/classes/Views/Help.php
@@ -14,6 +14,7 @@
use WSALHelpersSettings_Helper;
use WSALEntitiesMetadata_Entity;
use WSALEntitiesOccurrences_Entity;
+use WSALLicensingLicensing_Factory;
use WSALHelpersPlugin_Settings_Helper;
// Exit if accessed directly.
@@ -205,7 +206,7 @@
</div>
<div class="wsal-help-section">
<h2 class="wsal-tab__heading"><?php esc_html_e( 'Do you need help or want to report an issue?', 'wp-security-audit-log' ); ?></h2>
- <?php if ( wsal_freemius()->is_premium() ) : ?>
+ <?php if ( Licensing_Factory::is_premium_build() ) : ?>
<p>
<?php esc_html_e( "If you've encountered a problem or would like to report an issue while using WP Activity Log, click the button below to open a support ticket. As a Premium user, you receive dedicated 1-to-1 email support.", 'wp-security-audit-log' ); ?>
</p>
@@ -271,11 +272,16 @@
.fs-full-size-wrapper {
margin: 10px 20px 0 2px !important;
}
+ /* The Freemius contact form frame has no scrollbar and Freemius can size it shorter than the form, so its last topics end up hidden under the cookie notice. */
+ #fs_frame iframe {
+ min-height: 996px;
+ }
</style>
<?php
- if ( $freemius_id = wsal_freemius()->get_id() ) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
- $vars = array( 'id' => $freemius_id );
- echo fs_get_template( 'contact.php', $vars ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ $contact_form = Licensing_Factory::get_support_contact_form();
+
+ if ( '' !== $contact_form ) {
+ echo $contact_form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
echo '<p>';
printf(
@@ -354,6 +360,8 @@
* Method: Get system information.
*
* @return string - System information.
+ *
+ * @since 5.6.8 - Leaves out the license options, so the license key is never shown in the system information.
*/
public function get_sysinfo() {
// System info.
@@ -429,15 +437,15 @@
// WordPress active plugins.
$sysinfo .= "n" . '-- WordPress Active Plugins --' . "nn";
- $plugins = get_plugins();
- $active_plugins = get_option( 'active_plugins', array() );
- $can_use_freemius_premium_code = wsal_freemius()->can_use_premium_code();
+ $plugins = get_plugins();
+ $active_plugins = get_option( 'active_plugins', array() );
+ $can_use_premium_code = Licensing_Factory::can_use_premium_code();
foreach ( $plugins as $plugin_path => $plugin ) {
if ( ! in_array( $plugin_path, $active_plugins ) ) { // phpcs:ignore
continue;
}
- if ( 'WP Activity Log' === $plugin['Name'] && $can_use_freemius_premium_code ) {
+ if ( 'WP Activity Log' === $plugin['Name'] && $can_use_premium_code ) {
$update = ( array_key_exists( $plugin_path, $updates ) ) ? ' (needs update - ' . $updates[ $plugin_path ]->update->new_version . ')' : '';
$sysinfo .= $plugin['Name'] . ' Premium: ' . $plugin['Version'] . $update . "n";
} else {
@@ -472,7 +480,7 @@
continue;
}
- if ( 'WP Activity Log' === $plugin['Name'] && $can_use_freemius_premium_code ) {
+ if ( 'WP Activity Log' === $plugin['Name'] && $can_use_premium_code ) {
$update = ( array_key_exists( $plugin_path, $updates ) ) ? ' (needs update - ' . $updates[ $plugin_path ]->update->new_version . ')' : '';
$plugin = get_plugin_data( $plugin_path );
$sysinfo .= $plugin['Name'] . ' Premium: ' . $plugin['Version'] . $update . "n";
@@ -512,6 +520,11 @@
if ( ! empty( $options ) && is_array( $options ) ) {
foreach ( $options as $option ) {
+ // If the option is a license option, skip it, we don't want to show it in this page.
+ if ( Licensing_Factory::is_license_option( (string) $option->option_name ) ) {
+ continue;
+ }
+
$sysinfo .= 'Option: ' . $option->option_name . "n";
$sysinfo .= 'Value: ' . $option->option_value . "nn";
}
--- a/wp-security-audit-log/classes/Views/Settings.php
+++ b/wp-security-audit-log/classes/Views/Settings.php
@@ -13,13 +13,13 @@
use WSALHelpersUser_Helper;
use WSALHelpersEmail_Helper;
use WSALControllersConstants;
-use WSALControllersConnection;
use WSALHelpersSettings_Helper;
use WSALControllersAlert_Manager;
+use WSALLicensingLicensing_Helper;
use WSALEntitiesOccurrences_Entity;
+use WSALLicensingLicensing_Factory;
use WSALControllersDatabase_Manager;
use WSALHelpersPlugin_Settings_Helper;
-use WSALEntitiesArchiveDelete_Records;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
@@ -39,6 +39,18 @@
class WSAL_Views_Settings extends WSAL_AbstractView {
/**
+ * Premium settings tabs that show the read-only license notice in place of their content.
+ *
+ * @var string[]
+ *
+ * @since 5.6.8
+ */
+ public const PREMIUM_TABS = array(
+ 'settings-export-import',
+ 'logs-management',
+ );
+
+ /**
* Adapter Message.
*
* @var string
@@ -86,12 +98,17 @@
add_action( 'wp_ajax_wsal_ajax_get_all_event_types', array( $this, 'ajax_get_all_event_types' ) );
add_action( 'wp_ajax_wsal_ajax_get_all_object_types', array( $this, 'ajax_get_all_object_types' ) );
add_action( 'wp_ajax_wsal_ajax_get_all_event_ids', array( $this, 'ajax_get_all_event_ids' ) );
+
+ add_filter( 'removable_query_args', array( __CLASS__, 'add_removable_pruning_query_arg' ) );
}
/**
* Setup WSAL Settings Page Tabs.
*
+ * @return void
+ *
* @since 3.4
+