Skip to main content
Code Review

Return to Revisions

5 of 6
deleted 1656 characters in body

First attempt - Wordpress PHP Settings API wrapper

I've been working on what is essentially my first proper attempt at making a useful PHP wrapper class for the Wordpress Settings API. It works great so far and I plan to add more methods as I go so that I eventually build up a class that can be used in pretty much any situation.

As this is my first proper PHP class, I'm looking for suggestions of ways that I could refactor or do things more efficiently. Perhaps places where I could write code differently/use different functions to improve it. I'm not asking for a full rewrite – it's part of my learning process to do it myself.

The code is also on my GitHub repo, so I'd appreciate any constructive feedback.

<?php
/**
 * A PHP Wrapper class to help set up an options page for the Wordpress API.
 *
 * @package Wordpress
 * @subpackage ThemeName
 * @version 1.0
 *
 * @author Paul Brophy <[email protected]>
 */
class PBThemeOptions
{
 // Declare class variables
 public $pages = array();
 public $sections = array();
 public $fields = array();
 /**
 * Construct
 *
 * @since 1.0
 */
 public function __construct()
 { 
 // Set the pages, sections and fields 
 add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
 add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
 }
 /**
 * Add the options pages
 *
 * @since 1.0
 * @todo Add support for other menu types
 */
 public function addOptionsPages()
 { 
 
 foreach ( $this->pages as $page )
 {
 if ($page['type']=='menu')
 {
 add_menu_page(
 __( $page['title'] ),
 __( $page['title'] ),
 'administrator',
 $page['id'],
 array( &$this, 'displayOptionsPage' )
 );
 }
 else if ($page['type'] == 'submenu')
 {
 add_submenu_page(
 $page['parent'],
 __( $page['title'] ),
 __( $page['title'] ),
 'administrator',
 $page['id'],
 array( &$this, 'displayOptionsPage' )
 );
 }
 }
 }
 /**
 * Add the settings sections
 *
 * @since 1.0
 */
 private function addOptionsSections()
 {
 foreach( $this->sections as $section )
 {
 add_settings_section(
 $section['id'],
 __($section['title']),
 '', // Add the description for the section in later
 $section['page']
 );
 }
 }
 /**
 * Add the settings fields
 *
 * @since 1.0
 */
 private function addOptionsFields()
 {
 foreach ($this->fields as $section => $field)
 {
 foreach ($field as $option)
 {
 add_settings_field(
 $option['name'],
 __($option['label']),
 array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
 $section,
 $option['section'],
 array(
 'option' => $section,
 'optionName' => $option['name'] 
 )
 );
 }
 }
 }
 /**
 * Register the settings with register_setting()
 *
 * @since 1.0
 */
 private function registerSettings()
 {
 foreach ($this->pages as $page)
 {
 register_setting(
 $page['id'],
 $page['id']
 );
 }
 }
 /**
 * Display options page
 *
 * @since 1.0
 */
 public function displayOptionsPage()
 {
 if (isset($_GET['tab']))
 {
 $activeTab = $_GET['tab'];
 }
 else
 {
 $activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
 }
 ?>
 <div class="wrap">
 <div id="icon-themes" class="icon32"></div>
 <h2><?php _e( $this->pageTitle ); ?></h2>
 <?php
 settings_errors();
 $this->displayOptionsTabs( $activeTab );
 ?>
 <form method="post" action="options.php">
 <?php $this->displayOptionsSettings( $activeTab ); ?>
 <?php submit_button(); ?>
 </form>
 </div>
 <?php
 }
 /**
 * Set the pages, sections and fields
 *
 * @since 1.0
 */
 public function initializeThemeOptions()
 {
 $this->setOptionsDefaults();
 // Create the sections
 $this->addOptionsSections();
 // Create the fields
 $this->addOptionsFields(); 
 // Register the settings
 $this->registerSettings();
 }
 /**
 * Display the tabs
 *
 * @param string $activeTab;
 * @since 1.0
 */
 private function displayOptionsTabs( $activeTab = '' )
 {
 if ( count($this->pages) > 0 )
 { 
 $output = '<h2 class="nav-tab-wrapper">';
 foreach ( $this->pages as $page )
 {
 $currentTab = strtolower(str_replace('_', '-', $page['id']));
 $activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
 $output .= sprintf(
 '<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
 $page['id'],
 $currentTab,
 $page['title'],
 $activeClass
 );
 }
 $output .= '</h2>';
 echo $output;
 }
 }
 /**
 * Display the sections and fields according to the page
 *
 * @param string $activeTab;
 * @since 1.0
 */
 private function displayOptionsSettings( $activeTab = '' )
 {
 $currentTab = strtolower( str_replace('-', '_', $activeTab) );
 settings_fields( $currentTab );
 do_settings_sections( $currentTab );
 }
 /**
 * HTML output for text field
 *
 * @param array $option;
 * @since 1.0
 */
 public function displayTextField( $option = array() )
 {
 $value = get_option( $option['option'] );
 printf(
 '<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
 str_replace('_', '-', $option['optionName']),
 $option['option'],
 $option['optionName'],
 sanitize_text_field($value[$option['optionName']])
 );
 }
 /**
 * HTML output for textarea field
 *
 * @param array $option;
 * @since 1.0
 */
 public function displayTextareaField( $option = array() )
 {
 $value = get_option( $option['option'] );
 printf(
 '<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
 str_replace('_', '-', $option['optionName']),
 $option['option'],
 $option['optionName'],
 esc_textarea($value[$option['optionName']])
 );
 }
 /**
 * HTML output for url field
 *
 * @param array $option;
 * @since 1.0
 */
 public function displayUrlField( $option = array() )
 {
 $value = get_option( $option['option'] );
 printf(
 '<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
 str_replace('_', '-', $option['optionName']),
 $option['option'],
 $option['optionName'],
 esc_url($value[$option['optionName']])
 );
 }
 /**
 * Set default values for the settings.
 *
 * @since 1.0
 */
 private function setOptionsDefaults()
 {
 $defaults = array();
 
 foreach ($this->fields as $optionsName => $options)
 {
 $defaults[$optionsName] = array();
 foreach($options as $option)
 {
 $defaults[$optionsName][$option['name']] = '';
 }
 }
 foreach ($defaults as $option => $value)
 {
 if( get_option( $option ) == false )
 {
 add_option(
 $option,
 apply_filters( $option . '_defaults', $value )
 );
 }
 }
 }
 /**
 * Validate the input depending on it's type. Used in register_setting().
 *
 * @since 1.0
 * @todo Write the function!!!
 */
 public function validateOptions( $input )
 {
 return $input;
 }
}
lang-php

AltStyle によって変換されたページ (->オリジナル) /