PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.4.1
WP Popular Posts v6.4.1
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Themer.php
wordpress-popular-posts / src Last commit date
Activation 2 years ago Admin 2 years ago Block 2 years ago Container 2 years ago Front 2 years ago Rest 2 years ago Shortcode 2 years ago Traits 2 years ago Widget 2 years ago Bootstrap.php 2 years ago Cache.php 2 years ago Helper.php 2 years ago I18N.php 2 years ago Image.php 2 years ago Output.php 2 years ago Query.php 2 years ago Settings.php 2 years ago Themer.php 2 years ago Translate.php 2 years ago WordPressPopularPosts.php 2 years ago deprecated.php 2 years ago template-tags.php 2 years 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