25
Managing themes and server environments with extensible configuration arrays Chris Olbekson @chris_olbekson @X_Team

Managing themes and server environments with extensible configuration arrays

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Managing themes and server environments with extensible configuration arrays

Managing themes and server environments

with extensible configuration arrays

Chris Olbekson@chris_olbekson @X_Team

Page 2: Managing themes and server environments with extensible configuration arrays

Managing EnvironmentsMaking your code aware

Page 3: Managing themes and server environments with extensible configuration arrays

wp-config.php

Page 4: Managing themes and server environments with extensible configuration arrays

The Environment Config Class

class WPized_Env_Config {

1. Get defined environment from active-env and assign to $active_env

2. Load default.env

Page 5: Managing themes and server environments with extensible configuration arrays

The Environment Config Class

class WPized_Env_Config {

1. Get defined environment from active-env and assign to $active_env

2. Load default.env

3. Load defined environment (each environment checks for an override)

a. local.env.php

b. vagrant.env.php

c. staging.env.php

d. production.env.php

Page 6: Managing themes and server environments with extensible configuration arrays

The Environment Config Class

class WPized_Env_Config {

1. Get defined environment from active-env and assign to $active_env

2. Load default.env

3. Load defined environment (each environment checks for an override)

a. local.env.php

b. vagrant.env.php

c. staging.env.php

d. production.env.php

4. Recursively merge the loaded environment configuration array with the default

Page 7: Managing themes and server environments with extensible configuration arrays

The Environment Config Class

static function merge_arrays( array $array ) {return call_user_func_array(

array( 'WPized_Theme_Config','recursive_array_merge_assoc'),func_get_args());

}

continue loading WordPress.

/** Sets up WordPress vars and included files */require_once(ABSPATH, 'wp-settings.php');

Page 8: Managing themes and server environments with extensible configuration arrays

Component LibrariesKeeping a common codebase

Page 9: Managing themes and server environments with extensible configuration arrays

Components generic enough to be used in any WordPress project.

● Redirection● oEmbeds● Post/Taxonomy Ordering

Components used specifically in WordPress projects for specific client.

● Ad Utility● Event Post Type● Social Panel Widget

Components are housed in multiple libraries

WPized Base

Client Name

Common

Page 10: Managing themes and server environments with extensible configuration arrays

ComponentsLet's make 'em configurable!

Page 11: Managing themes and server environments with extensible configuration arrays

1. Default configs can be defined in the component's setup() method.

2. Since theme configs can override these defaults we can use the same

component code for any project!

3. "Mixin" themes are now possible (like child theme groups) which can be

very useful on large multisite installs.

4. Child themes are the final authority.

The merge order of configs

#4Child Theme

config.php

#3Mixin Theme

config.php

#2Parent Theme

config.php

#1Component(defaults)foo.php

Page 12: Managing themes and server environments with extensible configuration arrays

Load a component in functions.php

Path shortcut to the library we'll be using for this example.

define( 'WPIZED_BASE_LIB', WP_CONTENT_DIR . '/includes/wpized_base' );

function foo_load_config {

require_once( WPIZED_BASE_LIB . '/theme_config.php' );

// Custom logo

if ( WPized_Theme_Config::defined( 'custom_logo' ) ) {

require_once( WPIZED_BASE_LIB . '/custom-logo.php' );

WPized_Custom_Logo::setup( WPized_Theme_Config::get( 'custom_logo' ) );

}

}

add_action( 'after_setup_theme', 'foo_load_config' );

Page 13: Managing themes and server environments with extensible configuration arrays

Load a component in functions.php

The component will load when the theme is initialized.

define( 'WPIZED_BASE_LIB', WP_CONTENT_DIR . '/includes/wpized_base' );

function foo_load_config {

require_once( WPIZED_BASE_LIB . '/theme_config.php' );

// Custom logo

if ( WPized_Theme_Config::defined( 'custom_logo' ) ) {

require_once( WPIZED_BASE_LIB . '/custom-logo.php' );

WPized_Custom_Logo::setup( WPized_Theme_Config::get( 'custom_logo' ) );

}

}

add_action( 'after_setup_theme', 'foo_load_config' );

Page 14: Managing themes and server environments with extensible configuration arrays

Load a component in functions.php

Allows us to use some methods for fetching and merging configs.

define( 'WPIZED_BASE_LIB', WP_CONTENT_DIR . '/includes/wpized_base' );

function foo_load_config {

require_once( WPIZED_BASE_LIB . '/theme_config.php' );

// Custom logo

if ( WPized_Theme_Config::defined( 'custom_logo' ) ) {

require_once( WPIZED_BASE_LIB . '/custom-logo.php' );

WPized_Custom_Logo::setup( WPized_Theme_Config::get( 'custom_logo' ) );

}

}

add_action( 'after_setup_theme', 'foo_load_config' );

Page 15: Managing themes and server environments with extensible configuration arrays

Load a component in functions.php

If there's no config key provided for the component, it never runs.

define( 'WPIZED_BASE_LIB', WP_CONTENT_DIR . '/includes/wpized_base' );

function foo_load_config {

require_once( WPIZED_BASE_LIB . '/theme_config.php' );

// Custom logo

if ( WPized_Theme_Config::defined( 'custom_logo' ) ) {

require_once( WPIZED_BASE_LIB . '/custom-logo.php' );

WPized_Custom_Logo::setup( WPized_Theme_Config::get( 'custom_logo' ) );

}

}

add_action( 'after_setup_theme', 'foo_load_config' );

Page 16: Managing themes and server environments with extensible configuration arrays

Theme's config.php

We have a key! So this component will be loaded.

return array(

'brightcove_oembed' => array(

'players' => array( ... ),

),

'custom_logo' => array(

'default' => get_stylesheet_directory_uri() . '/images/common/logo.png',

'width' => 300,

'height' => 100,

),

'ads' => array(

'site' => 'example.com/foo',

),

);

Page 17: Managing themes and server environments with extensible configuration arrays

The simple concept

We can setup the component differently on a site-by-site basis!

Theme Configs

setup( )in component's

class

Parent > Mixin > Child

Default config values

Page 18: Managing themes and server environments with extensible configuration arrays

Load a component in functions.php

The config gets passed to component's setup() method.

define( 'WPIZED_BASE_LIB', WP_CONTENT_DIR . '/includes/wpized_base' );

function foo_load_config {

require_once( WPIZED_BASE_LIB . '/theme_config.php' );

// Custom logo

if ( WPized_Theme_Config::defined( 'custom_logo' ) ) {

require_once( WPIZED_BASE_LIB . '/custom-logo.php' );

WPized_Custom_Logo::setup( WPized_Theme_Config::get( 'custom_logo' ) );

}

}

add_action( 'after_setup_theme', 'foo_load_config' );

Page 19: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $overrides = array() ) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$overrides

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 20: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $really_default_defaults = array()) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$really_default_defaults

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 21: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $really_default_defaults = array() ) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$really_default_defaults

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 22: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $really_default_defaults = array() ) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$really_default_defaults

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 23: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $really_default_defaults = array() ) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$really_default_defaults

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 24: Managing themes and server environments with extensible configuration arrays

class WPized_Custom_Logo {

public static $options = array();

public static function setup( $really_default_defaults = array() ) {

self::$options = WPized_Theme_Config::recursive_array_merge_assoc(

array(

'default' => null,

'width' => 250,

'height' => 100,

),

$really_default_defaults

);

}

}

echo esc_html( WPized_Custom_Logo::$options['width'] ); // 300

Custom logo component

Page 25: Managing themes and server environments with extensible configuration arrays

Thank you!