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