Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 298 lines (245 sloc) 8.005 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
<?php
/*
* Plugin Name: Grid Columns
* Plugin URI: http://themehybrid.com/plugins/grid-columns
* Description: A [column] shortcode plugin.
* Version: 0.2.0
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*
* Grid Columns was created because of the sheer number of WordPress themes adding poorly-coded
* column shortcodes, which lock users into use that theme and system forever. The plugin is
* meant to be an all-around solution for any WordPress user, regardless of theme, to be able
* to have columnized content using a simple [column] shortcode.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package GridColumns
* @version 0.2.0
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2012 - 2013, Justin Tadlock
* @link http://themehybrid.com/plugins/grid-columns
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

class Grid_Columns {

/**
* Holds the instance of this class.
*
* @since 0.2.0
* @access private
* @var object
*/
private static $instance;

/**
* The current grid.
*
* @since 0.1.0
* @access public
* @var int
*/
public $grid = 4;

/**
* The current total number of columns in the grid.
*
* @since 0.1.0
* @access public
* @var int
*/
public $span = 0;

/**
* Whether we're viewing the first column.
*
* @since 0.1.0
* @access public
* @var bool
*/
public $is_first_column = true;

/**
* Whether we're viewing the last column.
*
* @since 0.1.0
* @access public
* @var bool
*/
public $is_last_column = false;

/**
* Allowed grids can be 2, 3, 4, 5, or 12 columns.
*
* @since 0.1.0
* @access public
* @var array
*/
public $allowed_grids = array( 2, 3, 4, 5, 12 );

/**
* Sets up our actions/filters.
*
* @since 0.1.0
* @access public
* @return void
*/
public function __construct() {

/* Register shortcodes on 'init'. */
add_action( 'init', array( $this, 'register_shortcode' ) );

/* Enqueue stylesheets on 'wp_enqueue_scripts'. */
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ), 1 );

/* Apply filters to the column content. */
add_filter( 'gc_column_content', 'wpautop' );
add_filter( 'gc_column_content', 'shortcode_unautop' );
add_filter( 'gc_column_content', 'do_shortcode' );
}

/**
* Registers the [column] shortcode.
*
* @since 0.1.0
* @access public
* @return void
*/
public function register_shortcode() {
add_shortcode( 'column', array( $this, 'do_shortcode' ) );
}

/**
* Enqueues the columns.css stylesheet to make the columns pretty.
*
* @since 0.1.0
* @access public
* @return void
*/
public function enqueue_styles() {

/* Use the .min stylesheet if SCRIPT_DEBUG is turned off. */
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

/* Enqueue the stylesheet. */
wp_enqueue_style(
'grid-columns',
trailingslashit( plugin_dir_url( __FILE__ ) ) . "css/columns$suffix.css",
null,
'20130123'
);
}

/**
* Returns the content of the column shortcode.
*
* @since 0.1.0
* @access public
* @param array $attr The user-inputted arguments.
* @param string $content The content to wrap in a shortcode.
* @return string
*/
public function do_shortcode( $attr, $content = null ) {

/* If there's no content, just return back what we got. */
if ( is_null( $content ) )
return $content;

/* Set up the default variables. */
$output = '';
$row_classes = array();
$column_classes = array();

/* Set up the default arguments. */
$defaults = apply_filters(
'gc_column_defaults',
array(
'grid' => $this->grid,
'span' => 1,
'push' => 0,
'class' => ''
)
);

/* Parse the arguments. */
$attr = shortcode_atts( $defaults, $attr );

/* Allow devs to filter the arguments. */
$attr = apply_filters( 'gc_column_args', $attr );

/* Allow devs to overwrite the allowed grids. */
$this->allowed_grids = apply_filters( 'gc_allowed_grids', $this->allowed_grids );

/* Make sure the grid is in the allowed grids array. */
if ( $this->is_first_column && in_array( $attr['grid'], $this->allowed_grids ) )
$this->grid = absint( $attr['grid'] );

/* Span cannot be greater than the grid. */
$attr['span'] = ( $this->grid >= $attr['span'] ) ? absint( $attr['span'] ) : 1;

/* The push argument should always be less than the grid. */
$attr['push'] = ( $this->grid > $attr['push'] ) ? absint( $attr['push'] ) : 0;

/* Add to the total $span. */
$this->span = $this->span + $attr['span'] + $attr['push'];

/* Column classes. */
$column_classes[] = 'column';
$column_classes[] = "column-span-{$attr['span']}";
$column_classes[] = "column-push-{$attr['push']}";

/* Add user-input custom class(es). */
if ( !empty( $attr['class'] ) ) {
if ( !is_array( $attr['class'] ) )
$attr['class'] = preg_split( '#\s+#', $attr['class'] );
$column_classes = array_merge( $column_classes, $attr['class'] );
}

/* Add the 'column-first' class if this is the first column. */
if ( $this->is_first_column )
$column_classes[] = 'column-first';

/* If the $span property is greater than (shouldn't be) or equal to the $grid property. */
if ( $this->span >= $this->grid ) {

/* Add the 'column-last' class. */
$column_classes[] = 'column-last';

/* Set the $is_last_column property to true. */
$this->is_last_column = true;
}

/* Object properties. */
$object_vars = get_object_vars( $this );

/* Allow devs to create custom classes. */
$column_classes = apply_filters( 'gc_column_class', $column_classes, $attr, $object_vars );

/* Sanitize and join all classes. */
$column_class = join( ' ', array_map( 'sanitize_html_class', array_unique( $column_classes ) ) );

/* Output */

/* If this is the first column. */
if ( $this->is_first_column ) {

/* Row classes. */
$row_classes = array( 'column-grid', "column-grid-{$this->grid}" );
$row_classes = apply_filters( 'gc_row_class', $row_classes, $attr, $object_vars );
$row_class = join( ' ', array_map( 'sanitize_html_class', array_unique( $row_classes ) ) );

/* Open a wrapper <div> to contain the columns. */
$output .= '<div class="' . $row_class . '">';

/* Set the $is_first_column property back to false. */
$this->is_first_column = false;
}

/* Add the current column to the output. */
$output .= '<div class="' . $column_class . '">' . apply_filters( 'gc_column_content', $content ) . '</div>';

/* If this is the last column. */
if ( $this->is_last_column ) {

/* Close the wrapper. */
$output .= '</div>';

/* Reset the properties that have been changed. */
$this->reset();
}

/* Return the output of the column. */
return apply_filters( 'gc_column', $output );
}

/**
* Resets the properties to their original states.
*
* @since 0.1.0
* @access public
* @return void
*/
public function reset() {

foreach ( get_class_vars( __CLASS__ ) as $name => $default ) {

if ( 'instance' !== $name )
$this->$name = $default;
}
}

/**
* Returns the instance.
*
* @since 0.2.0
* @access public
* @return object
*/
public static function get_instance() {

if ( !self::$instance )
self::$instance = new self;

return self::$instance;
}
}

Grid_Columns::get_instance();

?>
Something went wrong with that request. Please try again.