| @@ -1,28 +1,58 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Helper functions for theme support. | |
| 4 | + * | |
| 5 | + * @package WP_Defender\Traits | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace WP_Defender\Traits; |
| 4 | 9 | |
| 10 | +use WP_Theme; | |
| 11 | + | |
| 5 | 12 | trait Theme { |
| 6 | 13 | |
| 7 | 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 | + /** | |
| 8 | 24 | * Get all installed themes. |
| 9 | 25 | * |
| 10 | 26 | * @return array |
| 11 | 27 | */ |
| 12 | - public function get_themes() { | |
| 28 | + public function get_themes(): array { | |
| 13 | 29 | if ( ! function_exists( 'wp_get_themes' ) ) { |
| 14 | - require_once( ABSPATH . '/wp-includes/theme.php' ); | |
| 30 | + require_once ABSPATH . '/wp-includes/theme.php'; | |
| 15 | 31 | } |
| 32 | + | |
| 16 | 33 | return wp_get_themes(); |
| 17 | 34 | } |
| 18 | 35 | |
| 19 | 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 | + /** | |
| 20 | 50 | * Get all slugs |
| 21 | 51 | * |
| 22 | 52 | * @return array |
| 23 | 53 | */ |
| 24 | - public function get_theme_slugs() { | |
| 54 | + public function get_theme_slugs(): array { | |
| 25 | 55 | $slugs = array(); |
| 26 | 56 | foreach ( $this->get_themes() as $slug => $theme ) { |
| 27 | 57 | if ( is_object( $theme->parent() ) ) { |
| 28 | 58 | continue; |
| @@ -30,6 +60,36 @@ | ||
| 30 | 60 | $slugs[] = $slug; |
| 31 | 61 | } |
| 32 | 62 | |
| 33 | 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; | |
| 34 | 94 | } |
| 35 | 95 | } |