options = get_option( 'gorilla_cache_settings', array() ); } /** * Get a specific option value. * * @since 1.0.0 * @param string $key The option key. * @param mixed $fallback Value to return if the option does not exist. * @return mixed The option value. */ public function get_option( $key, $fallback = false ) { return isset( $this->options[ $key ] ) ? $this->options[ $key ] : $fallback; } /** * Set a specific option value. * * @since 1.0.0 * @param string $key The option key. * @param mixed $value The option value. * @return bool Whether the option was updated. */ public function set_option( $key, $value ) { $this->options[ $key ] = $value; return update_option( 'gorilla_cache_settings', $this->options ); } /** * Register the settings page. * * This method registers settings, sections, and fields for the WordPress Settings API. * * @since 1.0.0 */ public function register_settings() { // Step 1: Register the setting. register_setting( 'gorilla_cache_options', 'gorilla_cache_settings', array( 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); // Step 2: Register section. add_settings_section( 'gorilla_cache_general_section', __( 'Advanced settings', 'gorilla-cache' ), array( $this, 'general_section_callback' ), 'gorilla-cache' ); // Step 3: Register fields. Every one has a tooltip in its label linking to its section of the guide (written out, // so the docs site's link check can read every address); lists add to the built-in defaults. $fields = array( 'lifespan_hours' => array( __( 'Cache lifespan (hours)', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'lifespan' ), __( 'How long a cached page is kept before it is rebuilt on its next visit. Ten hours at most keeps the security codes in forms and checkouts valid: WordPress renews them every twelve hours.', 'gorilla-cache' ) ), 'preload' => array( __( 'Warm the cache', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'preload' ), __( 'Visits your pages in the background, a few at a time, so the first visitor gets a cached page too.', 'gorilla-cache' ) ), 'clear_all_on_publish' => array( __( 'Clear all on new content', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'clear-all-on-publish' ), __( 'A new post can appear on the home page, in archives and in widgets, so publishing one clears the whole cache. Updating a post clears only the pages it appears on.', 'gorilla-cache' ) ), 'mobile' => array( __( 'Separate cache for phones', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'mobile' ), __( 'Auto keeps one cached copy for all devices, unless Perfmatters serves phones different pages. Choose Always if your theme does.', 'gorilla-cache' ) ), 'excluded_paths' => array( __( 'Never cache these pages', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'excluded-paths' ), __( 'One address per line, such as /members/, or /members/* for everything under it. Cart, checkout and account pages of WooCommerce, Easy Digital Downloads and other shop and membership plugins are already left out: no need to add them.', 'gorilla-cache' ) ), 'excluded_cookies' => array( __( 'Never cache with cookies', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'excluded-cookies' ), __( 'One cookie name per line; each also covers every cookie that starts with it. Logged-in visitors, shopping carts and comment authors are already handled: no need to add them.', 'gorilla-cache' ) ), 'ignored_params' => array( __( 'Ignore these link parameters', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'ignored-params' ), __( 'Link parameters that do not change the page, one per line. Tracking parameters such as utm_source, fbclid and gclid are already ignored, so those links share the cached page.', 'gorilla-cache' ) ), 'cached_params' => array( __( 'Separate copy per parameter', 'gorilla-cache' ), gorilla_cache_docs_url( 'settings', 'cached-params' ), __( 'Links with parameters Gorilla Cache does not know are not cached. Add a parameter here, such as a filter, to keep one cached copy per value.', 'gorilla-cache' ) ), ); foreach ( $fields as $id => $field ) { add_settings_field( $id, '' . gorilla_cache_help_icon( $field[2], $field[1], $field[0] ), array( $this, 'field_callback' ), 'gorilla-cache', 'gorilla_cache_general_section', array( 'id' => $id ) ); } } /** * Section callback. */ public function general_section_callback() { echo '
' . esc_html__( 'Gorilla Cache works without any of these. They exist for sites that need an exception; every list adds to what is handled automatically.', 'gorilla-cache' ) . '
'; } /** * The current settings with their defaults. * * @return array */ public static function values() { $saved = get_option( 'gorilla_cache_settings', array() ); return array_merge( array( 'lifespan_hours' => 10, 'preload' => '1', 'clear_all_on_publish' => '1', 'mobile' => 'auto', 'excluded_paths' => array(), 'excluded_cookies' => array(), 'ignored_params' => array(), 'cached_params' => array(), ), is_array( $saved ) ? $saved : array() ); } /** * Renders one field. * * @param array $args Field arguments ("id"). */ public function field_callback( $args ) { $id = $args['id']; $values = self::values(); $value = $values[ $id ]; $name = 'gorilla_cache_settings[' . $id . ']'; switch ( $id ) { case 'lifespan_hours': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) (int) $value ) ); break; case 'preload': case 'clear_all_on_publish': printf( '', esc_attr( $id ), esc_attr( $name ), checked( '1', (string) $value, false ) ); break; case 'mobile': echo ''; break; default: printf( '', esc_attr( $id ), esc_attr( $name ), esc_textarea( implode( "\n", (array) $value ) ) ); } } /** * Sanitize plugin settings. Merges with what is saved, so a form never wipes other settings. * * @param array $input The input array from form submission. * @return array The sanitized settings. */ public function sanitize_settings( $input ) { $existing = get_option( 'gorilla_cache_settings', array() ); $sanitized = is_array( $existing ) ? $existing : array(); $input = is_array( $input ) ? $input : array(); $fields = $this->verified_form_fields(); $form_id = isset( $fields['form_id'] ) ? $fields['form_id'] : ''; if ( 'advanced_form' === $form_id ) { $sanitized['lifespan_hours'] = isset( $input['lifespan_hours'] ) ? max( 1, min( 10, absint( $input['lifespan_hours'] ) ) ) : 10; $sanitized['preload'] = isset( $input['preload'] ) ? '1' : '0'; $sanitized['clear_all_on_publish'] = isset( $input['clear_all_on_publish'] ) ? '1' : '0'; $sanitized['mobile'] = isset( $input['mobile'] ) && in_array( $input['mobile'], array( 'auto', 'always', 'never' ), true ) ? $input['mobile'] : 'auto'; $sanitized['excluded_paths'] = self::lines( isset( $input['excluded_paths'] ) ? $input['excluded_paths'] : '', '#[^\p{L}\p{N}/_.*%~-]#u' ); $sanitized['excluded_cookies'] = self::lines( isset( $input['excluded_cookies'] ) ? $input['excluded_cookies'] : '', '#[^A-Za-z0-9_.*-]#' ); $sanitized['ignored_params'] = self::lines( isset( $input['ignored_params'] ) ? $input['ignored_params'] : '', '#[^A-Za-z0-9_.\[\]-]#' ); $sanitized['cached_params'] = self::lines( isset( $input['cached_params'] ) ? $input['cached_params'] : '', '#[^A-Za-z0-9_.\[\]-]#' ); } // No "Settings saved." here: options.php adds it once the save succeeds (adding it here showed it twice). return $sanitized; } /** * Splits a textarea into clean, unique lines. Characters outside the allowed set are removed, so no * regular expression or markup can reach the cache rules. * * @param string $text Textarea value. * @param string $invalid Pattern of characters to remove. * @return string[] */ private static function lines( $text, $invalid ) { $out = array(); foreach ( preg_split( '/\R/', (string) $text ) as $line ) { $line = preg_replace( $invalid, '', trim( wp_unslash( $line ) ) ); if ( '' !== $line ) { $out[] = $line; } } return array_values( array_unique( array_slice( $out, 0, 100 ) ) ); } /** * Keeps the tab after a settings save: options.php redirects back to the screen, and the tab's hash would * otherwise be lost. * * @param string $location The redirect URL. * @return string */ public function preserve_tab_hash_in_redirect( $location ) { $fields = $this->verified_form_fields(); if ( empty( $fields['tab'] ) || false !== strpos( $location, '#' ) ) { return $location; } return $location . '#' . $fields['tab']; } /** * The settings form's own fields, read only when the request is that form's save: options.php, the nonce * settings_fields() printed for this option group, a user allowed to change these settings, and this option * group. Every other request (another form's redirect, a save from code or WP-CLI) gets an empty array, and * its nonce is never tested against ours. * * @return array{form_id?: string, tab?: string} */ private function verified_form_fields() { global $pagenow; if ( 'options.php' !== $pagenow ) { return array(); } if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'gorilla_cache_options-options' ) ) { return array(); } if ( ! current_user_can( 'manage_options' ) ) { return array(); } $option_page = isset( $_POST['option_page'] ) ? sanitize_key( wp_unslash( $_POST['option_page'] ) ) : ''; if ( 'gorilla_cache_options' !== $option_page ) { return array(); } return array( 'form_id' => isset( $_POST['form_id'] ) ? sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) : '', 'tab' => isset( $_POST['_wp_http_referer_tab'] ) ? sanitize_key( wp_unslash( $_POST['_wp_http_referer_tab'] ) ) : '', ); } }