/
class-advanced-variables.php
285 lines (245 loc) · 6.8 KB
/
class-advanced-variables.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
<?php
/**
* Advanced variable replacer.
*
* @since 1.0.33
* @package RankMath
* @subpackage RankMath\Replace_Variables
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Replace_Variables;
use RankMath\Paper\Paper;
use RankMath\Helpers\Str;
defined( 'ABSPATH' ) || exit;
/**
* Advanced_Variables class.
*/
class Advanced_Variables extends Author_Variables {
/**
* Setup advanced variables.
*/
public function setup_advanced_variables() {
$post = $this->get_post();
$this->register_replacement(
'id',
[
'name' => esc_html__( 'Post ID', 'rank-math' ),
'description' => esc_html__( 'ID of the current post/page', 'rank-math' ),
'variable' => 'id',
'example' => ! empty( $post ) ? $post->ID : __( 'Post ID', 'rank-math' ),
],
[ $this, 'get_id' ]
);
$keyword = $this->get_focus_keyword();
$this->register_replacement(
'focuskw',
[
'name' => esc_html__( 'Focus Keyword', 'rank-math' ),
'description' => esc_html__( 'Focus Keyword of the current post', 'rank-math' ),
'variable' => 'focuskw',
'example' => \is_null( $keyword ) ? '' : $keyword,
],
[ $this, 'get_focus_keyword' ]
);
$this->register_replacement(
'keywords',
[
'name' => esc_html__( 'Focus Keywords', 'rank-math' ),
'description' => esc_html__( 'Focus Keywords of the current post', 'rank-math' ),
'variable' => 'keywords',
'example' => $this->get_focus_keywords(),
],
[ $this, 'get_focus_keywords' ]
);
$this->register_replacement(
'customfield',
[
'name' => esc_html__( 'Custom Field (advanced)', 'rank-math' ),
'description' => esc_html__( 'Custom field value.', 'rank-math' ),
'variable' => 'customfield(field-name)',
'example' => esc_html__( 'Custom field value', 'rank-math' ),
'nocache' => true,
],
[ $this, 'get_customfield' ]
);
$this->setup_paging_variables();
$this->setup_post_types_variables();
}
/**
* Setup paging variables.
*/
private function setup_paging_variables() {
$this->register_replacement(
'page',
[
'name' => esc_html__( 'Page', 'rank-math' ),
'description' => esc_html__( 'Page number with context (i.e. page 2 of 4). Only displayed on page 2 and above.', 'rank-math' ),
'variable' => 'page',
'example' => ' page 2 of 4',
],
[ $this, 'get_page' ]
);
$this->register_replacement(
'pagenumber',
[
'name' => esc_html__( 'Page Number', 'rank-math' ),
'description' => esc_html__( 'Current page number', 'rank-math' ),
'variable' => 'pagenumber',
'example' => '4',
],
[ $this, 'get_pagenumber' ]
);
$this->register_replacement(
'pagetotal',
[
'name' => esc_html__( 'Max Pages', 'rank-math' ),
'description' => esc_html__( 'Max pages number', 'rank-math' ),
'variable' => 'pagetotal',
'example' => '4',
],
[ $this, 'get_pagetotal' ]
);
}
/**
* Setup post types variables.
*/
private function setup_post_types_variables() {
$this->register_replacement(
'pt_single',
[
'name' => esc_html__( 'Post Type Name Singular', 'rank-math' ),
'description' => esc_html__( 'Name of current post type (singular)', 'rank-math' ),
'variable' => 'pt_single',
'example' => esc_html__( 'Product', 'rank-math' ),
],
[ $this, 'get_post_type_single' ]
);
$this->register_replacement(
'pt_plural',
[
'name' => esc_html__( 'Post Type Name Plural', 'rank-math' ),
'description' => esc_html__( 'Name of current post type (plural)', 'rank-math' ),
'variable' => 'pt_plural',
'example' => esc_html__( 'Products', 'rank-math' ),
],
[ $this, 'get_post_type_plural' ]
);
}
/**
* Get the numeric post ID.
*
* @return string|null
*/
public function get_id() {
return ! empty( $this->args->ID ) ? $this->args->ID : null;
}
/**
* Get the focus keyword.
*
* @return string|null
*/
public function get_focus_keyword() {
$keywords = '';
if ( ! empty( $this->args->ID ) ) {
$keywords = get_post_meta( $this->args->ID, 'rank_math_focus_keyword', true );
}
if ( ! empty( $this->args->term_id ) ) {
$keywords = get_term_meta( $this->args->term_id, 'rank_math_focus_keyword', true );
}
$keywords = explode( ',', $keywords );
if ( '' !== $keywords[0] ) {
return $keywords[0];
}
return null;
}
/**
* Get Focus keywords.
*
* @return string
*/
public function get_focus_keywords() {
if ( is_singular() || is_category() || is_tag() || is_tax() ) {
return Paper::get()->get_keywords();
}
$keywords = '';
if ( ! empty( $this->args->ID ) ) {
$keywords = get_post_meta( $this->args->ID, 'rank_math_focus_keyword', true );
}
if ( ! empty( $this->args->term_id ) ) {
$keywords = get_term_meta( $this->args->term_id, 'rank_math_focus_keyword', true );
}
return $keywords;
}
/**
* Get the current page number as a string (i.e. "page 1 of 5").
*
* @return string
*/
public function get_page() {
$sep = $this->get_sep();
$max = $this->determine_max_pages();
$page = $this->determine_page_number();
if ( $max > 1 && $page > 1 ) {
/* translators: %1$d: current page number, %2$d: max pages. */
return sprintf( $sep . ' ' . __( 'Page %1$d of %2$d', 'rank-math' ), $page, $max );
}
return null;
}
/**
* Get only the page number (without context).
*
* @return string|null
*/
public function get_pagenumber() {
$page = $this->determine_page_number();
return $page > 0 ? (string) $page : null;
}
/**
* Get the max page number.
*
* @return string|null
*/
public function get_pagetotal() {
$max = $this->determine_max_pages();
return $max > 0 ? (string) $max : null;
}
/**
* Get a specific custom field value.
*
* @param string $name The name of the custom field to retrieve.
* @return string|null
*/
public function get_customfield( $name ) {
if ( Str::is_empty( $name ) ) {
return null;
}
if ( ! empty( get_query_var( 'sitemap' ) ) && 'locations' !== get_query_var( 'sitemap' ) ) {
return null;
}
if ( is_author() ) {
return get_user_meta( $this->args->ID, $name, true );
}
if ( is_category() || is_tag() || is_tax() ) {
return get_term_meta( $this->args->term_id, $name, true );
}
return is_singular() || ! empty( $this->args->post_type ) ? get_post_meta( $this->args->ID, $name, true ) : null;
}
/**
* Get the post type "single" label.
*
* @return string|null
*/
public function get_post_type_single() {
$name = $this->determine_post_type_label( 'single' );
return '' !== $name ? $name : null;
}
/**
* Get the post type "plural" label.
*
* @return string|null
*/
public function get_post_type_plural() {
$name = $this->determine_post_type_label( 'plural' );
return '' !== $name ? $name : null;
}
}