-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.php
More file actions
238 lines (200 loc) · 5.56 KB
/
theme.php
File metadata and controls
238 lines (200 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace BestProject\Wordpress;
use BestProject\Wordpress\Sidebar,
BestProject\Wordpress\Language;
defined('ABSPATH') or die;
/**
* Wordpress Theme class that takes care of all the required functions to build a theme and use custom widgets.
*/
class Theme
{
/**
* Name of this Theme
*
* @var String
*/
protected $name;
/**
* Base URI for this theme directory.
*
* @var String
*/
protected $path;
/**
* Example features: post-formats, post-thumbnails, custom-background, custom-header, custom-logo, automatic-feed-links, html5, title-tag, customize-selective-refresh-widgets
* Documentation: https://developer.wordpress.org/reference/functions/add_theme_support/#post-formats
* @var Array
*/
protected $features;
/**
* This variable holds Theme instance and is used by other classes to avoid recreating object.
* @var Theme
*/
protected static $instance;
/**
* Remove selected pages from interface.
*
* @var Array
*/
protected $remove_pages;
/**
*
* @param Array $sidebars Array of supported sidebars (string or Sidebar class instances).
* @param Array $features Array of features supported by this theme (https://developer.wordpress.org/reference/functions/add_theme_support/#post-formats)
* @param Array $remove_pages Array of pages to remvoe from admin interface.
*/
public function __construct($sidebars = array(), Array $features = array(),
Array $remove_pages = array())
{
$this->path = get_stylesheet_directory();
$this->name = basename($this->path);
$this->features = $features;
$this->remove_pages = $remove_pages;
$this->registerSidebars($sidebars);
$this->registerFeatures();
$this->registerWidgets();
// Default theme behavior for back-end pages
if (is_admin()) {
// Remove selected admin pages
$this->removePages();
// Add admin stylesheets
add_action( 'admin_enqueue_scripts', [$this, 'registerFrameworkAdminAssets'] );
// Theme behavior for front-end
} else {
// Remove generator tag
add_filter('the_generator',function(){
return '';
});
}
self::$instance = $this;
}
/**
* Register framework assets. it is called automaticly. You don't have to do it manualy.
*/
public function registerFrameworkAdminAssets() {
wp_register_style( 'bp_wordpress_framework', get_template_directory_uri().'/vendor/bestproject/wordpress-framework/assets/admin.min.css', false, bloginfo('version') );
wp_enqueue_style( 'bp_wordpress_framework' );
}
/**
* Returns an Theme object instance if there was any created.
*
* @return instanceof Theme
*
* @throws Exception
*/
public static function &getInstance()
{
$className = __CLASS__;
if (self::$instance instanceof $className) {
return self::$instance;
} else {
throw new \Exception('Currently there is no instance of `Theme` class. Please create it, then use getInstance() method.');
}
}
/**
*
* @param String $name Name of the property to return.
* @param Mixed $default Default property
* @return Mixed
*/
public static function get($name, $default = null)
{
if (isset($this->$name)) {
return $this->$name;
}
return $default;
}
/**
* Return this theme name.
* @return String
*/
public static function getName()
{
return basename(get_stylesheet_directory());
}
/**
* Declares sidebars for the theme.
*
* @param Array $sidebars An array of BestProject\Wordpress\Theme\Sidebar instances.
*/
protected function registerSidebars(array $sidebars)
{
if (!empty($sidebars)) {
foreach ($sidebars AS $sidebar) {
$className = 'BestProject\\Wordpress\\Theme\\Sidebar'; //$className
if ($sidebar instanceof \BestProject\Wordpress\Theme\Sidebar) {
$sidebar->register();
} elseif (is_string($sidebar)) {
(new Sidebar($sidebar, Language::_($sidebar)))->register();
}
}
}
}
/**
* Return TRUE when chosen sidebar has widgets.
*
* @param String $name Name of the sidebar to check
*
* @return Boolean
*/
public static function hasSidebar($name)
{
return (bool) is_active_sidebar($name);
}
/**
* Registers Theme features
*/
protected function registerFeatures()
{
// TODO: Register features.
}
/**
* Register all the widgets in this framework.
*/
protected function registerWidgets()
{
// Scan framework directory for widgets
$widgets = glob(__DIR__.'/widget/*', GLOB_ONLYDIR);
foreach ($widgets AS $widget) {
$name = basename($widget);
$path = $widget.'/'.$name.'.php';
// If there is a widget declaration file
if (file_exists($path)) {
// Include the widget class
require_once $path;
// Register widget
$className = '\\BestProject\\Wordpress\\Widget\\'.ucfirst($name);
call_user_func(array($className, 'register'), $className);
}
}
// Scan template directory for widgets
$template_widgets_path = dirname(dirname(dirname(__DIR__))).'/widgets';
if (is_dir($template_widgets_path)) {
$widgets = glob($template_widgets_path.'/*');
foreach ($widgets AS $widget_path) {
$name = pathinfo($widget_path, PATHINFO_FILENAME);
// Include the widget class
require_once $widget_path;
// Register widget
$className = '\\'.ucfirst($name);
call_user_func(array($className, 'register'), $className);
}
}
}
/**
* Add hook to remove selected pages from admin interface.
*/
public function removePages()
{
add_action('admin_menu', [$this, 'removePagesAction']);
}
/**
* Method to remove selected pages from admin interface.
*/
public function removePagesAction()
{
foreach ($this->remove_pages AS $page) {
remove_menu_page($page);
}
}
}