PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Utils / Views.php

Views.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.2, at includes/Utils/Views.php

149 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
96 // Sanitize: strip any directory traversal sequences to prevent LFI.
97 $name = str_replace( array( '../', '..\\' ), '', $name );
98
99 $_filename = $this->path . $name . '.php';
100
101 if ( ! file_exists( $_filename ) ) {
102 $_filename = $this->path . $default . '.php';
103 }
104
105 if ( file_exists( $_filename ) ) {
106 // Verify the resolved path stays within the plugin's base directory.
107 $_real_path = realpath( $_filename );
108 $_base_path = realpath( $this->path );
109
110 if ( $_real_path === false || $_base_path === false || strpos( $_real_path, $_base_path ) !== 0 ) {
111 return null;
112 }
113
114 return $_filename;
115 }
116 }
117
118 public $params = [];
119
120 public function get( $name, $params = [] ) {
121 $_view_file_path = $this->path( $name );
122
123 if ( ! $_view_file_path ) {
124 return __( 'View file path not available.', 'betterdocs' );
125 }
126
127 if ( file_exists( $_view_file_path ) ) {
128 if ( isset( $params['_view_file_path'] ) ) {
129 unset( $params['_view_file_path'] );
130 }
131
132 $view_object = $this;
133 $helper = Helper::get_instance();
134 $_view_type = $this->_view_type;
135 $params = empty( $params ) ? $this->params : array_merge( $this->params, $params );
136
137 extract( $params );
138 $this->params = array_merge( $this->params, $params );
139 include $_view_file_path;
140 } else {
141 return __( 'View file not exists.', 'betterdocs' );
142 }
143 }
144
145 public function merge( $a, $b ) {
146 return Helper::merge( $a, $b );
147 }
148 }
149