-
Notifications
You must be signed in to change notification settings - Fork 956
/
template-tags.php
355 lines (323 loc) · 10.8 KB
/
template-tags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Custom template tags for this theme
*
* Eventually, some of the functionality here could be replaced by core features.
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'understrap_posted_on' ) ) {
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function understrap_posted_on() {
$post = get_post();
if ( ! $post ) {
return;
}
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s"> (%4$s) </time>';
}
$time_string = sprintf(
$time_string,
esc_attr( get_the_date( 'c' ) ), // @phpstan-ignore-line -- post exists
esc_html( get_the_date() ), // @phpstan-ignore-line -- post exists
esc_attr( get_the_modified_date( 'c' ) ), // @phpstan-ignore-line -- post exists
esc_html( get_the_modified_date() ) // @phpstan-ignore-line -- post exists
);
$posted_on = apply_filters(
'understrap_posted_on',
sprintf(
'<span class="posted-on">%1$s <a href="%2$s" rel="bookmark">%3$s</a></span>',
esc_html_x( 'Posted on', 'post date', 'understrap' ),
esc_url( get_permalink() ), // @phpstan-ignore-line -- post exists
apply_filters( 'understrap_posted_on_time', $time_string )
)
);
$author_id = (int) get_the_author_meta( 'ID' );
if ( 0 === $author_id ) {
$byline = '';
} else {
$byline = apply_filters(
'understrap_posted_by',
sprintf(
'<span class="byline"> %1$s<span class="author vcard"> <a class="url fn n" href="%2$s">%3$s</a></span></span>',
$posted_on ? esc_html_x( 'by', 'post author', 'understrap' ) : esc_html_x( 'Posted by', 'post author', 'understrap' ),
esc_url( get_author_posts_url( $author_id ) ),
esc_html( get_the_author_meta( 'display_name', $author_id ) )
)
);
}
echo $posted_on . $byline; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! function_exists( 'understrap_entry_footer' ) ) {
/**
* Prints HTML with meta information for the categories, tags and comments.
*/
function understrap_entry_footer() {
// Hide category and tag text for pages.
if ( 'post' === get_post_type() ) {
understrap_categories_tags_list();
}
understrap_comments_popup_link();
understrap_edit_post_link();
}
}
if ( ! function_exists( 'understrap_categories_tags_list' ) ) {
/**
* Displays a list of categories and a list of tags.
*/
function understrap_categories_tags_list() {
understrap_categories_list();
understrap_tags_list();
}
}
if ( ! function_exists( 'understrap_categories_list' ) ) {
/**
* Displays a list of categories.
*/
function understrap_categories_list() {
$categories_list = get_the_category_list( understrap_get_list_item_separator() );
if ( $categories_list && understrap_categorized_blog() ) {
/* translators: %s: Categories of current post */
printf( '<span class="cat-links">' . esc_html__( 'Posted in %s', 'understrap' ) . '</span>', $categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
if ( ! function_exists( 'understrap_tags_list' ) ) {
/**
* Displays a list of tags.
*/
function understrap_tags_list() {
$tags_list = get_the_tag_list( '', understrap_get_list_item_separator() );
if ( $tags_list && ! is_wp_error( $tags_list ) ) {
/* translators: %s: Tags of current post */
printf( '<span class="tags-links">' . esc_html__( 'Tagged %s', 'understrap' ) . '</span>', $tags_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
if ( ! function_exists( 'understrap_comments_popup_link' ) ) {
/**
* Displays the link to the comments for the current post.
*/
function understrap_comments_popup_link() {
if ( is_single() || post_password_required() || ( ! comments_open() && 0 === absint( get_comments_number() ) ) ) {
return;
}
$post_title = get_the_title();
$leave_comment = sprintf(
/* translators: %s post title */
__( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'understrap' ),
$post_title
);
$leave_comment = wp_kses( $leave_comment, array( 'span' => array( 'class' => true ) ) );
echo '<span class="comments-link">';
comments_popup_link( $leave_comment );
echo '</span>';
}
}
if ( ! function_exists( 'understrap_categorized_blog' ) ) {
/**
* Returns true if a blog has more than 1 category.
*
* @return bool
*/
function understrap_categorized_blog() {
$all_the_cool_cats = get_transient( 'understrap_categories' );
if ( false === $all_the_cool_cats ) {
// Create an array of all the categories that are attached to posts.
$all_the_cool_cats = get_categories(
array(
'fields' => 'ids',
'hide_empty' => 1,
// We only need to know if there is more than one category.
'number' => 2,
)
);
// Count the number of categories that are attached to the posts.
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'understrap_categories', $all_the_cool_cats );
}
if ( $all_the_cool_cats > 1 ) {
// This blog has more than 1 category so understrap_categorized_blog should return true.
return true;
}
// This blog has only 1 category so understrap_categorized_blog should return false.
return false;
}
}
add_action( 'delete_category', 'understrap_category_transient_flusher' );
add_action( 'save_post', 'understrap_category_transient_flusher' );
add_action( 'trashed_post', 'understrap_category_transient_flusher' );
add_action( 'deleted_post', 'understrap_category_transient_flusher' );
if ( ! function_exists( 'understrap_category_transient_flusher' ) ) {
/**
* Flush out the transients used in understrap_categorized_blog.
*/
function understrap_category_transient_flusher() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Like, beat it. Dig?
delete_transient( 'understrap_categories' );
}
}
if ( ! function_exists( 'understrap_body_attributes' ) ) {
/**
* Displays the attributes for the body element.
*/
function understrap_body_attributes() {
/**
* Filters the body attributes.
*
* @param array $atts An associative array of attributes.
*/
$atts = array_unique( apply_filters( 'understrap_body_attributes', $atts = array() ) );
if ( ! is_array( $atts ) || empty( $atts ) ) {
return;
}
$attributes = '';
foreach ( $atts as $name => $value ) {
if ( $value ) {
if ( ! is_string( $value ) ) {
continue;
}
$attributes .= sanitize_key( $name ) . '="' . esc_attr( $value ) . '" ';
} else {
$attributes .= sanitize_key( $name ) . ' ';
}
}
echo trim( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput
}
}
if ( ! function_exists( 'understrap_comment_navigation' ) ) {
/**
* Displays the comment navigation.
*
* @param string $nav_id The ID of the comment navigation.
*/
function understrap_comment_navigation( $nav_id ) {
if ( get_comment_pages_count() <= 1 ) {
// Return early if there are no comments to navigate through.
return;
}
?>
<nav class="comment-navigation" id="<?php echo esc_attr( $nav_id ); ?>">
<h1 class="screen-reader-text"><?php esc_html_e( 'Comments navigation', 'understrap' ); ?></h1>
<?php if ( get_previous_comments_link() ) { ?>
<div class="nav-previous">
<?php previous_comments_link( __( '← Older Comments', 'understrap' ) ); ?>
</div>
<?php } ?>
<?php if ( get_next_comments_link() ) { ?>
<div class="nav-next">
<?php next_comments_link( __( 'Newer Comments →', 'understrap' ) ); ?>
</div>
<?php } ?>
</nav><!-- #<?php echo esc_attr( $nav_id ); ?> -->
<?php
}
}
if ( ! function_exists( 'understrap_edit_post_link' ) ) {
/**
* Displays the edit post link for post.
*/
function understrap_edit_post_link() {
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
esc_html__( 'Edit %s', 'understrap' ),
the_title( '<span class="screen-reader-text">"', '"</span>', false )
),
'<span class="edit-link">',
'</span>'
);
}
}
if ( ! function_exists( 'understrap_post_nav' ) ) {
/**
* Display navigation to next/previous post when applicable.
*
* @global WP_Post|null $post The current post.
*/
function understrap_post_nav() {
global $post;
if ( ! $post ) {
return;
}
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
}
?>
<nav class="container navigation post-navigation">
<h2 class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'understrap' ); ?></h2>
<div class="d-flex nav-links justify-content-between">
<?php
if ( get_previous_post_link() ) {
previous_post_link( '<span class="nav-previous">%link</span>', _x( '<i class="fa fa-angle-left"></i> %title', 'Previous post link', 'understrap' ) );
}
if ( get_next_post_link() ) {
next_post_link( '<span class="nav-next">%link</span>', _x( '%title <i class="fa fa-angle-right"></i>', 'Next post link', 'understrap' ) );
}
?>
</div><!-- .nav-links -->
</nav><!-- .post-navigation -->
<?php
}
}
if ( ! function_exists( 'understrap_link_pages' ) ) {
/**
* Displays/retrieves page links for paginated posts (i.e. including the
* `<!--nextpage-->` Quicktag one or more times). This tag must be
* within The Loop. Default: echo.
*
* @return void|string Formatted output in HTML.
*/
function understrap_link_pages() {
$args = apply_filters(
'understrap_link_pages_args',
array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'understrap' ),
'after' => '</div>',
)
);
wp_link_pages( $args );
}
}
if ( ! function_exists( 'understrap_get_select_control_class' ) ) {
/**
* Retrieves the Bootstrap CSS class for the select tag.
*
* @return string Bootstrap CSS class for the select tag.
*/
function understrap_get_select_control_class() {
if ( 'bootstrap4' === get_theme_mod( 'understrap_bootstrap_version', 'bootstrap4' ) ) {
return 'form-control';
}
return 'form-select';
}
}
if ( ! function_exists( 'understrap_get_list_item_separator' ) ) {
/**
* Retrieves the localized list item separator.
*
* `wp_get_list_item_separator()` has been introduced in WP 6.0.0. For WP
* versions lower than 6.0.0 we have to use a custom translation.
*
* @return string Localized list item separator.
*/
function understrap_get_list_item_separator() {
if ( function_exists( 'wp_get_list_item_separator' ) ) {
return esc_html( wp_get_list_item_separator() );
}
/* translators: used between list items, there is a space after the comma */
return esc_html__( ', ', 'understrap' );
}
}