| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace WPDeveloper\BetterDocs\Utils; |
| 4 | 4 | |
| 5 | 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | - exit; // Exit if accessed directly | |
| 6 | + exit; // Exit if accessed directly | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | use Exception; |
| 10 | 10 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| @@ -9,102 +9,140 @@ | ||
| 9 | 9 | use Exception; |
| 10 | 10 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 11 | 11 | |
| 12 | 12 | class Views extends Base { |
| 13 | - public $path = ''; | |
| 14 | - public $_view_type = 'free'; | |
| 15 | - protected $container = ''; | |
| 13 | + public $path = ''; | |
| 14 | + public $_view_type = 'free'; | |
| 15 | + protected $container = ''; | |
| 16 | 16 | |
| 17 | - protected $layout_directory; | |
| 17 | + protected $layout_directory; | |
| 18 | 18 | |
| 19 | - private $nested = 0; | |
| 19 | + private $nested = 0; | |
| 20 | 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 | - } | |
| 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 | 26 | |
| 27 | - public function get_layouts( $_local_dir = 'category-grid', $is_pro = false ) { | |
| 28 | - if ( empty( $_local_dir ) ) { | |
| 29 | - $_local_dir = 'category-grid'; | |
| 30 | - } | |
| 27 | + public function get_elementor_box_layouts() { | |
| 28 | + $layouts = $this->get_layouts( 'category-box' ); | |
| 31 | 29 | |
| 32 | - $dir = $this->layout_directory . $_local_dir; | |
| 30 | + if ( isset( $layouts['layout-4'] ) ) { | |
| 31 | + $layouts['layout-4'] = __( 'Sleek Layout', 'betterdocs' ); | |
| 32 | + } | |
| 33 | 33 | |
| 34 | - if ( ! is_dir( $dir ) && ! $is_pro ) { | |
| 35 | - throw new Exception( $dir . ': directory not exists.' ); | |
| 36 | - } | |
| 34 | + if ( isset( $layouts['default'] ) ) { | |
| 35 | + $layouts['default'] = __( 'Box Layout', 'betterdocs' ); | |
| 36 | + } | |
| 37 | 37 | |
| 38 | - return $this->normalize_scandir( $dir ); | |
| 39 | - } | |
| 38 | + if ( isset( $layouts['layout-2'] ) ) { | |
| 39 | + $layouts['layout-2'] = __( 'Card Layout', 'betterdocs' ); | |
| 40 | + } | |
| 40 | 41 | |
| 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 | - } | |
| 42 | + if ( isset( $layouts['layout-3'] ) ) { | |
| 43 | + $layouts['layout-3'] = __( 'Classic Layout', 'betterdocs' ); | |
| 44 | + } | |
| 45 | 45 | |
| 46 | - $layout_keys = array_reverse( array_keys( $layouts ) ); | |
| 47 | - return strtolower( array_pop( $layout_keys ) ); | |
| 48 | - } | |
| 46 | + return $layouts; | |
| 47 | + } | |
| 49 | 48 | |
| 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 | - } | |
| 49 | + public function get_layouts( $_local_dir = 'category-grid', $is_pro = false ) { | |
| 50 | + if ( empty( $_local_dir ) ) { | |
| 51 | + $_local_dir = 'category-grid'; | |
| 52 | + } | |
| 58 | 53 | |
| 59 | - $uniq_name = basename( $file, '.php' ); | |
| 60 | - $label = ucwords( str_replace( ['-', '_'], ' ', $uniq_name ) ); | |
| 54 | + $dir = $this->layout_directory . $_local_dir; | |
| 61 | 55 | |
| 62 | - $_return_value[$uniq_name] = $label; | |
| 63 | - } | |
| 56 | + if ( ! is_dir( $dir ) && ! $is_pro ) { | |
| 57 | + throw new Exception( esc_html( $dir ) . ': directory not exists.' ); | |
| 58 | + } | |
| 64 | 59 | |
| 65 | - return $_return_value; | |
| 66 | - } | |
| 67 | - return []; | |
| 68 | - } | |
| 60 | + return $this->normalize_scandir( $dir ); | |
| 61 | + } | |
| 69 | 62 | |
| 70 | - public function path( $name, $default = '' ) { | |
| 71 | - $this->_view_type = 'free'; | |
| 72 | - $name = str_replace( $this->path, '', $name ); | |
| 73 | - $_filename = $this->path . $name . '.php'; | |
| 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 | + } | |
| 74 | 67 | |
| 75 | - if ( ! file_exists( $_filename ) ) { | |
| 76 | - $_filename = $this->path . $default . '.php'; | |
| 77 | - } | |
| 68 | + $layout_keys = array_reverse( array_keys( $layouts ) ); | |
| 69 | + return strtolower( array_pop( $layout_keys ) ); | |
| 70 | + } | |
| 78 | 71 | |
| 79 | - if ( file_exists( $_filename ) ) { | |
| 80 | - return $_filename; | |
| 81 | - } | |
| 82 | - } | |
| 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 | + } | |
| 83 | 80 | |
| 84 | - public $params = []; | |
| 81 | + $uniq_name = basename( $file, '.php' ); | |
| 82 | + $label = ucwords( str_replace( [ '-', '_' ], ' ', $uniq_name ) ); | |
| 85 | 83 | |
| 86 | - public function get( $name, $params = [] ) { | |
| 87 | - $_view_file_path = $this->path( $name ); | |
| 84 | + $_return_value[ $uniq_name ] = $label; | |
| 85 | + } | |
| 88 | 86 | |
| 89 | - if ( file_exists( $_view_file_path ) ) { | |
| 90 | - if ( isset( $params['_view_file_path'] ) ) { | |
| 91 | - unset( $params['_view_file_path'] ); | |
| 92 | - } | |
| 87 | + return $_return_value; | |
| 88 | + } | |
| 89 | + return []; | |
| 90 | + } | |
| 93 | 91 | |
| 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 ); | |
| 92 | + public function path( $name, $default = '' ) { | |
| 93 | + $this->_view_type = 'free'; | |
| 94 | + $name = str_replace( $this->path, '', $name ); | |
| 98 | 95 | |
| 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 | - } | |
| 96 | + // Sanitize: strip any directory traversal sequences to prevent LFI. | |
| 97 | + $name = str_replace( array( '../', '..\\' ), '', $name ); | |
| 106 | 98 | |
| 107 | - public function merge( $a, $b ) { | |
| 108 | - return Helper::merge( $a, $b ); | |
| 109 | - } | |
| 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 | + } | |
| 110 | 148 | } |