PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.4.2
Forumax – AI Powered Advanced Community Forum Plugin v2.4.2
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / lib / bbpress / includes / core / abstraction.php

abstraction.php in Forumax – AI Powered Advanced Community Forum Plugin 2.4.2, at lib/bbpress/includes/core/abstraction.php

499 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Abstractions
5 *
6 * This file contains functions for abstracting WordPress core functionality
7 * into convenient wrappers so they can be used more reliably.
8 *
9 * Many of the functions in this file are considered superfluous by
10 * WordPress coding standards, but they're handy for plugins of plugins to use.
11 *
12 * @package bbPress
13 * @subpackage Core
14 */
15
16 // Exit if accessed directly
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Setup Admin
21 *
22 * This exists outside of "/includes/admin/" because the converter may need to
23 * be setup to convert the passwords of users that were migrated from another
24 * forum platform.
25 *
26 * @since 2.6.0 bbPress (r2596)
27 */
28 function bbp_setup_admin() {
29 $bbp = bbpress();
30
31 // Skip if already setup
32 if ( empty( $bbp->admin ) ) {
33
34 // Require the admin class
35 require_once $bbp->includes_dir . 'admin/classes/class-bbp-admin.php';
36
37 // Setup
38 $bbp->admin = class_exists( 'BBP_Admin' )
39 ? new BBP_Admin()
40 : new stdClass();
41 }
42
43 // Return the admin object
44 return $bbp->admin;
45 }
46
47 /**
48 * Setup Converter
49 *
50 * This exists outside of "/includes/admin/" because the converter may need to
51 * be setup to convert the passwords of users that were migrated from another
52 * forum platform.
53 *
54 * @since 2.6.0 bbPress (r2596)
55 */
56 function bbp_setup_converter() {
57 $bbp_admin = bbp_setup_admin();
58
59 // Skip if already setup
60 if ( empty( $bbp_admin->converter ) ) {
61
62 // Require the converter files
63 require_once $bbp_admin->admin_dir . 'tools/converter.php';
64 require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter.php';
65 require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter-db.php';
66 require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter-base.php';
67
68 // Setup
69 $bbp_admin->converter = class_exists( 'BBP_Converter' )
70 ? new BBP_Converter()
71 : new stdClass();
72 }
73
74 // Return the converter
75 return $bbp_admin->converter;
76 }
77
78 /** Globals *******************************************************************/
79
80 /**
81 * Lookup and return a global variable
82 *
83 * @since 2.5.8 bbPress (r5814)
84 *
85 * @param string $name Name of global variable
86 * @param string $type Type of variable to check with `is_a()`
87 * @param mixed $default Default value to return if no global found
88 *
89 * @return mixed Verified object if valid, Default or null if invalid
90 */
91 function bbp_get_global_object( $name = '', $type = '', $default = null ) {
92
93 // If no name passed
94 if ( empty( $name ) ) {
95 $retval = $default;
96
97 // If no global exists
98 } elseif ( ! isset( $GLOBALS[ $name ] ) ) {
99 $retval = $default;
100
101 // If not the correct type of global
102 } elseif ( ! empty( $type ) && ! is_a( $GLOBALS[ $name ], $type ) ) {
103 $retval = $default;
104
105 // Global variable exists
106 } else {
107 $retval = $GLOBALS[ $name ];
108 }
109
110 // Filter & return
111 return apply_filters( 'bbp_get_global_object', $retval, $name, $type, $default );
112 }
113
114 /**
115 * Get the `$wp_query` global without needing to declare it everywhere
116 *
117 * @since 2.6.0 bbPress (r6582)
118 *
119 * @return WP_Roles
120 */
121 function bbp_get_wp_query() {
122 return bbp_get_global_object( 'wp_query', 'WP_Query' );
123 }
124
125 /**
126 * Get the `$wp_roles` global without needing to declare it everywhere
127 *
128 * @since 2.2.0 bbPress (r4293)
129 *
130 * @return WP_Roles
131 */
132 function bbp_get_wp_roles() {
133 return bbp_get_global_object( 'wp_roles', 'WP_Roles' );
134 }
135
136 /**
137 * Return the database class being used to interface with the environment.
138 *
139 * This function is abstracted to avoid global touches to the primary database
140 * class. bbPress supports WordPress's `$wpdb` global by default, and can be
141 * filtered to support other configurations if needed.
142 *
143 * @since 2.5.8 bbPress (r5814)
144 *
145 * @return object
146 */
147 function bbp_db() {
148 return bbp_get_global_object( 'wpdb', 'WPDB' );
149 }
150
151 /** Pagination ****************************************************************/
152
153 /**
154 * Return the rewrite rules class being used to interact with URLs.
155 *
156 * This function is abstracted to avoid global touches to the primary rewrite
157 * rules class. bbPress supports WordPress's `$wp_rewrite` by default, but can
158 * be filtered to support other configurations if needed.
159 *
160 * @since 2.5.8 bbPress (r5814)
161 *
162 * @return object
163 */
164 function bbp_rewrite() {
165 return bbp_get_global_object( 'wp_rewrite', 'WP_Rewrite', (object) array(
166 'root' => '',
167 'pagination_base' => 'page',
168 ) );
169 }
170
171 /**
172 * Get the root URL
173 *
174 * @since 2.5.8 bbPress (r5814)
175 *
176 * @return string
177 */
178 function bbp_get_root_url() {
179
180 // Default
181 $retval = '';
182 $rewrite = bbp_rewrite();
183
184 // Use $wp_rewrite->root if available
185 if ( property_exists( $rewrite, 'root' ) ) {
186 $retval = $rewrite->root;
187 }
188
189 // Filter & return
190 return apply_filters( 'bbp_get_root_url', $retval );
191 }
192
193 /**
194 * Get the slug used for paginated requests
195 *
196 * @since 2.4.0 bbPress (r4926)
197 *
198 * @return string
199 */
200 function bbp_get_paged_slug() {
201
202 // Default
203 $retval = 'page';
204 $rewrite = bbp_rewrite();
205
206 // Use $wp_rewrite->pagination_base if available
207 if ( property_exists( $rewrite, 'pagination_base' ) ) {
208 $retval = $rewrite->pagination_base;
209 }
210
211 // Filter & return
212 return apply_filters( 'bbp_get_paged_slug', $retval );
213 }
214
215 /**
216 * Is the environment using pretty URLs?
217 *
218 * @since 2.5.8 bbPress (r5814)
219 *
220 * @global object $wp_rewrite The WP_Rewrite object
221 *
222 * @return bool
223 */
224 function bbp_use_pretty_urls() {
225
226 // Default
227 $retval = false;
228 $rewrite = bbp_rewrite();
229
230 // Use $wp_rewrite->using_permalinks() if available
231 if ( method_exists( $rewrite, 'using_permalinks' ) ) {
232 $retval = $rewrite->using_permalinks();
233 }
234
235 // Filter & return
236 return apply_filters( 'bbp_pretty_urls', $retval );
237 }
238
239 /**
240 * Remove the first-page from a pagination links result set, ensuring that it
241 * points to the canonical first page URL.
242 *
243 * This is a bit of an SEO hack, to guarantee that the first page in a loop will
244 * never have pagination appended to the end of it, regardless of what the other
245 * functions have decided for us.
246 *
247 * @since 2.6.0 bbPress (r6678)
248 *
249 * @param string $pagination_links The HTML links used for pagination
250 *
251 * @return string
252 */
253 function bbp_make_first_page_canonical( $pagination_links = '' ) {
254
255 // Default value
256 $retval = $pagination_links;
257
258 // Remove first page from pagination
259 if ( ! empty( $pagination_links ) ) {
260 $retval = bbp_use_pretty_urls()
261 ? str_replace( bbp_get_paged_slug() . '/1/', '', $pagination_links )
262 : preg_replace( '/&#038;paged=1(?=[^0-9])/m', '', $pagination_links );
263 }
264
265 // Filter & return
266 return apply_filters( 'bbp_make_first_page_canonical', $retval, $pagination_links );
267 }
268
269 /**
270 * A convenient wrapper for common calls to paginate_links(), complete with
271 * support for parameters that aren't used internally by bbPress.
272 *
273 * @since 2.6.0 bbPress (r6679)
274 *
275 * @param array $args
276 *
277 * @return string
278 */
279 function bbp_paginate_links( $args = array() ) {
280
281 // Maybe add view-all args
282 $add_args = empty( $args['add_args'] ) && bbp_get_view_all()
283 ? array( 'view' => 'all' )
284 : false;
285
286 // Pagination settings with filter
287 $r = bbp_parse_args( $args, array(
288
289 // Used by callers
290 'base' => '',
291 'total' => 1,
292 'current' => bbp_get_paged(),
293 'prev_next' => true,
294 'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
295 'next_text' => is_rtl() ? '&larr;' : '&rarr;',
296 'mid_size' => 1,
297 'end_size' => 3,
298 'add_args' => $add_args,
299
300 // Unused by callers
301 'show_all' => false,
302 'type' => 'plain',
303 'format' => '',
304 'add_fragment' => '',
305 'before_page_number' => '',
306 'after_page_number' => ''
307 ), 'paginate_links' );
308
309 // Return paginated links
310 return bbp_make_first_page_canonical( paginate_links( $r ) );
311 }
312
313 /**
314 * Parse the WordPress core version number
315 *
316 * @since 2.6.0 bbPress (r6051)
317 *
318 * @global string $wp_version
319 *
320 * @return string $wp_version
321 */
322 function bbp_get_major_wp_version() {
323 global $wp_version;
324
325 return (float) $wp_version;
326 }
327
328 /** Multisite *****************************************************************/
329
330 /**
331 * Is this a large bbPress installation?
332 *
333 * @since 2.6.0 bbPress (r6242)
334 *
335 * @return bool True if more than 10000 users, false not
336 */
337 function bbp_is_large_install() {
338
339 // Multisite has a function specifically for this
340 $retval = function_exists( 'wp_is_large_network' )
341 ? wp_is_large_network( 'users' )
342 : ( bbp_get_total_users() > 10000 );
343
344 // Filter & return
345 return (bool) apply_filters( 'bbp_is_large_install', $retval );
346 }
347
348 /**
349 * Get the total number of users on the forums
350 *
351 * @since 2.0.0 bbPress (r2769)
352 *
353 * @return int Total number of users
354 */
355 function bbp_get_total_users() {
356 $bbp_db = bbp_db();
357 $count = $bbp_db->get_var( "SELECT COUNT(ID) as c FROM {$bbp_db->users} WHERE user_status = '0'" );
358
359 // Filter & return
360 return (int) apply_filters( 'bbp_get_total_users', (int) $count );
361 }
362
363 /**
364 * Switch to a site in a multisite installation.
365 *
366 * If not a multisite installation, no switching will occur.
367 *
368 * @since 2.6.0 bbPress (r6733)
369 *
370 * @param int $site_id
371 */
372 function bbp_switch_to_site( $site_id = 0 ) {
373
374 // Switch to a specific site
375 if ( is_multisite() ) {
376 switch_to_blog( $site_id );
377 }
378 }
379
380 /**
381 * Switch back to the original site in a multisite installation.
382 *
383 * If not a multisite installation, no switching will occur.
384 *
385 * @since 2.6.0 bbPress (r6733)
386 */
387 function bbp_restore_current_site() {
388
389 // Switch back to the original site
390 if ( is_multisite() ) {
391 restore_current_blog();
392 }
393 }
394
395 /** Interception **************************************************************/
396
397 /**
398 * Generate a default intercept value.
399 *
400 * @since 2.6.0
401 *
402 * @staticvar mixed $rand Null by default, random string on first call
403 *
404 * @return string
405 */
406 function bbp_default_intercept() {
407 static $rand = null;
408
409 // Generate a new random and unique string
410 if ( null === $rand ) {
411
412 // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
413 $algo = function_exists( 'hash' )
414 ? 'sha256'
415 : 'sha1';
416
417 // Old WP installs may not have AUTH_SALT defined.
418 $salt = defined( 'AUTH_SALT' ) && AUTH_SALT
419 ? AUTH_SALT
420 : (string) wp_rand();
421
422 // Create unique ID
423 $rand = hash_hmac( $algo, uniqid( $salt, true ), $salt );
424 }
425
426 // Return random string (from locally static variable)
427 return $rand;
428 }
429
430 /**
431 * Whether a value has been intercepted
432 *
433 * @since 2.6.0
434 *
435 * @param bool $value
436 */
437 function bbp_is_intercepted( $value = '' ) {
438 return ( bbp_default_intercept() !== $value );
439 }
440
441 /**
442 * Allow interception of a method or function call.
443 *
444 * @since 2.6.0
445 *
446 * @param string $action Typically the name of the caller function
447 * @param array $args Typically the results of caller function func_get_args()
448 *
449 * @return mixed Intercept results. Default bbp_default_intercept().
450 */
451 function bbp_maybe_intercept( $action = '', $args = array() ) {
452
453 // Backwards compatibility juggle
454 $hook = ( false === strpos( $action, 'pre_' ) )
455 ? "pre_{$action}"
456 : $action;
457
458 // Default value
459 $default = bbp_default_intercept();
460
461 // Parse args
462 $r = bbp_parse_args( (array) $args, array(), 'maybe_intercept' );
463
464 // Bail if no args
465 if ( empty( $r ) ) {
466 return $default;
467 }
468
469 // Filter
470 $args = array_merge( array( $hook ), $r );
471 $filtered = call_user_func_array( 'apply_filters', $args );
472
473 // Return filtered value, or default if not intercepted
474 return ( $filtered === reset( $r ) )
475 ? $default
476 : $filtered;
477 }
478
479 /** Date/Time *****************************************************************/
480
481 /**
482 * Get an empty datetime value.
483 *
484 * @since 2.6.6 bbPress (r7094)
485 *
486 * @return string
487 */
488 function bbp_get_empty_datetime() {
489
490 // Get the database version
491 $db_version = bbp_db()->db_version();
492
493 // Default return value
494 $retval = '0000-00-00 00:00:00';
495
496 // Filter & return
497 return (string) apply_filters( 'bbp_get_default_zero_date', $retval, $db_version );
498 }
499