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_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 ) : ?>

$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 ) ); ?>

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 ); } }