| 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 |
do_action( 'flex_posts_meta_start' ); |
| 44 |
|
| 45 |
if ( ! empty( $instance['show_author'] ) || ! empty( $instance['show_avatar'] ) ) { |
| 46 |
flex_posts_author_meta( $instance ); |
| 47 |
} |
| 48 |
if ( ! empty( $instance['show_date'] ) ) { |
| 49 |
flex_posts_date_meta(); |
| 50 |
} |
| 51 |
if ( ! empty( $instance['show_comments'] ) ) { |
| 52 |
flex_posts_comments_meta(); |
| 53 |
} |
| 54 |
|
| 55 |
do_action( 'flex_posts_meta_end' ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! function_exists( 'flex_posts_author_meta' ) ) { |
| 60 |
/** |
| 61 |
* Display author meta. |
| 62 |
* |
| 63 |
* @param array $instance Widget settings. |
| 64 |
*/ |
| 65 |
function flex_posts_author_meta( $instance = array() ) { |
| 66 |
$author_id = get_the_author_meta( 'ID' ); |
| 67 |
$author_url = get_author_posts_url( $author_id ); |
| 68 |
$image_size = apply_filters( 'flex_posts_author_image_size', 24 ); |
| 69 |
?> |
| 70 |
<span class="fp-author"> |
| 71 |
<span class="author vcard"> |
| 72 |
<?php if ( ! empty( $instance['show_avatar'] ) ) : ?> |
| 73 |
<a class="author-image" href="<?php echo esc_url( $author_url ); ?>"> |
| 74 |
<?php echo get_avatar( $author_id, $image_size ); ?> |
| 75 |
</a> |
| 76 |
<?php endif; ?> |
| 77 |
<?php if ( ! empty( $instance['show_author'] ) ) : ?> |
| 78 |
<a class="url fn n" href="<?php echo esc_url( $author_url ); ?>"> |
| 79 |
<span><?php the_author(); ?></span> |
| 80 |
</a> |
| 81 |
<?php endif; ?> |
| 82 |
</span> |
| 83 |
</span> |
| 84 |
<?php |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! function_exists( 'flex_posts_date_meta' ) ) { |
| 89 |
/** |
| 90 |
* Display date meta. |
| 91 |
*/ |
| 92 |
function flex_posts_date_meta() { |
| 93 |
?> |
| 94 |
<span class="fp-date"> |
| 95 |
<a href="<?php the_permalink(); ?>" rel="bookmark"> |
| 96 |
<time class="entry-date published" datetime="<?php the_date( 'c' ); ?>"> |
| 97 |
<?php echo esc_html( get_the_date() ); ?> |
| 98 |
</time> |
| 99 |
</a> |
| 100 |
</span> |
| 101 |
<?php |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! function_exists( 'flex_posts_comments_meta' ) ) { |
| 106 |
/** |
| 107 |
* Display comments meta. |
| 108 |
*/ |
| 109 |
function flex_posts_comments_meta() { |
| 110 |
?> |
| 111 |
<span class="fp-comments"> |
| 112 |
<?php comments_popup_link(); ?> |
| 113 |
</span> |
| 114 |
<?php |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! function_exists( 'flex_posts_categories_meta' ) ) { |
| 119 |
/** |
| 120 |
* Display categories meta. |
| 121 |
*/ |
| 122 |
function flex_posts_categories_meta() { |
| 123 |
?> |
| 124 |
<span class="fp-categories"> |
| 125 |
<?php the_category( ', ' ); ?> |
| 126 |
</span> |
| 127 |
<?php |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! function_exists( 'flex_posts_thumbnail' ) ) { |
| 132 |
/** |
| 133 |
* Display post thumbnail. |
| 134 |
* |
| 135 |
* @param string $size Image size. |
| 136 |
* @param array $instance Widget settings. |
| 137 |
* @param int $current_post Post index number. |
| 138 |
*/ |
| 139 |
function flex_posts_thumbnail( $size, $instance = array(), $current_post = 0 ) { |
| 140 |
if ( isset( $instance['show_image'] ) ) { |
| 141 |
if ( 'none' === $instance['show_image'] ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
if ( 'first' === $instance['show_image'] && $current_post > 0 ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
} |
| 148 |
$default_image = apply_filters( 'flex_posts_default_image', FLEX_POSTS_URL . 'public/images/default.png' ); |
| 149 |
?> |
| 150 |
<div class="fp-media"> |
| 151 |
<a class="fp-thumbnail" href="<?php the_permalink(); ?>"> |
| 152 |
<?php if ( has_post_thumbnail() ) : ?> |
| 153 |
<?php the_post_thumbnail( $size ); ?> |
| 154 |
<?php else : ?> |
| 155 |
<img src="<?php echo esc_url( $default_image ); ?>" class="size-<?php echo esc_attr( $size ); ?>" alt=""> |
| 156 |
<?php endif; ?> |
| 157 |
</a> |
| 158 |
<?php do_action( 'flex_posts_media', $instance ); ?> |
| 159 |
</div> |
| 160 |
<?php |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$flex_posts_excerpt_length = 15; |
| 165 |
|
| 166 |
/** |
| 167 |
* Callback for the excerpt_length filter |
| 168 |
* |
| 169 |
* @return int |
| 170 |
*/ |
| 171 |
function flex_posts_get_excerpt_length() { |
| 172 |
global $flex_posts_excerpt_length; |
| 173 |
return $flex_posts_excerpt_length; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! function_exists( 'flex_posts_excerpt' ) ) { |
| 177 |
/** |
| 178 |
* Display excerpt. |
| 179 |
* |
| 180 |
* @param int $length Number of words. |
| 181 |
*/ |
| 182 |
function flex_posts_excerpt( $length = 15 ) { |
| 183 |
global $flex_posts_excerpt_length; |
| 184 |
$flex_posts_excerpt_length = $length; |
| 185 |
add_filter( 'excerpt_length', 'flex_posts_get_excerpt_length' ); |
| 186 |
echo get_the_excerpt(); // phpcs:ignore WordPress.Security.EscapeOutput |
| 187 |
remove_filter( 'excerpt_length', 'flex_posts_get_excerpt_length' ); |
| 188 |
} |
| 189 |
} |
| 190 |
|