| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 11 |
|
| 12 |
class Views extends Base { |
| 13 |
public $path = ''; |
| 14 |
public $_view_type = 'free'; |
| 15 |
protected $container = ''; |
| 16 |
|
| 17 |
protected $layout_directory; |
| 18 |
|
| 19 |
private $nested = 0; |
| 20 |
|
| 21 |
public function __construct( $path, Container $container, $layout_directory = 'layouts/' ) { |
| 22 |
$this->path = $path; |
| 23 |
$this->container = $container; |
| 24 |
$this->layout_directory = $this->path . $layout_directory; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_layouts( $_local_dir = 'category-grid', $is_pro = false ) { |
| 28 |
if ( empty( $_local_dir ) ) { |
| 29 |
$_local_dir = 'category-grid'; |
| 30 |
} |
| 31 |
|
| 32 |
$dir = $this->layout_directory . $_local_dir; |
| 33 |
|
| 34 |
if ( ! is_dir( $dir ) && ! $is_pro ) { |
| 35 |
throw new Exception( $dir . ': directory not exists.' ); |
| 36 |
} |
| 37 |
|
| 38 |
return $this->normalize_scandir( $dir ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_default_layout( $local_dir = 'category-grid', $layouts = [] ) { |
| 42 |
if ( empty( $layouts ) || ! is_array( $layouts ) ) { |
| 43 |
$layouts = $this->get_layouts( $local_dir ); |
| 44 |
} |
| 45 |
|
| 46 |
$layout_keys = array_reverse( array_keys( $layouts ) ); |
| 47 |
return strtolower( array_pop( $layout_keys ) ); |
| 48 |
} |
| 49 |
|
| 50 |
public function normalize_scandir( $dir ) { |
| 51 |
$_layouts_dir = scandir( $dir ); |
| 52 |
if ( ! empty( $_layouts_dir ) ) { |
| 53 |
$_return_value = []; |
| 54 |
foreach ( $_layouts_dir as $file ) { |
| 55 |
if ( $file == '.' || $file == '..' ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$uniq_name = basename( $file, '.php' ); |
| 60 |
$label = ucwords( str_replace( ['-', '_'], ' ', $uniq_name ) ); |
| 61 |
|
| 62 |
$_return_value[$uniq_name] = $label; |
| 63 |
} |
| 64 |
|
| 65 |
return $_return_value; |
| 66 |
} |
| 67 |
return []; |
| 68 |
} |
| 69 |
|
| 70 |
public function path( $name, $default = '' ) { |
| 71 |
$this->_view_type = 'free'; |
| 72 |
$name = str_replace( $this->path, '', $name ); |
| 73 |
$_filename = $this->path . $name . '.php'; |
| 74 |
|
| 75 |
if ( ! file_exists( $_filename ) ) { |
| 76 |
$_filename = $this->path . $default . '.php'; |
| 77 |
} |
| 78 |
|
| 79 |
if ( file_exists( $_filename ) ) { |
| 80 |
return $_filename; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public $params = []; |
| 85 |
|
| 86 |
public function get( $name, $params = [] ) { |
| 87 |
$_view_file_path = $this->path( $name ); |
| 88 |
|
| 89 |
if ( file_exists( $_view_file_path ) ) { |
| 90 |
if ( isset( $params['_view_file_path'] ) ) { |
| 91 |
unset( $params['_view_file_path'] ); |
| 92 |
} |
| 93 |
|
| 94 |
$view_object = $this; |
| 95 |
$helper = Helper::get_instance(); |
| 96 |
$_view_type = $this->_view_type; |
| 97 |
$params = empty( $params ) ? $this->params : array_merge( $this->params, $params ); |
| 98 |
|
| 99 |
extract( $params ); |
| 100 |
$this->params = array_merge( $this->params, $params ); |
| 101 |
include $_view_file_path; |
| 102 |
} else { |
| 103 |
return __( 'View file not exists.', 'betterdocs' ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function merge( $a, $b ) { |
| 108 |
return Helper::merge( $a, $b ); |
| 109 |
} |
| 110 |
} |
| 111 |
|