| 1 |
<?php |
| 2 |
namespace Averta\WordPress\Utility; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Str; |
| 5 |
|
| 6 |
class Post |
| 7 |
{ |
| 8 |
|
| 9 |
/** |
| 10 |
* Generates an excerpt and trims it by characters from the content outside of loop |
| 11 |
* |
| 12 |
* @param int $postId ID of the post. |
| 13 |
* @param int $maxCharLength The maximum number of words in a post excerpt. |
| 14 |
* @param array $excludeStripShortcodeTags |
| 15 |
* @param bool $skipMoreTag |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public static function getExcerptTrimmedByChars( $postId = null, $maxCharLength = null, $excludeStripShortcodeTags = null, $skipMoreTag = false ) { |
| 20 |
$post = get_post( $postId ); |
| 21 |
if( ! isset( $post ) ) return ''; |
| 22 |
|
| 23 |
// If post password required and it doesn't match the cookie. |
| 24 |
if ( post_password_required( $post ) ){ |
| 25 |
return get_the_password_form( $post ); |
| 26 |
} |
| 27 |
|
| 28 |
$excerptMore = apply_filters( 'excerpt_more', " ..." ); |
| 29 |
$excerptMore = apply_filters( 'averta/wordpress/excerpt/trim/chars/more', $excerptMore ); |
| 30 |
|
| 31 |
if ( $post->post_excerpt ){ |
| 32 |
$excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt ); |
| 33 |
|
| 34 |
} else { |
| 35 |
$postContent = $post->post_content; |
| 36 |
$content = $postContent; |
| 37 |
|
| 38 |
// check for <!--more--> tag |
| 39 |
if ( ! $skipMoreTag && preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { |
| 40 |
$content = explode( $matches[0], $content, 2 ); |
| 41 |
|
| 42 |
if ( ! empty( $matches[1] ) ){ |
| 43 |
$moreLinkText = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); |
| 44 |
$excerptMore = ! empty( $moreLinkText ) ? $moreLinkText : $excerptMore; |
| 45 |
} |
| 46 |
|
| 47 |
return $content[0] . $excerptMore; |
| 48 |
} |
| 49 |
// If char length is defined use it, otherwise use default char length |
| 50 |
$maxCharLength = empty( $maxCharLength ) ? apply_filters( 'averta/wordpress/excerpt/trim/chars/length', 250 ) : $maxCharLength; |
| 51 |
|
| 52 |
// Clean post content |
| 53 |
$excerpt = strip_tags( Sanitize::stripShortcodes( $content, $excludeStripShortcodeTags ) ); |
| 54 |
} |
| 55 |
|
| 56 |
$excerpt = Str::trimByChars( $excerpt, $maxCharLength, $excerptMore ); |
| 57 |
|
| 58 |
return apply_filters( 'averta/wordpress/excerpt/trim/chars/result', $excerpt, $post, $content, $postContent, $maxCharLength, $excerptMore ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Generates an excerpt and trims it by words from the content outside of loop |
| 64 |
* |
| 65 |
* @param int $postId ID of the post. |
| 66 |
* @param int $excerptLength The maximum number of words in a post excerpt. |
| 67 |
* @param array $excludeStripShortcodeTags |
| 68 |
* @param bool $skipMoreTag |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public static function getExcerptTrimmedByWords( $postId = null, $excerptLength = null, $excludeStripShortcodeTags = null, $skipMoreTag = false ) { |
| 73 |
$post = get_post( $postId ); |
| 74 |
if( ! isset( $post ) ) return ''; |
| 75 |
|
| 76 |
// If post password required and it doesn't match the cookie. |
| 77 |
if ( post_password_required( $post ) ) |
| 78 |
return get_the_password_form( $post ); |
| 79 |
|
| 80 |
if ( $post->post_excerpt ) { |
| 81 |
$result = apply_filters( 'get_the_excerpt', $post->post_excerpt ); |
| 82 |
|
| 83 |
} else { |
| 84 |
$content = $post->post_content; |
| 85 |
$content = apply_filters( 'the_content', $content ); |
| 86 |
|
| 87 |
// If excerpt length is defined use it, otherwise use default excerpt length |
| 88 |
$excerptLength = empty( $excerptLength ) ? apply_filters( 'excerpt_length', 55 ) : $excerptLength; |
| 89 |
$excerptMore = apply_filters( 'excerpt_more', " ..." ); |
| 90 |
|
| 91 |
// check for <!--more--> tag |
| 92 |
if ( ! $skipMoreTag && preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { |
| 93 |
$content = explode( $matches[0], $content, 2 ); |
| 94 |
|
| 95 |
if ( ! empty( $matches[1] ) ){ |
| 96 |
$moreLinkText = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); |
| 97 |
$excerptMore = ! empty( $moreLinkText ) ? $moreLinkText : $excerptMore; |
| 98 |
} |
| 99 |
|
| 100 |
return $content[0] . $excerptMore; |
| 101 |
} |
| 102 |
|
| 103 |
// Clean post content |
| 104 |
$excerpt = strip_tags( Sanitize::stripShortcodes( $content, $excludeStripShortcodeTags ) ); |
| 105 |
$result = wp_trim_words( $excerpt, $excerptLength, $excerptMore ); |
| 106 |
} |
| 107 |
|
| 108 |
return apply_filters( 'averta/wordpress/excerpt/trim/words/result', $result ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Retrieves a post meta field for the given post ID. |
| 113 |
* |
| 114 |
* @param int $postId ID of the object metadata is for. |
| 115 |
* @param string $metaKey Metadata key. If not specified, retrieve all metadata for the specified object. |
| 116 |
* @param string $default Default metadata value for the specified meta key |
| 117 |
* |
| 118 |
* @return mixed|string |
| 119 |
*/ |
| 120 |
public static function getMeta( $postId, $metaKey = '', $default = '' ){ |
| 121 |
$post = get_post( $postId ); |
| 122 |
|
| 123 |
if( empty( $post ) || empty( $post->ID ) ) |
| 124 |
return $default; |
| 125 |
|
| 126 |
$metaValue = get_metadata( 'post', $post->ID, $metaKey, true ); |
| 127 |
return '' === $metaValue ? $default : $metaValue; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|