| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper functions for the item template. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Helper functions for the item template. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_Template_Helper { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get attributes for the label container. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
*/ |
| 27 |
public static function get_label_container_atts() { |
| 28 |
return array( |
| 29 |
'icon' => array( |
| 30 |
'default' => '', |
| 31 |
'type' => 'icon', |
| 32 |
), |
| 33 |
'icon_color' => array( |
| 34 |
'default' => '#333333', |
| 35 |
'type' => 'color', |
| 36 |
'dependency' => array( |
| 37 |
'id' => 'icon', |
| 38 |
'value' => '', |
| 39 |
'type' => 'inverse', |
| 40 |
), |
| 41 |
), |
| 42 |
'label' => array( |
| 43 |
'default' => '', |
| 44 |
'type' => 'text', |
| 45 |
), |
| 46 |
'label_separator' => array( |
| 47 |
'default' => ' ', |
| 48 |
'type' => 'text', |
| 49 |
'dependency' => array( |
| 50 |
'id' => 'label', |
| 51 |
'value' => '', |
| 52 |
'type' => 'inverse', |
| 53 |
), |
| 54 |
), |
| 55 |
'label_style' => array( |
| 56 |
'default' => 'normal', |
| 57 |
'type' => 'dropdown', |
| 58 |
'options' => 'text_styles', |
| 59 |
'dependency' => array( |
| 60 |
'id' => 'label', |
| 61 |
'value' => '', |
| 62 |
'type' => 'inverse', |
| 63 |
), |
| 64 |
), |
| 65 |
'label_on_own_line' => array( |
| 66 |
'default' => '0', |
| 67 |
'type' => 'toggle', |
| 68 |
'dependency' => array( |
| 69 |
'id' => 'label', |
| 70 |
'value' => '', |
| 71 |
'type' => 'inverse', |
| 72 |
), |
| 73 |
), |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get label container. |
| 79 |
* |
| 80 |
* @since 3.0.0 |
| 81 |
* @param mixed $atts Attributes for the shortcode. |
| 82 |
* @param string $field Field to get the container for. |
| 83 |
*/ |
| 84 |
public static function get_label_container( $atts, $field ) { |
| 85 |
$field = str_replace( ' ', '', $field ); |
| 86 |
|
| 87 |
// Get optional icon. |
| 88 |
$icon = ''; |
| 89 |
if ( $atts['icon'] ) { |
| 90 |
$icon = WPUPG_Icon::get( $atts['icon'], $atts['icon_color'] ); |
| 91 |
|
| 92 |
if ( $icon ) { |
| 93 |
$icon = '<span class="wpupg-icon wpupg-item-' . esc_attr( $field ) . '-icon">' . $icon . '</span> '; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Get optional label. |
| 98 |
$label = ''; |
| 99 |
if ( $atts['label'] ) { |
| 100 |
$label = '<span class="wpupg-item-label wpupg-block-text-' . esc_attr( $atts['label_style'] ) . ' wpupg-item-' . esc_attr( $field ) . '-label">' . WPUPG_Shortcode::sanitize_html( __( $atts['label'], 'wp-ultimate-post-grid' ) . $atts['label_separator'] ) . '</span>'; |
| 101 |
} |
| 102 |
|
| 103 |
$label_container = ''; |
| 104 |
if ( $icon || $label ) { |
| 105 |
$tag = $atts['label_on_own_line'] ? 'div' : 'span'; |
| 106 |
$label_container = '<' . $tag . ' class="wpupg-item-label-container">' . $icon . $label . '</' . $tag . '>'; |
| 107 |
} |
| 108 |
|
| 109 |
return $label_container; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get attributes for limit text. |
| 114 |
* |
| 115 |
* @since 3.0.0 |
| 116 |
*/ |
| 117 |
public static function limit_text_atts() { |
| 118 |
return array( |
| 119 |
'limit_text' => array( |
| 120 |
'default' => '0', |
| 121 |
'type' => 'toggle', |
| 122 |
), |
| 123 |
'limit_type' => array( |
| 124 |
'default' => 'characters', |
| 125 |
'type' => 'dropdown', |
| 126 |
'options' => array( |
| 127 |
'characters' => 'Limit to X characters', |
| 128 |
'words' => 'Limit to X words', |
| 129 |
), |
| 130 |
'dependency' => array( |
| 131 |
'id' => 'limit_text', |
| 132 |
'value' => '1', |
| 133 |
), |
| 134 |
), |
| 135 |
'limit_number' => array( |
| 136 |
'default' => '20', |
| 137 |
'type' => 'number', |
| 138 |
'dependency' => array( |
| 139 |
'id' => 'limit_text', |
| 140 |
'value' => '1', |
| 141 |
), |
| 142 |
), |
| 143 |
'limit_suffix' => array( |
| 144 |
'default' => '…', |
| 145 |
'type' => 'text', |
| 146 |
'dependency' => array( |
| 147 |
'id' => 'limit_text', |
| 148 |
'value' => '1', |
| 149 |
), |
| 150 |
), |
| 151 |
'limit_ignore_html' => array( |
| 152 |
'default' => '0', |
| 153 |
'type' => 'toggle', |
| 154 |
'dependency' => array( |
| 155 |
'id' => 'limit_text', |
| 156 |
'value' => '1', |
| 157 |
), |
| 158 |
), |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Limit text length. |
| 164 |
* |
| 165 |
* @since 3.0.0 |
| 166 |
* @param mixed $atts Attributes for the shortcode. |
| 167 |
* @param string $text Text to limit. |
| 168 |
*/ |
| 169 |
public static function limit_text( $atts, $text ) { |
| 170 |
if ( $atts['limit_text'] ) { |
| 171 |
$limited = false; |
| 172 |
$limit = intval( $atts['limit_number'] ); |
| 173 |
|
| 174 |
if ( 0 < $limit ) { |
| 175 |
if ( (bool) $atts['limit_ignore_html'] ) { |
| 176 |
$text = wp_strip_all_tags( $text ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( 'words' === $atts['limit_type'] && $limit < str_word_count( $text, 0 ) ) { |
| 180 |
// Limit to X words. |
| 181 |
$words = str_word_count( $text, 2 ); |
| 182 |
$pos = array_keys( $words ); |
| 183 |
$text = substr( $text, 0, $pos[ $limit ] ); |
| 184 |
|
| 185 |
$limited = true; |
| 186 |
} elseif ( 'characters' === $atts['limit_type'] && $limit < strlen( $text ) ) { |
| 187 |
// Limit to X characters. |
| 188 |
$text = substr( $text, 0, $limit ); |
| 189 |
|
| 190 |
$limited = true; |
| 191 |
} |
| 192 |
|
| 193 |
if ( $limited ) { |
| 194 |
$text = self::limit_text_clean_html( $text ); |
| 195 |
$text = rtrim( $text ) . $atts['limit_suffix']; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return $text; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Clean limited text HTML. Should make sure any tags are closed again. |
| 205 |
* |
| 206 |
* @since 3.0.0 |
| 207 |
* @param mixed $html HTML to clean. |
| 208 |
*/ |
| 209 |
private static function limit_text_clean_html( $html ) { |
| 210 |
$doc = new DOMDocument(); |
| 211 |
libxml_use_internal_errors( true ); |
| 212 |
$doc->loadHTML( '<?xml version="1.0" encoding="UTF-8"?><html_tags>' . $html . '</html_tags>' ); |
| 213 |
libxml_clear_errors(); |
| 214 |
|
| 215 |
return substr( $doc->saveXML( $doc->getElementsByTagName( 'html_tags' )->item( 0 ) ), strlen( '<html_tags>' ), -strlen( '</html_tags>' ) ); |
| 216 |
} |
| 217 |
} |
| 218 |
|