WordPress.org

Make WordPress Core

Opened 20 months ago

#38690 new enhancement

Introduce classes for settings

Reported by: flixos90 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: needs-patch 2nd-opinion
Focuses: Cc:

Description

Let's add classes surrounding settings to provide a better structure for dealing with them. It will also allow us to get rid of some globals if we are in a position to remove them (in terms of BC).

Here is what I have in mind:

  • A WP_Settings class should be introduced that contains get(), update(), add() and delete() methods. This will mostly be copy-paste from the related functions. The functions themselves will become wrappers.
  • A WP_Settings_Registry will be introduced. It should contain all methods that handle registered settings (mostly introduced in 4.7). Again, the functions would become wrappers. We could get rid of the $wp_registered_settings global here and store these in a class property instead.
  • The WP_Settings_Registry instance will be contained by the WP_Settings instance as a public property.
  • A function wp_settings() will be introduced to access the WP_Settings instance or generate it if it does not exist yet. I'm not sure yet how to store the instance: The easy way is a global, but I was wondering where we're at with plans like a WP::get( 'settings' ) so that we could do it differently. Anyway, let's assume a global first.

I think it would be a good pattern to build the class in a flexible way, so that the registry instance and database instance are passed to the class constructor. The following is how I would envision the wp_settings() function:

function wp_settings() {
    global $wp_settings;

    if ( ! isset( $wp_settings ) ) {
        $wp_settings = new WP_Settings( new WP_Settings_Registry(), $GLOBALS['wpdb'] );
    }

    return $wp_settings;
}

I think once we agree on an approach, we should do something similar for metadata. But let's have the discussion in here first and open the other ticket afterwards.

Change History (0)

Note: See TracTickets for help on using tickets.