WordPress.org

Plugin Directory

Changeset 1295326


Ignore:
Timestamp:
11/26/2015 11:56:25 PM (3 years ago)
Author:
mikeselander
Message:

Cleaning up, commenting, and removing unnecessary methods

Location:
envira-tamer
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • envira-tamer/trunk/admin/settings-page.php

    r1011854 r1295326  
    22defined( 'ABSPATH' ) OR exit;
    33
     4/*
     5 * Setup the settings page for Envira Tamer.
     6 *
     7 * @package    PackageName
     8 * @author     Mike Selander <[email protected]>
     9 * @since      1.0.0
     10 */
    411class EnviraTamerSettingsPage {
     12
     13    /**
     14     * dir
     15     * Main directory path.
     16     *
     17     * @var string
     18     * @access private
     19     */
    520    private $dir;
     21
     22    /**
     23     * file
     24     * Main file path.
     25     *
     26     * @var string
     27     * @access private
     28     */
    629    private $file;
    7     private $assets_dir;
    8     private $assets_url;
     30
     31    /**
     32     * settings_base
     33     * Prefix for the settings.
     34     *
     35     * @var string
     36     * @access private
     37     */
    938    private $settings_base;
     39
     40    /**
     41     * settings
     42     * Array of the settings.
     43     *
     44     * @var array
     45     * @access private
     46     */
    1047    private $settings;
    1148
     49
     50    /**
     51     * Constructor function.
     52     *
     53     * @see add_action, add_filter
     54     */
    1255    public function __construct( $file ) {
    13         $this->file = $file;
    14         $this->dir = dirname( $this->file );
    15         $this->assets_dir = trailingslashit( $this->dir ) . 'assets';
    16         $this->assets_url = esc_url( trailingslashit( plugins_url( '/assets/', $this->file ) ) );
     56
     57        $this->file         = $file;
     58        $this->dir          = dirname( $this->file );
    1759        $this->settings_base = 'et_';
    1860
     
    2163        add_action( 'admin_menu', array( $this, 'add_menu_item' ) );
    2264        add_filter( 'plugin_action_links_' . plugin_basename( $this->file ) , array( $this, 'add_settings_link' ) );
     65
    2366    }
    2467
    2568    /**
    2669     * Initialise settings
    27      * @return void
    2870     */
    2971    public function init() {
     72
    3073        $this->settings = $this->settings_fields();
    31     }
    32 
    33     /**
    34      * Add settings page to admin menu
    35      * @return void
     74
     75    }
     76
     77
     78    /**
     79     * Add settings page to admin menu.
     80     *
     81     * @see add_submenu_page
    3682     */
    3783    public function add_menu_item() {
     84
    3885        add_submenu_page(
    3986            'edit.php?post_type=envira',
     
    4491            array( $this, 'settings_page' )
    4592        );
    46     }
    47 
    48     /**
    49      * Add settings link to plugin list table
     93
     94    }
     95
     96
     97    /**
     98     * Add settings link to plugin list table.
     99     *
    50100     * @param  array $links Existing links
    51      * @return array        Modified links
     101     * @return array Modified links
    52102     */
    53103    public function add_settings_link( $links ) {
     104
    54105        $settings_link = '<a href="edit.php?post_type=envira&page=envira_tamer">' . __( 'Settings', 'envira_tamer' ) . '</a>';
    55106        array_push( $links, $settings_link );
     107
    56108        return $links;
    57     }
    58 
    59     /**
    60      * Build settings fields
     109
     110    }
     111
     112
     113    /**
     114     * Build settings fields.
     115     *
    61116     * @return array Fields to be displayed on settings page
    62117     */
     
    94149
    95150        return $settings;
    96     }
    97 
    98     /**
    99      * Register plugin settings
    100      * @return void
     151
     152    }
     153
     154
     155    /**
     156     * Register plugin settings.
     157     *
     158     *
    101159     */
    102160    public function register_settings() {
    103         if( is_array( $this->settings ) ) {
    104             foreach( $this->settings as $section => $data ) {
    105 
    106                 // Add section to page
    107                 add_settings_section( $section, $data['title'], array( $this, 'settings_section' ), 'envira_tamer' );
    108 
    109                 foreach( $data['fields'] as $field ) {
    110 
    111                     // Validation callback for field
    112                     $validation = '';
    113                     if( isset( $field['callback'] ) ) {
    114                         $validation = $field['callback'];
    115                     }
    116 
    117                     // Register field
    118                     $option_name = $this->settings_base . $field['id'];
    119                     register_setting( 'envira_tamer', $option_name, $validation );
    120 
    121                     // Add field to page
    122                     add_settings_field( $field['id'], $field['label'], array( $this, 'display_field' ), 'envira_tamer', $section, array( 'field' => $field ) );
     161
     162        if ( !is_array( $this->settings ) ) {
     163            return;
     164        }
     165
     166        foreach ( $this->settings as $section => $data ) {
     167
     168            // Add section to page
     169            add_settings_section( $section, $data['title'], '', 'envira_tamer' );
     170
     171            foreach( $data['fields'] as $field ) {
     172
     173                // Validation callback for field
     174                $validation = '';
     175                if( isset( $field['callback'] ) ) {
     176                    $validation = $field['callback'];
    123177                }
    124             }
    125         }
    126     }
    127 
    128     public function settings_section( $section ) {
    129         $html = '<p> ' . $this->settings[ $section['id'] ]['description'] . '</p>' . "\n";
    130         echo $html;
    131     }
    132 
    133     /**
    134      * Generate HTML for displaying fields
     178
     179                // Register field
     180                $option_name = $this->settings_base . $field['id'];
     181                register_setting( 'envira_tamer', $option_name, $validation );
     182
     183                // Add field to page
     184                add_settings_field( $field['id'], $field['label'], array( $this, 'display_field' ), 'envira_tamer', $section, array( 'field' => $field ) );
     185            } // end foreach ['fields']
     186
     187        } // end foreach $settings
     188
     189    }
     190
     191
     192    /**
     193     * Generate HTML for displaying fields.
     194     *
    135195     * @param  array $args Field data
    136      * @return void
    137196     */
    138197    public function display_field( $args ) {
     
    140199        $field = $args['field'];
    141200
    142         $html = '';
     201        $html = $data = '';
    143202
    144203        $option_name = $this->settings_base . $field['id'];
    145204        $option = get_option( $option_name );
    146205
    147         $data = '';
     206        //
    148207        if( isset( $field['default'] ) ) {
    149208            $data = $field['default'];
     
    153212        }
    154213
    155         switch( $field['type'] ) {
    156 
    157             case 'checkbox_multi':
    158                 foreach( $field['options'] as $k => $v ) {
    159                     $checked = false;
    160                     if( in_array( $k, $data ) ) {
    161                         $checked = true;
    162                     }
    163                     $html .= '<label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="checkbox" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '[]" value="' . esc_attr( $k ) . '" id="' . esc_attr( $field['id'] . '_' . $k ) . '" /> ' . $v . '</label><br>';
    164                 }
    165                 $html .= '<br/><span class="description">' . $field['description'] . '</span>';
    166             break;
    167 
    168         }
     214        // Loop through the post type options
     215        foreach( $field['options'] as $k => $v ) {
     216
     217            // Check our field if need be.
     218            $checked = false;
     219            if( in_array( $k, $data ) ) {
     220                $checked = true;
     221            }
     222
     223            // Main output
     224            $html .= '<label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="checkbox" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '[]" value="' . esc_attr( $k ) . '" id="' . esc_attr( $field['id'] . '_' . $k ) . '" /> ' . $v . '</label><br>';
     225
     226        }
     227
     228        $html .= '<br/><span class="description">' . $field['description'] . '</span>';
    169229
    170230        echo $html;
    171     }
     231
     232    }
     233
    172234
    173235    /**
    174236     * Validate individual settings field
     237     *
    175238     * @param  string $data Inputted value
    176239     * @return string       Validated value
     
    178241    public function validate_field( $data ) {
    179242
    180         if( $data && strlen( $data ) > 0 && $data != '' ) {
     243        if ( $data && strlen( $data ) > 0 && $data != '' ) {
    181244            $data = urlencode( strtolower( str_replace( ' ' , '-' , $data ) ) );
    182245        }
    183246
    184247        return $data;
    185     }
    186 
    187     /**
    188      * Load settings page content
    189      * @return void
     248
     249    }
     250
     251
     252    /**
     253     * Print the page content for the settings section of Envira Tamer.
     254     *
     255     * @see settings_fields, do_settings_sections
    190256     */
    191257    public function settings_page() {
     
    200266                // Get settings fields
    201267                ob_start();
    202                 settings_fields( 'envira_tamer' );
    203                 do_settings_sections( 'envira_tamer' );
     268
     269                    settings_fields( 'envira_tamer' );
     270                    do_settings_sections( 'envira_tamer' );
     271
    204272                $html .= ob_get_clean();
    205273
  • envira-tamer/trunk/envira-tamer.php

    r1011854 r1295326  
    55Description: Control which post types Envira Gallery meta box field shows up on
    66Author: Mike Selander
    7 Version: 1.0
     7Version: 1.0.1
    88Author URI: http://www.mikeselander.com/
     9License: GPLv2 or later
    910*/
    1011
    11 // Load the settings page if we're in the admin section
     12/*
     13 * Load the settings page if we're in the admin section
     14 */
    1215if ( is_admin() ){
    1316    require_once( 'admin/settings-page.php' );
     
    1518}
    1619
     20/*
     21 * Apply our settings to Envira to restrict the post types.
     22 *
     23 * @see envira_gallery_skipped_posttypes, get_option
     24 */
    1725add_filter( 'envira_gallery_skipped_posttypes', 'restrict_envira_post_types' );
    1826function restrict_envira_post_types( $rejects ){
  • envira-tamer/trunk/readme.txt

    r1011854 r1295326  
    44Tags: envira, gallery, custom post type, cpt, envira gallery, retrict
    55Requires at least: 3.8
    6 Tested up to: 4.0
    7 Stable tag: 1.0
     6Tested up to: 4.4
     7Stable tag: 1.0.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1212
    1313== Description ==
     14
     15This plugin appears to no longer be needed, but I am cleaning it up and maintaining it for those on legacy versions of Envira Gallery.
    1416
    1517Envira Gallery is an amazing gallery plugin, however it displays its metabox on every post type on your site. This is unnecessary and distracting for you and your clients. This plugin gives you an easy to use settings page that allows you to quickly change the post types that the metabox shows on.
     
    2729== Upgrade Notice ==
    2830
     31= 1.0.1 =
     32Cleaning up, commenting, and getting rid of unnecessary methods
     33
    2934= 1.0.0 =
    3035Initial Release
Note: See TracChangeset for help on using the changeset viewer.