Notices). * * Privacy: once a day, in WP-Cron, the plugin asks aiolosweb.com for notices, sending only the * plugin's edition and version, the site language and the WordPress/WooCommerce versions; never * the site's address or any data about the shop or its customers. The site owner can switch it * off (Aiolos Checkout > General). * * Safety: the answer is accepted only when signed (Ed25519) with a key this plugin carries, over * "aiolos-notices-v1\n" + payload, so it can never be mistaken for, or made from, a license answer. * Notices are plain text: nothing the server sends is printed as HTML, and buttons may only link * to aiolosweb.com. Even a compromised server cannot put script or a foreign link in a shop's admin. * * Restraint (WordPress.org guidelines): at most one notice per screen, only to shop managers, * every notice can be dismissed and stays dismissed for that user. */ final class Notices { public const ENDPOINT = 'https://aiolosweb.com/wp-json/aiolos-license/v1/notices'; public const CONTEXT = "aiolos-notices-v1\n"; public const OPTION = 'aiolos_remote_notices'; public const CRON = 'aiolos_fetch_notices'; public const DISMISSED = 'aiolos_dismissed_notices'; public const ALLOWED_HOST = 'aiolosweb.com'; /** * Public keys the notices must be signed with: kid => base64 Ed25519 public key (the * aiolosweb.com signing key). */ private const KEYS = array( '879712dc' => 'n0x7NZAIlIUsHJPcP5SJiwILGQUWEyf7cLg+t/5nqlo=', ); /** * Hooks. */ public static function init(): void { add_action( self::CRON, array( self::class, 'fetch' ) ); if ( ! is_admin() ) { return; } add_action( 'admin_init', array( self::class, 'schedule' ) ); add_action( 'admin_notices', array( self::class, 'render' ) ); add_action( 'wp_ajax_aiolos_dismiss_notice', array( self::class, 'dismiss' ) ); } /** * Whether notices are on. */ public static function enabled(): bool { /** * Filters whether the plugin asks aiolosweb.com for notices. * * @since 1.0.0 * * @param bool $enabled Whether it does. */ return (bool) apply_filters( 'aiolos_remote_notices_enabled', Settings::enabled( 'notices_enabled' ) ); } /** * Schedules (or clears) the daily fetch. */ public static function schedule(): void { $next = wp_next_scheduled( self::CRON ); if ( self::enabled() && ! $next ) { wp_schedule_event( time() + wp_rand( 60, HOUR_IN_SECONDS ), 'daily', self::CRON ); } elseif ( ! self::enabled() && $next ) { wp_clear_scheduled_hook( self::CRON ); delete_option( self::OPTION ); } } /** * Removes the schedule (deactivation). */ public static function unschedule(): void { wp_clear_scheduled_hook( self::CRON ); } /** * The edition and version this site runs. * * @return array [ edition, version ] */ private static function edition(): array { return defined( 'ACWP_VERSION' ) ? array( 'pro', (string) ACWP_VERSION ) : array( 'free', (string) AIOLOS_CHECKOUT_VERSION ); } /** * Where the notices come from: aiolosweb.com; another server only on local and development sites * (tests against a development license server). */ private static function endpoint(): string { if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { /** * Filters the notices endpoint, on local and development sites only. * * @since 1.0.0 * * @param string $url Endpoint. */ return (string) apply_filters( 'aiolos_notices_dev_endpoint', self::ENDPOINT ); } return self::ENDPOINT; } /** * Asks aiolosweb.com for notices (WP-Cron). */ public static function fetch(): void { if ( ! self::enabled() ) { return; } list( $edition, $version ) = self::edition(); $url = add_query_arg( array( 'plugin' => 'aiolos-checkout', 'edition' => $edition, 'version' => $version, 'locale' => substr( (string) get_locale(), 0, 5 ), 'wp' => get_bloginfo( 'version' ), 'wc' => defined( 'WC_VERSION' ) ? WC_VERSION : '', ), self::endpoint() ); $response = wp_remote_get( $url, array( 'timeout' => 5, 'redirection' => 1, // WordPress's default user agent carries the site's address; this one does not. 'user-agent' => 'AiolosCheckout/' . AIOLOS_CHECKOUT_VERSION, 'headers' => array( 'Accept' => 'application/json' ), ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return; // The notices fetched last time stay until they end. } $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $payload = is_array( $body ) ? self::verify( $body ) : null; if ( null === $payload || 'aiolos-checkout' !== ( $payload['plugin'] ?? '' ) ) { return; } $notices = array(); foreach ( (array) ( $payload['notices'] ?? array() ) as $notice ) { $clean = is_array( $notice ) ? self::clean( $notice ) : null; if ( $clean ) { $notices[] = $clean; } if ( count( $notices ) >= 10 ) { break; } } update_option( self::OPTION, array( 'fetched_at' => time(), 'notices' => $notices, ), false ); } /** * The payload of a signed answer, or null. * * @param array $signed {kid, payload, signature} (base64url). */ public static function verify( array $signed ): ?array { $kid = (string) ( $signed['kid'] ?? '' ); $keys = self::KEYS; if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { /** * Filters the notice signing keys, on local and development sites only (tests against a * development license server). * * @since 1.0.0 * * @param array $keys kid => base64 Ed25519 public key. */ $keys = (array) apply_filters( 'aiolos_notices_dev_keys', $keys ); } if ( ! isset( $keys[ $kid ] ) ) { return null; } if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) && is_readable( ABSPATH . WPINC . '/sodium_compat/autoload.php' ) ) { require_once ABSPATH . WPINC . '/sodium_compat/autoload.php'; } $public = base64_decode( (string) $keys[ $kid ], true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- a public key. $json = self::b64url_decode( (string) ( $signed['payload'] ?? '' ) ); $signature = self::b64url_decode( (string) ( $signed['signature'] ?? '' ) ); if ( false === $public || 32 !== strlen( $public ) || 64 !== strlen( $signature ) || '' === $json ) { return null; } try { $valid = sodium_crypto_sign_verify_detached( $signature, self::CONTEXT . $json, $public ); } catch ( \Throwable $e ) { return null; } $payload = $valid ? json_decode( $json, true ) : null; if ( ! is_array( $payload ) || (int) ( $payload['issued_at'] ?? 0 ) > time() + DAY_IN_SECONDS ) { return null; } return $payload; } /** * Decodes base64url. * * @param string $data Data. */ private static function b64url_decode( string $data ): string { $raw = base64_decode( strtr( $data, '-_', '+/' ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- signed payloads. return false === $raw ? '' : $raw; } /** * A notice reduced to what may be shown: plain text, a known type and screens, an * aiolosweb.com link. Null when it cannot be shown. * * @param array $notice Notice from the server. */ public static function clean( array $notice ): ?array { $id = sanitize_key( (string) ( $notice['id'] ?? '' ) ); $title = sanitize_text_field( (string) ( $notice['title'] ?? '' ) ); $message = sanitize_textarea_field( (string) ( $notice['message'] ?? '' ) ); if ( '' === $id || ( '' === $title && '' === $message ) ) { return null; } $type = in_array( $notice['type'] ?? '', array( 'info', 'success', 'warning', 'promo' ), true ) ? (string) $notice['type'] : 'info'; $where = array_values( array_intersect( (array) ( $notice['where'] ?? array( 'plugin' ) ), array( 'plugin', 'dashboard', 'plugins' ) ) ); $cta = null; if ( ! empty( $notice['cta']['url'] ) && ! empty( $notice['cta']['label'] ) && self::allowed_url( (string) $notice['cta']['url'] ) ) { $cta = array( 'label' => sanitize_text_field( (string) $notice['cta']['label'] ), 'url' => esc_url_raw( (string) $notice['cta']['url'], array( 'https' ) ), ); } return array( 'id' => substr( $id, 0, 64 ), 'type' => $type, 'title' => mb_substr( $title, 0, 140 ), 'message' => mb_substr( $message, 0, 600 ), 'cta' => $cta, 'where' => $where ? $where : array( 'plugin' ), 'starts_at' => (int) ( $notice['starts_at'] ?? 0 ), 'ends_at' => (int) ( $notice['ends_at'] ?? 0 ), 'editions' => array_values( array_intersect( (array) ( $notice['editions'] ?? array( 'free', 'pro' ) ), array( 'free', 'pro' ) ) ), 'min_version' => preg_replace( '/[^0-9.]/', '', (string) ( $notice['min_version'] ?? '' ) ), 'max_version' => preg_replace( '/[^0-9.]/', '', (string) ( $notice['max_version'] ?? '' ) ), 'dismissible' => false !== ( $notice['dismissible'] ?? true ), ); } /** * Whether a link may be shown: https on aiolosweb.com or one of its subdomains. * * @param string $url URL. */ public static function allowed_url( string $url ): bool { $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || 'https' !== strtolower( (string) ( $parts['scheme'] ?? '' ) ) || ! empty( $parts['user'] ) || ! empty( $parts['pass'] ) ) { return false; } $host = strtolower( (string) ( $parts['host'] ?? '' ) ); return self::ALLOWED_HOST === $host || substr( $host, -strlen( '.' . self::ALLOWED_HOST ) ) === '.' . self::ALLOWED_HOST; } /** * The notice to show on the current screen, if any. */ private static function current(): ?array { if ( ! self::enabled() || ! current_user_can( Admin::CAPABILITY ) ) { return null; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; // Only on this plugin's own settings screen. $map = array( 'toplevel_page_' . Admin::PAGE => 'plugin', ); $where = $screen && isset( $map[ $screen->id ] ) ? $map[ $screen->id ] : ''; if ( '' === $where ) { return null; } $stored = get_option( self::OPTION, array() ); $dismissed = (array) get_user_meta( get_current_user_id(), self::DISMISSED, true ); list( $edition, $version ) = self::edition(); $now = time(); foreach ( (array) ( $stored['notices'] ?? array() ) as $notice ) { if ( ! is_array( $notice ) ) { continue; } // A stored notice of another shape (an old version, a damaged option) is read safely. $notice = array_merge( array( 'id' => '', 'starts_at' => 0, 'ends_at' => 0, 'min_version' => '', 'max_version' => '', ), $notice ); $notice['where'] = (array) ( $notice['where'] ?? array() ); $notice['editions'] = (array) ( $notice['editions'] ?? array() ); if ( ! in_array( $where, $notice['where'], true ) || in_array( $notice['id'], $dismissed, true ) || ! in_array( $edition, $notice['editions'], true ) ) { continue; } if ( ( $notice['starts_at'] && $now < $notice['starts_at'] ) || ( $notice['ends_at'] && $now > $notice['ends_at'] ) ) { continue; } if ( ( '' !== $notice['min_version'] && version_compare( $version, $notice['min_version'], '<' ) ) || ( '' !== $notice['max_version'] && version_compare( $version, $notice['max_version'], '>' ) ) ) { continue; } return $notice; } return null; } /** * Prints the notice of this screen. */ public static function render(): void { $notice = self::current(); if ( ! $notice ) { return; } $class = array( 'info' => 'notice-info', 'success' => 'notice-success', 'warning' => 'notice-warning', 'promo' => 'notice-info acw-promo', )[ $notice['type'] ]; $url = $notice['cta'] ? add_query_arg( array( 'utm_source' => 'aiolos-checkout', 'utm_medium' => 'admin-notice', 'utm_campaign' => $notice['id'], ), $notice['cta']['url'] ) : ''; ?>