| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions used in template files |
| 4 |
* |
| 5 |
* @package Flex Posts |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! function_exists( 'flex_posts_title' ) ) { |
| 13 |
/** |
| 14 |
* Display post title. |
| 15 |
* |
| 16 |
* @param array $instance Widget settings. |
| 17 |
*/ |
| 18 |
function flex_posts_title( $instance ) { |
| 19 |
if ( empty( $instance['show_title'] ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
$el = 'h4'; |
| 23 |
if ( ! empty( $instance['post_title_el'] ) ) { |
| 24 |
if ( in_array( $instance['post_title_el'], flex_posts_get_title_elements(), true ) ) { |
| 25 |
$el = $instance['post_title_el']; |
| 26 |
} |
| 27 |
} |
| 28 |
?> |
| 29 |
<<?php echo sanitize_key( $el ); ?> class="fp-title"> |
| 30 |
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> |
| 31 |
</<?php echo sanitize_key( $el ); ?>> |
| 32 |
<?php |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! function_exists( 'flex_posts_meta' ) ) { |
| 37 |
/** |
| 38 |
* Display meta information. |
| 39 |
* |
| 40 |
* @param array $instance Widget settings. |
| 41 |
*/ |
| 42 |
function flex_posts_meta( $instance ) { |
| 43 |
/** |
| 44 |
* Action: flex_posts_meta_start |
| 45 |
* |
| 46 |
* Fired at the start of meta output for a post inside the widget/block. |
| 47 |
* Example: |
| 48 |
* add_action( 'flex_posts_meta_start', function() { echo '<span>Start</span>'; } ); |
| 49 |
* |
| 50 |
* @param none |
| 51 |
*/ |
| 52 |
do_action( 'flex_posts_meta_start' ); |
| 53 |
|
| 54 |
if ( ! empty( $instance['show_author'] ) || ! empty( $instance['show_avatar'] ) ) { |
| 55 |
flex_posts_author_meta( $instance ); |
| 56 |
} |
| 57 |
if ( ! empty( $instance['show_date'] ) ) { |
| 58 |
flex_posts_date_meta(); |
| 59 |
} |
| 60 |
if ( ! empty( $instance['show_comments'] ) ) { |
| 61 |
flex_posts_comments_meta(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Action: flex_posts_meta_end |
| 66 |
* |
| 67 |
* Fired at the end of meta output for a post inside the widget/block. |
| 68 |
* Useful for adding extra meta content. |
| 69 |
* |
| 70 |
* @param none |
| 71 |
*/ |
| 72 |
do_action( 'flex_posts_meta_end' ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! function_exists( 'flex_posts_author_meta' ) ) { |
| 77 |
/** |
| 78 |
* Display author meta. |
| 79 |
* |
| 80 |
* @param array $instance Widget settings. |
| 81 |
*/ |
| 82 |
function flex_posts_author_meta( $instance = array() ) { |
| 83 |
$author_id = get_the_author_meta( 'ID' ); |
| 84 |
$author_url = get_author_posts_url( $author_id ); |
| 85 |
$author_title = sprintf( |
| 86 |
/* translators: %s: Author's display name. */ |
| 87 |
__( 'Posts by %s', 'flex-posts' ), |
| 88 |
get_the_author() |
| 89 |
); |
| 90 |
|
| 91 |
/** |
| 92 |
* Filter: flex_posts_author_image_size |
| 93 |
* |
| 94 |
* Filter the size (in pixels) used for author avatars in meta output. |
| 95 |
* |
| 96 |
* @param int $size Default avatar size in pixels. |
| 97 |
* @return int Modified avatar size. |
| 98 |
* |
| 99 |
* Example: |
| 100 |
* add_filter( 'flex_posts_author_image_size', function( $size ) { return 32; } ); |
| 101 |
*/ |
| 102 |
$image_size = apply_filters( 'flex_posts_author_image_size', 24 ); |
| 103 |
?> |
| 104 |
<span class="fp-author"> |
| 105 |
<span class="author vcard"> |
| 106 |
<?php if ( ! empty( $instance['show_avatar'] ) ) : ?> |
| 107 |
<a class="author-image" href="<?php echo esc_url( $author_url ); ?>" rel="author" title="<?php echo esc_attr( $author_title ); ?>"> |
| 108 |
<?php echo get_avatar( $author_id, $image_size ); ?> |
| 109 |
</a> |
| 110 |
<?php endif; ?> |
| 111 |
<?php if ( ! empty( $instance['show_author'] ) ) : ?> |
| 112 |
<a class="url fn n" href="<?php echo esc_url( $author_url ); ?>" rel="author"> |
| 113 |
<span><?php the_author(); ?></span> |
| 114 |
</a> |
| 115 |
<?php endif; ?> |
| 116 |
</span> |
| 117 |
</span> |
| 118 |
<?php |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! function_exists( 'flex_posts_date_meta' ) ) { |
| 123 |
/** |
| 124 |
* Display date meta. |
| 125 |
*/ |
| 126 |
function flex_posts_date_meta() { |
| 127 |
?> |
| 128 |
<span class="fp-date"> |
| 129 |
<a href="<?php the_permalink(); ?>" rel="bookmark"> |
| 130 |
<time class="entry-date published" datetime="<?php the_date( 'c' ); ?>"> |
| 131 |
<?php echo esc_html( get_the_date() ); ?> |
| 132 |
</time> |
| 133 |
</a> |
| 134 |
</span> |
| 135 |
<?php |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! function_exists( 'flex_posts_comments_meta' ) ) { |
| 140 |
/** |
| 141 |
* Display comments meta. |
| 142 |
*/ |
| 143 |
function flex_posts_comments_meta() { |
| 144 |
?> |
| 145 |
<span class="fp-comments"> |
| 146 |
<?php comments_popup_link(); ?> |
| 147 |
</span> |
| 148 |
<?php |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! function_exists( 'flex_posts_categories_meta' ) ) { |
| 153 |
/** |
| 154 |
* Display categories meta. |
| 155 |
*/ |
| 156 |
function flex_posts_categories_meta() { |
| 157 |
$categories = get_the_category(); |
| 158 |
if ( ! empty( $categories ) ) { |
| 159 |
$i = 0; |
| 160 |
echo '<span class="fp-categories">'; |
| 161 |
foreach ( $categories as $category ) { |
| 162 |
if ( 0 < $i ) { |
| 163 |
echo ', '; |
| 164 |
} |
| 165 |
$color = get_term_meta( $category->term_id, 'fp_color', true ); |
| 166 |
$style = $color ? "--fp-color: $color" : ''; |
| 167 |
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" rel="category tag" style="' . esc_attr( $style ) . '">' . esc_html( $category->name ) . '</a>'; |
| 168 |
++$i; |
| 169 |
} |
| 170 |
echo '</span>'; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if ( ! function_exists( 'flex_posts_thumbnail' ) ) { |
| 176 |
/** |
| 177 |
* Display post thumbnail. |
| 178 |
* |
| 179 |
* @param string $size Image size. |
| 180 |
* @param array $instance Widget settings. |
| 181 |
* @param int $current_post Post index number. |
| 182 |
*/ |
| 183 |
function flex_posts_thumbnail( $size, $instance = array(), $current_post = 0 ) { |
| 184 |
if ( isset( $instance['show_image'] ) ) { |
| 185 |
if ( 'none' === $instance['show_image'] ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
if ( 'first' === $instance['show_image'] && $current_post > 0 ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Filter: flex_posts_default_image |
| 195 |
* |
| 196 |
* Filter the default image URL used when a post has no featured image. |
| 197 |
* |
| 198 |
* @param string $url Default image URL. |
| 199 |
* @return string Modified image URL. |
| 200 |
* |
| 201 |
* Example: |
| 202 |
* add_filter( 'flex_posts_default_image', function( $url ) { return get_stylesheet_directory_uri() . '/img/default.png'; } ); |
| 203 |
*/ |
| 204 |
$default_image = apply_filters( 'flex_posts_default_image', FLEX_POSTS_URL . 'public/images/default.png' ); |
| 205 |
?> |
| 206 |
<div class="fp-media"> |
| 207 |
<a class="fp-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1"> |
| 208 |
<?php if ( has_post_thumbnail() ) : ?> |
| 209 |
<?php the_post_thumbnail( $size ); ?> |
| 210 |
<?php else : ?> |
| 211 |
<img src="<?php echo esc_url( $default_image ); ?>" class="size-<?php echo esc_attr( $size ); ?>" alt=""> |
| 212 |
<?php endif; ?> |
| 213 |
</a> |
| 214 |
<?php |
| 215 |
/** |
| 216 |
* Action: flex_posts_media |
| 217 |
* |
| 218 |
* Action hook inside the media block (thumbnail/link) where additional |
| 219 |
* markup can be injected. Signature: function( $instance ). |
| 220 |
* |
| 221 |
* @param array $instance Widget settings. |
| 222 |
* |
| 223 |
* Example: |
| 224 |
* add_action( 'flex_posts_media', function( $instance ) { echo '<span class="badge">New</span>'; } ); |
| 225 |
*/ |
| 226 |
do_action( 'flex_posts_media', $instance ); |
| 227 |
?> |
| 228 |
</div> |
| 229 |
<?php |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
$flex_posts_excerpt_length = 15; |
| 234 |
|
| 235 |
/** |
| 236 |
* Callback for the excerpt_length filter |
| 237 |
* |
| 238 |
* @return int |
| 239 |
*/ |
| 240 |
function flex_posts_get_excerpt_length() { |
| 241 |
global $flex_posts_excerpt_length; |
| 242 |
return $flex_posts_excerpt_length; |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! function_exists( 'flex_posts_excerpt' ) ) { |
| 246 |
/** |
| 247 |
* Display excerpt. |
| 248 |
* |
| 249 |
* @param int $length Number of words. |
| 250 |
*/ |
| 251 |
function flex_posts_excerpt( $length = 15 ) { |
| 252 |
global $flex_posts_excerpt_length; |
| 253 |
$flex_posts_excerpt_length = $length; |
| 254 |
add_filter( 'excerpt_length', 'flex_posts_get_excerpt_length' ); |
| 255 |
echo get_the_excerpt(); // phpcs:ignore WordPress.Security.EscapeOutput |
| 256 |
remove_filter( 'excerpt_length', 'flex_posts_get_excerpt_length' ); |
| 257 |
} |
| 258 |
} |
| 259 |
|