Skip to content

Commit 965c9da

Browse files
committed
Moving the initiation class to a core class
1 parent 1d07474 commit 965c9da

File tree

2 files changed

+127
-94
lines changed

2 files changed

+127
-94
lines changed

inc/class-core.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
namespace {{namespace|lowerCase}}\classes;
3+
4+
/**
5+
* Core Class intiating the rest of the classes.
6+
*
7+
* @package {{namespace}}
8+
*/
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit;
12+
}
13+
14+
/**
15+
* Main plugin class.
16+
*/
17+
class Core {
18+
19+
/**
20+
* Constructor.
21+
*/
22+
public function __construct() {
23+
$this->load_classes();
24+
25+
add_action( 'init', array( $this, 'init' ) );
26+
add_action( 'init', array( $this, 'register_blocks' ) );
27+
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
28+
29+
// Initialize components.
30+
new Post_Types();
31+
new Taxonomies();
32+
new Fields();
33+
new Repeater_Fields();
34+
new Options();
35+
new SCF_JSON();
36+
new Block_Templates();
37+
new Block_Bindings();
38+
new Patterns();
39+
}
40+
41+
/**
42+
* Load the plugin classes.
43+
*
44+
* @return void
45+
*/
46+
public function load_classes() {
47+
// Include core classes.
48+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-post-types.php';
49+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-taxonomies.php';
50+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-fields.php';
51+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-repeater-fields.php';
52+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-options.php';
53+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-scf-json.php';
54+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-block-templates.php';
55+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-block-bindings.php';
56+
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-patterns.php';
57+
}
58+
59+
/**
60+
* Initialize the plugin.
61+
*
62+
* @return void
63+
*/
64+
public function init() {
65+
// Register block category.
66+
add_filter( 'block_categories_all', array( $this, 'register_block_category' ) );
67+
}
68+
69+
/**
70+
* Register blocks from the blocks directory.
71+
*
72+
* @return void
73+
*/
74+
public function register_blocks() {
75+
// Auto-register all blocks in src/blocks/.
76+
$blocks_dir = {{namespace|upper}}_PLUGIN_DIR . 'build/blocks/';
77+
78+
if ( is_dir( $blocks_dir ) ) {
79+
$blocks = glob( $blocks_dir . '*/block.json' );
80+
81+
foreach ( $blocks as $block_json ) {
82+
register_block_type( dirname( $block_json ) );
83+
}
84+
}
85+
}
86+
87+
/**
88+
* Register custom block category.
89+
*
90+
* @param array $categories Existing block categories.
91+
* @return array Modified block categories.
92+
*/
93+
public function register_block_category( $categories ) {
94+
return array_merge(
95+
array(
96+
array(
97+
'slug' => '{{slug}}',
98+
'title' => __( '{{name}}', '{{textdomain}}' ),
99+
'icon' => 'admin-generic',
100+
),
101+
),
102+
$categories
103+
);
104+
}
105+
106+
/**
107+
* Load plugin text domain for translations.
108+
*
109+
* @return void
110+
*/
111+
public function load_textdomain() {
112+
load_plugin_textdomain(
113+
'{{textdomain}}',
114+
false,
115+
dirname( {{namespace|upper}}_PLUGIN_BASENAME ) . '/languages'
116+
);
117+
}
118+
}

{{slug}}.php

Lines changed: 9 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -50,101 +50,16 @@ function () {
5050
return;
5151
}
5252

53-
// Include core classes.
54-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-post-types.php';
55-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-taxonomies.php';
56-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-fields.php';
57-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-repeater-fields.php';
58-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-options.php';
59-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-scf-json.php';
60-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-block-templates.php';
61-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-block-bindings.php';
62-
require_once {{namespace|upper}}_PLUGIN_DIR . 'inc/class-patterns.php';
63-
6453
/**
65-
* Main plugin class.
54+
* Initiate an instance of our plugin.
55+
*
56+
* @return {{namespace|lowerCase}}_plugin();
6657
*/
67-
class {{namespace|pascalCase}}_Plugin {
68-
69-
/**
70-
* Constructor.
71-
*/
72-
public function __construct() {
73-
add_action( 'init', array( $this, 'init' ) );
74-
add_action( 'init', array( $this, 'register_blocks' ) );
75-
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
76-
77-
// Initialize components.
78-
new \{{namespace|lowerCase}}\classes\Post_Types();
79-
new \{{namespace|lowerCase}}\classes\Taxonomies();
80-
new \{{namespace|lowerCase}}\classes\Fields();
81-
new \{{namespace|lowerCase}}\classes\Repeater_Fields();
82-
new \{{namespace|lowerCase}}\classes\Options();
83-
new \{{namespace|lowerCase}}\classes\SCF_JSON();
84-
new \{{namespace|lowerCase}}\classes\Block_Templates();
85-
new \{{namespace|lowerCase}}\classes\Block_Bindings();
86-
new \{{namespace|lowerCase}}\classes\Patterns();
87-
}
88-
89-
/**
90-
* Initialize the plugin.
91-
*
92-
* @return void
93-
*/
94-
public function init() {
95-
// Register block category.
96-
add_filter( 'block_categories_all', array( $this, 'register_block_category' ) );
97-
}
98-
99-
/**
100-
* Register blocks from the blocks directory.
101-
*
102-
* @return void
103-
*/
104-
public function register_blocks() {
105-
// Auto-register all blocks in src/blocks/.
106-
$blocks_dir = {{namespace|upper}}_PLUGIN_DIR . 'build/blocks/';
107-
108-
if ( is_dir( $blocks_dir ) ) {
109-
$blocks = glob( $blocks_dir . '*/block.json' );
110-
111-
foreach ( $blocks as $block_json ) {
112-
register_block_type( dirname( $block_json ) );
113-
}
114-
}
115-
}
116-
117-
/**
118-
* Register custom block category.
119-
*
120-
* @param array $categories Existing block categories.
121-
* @return array Modified block categories.
122-
*/
123-
public function register_block_category( $categories ) {
124-
return array_merge(
125-
array(
126-
array(
127-
'slug' => '{{slug}}',
128-
'title' => __( '{{name}}', '{{textdomain}}' ),
129-
'icon' => 'admin-generic',
130-
),
131-
),
132-
$categories
133-
);
134-
}
135-
136-
/**
137-
* Load plugin text domain for translations.
138-
*
139-
* @return void
140-
*/
141-
public function load_textdomain() {
142-
load_plugin_textdomain(
143-
'{{textdomain}}',
144-
false,
145-
dirname( {{namespace|upper}}_PLUGIN_BASENAME ) . '/languages'
146-
);
58+
function {{namespace|lowerCase}}_plugin() {
59+
global ${{namespace|lowerCase}}_plugin;
60+
if ( null === ${{namespace|lowerCase}}_plugin ) {
61+
${{namespace|lowerCase}}_plugin = new \{{namespace|lowerCase}}\classes\Core();
14762
}
63+
return ${{namespace|lowerCase}}_plugin;
14864
}
149-
150-
new {{namespace|pascalCase}}_Plugin();
65+
{{namespace|lowerCase}}_plugin();

0 commit comments

Comments
 (0)