|
| 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 | +} |
0 commit comments