manager.php
52 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Form_Patterns; |
| 4 | |
| 5 | use Jet_Form_Builder\Plugin; |
| 6 | |
| 7 | class Manager { |
| 8 | |
| 9 | public function __construct() { |
| 10 | add_action( 'current_screen', array( $this, 'maybe_register_patterns' ) ); |
| 11 | } |
| 12 | |
| 13 | private function namespace() { |
| 14 | return Plugin::instance()->post_type->slug(); |
| 15 | } |
| 16 | |
| 17 | public function maybe_register_patterns() { |
| 18 | if ( get_current_screen()->post_type !== $this->namespace() ) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | $this->register_block_patterns(); |
| 23 | } |
| 24 | |
| 25 | public function get_patterns() { |
| 26 | return apply_filters( |
| 27 | 'jet-form-builder/form-patterns', |
| 28 | require_once Plugin::instance()->plugin_dir( 'includes/form-patterns/source-patterns.php' ) |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | public function register_block_patterns() { |
| 33 | register_block_pattern_category( |
| 34 | $this->namespace(), |
| 35 | array( |
| 36 | 'label' => __( 'JetForms', 'jet-form-builder' ), |
| 37 | ) |
| 38 | ); |
| 39 | |
| 40 | foreach ( $this->get_patterns() as $pattern_name => $pattern ) { |
| 41 | $pattern['categories'] = array( $this->namespace() ); |
| 42 | |
| 43 | register_block_pattern( $this->pattern_name( $pattern_name ), $pattern ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private function pattern_name( $name ) { |
| 48 | return $this->namespace() . '/' . $name; |
| 49 | } |
| 50 | |
| 51 | } |
| 52 |