Activation
5 years ago
Admin
5 years ago
Container
5 years ago
Front
5 years ago
Moment
5 years ago
Rest
5 years ago
Widget
5 years ago
Bootstrap.php
5 years ago
Cache.php
5 years ago
Helper.php
5 years ago
I18N.php
5 years ago
Image.php
5 years ago
Output.php
5 years ago
Query.php
5 years ago
Settings.php
5 years ago
Themer.php
5 years ago
Translate.php
5 years ago
WordPressPopularPosts.php
5 years ago
deprecated.php
5 years ago
template-tags.php
5 years ago
Themer.php
144 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 | $this->load_theme($fileinfo->getPathName()); |
| 63 | } |
| 64 | |
| 65 | if ( has_filter('wpp_additional_themes') ) { |
| 66 | $additional_themes = apply_filters('wpp_additional_themes', []); |
| 67 | |
| 68 | if ( is_array($additional_themes) && ! empty($additional_themes) ) { |
| 69 | foreach( $additional_themes as $additional_theme ) { |
| 70 | $this->load_theme($additional_theme); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Reads and loads theme into the class. |
| 78 | * |
| 79 | * @since 5.0.0 |
| 80 | * @param string $path Path to theme folder |
| 81 | */ |
| 82 | private function load_theme($path) |
| 83 | { |
| 84 | $theme_folder = is_string($path) && is_dir($path) && is_readable($path) ? basename($path) : null; |
| 85 | $theme_folder = $theme_folder ? preg_replace("/[^a-z0-9\_\-\.]/i", '', $theme_folder) : null; |
| 86 | $theme_path = $theme_folder ? $path : null; |
| 87 | |
| 88 | if ( |
| 89 | $theme_path |
| 90 | && '.' != $theme_folder |
| 91 | && '..' != $theme_folder |
| 92 | && false === strpos($theme_path, '..') |
| 93 | && ! isset($this->themes[$theme_folder]) |
| 94 | && file_exists($theme_path . '/config.json') |
| 95 | && file_exists($theme_path . '/style.css') |
| 96 | ) { |
| 97 | $str = file_get_contents($theme_path . '/config.json'); |
| 98 | $json = json_decode($str, true); |
| 99 | |
| 100 | if ( $this->is_valid_config($json) ) { |
| 101 | $this->themes[$theme_folder] = [ |
| 102 | 'json' => $json, |
| 103 | 'path' => $theme_path |
| 104 | ]; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns an array of available themes. |
| 111 | * |
| 112 | * @since 5.0.0 |
| 113 | * @return array |
| 114 | */ |
| 115 | public function get_themes() |
| 116 | { |
| 117 | return $this->themes; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns data of a specific theme (if found). |
| 122 | * |
| 123 | * @since 5.0.0 |
| 124 | * @param string $theme |
| 125 | * @return array|bool |
| 126 | */ |
| 127 | public function get_theme($theme) |
| 128 | { |
| 129 | return isset($this->themes[$theme]) ? $this->themes[$theme] : false; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Checks whether a $json array is a valid theme config. |
| 134 | * |
| 135 | * @since 5.0.0 |
| 136 | * @param array |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function is_valid_config($json = []) |
| 140 | { |
| 141 | return is_array($json) && ! empty($json) && isset($json['name']) && isset($json['config']) && is_array($json['config']); |
| 142 | } |
| 143 | } |
| 144 |