1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Auto Load Next Post Template |
4
|
|
|
* |
5
|
|
|
* Functions for the templating system. |
6
|
|
|
* |
7
|
|
|
* @since 1.6.0 |
8
|
|
|
* @author Sébastien Dumont |
9
|
|
|
* @category Core |
10
|
|
|
* @package Auto Load Next Post/Functions |
11
|
|
|
* @license GPL-2.0+ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
// Exit if accessed directly. |
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
16
|
|
|
exit; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if ( ! function_exists( 'alnp_template_redirect' ) ) { |
20
|
|
|
/** |
21
|
|
|
* When the 'alnp' endpoint is used on a singular post it forces |
22
|
|
|
* the template redirect to retrieve only the post content. |
23
|
|
|
* |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* @version 1.6.0 |
26
|
|
|
* @global WP_Query $wp_query - The object information defining the current request and determines what type of query it's dealing with. See https://codex.wordpress.org/Class_Reference/WP_Query |
27
|
|
|
*/ |
28
|
|
|
function alnp_template_redirect() { |
29
|
|
|
global $wp_query; |
30
|
|
|
|
31
|
|
|
// If this is not a request for alnp or a singular object then bail. |
32
|
|
|
if ( ! isset( $wp_query->query_vars['alnp'] ) || ! is_singular() ) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Load the template file from the theme (child or parent) if one exists. |
38
|
|
|
* If theme does not have a template file for Auto Load Next Post, |
39
|
|
|
* the plugin will load a default template. |
40
|
|
|
*/ |
41
|
|
|
$child_path = get_stylesheet_directory() . '/' . AUTO_LOAD_NEXT_POST_TEMPLATE_PATH; |
42
|
|
|
$template_path = get_template_directory() . '/' . AUTO_LOAD_NEXT_POST_TEMPLATE_PATH; |
43
|
|
|
$default_path = AUTO_LOAD_NEXT_POST_FILE_PATH; |
44
|
|
|
$template_redirect = ''; |
45
|
|
|
|
46
|
|
|
if ( file_exists( $child_path . 'content-alnp.php' ) ) { |
47
|
|
|
$template_redirect = $child_path . 'content-alnp.php'; |
48
|
|
|
} else if ( file_exists( $template_path . 'content-alnp.php' ) ) { |
49
|
|
|
$template_redirect = $template_path . 'content-alnp.php'; |
50
|
|
|
} else if ( file_exists( $default_path . '/templates/content-alnp.php' ) ) { |
51
|
|
|
$template_redirect = $default_path . '/templates/content-alnp.php'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$template_redirect = apply_filters( 'alnp_template_redirect', $template_redirect ); |
55
|
|
|
|
56
|
|
|
include( $template_redirect ); |
57
|
|
|
|
58
|
|
|
exit; |
59
|
|
|
} // END alnp_template_redirect() |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ( ! function_exists( 'auto_load_next_post_comments' ) ) { |
63
|
|
|
/** |
64
|
|
|
* Adds the comments template after the post content. |
65
|
|
|
* |
66
|
|
|
* @since 1.4.8 |
67
|
|
|
* @version 1.5.4 |
68
|
|
|
*/ |
69
|
|
|
function auto_load_next_post_comments() { |
70
|
|
|
// If comments are open or we have at least one comment, load up the comment template. |
71
|
|
|
if ( comments_open() || get_comments_number() ) : |
72
|
|
|
comments_template(); |
73
|
|
|
endif; |
74
|
|
|
} // END auto_load_next_post_comments() |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ( ! function_exists( 'auto_load_next_post_navigation' ) ) { |
78
|
|
|
/** |
79
|
|
|
* Adds the post navigation for the previous link only after the post content. |
80
|
|
|
* |
81
|
|
|
* @since 1.4.8 |
82
|
|
|
* @version 1.5.4 |
83
|
|
|
*/ |
84
|
|
|
function auto_load_next_post_navigation() { |
85
|
|
|
?> |
86
|
|
|
<nav class="navigation post-navigation" role="navigation"> |
87
|
|
|
<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'auto-load-next-post' ) . '</span> %title' ); ?></span> |
88
|
|
|
</nav> |
89
|
|
|
<?php |
90
|
|
|
} // END auto_load_next_post_navigation() |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( ! function_exists( 'alnp_get_locations' ) ) { |
94
|
|
|
/** |
95
|
|
|
* Get list of locations as to where the content for the theme is located. |
96
|
|
|
* The list is in the order to look for the templates that store the content. |
97
|
|
|
* |
98
|
|
|
* @since 1.6.0 |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
function alnp_get_locations() { |
102
|
|
|
return array( |
103
|
|
|
'', // Parent theme folder |
104
|
|
|
'components/', |
105
|
|
|
'components/post/', |
106
|
|
|
'components/page/', |
107
|
|
|
'template-parts/', |
108
|
|
|
'template-parts/content/', |
109
|
|
|
'template-parts/post/', |
110
|
|
|
'template-parts/page/', |
111
|
|
|
'templates/', |
112
|
|
|
'partials/', |
113
|
|
|
'loop-templates/' |
114
|
|
|
); |
115
|
|
|
} // END alnp_get_locations() |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ( ! function_exists( 'alnp_get_templates' ) ) { |
119
|
|
|
/** |
120
|
|
|
* Get list of templates to look for. |
121
|
|
|
* |
122
|
|
|
* @since 1.6.0 |
123
|
|
|
* @param string $post_type |
124
|
|
|
* @param string $post_format |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
function alnp_get_templates( $post_type = 'post', $post_format = '' ) { |
128
|
|
|
$get_standard = array( |
129
|
|
|
'content-single.php', |
130
|
|
|
'content-post.php', |
131
|
|
|
'content-' . $post_type . '.php', |
132
|
|
|
'content.php' |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
if ( ! empty( $post_format ) ) { |
136
|
|
|
$get_formats = array( |
137
|
|
|
'format-' . $post_format . '.php', |
138
|
|
|
'content' . $post_format . '.php' |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
return array_merge( $get_standard, $get_formats ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $get_standard; |
145
|
|
|
} // END alnp_get_templates() |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ( ! function_exists( 'alnp_scan_directories' ) ) { |
149
|
|
|
/** |
150
|
|
|
* Scans for the theme template directory depending on the post type requested and saves it. |
151
|
|
|
* |
152
|
|
|
* @since 1.6.0 |
153
|
|
|
* @param string $post_type |
154
|
|
|
* @param string $post_format |
155
|
|
|
*/ |
156
|
|
|
function alnp_scan_directories( $post_type = 'post', $post_format = '' ) { |
157
|
|
|
// Possible locations where the content files are found. |
158
|
|
|
$locations = alnp_get_locations(); |
159
|
|
|
|
160
|
|
|
// Templates to look for based on the post that is loaded. |
161
|
|
|
$templates = alnp_get_templates( $post_type, $post_format ); |
162
|
|
|
|
163
|
|
|
$content_found = false; |
164
|
|
|
|
165
|
|
|
// Scanning all possible locations. |
166
|
|
|
foreach( $locations as $location ) { |
167
|
|
|
// Scanning all possible templates within the locations. |
168
|
|
|
foreach( $templates as $template ) { |
169
|
|
|
// Remove forwardslash if location is the parent theme folder. |
170
|
|
|
if ( empty( $location ) ) { |
171
|
|
|
$location = str_replace('/', '', $location); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// If a template has been found then save it. |
175
|
|
|
if ( locate_template( $location . $template ) != '' && $content_found !== true ) { |
176
|
|
|
// Save template found. |
177
|
|
|
if ( ! empty( $post_format ) ) { |
178
|
|
|
update_option( 'auto_load_next_post_directory_post_' . $post_format, $location ); |
179
|
|
|
} else { |
180
|
|
|
update_option( 'auto_load_next_post_directory_' . $post_type, $location ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$content_found = true; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} // END alnp_scan_directories() |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
if ( ! function_exists( 'alnp_get_template_directory' ) ) { |
191
|
|
|
/** |
192
|
|
|
* Returns the template directory saved. |
193
|
|
|
* |
194
|
|
|
* @since 1.6.0 |
195
|
|
|
* @param string $post_type |
196
|
|
|
* @param string $post_format |
197
|
|
|
* @return string $template |
198
|
|
|
*/ |
199
|
|
|
function alnp_get_template_directory( $post_type = 'post', $post_format = '' ) { |
200
|
|
|
if ( ! empty( $post_format ) ) { |
201
|
|
|
$template = get_option( 'auto_load_next_post_directory_post_' . $post_format ); |
202
|
|
|
} else { |
203
|
|
|
$template = get_option( 'auto_load_next_post_directory_' . $post_type ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ( !$template ) { |
207
|
|
|
return ''; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $template; |
211
|
|
|
} // END alnp_get_template_directory() |
212
|
|
|
} |
213
|
|
|
|
214
|
|
View Code Duplication |
if ( ! function_exists( 'alnp_get_template' ) ) { |
215
|
|
|
/** |
216
|
|
|
* Returns the template file saved. |
217
|
|
|
* |
218
|
|
|
* @since 1.6.0 |
219
|
|
|
* @param string $post_type |
220
|
|
|
* @param string $post_format |
221
|
|
|
* @return string $template |
222
|
|
|
*/ |
223
|
|
|
function alnp_get_template( $post_type = 'post', $post_format = '' ) { |
224
|
|
|
if ( ! empty( $post_format ) ) { |
225
|
|
|
$template = get_option( 'auto_load_next_post_template_post_' . strtolower( $post_format ) ); |
226
|
|
|
} else { |
227
|
|
|
$template = get_option( 'auto_load_next_post_template_' . strtolower( $post_type ) ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ( !$template ) { |
231
|
|
|
return ''; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $template; |
235
|
|
|
} // END alnp_get_template() |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ( ! function_exists( 'alnp_find_template' ) ) { |
239
|
|
|
/** |
240
|
|
|
* Scans through the active theme to look for the content to load. |
241
|
|
|
* If the content is not found then the fallback will be used. |
242
|
|
|
* |
243
|
|
|
* @since 1.6.0 |
244
|
|
|
* @param string $location |
245
|
|
|
* @param string $post_type |
246
|
|
|
* @param string $post_format |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
function alnp_find_template( $location = '', $post_type, $post_format ) { |
250
|
|
|
// Templates to look for based on the post that is loaded. |
251
|
|
|
$templates = alnp_get_templates( $post_type, $post_format ); |
252
|
|
|
|
253
|
|
|
$found = false; |
254
|
|
|
|
255
|
|
|
// Scanning all possible templates within the set location. |
256
|
|
|
foreach( $templates as $template ) { |
257
|
|
|
// Remove forwardslash if location is the parent theme folder. |
258
|
|
|
if ( empty( $location ) ) { |
259
|
|
|
$location = str_replace('/', '', $location); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// If a template has been found then return it. |
263
|
|
|
if ( locate_template( $location . $template ) != '' && $found != true ) { |
|
|
|
|
264
|
|
|
$file = $location . $template; |
265
|
|
|
|
266
|
|
|
if ( ! empty( $post_format ) ) { |
267
|
|
|
update_option( 'auto_load_next_post_template_post_' . strtolower( $post_format ), $file ); |
268
|
|
|
} else { |
269
|
|
|
update_option( 'auto_load_next_post_template_' . strtolower( $post_type ), $file ); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$found = true; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return array( |
277
|
|
|
'template' => $file, |
|
|
|
|
278
|
|
|
'found' => $found |
279
|
|
|
); |
280
|
|
|
} // END alnp_find_template() |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if ( ! function_exists( 'alnp_load_content' ) ) { |
284
|
|
|
/** |
285
|
|
|
* Loads theme template set either by theme support or setup wizard. |
286
|
|
|
* If the content is not found then a fallback template will be used. |
287
|
|
|
* |
288
|
|
|
* @since 1.6.0 |
289
|
|
|
* @param string $post_type |
290
|
|
|
* @param string $post_format |
291
|
|
|
*/ |
292
|
|
|
function alnp_load_content( $post_type, $post_format ) { |
293
|
|
|
// Returns template location for supported themes. |
294
|
|
|
$template_location = alnp_template_location(); |
295
|
|
|
|
296
|
|
|
$content_found = false; |
297
|
|
|
|
298
|
|
|
// Check and return directory and template if already found. |
299
|
|
|
if ( empty( $template_location ) ) { |
300
|
|
|
$directory = alnp_get_template_directory( $post_type, $post_format ); |
301
|
|
|
} else { |
302
|
|
|
$directory = $template_location; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$template = alnp_get_template( $post_type, $post_format ); |
306
|
|
|
|
307
|
|
|
if ( ! empty( $directory ) && ! empty( $template ) ) { |
308
|
|
|
$template = $directory . $template; |
309
|
|
|
$content_found = true; |
310
|
|
|
} else { |
311
|
|
|
// Possible locations where the content files are found. |
312
|
|
|
$locations = alnp_get_locations(); |
313
|
|
|
|
314
|
|
|
// Scanning all possible locations. |
315
|
|
|
foreach( $locations as $location ) { |
316
|
|
|
$template = alnp_find_template( $directory, $post_type, $post_format ); |
|
|
|
|
317
|
|
|
$content_found = $template['found']; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// Can be overridden. |
322
|
|
|
$content_found = apply_filters( 'alnp_content_found', $content_found ); |
323
|
|
|
|
324
|
|
|
// Check if the user has forced the use of the fallback template. |
325
|
|
|
$use_fallback = get_option( 'auto_load_next_post_use_fallback' ); |
326
|
|
|
|
327
|
|
|
// If content is found then load the template part. |
328
|
|
|
if ( $content_found && empty( $use_fallback ) ) { |
329
|
|
|
locate_template( $template, true, false ); |
330
|
|
|
} |
331
|
|
|
else { |
332
|
|
|
do_action( 'alnp_load_content', $post_type ); |
333
|
|
|
} |
334
|
|
|
} // END alnp_load_content() |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
if ( ! function_exists( 'alnp_load_fallback_content' ) ) { |
338
|
|
|
/** |
339
|
|
|
* Load fallback template should no content file be found. |
340
|
|
|
* |
341
|
|
|
* @since 1.6.0 |
342
|
|
|
* @param string $post_type |
343
|
|
|
*/ |
344
|
|
|
function alnp_load_fallback_content( $post_type ) { |
345
|
|
|
get_template_part( AUTO_LOAD_NEXT_POST_FILE_PATH . '/templates/content/content', $post_type ); |
346
|
|
|
} // END alnp_load_fallback_content() |
347
|
|
|
} |
348
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.