| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper functions for theme support. |
| 4 |
* |
| 5 |
* @package WP_Defender\Traits |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Traits; |
| 9 |
|
| 10 |
use WP_Theme; |
| 11 |
|
| 12 |
trait Theme { |
| 13 |
|
| 14 |
/** |
| 15 |
* No use get_theme_root(). |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public function get_path_of_themes_dir(): string { |
| 20 |
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get all installed themes. |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
public function get_themes(): array { |
| 29 |
if ( ! function_exists( 'wp_get_themes' ) ) { |
| 30 |
require_once ABSPATH . '/wp-includes/theme.php'; |
| 31 |
} |
| 32 |
|
| 33 |
return wp_get_themes(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get theme's details. |
| 38 |
* |
| 39 |
* @return WP_Theme |
| 40 |
*/ |
| 41 |
public function get_theme(): WP_Theme { |
| 42 |
if ( ! function_exists( 'wp_get_themes' ) ) { |
| 43 |
require_once ABSPATH . '/wp-includes/theme.php'; |
| 44 |
} |
| 45 |
|
| 46 |
return wp_get_theme(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get all slugs |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function get_theme_slugs(): array { |
| 55 |
$slugs = array(); |
| 56 |
foreach ( $this->get_themes() as $slug => $theme ) { |
| 57 |
if ( is_object( $theme->parent() ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
$slugs[] = $slug; |
| 61 |
} |
| 62 |
|
| 63 |
return $slugs; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Check if the given theme file path is the active theme. |
| 68 |
* |
| 69 |
* @param string $file_path The absolute file path to the theme file. |
| 70 |
* |
| 71 |
* @return bool Returns true if the given theme file path is the active theme, false otherwise. |
| 72 |
*/ |
| 73 |
public function is_active_theme( $file_path ): bool { |
| 74 |
$active_theme = $this->get_theme(); |
| 75 |
$theme_dir = $this->get_path_of_themes_dir(); |
| 76 |
$abs_path = $theme_dir; |
| 77 |
$abs_path = defender_replace_line( $abs_path ); |
| 78 |
// Without the first slash. |
| 79 |
$rev_file = str_replace( $abs_path, '', $file_path ); |
| 80 |
$theme_data = explode( '/', $rev_file ); |
| 81 |
$theme_slug = $theme_data[0]; |
| 82 |
// Checking if the given theme is a child theme. |
| 83 |
if ( false !== stripos( $theme_slug, '-child' ) ) { |
| 84 |
$theme_slug = str_replace( '-child', '', $theme_slug ); |
| 85 |
} |
| 86 |
// Checking if the active theme is a parent theme. |
| 87 |
if ( is_object( $active_theme->parent() ) ) { |
| 88 |
$active_theme_slug = $active_theme->parent()->get_stylesheet(); |
| 89 |
} else { |
| 90 |
$active_theme_slug = $active_theme->get_stylesheet(); |
| 91 |
} |
| 92 |
|
| 93 |
return $theme_slug === $active_theme_slug; |
| 94 |
} |
| 95 |
} |
| 96 |
|