Advertisement
Guest User

Untitled

a guest
Dec 16th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.67 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Plugin Name: Grid Columns
  4.  * Plugin URI: http://themehybrid.com/plugins/grid-columns
  5.  * Description: A [column] shortcode plugin.
  6.  * Version: 0.1.1
  7.  * Author: Justin Tadlock
  8.  * Author URI: http://justintadlock.com
  9.  *
  10.  * Grid Columns was created because of the sheer number of WordPress themes adding poorly-coded
  11.  * column shortcodes, which lock users into use that theme and system forever.  The plugin is
  12.  * meant to be an all-around solution for any WordPress user, regardless of theme, to be able
  13.  * to have columnized content using a simple [column] shortcode.
  14.  *
  15.  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  16.  * General Public License as published by the Free Software Foundation; either version 2 of the License,
  17.  * or (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  20.  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  * You should have received a copy of the GNU General Public License along with this program; if not, write
  23.  * to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24.  *
  25.  * @package   GridColumns
  26.  * @version   0.1.1
  27.  * @author    Justin Tadlock <justin@justintadlock.com>
  28.  * @copyright Copyright (c) 2012, Justin Tadlock
  29.  * @link      http://themehybrid.com/plugins/grid-columns
  30.  * @license   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  31.  */
  32.  
  33. class Grid_Columns {
  34.  
  35.     /**
  36.      * The current grid.
  37.      *
  38.      * @since  0.1.0
  39.      * @access public
  40.      * @var    int
  41.      */
  42.     public $grid = 4;
  43.  
  44.     /**
  45.      * The current total number of columns in the grid.
  46.      *
  47.      * @since  0.1.0
  48.      * @access public
  49.      * @var    int
  50.      */
  51.     public $span = 0;
  52.  
  53.     /**
  54.      * Whether we're viewing the first column.
  55.      *
  56.      * @since  0.1.0
  57.      * @access public
  58.      * @var    bool
  59.      */
  60.     public $is_first_column = true;
  61.  
  62.     /**
  63.      * Whether we're viewing the last column.
  64.      *
  65.      * @since  0.1.0
  66.      * @access public
  67.      * @var    bool
  68.      */
  69.     public $is_last_column = false;
  70.  
  71.     /**
  72.      * Allowed grids can be 2, 3, 4, 5, or 12 columns.
  73.      *
  74.      * @since  0.1.0
  75.      * @access public
  76.      * @var    array
  77.      */
  78.     public $allowed_grids = array( 2, 3, 4, 5, 12 );
  79.  
  80.     /**
  81.      * Sets up our actions/filters.
  82.      *
  83.      * @since 0.1.0
  84.      * @access public
  85.      * @return void
  86.      */
  87.     public function __construct() {
  88.  
  89.         /* Register shortcodes on 'init'. */
  90.         add_action( 'init', array( &$this, 'register_shortcode' ) );
  91.  
  92.         /* Enqueue stylesheets on 'wp_enqueue_scripts'. */
  93.         add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_styles' ), 1 );
  94.  
  95.         /* Apply filters to the column content. */
  96.         add_filter( 'gc_column_content', 'wpautop' );
  97.         add_filter( 'gc_column_content', 'shortcode_unautop' );
  98.         add_filter( 'gc_column_content', 'do_shortcode' );
  99.     }
  100.  
  101.     /**
  102.      * Registers the [column] shortcode.
  103.      *
  104.      * @since  0.1.0
  105.      * @access public
  106.      * @return void
  107.      */
  108.     public function register_shortcode() {
  109.         add_shortcode( 'column', array( &$this, 'do_shortcode' ) );
  110.     }
  111.  
  112.     /**
  113.      * Enqueues the columns.css stylesheet to make the columns pretty.
  114.      *
  115.      * @since  0.1.0
  116.      * @access public
  117.      * @return void
  118.      */
  119.     public function enqueue_styles() {
  120.  
  121.         /* Use the .min stylesheet if SCRIPT_DEBUG is turned off. */
  122.         $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  123.  
  124.         /* Enqueue the stylesheet. */
  125.         wp_enqueue_style(
  126.             'grid-columns',
  127.             trailingslashit( plugin_dir_url( __FILE__ ) ) . "css/columns$suffix.css",
  128.             null,
  129.             '20121007'
  130.         );
  131.     }
  132.  
  133.     /**
  134.      * Returns the content of the column shortcode.
  135.      *
  136.      * @since  0.1.0
  137.      * @access public
  138.      * @param  array  $attr The user-inputted arguments.
  139.      * @param  string $content The content to wrap in a shortcode.
  140.      * @return string
  141.      */
  142.     public function do_shortcode( $attr, $content = null ) {
  143.  
  144.         /* If there's no content, just return back what we got. */
  145.         if ( is_null( $content ) )
  146.             return $content;
  147.  
  148.         /* Set up the default variables. */
  149.         $output = '';
  150.         $row_classes = array();
  151.         $column_classes = array();
  152.  
  153.         /* Set up the default arguments. */
  154.         $defaults = apply_filters(
  155.             'gc_column_defaults',
  156.             array(
  157.                 'grid'  => $this->grid,
  158.                 'span'  => 1,
  159.                 'push'  => 0,
  160.                 'class' => ''
  161.             )
  162.         );
  163.  
  164.         /* Parse the arguments. */
  165.         $attr = shortcode_atts( $defaults, $attr );
  166.  
  167.         /* Allow devs to filter the arguments. */
  168.         $attr = apply_filters( 'gc_column_args', $attr );
  169.  
  170.         /* Allow devs to overwrite the allowed grids. */
  171.         $this->allowed_grids = apply_filters( 'gc_allowed_grids', $this->allowed_grids );
  172.  
  173.         /* Make sure the grid is in the allowed grids array. */
  174.         if ( $this->is_first_column && in_array( $attr['grid'], $this->allowed_grids ) )
  175.             $this->grid = absint( $attr['grid'] );
  176.  
  177.         /* Span cannot be greater than the grid. */
  178.         $attr['span'] = ( $this->grid >= $attr['span'] ) ? absint( $attr['span'] ) : 1;
  179.  
  180.         /* The push argument should always be less than the grid. */
  181.         $attr['push'] = ( $this->grid > $attr['push'] ) ? absint( $attr['push'] ) : 0;
  182.  
  183.         /* Add to the total $span. */
  184.         $this->span = $this->span + $attr['span'] + $attr['push'];
  185.  
  186.         /* Column classes. */
  187.         $column_classes[] = 'column';
  188.         $column_classes[] = "column-span-{$attr['span']}";
  189.         $column_classes[] = "column-push-{$attr['push']}";
  190.  
  191.         /* Add user-input custom class(es). */
  192.         if ( !empty( $attr['class'] ) ) {
  193.             if ( !is_array( $attr['class'] ) )
  194.                 $attr['class'] = preg_split( '#\s+#', $attr['class'] );
  195.             $column_classes = array_merge( $column_classes, $attr['class'] );
  196.         }
  197.  
  198.         /* Add the 'column-first' class if this is the first column. */
  199.         if ( $this->is_first_column )
  200.             $column_classes[] = 'column-first';
  201.  
  202.         /* If the $span property is greater than (shouldn't be) or equal to the $grid property. */
  203.         if ( $this->span >= $this->grid ) {
  204.  
  205.             /* Add the 'column-last' class. */
  206.             $column_classes[] = 'column-last';
  207.  
  208.             /* Set the $is_last_column property to true. */
  209.             $this->is_last_column = true;
  210.         }
  211.  
  212.         /* Object properties. */
  213.         $object_vars = get_object_vars( $this );
  214.  
  215.         /* Allow devs to create custom classes. */
  216.         $column_classes = apply_filters( 'gc_column_class', $column_classes, $attr, $object_vars );
  217.  
  218.         /* Sanitize and join all classes. */
  219.         $column_class = join( ' ', array_map( 'sanitize_html_class', array_unique( $column_classes ) ) );
  220.  
  221.         /* Output */
  222.  
  223.         /* If this is the first column. */
  224.         if ( $this->is_first_column ) {
  225.  
  226.             /* Row classes. */
  227.             $row_classes = array( 'column-grid', "column-grid-{$this->grid}" );
  228.             $row_classes = apply_filters( 'gc_row_class', $row_classes, $attr, $object_vars );
  229.             $row_class = join( ' ', array_map( 'sanitize_html_class', array_unique( $row_classes ) ) );
  230.  
  231.             /* Open a wrapper <div> to contain the columns. */
  232.             $output .= '<div class="' . $row_class . '">';
  233.  
  234.             /* Set the $is_first_column property back to false. */
  235.             $this->is_first_column = false;
  236.         }
  237.  
  238.         /* Add the current column to the output. */
  239.         $output .= '<div class="' . $column_class . '">' . apply_filters( 'gc_column_content', $content ) . '</div>';
  240.  
  241.         /* If this is the last column. */
  242.         if ( $this->is_last_column ) {
  243.  
  244.             /* Close the wrapper. */
  245.             $output .= '</div>';
  246.  
  247.             /* Reset the properties that have been changed. */
  248.             $this->reset();
  249.         }
  250.  
  251.         /* Return the output of the column. */
  252.         return apply_filters( 'gc_column', $output );
  253.     }
  254.  
  255.     /**
  256.      * Resets the properties to their original states.
  257.      *
  258.      * @since  0.1.0
  259.      * @access public
  260.      * @return void
  261.      */
  262.     public function reset() {
  263.  
  264.         foreach ( get_class_vars( __CLASS__ ) as $name => $default )
  265.             $this->$name = $default;
  266.     }
  267. }
  268.  
  269. new Grid_Columns();
  270.  
  271. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement