| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Template |
| 7 |
* |
| 8 |
* @package LearnPress\Helpers |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.1 |
| 11 |
*/ |
| 12 |
class Template { |
| 13 |
/** |
| 14 |
* @var bool |
| 15 |
*/ |
| 16 |
protected $include; |
| 17 |
|
| 18 |
protected function __construct() { |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Set 1 for include file, 0 for not |
| 24 |
* Set 1 for separate template is block, 0 for not | use "wp_is_block_theme" function |
| 25 |
* |
| 26 |
* @param bool $include |
| 27 |
* |
| 28 |
* @return self |
| 29 |
*/ |
| 30 |
public static function instance( bool $include = true ): Template { |
| 31 |
$self = new self(); |
| 32 |
$self->include = $include; |
| 33 |
|
| 34 |
return $self; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get template admin file |
| 39 |
* |
| 40 |
* @param string $file_name |
| 41 |
* @param array $args |
| 42 |
* |
| 43 |
* @return void|string |
| 44 |
* @since 1.0.0 |
| 45 |
* @version 1.0.0 |
| 46 |
*/ |
| 47 |
public function get_admin_template( string $file_name = '', array $args = array() ) { |
| 48 |
$file_name = str_replace( '.php', '', $file_name ); |
| 49 |
$path_file = LP_PLUGIN_PATH . "inc/admin/views/{$file_name}.php"; |
| 50 |
|
| 51 |
$template = $this->get_template( $path_file, $args ); |
| 52 |
|
| 53 |
if ( ! $this->include ) { |
| 54 |
return $template; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get template frontend file |
| 60 |
* |
| 61 |
* @param string $file_name |
| 62 |
* @param array $args |
| 63 |
* |
| 64 |
* @return void|string |
| 65 |
* @since 1.0.0 |
| 66 |
* @version 1.0.0 |
| 67 |
*/ |
| 68 |
public function get_frontend_template( string $file_name = '', array $args = array() ) { |
| 69 |
$default_path = LP_PLUGIN_PATH . "templates/{$file_name}"; |
| 70 |
$folder_name_rewrite = learn_press_template_path(); |
| 71 |
$from_child_theme_path = sprintf( |
| 72 |
'%s/%s/%s', |
| 73 |
get_stylesheet_directory(), |
| 74 |
$folder_name_rewrite, |
| 75 |
$file_name |
| 76 |
); |
| 77 |
$from_theme_path = sprintf( |
| 78 |
'%s/%s/%s', |
| 79 |
get_template_directory(), |
| 80 |
$folder_name_rewrite, |
| 81 |
$file_name |
| 82 |
); |
| 83 |
|
| 84 |
$path_load = $default_path; |
| 85 |
if ( file_exists( $from_child_theme_path ) ) { |
| 86 |
$path_load = $from_child_theme_path; |
| 87 |
} elseif ( file_exists( $from_theme_path ) ) { |
| 88 |
$path_load = $from_theme_path; |
| 89 |
} |
| 90 |
$template = $this->get_template( $path_load, $args ); |
| 91 |
|
| 92 |
if ( ! $this->include ) { |
| 93 |
return $template; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Include path file |
| 99 |
* |
| 100 |
* @param string $path_file |
| 101 |
* @param array $args |
| 102 |
* |
| 103 |
* @return string|void |
| 104 |
* @since 1.0.0 |
| 105 |
* @version 1.0.0 |
| 106 |
*/ |
| 107 |
public function get_template( string $path_file, array $args = array() ) { |
| 108 |
try { |
| 109 |
extract( $args ); |
| 110 |
|
| 111 |
if ( file_exists( $path_file ) ) { |
| 112 |
if ( $this->include ) { |
| 113 |
include $path_file; |
| 114 |
} else { |
| 115 |
return $path_file; |
| 116 |
} |
| 117 |
} else { |
| 118 |
printf( esc_html__( 'Path file %s not exists', 'realpress' ), $path_file ); |
| 119 |
echo '<br>'; |
| 120 |
} |
| 121 |
} catch ( \Throwable $e ) { |
| 122 |
error_log( $e->getMessage() ); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|