| 1 |
<?php |
| 2 |
namespace WPDeveloper\BetterDocs\Editors\Elementor\Traits; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly |
| 6 |
} |
| 7 |
|
| 8 |
trait TemplateQuery { |
| 9 |
public $current_widget_name = ''; |
| 10 |
|
| 11 |
public function set_widget_name( $name = '' ) { |
| 12 |
$this->current_widget_name = $name; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Retrieves Template name from file header. |
| 17 |
* |
| 18 |
* @array |
| 19 |
*/ |
| 20 |
private $template_headers = [ |
| 21 |
'Template Name' => 'Template Name' |
| 22 |
]; |
| 23 |
|
| 24 |
/** |
| 25 |
* Prepare the directory name from the following widget name. |
| 26 |
* |
| 27 |
* @access private |
| 28 |
* |
| 29 |
* |
| 30 |
* @return string $widget_name |
| 31 |
*/ |
| 32 |
private function process_directory_name() { |
| 33 |
if ( empty( $this->current_widget_name ) ) { |
| 34 |
$this->current_widget_name = $this->get_name(); |
| 35 |
} |
| 36 |
|
| 37 |
$widget_name = str_replace( 'betterdocs-', '', $this->current_widget_name ); |
| 38 |
$widget_name = str_replace( '-', ' ', $widget_name ); |
| 39 |
$widget_name = ucwords( $widget_name ); |
| 40 |
$widget_name = str_replace( ' ', '-', $widget_name ); |
| 41 |
|
| 42 |
return $widget_name; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Retrieve `Theme Template Directory` |
| 47 |
* |
| 48 |
* @return string templates directory from the active theme. |
| 49 |
*/ |
| 50 |
private function theme_templates_dir() { |
| 51 |
$current_theme = wp_get_theme(); |
| 52 |
|
| 53 |
$dir = sprintf( |
| 54 |
'%s/%s/Template/%s', |
| 55 |
$current_theme->theme_root, |
| 56 |
$current_theme->stylesheet, |
| 57 |
$this->process_directory_name() |
| 58 |
); |
| 59 |
|
| 60 |
if ( is_dir( $dir ) ) { |
| 61 |
$file = scandir( $dir ); |
| 62 |
$file = array_pop( $file ); |
| 63 |
|
| 64 |
return pathinfo( $file, PATHINFO_EXTENSION ) === 'php' ? $dir : false; |
| 65 |
} |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Retrieves the lite plugin template directory path. |
| 72 |
* |
| 73 |
* @return string templates directory path of lite version. |
| 74 |
*/ |
| 75 |
private function get_template_dir() { |
| 76 |
return \sprintf( |
| 77 |
'%sincludes/elementor/Template/%s', |
| 78 |
BETTERDOCS_ABSPATH, $this->process_directory_name() |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Retrieves the pro plugin template directory path. |
| 84 |
* |
| 85 |
* @return string templates directory path of pro version. |
| 86 |
*/ |
| 87 |
private function get_pro_template_dir() { |
| 88 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 89 |
if ( ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
return \sprintf( |
| 94 |
'%sincludes/elementor/Template/%s', |
| 95 |
BETTERDOCS_PRO_ROOT_DIR_PATH, $this->process_directory_name() |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Collecting templates from different sources. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
private function get_template_files() { |
| 105 |
$templates = []; |
| 106 |
|
| 107 |
if ( is_dir( $this->get_template_dir() ) ) { |
| 108 |
$templates['lite'] = scandir( $this->get_template_dir(), 1 ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( is_dir( $this->get_pro_template_dir() ) ) { |
| 112 |
$templates['pro'] = scandir( $this->get_pro_template_dir(), 1 ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( $this->theme_templates_dir() ) { |
| 116 |
$templates['theme'] = scandir( $this->theme_templates_dir(), 1 ); |
| 117 |
} |
| 118 |
|
| 119 |
return $templates; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves template list from template directory. |
| 124 |
* |
| 125 |
* @return array template list. |
| 126 |
*/ |
| 127 |
protected function get_template_list() { |
| 128 |
$files = []; |
| 129 |
|
| 130 |
if ( $this->get_template_files() ) { |
| 131 |
foreach ( $this->get_template_files() as $key => $handler ) { |
| 132 |
foreach ( $handler as $handle ) { |
| 133 |
if ( strpos( $handle, '.php' ) !== false ) { |
| 134 |
if ( $key === 'lite' ) { |
| 135 |
$path = sprintf( '%s/%s', $this->get_template_dir(), $handle ); |
| 136 |
} else if ( $key === 'pro' ) { |
| 137 |
$path = sprintf( '%s/%s', $this->get_pro_template_dir(), $handle ); |
| 138 |
} else if ( $key === 'theme' ) { |
| 139 |
$path = sprintf( '%s/%s', $this->theme_templates_dir(), $handle ); |
| 140 |
} |
| 141 |
|
| 142 |
$template_info = get_file_data( $path, $this->template_headers ); |
| 143 |
$template_name = $template_info['Template Name']; |
| 144 |
|
| 145 |
if ( $template_name ) { |
| 146 |
$files[$template_name] = $path; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $files; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Retrieves template list from template directory. |
| 158 |
* |
| 159 |
* @return array template list. |
| 160 |
*/ |
| 161 |
public function get_template_list_for_dropdown() { |
| 162 |
$files = []; |
| 163 |
if ( $this->get_template_files() ) { |
| 164 |
foreach ( $this->get_template_files() as $key => $handler ) { |
| 165 |
foreach ( $handler as $handle ) { |
| 166 |
if ( strpos( $handle, '.php' ) !== false ) { |
| 167 |
$path = $this->_get_path( $key, $handle ); |
| 168 |
$template_info = get_file_data( $path, $this->template_headers ); |
| 169 |
$template_name = $template_info['Template Name']; |
| 170 |
$template_key = str_replace( ' ', '-', strtolower( $template_name ) ); |
| 171 |
if ( $template_name ) { |
| 172 |
$files[$template_key] = sprintf( "%s (%s)", ucfirst( $template_name ), ucfirst( $key ) ); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
return $files; |
| 179 |
} |
| 180 |
|
| 181 |
public function _get_path( $key, $handle ) { |
| 182 |
$path = ''; |
| 183 |
if ( $key === 'lite' ) { |
| 184 |
$path = sprintf( '%s/%s', $this->get_template_dir(), $handle ); |
| 185 |
} else if ( $key === 'pro' ) { |
| 186 |
$path = sprintf( '%s/%s', $this->get_pro_template_dir(), $handle ); |
| 187 |
} else if ( $key === 'theme' ) { |
| 188 |
$path = sprintf( '%s/%s', $this->theme_templates_dir(), $handle ); |
| 189 |
} |
| 190 |
return $path; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Preparing template options for frontend select |
| 195 |
* |
| 196 |
* @return array teplate select options. |
| 197 |
*/ |
| 198 |
private function get_template_options() { |
| 199 |
$files = []; |
| 200 |
|
| 201 |
if ( $this->get_template_list() ) { |
| 202 |
foreach ( $this->get_template_list() as $filename => $path ) { |
| 203 |
$filename = \str_replace( ' ', '-', $filename ); |
| 204 |
|
| 205 |
$files[strtolower( $filename )] = $path; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $files; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Adding key value pairs in template options. |
| 214 |
* |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
private function template_options() { |
| 218 |
$keys = array_keys( $this->get_template_options() ); |
| 219 |
$values = array_keys( $this->get_template_list() ); |
| 220 |
|
| 221 |
return array_combine( $keys, $values ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Retrieve template |
| 226 |
* |
| 227 |
* @param string $filename |
| 228 |
* |
| 229 |
* @return string include-able full template path. |
| 230 |
*/ |
| 231 |
public function get_template( $filename ) { |
| 232 |
if ( in_array( $filename, array_keys( $this->get_template_options() ) ) ) { |
| 233 |
return $this->get_template_options()[$filename]; |
| 234 |
} |
| 235 |
|
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Set default option in frontend select control. |
| 241 |
* |
| 242 |
* @return string first option. |
| 243 |
*/ |
| 244 |
public function get_default() { |
| 245 |
$dt = array_reverse( $this->template_options() ); |
| 246 |
|
| 247 |
return strtolower( array_pop( $dt ) ); |
| 248 |
} |
| 249 |
} |
| 250 |
|