-
Notifications
You must be signed in to change notification settings - Fork 34
/
single.php
executable file
·158 lines (138 loc) · 5.22 KB
/
single.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
<?php
/**
* The Template for displaying all single posts
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/
use P4\MasterTheme\Context;
use P4\MasterTheme\Post;
use P4\MasterTheme\Settings\CommentsGdpr;
use Timber\Timber;
// Initializing variables.
$context = Timber::get_context();
/**
* P4 Post Object
*
* @var Post $post
*/
$post = Timber::query_post(false, Post::class); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$context['post'] = $post;
// Set Navigation Issues links.
$post->set_issues_links();
// Get the cmb2 custom fields data
// Articles block parameters to populate the articles block
// p4_take_action_page parameter to populate the take action boxout block
// Author override parameter. If this is set then the author profile section will not be displayed.
$page_meta_data = get_post_meta($post->ID);
$page_meta_data = array_map(fn($v) => reset($v), $page_meta_data);
$page_terms_data = get_the_terms($post, 'p4-page-type');
$page_terms_data = is_array($page_terms_data) ? reset($page_terms_data) : null;
$context['background_image'] = $page_meta_data['p4_background_image_override'] ?? '';
$take_action_page = $page_meta_data['p4_take_action_page'] ?? '';
$context['page_type'] = $page_terms_data->name ?? '';
$context['page_term_id'] = $page_terms_data->term_id ?? '';
$context['custom_body_classes'] = 'white-bg';
$context['page_type_slug'] = $page_terms_data->slug ?? '';
$context['social_accounts'] = $post->get_social_accounts($context['footer_social_menu'] ?: []);
$context['page_category'] = 'Post Page';
$context['post_tags'] = implode(', ', $post->tags());
$context['post_categories'] = implode(', ', $post->categories());
// We need the explode because we want to remove "+00:00" at the end of the string.
$context['page_date'] = explode('+', get_the_date('c', $post->ID))[0];
Context::set_og_meta_fields($context, $post);
Context::set_campaign_datalayer($context, $page_meta_data);
Context::set_utm_params($context, $post);
Context::set_reading_time_datalayer($context, $post);
$context['filter_url'] = add_query_arg(
[
's' => ' ',
'orderby' => 'relevant',
'f[ptype][' . $context['page_type'] . ']' => $context['page_term_id'],
],
get_home_url()
);
// Build the shortcode for related-posts block.
if ('yes' === $post->include_articles) {
$tag_id_array = [];
foreach ($post->tags() as $post_tag) {
$tag_id_array[] = $post_tag->id;
}
$category_id_array = [];
foreach ($post->terms('category') as $category) {
$category_id_array[] = $category->id;
}
$block_attributes = [
'query' => [
'perPage' => 3,
'post_type' => 'post',
'taxQuery' => [
'post_tag' => $tag_id_array,
'category' => $category_id_array,
],
'exclude' => [$post->ID],
],
'className' => 'posts-list p4-query-loop is-custom-layout-list',
'hasPassword' => false,
'layout' => [
'type' => 'default',
'columnCount' => 3,
],
'namespace' => 'planet4-blocks/posts-list',
];
$post->articles = '<!-- wp:p4/related-posts {"query_attributes" : ' . wp_json_encode($block_attributes) . '} /-->';
}
if (! empty($take_action_page) && ! has_block('planet4-blocks/take-action-boxout')) {
$post->take_action_page = $take_action_page;
$block_attributes = [
'take_action_page' => $take_action_page,
];
$post->take_action_boxout = '<!-- wp:planet4-blocks/take-action-boxout '
. wp_json_encode($block_attributes, JSON_UNESCAPED_SLASHES)
. ' /-->';
}
// Build an arguments array to customize WordPress comment form.
$comments_args = [
'comment_notes_before' => '',
'comment_notes_after' => '',
'comment_field' => Timber::compile('comment_form/comment_field.twig'),
'submit_button' => Timber::compile(
'comment_form/submit_button.twig',
[
'gdpr_checkbox' => CommentsGdpr::get_option(),
'gdpr_label' => __(
// phpcs:ignore Generic.Files.LineLength.MaxExceeded
'I agree on providing my name, email and content so that my comment can be stored and displayed in the website.',
'planet4-master-theme'
),
]
),
'title_reply' => __('Leave your reply', 'planet4-master-theme'),
];
$context['comments_args'] = $comments_args;
$context['show_comments'] = comments_open($post->ID);
$context['post_comments_count'] = get_comments(
[
'post_id' => $post->ID,
'status' => 'approve',
'type' => 'comment',
'count' => true,
]
);
Context::set_p4_blocks_datalayer($context, $post);
if (post_password_required($post->ID)) {
// Password protected form validation.
$context['is_password_valid'] = $post->is_password_valid();
// Hide the post title from links to the extra feeds.
remove_action('wp_head', 'feed_links_extra', 3);
$context['login_url'] = wp_login_url();
Timber::render('single-password.twig', $context);
} else {
Timber::render(
[ 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ],
$context
);
}