hook_suffix = (string) $hook; } /** * Register the option with the Settings API. */ public function register_settings(): void { register_setting( self::GROUP, Options::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => Options::defaults(), ) ); } /** * Add a Settings link on the Plugins screen. * * @param string[] $links Existing action links. * @return string[] */ public function add_action_link( array $links ): array { $url = add_query_arg( 'page', self::PAGE, admin_url( 'admin.php' ) ); array_unshift( $links, '' . esc_html__( 'Settings', 'styleraise' ) . '' ); return $links; } /** * Sanitize the submitted settings. * * @param mixed $input Raw submitted value. * @return array */ public function sanitize( $input ): array { $clean = Options::defaults(); if ( ! is_array( $input ) ) { return $clean; } $registry = array_keys( Plugin::instance()->get_widget_registry() ); $enabled = isset( $input['enabled_widgets'] ) && is_array( $input['enabled_widgets'] ) ? array_map( 'sanitize_key', $input['enabled_widgets'] ) : array(); // The form submits what is *on*; storing what is *off* keeps a widget // added in a future release enabled by default rather than silently // hidden. $clean['disabled_widgets'] = array_values( array_diff( $registry, $enabled ) ); $clean = $this->sanitize_freshness( $input, $clean ); // A checkbox: absent from the submission means switched off. $clean['editor_form_preview'] = ! empty( $input['editor_form_preview'] ); // Turning a widget off changes which controls exist, so the generated // per-page CSS has to be rebuilt. $this->clear_elementor_cache(); add_settings_error( Options::OPTION, 'wcdp_el_saved', esc_html__( 'Settings saved. Elementor\'s generated CSS was rebuilt.', 'styleraise' ), 'success' ); return $clean; } /** * Validate the freshness block of the form. * * @param array $input Raw submitted values. * @param array $clean Values collected so far. * @return array */ private function sanitize_freshness( array $input, array $clean ): array { $pattern = isset( $input['freshness_pattern'] ) ? sanitize_key( $input['freshness_pattern'] ) : 'balanced'; if ( 'custom' !== $pattern && ! isset( Freshness::patterns()[ $pattern ] ) ) { $pattern = 'balanced'; } $clean['freshness_pattern'] = $pattern; $hydrate = isset( $input['freshness_hydrate'] ) ? sanitize_key( $input['freshness_hydrate'] ) : 'focus'; if ( ! in_array( $hydrate, array( Freshness::HYDRATE_OFF, Freshness::HYDRATE_LOAD, Freshness::HYDRATE_FOCUS, Freshness::HYDRATE_INTERVAL ), true ) ) { $hydrate = Freshness::HYDRATE_FOCUS; } $purge = isset( $input['freshness_purge'] ) ? sanitize_key( $input['freshness_purge'] ) : 'index'; if ( ! in_array( $purge, array( Freshness::PURGE_OFF, Freshness::PURGE_INDEX, Freshness::PURGE_URLS, Freshness::PURGE_ALL ), true ) ) { $purge = Freshness::PURGE_INDEX; } $clean['freshness_hydrate'] = $hydrate; $clean['freshness_interval'] = max( 10, min( 3600, absint( $input['freshness_interval'] ?? 30 ) ) ); $clean['freshness_purge'] = $purge; $clean['freshness_purge_throttle'] = max( 0, min( 3600, absint( $input['freshness_purge_throttle'] ?? 60 ) ) ); $clean['freshness_data_ttl'] = max( 0, min( 6 * HOUR_IN_SECONDS, absint( $input['freshness_data_ttl'] ?? 0 ) ) ); $clean['freshness_force_recalc'] = ! empty( $input['freshness_force_recalc'] ); // Stored as submitted text: one URL or path per line, resolved against // the site address only when it is used, so the list survives a domain // change. $urls = isset( $input['freshness_purge_urls'] ) ? (string) $input['freshness_purge_urls'] : ''; $urls = preg_split( '/[\r\n]+/', $urls ) ?: array(); $urls = array_filter( array_map( 'trim', array_map( 'sanitize_text_field', $urls ) ) ); $clean['freshness_purge_urls'] = implode( "\n", array_slice( array_values( $urls ), 0, 50 ) ); $guard = isset( $input['script_guard'] ) ? sanitize_key( $input['script_guard'] ) : 'auto'; $clean['script_guard'] = in_array( $guard, array( 'auto', 'attributes', 'off' ), true ) ? $guard : 'auto'; Freshness::flush(); return $clean; } /** * Handle the cache-flush button. */ public function handle_flush(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'styleraise' ), 403 ); } check_admin_referer( self::FLUSH_ACTION ); Product_Resolver::flush_options_cache(); $this->clear_elementor_cache(); wp_safe_redirect( add_query_arg( array( 'page' => self::PAGE, 'wcdp-el-msg' => 'flushed', ), admin_url( 'admin.php' ) ) ); exit; } /** * Drop Elementor's generated CSS files. */ private function clear_elementor_cache(): void { if ( ! class_exists( '\Elementor\Plugin' ) ) { return; } $elementor = \Elementor\Plugin::$instance; if ( isset( $elementor->files_manager ) ) { $elementor->files_manager->clear_cache(); } } /* ─────────────────────────── Rendering ─────────────────────────── */ /** * Render the screen. */ public function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $registry = Plugin::instance()->get_widget_registry(); ?> ' . esc_html__( 'Caches cleared.', 'styleraise' ) . ''; } if ( 'purged' === $message ) { echo '' . esc_html__( 'Page caches purged.', 'styleraise' ) . ''; } settings_errors( Options::OPTION ); ?> render_freshness_card(); ?> $class_name ) : ?> > get_widget_label( $class_name ) ); ?> > get_diagnostics() as $fact ) : ?> detected_caches_text() ); ?> get_title(); } return str_replace( '_', ' ', $class_name ); } /** * Environment facts worth knowing when something misbehaves. * * Each entry carries a state so the value can be rendered as a status pill * rather than a wall of identical grey text: `ok`, `warn` or `neutral`. * * @return array */ private function get_diagnostics(): array { $unknown = esc_html__( 'not detected', 'styleraise' ); $missing = array(); foreach ( array( 'wcdp_progress', 'wcdp_order_counter', 'wcdp_leaderboard', 'wcdp_donation_form' ) as $shortcode ) { if ( ! Shortcode_Runner::exists( $shortcode ) ) { $missing[] = $shortcode; } } $multilingual = esc_html__( 'none', 'styleraise' ); if ( function_exists( 'pll_get_post_language' ) ) { $multilingual = 'Polylang'; } elseif ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $multilingual = 'WPML'; } $analytics_on = 'no' !== get_option( 'woocommerce_analytics_enabled', 'yes' ); return array( array( 'label' => esc_html__( 'Elementor', 'styleraise' ), 'value' => defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : $unknown, 'state' => defined( 'ELEMENTOR_VERSION' ) ? 'ok' : 'warn', ), array( 'label' => esc_html__( 'WCDP', 'styleraise' ), 'value' => defined( 'WCDP_VERSION' ) ? WCDP_VERSION : $unknown, 'state' => defined( 'WCDP_VERSION' ) ? 'ok' : 'warn', ), array( 'label' => esc_html__( 'WooCommerce', 'styleraise' ), 'value' => defined( 'WC_VERSION' ) ? WC_VERSION : $unknown, 'state' => defined( 'WC_VERSION' ) ? 'ok' : 'warn', ), array( 'label' => esc_html__( 'WCDP shortcodes', 'styleraise' ), 'value' => empty( $missing ) ? esc_html__( 'all available', 'styleraise' ) : sprintf( /* translators: %s: comma-separated shortcode names. */ esc_html__( 'missing: %s', 'styleraise' ), implode( ', ', $missing ) ), 'state' => empty( $missing ) ? 'ok' : 'warn', ), array( 'label' => esc_html__( 'WooCommerce Analytics', 'styleraise' ), 'value' => $analytics_on ? esc_html__( 'enabled', 'styleraise' ) : esc_html__( 'disabled — figures will be wrong', 'styleraise' ), 'state' => $analytics_on ? 'ok' : 'warn', ), array( 'label' => esc_html__( 'Multilingual plugin', 'styleraise' ), 'value' => $multilingual, 'state' => 'neutral', ), array( 'label' => esc_html__( 'WordPress / PHP', 'styleraise' ), 'value' => get_bloginfo( 'version' ) . ' / ' . PHP_VERSION, 'state' => 'neutral', ), array( 'label' => esc_html__( 'Minimum required', 'styleraise' ), 'value' => sprintf( /* translators: 1: Elementor version, 2: WCDP version. */ esc_html__( 'Elementor %1$s · WCDP %2$s', 'styleraise' ), Requirements::MINIMUM_ELEMENTOR_VERSION, Requirements::MINIMUM_WCDP_VERSION ), 'state' => 'neutral', ), $this->freshness_fact(), $this->purge_fact(), $this->index_fact(), $this->script_guard_fact(), ); } /** * Diagnostics row describing form-script protection. * * @return array{label:string,value:string,state:string} */ private function script_guard_fact(): array { $mode = Script_Guard::mode(); $optimisers = Script_Guard::detected_optimisers(); $unreachable = array_filter( $optimisers, static function ( $reachable ) { return ! $reachable; } ); if ( 'off' === $mode ) { return array( 'label' => esc_html__( 'Form script', 'styleraise' ), 'value' => empty( $optimisers ) ? esc_html__( 'unprotected, no optimiser found', 'styleraise' ) : esc_html__( 'unprotected — the form may hang on a spinner', 'styleraise' ), 'state' => empty( $optimisers ) ? 'neutral' : 'warn', ); } // An optimiser that only attributes can reach, while attribute mode is // off, is the exact configuration that leaves a spinner on screen. if ( 'auto' === $mode && ! empty( $unreachable ) ) { return array( 'label' => esc_html__( 'Form script', 'styleraise' ), 'value' => sprintf( /* translators: %s: comma-separated optimisation plugin names. */ esc_html__( 'protected, but %s needs tag marking', 'styleraise' ), implode( ', ', array_keys( $unreachable ) ) ), 'state' => 'warn', ); } return array( 'label' => esc_html__( 'Form script', 'styleraise' ), 'value' => 'attributes' === $mode ? esc_html__( 'protected, tags marked', 'styleraise' ) : esc_html__( 'protected on form pages', 'styleraise' ), 'state' => 'ok', ); } /** * Diagnostics row describing the active freshness pattern. * * @return array{label:string,value:string,state:string} */ private function freshness_fact(): array { $pattern = Freshness::pattern(); $labels = Freshness::pattern_labels(); $config = Freshness::config(); $refreshing = Freshness::HYDRATE_OFF !== $config['hydrate']; return array( 'label' => esc_html__( 'Freshness', 'styleraise' ), 'value' => sprintf( /* translators: 1: pattern name, 2: whether figures refresh in the browser. */ esc_html__( '%1$s · %2$s', 'styleraise' ), $labels[ $pattern ]['title'] ?? $pattern, $refreshing ? esc_html__( 'figures refresh', 'styleraise' ) : esc_html__( 'no refreshing', 'styleraise' ) ), // Without refreshing, a cached page keeps its original totals for // anyone whose browser or CDN already holds it. 'state' => $refreshing ? 'ok' : 'warn', ); } /** * Diagnostics row describing the last purge. * * @return array{label:string,value:string,state:string} */ private function purge_fact(): array { $last = Purger::last_purge(); if ( ! $last || empty( $last['time'] ) ) { return array( 'label' => esc_html__( 'Last purge', 'styleraise' ), 'value' => esc_html__( 'never', 'styleraise' ), 'state' => 'neutral', ); } $pages = (int) ( $last['posts'] ?? 0 ); return array( 'label' => esc_html__( 'Last purge', 'styleraise' ), // Raw on purpose: the diagnostics table escapes every value on output. 'value' => sprintf( /* translators: 1: human time difference, 2: number of pages, e.g. "3 pages". */ __( '%1$s ago · %2$s', 'styleraise' ), human_time_diff( (int) $last['time'] ), self::count_pages( $pages ) ), 'state' => 'neutral', ); } /** * Diagnostics row describing the campaign location index. * * @return array{label:string,value:string,state:string} */ private function index_fact(): array { $stats = Location_Index::stats(); $campaigns = (int) $stats['campaigns']; return array( 'label' => esc_html__( 'Known locations', 'styleraise' ), // Raw on purpose: the diagnostics table escapes every value on output. 'value' => sprintf( /* translators: 1: number of campaigns, e.g. "2 campaigns"; 2: number of pages, e.g. "5 pages". */ __( '%1$s on %2$s', 'styleraise' ), sprintf( /* translators: %d: number of campaigns. */ _n( '%d campaign', '%d campaigns', $campaigns, 'styleraise' ), $campaigns ), self::count_pages( (int) $stats['pages'] ) ), 'state' => 'neutral', ); } /** * "1 page" / "5 pages", with the plural form of the site language. * * @param int $pages Number of pages. * @return string Unescaped. */ private static function count_pages( int $pages ): string { return sprintf( /* translators: %d: number of pages. */ _n( '%d page', '%d pages', $pages, 'styleraise' ), $pages ); } /** * Handle the page-cache purge button. */ public function handle_purge(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'styleraise' ), 403 ); } check_admin_referer( self::PURGE_ACTION ); // A manual purge is a deliberate act, so it ignores the throttle and // covers every campaign the index knows about. $campaigns = array_keys( Location_Index::all() ); if ( empty( $campaigns ) ) { Purger::purge_all(); } else { Purger::purge_campaigns( $campaigns, true ); } wp_safe_redirect( add_query_arg( array( 'page' => self::PAGE, 'wcdp-el-msg' => 'purged', ), admin_url( 'admin.php' ) ) ); exit; } /** * Names of the page caches this site actually has. * * @return string */ private function detected_caches_text(): string { $labels = array(); foreach ( Purger::active_adapters() as $slug => $adapter ) { // The generic core-signal adapter is always active and would tell // the reader nothing about their own stack. if ( 'generic' === $slug ) { continue; } $labels[] = (string) $adapter['label']; } if ( empty( $labels ) ) { return esc_html__( 'no page caching plugin found — only the core purge signal will be sent', 'styleraise' ); } return implode( ', ', $labels ); } /** * The freshness card. * * Page caches store the figures that were current when a page was written, * which is why an active fundraiser can spend hours advertising its opening * total. This card is where that is dealt with. */ private function render_freshness_card(): void { $pattern = Freshness::pattern(); $labels = Freshness::pattern_labels(); $name = Options::OPTION; ?> $label ) : ?> > esc_html__( 'Never', 'styleraise' ), Freshness::HYDRATE_LOAD => esc_html__( 'On page load', 'styleraise' ), Freshness::HYDRATE_FOCUS => esc_html__( 'On load and when the tab regains focus', 'styleraise' ), Freshness::HYDRATE_INTERVAL => esc_html__( 'Continuously, on a timer', 'styleraise' ), ); foreach ( $modes as $value => $mode_label ) { printf( '%3$s', esc_attr( $value ), selected( $value, Options::get( 'freshness_hydrate', 'focus' ), false ), esc_html( $mode_label ) ); } ?> esc_html__( 'Never', 'styleraise' ), Freshness::PURGE_INDEX => esc_html__( 'Pages that show the campaign', 'styleraise' ), Freshness::PURGE_URLS => esc_html__( 'Only the URLs listed below', 'styleraise' ), Freshness::PURGE_ALL => esc_html__( 'The entire site cache', 'styleraise' ), ); foreach ( $purges as $value => $purge_label ) { printf( '%3$s', esc_attr( $value ), selected( $value, Options::get( 'freshness_purge', 'index' ), false ), esc_html( $purge_label ) ); } ?> > $reachable ) { $rendered[] = $reachable ? $label /* translators: %s: optimisation plugin name. */ : sprintf( esc_html__( '%s (needs "Also mark the tags")', 'styleraise' ), $label ); } echo esc_html( implode( ', ', $rendered ) ); ?> array( esc_html__( 'Only where the form is (recommended)', 'styleraise' ), esc_html__( 'Tells WP Rocket, LiteSpeed, Perfmatters and Autoptimize to leave the donation form script and the scripts it needs alone — the WordPress translation scripts, or jQuery with Donation Platform for WooCommerce older than 1.5.0 — but only on pages that actually contain a form. The rest of the site keeps every optimisation.', 'styleraise' ), ), 'attributes' => array( esc_html__( 'Also mark the tags (widest coverage)', 'styleraise' ), esc_html__( 'Adds opt-out attributes the above plus Cloudflare Rocket Loader, WP Meteor and NitroPack understand. These cannot be limited to form pages, so the scripts the donation form script needs (the WordPress translation scripts, or jQuery with Donation Platform for WooCommerce older than 1.5.0) stop being delayed site-wide. Use it if the form still shows a spinner.', 'styleraise' ), ), 'off' => array( esc_html__( 'Off', 'styleraise' ), esc_html__( 'Change nothing. Only sensible if you maintain the exclusions by hand in your caching plugin.', 'styleraise' ), ), ); foreach ( $guard_options as $value => $copy ) : $id = 'wcdp-el-guard-' . sanitize_html_class( $value ); ?> > hook_suffix || $hook_suffix !== $this->hook_suffix ) { return; } wp_enqueue_style( 'wcdp-el-settings', WCDP_EL_URL . 'assets/css/wcdp-el-settings.css', array(), WCDP_EL_VERSION ); } }
' . esc_html__( 'Caches cleared.', 'styleraise' ) . '
' . esc_html__( 'Page caches purged.', 'styleraise' ) . '
detected_caches_text() ); ?>
$reachable ) { $rendered[] = $reachable ? $label /* translators: %s: optimisation plugin name. */ : sprintf( esc_html__( '%s (needs "Also mark the tags")', 'styleraise' ), $label ); } echo esc_html( implode( ', ', $rendered ) ); ?>