Activation
1 year ago
Admin
1 year ago
Block
1 year ago
Compatibility
1 year ago
Container
1 year ago
Front
1 year ago
Rest
1 year ago
Shortcode
1 year ago
Traits
1 year ago
Widget
1 year ago
Bootstrap.php
1 year ago
Cache.php
1 year ago
Helper.php
1 year ago
I18N.php
1 year ago
Image.php
1 year ago
Output.php
1 year ago
Query.php
1 year ago
Settings.php
1 year ago
Themer.php
1 year ago
Translate.php
1 year ago
WordPressPopularPosts.php
1 year ago
deprecated.php
1 year ago
template-tags.php
1 year ago
Themer.php
147 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 | $theme_folder = is_string($path) && is_dir($path) && is_readable($path) ? basename($path) : null; |
| 88 | $theme_folder = $theme_folder ? preg_replace('/[^a-z0-9\_\-\.]/i', '', $theme_folder) : null; |
| 89 | $theme_path = $theme_folder ? $path : null; |
| 90 | |
| 91 | if ( |
| 92 | $theme_path |
| 93 | && '.' != $theme_folder |
| 94 | && '..' != $theme_folder |
| 95 | && false === strpos($theme_path, '..') |
| 96 | && ! isset($this->themes[$theme_folder]) |
| 97 | && file_exists($theme_path . '/config.json') |
| 98 | && file_exists($theme_path . '/style.css') |
| 99 | ) { |
| 100 | $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 |
| 101 | $json = json_decode($str, true); |
| 102 | |
| 103 | if ( $this->is_valid_config($json) ) { |
| 104 | $this->themes[$theme_folder] = [ |
| 105 | 'json' => $json, |
| 106 | 'path' => $theme_path |
| 107 | ]; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns an array of available themes. |
| 114 | * |
| 115 | * @since 5.0.0 |
| 116 | * @return array |
| 117 | */ |
| 118 | public function get_themes() |
| 119 | { |
| 120 | return $this->themes; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns data of a specific theme (if found). |
| 125 | * |
| 126 | * @since 5.0.0 |
| 127 | * @param string $theme |
| 128 | * @return array|bool |
| 129 | */ |
| 130 | public function get_theme(string $theme) |
| 131 | { |
| 132 | return isset($this->themes[$theme]) ? $this->themes[$theme] : false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Checks whether a $json array is a valid theme config. |
| 137 | * |
| 138 | * @since 5.0.0 |
| 139 | * @param array |
| 140 | * @return bool |
| 141 | */ |
| 142 | public function is_valid_config(array $json) |
| 143 | { |
| 144 | return is_array($json) && ! empty($json) && isset($json['name']) && isset($json['config']) && is_array($json['config']); |
| 145 | } |
| 146 | } |
| 147 |