-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplainpost-features.php
More file actions
71 lines (60 loc) · 1.73 KB
/
plainpost-features.php
File metadata and controls
71 lines (60 loc) · 1.73 KB
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
<?php
/**
* Plugin Name: Plain Post Features
* Description: Additional blocks for the Plain Post Theme.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 1.0
* Author: Mashiur Rahman
* Author URI: https://mashiurz.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: plainpost-features
*
* @package plainpost-features
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function plainPostBlockInit() {
$blocks = array(
'categories-list',
'trending-posts',
);
foreach ($blocks as $block) {
register_block_type(__DIR__ . "/build/{$block}");
}
}
add_action( 'init', 'plainPostBlockInit' );
/**
* Post View count
*/
function plainpostSetPostViews($postID = null)
{
if (!is_single() || 'post' !== get_post_type() || current_user_can('administrator'))
return;
if (empty($postID)) {
global $post;
$postID = $post->ID;
}
$postID = !empty($postID) ? $postID : get_the_ID();
$countKey = 'plainpost_post_views_count';
$count = get_post_meta($postID, $countKey, true);
//update_post_meta($postID, $countKey, ((int) $count) + 1);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $countKey);
add_post_meta($postID, $countKey, '0');
} else {
$count++;
update_post_meta($postID, $countKey, $count);
}
}
add_action('template_redirect', 'plainpostSetPostViews');