Scottnsd
3/20/2014 - 7:25 PM

WordPress and the single responsibility principle

WordPress and the single responsibility principle

<?php

/*
Plugin Name: WordPress Meme Shortcode
Description: Add some memes to your posts using a shortcode!
Author: Carl Alexander
Author URI: http://www.carlalexander.ca
License: GPL3
*/

// Load classes
require_once dirname(__FILE__) . '/AdminPage.php';
require_once dirname(__FILE__) . '/Options.php';
require_once dirname(__FILE__) . '/Plugin.php';
require_once dirname(__FILE__) . '/Shortcode.php';

WPMemeShortcode_Plugin::load();
<?php

/**
 * WordPress Meme Shortcode handler.
 *
 * @author Carl Alexander
 */
class WPMemeShortcode_Shortcode
{
    /**
     * @var WPMemeShortcode_Options
     */
    private $options;

    /**
     * Register the shortcode class with all the appropriate WordPress hooks.
     *
     * @param WPMemeShortcode_Options $options
     */
    public static function register(WPMemeShortcode_Options $options)
    {
        $shortcode = new self($options);

        add_shortcode('meme', array($shortcode, 'handle'));
    }

    /**
     * Constructor.
     *
     * @param WPMemeShortcode_Options $options
     */
    public function __construct(WPMemeShortcode_Options $options)
    {
        $this->options = $options;
    }

    /**
     * Handles the output of the shortcode.
     *
     * @param array  $attributes
     * @param string $content
     */
    public function handle(array $attributes, $content = null)
    {
        // Do nothing if no ID is given or it is not numeric
        if (!isset($attributes['id']) || !is_numeric($attributes['id'])) {
            return $content;
        }

        // If no size is given or it is not a numeric value, get default.
        if (!isset($attributes['size']) || !is_numeric($attributes['size'])) {
            $attributes['size'] = $this->options->get('size', '500');
        }

        return "<img src=\"http://cdn.memegenerator.net/instances/{$attributes['size']}x/{$attributes['id']}.jpg\" />";
    }
}
<?php

/**
 * WordPress Meme Shortcode Plugin.
 *
 * @author Carl Alexander
 */
class WPMemeShortcode_Plugin
{
    /**
     * Loads the plugin into WordPress.
     */
    public static function load()
    {
        $options = WPMemeShortcode_Options::load();

        WPMemeShortcode_AdminPage::register($options);
        WPMemeShortcode_Shortcode::register($options);
    }
}
<?php

/**
 * Manages WordPress Meme Shortcode options.
 *
 * @author Carl Alexander
 */
class WPMemeShortcode_Options
{
    /**
     * @var array
     */
    private $options;

    /**
     * Load the plugin options from WordPress.
     *
     * @return WPMemeShortcode_Options
     */
    public static function load()
    {
        $options = get_option('wp_meme_shortcode', array());

        return new self($options);
    }

    /**
     * Constructor.
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        $this->options = $options;
    }

    /**
     * Gets the option for the given name. Returns the default value if the
     * value does not exist.
     *
     * @param string $name
     * @param mixed  $default
     *
     * @return mixed
     */
    public function get($name, $default = null)
    {
        if (!$this->has($name)) {
            return $default;
        }

        return $this->options[$name];
    }

    /**
     * Checks if the option exists or not.
     *
     * @param string $name
     *
     * @return Boolean
     */
    public function has($name)
    {
        return isset($this->options[$name]);
    }

    /**
     * Sets an option. Overwrites the existing option if the name is already in use.
     *
     * @param string $name
     * @param mixed  $value
     */
    public function set($name, $value)
    {
        $this->options[$name] = $value;
    }
}
<?php

/**
 * The WordPress Meme Shortcode admin page.
 *
 * @author Carl Alexander
 */
class WPMemeShortcode_AdminPage
{
    /**
     * @var WPMemeShortcode_Options
     */
    private $options;

    /**
     * Register the admin page class with all the appropriate WordPress hooks.
     *
     * @param WPMemeShortcode_Options $options
     */
    public static function register(WPMemeShortcode_Options $options)
    {
        $page = new self($options);

        add_action('admin_init', array($page, 'configure'));
        add_action('admin_menu', array($page, 'addAdminPage'));
    }

    /**
     * Constructor.
     *
     * @param WPMemeShortcode_Options $options
     */
    public function __construct(WPMemeShortcode_Options $options)
    {
        $this->options = $options;
    }

    /**
     * Adds the admin page to the menu.
     */
    public function addAdminPage()
    {
        add_options_page(__('WordPress Meme Shortcode', 'wp_meme_shortcode'), __('Meme Shortcode', 'wp_meme_shortcode'), 'install_plugins', 'wp_meme_shortcode', array($this, 'render'));
    }

    /**
     * Configure the option page using the settings API.
     */
    public function configure()
    {
        // Register settings
        register_setting('wp_meme_shortcode', 'wp_meme_shortcode');

        // General Section
        add_settings_section('wp_meme_shortcode_general', __('General', 'wp_meme_shortcode'), array($this, 'renderGeneralSection'), 'wp_meme_shortcode');
        add_settings_field('wp_meme_shortcode_size', __('Default Image Size', 'wp_meme_shortcode'), array($this, 'renderSizeField'), 'wp_meme_shortcode', 'wp_meme_shortcode_general');
    }

    /**
     * Renders the admin page using the Settings API.
     */
    public function render()
    {
        ?>
        <div class="wrap" id="wp-meme-shortcode-admin">
            <h2><?php _e('WordPress Meme Shortcode', 'wp_meme_shortcode'); ?></h2>
            <form action="options.php" method="POST">
                <?php settings_fields('wp_meme_shortcode'); ?>
                <?php do_settings_sections('wp_meme_shortcode'); ?>
                <?php submit_button(); ?>
            </form>
        </div>
        <?php
    }

    /**
     * Renders the general section.
     */
    public function renderGeneralSection()
    {
        ?>
        <p><?php _e('Configure WordPress Meme Shortcode.', 'wp_meme_shortcode'); ?></p>
        <?php
    }

    /**
     * Renders the size field.
     */
    public function renderSizeField()
    {
        ?>
        <input id="wp_meme_shortcode_size" name="wp_meme_shortcode[size]" type="number" value="<?php echo $this->options->get('size', '500'); ?>" />
        <?php
    }
}