Completed
Pull Request — master (#133)
by Sébastien
01:51
created

auto-load-next-post-conditional-functions.php ➔ alnp_get_theme_support()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 12
nop 2
dl 0
loc 34
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Auto Load Next Post: Conditional Functions
4
 *
5
 * Functions for determining the current query/page,
6
 * theme support and if user agent is a bot.
7
 *
8
 * @since    1.0.0
9
 * @version  1.5.0
10
 * @author   Sébastien Dumont
11
 * @category Core
12
 * @package  Auto Load Next Post/Core/Functions
13
 * @license  GPL-2.0+
14
 */
15
16
// Exit if accessed directly.
17
if ( ! defined( 'ABSPATH' ) ) {
18
	exit;
19
}
20
21
if ( ! function_exists( 'auto_load_next_post_is_ajax' ) ) {
22
	/**
23
	 * Returns true when the page is loaded via ajax.
24
	 *
25
	 * @access public
26
	 * @since  1.0.0
27
	 * @return bool
28
	 */
29
	function auto_load_next_post_is_ajax() {
30
		if ( defined( 'DOING_AJAX' ) ) {
31
			return true;
32
		}
33
34
		return( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) ? true : false;
35
	} // END auto_load_next_post_is_ajax
36
}
37
38
if ( ! function_exists( 'alnp_template_location' ) ) {
39
	/**
40
	 * Filters the template location for get_template_part().
41
	 *
42
	 * @access  public
43
	 * @since   1.4.8
44
	 * @version 1.5.0
45
	 * @return  boolean
46
	 */
47
	function alnp_template_location() {
48
		$current_theme = get_option('template');
49
50
		switch( $current_theme ) {
51
			case 'twentyseventeen':
52
				$path = 'template-parts/post/';
53
				break;
54
55
			case 'twentysixteen':
56
				$path = 'template-parts/';
57
				break;
58
59
			default:
60
				$path = '';
61
				break;
62
		}
63
64
		return apply_filters( 'alnp_template_location', $path );
65
	} // END alnp_template_location()
66
}
67
68
if ( ! function_exists( 'is_alnp_pro_version_installed' ) ) {
69
	/**
70
	 * Detects if Auto Load Next Post Pro is installed.
71
	 *
72
	 * @access public
73
	 * @since  1.4.10
74
	 * @return boolean
75
	 */
76
	function is_alnp_pro_version_installed() {
77
		$active_plugins = (array) get_option( 'active_plugins', array() );
78
79
		if ( is_multisite() ) {
80
			$active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
81
		}
82
83
		return in_array( 'auto-load-next-post-pro/auto-load-next-post-pro.php', $active_plugins ) || array_key_exists( 'auto-load-next-post-pro/auto-load-next-post-pro.php', $active_plugins );
84
	}
85
}
86
87
if ( ! function_exists( 'is_alnp_beta' ) ) {
88
	/**
89
	 * Returns true if Auto Load Next Post is a beta release.
90
	 *
91
	 * @since  1.5.0
92
	 * @return boolean
93
	 */
94
	function is_alnp_beta() {
95
		if ( strpos( AUTO_LOAD_NEXT_POST_VERSION, 'beta' ) ) {
96
			return true;
97
		}
98
99
		return false;
100
	}
101
}
102
103
if ( ! function_exists( 'is_alnp_active_theme' ) ) {
104
	/**
105
	 * See if theme/s is activate or not.
106
	 *
107
	 * @since 1.5.0
108
	 * @param string|array $theme Theme name or array of theme names to check.
109
	 * @return boolean
110
	 */
111
	function is_alnp_active_theme( $theme ) {
112
		return is_array( $theme ) ? in_array( get_template(), $theme, true ) : get_template() === $theme;
113
	}
114
}
115
116
if ( ! function_exists( 'is_alnp_supported' ) ) {
117
	/**
118
	 * Returns true if Auto Load Next Post is supported in the theme.
119
	 *
120
	 * @since  1.5.0
121
	 * @return boolean
122
	 */
123
	function is_alnp_supported() {
124
		$theme_support = current_theme_supports( 'auto-load-next-post' );
125
126
		if ( ! $theme_support ) {
127
			return false;
128
		}
129
130
		return true;
131
	}
132
}
133
134
if ( ! function_exists( 'alnp_get_theme_support' ) ) {
135
	/**
136
	 * Return "theme support" values from the current theme, if set.
137
	 *
138
	 * @since  1.5.0
139
	 * @param  string $prop Name of prop (or key::subkey for arrays of props) if you want a specific value. Leave blank to get all props as an array.
140
	 * @param  mixed  $default Optional value to return if the theme does not declare support for a prop.
141
	 * @return mixed  Value of prop(s).
142
	 */
143
	function alnp_get_theme_support( $prop = '', $default = null ) {
144
		$theme_support = get_theme_support( 'auto-load-next-post' );
145
		$theme_support = is_array( $theme_support ) ? $theme_support[0] : false;
146
147
		if ( ! $theme_support ) {
148
			return $default;
149
		}
150
151
		if ( ! empty( $prop ) ) {
152
			$prop_stack = explode( '::', $prop );
153
			$prop_key   = array_shift( $prop_stack );
154
155
			if ( isset( $theme_support[ $prop_key ] ) ) {
156
				$value = $theme_support[ $prop_key ];
157
158
				if ( count( $prop_stack ) ) {
159
					foreach ( $prop_stack as $prop_key ) {
160
						if ( is_array( $value ) && isset( $value[ $prop_key ] ) ) {
161
							$value = $value[ $prop_key ];
162
						} else {
163
							$value = $default;
164
							break;
165
						}
166
					}
167
				}
168
			} else {
169
				$value = $default;
170
			}
171
172
			return $value;
173
		}
174
175
		return $theme_support;
176
	}
177
}
178
179
if ( ! function_exists( 'alnp_is_bot' ) ) {
180
	/**
181
	 * Was the current request made by a known bot?
182
	 *
183
	 * @since  1.5.0
184
	 * @return boolean
185
	 */
186
	function alnp_is_bot() {
187
		$is_bot = alnp_is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
188
189
		return $is_bot;
190
	}
191
}
192
193
if ( ! function_exists( 'alnp_is_bot_user_agent' ) ) {
194
	/**
195
	 * Is the given user-agent a known bot?
196
	 *
197
	 * @since  1.5.0
198
	 * @param  string A user-agent string
199
	 * @return boolean
200
	 */
201
	function alnp_is_bot_user_agent( $ua = null ) {
202
		if ( empty( $ua ) ) {
203
			return false;
204
		}
205
206
		$bot_agents = array(
207
			'alexa', 'altavista', 'ask jeeves', 'attentio', 'baiduspider', 'bingbot', 'chtml generic', 'crawler', 'fastmobilecrawl',
208
			'feedfetcher-google', 'firefly', 'froogle', 'gigabot', 'googlebot', 'googlebot-mobile', 'heritrix', 'httrack', 'ia_archiver', 'irlbot',
209
			'iescholar', 'infoseek', 'jumpbot', 'linkcheck', 'lycos', 'mediapartners', 'mediobot', 'motionbot', 'msnbot', 'mshots', 'openbot',
210
			'pss-webkit-request', 'pythumbnail', 'scooter', 'slurp', 'snapbot', 'spider', 'taptubot', 'technoratisnoop',
211
			'teoma', 'twiceler', 'yahooseeker', 'yahooysmcm', 'yammybot', 'ahrefsbot', 'pingdom.com_bot', 'kraken', 'yandexbot',
212
			'twitterbot', 'tweetmemebot', 'openhosebot', 'queryseekerspider', 'linkdexbot', 'grokkit-crawler',
213
			'livelapbot', 'germcrawler', 'domaintunocrawler', 'grapeshotcrawler', 'cloudflare-alwaysonline',
214
		);
215
216
		foreach ( $bot_agents as $bot_agent ) {
217
			if ( false !== stripos( $ua, $bot_agent ) ) {
218
				return true;
219
			}
220
		}
221
222
		return false;
223
	}
224
}
225
226
if ( ! function_exists( 'alnp_get_post_type' ) ) {
227
	/**
228
	 * Returns the post type.
229
	 *
230
	 * @access public
231
	 * @since  1.4.12
232
	 * @return string
233
	 */
234
	function alnp_get_post_type() {
235
		$post_type = get_post_type();
236
237
		// If the post type is a post then return single instead.
238
		if ( $post_type == 'post' ) {
239
			return 'single';
240
		}
241
242
		return $post_type;
243
	}
244
}
245