Make WordPress Core

Changeset 41627


Ignore:
Timestamp:
09/28/2017 12:22:48 AM (9 years ago)
Author:
pento
Message:

Options: Add new alloptions and pre_cache_alloptions filters.

pre_cache_alloptions is run before the alloptions array is inserted into the cache, and is valuable for sanity checking the options, particularly if your caching scheme has size limitations.

alloptions is run before returning the alloptions array, and is useful for when you have extra information that alloptions should return.

Props sebastian.pisula, keesiemeijer.
Fixes #33958.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r41242 r41627  
    187187        global $wpdb;
    188188
    189         if ( ! wp_installing() || ! is_multisite() )
     189        if ( ! wp_installing() || ! is_multisite() ) {
    190190                $alloptions = wp_cache_get( 'alloptions', 'options' );
    191         else
     191        } else {
    192192                $alloptions = false;
    193 
    194         if ( !$alloptions ) {
     193        }
     194
     195        if ( ! $alloptions ) {
    195196                $suppress = $wpdb->suppress_errors();
    196                 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
     197                if ( ! $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) {
    197198                        $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
    198                 $wpdb->suppress_errors($suppress);
     199                }
     200                $wpdb->suppress_errors( $suppress );
     201
    199202                $alloptions = array();
    200203                foreach ( (array) $alloptions_db as $o ) {
    201204                        $alloptions[$o->option_name] = $o->option_value;
    202205                }
    203                 if ( ! wp_installing() || ! is_multisite() )
     206
     207                if ( ! wp_installing() || ! is_multisite() ) {
     208                        /**
     209                         * Filters all options before caching them.
     210                         *
     211                         * @since 4.9.0
     212                         *
     213                         * @param array $alloptions Array with all options.
     214                         */
     215                        $alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
    204216                        wp_cache_add( 'alloptions', $alloptions, 'options' );
    205         }
    206 
    207         return $alloptions;
     217                }
     218        }
     219
     220        /**
     221         * Filters all options after retrieving them.
     222         *
     223         * @since 4.9.0
     224         *
     225         * @param array $alloptions Array with all options.
     226         */
     227        return apply_filters( 'alloptions', $alloptions );
    208228}
    209229
Note: See TracChangeset for help on using the changeset viewer.