| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Converter |
| 5 |
* |
| 6 |
* Based on the hard work of Adam Ellis |
| 7 |
* |
| 8 |
* @package bbPress |
| 9 |
* @subpackage Administration |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Return an array of available converters |
| 17 |
* |
| 18 |
* @since 2.6.0 bbPress (r6447) |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
function bbp_get_converters() { |
| 23 |
static $files = array(); |
| 24 |
|
| 25 |
// Only hit the file system one time per page load |
| 26 |
if ( empty( $files ) ) { |
| 27 |
|
| 28 |
// Open the converter directory |
| 29 |
$path = bbp_setup_converter()->converters_dir; |
| 30 |
$curdir = opendir( $path ); |
| 31 |
|
| 32 |
// Look for the converter file in the converters directory |
| 33 |
if ( false !== $curdir ) { |
| 34 |
while ( $file = readdir( $curdir ) ) { |
| 35 |
if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === false ) { |
| 36 |
$name = preg_replace( '/.php/', '', $file ); |
| 37 |
if ( 'Example' !== $name ) { |
| 38 |
$files[ $name ] = $path . $file; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Close the directory |
| 45 |
closedir( $curdir ); |
| 46 |
|
| 47 |
// Sort keys alphabetically, ignoring upper/lower casing |
| 48 |
if ( ! empty( $files ) ) { |
| 49 |
natcasesort( $files ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Filter & return |
| 54 |
return (array) apply_filters( 'bbp_get_converters', $files ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* This is a function that is purposely written to look like a "new" statement. |
| 59 |
* It is basically a dynamic loader that will load in the platform conversion |
| 60 |
* of your choice. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* |
| 64 |
* @param string $platform Name of valid platform class. |
| 65 |
* |
| 66 |
* @return mixed Object if converter exists, null if not |
| 67 |
*/ |
| 68 |
function bbp_new_converter( $platform = '' ) { |
| 69 |
|
| 70 |
// Default converter |
| 71 |
$converter = null; |
| 72 |
|
| 73 |
// Bail if no platform |
| 74 |
if ( empty( $platform ) ) { |
| 75 |
return $converter; |
| 76 |
} |
| 77 |
|
| 78 |
// Get the available converters |
| 79 |
$converters = bbp_get_converters(); |
| 80 |
|
| 81 |
// Get the converter file form converters array |
| 82 |
$converter_file = isset( $converters[ $platform ] ) |
| 83 |
? $converters[ $platform ] |
| 84 |
: ''; |
| 85 |
|
| 86 |
// Try to create a new converter object |
| 87 |
if ( ! empty( $converter_file ) ) { |
| 88 |
|
| 89 |
// Try to include the converter |
| 90 |
@include_once $converter_file; |
| 91 |
|
| 92 |
// Try to instantiate the converter object |
| 93 |
if ( class_exists( $platform ) ) { |
| 94 |
$converter = new $platform(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Filter & return |
| 99 |
return apply_filters( 'bbp_new_converter', $converter, $platform ); |
| 100 |
} |
| 101 |
|