Skip to content

Instantly share code, notes, and snippets.

@leepeterson
Forked from carlalexander/AdminPage.php
Created August 27, 2020 17:33
Show Gist options
  • Select an option

  • Save leepeterson/f3ff01d59ff9970638a1c102597d8839 to your computer and use it in GitHub Desktop.

Select an option

Save leepeterson/f3ff01d59ff9970638a1c102597d8839 to your computer and use it in GitHub Desktop.

Revisions

  1. @carlalexander carlalexander revised this gist Jun 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdminPage.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php

    namespace WPMemeShortcode
    namespace WPMemeShortcode;

    /**
    * The WordPress Meme Shortcode admin page.
  2. @carlalexander carlalexander revised this gist Jun 2, 2019. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions Shortcode.php
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,25 @@
    <?php

    namespace WPMemeShortcode;

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

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

    @@ -27,9 +29,9 @@ public static function register(WPMemeShortcode_Options $options)
    /**
    * Constructor.
    *
    * @param WPMemeShortcode_Options $options
    * @param Options $options
    */
    public function __construct(WPMemeShortcode_Options $options)
    public function __construct(Options $options)
    {
    $this->options = $options;
    }
  3. @carlalexander carlalexander revised this gist Jun 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,4 @@
    require_once dirname(__FILE__) . '/Plugin.php';
    require_once dirname(__FILE__) . '/Shortcode.php';

    WPMemeShortcode_Plugin::load();
    \WPMemeShortcode\Plugin::load();
  4. @carlalexander carlalexander revised this gist Jun 2, 2019. 3 changed files with 18 additions and 12 deletions.
    14 changes: 8 additions & 6 deletions AdminPage.php
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,25 @@
    <?php

    namespace WPMemeShortcode

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

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

    @@ -28,9 +30,9 @@ public static function register(WPMemeShortcode_Options $options)
    /**
    * Constructor.
    *
    * @param WPMemeShortcode_Options $options
    * @param Options $options
    */
    public function __construct(WPMemeShortcode_Options $options)
    public function __construct(Options $options)
    {
    $this->options = $options;
    }
    6 changes: 4 additions & 2 deletions Options.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    <?php

    namespace WPMemeShortcode;

    /**
    * Manages WordPress Meme Shortcode options.
    *
    * @author Carl Alexander
    */
    class WPMemeShortcode_Options
    class Options
    {
    /**
    * @var array
    @@ -15,7 +17,7 @@ class WPMemeShortcode_Options
    /**
    * Load the plugin options from WordPress.
    *
    * @return WPMemeShortcode_Options
    * @return Options
    */
    public static function load()
    {
    10 changes: 6 additions & 4 deletions Plugin.php
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,22 @@
    <?php

    namespace WPMemeShortcode;

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

    WPMemeShortcode_AdminPage::register($options);
    WPMemeShortcode_Shortcode::register($options);
    AdminPage::register($options);
    Shortcode::register($options);
    }
    }
  5. @carlalexander carlalexander revised this gist Nov 25, 2016. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?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();
  6. @carlalexander carlalexander revised this gist Mar 20, 2014. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion AdminPage.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ class WPMemeShortcode_AdminPage
    /**
    * Register the admin page class with all the appropriate WordPress hooks.
    *
    * @param array $options
    * @param WPMemeShortcode_Options $options
    */
    public static function register(WPMemeShortcode_Options $options)
    {
    2 changes: 1 addition & 1 deletion Shortcode.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ class WPMemeShortcode_Shortcode
    /**
    * Register the shortcode class with all the appropriate WordPress hooks.
    *
    * @param array $options
    * @param WPMemeShortcode_Options $options
    */
    public static function register(WPMemeShortcode_Options $options)
    {
  7. @carlalexander carlalexander revised this gist Mar 20, 2014. 4 changed files with 8 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions AdminPage.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * The WordPress Meme Shortcode admin page.
    *
    2 changes: 2 additions & 0 deletions Options.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * Manages WordPress Meme Shortcode options.
    *
    2 changes: 2 additions & 0 deletions Plugin.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * WordPress Meme Shortcode Plugin.
    *
    2 changes: 2 additions & 0 deletions Shortcode.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * WordPress Meme Shortcode handler.
    *
  8. @carlalexander carlalexander created this gist Mar 20, 2014.
    93 changes: 93 additions & 0 deletions AdminPage.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    /**
    * 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 array $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
    }
    }
    75 changes: 75 additions & 0 deletions Options.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    /**
    * 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;
    }
    }
    18 changes: 18 additions & 0 deletions Plugin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /**
    * 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);
    }
    }
    55 changes: 55 additions & 0 deletions Shortcode.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    /**
    * 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 array $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\" />";
    }
    }