1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Auto Load Next Post Core Functions |
4
|
|
|
* |
5
|
|
|
* General core functions available for both the front-end and admin. |
6
|
|
|
* |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
* @version 1.5.7 |
9
|
|
|
* @author Sébastien Dumont |
10
|
|
|
* @category Core |
11
|
|
|
* @package Auto Load Next Post/Core/Functions |
12
|
|
|
* @license GPL-2.0+ |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
// Exit if accessed directly. |
16
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
17
|
|
|
exit; |
18
|
|
|
} |
19
|
|
|
|
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
|
|
|
* @access public |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
* @version 1.5.4 |
27
|
|
|
* @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 |
28
|
|
|
*/ |
29
|
|
|
if ( ! function_exists( 'auto_load_next_post_template_redirect' ) ) { |
30
|
|
|
function auto_load_next_post_template_redirect() { |
31
|
|
|
global $wp_query; |
32
|
|
|
|
33
|
|
|
// If this is not a request for alnp or a singular object then bail |
34
|
|
|
if ( ! isset( $wp_query->query_vars['alnp'] ) || ! is_singular() ) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Load the template file from the theme (child or parent) if one exists. |
40
|
|
|
* If theme does not have a template file for Auto Load Next Post, |
41
|
|
|
* the plugin will load a default template. |
42
|
|
|
*/ |
43
|
|
|
$child_path = get_stylesheet_directory() . '/' . AUTO_LOAD_NEXT_POST_TEMPLATE_PATH; |
44
|
|
|
$template_path = get_template_directory() . '/' . AUTO_LOAD_NEXT_POST_TEMPLATE_PATH; |
45
|
|
|
$default_path = AUTO_LOAD_NEXT_POST_FILE_PATH; |
46
|
|
|
|
47
|
|
|
if ( file_exists( $child_path . 'content-alnp.php' ) ) { |
48
|
|
|
$template_redirect = $child_path . 'content-alnp.php'; |
49
|
|
|
} |
50
|
|
|
else if( file_exists( $template_path . 'content-alnp.php') ) { |
51
|
|
|
$template_redirect = $template_path . 'content-alnp.php'; |
52
|
|
|
} |
53
|
|
|
else if( file_exists( $default_path . '/template/content-alnp.php' ) ) { |
54
|
|
|
$template_redirect = $default_path . '/template/content-alnp.php'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$template_redirect = apply_filters( 'alnp_template_redirect', $template_redirect ); |
|
|
|
|
58
|
|
|
|
59
|
|
|
include( $template_redirect ); |
60
|
|
|
|
61
|
|
|
exit; |
62
|
|
|
} // END auto_load_next_post_template_redirect() |
63
|
|
|
} |
64
|
|
|
add_action( 'template_redirect', 'auto_load_next_post_template_redirect' ); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds the comments template after the post content. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @since 1.4.8 |
71
|
|
|
* @version 1.5.4 |
72
|
|
|
*/ |
73
|
|
|
if ( ! function_exists( 'auto_load_next_post_comments' ) ) { |
74
|
|
|
function auto_load_next_post_comments() { |
75
|
|
|
// If comments are open or we have at least one comment, load up the comment template. |
76
|
|
|
if ( comments_open() || get_comments_number() ) : |
77
|
|
|
comments_template(); |
78
|
|
|
endif; |
79
|
|
|
} // END auto_load_next_post_comments() |
80
|
|
|
} |
81
|
|
|
add_action( 'alnp_load_after_content', 'auto_load_next_post_comments', 1, 5 ); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Adds the post navigation for the previous link only after the post content. |
85
|
|
|
* |
86
|
|
|
* @access public |
87
|
|
|
* @since 1.4.8 |
88
|
|
|
* @version 1.5.4 |
89
|
|
|
*/ |
90
|
|
|
if ( ! function_exists( 'auto_load_next_post_navigation' ) ) { |
91
|
|
|
function auto_load_next_post_navigation() { |
92
|
|
|
?> |
93
|
|
|
<nav class="navigation post-navigation" role="navigation"> |
94
|
|
|
<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'auto-load-next-post' ) . '</span> %title' ); ?></span> |
95
|
|
|
</nav> |
96
|
|
|
<?php |
97
|
|
|
} // END auto_load_next_post_navigation() |
98
|
|
|
} |
99
|
|
|
add_action( 'alnp_load_after_content', 'auto_load_next_post_navigation', 1, 10 ); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the permalink of a random page |
103
|
|
|
* |
104
|
|
|
* @since 1.5.0 |
105
|
|
|
* @param string $post_type - Default is post. |
106
|
|
|
* @return int|boolean |
107
|
|
|
*/ |
108
|
|
|
if ( ! function_exists( 'alnp_get_random_page_permalink' ) ) { |
109
|
|
|
function alnp_get_random_page_permalink( $post_type = 'post' ) { |
110
|
|
|
$args = array( |
111
|
|
|
'post_type' => $post_type, |
112
|
|
|
'post_status' => 'publish', |
113
|
|
|
'orderby' => 'rand', |
114
|
|
|
'posts_per_page' => 1 |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$query = new WP_Query( $args ); |
118
|
|
|
|
119
|
|
|
if ( $query->have_posts() ) { |
120
|
|
|
while ( $query->have_posts() ) : $query->the_post(); |
121
|
|
|
$id = get_the_ID(); |
122
|
|
|
|
123
|
|
|
return get_permalink( $id ); |
124
|
|
|
endwhile; |
125
|
|
|
} |
126
|
|
|
else { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} // END alnp_get_random_page_permalink() |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* This helps the plugin decide to load the JavaScript in the footer or not. |
134
|
|
|
* |
135
|
|
|
* @since 1.5.7 |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
if ( ! function_exists( 'alnp_load_js_in_footer' ) ) { |
139
|
|
|
function alnp_load_js_in_footer() { |
140
|
|
|
$load_in_footer = get_option( 'auto_load_next_post_load_js_in_footer', false ); |
141
|
|
|
|
142
|
|
|
if ( isset( $load_in_footer ) && $load_in_footer == 'yes' ) { |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* These are the only screens Auto Load Next Post will focus |
152
|
|
|
* on displaying notices or equeue scripts/styles. |
153
|
|
|
* |
154
|
|
|
* @since 1.5.11 |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
if ( ! function_exists( 'alnp_get_admin_screens' ) ) { |
158
|
|
|
function alnp_get_admin_screens() { |
159
|
|
|
$show_on_screens = array( |
160
|
|
|
'dashboard', |
161
|
|
|
'plugins', |
162
|
|
|
'settings_page_auto-load-next-post-settings' |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return $show_on_screens; |
166
|
|
|
} |
167
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: