| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content helper functions. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
* @since 2.0.0 |
| 7 |
* @copyright (c) 2023 Code Atlantic LLC |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace ContentControl; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get post excerpt or <!--more--> tag content for a post. |
| 14 |
* |
| 15 |
* This differs from get_the_excerpt in that it will return the content |
| 16 |
* before the <!--more--> tag if it exists, but not generate an excerpt |
| 17 |
* from the_contnet. It also doesn't filter the content. |
| 18 |
* |
| 19 |
* @param int|\WP_Post|null $post_id Post ID or object. Defaults to global $post. |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
function get_excerpt_by_id( $post_id = null ) { |
| 23 |
$post = get_post( $post_id ); |
| 24 |
|
| 25 |
$excerpt = ''; |
| 26 |
|
| 27 |
if ( $post ) { |
| 28 |
if ( has_excerpt( $post ) ) { |
| 29 |
// Use the excerpt if it's set. |
| 30 |
$excerpt = $post->post_excerpt; |
| 31 |
} else { |
| 32 |
// Otherwise, use the content before the 'more' tag. |
| 33 |
$content = $post->post_content; |
| 34 |
$more_position = strpos( $content, '<!--more-->' ); |
| 35 |
if ( false !== $more_position ) { |
| 36 |
// If there's a 'more' tag, return everything before it. |
| 37 |
$excerpt = substr( $content, 0, $more_position ); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $excerpt; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Filter feed post content when needed. |
| 47 |
* |
| 48 |
* @param string $content Content to display. |
| 49 |
* @param \ContentControl\Models\Restriction $restriction Restriction object. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
function append_post_excerpts( $content, $restriction ) { |
| 54 |
global $post; |
| 55 |
|
| 56 |
if ( $restriction->show_excerpts() ) { |
| 57 |
|
| 58 |
// Get unfiltered excerpt. |
| 59 |
$excerpt = get_excerpt_by_id( $post ); |
| 60 |
|
| 61 |
if ( ! empty( $excerpt ) ) { |
| 62 |
/** |
| 63 |
* Filter the allowed tags for excerpts. |
| 64 |
* |
| 65 |
* @param string $tags Allowed tags. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
$tags = apply_filters( |
| 70 |
'content_control/excerpt_allowed_tags', |
| 71 |
'<a><em><strong><blockquote><ul><ol><li><p>' |
| 72 |
); |
| 73 |
|
| 74 |
// Strip tags from excerpt. |
| 75 |
$excerpt = wp_kses( $excerpt, $tags ); |
| 76 |
|
| 77 |
// Wrap excerpt in div with class. |
| 78 |
$excerpt = '<div class="cc-content-excerpt">' . $excerpt . '</div>'; |
| 79 |
|
| 80 |
// Prepend excerpt to content. |
| 81 |
$content = $excerpt . $content; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $content; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Apply content filters for the_content without our own again. |
| 90 |
* |
| 91 |
* @param string $content Content to display. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function the_content_filters( $content ) { |
| 96 |
return apply_filters( 'the_content', $content ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Apply get_the_excerpt fitlers without our own again. |
| 101 |
* |
| 102 |
* @param string $excerpt Excerpt to display. |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
function the_excerpt_filters( $excerpt ) { |
| 107 |
global $post; |
| 108 |
return apply_filters( 'get_the_excerpt', $excerpt, $post ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the current page URL. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
function get_current_page_url() { |
| 117 |
global $wp; |
| 118 |
|
| 119 |
$current_page = trailingslashit( home_url( $wp->request ) ); |
| 120 |
|
| 121 |
/* phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ |
| 122 |
return add_query_arg( $_SERVER['QUERY_STRING'], '', $current_page ); |
| 123 |
/* phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ |
| 124 |
} |
| 125 |
|