| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template view manager class. |
| 4 |
* |
| 5 |
* @link https://duckdev.com/products/loggedin-limit-active-logins/ |
| 6 |
* @license http://www.gnu.org/licenses/ GNU General Public License |
| 7 |
* @category Core |
| 8 |
* @package Loggedin |
| 9 |
* @author Joel James <me@joelsays.com> |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace DuckDev\Loggedin; |
| 13 |
|
| 14 |
// If this file is called directly, abort. |
| 15 |
defined( 'WPINC' ) || die; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class View |
| 19 |
*/ |
| 20 |
class View { |
| 21 |
|
| 22 |
/** |
| 23 |
* Dummy initializer. |
| 24 |
* |
| 25 |
* @since 2.0.0 |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
protected function __construct() { |
| 30 |
// Nothing. |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Render a template into a variable. |
| 35 |
* |
| 36 |
* This will look for the template file inside |
| 37 |
* /app/templates/{file}.php |
| 38 |
* |
| 39 |
* @since 2.0.0 |
| 40 |
* |
| 41 |
* @param string $file File path. |
| 42 |
* @param array $args Arguments. |
| 43 |
* @param bool $once Should include once. |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public static function get_render( string $file, array $args = array(), bool $once = true ): string { |
| 48 |
ob_start(); |
| 49 |
|
| 50 |
// Render the template. |
| 51 |
self::render( $file, $args, $once ); |
| 52 |
|
| 53 |
return ob_get_clean(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Render a template. |
| 58 |
* |
| 59 |
* This will look for the template file inside |
| 60 |
* /app/templates/{file}.php |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* |
| 64 |
* @param string $file File path. |
| 65 |
* @param array $args Arguments. |
| 66 |
* @param bool $once Should include once. |
| 67 |
* @param bool $absolute Is absolute path. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public static function render( string $file, array $args = array(), bool $once = false, bool $absolute = false ) { |
| 72 |
// Full path to the file. |
| 73 |
if ( ! $absolute ) { |
| 74 |
$file = LOGGEDIN_DIR . "/app/templates/{$file}.php"; |
| 75 |
} |
| 76 |
|
| 77 |
if ( file_exists( $file ) ) { |
| 78 |
extract( $args ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 79 |
|
| 80 |
if ( $once ) { |
| 81 |
include_once $file; |
| 82 |
} else { |
| 83 |
include $file; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|