elementor
1 year ago
logs
1 month ago
strong-testimonials-beaver-block
1 year ago
submodules
8 months ago
class-strong-gutemberg.php
1 year ago
class-strong-log.php
1 year ago
class-strong-mail.php
1 year ago
class-strong-testimonials-average-shortcode.php
1 year ago
class-strong-testimonials-count-shortcode.php
1 year ago
class-strong-testimonials-defaults.php
1 month ago
class-strong-testimonials-form.php
1 month ago
class-strong-testimonials-order.php
1 year ago
class-strong-testimonials-privacy.php
1 year ago
class-strong-testimonials-render.php
1 month ago
class-strong-testimonials-templates.php
1 year ago
class-strong-testimonials-view-shortcode.php
2 weeks ago
class-strong-testimonials-view-widget.php
1 year ago
class-strong-view-display.php
1 week ago
class-strong-view-form.php
1 week ago
class-strong-view-slideshow.php
1 week ago
class-strong-view.php
1 week ago
class-walker-strong-category-checklist-front.php
1 year ago
deprecated.php
1 year ago
filters.php
1 month ago
functions-activation.php
1 month ago
functions-content.php
11 months ago
functions-image.php
6 months ago
functions-rating.php
1 year ago
functions-template-form.php
2 weeks ago
functions-template.php
4 months ago
functions-views.php
1 year ago
functions.php
1 month ago
l10n-polylang.php
1 year ago
l10n-wpml.php
1 year ago
post-types.php
2 weeks ago
retro.php
1 year ago
scripts.php
1 month ago
class-strong-testimonials-templates.php
543 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Templates class. |
| 4 | * |
| 5 | * @since 1.25 |
| 6 | */ |
| 7 | |
| 8 | // Exit if accessed directly |
| 9 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 | |
| 11 | if ( ! class_exists( 'Strong_Templates' ) ) : |
| 12 | |
| 13 | class Strong_Templates { |
| 14 | |
| 15 | /** |
| 16 | * @var array |
| 17 | */ |
| 18 | public $templates; |
| 19 | |
| 20 | public function __construct() { |
| 21 | $this->templates = $this->find_templates(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param null $type |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function find_templates( $type = null ) { |
| 30 | $search = array( |
| 31 | 'child_theme' => array( |
| 32 | 'source' => esc_html__( 'Child Theme', 'strong-testimonials' ), |
| 33 | 'path' => get_stylesheet_directory() . '/' . WPMTST, |
| 34 | 'uri' => get_stylesheet_directory_uri() . '/' . WPMTST, |
| 35 | 'order' => 2, |
| 36 | ), |
| 37 | 'parent_theme' => array( |
| 38 | 'source' => esc_html__( 'Parent Theme', 'strong-testimonials' ), |
| 39 | 'path' => get_template_directory() . '/' . WPMTST, |
| 40 | 'uri' => get_template_directory_uri() . '/' . WPMTST, |
| 41 | 'order' => 3, |
| 42 | ), |
| 43 | 'plugin' => array( |
| 44 | 'source' => esc_html__( 'Plugin', 'strong-testimonials' ), |
| 45 | 'path' => WPMTST_TPL, |
| 46 | 'uri' => WPMTST_TPL_URI, |
| 47 | 'order' => 4, |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | /** |
| 52 | * Filter the search paths. |
| 53 | */ |
| 54 | $search = apply_filters( 'wpmtst_template_search_paths', $search ); |
| 55 | |
| 56 | /** |
| 57 | * Insert order if necessary so custom templates appear first. |
| 58 | * |
| 59 | * @since 2.22 |
| 60 | */ |
| 61 | foreach ( $search as $key => $where ) { |
| 62 | if ( ! isset( $where['source'] ) ) { |
| 63 | $search[ $key ]['source'] = esc_html__( 'Custom', 'strong-testimonials' ); |
| 64 | } |
| 65 | if ( ! isset( $where['order'] ) ) { |
| 66 | $search[ $key ]['order'] = 1; |
| 67 | } |
| 68 | } |
| 69 | // Reverse the search order so templates can be overridden. |
| 70 | uasort( $search, array( $this, 'sort_array_by_search_order' ) ); |
| 71 | |
| 72 | /** |
| 73 | * Search |
| 74 | */ |
| 75 | $files = array(); |
| 76 | foreach ( $search as $key => $bases ) { |
| 77 | $new_files = $this->scandir_top( $key, $bases['path'], $bases['uri'], $type ); |
| 78 | // Merge (override) templates. |
| 79 | if ( is_array( $new_files ) && $new_files ) { |
| 80 | $files = array_merge( $files, $new_files ); |
| 81 | } |
| 82 | } |
| 83 | uasort( $files, array( $this, 'sort_array_by_order' ) ); |
| 84 | |
| 85 | /** |
| 86 | * Filter the list of found templates. |
| 87 | */ |
| 88 | $files = array_filter( apply_filters( 'wpmtst_templates_found', array_filter( $files ) ) ); |
| 89 | |
| 90 | return $files; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param string $name |
| 95 | * |
| 96 | * @return array|bool |
| 97 | */ |
| 98 | public function get_template_by_name( $name = '' ) { |
| 99 | if ( isset( $this->templates[ $name ] ) ) { |
| 100 | return $this->templates[ $name ]; |
| 101 | } |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param null $types |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | public function get_templates( $types = null ) { |
| 112 | return $this->get_templates_by_type( $types ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param null $types |
| 117 | * |
| 118 | * @return array |
| 119 | */ |
| 120 | public function get_templates_by_type( $types = null ) { |
| 121 | if ( ! $types ) { |
| 122 | return $this->templates; |
| 123 | } |
| 124 | |
| 125 | $types = (array) $types; |
| 126 | $filtered = array(); |
| 127 | |
| 128 | foreach ( $this->templates as $key => $template ) { |
| 129 | if ( isset( $template['config']['type'] ) ) { |
| 130 | if ( in_array( $template['config']['type'], $types ) ) { |
| 131 | $filtered[ $key ] = $template; |
| 132 | } |
| 133 | } |
| 134 | else { |
| 135 | $key_parts = explode( ':', $key ); |
| 136 | $type = $key_parts[1]; |
| 137 | if ( 'content' == $type ) { |
| 138 | $type = 'display'; |
| 139 | } |
| 140 | if ( in_array( $type, $types ) ) { |
| 141 | $filtered[ $key ] = $template; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return array_filter( $filtered ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Return list of templates by key. |
| 151 | * |
| 152 | * @return array |
| 153 | */ |
| 154 | public function get_template_keys() { |
| 155 | return array_keys( $this->templates ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get template attribute. |
| 160 | * |
| 161 | * @param $atts |
| 162 | * @param string $part |
| 163 | * @param bool|true $use_default |
| 164 | * |
| 165 | * @return string |
| 166 | */ |
| 167 | public function get_template_attr( $atts, $part = 'template', $use_default = true ) { |
| 168 | // Build a list of potential template part names. |
| 169 | $template_search = array(); |
| 170 | |
| 171 | // [1] |
| 172 | /* |
| 173 | * Divi Builder compatibility. Everybody has to be special. |
| 174 | * @since 2.22.0 |
| 175 | * TODO Abstract this. |
| 176 | */ |
| 177 | if ( 'stylesheet' == $part ) { |
| 178 | if ( isset( $atts['divi_builder'] ) && $atts['divi_builder'] && wpmtst_divi_builder_active() ) { |
| 179 | $template_search[] = $atts['template'] .= '-divi'; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // [2] |
| 184 | if ( isset( $atts['template'] ) ) { |
| 185 | $template_search[] = $atts['template']; |
| 186 | } |
| 187 | |
| 188 | // [3] |
| 189 | if ( $use_default ) { |
| 190 | $template_search[] = apply_filters( 'wpmtst_default_template', 'default', $atts ); |
| 191 | } |
| 192 | |
| 193 | // Search list of already found template files. Stop at first match. |
| 194 | $template_info = false; |
| 195 | foreach ( $template_search as $template_key ) { |
| 196 | if ( isset( $this->templates[ $template_key ] ) ) { |
| 197 | $template_info = $this->templates[ $template_key ]; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Return the requested part |
| 203 | if ( $template_info && isset( $template_info[ $part ] ) && $template_info[ $part ] ) { |
| 204 | return $template_info[ $part ]; |
| 205 | } |
| 206 | |
| 207 | return ''; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get template config. |
| 212 | * |
| 213 | * @param $atts |
| 214 | * @param string $part |
| 215 | * @param bool|true $use_default |
| 216 | * |
| 217 | * @return string |
| 218 | */ |
| 219 | public function get_template_config( $atts, $part = 'name', $use_default = true ) { |
| 220 | // Build a list of potential template part names. |
| 221 | $template_search = array(); |
| 222 | |
| 223 | // [1] |
| 224 | /* |
| 225 | * Divi Builder compatibility. Everybody has to be special. |
| 226 | * @since 2.22.0 |
| 227 | * TODO Abstract this. |
| 228 | */ |
| 229 | if ( 'stylesheet' == $part ) { |
| 230 | if ( isset( $atts['divi_builder'] ) && $atts['divi_builder'] && wpmtst_divi_builder_active() ) { |
| 231 | $template_search[] = $atts['template'] .= '-divi'; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // [2] |
| 236 | if ( isset( $atts['template'] ) ) { |
| 237 | $template_search[] = $atts['template']; |
| 238 | } |
| 239 | |
| 240 | // [3] |
| 241 | if ( $use_default ) { |
| 242 | $template_search[] = apply_filters( 'wpmtst_default_template', 'default', $atts ); |
| 243 | } |
| 244 | |
| 245 | // Search list of already found template files. Stop at first match. |
| 246 | $template_info = false; |
| 247 | foreach ( $template_search as $template_key ) { |
| 248 | if ( isset( $this->templates[ $template_key ] ) ) { |
| 249 | $template_info = $this->templates[ $template_key ]; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Return the requested part (name, template, stylesheet,etc.) |
| 255 | if ( $template_info && isset( $template_info['config'][ $part ] ) && $template_info['config'][ $part ] ) { |
| 256 | return $template_info['config'][ $part ]; |
| 257 | } |
| 258 | |
| 259 | return ''; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param $source_key |
| 264 | * @param $path |
| 265 | * @param $uri |
| 266 | * @param $type |
| 267 | * |
| 268 | * @return array|bool |
| 269 | */ |
| 270 | public function scandir_top( $source_key, $path, $uri, $type ) { |
| 271 | if ( ! is_dir( $path ) ) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | $files = array(); |
| 276 | $templates = scandir( $path ); |
| 277 | foreach ( $templates as $template ) { |
| 278 | if ( '.' == $template[0] ) { |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | if ( ! is_dir( $path . '/' . $template ) ) { |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | // Find files in this directory |
| 287 | $files_found = $this->scandir( $template, $path, $uri, array( 'json', 'php', 'css', 'js' ), $type ); |
| 288 | |
| 289 | if ( ! $files_found ) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | foreach ( $files_found as $key => $template_files ) { |
| 294 | $template_files['source'] = $source_key; |
| 295 | if ( isset( $template_files['config']['format_version'] ) && '1.0' == $template_files['config']['format_version'] ) { |
| 296 | // Template format version 1 (no config file) |
| 297 | $template_name = basename( $template_files['template'], '.php' ); |
| 298 | $new_key = $template . ':' . $template_name; |
| 299 | $files[ $new_key ] = $template_files; |
| 300 | } |
| 301 | else { |
| 302 | // Template format version 2 (has config.json) |
| 303 | $files[ $template ] = $template_files; |
| 304 | } |
| 305 | |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return $files; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @param $template |
| 314 | * @param $path |
| 315 | * @param $uri |
| 316 | * @param null $extensions |
| 317 | * @param $type |
| 318 | * |
| 319 | * @return array|bool |
| 320 | */ |
| 321 | public function scandir( $template, $path, $uri, $extensions = null, $type = null ) { |
| 322 | if ( ! is_dir( $path . '/' . $template ) ) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | if ( $extensions ) { |
| 327 | $extensions = (array) $extensions; |
| 328 | } |
| 329 | |
| 330 | // Bail if requested template type not found |
| 331 | if ( $type && ! file_exists( $path . '/' . $template . '/' . $type . '.php' ) ) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | $files = array(); |
| 336 | |
| 337 | /** |
| 338 | * Template header tags for template format version 1 |
| 339 | */ |
| 340 | $tags = apply_filters( 'wpmtst_template_header_tags', array( |
| 341 | 'name' => 'Template Name', |
| 342 | 'description' => 'Description', |
| 343 | 'deps' => 'Scripts', // registered scripts |
| 344 | 'styles' => 'Styles', // registered styles or fonts |
| 345 | 'force' => 'Force', // dependent options |
| 346 | ) ); |
| 347 | |
| 348 | /** |
| 349 | * Check for config file first. |
| 350 | * |
| 351 | * @since 2.30.0 |
| 352 | */ |
| 353 | $config_found = false; |
| 354 | $config = $path . '/' . $template . '/config.json'; |
| 355 | if ( file_exists( $config ) ) { |
| 356 | $files[ $template ]['config'] = (array) json_decode( file_get_contents( $config ) ); |
| 357 | $config_found = true; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Process the files. |
| 363 | * This creates an array of properties: file paths and config parameters. |
| 364 | */ |
| 365 | $results = scandir( $path . '/' . $template ); |
| 366 | foreach ( $results as $result ) { |
| 367 | if ( '.' == $result[0] ) { |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if ( is_dir( $path . '/' . $template . '/' . $result ) ) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | // If no extensions specified or if extension matches |
| 376 | if ( ! $extensions || preg_match( '~\.(' . implode( '|', $extensions ) . ')$~', $result ) ) { |
| 377 | |
| 378 | $default_config = array( |
| 379 | 'name' => '', |
| 380 | 'description' => '', |
| 381 | 'type' => 'display', |
| 382 | 'order' => 10, |
| 383 | 'scripts' => '', |
| 384 | 'styles' => '', |
| 385 | 'force' => '', |
| 386 | 'options' => '', |
| 387 | 'format_version' => '2.0', |
| 388 | ); |
| 389 | |
| 390 | $filename = pathinfo( $result, PATHINFO_FILENAME ); |
| 391 | $ext = pathinfo( $result, PATHINFO_EXTENSION ); |
| 392 | |
| 393 | // Template, stylesheet, script, or other? |
| 394 | switch ( $ext ) { |
| 395 | //case 'json': |
| 396 | // $key = 'config'; |
| 397 | // $base = $uri; |
| 398 | // break; |
| 399 | case 'php': |
| 400 | $key = 'template'; |
| 401 | $base = $path; |
| 402 | break; |
| 403 | case 'css': |
| 404 | $key = 'stylesheet'; |
| 405 | $base = $uri; |
| 406 | break; |
| 407 | case 'js': |
| 408 | $key = 'script'; |
| 409 | $base = $uri; |
| 410 | break; |
| 411 | default: |
| 412 | $key = ''; |
| 413 | $base = ''; |
| 414 | } |
| 415 | |
| 416 | if ( $key ) { |
| 417 | $files[ $template ][ $key ] = $base . '/' . $template . '/' . $result; |
| 418 | } |
| 419 | |
| 420 | // Convert V1 templates to V2 by creating config array from main template file. |
| 421 | if ( 'template' == $key && ! $config_found ) { |
| 422 | $file_data = get_file_data( $path . '/' . $template . '/' . $result, $tags ); |
| 423 | |
| 424 | // Start config array |
| 425 | $config = array( |
| 426 | 'format_version' => '1.0', |
| 427 | ); |
| 428 | |
| 429 | // Store header tags |
| 430 | foreach ( $tags as $tag => $label ) { |
| 431 | |
| 432 | if ( 'name' == $tag ) { |
| 433 | // Get the template name |
| 434 | if ( isset( $file_data['name'] ) && $file_data['name'] ) { |
| 435 | $config['name'] = $file_data['name']; |
| 436 | } else { |
| 437 | // Use the directory name |
| 438 | $config['name'] = ucwords( str_replace( array( '_', '-' ), ' ', basename( $path ) ) ); |
| 439 | } |
| 440 | } |
| 441 | else { |
| 442 | $config[ $tag ] = $file_data[ $tag ]; |
| 443 | } |
| 444 | |
| 445 | } |
| 446 | |
| 447 | // Set the template type |
| 448 | if ( 'form.php' == $filename ) { |
| 449 | $config['type'] = 'form'; |
| 450 | } |
| 451 | elseif ( 'widget.php' == $filename ) { |
| 452 | $config['type'] = 'widget'; |
| 453 | } |
| 454 | |
| 455 | $files[ $template ]['config'] = array_merge( $default_config, $config ); |
| 456 | } |
| 457 | |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return $files; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * @param $a |
| 466 | * @param $b |
| 467 | * |
| 468 | * @return int |
| 469 | */ |
| 470 | public function sort_array_by_name( $a, $b ) { |
| 471 | if ( ! isset( $a['name'] ) ) { |
| 472 | $a['name'] = ''; |
| 473 | } |
| 474 | if ( ! isset( $b['name'] ) ) { |
| 475 | $b['name'] = ''; |
| 476 | } |
| 477 | |
| 478 | return strcmp( $a['name'], $b['name'] ); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @param $a |
| 483 | * @param $b |
| 484 | * |
| 485 | * @return int |
| 486 | */ |
| 487 | public function sort_array_by_order( $a, $b ) { |
| 488 | if ( ! isset( $a['config']['order'] ) ) { |
| 489 | $a['config']['order'] = 0; |
| 490 | } |
| 491 | if ( ! isset( $b['config']['order'] ) ) { |
| 492 | $b['config']['order'] = 0; |
| 493 | } |
| 494 | |
| 495 | if ( $a['config']['order'] == $b['config']['order'] ) { |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | // `<` returns descending order |
| 500 | return ( $a['config']['order'] < $b['config']['order'] ) ? -1 : 1; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * @param $a |
| 505 | * @param $b |
| 506 | * |
| 507 | * @return int |
| 508 | */ |
| 509 | public function sort_array_by_search_order( $a, $b ) { |
| 510 | if ( ! isset( $a['order'] ) || ! isset( $b['order'] ) ) { |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | if ( $a['order'] == $b['order'] ) { |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | // `>` returns ascending order |
| 519 | return ( $a['order'] > $b['order'] ) ? -1 : 1; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * @param $a |
| 524 | * @param $b |
| 525 | * |
| 526 | * @return int |
| 527 | */ |
| 528 | public function sort_array_by_order_name( $a, $b ) { |
| 529 | if ( ! isset( $a['name'] ) || ! isset( $b['name'] ) ) { |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | if ( $a['order'] == $b['order'] ) { |
| 534 | return strcasecmp( $a['name'], $b['name'] ); |
| 535 | } |
| 536 | |
| 537 | return ( $a['order'] < $b['order'] ) ? -1 : 1; |
| 538 | } |
| 539 | |
| 540 | } |
| 541 | |
| 542 | endif; |
| 543 |