-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.php
More file actions
744 lines (592 loc) · 22.2 KB
/
init.php
File metadata and controls
744 lines (592 loc) · 22.2 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
<?php
/**
* Plugin Name: HumanlikeDev Framework
* Plugin URI: https://www.behumanlike.com
* Description: A modern framework designed and built for themes by HumanlikeDev. Adds Post Types, Shortcodes, Additional Functionality etc. on a per-theme basis.
* Author: Moiz Farooq
* Author URI: https://www.behumanlike.com
* Contributors: Moiz Farooq
*
* Version: 1.0.0
*
* Text Domain: hlframework
* Domain Path: languages
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* https://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
if( !class_exists( 'HumanlikeDev_Framework' ) ){
class HumanlikeDev_Framework {
/**
* Instance of HumanlikeDev_Framework
*
* @since 1.0.0
* @access private
* @var object $instance The instance of HumanlikeDev_Framework
* @blame Moiz Farooq
*/
private static $instance;
/**
* Instance of HumanlikeDev_Framework
*
* @since 1.0.0
* @access private
* @var object $instance The instance of HumanlikeDev_Framework
* @blame Moiz Farooq
*/
public $theme_support;
/**
* Instance of HumanlikeDev_Framework
*
* @since 1.0.0
* @access private
* @var object $instance The instance of HumanlikeDev_Framework
* @blame Moiz Farooq
*/
public $path;
/**
* Instance.
*
* An global instance of the class. Used to retrieve the instance
* to use on other files/plugins/themes.
*
* @since 1.0.0
* @return object Instance of the class.
* @blame Moiz Farooq
*/
public static function instance() {
if ( is_null( self::$instance ) ){
self::$instance = new self();
}
return self::$instance;
}
/**
* __clone()
*
* Cloning is forbidden.
*
* @documentation Class structure taken from WooCommerce main Class.
* @since 1.0.0
* @blame Moiz Farooq
*/
public function __clone(){
// Do nothing
}
/**
* __wakeup()
*
* Unserialising instances of this class is forbidden.
*
* @documentation Class structure taken from WooCommerce main Class.
* @since 1.0.0
* @blame Moiz Farooq
*/
public function __wakeup(){
// Do nothing
}
/**
* __construct()
*
* Standard class construction function, from here we'll setup all the actions that the framework
* needs to register. Each action will generally setup a theme feature, CPTs, Shortodes etc.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function __construct(){
// First, grab the theme support for humanlikedev-framework for us to process afterward
add_action( 'after_setup_theme', array( $this, 'gather_theme_support' ), 10 );
// Next, let's process the custom post types
add_action( 'after_setup_theme', array( $this, 'process_gutenberg_blocks' ), 15 );
// Process WPBakery Blocks
add_action( 'after_setup_theme', array( $this, 'process_wpb_blocks' ), 25 );
// Process Elementor Blocks
add_action( 'init', array( $this, 'process_elementor_functions' ), 25 );
// Next, let's process the custom post types
add_action( 'init', array( $this, 'process_custom_post_types' ), 15 );
// Second, process custom widgets
add_action( 'widgets_init', array( $this, 'process_widgets' ), 10 );
// Third, process the custom taxonomies
add_action( 'init', array( $this, 'process_custom_post_taxonomies' ), 20 );
// Process Theme Options
add_action( 'customize_register', array( $this, 'process_options' ), 15 );
// Elementor + WPML
add_filter( 'wpml_elementor_widgets_to_translate', array( $this, 'wpml_widgets_to_translate_filter' ), 10, 1 );
}
/**
* process_options()
*
* Takes the options array from theme support and loops over to separate all options into
* panels, sections, and options. Then registers all and creates usable options
* within the WP Customiser
*
* @documentation https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_setting
* @documentation https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_section
* @documentation https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
* @documentation https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_panel
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_options( $wp_customize ){
// Add Custom Toggle Control
include( $this->path . 'customizer-controls/toggle-control.php' );
// Register Toggle Control
$wp_customize->register_control_type( 'Humanlike_Dev_Toggle_Control' );
$panel_priority = 300;
$section_priority = 100;
$option_priority = 100;
if( isset( $this->theme_support['theme_options'] ) && is_array( $this->theme_support['theme_options'] ) ){
// Loop over each options group
foreach( $this->theme_support['theme_options'] as $panel ){
// Add each panel that's found
$wp_customize->add_panel(
$panel['id'], array(
'title' => $panel['title'],
'description' => $panel['description'],
'priority' => $panel_priority++
)
);
// Loop over each section that's found
if( is_array( $panel['sections'] ) ){
foreach( $panel['sections'] as $section ){
$wp_customize->add_section(
$section['id'], array(
'title' => $section['title'],
'description' => $section['description'],
'priority' => $section_priority++,
'panel' => $panel['id']
)
);
// Loop over each option that's found
if( is_array( $section['options'] ) ){
foreach( $section['options'] as $option ){
$wp_customize->add_setting(
$option['id'],
array(
'default' => $option['default'],
'transport' => $option['transport'],
'type' => ( isset( $option['option_type'] ) ) ? $option['option_type'] : 'theme_mod'
)
);
if( 'image' == $option['type'] ){
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
$option['id'],
array(
'label' => $option['title'],
'section' => $section['id'],
'priority' => $option_priority++
)
)
);
} elseif( 'color' == $option['type'] ){
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
$option['id'],
array(
'label' => $option['title'],
'section' => $section['id'],
'priority' => $option_priority++
)
)
);
} elseif( 'toggle' == $option['type'] ){
$wp_customize->add_control(
new Humanlike_Dev_Toggle_Control(
$wp_customize,
$option['id'],
array(
'label' => $option['title'],
'section' => $section['id'],
'type' => 'toggle',
'priority' => $option_priority++
)
)
);
} else {
$choices = ( isset( $option['choices'] ) ) ? $option['choices'] : false;
$wp_customize->add_control(
$option['id'],
array(
'type' => $option['type'],
'label' => $option['title'],
'section' => $section['id'],
'priority' => $option_priority++,
'choices' => $choices
)
);
}
}
}
}
}
} // End Foreach
} // End If
}
/**
* gather_theme_support()
*
* Basic function, simply loads the theme support as registered in our theme.
* This is hooked to the init event which loads after the after_theme_setup event where theme support is registered.
*
* @documentation https://developer.wordpress.org/reference/functions/add_theme_support/
* @documentation https://developer.wordpress.org/reference/functions/get_theme_support/
* @since 1.0.0
* @blame Moiz Farooq
*/
public function gather_theme_support(){
$tr_framework_support = get_theme_support( 'humanlikedev-framework' );
if( is_array( $tr_framework_support ) ){
$this->path = plugin_dir_path( __FILE__ );
$this->theme_support = $tr_framework_support[0];
}
}
/**
* process_custom_post_types()
*
* Loops through our post_types array item (checks if it exists first) and then
* sends off any found to register_custom_post_type() to turn that into an actual post type registration.
*
* @since v1.0.0
* @blame Moiz Farooq
*/
public function process_custom_post_types(){
// Check that we're actually registering post types in this theme, and that it's an array.
if( isset( $this->theme_support['post_types'] ) && is_array( $this->theme_support['post_types'] ) ){
// Loop through all post types and send data off to register_custom_post_type()
foreach( $this->theme_support['post_types'] as $post_type => $args ){
$this->register_custom_post_type( $post_type, $args );
}
}
}
/**
* process_custom_post_taxonomies()
*
* Loops through our post_types taxonomies array item (checks if it exists first) and then
* sends off any found to register_custom_post_taxonomy() to turn that into an actual taxonomy registration.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_custom_post_taxonomies(){
// Check that we're actually registering post types in this theme, and that it's an array.
if( isset( $this->theme_support['taxonomy_types'] ) && is_array( $this->theme_support['taxonomy_types'] ) ){
// Loop through all post types and send data off to register_custom_post_type()
foreach( $this->theme_support['taxonomy_types'] as $taxonomy_type => $args ){
$this->register_custom_post_taxonomy( $taxonomy_type, $args );
}
}
}
/**
* register_custom_post_type()
*
* Accepts the arguments from our processed custom post types array, turns those arguments
* into living, breathing post types that can be modified directly from the main theme.
*
* @documentation https://codex.wordpress.org/Function_Reference/register_post_type
* @documentation https://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type
* @since 1.0.0
* @blame Moiz Farooq
*/
public function register_custom_post_type( $type = false, $args = false ){
// Allow this CPT to use post formats (if needed)
register_taxonomy_for_object_type( 'post_format', $type );
// Register the post type, modify args using filter humanlikedev_framework_cpt_{post-type}_args
register_post_type( $type, apply_filters( 'humanlikedev_framework_cpt_'. $type .'_args', $args ) );
}
/**
* register_custom_post_taxonomy()
*
* Accepts the arguments from our processed custom taxonomy types array, turns those arguments
* into living, breathing post taxonomies that can be modified directly from the main theme.
*
* @documentation https://developer.wordpress.org/reference/functions/register_taxonomy/
* @param STRING - $type - taxonomy name string
* @param ARGS - $array - Array of arguments for registering the taxonomy
* @since 1.0.0
* @blame Moiz Farooq
*/
public function register_custom_post_taxonomy( $type = false, $args = false ){
// Modify args using filter humanlikedev_framework_taxonomy_{taxonomy-type}_args
$filtered_args = apply_filters( 'humanlikedev_framework_taxonomy_'. $type .'_args', $args );
// Register the taxonomy
register_taxonomy( $type, $filtered_args['for_post_types'], $filtered_args );
}
/**
* process_gutenberg_blocks()
*
* Loops through our registered gutenberg blocks from theme support, functionality of each block
* is contained within the included file.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_gutenberg_blocks(){
// Check that this theme actually has gutenberg blocks, and then ensure we have a theme name set
if( isset( $this->theme_support['gutenberg_blocks'] ) && isset( $this->theme_support['gutenberg_blocks']['theme_name'] ) ){
// Grab blocks and loop over them
if( is_array( $this->theme_support['gutenberg_blocks']['blocks'] ) ){
foreach( $this->theme_support['gutenberg_blocks']['blocks'] as $block ){
include( $this->path . 'gutenberg-blocks/'. trailingslashit( $this->theme_support['gutenberg_blocks']['theme_name'] ) . $block .'.php' );
}
}
}
}
/**
* process_wpb_blocks()
*
* Loops through our registered wpb blocks from theme support, functionality of each block
* is contained within the included file.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_wpb_blocks(){
// Check that this theme actually has wpb blocks, and then ensure we have a theme name set
if( isset( $this->theme_support['wpb_blocks'] ) && isset( $this->theme_support['wpb_blocks']['theme_name'] ) ){
// Grab blocks and loop over them
if( is_array( $this->theme_support['wpb_blocks']['blocks'] ) ){
include( $this->path . 'wpb-blocks/'. trailingslashit( $this->theme_support['wpb_blocks']['theme_name'] ) . 'functions.php' );
foreach( $this->theme_support['wpb_blocks']['blocks'] as $block ){
include( $this->path . 'wpb-blocks/'. trailingslashit( $this->theme_support['wpb_blocks']['theme_name'] ) . $block .'.php' );
}
}
}
}
public function process_elementor_functions(){
// Check that this theme actually has elementor blocks, and then ensure we have a theme name set
if( isset( $this->theme_support['elementor_blocks'] ) && isset( $this->theme_support['elementor_blocks']['theme_name'] ) ){
// Check we have blocks registered
if( is_array( $this->theme_support['elementor_blocks']['blocks'] ) ){
// Include our elementor functions on the init hook
include( $this->path . 'elementor-blocks/'. trailingslashit( $this->theme_support['elementor_blocks']['theme_name'] ) . 'functions.php' );
// Register our widgets to the correct hook
add_action( 'elementor/widgets/widgets_registered', array( $this, 'process_elementor_blocks' ), 25 );
}
}
}
/**
* process_elementor_blocks()
*
* Loops through our registered elementor blocks from theme support, functionality of each block
* is contained within the included file.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_elementor_blocks(){
// Check that this theme actually has elementor blocks, and then ensure we have a theme name set
if( isset( $this->theme_support['elementor_blocks'] ) && isset( $this->theme_support['elementor_blocks']['theme_name'] ) ){
// Grab blocks and loop over them
if( is_array( $this->theme_support['elementor_blocks']['blocks'] ) ){
foreach( $this->theme_support['elementor_blocks']['blocks'] as $block ){
include( $this->path . 'elementor-blocks/'. trailingslashit( $this->theme_support['elementor_blocks']['theme_name'] ) . $block .'.php' );
}
}
}
}
public function wpml_widgets_to_translate_filter( $widgets ) {
$widgets[ 'humanlikedev-icon-text-block' ] = array(
'conditions' => [
'widgetType' => 'humanlikedev-icon-text-block'
],
'fields' => [
[
'field' => 'content',
'type' => 'Icon & Text Block Content',
'editor_type' => 'VISUAL'
],
],
);
$widgets[ 'humanlikedev-accordion-block' ] = array(
'conditions' => [
'widgetType' => 'humanlikedev-accordion-block'
],
'fields' => [
[
'field' => 'item_title',
'type' => 'Accordion Title',
'editor_type' => 'LINE'
],
[
'field' => 'item_content',
'type' => 'Accordion Content',
'editor_type' => 'VISUAL'
],
],
);
return $widgets;
}
/**
* process_widgets()
*
* Loops through our registered wpb blocks from theme support, functionality of each block
* is contained within the included file.
*
* @since 1.0.0
* @blame Moiz Farooq
*/
public function process_widgets(){
// Check that this theme actually has wpb blocks, and then ensure we have a theme name set
if( isset( $this->theme_support['widgets'] ) && isset( $this->theme_support['widgets']['theme_name'] ) ){
// Grab blocks and loop over them
if( is_array( $this->theme_support['widgets']['widgets'] ) ){
foreach( $this->theme_support['widgets']['widgets'] as $block ){
include( $this->path . 'widgets/'. trailingslashit( $this->theme_support['widgets']['theme_name'] ) . $block .'.php' );
}
}
}
}
/**
* the_terms()
*
* A simple to use function that takes a taxonomy and post id, and returns a formatted list
* of assigned terms. Has before and after markup functions
*
* @param INT - $id - Post ID to check against
* @param STRING - $taxonomy - Taxonomy to check against
* @param STRING - $display - The display type to return, slug, name or link
* @param STRING - $separator - The string to add between items
* @since 1.0.0
* @blame Moiz Farooq
* @todo Tidy this up
*/
public function the_terms( $id = false, $taxonomy = false, $display = 'name', $separator = '', $before = false, $after = false, $class = false ){
// Exit if we've not provided correct input
if( !$id || !$taxonomy ){
return false;
}
// Gather terms for this post
$terms = get_the_terms( $id, $taxonomy );
// If terms are empty, just return false
if(!( is_array( $terms ) )){
return false;
}
$output = $before;
foreach( $terms as $term ){
if( 'slug' == $display ){
$output .= $term->slug;
} elseif( 'name' == $display ){
$output .= $term->name;
} elseif( 'link' == $display ){
$class_attr = ( $class ) ? 'class="'. $class .'"' : false;
$output .= '<a href="'. get_term_link( $term ) .'" data-term-slug="'. $term->slug .'" '. $class_attr .'>'. $term->name .'</a>';
}
$output .= $separator;
}
$length = strlen( $separator );
return substr( $output, 0, -$length ) . $after;
}
}
add_action( 'plugins_loaded', 'HumanlikeDev_Framework', 10 );
}
if( !function_exists( 'HumanlikeDev_Framework' ) ){
function HumanlikeDev_Framework() {
return HumanlikeDev_Framework::instance();
}
}
/* Disable Elementor's getting started redirect as its interupting merlin setup */
add_action( 'admin_init', function() {
if ( did_action( 'elementor/loaded' ) ) {
remove_action( 'admin_init', [ \Elementor\Plugin::$instance->admin, 'maybe_redirect_to_getting_started' ] );
}
}, 1 );
/* Add post type options automatically */
function tr_framework_post_type_options( $args ){
/**
* Setup theme customiser options
*/
if( is_array( $args['post_types'] ) ) :
$sections = array();
foreach( $args['post_types'] as $key => $array ) :
$options = array();
if( $array['has_archive'] ){
$options[] = array(
'id' => $key . '_post_type_slug',
'title' => ucfirst( $key ) . ' Archive URL Slug',
'default' => get_option( $key . '_post_type_slug', $key ),
'type' => 'text',
'transport' => 'postMessage',
'choices' => '',
'option_type' => 'option'
);
}
$options[] = array(
'id' => $key . '_post_type_active',
'title' => ucfirst( $key ) . ' Post Type Active?',
'default' => 'yes',
'type' => 'select',
'transport' => 'postMessage',
'choices' => array(
'yes' => 'Yes',
'no' => 'No'
),
'option_type' => 'option'
);
if( isset( $array['taxonomies'] ) && is_array( $array['taxonomies'] ) ){
foreach( $array['taxonomies'] as $value ){
$options[] = array(
'id' => $value . '_taxonomy_slug',
'title' => ucwords( str_replace( '_', ' ', $value ) ) . ' Taxonomy Slug',
'default' => $value,
'type' => 'text',
'transport' => 'postMessage',
'choices' => '',
'option_type' => 'option'
);
}
}
$sections[] = array(
'id' => $key . '_post_type_settings',
'title' => ucfirst( $key ) . ' Settings',
'description' => 'When changing these settings, you will need to go to "settings => permalinks" and hit the "Save Changes" button if you receive any unexpected 404 errors.',
'options' => $options
);
endforeach;
$args['theme_options'][] = array(
'title' => 'Post Type Settings',
'id' => 'post_type_settings',
'description' => '',
'sections' => $sections
);
endif;
/**
* Enable / Disable post types
*/
if( is_array( $args['post_types'] ) ){
foreach( $args['post_types'] as $key => $array ){
if( 'no' == get_option( $key . '_post_type_active', 'yes' ) ){
unset( $args['post_types'][$key] );
}
}
}
if( is_array( $args['taxonomy_types'] ) ){
foreach( $args['taxonomy_types'] as $key => $array ){
if( $new_slug = get_option( $key . '_taxonomy_slug' ) ){
$args[ 'taxonomy_types' ][ $key ][ 'rewrite' ] = array(
'slug' => $new_slug,
'with_front' => false
);
}
}
}
return $args;
}
add_filter( 'humanlikedev_framework_theme_args', 'tr_framework_post_type_options', 10, 1 );