| 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_elementor_box_layouts() { |
| 28 |
$layouts = $this->get_layouts( 'category-box' ); |
| 29 |
|
| 30 |
if ( isset( $layouts['layout-4'] ) ) { |
| 31 |
$layouts['layout-4'] = __( 'Sleek Layout', 'betterdocs' ); |
| 32 |
} |
| 33 |
|
| 34 |
if ( isset( $layouts['default'] ) ) { |
| 35 |
$layouts['default'] = __( 'Box Layout', 'betterdocs' ); |
| 36 |
} |
| 37 |
|
| 38 |
if ( isset( $layouts['layout-2'] ) ) { |
| 39 |
$layouts['layout-2'] = __( 'Card Layout', 'betterdocs' ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( isset( $layouts['layout-3'] ) ) { |
| 43 |
$layouts['layout-3'] = __( 'Classic Layout', 'betterdocs' ); |
| 44 |
} |
| 45 |
|
| 46 |
return $layouts; |
| 47 |
} |
| 48 |
|
| 49 |
public function get_layouts( $_local_dir = 'category-grid', $is_pro = false ) { |
| 50 |
if ( empty( $_local_dir ) ) { |
| 51 |
$_local_dir = 'category-grid'; |
| 52 |
} |
| 53 |
|
| 54 |
$dir = $this->layout_directory . $_local_dir; |
| 55 |
|
| 56 |
if ( ! is_dir( $dir ) && ! $is_pro ) { |
| 57 |
throw new Exception( esc_html( $dir ) . ': directory not exists.' ); |
| 58 |
} |
| 59 |
|
| 60 |
return $this->normalize_scandir( $dir ); |
| 61 |
} |
| 62 |
|
| 63 |
public function get_default_layout( $local_dir = 'category-grid', $layouts = [] ) { |
| 64 |
if ( empty( $layouts ) || ! is_array( $layouts ) ) { |
| 65 |
$layouts = $this->get_layouts( $local_dir ); |
| 66 |
} |
| 67 |
|
| 68 |
$layout_keys = array_reverse( array_keys( $layouts ) ); |
| 69 |
return strtolower( array_pop( $layout_keys ) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function normalize_scandir( $dir ) { |
| 73 |
$_layouts_dir = scandir( $dir ); |
| 74 |
if ( ! empty( $_layouts_dir ) ) { |
| 75 |
$_return_value = []; |
| 76 |
foreach ( $_layouts_dir as $file ) { |
| 77 |
if ( $file == '.' || $file == '..' ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
$uniq_name = basename( $file, '.php' ); |
| 82 |
$label = ucwords( str_replace( [ '-', '_' ], ' ', $uniq_name ) ); |
| 83 |
|
| 84 |
$_return_value[ $uniq_name ] = $label; |
| 85 |
} |
| 86 |
|
| 87 |
return $_return_value; |
| 88 |
} |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
public function path( $name, $default = '' ) { |
| 93 |
$this->_view_type = 'free'; |
| 94 |
$name = str_replace( $this->path, '', $name ); |
| 95 |
$_filename = $this->path . $name . '.php'; |
| 96 |
|
| 97 |
if ( ! file_exists( $_filename ) ) { |
| 98 |
$_filename = $this->path . $default . '.php'; |
| 99 |
} |
| 100 |
|
| 101 |
if ( file_exists( $_filename ) ) { |
| 102 |
return $_filename; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public $params = []; |
| 107 |
|
| 108 |
public function get( $name, $params = [] ) { |
| 109 |
$_view_file_path = $this->path( $name ); |
| 110 |
|
| 111 |
if ( ! $_view_file_path ) { |
| 112 |
return __( 'View file path not available.', 'betterdocs' ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( file_exists( $_view_file_path ) ) { |
| 116 |
if ( isset( $params['_view_file_path'] ) ) { |
| 117 |
unset( $params['_view_file_path'] ); |
| 118 |
} |
| 119 |
|
| 120 |
$view_object = $this; |
| 121 |
$helper = Helper::get_instance(); |
| 122 |
$_view_type = $this->_view_type; |
| 123 |
$params = empty( $params ) ? $this->params : array_merge( $this->params, $params ); |
| 124 |
|
| 125 |
extract( $params ); |
| 126 |
$this->params = array_merge( $this->params, $params ); |
| 127 |
include $_view_file_path; |
| 128 |
} else { |
| 129 |
return __( 'View file not exists.', 'betterdocs' ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public function merge( $a, $b ) { |
| 134 |
return Helper::merge( $a, $b ); |
| 135 |
} |
| 136 |
} |
| 137 |
|