Activation
8 months ago
Admin
3 weeks ago
Block
3 weeks ago
Compatibility
8 months ago
Container
8 months ago
Front
1 year ago
Rest
8 months ago
Shortcode
1 year ago
Traits
4 years ago
Widget
3 weeks ago
Bootstrap.php
5 years ago
Cache.php
8 months ago
Helper.php
3 weeks ago
I18N.php
5 years ago
Image.php
8 months ago
Output.php
4 months ago
Query.php
3 weeks ago
Settings.php
3 weeks ago
Themer.php
8 months ago
Translate.php
3 years ago
Upgrader.php
8 months ago
WordPressPopularPosts.php
8 months ago
deprecated.php
4 years ago
template-tags.php
3 weeks ago
Themer.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class that loads WPP's themes. |
| 4 | * |
| 5 | * @package WordPressPopularPosts |
| 6 | * @author Hector Cabrera <me@cabrerahector.com> |
| 7 | */ |
| 8 | |
| 9 | namespace WordPressPopularPosts; |
| 10 | |
| 11 | class Themer { |
| 12 | |
| 13 | /** |
| 14 | * Path to themes files. |
| 15 | * |
| 16 | * @var string $config |
| 17 | * @access private |
| 18 | */ |
| 19 | private $path; |
| 20 | |
| 21 | /** |
| 22 | * Registered themes. |
| 23 | * |
| 24 | * @var array $config |
| 25 | * @access private |
| 26 | */ |
| 27 | private $themes; |
| 28 | |
| 29 | /** |
| 30 | * Construct function. |
| 31 | * |
| 32 | * @since 5.0.0 |
| 33 | */ |
| 34 | public function __construct() |
| 35 | { |
| 36 | $this->themes = []; |
| 37 | $this->path = plugin_dir_path(dirname(__FILE__)) . 'assets/themes'; |
| 38 | |
| 39 | $this->hooks(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Themer's hooks. |
| 44 | * |
| 45 | * @since 5.0.0 |
| 46 | */ |
| 47 | public function hooks() |
| 48 | { |
| 49 | add_action('after_setup_theme', [$this, 'read']); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Loads information about existing themes. |
| 54 | * |
| 55 | * @since 5.0.0 |
| 56 | */ |
| 57 | public function read() |
| 58 | { |
| 59 | $directories = new \DirectoryIterator($this->path); |
| 60 | |
| 61 | foreach( $directories as $fileinfo ) { |
| 62 | if ( $fileinfo->isDot() || $fileinfo->isFile() ) { |
| 63 | continue; |
| 64 | } |
| 65 | $this->load_theme($fileinfo->getPathName()); |
| 66 | } |
| 67 | |
| 68 | if ( has_filter('wpp_additional_themes') ) { |
| 69 | $additional_themes = apply_filters('wpp_additional_themes', []); |
| 70 | |
| 71 | if ( is_array($additional_themes) && ! empty($additional_themes) ) { |
| 72 | foreach( $additional_themes as $additional_theme ) { |
| 73 | $this->load_theme($additional_theme); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Reads and loads theme into the class. |
| 81 | * |
| 82 | * @since 5.0.0 |
| 83 | * @param string $path Path to theme folder |
| 84 | */ |
| 85 | private function load_theme(string $path) |
| 86 | { |
| 87 | /** @TODO Looks like this entire code block could use a refactor */ |
| 88 | $override_folder = get_stylesheet_directory() . '/wordpress-popular-posts/themes'; |
| 89 | $theme_override = false; |
| 90 | $theme_folder = is_string($path) && is_dir($path) && is_readable($path) ? basename($path) : null; |
| 91 | $theme_folder = $theme_folder ? preg_replace('/[^a-z0-9\_\-]/i', '', $theme_folder) : null; |
| 92 | $theme_path = $theme_folder ? $path : null; |
| 93 | |
| 94 | // Override from WP theme |
| 95 | if ( |
| 96 | $theme_folder |
| 97 | && @file_exists($override_folder . '/' . $theme_folder . '/style.css') |
| 98 | && @file_exists($override_folder . '/' . $theme_folder . '/config.json') |
| 99 | ) { |
| 100 | $theme_override = true; |
| 101 | $theme_path = $override_folder . '/' . $theme_folder; |
| 102 | } |
| 103 | |
| 104 | if ( |
| 105 | $theme_path |
| 106 | && '.' != $theme_folder |
| 107 | && '..' != $theme_folder |
| 108 | && false === strpos($theme_path, '..') |
| 109 | && ! isset($this->themes[$theme_folder]) |
| 110 | && file_exists($theme_path . '/config.json') |
| 111 | && file_exists($theme_path . '/style.css') |
| 112 | ) { |
| 113 | $str = file_get_contents($theme_path . '/config.json'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- We're loading a local file |
| 114 | $json = json_decode($str, true); |
| 115 | |
| 116 | if ( $this->is_valid_config($json) ) { |
| 117 | if ( $theme_override ) { |
| 118 | $json['name'] .= ' (override)'; |
| 119 | } |
| 120 | |
| 121 | $this->themes[$theme_folder] = [ |
| 122 | 'json' => $json, |
| 123 | 'path' => $theme_path |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns an array of available themes. |
| 131 | * |
| 132 | * @since 5.0.0 |
| 133 | * @return array |
| 134 | */ |
| 135 | public function get_themes() |
| 136 | { |
| 137 | return $this->themes; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns data of a specific theme (if found). |
| 142 | * |
| 143 | * @since 5.0.0 |
| 144 | * @param string $theme |
| 145 | * @return array|bool |
| 146 | */ |
| 147 | public function get_theme(string $theme) |
| 148 | { |
| 149 | return isset($this->themes[$theme]) ? $this->themes[$theme] : false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Checks whether a $json array is a valid theme config. |
| 154 | * |
| 155 | * @since 5.0.0 |
| 156 | * @param array |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function is_valid_config(array $json) |
| 160 | { |
| 161 | return is_array($json) && ! empty($json) && isset($json['name']) && isset($json['config']) && is_array($json['config']); |
| 162 | } |
| 163 | } |
| 164 |