Add View count on stories #12

Closed
Nabeelshar wants to merge 6 commits from main into main
4 changed files with 31 additions and 1 deletions

View File

@ -167,6 +167,19 @@ function fictioneer_fcn_story_post_type() {
register_post_type( 'fcn_story', $args ); register_post_type( 'fcn_story', $args );
} }
add_action( 'init', 'fictioneer_fcn_story_post_type', 0 ); add_action( 'init', 'fictioneer_fcn_story_post_type', 0 );
function set_post_views($post_id) {
$key = 'post_views';
$count = get_post_meta($post_id, $key, true);
$count = (empty($count)) ? 1 : $count + 1;
update_post_meta($post_id, $key, $count);
}
function track_post_views() {
if (is_single() && get_post_type() == 'fcn_story') {
set_post_views(get_the_ID());
}
}
add_action('wp', 'track_post_views');
// ============================================================================= // =============================================================================
// CUSTOM POST TYPE: FCN_CHAPTER // CUSTOM POST TYPE: FCN_CHAPTER

View File

@ -4051,3 +4051,4 @@ function fictioneer_save_recommendation_metaboxes( $post_id ) {
add_action( 'save_post', 'fictioneer_save_recommendation_metaboxes' ); add_action( 'save_post', 'fictioneer_save_recommendation_metaboxes' );
?> ?>
Tetrakern commented 2023-12-28 08:54:13 +08:00 (Migrated from github.com)
Review

What the... why are my newlines at the end of file not committed? Gotta check this.

What the... why are my newlines at the end of file not committed? Gotta check this.

View File

@ -61,12 +61,21 @@ if ( ! $thumbnail_shown ) {
<?php endif; ?> <?php endif; ?>
<div class="story__identity"> <div class="story__identity">
<h1 class="story__identity-title"><?php echo $story['title']; ?></h1> <h1 class="story__identity-title"><?php echo $story['title'];
?></h1> <?php
$views = get_post_views(get_the_ID());
echo '<i class="fas fa-eye" style="font-size: 14px;"> ' . $views . '</i>';
?>
<div class="story__identity-meta"><?php <div class="story__identity-meta"><?php
Tetrakern commented 2023-12-28 08:51:48 +08:00 (Migrated from github.com)
Review

What happened with the formatting here?

What happened with the formatting here?
printf( printf(
_x( 'by %s', 'Story page: by {Author(s)}', 'fictioneer' ), _x( 'by %s', 'Story page: by {Author(s)}', 'fictioneer' ),
fictioneer_get_story_author_nodes( $story_id ) fictioneer_get_story_author_nodes( $story_id )
); );
?></div> ?></div>
</div> </div>

View File

@ -25,6 +25,13 @@ if ( get_post_meta( get_the_ID(), 'fictioneer_story_hidden', true ) ) {
get_header( null, $header_args ); get_header( null, $header_args );
// Update the Display of View Count
function get_post_views($post_id) {
$key = 'post_views';
$count = get_post_meta($post_id, $key, true);
return (empty($count)) ? 0 : $count;
}
Tetrakern commented 2023-12-28 08:57:06 +08:00 (Migrated from github.com)
Review

This should really not be in the template or partial. Also, defining the key if it is immediately used and only once is a bit of a waste. The key 'post_views' is also not save, it can be manipulated in the editor under certain circumstances. You should prefix internal meta keys with "fictioneer_" or just "_" if you are confident they will not conflict (which I wouldn't be).

This should really not be in the template or partial. Also, defining the key if it is immediately used and only once is a bit of a waste. The key 'post_views' is also not save, it can be manipulated in the editor under certain circumstances. You should prefix internal meta keys with "fictioneer_" or just "_" if you are confident they will not conflict (which I wouldn't be).
?> ?>
<main id="main" class="main story"> <main id="main" class="main story">