PluginProbe
Powerkit – Supercharge your WordPress Site / trunk
Powerkit – Supercharge your WordPress Site vtrunk
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / basic-elements / templates / grid.php

grid.php in Powerkit – Supercharge your WordPress Site trunk, at modules/basic-elements/templates/grid.php

207 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shortcode Grid config
4 *
5 * @package Powerkit
6 * @subpackage Templates
7 */
8
9 // Exit if accessed directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Init module
16 */
17 class Powerkit_Basic_Grid {
18
19 /**
20 * Initialize.
21 */
22 public function __construct() {
23
24 if ( class_exists( 'Gridable' ) ) {
25 add_filter( 'gridable_row_class', array( $this, 'gridable_row_class' ) );
26 add_filter( 'gridable_column_class', array( $this, 'gridable_column_class' ), 10, 4 );
27 add_filter( 'gridable_load_public_style', '__return_false' );
28
29 } else {
30
31 add_shortcode( 'powerkit_row', array( $this, 'add_row_shortcode' ) );
32 add_shortcode( 'powerkit_col', array( $this, 'add_column_shortcode' ) );
33 add_shortcode( 'row', array( $this, 'add_row_shortcode' ) );
34 add_shortcode( 'col', array( $this, 'add_column_shortcode' ) );
35 add_filter( 'the_content', array( $this, 'parse_content_for_nested_rows' ), 9 );
36 add_filter( 'powerkit_the_column_content', array( $this, 'fix_lost_p_tags' ), 10, 2 );
37 }
38 }
39
40 /**
41 * Render the [powerkit-row]
42 *
43 * @param array $atts The atts.
44 * @param string $content The content.
45 */
46 public function add_row_shortcode( $atts, $content ) {
47 ob_start();
48 ?>
49 <div class="pk-row">
50 <?php
51 $row_content = apply_filters( 'powerkit_the_row_content', $content, $atts );
52
53 if ( apply_filters( 'powerkit_render_shortcodes_in_row', true, $content, $atts ) ) {
54 echo do_shortcode( $row_content );
55 } else {
56 echo wp_kses_post( $row_content );
57 }
58 ?>
59 </div>
60 <?php
61 return ob_get_clean();
62 }
63
64 /**
65 * Render the [powerkit-col]
66 *
67 * @param array $atts The atts.
68 * @param string $content The content.
69 */
70 public function add_column_shortcode( $atts, $content ) {
71 $size = 1;
72 if ( ! empty( $atts['size'] ) ) {
73 $size = (int) $atts['size'];
74 }
75
76 ob_start();
77 ?>
78 <div class="pk-col-md-<?php echo esc_attr( $size ); ?>">
79 <?php
80 $column_content = apply_filters( 'powerkit_the_column_content', $content, $atts );
81
82 if ( apply_filters( 'powerkit_render_shortcodes_in_column', true, $content, $atts ) ) {
83 echo do_shortcode( $column_content );
84 } else {
85 echo wp_kses_post( $column_content );
86 }
87 ?>
88 </div>
89 <?php
90 return ob_get_clean();
91 }
92
93 /**
94 * This function strips unclosed p tags at a beggining and at the end of a row
95 *
96 * @param string $content The content.
97 * @param array $atts The atts.
98 */
99 public function fix_lost_p_tags( $content, $atts ) {
100 if ( is_admin() ) {
101 return $content;
102 }
103
104 $first_4_chars = substr( $content, 0, 4 );
105
106 $last_3_chars = substr( $content, -3, 4 );
107
108 if ( '</p>' === $first_4_chars ) {
109 $content = substr( $content, 5 );
110 }
111
112 if ( '<p>' === $last_3_chars ) {
113 $content = substr( $content, 0, -4 );
114 }
115
116 return $content;
117 }
118
119 /**
120 * Try to allow one level of nested rows
121 *
122 * @param string $content The content.
123 * @param bool $rec The rec.
124 */
125 public function parse_content_for_nested_rows( $content, $rec = false ) {
126 $rows_matches = array();
127
128 preg_match_all( '#' . get_shortcode_regex( array( 'powerkit-row' ) ) . '#ims', $content, $rows_matches );
129
130 /**
131 * Basically in the first group of matches are the plain row texts
132 * If a row contains another row, we should render it before.
133 */
134 if ( ! empty( $rows_matches[0] ) ) {
135
136 // Iterate through each row and check if anyone has a nested row.
137 foreach ( $rows_matches[0] as $key => $match ) {
138
139 $row_pos = strpos( $rows_matches[0][ $key ], '[powerkit-row cols_nr="', 5 );
140
141 // If there is another row inside render it first.
142 if ( false !== $row_pos ) {
143 // Make a clone of the original row.
144 $temp_row = $match;
145 // If this row has an inner row, let's render it and replace it in the clone row.
146 preg_match( '#' . get_shortcode_regex( array( 'powerkit-row' ) ) . '#', $match, $smatch );
147 if ( substr_count( $smatch[0], '[powerkit-row ' ) > 1 ) {
148 $inner_rows = array();
149
150 // Right now the row form is [powerkit-row] content [powerkit-row]content[/powerkit-row]
151 // if we render the available rows we will have a nested-free row.
152 $remove_starting_row = '~\[' . $smatch[1] . $smatch[2] . $smatch[3] . '\]~';
153
154 $temp_content = preg_replace( $remove_starting_row, '', $smatch[0], 1 );
155
156 preg_match_all( '#' . get_shortcode_regex( array( 'powerkit-row' ) ) . '#ms', $temp_content, $inner_rows );
157
158 // There may be more than one inner row, catch'em all.
159 foreach ( $inner_rows[0] as $inner_row ) {
160 $temp_row = str_replace( $inner_row, do_shortcode( $inner_row ), $temp_row );
161 }
162 }
163 // Now we have a [powerkit-row] content <div class="pk-row"></div>
164 // the closing [/powerkit-row] is definetly somewhere after.
165 $content = str_replace( $match, $temp_row, $content );
166 } else {
167 if ( ! $rec ) {
168 $content = $this->parse_content_for_nested_rows( $content, true );
169 }
170 }
171 }
172 }
173
174 return $content;
175 }
176
177 /**
178 * -------------------------------------------------------------------------
179 * [ Support Gridable ]
180 * -------------------------------------------------------------------------
181 */
182
183 /**
184 * Row Class
185 */
186 public function gridable_row_class() {
187 return array( 'pk-row' );
188 }
189
190 /**
191 * Column Class
192 *
193 * @param array $classes Available classes.
194 * @param int $size Column size.
195 * @param array $atts Attributes.
196 * @param string $content Content.
197 */
198 public function gridable_column_class( $classes, $size, $atts, $content ) {
199
200 $classes = array( 'pk-col-md-' . $size );
201
202 return $classes;
203 }
204 }
205
206 new Powerkit_Basic_Grid();
207