-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTheme.php
More file actions
479 lines (418 loc) · 12 KB
/
Copy pathTheme.php
File metadata and controls
479 lines (418 loc) · 12 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
namespace JustCoded\WP\Framework;
use JustCoded\WP\Framework\Objects\Singleton;
use JustCoded\WP\Framework\Supports\Autoptimize;
use JustCoded\WP\Framework\Supports\Just_Custom_Fields;
use JustCoded\WP\Framework\Supports\Just_Post_Preview;
use JustCoded\WP\Framework\Supports\Just_Responsive_Images;
use JustCoded\WP\Framework\Supports\Just_Tinymce;
use JustCoded\WP\Framework\Web\Template_Hierarchy;
use JustCoded\WP\Framework\Supports\Just_Load_More;
use JustCoded\WP\Framework\Web\View;
/**
* Main base class for theme.
* Init main path rewrite operations and activate / register hooks
*/
abstract class Theme {
use Singleton;
/**
* Theme version
*
* @var float
*/
public $version = 1.0;
/**
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*
* @var boolean $auto_title
*/
public $auto_title = true;
/**
* Enable support for Post Thumbnails on posts and pages.
*
* @var boolean
*/
public $post_thumbnails = true;
/**
* Add allowed mime types for upload.
* Will be added to a standard WordPress list.
*
* If you need SVG uploads, you should also add the line below to your `wp-config.php` file:
* define( 'ALLOW_UNFILTERED_UPLOADS', true )
*
* @var array
*/
public $upload_mime_types = array(
'svg' => 'image/svg+xml',
);
/**
* Available image sizes in Media upload dialog to insert correctly resized image.
*
* @var array
*/
public $available_image_sizes = array(
'thumbnail' => 'Thumbnail',
'medium' => 'Medium',
'large' => 'Large',
'full' => 'Full Size',
);
/**
* JPEG images compression quality value
* max 100%
*
* @var int
*/
public $jpeg_quality = 100;
/**
* Enable support for Post Formats.
* Set FALSE to disable post formats
*
* @var array $post_formats
* @link https://developer.wordpress.org/themes/functionality/post-formats/
*/
public $post_formats = array(
'aside',
'image',
'video',
'quote',
'link',
);
/**
* Enable/disable admin bar
*
* @var boolean $show_admin_bar
*/
public $show_admin_bar = true;
/**
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
* Set FALSE to skip
*
* @var array $html5
*/
public $html5 = array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
);
/**
* Disable gutenberg for posts and custom post type.
*
* Set TRUE to disable it totally.
* Set ARRAY to disable only specific ones.
*
* @var array|bool $disable_gutenberg
*/
public $disable_gutenberg;
/**
* Init actions and hooks
*/
protected function __construct() {
$this->register_post_types();
$this->register_taxonomies();
$this->init_views_templates();
$this->register_api_endpoints();
/**
* Pretty standard theme hooks
*/
add_action( 'after_setup_theme', array( $this, 'theme_setup' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
add_action( 'widgets_init', array( $this, 'register_sidebars' ) );
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_filter( 'jpeg_quality', array( $this, 'filter_jpeg_quality' ) );
// activate hooks to clean up rewrite rules.
add_action( 'after_switch_theme', array( $this, 'activate' ) );
add_action( 'switch_theme', array( $this, 'deactivate' ) );
add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ) );
// Custom SMTP settings.
add_action( 'phpmailer_init', array( $this, 'phpmailer_init_smtp' ), 20 );
/**
* Remove Unnecessary Code from wp_head
*/
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
add_action( 'wp_enqueue_scripts', array( $this, 'remove_base_stylesheet' ), 20 );
if ( ! is_admin() ) {
add_filter( 'script_loader_src', array( $this, 'remove_assets_query_string' ), 15, 1 );
add_filter( 'style_loader_src', array( $this, 'remove_assets_query_string' ), 15, 1 );
}
$this->support_plugins();
$this->init();
}
/**
* Remove default stylesheet
*/
public function remove_base_stylesheet() {
/* @var \WP_Theme $theme */
$theme = wp_get_theme();
$stylesheet = $theme->get_stylesheet();
wp_dequeue_style( "$stylesheet-style" );
wp_deregister_style( "$stylesheet-style" );
}
/**
* Remove query string from static resources
*
* @param string $src Assets src path.
*
* @return string
*/
public function remove_assets_query_string( $src ) {
if ( strpos( $src, '?ver' ) !== false ) {
$src_parts = explode( '?ver', $src, 2 );
$src = array_shift( $src_parts );
}
if ( strpos( $src, '&ver' ) !== false ) {
$src_parts = explode( '&ver', $src, 2 );
$src = array_shift( $src_parts );
}
return $src;
}
/**
* Init new Template Hierarchy based on "views" folder and load Views engine.
*/
public function init_views_templates() {
Template_Hierarchy::instance();
View::instance();
}
/**
* Called right away after constructor. You can define/call additional actions here
*/
public function init() {
}
/**
* Theme activation hook.
* Flush rewrite rules on activate
*
* @param string $oldname previous active theme name.
* @param string|boolean $oldtheme previous active theme.
*/
public function activate( $oldname, $oldtheme = false ) {
flush_rewrite_rules();
}
/**
* Theme deactivation hook.
* Flush rewrite rules on activate
*
* @param string $newname new active theme name.
* @param string|boolean $newtheme new active theme.
*/
public function deactivate( $newname, $newtheme = '' ) {
// do nothing.
}
/**
* Main theme setup function.
*
* Register components, theme options, widgets etc
*
* Should be called on after_theme_setup action hook
*/
public function theme_setup() {
if ( $this->auto_title ) {
add_theme_support( 'title-tag' );
}
if ( $this->post_thumbnails ) {
add_theme_support( 'post-thumbnails' );
}
if ( ! empty( $this->post_formats ) && is_array( $this->post_formats ) ) {
add_theme_support( 'post-formats', $this->post_formats );
}
if ( ! $this->show_admin_bar ) {
add_filter( 'show_admin_bar', '__return_false' );
}
if ( $this->upload_mime_types ) {
add_filter( 'upload_mimes', array( $this, 'add_upload_mimes' ) );
}
if ( ! empty( $this->html5 ) && is_array( $this->html5 ) ) {
add_theme_support( 'html5', $this->html5 );
}
if ( isset( $this->disable_gutenberg ) ) {
if ( is_bool( $this->disable_gutenberg ) && ! empty( $this->disable_gutenberg ) ) {
add_filter( 'use_block_editor_for_post_type', '__return_false', 10 );
}
if ( is_array( $this->disable_gutenberg ) ) {
add_filter( 'use_block_editor_for_post_type', function ( $use_block_editor, $post_type ) {
return ! in_array( $post_type, $this->disable_gutenberg, true );
}, 10, 2 );
}
}
/**
* Remove global content width.
* This can affect "width" attribute for images. If required can be overwritten in app file.
*
* @global int $GLOBALS ['content_width']
* @name $content_width
* @link https://codex.wordpress.org/Content_Width
*/
$GLOBALS['content_width'] = '';
}
/**
* Overwrite available image sizes inside media upload screen.
*
* @param array $sizes WordPress standard sizes array.
*
* @return array
*/
public function image_size_names_choose( $sizes ) {
return $this->available_image_sizes;
}
/**
* Redefine certain PHPMailer options with our custom ones.
*
* @param \PHPMailer $phpmailer The PHPMailer instance (passed by reference).
*/
public function phpmailer_init_smtp( $phpmailer ) {
if ( empty( JC_MAIL_USERNAME ) || empty( JC_MAIL_PASSWORD ) ) {
return;
}
$phpmailer->isSMTP();
$phpmailer->Host = JC_MAIL_HOST;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = JC_MAIL_PORT;
$phpmailer->Username = JC_MAIL_USERNAME;
$phpmailer->Password = JC_MAIL_PASSWORD;
}
/**
* Register styles and scripts
*
* Called on 'wp_enqueue_scripts'
*/
abstract public function register_assets();
/**
* Register array of css files one-by-one.
* You can set general dependencies for all of them
*
* @param array $styles Array of css files to register.
* @param array $dependencies Array of script files which should be registered before this.
* @param null|string $base_uri Uri prefix for all scripts from.
*
* @return void|null
*/
public function register_assets_css( $styles, $dependencies = array(), $base_uri = null ) {
if ( empty( $styles ) ) {
return;
}
if ( ! is_array( $styles ) ) {
$styles = array( $styles );
}
if ( is_null( $base_uri ) ) {
$base_uri = get_template_directory_uri() . '/assets/css/';
}
foreach ( $styles as $filename ) {
wp_enqueue_style( '_jmvt-' . preg_replace( '/(\.(.+?))$/', '', $filename ), $base_uri . $filename, $dependencies );
}
}
/**
* Register array of script files one-by-one.
* You can set general dependencies for all of them
*
* @param array $scripts Array of script files to register.
* @param array $dependencies Array of script files which should be registered before this.
* @param null|string $base_uri Uri prefix for all scripts from.
*
* @return void|null
*/
public function register_assets_scripts( $scripts, $dependencies = array(), $base_uri = null ) {
if ( empty( $scripts ) ) {
return;
}
if ( ! is_array( $scripts ) ) {
$scripts = array( $scripts );
}
if ( is_null( $base_uri ) ) {
$base_uri = get_template_directory_uri() . '/assets/js/';
}
foreach ( $scripts as $filename ) {
wp_enqueue_script( '_jmvt-' . preg_replace( '/(\.(.+?))$/', '', $filename ), $base_uri . $filename, $dependencies, $this->version, true );
}
}
/**
* Register theme sidebars
* Usage:
* register_sidebar( ... );
*
* Called on 'widgets_init'
*/
public function register_sidebar() {
}
/**
* Register custom widgets
* Usage:
* include_once 'widgets/MyWidget.php';
* register_widget( 'MyWidget');
*
* Called on 'widgets_init'
*/
public function register_widgets() {
}
/**
* Register post types
* Usage:
* new \namespace\App\Post_Type\MyPostType();
*
* Each post type register it's own action hook
*/
public function register_post_types() {
}
/**
* Register taxonomies
* Usage:
* new \namespace\App\Taxonomy\MyTaxonomy();
*
* Each Taxonomy register it's own action hook
*/
public function register_taxonomies() {
}
/**
* Register API Endpoints
* Usage:
* new \namespace\App\Endpoints\MyEndpoint();
*
* Each endpoint register it's own action hook
*/
public function register_api_endpoints() {
}
/**
* Adds loading of custom features provided by 3d-party plugins.
*/
public function support_plugins() {
Just_Load_More::instance();
Just_Responsive_Images::instance();
Just_Custom_Fields::instance();
Just_Post_Preview::instance();
Just_Tinymce::instance();
}
/**
* Filter JPEG image quality compression to prevent image quality loss
*
* @return int
*/
public function filter_jpeg_quality() {
// limit jpeg_quality between 10 and 100%.
$quality = max( 10, min( $this->jpeg_quality, 100 ) );
return $quality;
}
/**
* Filters list of allowed mime types and file extensions.
* By default we add new extensions, which are defined in $this->upload_mime_types property
*
* @param array $mimes Mime types keyed by the file extension regex corresponding to
* those types. 'swf' and 'exe' removed from full list. 'htm|html' also
* removed depending on '$user' capabilities.
*
* @return array
*/
public function add_upload_mimes( $mimes ) {
$mimes = array_merge( $mimes, $this->upload_mime_types );
return $mimes;
}
}