| 1 |
<?php |
| 2 |
|
| 3 |
namespace TSB; |
| 4 |
|
| 5 |
|
| 6 |
class Init { |
| 7 |
function __construct() { |
| 8 |
add_action( 'init', [ $this, 'onInit' ] ); |
| 9 |
add_filter( 'default_title', [$this, 'defaultTitle'], 10, 2 ); |
| 10 |
add_filter( 'default_content', [$this, 'defaultContent'], 10, 2 ); |
| 11 |
} |
| 12 |
|
| 13 |
function onInit() { |
| 14 |
$this->tsb_register_blocks(); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
$template = [ [ 'tsb/team' ] ]; |
| 19 |
|
| 20 |
|
| 21 |
register_post_type( 'tsb', [ |
| 22 |
'labels' => [ |
| 23 |
'name' => __( 'Team Section', 'team-section' ), |
| 24 |
'singular_name' => __( 'ShortCode', 'team-section' ), |
| 25 |
'add_new_item' => __( 'Add New ShortCode', 'team-section' ), |
| 26 |
'edit_item' => __( 'Edit ShortCode', 'team-section' ), |
| 27 |
'new_item' => __( 'New ShortCode', 'team-section' ), |
| 28 |
'view_item' => __( 'View ShortCode', 'team-section' ), |
| 29 |
'search_items' => __( 'Search ShortCodes', 'team-section' ), |
| 30 |
'not_found' => __( 'Sorry, we couldn\'t find the ShortCode you are looking for.', 'team-section' ) |
| 31 |
], |
| 32 |
'public' => false, |
| 33 |
'show_ui' => true, |
| 34 |
'show_in_rest' => true, |
| 35 |
'publicly_queryable' => false, |
| 36 |
'show_in_menu' => true, |
| 37 |
'exclude_from_search' => true, |
| 38 |
'menu_position' => 14, |
| 39 |
'menu_icon' => 'dashicons-groups', |
| 40 |
'has_archive' => false, |
| 41 |
'hierarchical' => false, |
| 42 |
'capability_type' => 'page', |
| 43 |
'rewrite' => [ 'slug' => 'team-section' ], |
| 44 |
'supports' => [ 'title', 'editor' ], |
| 45 |
'template' => $template, |
| 46 |
'template_lock' => 'all', |
| 47 |
] ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
function tsb_register_blocks() { |
| 52 |
$blocks_path = TSB_DIR_PATH . 'build/blocks/'; |
| 53 |
|
| 54 |
// Use scandir instead of glob to prevent issues on restricted servers |
| 55 |
if ( ! is_dir( $blocks_path ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$files = scandir( $blocks_path ); |
| 60 |
$all_blocks = []; |
| 61 |
|
| 62 |
foreach ( $files as $file ) { |
| 63 |
if ( $file !== '.' && $file !== '..' && is_dir( $blocks_path . $file ) ) { |
| 64 |
$all_blocks[] = $blocks_path . $file; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( empty( $all_blocks ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// Blocks toggled OFF by the admin dashboard. |
| 73 |
$disabled_blocks = get_option( 'tsbBlocks', [] ); |
| 74 |
if ( ! is_array( $disabled_blocks ) ) { |
| 75 |
$disabled_blocks = []; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
foreach ( $all_blocks as $block_path ) { |
| 80 |
$block_name = basename( $block_path ); |
| 81 |
|
| 82 |
|
| 83 |
if ( in_array( $block_name, $disabled_blocks, true ) ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if ( in_array( $block_name, [ 'team-section'], true ) ) { |
| 89 |
register_block_type( $block_path ); |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
function defaultTitle( $title, $post ) { |
| 99 |
if ( 'page' === $post->post_type && isset( $_GET['title'] ) ) { |
| 100 |
return sanitize_text_field( wp_unslash( $_GET['title'] ) ); |
| 101 |
} |
| 102 |
return $title; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Allow ?content= query param to set default post content. |
| 107 |
*/ |
| 108 |
function defaultContent( $content, $post ) { |
| 109 |
if ( 'page' === $post->post_type && isset( $_GET['content'] ) ) { |
| 110 |
return wp_unslash( $_GET['content'] ); |
| 111 |
} |
| 112 |
return $content; |
| 113 |
} |
| 114 |
} |
| 115 |
|