| 1 |
<?php |
| 2 |
namespace BBlocks\Inc\Posts; |
| 3 |
|
| 4 |
use BBlocks\Inc\Utils as Utils; |
| 5 |
|
| 6 |
class Functions{ |
| 7 |
static function sanitize_array($array){ |
| 8 |
if( !is_array( $array ) ) { |
| 9 |
return false; |
| 10 |
} |
| 11 |
|
| 12 |
foreach( $array as $key => $value ) { |
| 13 |
if( strpos( $key, 'secret_key' ) !== false && strlen( $value ) == 32 ) { |
| 14 |
$value = sanitize_text_field( str_replace( '<', '<', $value ) ); |
| 15 |
$value = sanitize_text_field( $value ); |
| 16 |
$array[$key] = str_replace( ['<', '>', '&'], [ '<', '>', '&'], $value ); |
| 17 |
}else { |
| 18 |
if( is_array( $value ) ) { |
| 19 |
$array[$key] = self::sanitize_array( $value ); |
| 20 |
}else { |
| 21 |
$array[$key] =$value == 'true' ? true : ( $value == 'false' ? false : sanitize_text_field( $value ) ); |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
return $array; |
| 26 |
} |
| 27 |
|
| 28 |
static function filterNaN( $array ) { |
| 29 |
return array_filter( $array, function( $id ) { |
| 30 |
return $id && is_numeric( $id ); |
| 31 |
}); |
| 32 |
} |
| 33 |
|
| 34 |
static function wordCount( $content ) { |
| 35 |
return $content ? count( preg_split( |
| 36 |
'/[\s]+/', |
| 37 |
preg_replace( '/(<([^>]+)>)/i', '', $content ) |
| 38 |
) ) : 0; |
| 39 |
} |
| 40 |
|
| 41 |
static function applyContentFilter( $rawContent ){ |
| 42 |
// remove script and style tag |
| 43 |
// $rawContent = preg_replace( '/<script\b[^>]*>(.*?)<\/script>|<style\b[^>]*>(.*?)<\/style>/is', '', $rawContent ); |
| 44 |
|
| 45 |
$textAllowedHTML = [ 'a' => [ 'href' => [], 'title' => [] ], 'br' => [], 'em' => [], 'strong' => [] ]; |
| 46 |
$innerAllowedHTML = array_merge( [ 'span' => [ $textAllowedHTML ] ], $textAllowedHTML ); |
| 47 |
$allowedHTML = array_merge( [ 'p' => [ $innerAllowedHTML ] ], $innerAllowedHTML ); |
| 48 |
$content = wp_kses( $rawContent, $allowedHTML ); |
| 49 |
$plainText = trim( wp_strip_all_tags( $content ?? '' ) ); |
| 50 |
|
| 51 |
return Utils::isPro() ? apply_filters( 'b_blocks_posts_excerpt_filter', $plainText, $content ) : $plainText; |
| 52 |
} |
| 53 |
|
| 54 |
static function arrangedPosts ( $posts, $postType, $fImgSize = 'full', $metaDateFormat = 'M j, Y', $isExcerptFromContent = false, $excerptLength = 25 ) { |
| 55 |
$arranged = []; |
| 56 |
|
| 57 |
$excerptLength = (int)$excerptLength; |
| 58 |
|
| 59 |
$taxOfPostType = array_diff( get_object_taxonomies( $postType ), array( 'post_format', 'category' ) ); |
| 60 |
|
| 61 |
foreach( $posts as $post ){ |
| 62 |
$id = $post->ID; |
| 63 |
$content = preg_replace( '/(<([^>]+)>)/i', '', $post->post_content ); |
| 64 |
$contentWords = self::wordCount( $content ); |
| 65 |
|
| 66 |
$thumbnail = [ |
| 67 |
'url' => get_the_post_thumbnail_url( $post, $fImgSize ), |
| 68 |
'alt' => get_post_meta( get_post_thumbnail_id( $id ), '_wp_attachment_image_alt', true ) |
| 69 |
]; |
| 70 |
|
| 71 |
$taxonomies = []; |
| 72 |
foreach ( $taxOfPostType as $key => $slug ) { |
| 73 |
$terms = wp_get_post_terms( $id, $slug ); |
| 74 |
|
| 75 |
$links = ''; |
| 76 |
foreach( $terms as $index => $t ){ |
| 77 |
$link = get_term_link( $t->slug, $slug ); |
| 78 |
$terms[$index]->link = $link; |
| 79 |
|
| 80 |
$links .= "<a href='$link' rel='$slug'>$t->name</a>"; |
| 81 |
}; |
| 82 |
$taxonomies[$slug] = $links; |
| 83 |
} |
| 84 |
|
| 85 |
$contentOrExcerptArr = $isExcerptFromContent ? [ 'content' => $excerptLength > -1 ? wp_trim_words( self::applyContentFilter( $post->post_content ), $excerptLength, '' ) : self::applyContentFilter( $post->post_content ) ] : [ 'excerpt' => self::applyContentFilter( $post->post_excerpt ) ]; |
| 86 |
|
| 87 |
$arranged[] = array_merge( [ |
| 88 |
'id' => $id, |
| 89 |
'link' => get_permalink( $post ), |
| 90 |
'name' => $post->post_name, |
| 91 |
'thumbnail' => $thumbnail, |
| 92 |
'title' => $post->post_title, |
| 93 |
'author' => [ |
| 94 |
'name' => get_the_author_meta( 'display_name', $post->post_author ), |
| 95 |
'link' => get_author_posts_url( $post->post_author ) |
| 96 |
], |
| 97 |
'date' => $post->post_date, |
| 98 |
'date' => get_the_date( $metaDateFormat, $id ), |
| 99 |
'dateGMT' => $post->post_date_gmt, |
| 100 |
'modifiedDate' => $post->post_modified, |
| 101 |
'modifiedDateGMT' => $post->post_modified_gmt, |
| 102 |
'commentCount' => $post->comment_count, |
| 103 |
'commentStatus' => $post->comment_status, |
| 104 |
'categories' => [ |
| 105 |
'coma' => get_the_category_list( ', ', '', $id ), |
| 106 |
'space' => get_the_category_list( ' ', '', $id ) |
| 107 |
], |
| 108 |
'taxonomies' => $taxonomies, |
| 109 |
'readTime' => [ |
| 110 |
'min' => floor( $contentWords / 200 ), |
| 111 |
'sec' => floor( $contentWords % 200 / ( 200 / 60 ) ) |
| 112 |
], |
| 113 |
'status' => $post->post_status |
| 114 |
], $contentOrExcerptArr ); |
| 115 |
} |
| 116 |
|
| 117 |
return $arranged; |
| 118 |
} |
| 119 |
} |