| 1 |
<?php |
| 2 |
class CTBCustomPost{ |
| 3 |
public $post_type = 'ctb'; |
| 4 |
|
| 5 |
public function __construct(){ |
| 6 |
add_action( 'admin_enqueue_scripts', [$this, 'adminEnqueueScripts'] ); |
| 7 |
add_action( 'init', [$this, 'onInit'] ); |
| 8 |
add_shortcode( 'ctb', [$this, 'onAddShortcode'] ); |
| 9 |
add_filter( 'manage_ctb_posts_columns', [$this, 'manageCTBPostsColumns'], 10 ); |
| 10 |
add_action( 'manage_ctb_posts_custom_column', [$this, 'manageCTBPostsCustomColumns'], 10, 2 ); |
| 11 |
add_action( 'use_block_editor_for_post', [$this, 'useBlockEditorForPost'], 999, 2 ); |
| 12 |
add_filter( 'custom_menu_order', [$this, 'orderSubMenu'] ); |
| 13 |
} |
| 14 |
|
| 15 |
function adminEnqueueScripts( $hook ){ |
| 16 |
if( 'edit.php' === $hook || 'post.php' === $hook ){ |
| 17 |
wp_enqueue_style( 'ctb-admin-post', CTB_DIR_URL . 'build/admin-post.css', [], CTB_VERSION ); |
| 18 |
wp_enqueue_script( 'ctb-admin-post', CTB_DIR_URL . 'build/admin-post.js', [], CTB_VERSION ); |
| 19 |
wp_set_script_translations( 'ctb-admin-post', 'countdown-time', CTB_DIR_PATH . 'languages' ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
function onInit(){ |
| 24 |
$menuIcon = "<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 512 512' fill='#fff'><path d='m288 48h-64a24 24 0 0 1 0-48h64a24 24 0 0 1 0 48z' /><path d='m414.392 129.608c-1.867-1.867-3.772-3.684-5.692-5.477l19.963-26.769a8 8 0 0 0 -1.63-11.195l-25.65-19.13a8 8 0 0 0 -11.2 1.631l-19.844 26.614a224.63 224.63 0 1 0 44.053 34.326zm-158.392 350.392c-105.869 0-192-86.131-192-192s86.131-192 192-192 192 86.131 192 192-86.131 192-192 192z' /><path d='m408 296h-152a8 8 0 0 1 -8-8v-152a8 8 0 0 1 8-8c88.224 0 160 71.775 160 160a8 8 0 0 1 -8 8z' /><circle cx='201.421' cy='145.912' r='8' /><circle cx='160.185' cy='169.732' r='8' /><circle cx='128.327' cy='205.129' r='8' /><circle cx='108.967' cy='248.638' r='8' /><circle cx='104' cy='296' r='8' /><circle cx='113.912' cy='342.579' r='8' /><circle cx='137.732' cy='383.815' r='8' /><circle cx='173.129' cy='415.673' r='8' /><circle cx='216.638' cy='435.033' r='8' /><circle cx='264' cy='440' r='8' /><circle cx='310.579' cy='430.088' r='8' /><circle cx='351.815' cy='406.268' r='8' /><circle cx='383.673' cy='370.871' r='8' /><circle cx='403.033' cy='327.362' r='8' /></svg>"; |
| 25 |
|
| 26 |
register_post_type( 'ctb', [ |
| 27 |
'labels' => [ |
| 28 |
'name' => __( 'Countdown Time', 'countdown-time'), |
| 29 |
'singular_name' => __( 'Countdown Time', 'countdown-time' ), |
| 30 |
'add_new' => __( 'Add New', 'countdown-time' ), |
| 31 |
'add_new_item' => __( 'Add New', 'countdown-time' ), |
| 32 |
'edit_item' => __( 'Edit', 'countdown-time' ), |
| 33 |
'new_item' => __( 'New', 'countdown-time' ), |
| 34 |
'view_item' => __( 'View', 'countdown-time' ), |
| 35 |
'search_items' => __( 'Search', 'countdown-time'), |
| 36 |
'not_found' => __( 'Sorry, we couldn\'t find the that you are looking for.', 'countdown-time' ) |
| 37 |
], |
| 38 |
'public' => false, |
| 39 |
'show_ui' => true, |
| 40 |
'show_in_rest' => true, |
| 41 |
'publicly_queryable' => false, |
| 42 |
'exclude_from_search' => true, |
| 43 |
'menu_position' => 14, |
| 44 |
'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $menuIcon ), |
| 45 |
'has_archive' => false, |
| 46 |
'hierarchical' => false, |
| 47 |
'capability_type' => 'page', |
| 48 |
'rewrite' => [ 'slug' => 'ctb' ], |
| 49 |
'supports' => [ 'title', 'editor' ], |
| 50 |
'template' => [ ['ctb/countdown-time'] ], |
| 51 |
'template_lock' => 'all', |
| 52 |
]); // Register Post Type |
| 53 |
} |
| 54 |
|
| 55 |
function onAddShortcode( $atts ) { |
| 56 |
$post_id = $atts['id']; |
| 57 |
$post = get_post( $post_id ); |
| 58 |
|
| 59 |
if ( !$post ) { |
| 60 |
return ''; |
| 61 |
} |
| 62 |
|
| 63 |
if ( post_password_required( $post ) ) { |
| 64 |
return get_the_password_form( $post ); |
| 65 |
} |
| 66 |
|
| 67 |
switch ( $post->post_status ) { |
| 68 |
case 'publish': |
| 69 |
return $this->displayContent( $post ); |
| 70 |
|
| 71 |
case 'private': |
| 72 |
if (current_user_can('read_private_posts')) { |
| 73 |
return $this->displayContent( $post ); |
| 74 |
} |
| 75 |
return ''; |
| 76 |
|
| 77 |
case 'draft': |
| 78 |
case 'pending': |
| 79 |
case 'future': |
| 80 |
if ( current_user_can( 'edit_post', $post_id ) ) { |
| 81 |
return $this->displayContent( $post ); |
| 82 |
} |
| 83 |
return ''; |
| 84 |
|
| 85 |
default: |
| 86 |
return ''; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
function displayContent( $post ){ |
| 91 |
$blocks = parse_blocks( $post->post_content ); |
| 92 |
|
| 93 |
global $allowedposttags; |
| 94 |
$allowed_html = wp_parse_args( ['style' => [] ], $allowedposttags ); |
| 95 |
|
| 96 |
return wp_kses( render_block( $blocks[0] ), $allowed_html ); |
| 97 |
} |
| 98 |
|
| 99 |
function manageCTBPostsColumns( $defaults ) { |
| 100 |
unset( $defaults['date'] ); |
| 101 |
$defaults['shortcode'] = 'ShortCode'; |
| 102 |
$defaults['date'] = 'Date'; |
| 103 |
return $defaults; |
| 104 |
} |
| 105 |
|
| 106 |
function manageCTBPostsCustomColumns( $column_name, $post_ID ) { |
| 107 |
if ( $column_name == 'shortcode' ) { |
| 108 |
echo "<div class='ctbFrontShortcode' id='ctbFrontShortcode-$post_ID'> |
| 109 |
<input value='[ctb id=$post_ID]' onclick='ctbHandleShortcode( $post_ID )'> |
| 110 |
<span class='tooltip'>Copy To Clipboard</span> |
| 111 |
</div>"; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
function useBlockEditorForPost( $use, $post ){ |
| 116 |
if ( $this->post_type === $post->post_type ) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
return $use; |
| 120 |
} |
| 121 |
|
| 122 |
function orderSubMenu( $menu_ord ){ |
| 123 |
global $submenu; |
| 124 |
|
| 125 |
$sMenu = $submenu['edit.php?post_type=ctb'] ?? []; |
| 126 |
$arr = []; |
| 127 |
if( CTB_HAS_PRO && ctbIsPremium() ){ |
| 128 |
if( isset( $sMenu[5] ) ){ |
| 129 |
$arr[] = $sMenu[5]; // Countdown Time |
| 130 |
} |
| 131 |
if( isset( $sMenu[10] ) ){ |
| 132 |
$arr[] = $sMenu[10]; // Add New |
| 133 |
} |
| 134 |
} |
| 135 |
if( isset( $sMenu[11] ) ){ |
| 136 |
$arr[] = $sMenu[11]; // Help |
| 137 |
} |
| 138 |
if( isset( $sMenu[12] ) ){ |
| 139 |
$arr[] = $sMenu[12]; // Upgrade FS or Account |
| 140 |
} |
| 141 |
$submenu['edit.php?post_type=ctb'] = $arr; |
| 142 |
|
| 143 |
return $menu_ord; |
| 144 |
} |
| 145 |
} |
| 146 |
new CTBCustomPost(); |