/wp-content/themes/michael_karas/shortcodes.php

https://bitbucket.org/rossberenson/michael-karas · PHP · 281 lines · 110 code · 45 blank · 126 comment · 9 complexity · e404c82c6a753e9bd229d17dd1b1cc94 MD5 · raw file

  1. <?php
  2. /**
  3. * Plugin Name: r+ Columns Shortcode
  4. * Plugin URI: http://themes.required.ch/
  5. * Description: A [column] shortcode plugin for the required+ Foundation parent theme and child themes, based on <a href="http://themehybrid.com/plugins/grid-columns">GridColumns by Justin Tadlock</a>.
  6. * Version: 0.1.2
  7. * Author: required+ Team
  8. * Author URI: http://required.ch
  9. *
  10. * @package required+ Foundation
  11. * @version 0.1.2
  12. * @author Silvan Hagen <silvan@required.ch>
  13. * @copyright Copyright (c) 2012, Silvan Hagen
  14. * @link http://themes.required.ch/theme-features/shortcodes/
  15. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  16. */
  17. /**
  18. * REQ_Grid_Columns Shortcode Class
  19. *
  20. * @version 0.1.0
  21. */
  22. class REQ_Column_Shortcode {
  23. /**
  24. * The columns in our grid
  25. *
  26. * @since 0.1.0
  27. * @access public
  28. * @var int
  29. */
  30. public $grid = 12;
  31. /**
  32. * The current total number of columns in the grid.
  33. *
  34. * @since 0.1.0
  35. * @access public
  36. * @var int
  37. */
  38. public $columns = 0;
  39. /**
  40. * Whether we're viewing the first column.
  41. *
  42. * @since 0.1.0
  43. * @access public
  44. * @var bool
  45. */
  46. public $is_first_column = true;
  47. /**
  48. * Whether we're viewing the last column.
  49. *
  50. * @since 0.1.0
  51. * @access public
  52. * @var bool
  53. */
  54. public $is_last_column = false;
  55. /**
  56. * Sets up our actions/filters.
  57. *
  58. * @since 0.1.0
  59. * @access public
  60. * @return void
  61. */
  62. public function __construct() {
  63. /* Register shortcodes on 'init'. */
  64. add_action( 'init', array( &$this, 'register_shortcode' ) );
  65. /* Apply filters to the column content. */
  66. add_filter( 'req_column_content', 'wpautop' );
  67. add_filter( 'req_column_content', 'shortcode_unautop' );
  68. add_filter( 'req_column_content', 'do_shortcode' );
  69. }
  70. /**
  71. * Convert int into a word for our column classes
  72. *
  73. * @since 0.1.0
  74. * @access protected
  75. * @param int $int
  76. * @return string $word
  77. */
  78. protected function convert_int_to_word( $int ) {
  79. // Make sure it's an integer
  80. absint( $int );
  81. switch( $int ) {
  82. case 1: $word = "one"; break;
  83. case 2: $word = "two"; break;
  84. case 3: $word = "three"; break;
  85. case 4: $word = "four"; break;
  86. case 5: $word = "five"; break;
  87. case 6: $word = "six"; break;
  88. case 7: $word = "seven"; break;
  89. case 8: $word = "eight"; break;
  90. case 9: $word = "nine"; break;
  91. case 10: $word = "ten"; break;
  92. case 11: $word = "eleven"; break;
  93. case 12: $word = "twelve"; break;
  94. case 0:
  95. default:
  96. $word = "zero"; break;
  97. }
  98. return $word;
  99. }
  100. /**
  101. * Convert word to int for legacy support of old colmun shortcodes
  102. *
  103. * @since 0.1.0
  104. * @access protected
  105. * @param string $word
  106. * @return int $int
  107. */
  108. protected function convert_word_to_int( $word ) {
  109. switch( $word ) {
  110. case "one": $int = 1; break;
  111. case "two": $int = 2; break;
  112. case "three": $int = 3; break;
  113. case "four": $int = 4; break;
  114. case "five": $int = 5; break;
  115. case "six": $int = 6; break;
  116. case "seven": $int = 7; break;
  117. case "eight": $int = 8; break;
  118. case "nine": $int = 9; break;
  119. case "ten": $int = 10; break;
  120. case "eleven": $int = 11; break;
  121. case "twelve": $int = 12; break;
  122. case "zero":
  123. default:
  124. $int = 0; break;
  125. }
  126. return $int;
  127. }
  128. /**
  129. * Registers the [column] shortcode.
  130. *
  131. * @since 0.1.0
  132. * @access public
  133. * @return void
  134. */
  135. public function register_shortcode() {
  136. add_shortcode( 'column', array( &$this, 'do_shortcode' ) );
  137. }
  138. /**
  139. * Returns the content of the column shortcode.
  140. *
  141. * @since 0.1.0
  142. * @access public
  143. * @param array $attr The user-inputted arguments.
  144. * @param string $content The content to wrap in a shortcode.
  145. * @return string
  146. */
  147. public function do_shortcode( $attr, $content = null ) {
  148. /* If there's no content, just return back what we got. */
  149. if ( is_null( $content ) )
  150. return $content;
  151. /* Set up the default variables. */
  152. $output = '';
  153. $row_classes = array();
  154. $column_classes = array();
  155. /* Set up the default arguments. */
  156. $defaults = apply_filters(
  157. 'req_column_defaults',
  158. array(
  159. 'columns' => 1,
  160. 'offset' => 0,
  161. 'class' => ''
  162. )
  163. );
  164. /* Parse the arguments. */
  165. $attr = shortcode_atts( $defaults, $attr );
  166. /* Allow devs to filter the arguments. */
  167. $attr = apply_filters( 'req_column_args', $attr );
  168. /* Legacy support for old column shortcode */
  169. if ( !is_numeric( $attr['columns'] ) )
  170. $attr['columns'] = $this->convert_word_to_int( $attr['columns'] );
  171. /* Columns cannot be greater than the grid. */
  172. $attr['columns'] = ( $this->grid >= $attr['columns'] ) ? absint( $attr['columns'] ) : 3;
  173. /* The offset argument should always be less than the grid. */
  174. $attr['offset'] = ( $this->grid > $attr['offset'] ) ? absint( $attr['offset'] ) : 0;
  175. /* Add to the total $columns. */
  176. $this->columns = $this->columns + $attr['columns'] + $attr['offset'];
  177. /* Column classes. */
  178. $column_classes[] = 'columns';
  179. $column_classes[] = $this->convert_int_to_word( $attr['columns'] );
  180. if ( $attr['offset'] !== 0 ) // Offset is only necessary if it's not 0
  181. $column_classes[] = "offset-by-{$this->convert_int_to_word( $attr['offset'] )}";
  182. /* Add user-input custom class(es). */
  183. if ( !empty( $attr['class'] ) ) {
  184. if ( !is_array( $attr['class'] ) )
  185. $attr['class'] = preg_split( '#\s+#', $attr['class'] );
  186. $column_classes = array_merge( $column_classes, $attr['class'] );
  187. }
  188. /* If the $span property is greater than (shouldn't be) or equal to the $grid property. */
  189. if ( $this->columns >= $this->grid ) {
  190. /* Set the $is_last_column property to true. */
  191. $this->is_last_column = true;
  192. }
  193. /* Object properties. */
  194. $object_vars = get_object_vars( $this );
  195. /* Allow devs to create custom classes. */
  196. $column_classes = apply_filters( 'req_column_class', $column_classes, $attr, $object_vars );
  197. /* Sanitize and join all classes. */
  198. $column_class = join( ' ', array_map( 'sanitize_html_class', array_unique( $column_classes ) ) );
  199. /* Output */
  200. /* If this is the first column. */
  201. if ( $this->is_first_column ) {
  202. /* Open a wrapper <div> to contain the columns. */
  203. $output .= '<div class="row">';
  204. /* Set the $is_first_column property back to false. */
  205. $this->is_first_column = false;
  206. }
  207. /* Add the current column to the output. */
  208. $output .= '<div class="' . $column_class . '">' . apply_filters( 'req_column_content', $content ) . '</div>';
  209. /* If this is the last column. */
  210. if ( $this->is_last_column ) {
  211. /* Close the wrapper. */
  212. $output .= '</div>';
  213. /* Reset the properties that have been changed. */
  214. $this->reset();
  215. }
  216. /* Return the output of the column. */
  217. return apply_filters( 'req_column', $output );
  218. }
  219. /**
  220. * Resets the properties to their original states.
  221. *
  222. * @since 0.1.0
  223. * @access public
  224. * @return void
  225. */
  226. public function reset() {
  227. foreach ( get_class_vars( __CLASS__ ) as $name => $default )
  228. $this->$name = $default;
  229. }
  230. }
  231. /**
  232. * If you prefer the shortcode by http://themehybrid.com/plugins/grid-columns
  233. * please go ahead and use it. We don't stop you!
  234. */
  235. if ( ! class_exists( 'Grid_Columns' ) )
  236. new REQ_Column_Shortcode();