PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / trunk
WP Popular Posts vtrunk
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 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