| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Meta Helper Functions |
| 4 |
* |
| 5 |
* These helper functions return post meta. |
| 6 |
* |
| 7 |
* @package Powerkit |
| 8 |
* @subpackage Core |
| 9 |
* @version 1.0.0 |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! function_exists( 'powerkit_allowed_post_meta' ) ) { |
| 14 |
/** |
| 15 |
* Allowed Post Meta |
| 16 |
* |
| 17 |
* @param bool $compact If compact version shall be displayed. |
| 18 |
* @param array $exclude Exclude list. |
| 19 |
*/ |
| 20 |
function powerkit_allowed_post_meta( $compact = false, $exclude = array() ) { |
| 21 |
$allowed = array( |
| 22 |
'category' => esc_html__( 'Category', 'powerkit' ), |
| 23 |
'date' => esc_html__( 'Date', 'powerkit' ), |
| 24 |
'author' => esc_html__( 'Author', 'powerkit' ), |
| 25 |
'comments' => esc_html__( 'Comments count', 'powerkit' ), |
| 26 |
); |
| 27 |
|
| 28 |
if ( powerkit_post_views_enabled() ) { |
| 29 |
$allowed['views'] = esc_html__( 'Views', 'powerkit' ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( powerkit_module_enabled( 'reading_time' ) ) { |
| 33 |
$allowed['reading_time'] = esc_html__( 'Reading time', 'powerkit' ); |
| 34 |
} |
| 35 |
|
| 36 |
$allowed = apply_filters( 'powerkit_allowed_post_meta', $allowed ); |
| 37 |
|
| 38 |
// Computes the difference of arrays. |
| 39 |
$allowed = array_diff_key( $allowed, array_flip( (array) $exclude ) ); |
| 40 |
|
| 41 |
// If compact. |
| 42 |
if ( $compact ) { |
| 43 |
return array_keys( $allowed ); |
| 44 |
} |
| 45 |
|
| 46 |
return $allowed; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! function_exists( 'powerkit_block_post_meta' ) ) { |
| 51 |
/** |
| 52 |
* Block Post Meta |
| 53 |
* |
| 54 |
* A wrapper function that returns all post meta types either |
| 55 |
* in an ordered list <ul> or as a single element <span>. |
| 56 |
* |
| 57 |
* @param array $settings Settings of block. |
| 58 |
* @param mixed $meta Contains post meta types. |
| 59 |
* @param bool $echo Echo or return. |
| 60 |
* @param bool $compact If meta compact. |
| 61 |
*/ |
| 62 |
function powerkit_block_post_meta( $settings, $meta, $echo = true, $compact = false ) { |
| 63 |
|
| 64 |
$allowed = array(); |
| 65 |
|
| 66 |
if ( isset( $settings['showMetaCategory'] ) && $settings['showMetaCategory'] ) { |
| 67 |
$allowed[] = 'category'; |
| 68 |
} |
| 69 |
|
| 70 |
if ( isset( $settings['showMetaAuthor'] ) && $settings['showMetaAuthor'] ) { |
| 71 |
$allowed[] = 'author'; |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $settings['showMetaDate'] ) && $settings['showMetaDate'] ) { |
| 75 |
$allowed[] = 'date'; |
| 76 |
} |
| 77 |
|
| 78 |
if ( isset( $settings['showMetaComments'] ) && $settings['showMetaComments'] ) { |
| 79 |
$allowed[] = 'comments'; |
| 80 |
} |
| 81 |
|
| 82 |
if ( isset( $settings['showMetaViews'] ) && $settings['showMetaViews'] ) { |
| 83 |
$allowed[] = 'views'; |
| 84 |
} |
| 85 |
|
| 86 |
if ( isset( $settings['showMetaReadingTime'] ) && $settings['showMetaReadingTime'] ) { |
| 87 |
$allowed[] = 'reading_time'; |
| 88 |
} |
| 89 |
|
| 90 |
if ( isset( $settings['showMetaShares'] ) && $settings['showMetaShares'] ) { |
| 91 |
$allowed[] = 'shares'; |
| 92 |
} |
| 93 |
|
| 94 |
if ( isset( $settings['metaCompact'] ) && $settings['metaCompact'] ) { |
| 95 |
$compact = true; |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! $allowed ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
powerkit_get_post_meta( $meta, $compact, $echo, $allowed ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! function_exists( 'powerkit_get_post_meta' ) ) { |
| 107 |
/** |
| 108 |
* Post Meta |
| 109 |
* |
| 110 |
* A wrapper function that returns all post meta types either |
| 111 |
* in an ordered list <ul> or as a single element <span>. |
| 112 |
* |
| 113 |
* @param mixed $meta Contains post meta types. |
| 114 |
* @param bool $compact If compact version shall be displayed. |
| 115 |
* @param bool $echo Echo or return. |
| 116 |
* @param array $allowed Allowed meta types. |
| 117 |
*/ |
| 118 |
function powerkit_get_post_meta( $meta, $compact = false, $echo = true, $allowed = array() ) { |
| 119 |
|
| 120 |
$func_handler = apply_filters( 'powerkit_get_post_meta_handler', false ); |
| 121 |
|
| 122 |
if ( $func_handler ) { |
| 123 |
return call_user_func( $func_handler, $meta, $compact, $echo, $allowed ); |
| 124 |
} |
| 125 |
|
| 126 |
// Return if no post meta types provided. |
| 127 |
if ( ! $meta ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// Set default allowed post meta types. |
| 132 |
if ( ! $allowed ) { |
| 133 |
$allowed = powerkit_allowed_post_meta(); |
| 134 |
} |
| 135 |
|
| 136 |
if ( is_array( $meta ) ) { |
| 137 |
// Intersect provided and allowed meta types. |
| 138 |
$meta = array_intersect( $allowed, $meta ); |
| 139 |
} |
| 140 |
|
| 141 |
$output = null; |
| 142 |
|
| 143 |
if ( $meta && is_array( $meta ) ) { |
| 144 |
|
| 145 |
$output .= '<ul class="pk-post-meta post-meta">'; |
| 146 |
|
| 147 |
// Add normal meta types to the list. |
| 148 |
foreach ( $meta as $type ) { |
| 149 |
$function = "powerkit_get_meta_$type"; |
| 150 |
|
| 151 |
if ( function_exists( $function ) ) { |
| 152 |
$output .= $function( 'li', $compact ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$output .= '</ul>'; |
| 157 |
|
| 158 |
} else { |
| 159 |
if ( in_array( $meta, $allowed, true ) ) { |
| 160 |
// Output single meta type. |
| 161 |
$function = "powerkit_get_meta_$meta"; |
| 162 |
|
| 163 |
if ( function_exists( $function ) ) { |
| 164 |
$output .= $function( 'div', $compact ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if ( $echo ) { |
| 170 |
echo (string) $output; // XSS ok. |
| 171 |
} else { |
| 172 |
return $output; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! function_exists( 'powerkit_get_meta_date' ) ) { |
| 178 |
/** |
| 179 |
* Post Date |
| 180 |
* |
| 181 |
* @param string $tag Element tag, i.e. div or span. |
| 182 |
* @param bool $compact If compact version shall be displayed. |
| 183 |
*/ |
| 184 |
function powerkit_get_meta_date( $tag = 'span', $compact = false ) { |
| 185 |
|
| 186 |
$output = '<' . esc_html( $tag ) . ' class="pk-meta-date meta-date">'; |
| 187 |
|
| 188 |
if ( false === $compact ) { |
| 189 |
$time_string = get_the_date(); |
| 190 |
} else { |
| 191 |
$time_string = get_the_date( 'd.m.y' ); |
| 192 |
} |
| 193 |
|
| 194 |
$output .= apply_filters( 'powerkit_post_meta_date_output', '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>' ); |
| 195 |
|
| 196 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 197 |
|
| 198 |
return $output; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if ( ! function_exists( 'powerkit_get_meta_author' ) ) { |
| 203 |
/** |
| 204 |
* Post Author |
| 205 |
* |
| 206 |
* @param string $tag Element tag, i.e. div or span. |
| 207 |
* @param bool $compact If compact version shall be displayed. |
| 208 |
*/ |
| 209 |
function powerkit_get_meta_author( $tag = 'span', $compact = false ) { |
| 210 |
|
| 211 |
$authors = array( get_the_author_meta( 'ID' ) ); |
| 212 |
|
| 213 |
$output = '<' . esc_html( $tag ) . ' class="pk-meta-author meta-author">'; |
| 214 |
|
| 215 |
$output .= sprintf( |
| 216 |
'<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>', |
| 217 |
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), |
| 218 |
/* translators: %s: author name */ |
| 219 |
esc_attr( sprintf( __( 'View all posts by %s', 'powerkit' ), get_the_author() ) ), |
| 220 |
// Author. |
| 221 |
get_the_author() |
| 222 |
); |
| 223 |
|
| 224 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 225 |
|
| 226 |
return $output; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if ( ! function_exists( 'powerkit_get_meta_category' ) ) { |
| 231 |
/** |
| 232 |
* Post Сategory |
| 233 |
* |
| 234 |
* @param string $tag Element tag, i.e. div or span. |
| 235 |
* @param bool $compact If compact version shall be displayed. |
| 236 |
* @param int $post_id Post ID. |
| 237 |
*/ |
| 238 |
function powerkit_get_meta_category( $tag = 'span', $compact = false, $post_id = null ) { |
| 239 |
|
| 240 |
if ( ! $post_id ) { |
| 241 |
$post_id = get_the_ID(); |
| 242 |
} |
| 243 |
|
| 244 |
$output = '<' . esc_html( $tag ) . ' class="pk-meta-category meta-category">'; |
| 245 |
|
| 246 |
$output .= get_the_category_list( '', '', $post_id ); |
| 247 |
|
| 248 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 249 |
|
| 250 |
return $output; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if ( ! function_exists( 'powerkit_get_meta_comments' ) ) { |
| 255 |
/** |
| 256 |
* Post Comments |
| 257 |
* |
| 258 |
* @param string $tag Element tag, i.e. div or span. |
| 259 |
* @param bool $compact If compact version shall be displayed. |
| 260 |
*/ |
| 261 |
function powerkit_get_meta_comments( $tag = 'span', $compact = false ) { |
| 262 |
$output = ''; |
| 263 |
|
| 264 |
$output .= '<' . esc_html( $tag ) . ' class="pk-meta-comments meta-comments">'; |
| 265 |
|
| 266 |
if ( true === $compact ) { |
| 267 |
$output .= '<i class="pk-icon pk-icon-comment"></i>'; |
| 268 |
ob_start(); |
| 269 |
comments_popup_link( '0', '1', '%', 'comments-link', '' ); |
| 270 |
$output .= ob_get_clean(); |
| 271 |
} else { |
| 272 |
ob_start(); |
| 273 |
comments_popup_link( esc_html__( 'No comments', 'powerkit' ), esc_html__( 'One comment', 'powerkit' ), '% ' . esc_html__( 'comments', 'powerkit' ), 'comments-link', '' ); |
| 274 |
$output .= ob_get_clean(); |
| 275 |
} |
| 276 |
|
| 277 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 278 |
|
| 279 |
return $output; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if ( ! function_exists( 'powerkit_get_meta_reading_time' ) ) { |
| 284 |
/** |
| 285 |
* Post Reading Time |
| 286 |
* |
| 287 |
* @param string $tag Element tag, i.e. div or span. |
| 288 |
* @param bool $compact If compact version shall be displayed. |
| 289 |
*/ |
| 290 |
function powerkit_get_meta_reading_time( $tag = 'span', $compact = false ) { |
| 291 |
|
| 292 |
if ( ! powerkit_module_enabled( 'reading_time' ) ) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
$reading_time = powerkit_get_post_reading_time(); |
| 297 |
|
| 298 |
$output = '<' . esc_html( $tag ) . ' class="pk-meta-reading-time meta-reading-time">'; |
| 299 |
|
| 300 |
if ( true === $compact ) { |
| 301 |
$output .= '<i class="pk-icon pk-icon-watch"></i>' . intval( $reading_time ) . ' ' . esc_html( 'min', 'powerkit' ); |
| 302 |
} else { |
| 303 |
/* translators: %s number of minutes */ |
| 304 |
$output .= esc_html( sprintf( _n( '%s minute read', '%s minute read', $reading_time, 'powerkit' ), $reading_time ) ); |
| 305 |
} |
| 306 |
|
| 307 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 308 |
|
| 309 |
return $output; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if ( ! function_exists( 'powerkit_get_meta_views' ) ) { |
| 314 |
/** |
| 315 |
* Post Reading Time |
| 316 |
* |
| 317 |
* @param string $tag Element tag, i.e. div or span. |
| 318 |
* @param bool $compact If compact version shall be displayed. |
| 319 |
*/ |
| 320 |
function powerkit_get_meta_views( $tag = 'span', $compact = false ) { |
| 321 |
|
| 322 |
switch ( powerkit_post_views_enabled() ) { |
| 323 |
case 'post_views': |
| 324 |
$views = pvc_get_post_views(); |
| 325 |
break; |
| 326 |
case 'pk_post_views': |
| 327 |
$views = powerkit_get_post_views( null, false ); |
| 328 |
break; |
| 329 |
default: |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
// Don't display if minimum threshold is not met. |
| 334 |
if ( $views < apply_filters( 'powerkit_post_minimum_views', 1 ) ) { |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
$output = '<' . esc_html( $tag ) . ' class="pk-meta-views meta-views">'; |
| 339 |
|
| 340 |
$views_rounded = powerkit_get_round_number( $views ); |
| 341 |
|
| 342 |
if ( true === $compact ) { |
| 343 |
$output .= '<i class="pk-icon pk-icon-eye"></i>' . esc_html( $views_rounded ); |
| 344 |
} else { |
| 345 |
/* translators: %s number of post views */ |
| 346 |
$output .= esc_html( sprintf( _n( '%s view', '%s views', $views, 'powerkit' ), $views_rounded ) ); |
| 347 |
} |
| 348 |
|
| 349 |
$output .= '</' . esc_html( $tag ) . '>'; |
| 350 |
|
| 351 |
return $output; |
| 352 |
} |
| 353 |
} |
| 354 |
|