| 1 |
<?php |
| 2 |
namespace BTN\Includes; |
| 3 |
|
| 4 |
if( !class_exists( 'BTNCustomPost' ) ){ |
| 5 |
class BTNCustomPost{ |
| 6 |
public $post_type = 'button-block'; |
| 7 |
|
| 8 |
public function __construct(){ |
| 9 |
add_action( 'init', [$this, 'onInit'] ); |
| 10 |
add_action( 'post_row_actions', [$this, 'postRowActions'], 10, 2 ); |
| 11 |
add_action( 'admin_action_duplicate_btn_block_post', [$this, 'duplicatePost'] ); |
| 12 |
add_filter( 'manage_button-block_posts_columns', [$this, 'manageBTNBlockPostsColumns'], 10 ); |
| 13 |
add_action( 'manage_button-block_posts_custom_column', [$this, 'manageBTNBlockPostsCustomColumns'], 10, 2 ); |
| 14 |
add_shortcode( 'btn_block', [$this, 'onAddShortcode'] ); |
| 15 |
add_action( 'use_block_editor_for_post', [$this, 'useBlockEditorForPost'], 999, 2 ); |
| 16 |
} |
| 17 |
|
| 18 |
function onInit(){ |
| 19 |
$menuIcon = "<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 48 48' fill='#fff'><path d='M7 34Q5.75 34 4.875 33.125Q4 32.25 4 31V17Q4 15.75 4.875 14.875Q5.75 14 7 14H41Q42.25 14 43.125 14.875Q44 15.75 44 17V31Q44 32.25 43.125 33.125Q42.25 34 41 34H37.8V31H41Q41 31 41 31Q41 31 41 31V17Q41 17 41 17Q41 17 41 17H7Q7 17 7 17Q7 17 7 17V31Q7 31 7 31Q7 31 7 31H20.2V34ZM29 38 27.2 34 23.2 32.2 27.2 30.4 29 26.4 30.8 30.4 34.8 32.2 30.8 34ZM34 27.4 32.95 25.05 30.6 24 32.95 22.95 34 20.6 35.05 22.95 37.4 24 35.05 25.05Z'></path></svg>"; |
| 20 |
|
| 21 |
$hide_form_menu = get_option( 'button_block_option', 'false' ); |
| 22 |
$show_ui = ( $hide_form_menu === 'true' ) ? false : true; |
| 23 |
|
| 24 |
register_post_type( $this->post_type, [ |
| 25 |
'labels' => [ |
| 26 |
'name' => __( 'Button Block', 'button-block' ), |
| 27 |
'singular_name' => __( 'Button Block', 'button-block' ), |
| 28 |
'add_new' => __( 'Add New', 'button-block' ), |
| 29 |
'add_new_item' => __( 'Add New Button', 'button-block' ), |
| 30 |
'edit_item' => __( 'Edit Button', 'button-block' ), |
| 31 |
'new_item' => __( 'New Button', 'button-block' ), |
| 32 |
'view_item' => __( 'View Button', 'button-block' ), |
| 33 |
'search_items' => __( 'Search Button', 'button-block' ), |
| 34 |
'not_found' => __( 'Sorry, we couldn\'t find any item you are looking for.', 'button-block' ) |
| 35 |
], |
| 36 |
'public' => false, |
| 37 |
'show_ui' => $show_ui, |
| 38 |
'publicly_queryable' => false, |
| 39 |
'exclude_from_search' => false, |
| 40 |
'show_in_rest' => true, |
| 41 |
'menu_position' => 14, |
| 42 |
'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode($menuIcon), |
| 43 |
'has_archive' => false, |
| 44 |
'hierarchical' => false, |
| 45 |
'capability_type' => 'page', |
| 46 |
'rewrite' => [ 'slug' => 'button-block' ], |
| 47 |
'supports' => [ 'title', 'editor' ], |
| 48 |
'template' => [ ['btn/button'] ], |
| 49 |
'template_lock' => 'all' |
| 50 |
] ); // Register Post Type |
| 51 |
} |
| 52 |
|
| 53 |
function postRowActions( $actions, $post ){ |
| 54 |
if ( $post->post_type == $this->post_type ) { |
| 55 |
$actions['duplicate'] = '<a href="' . admin_url( "admin.php?action=duplicate_btn_block_post&post={$post->ID}" ) . '">Duplicate</a>'; |
| 56 |
} |
| 57 |
|
| 58 |
return $actions; |
| 59 |
} |
| 60 |
|
| 61 |
function duplicatePost(){ |
| 62 |
if ( !isset( $_GET['post'] ) || !current_user_can( 'edit_posts') ) { |
| 63 |
wp_die( 'Permission denied' ); |
| 64 |
} |
| 65 |
|
| 66 |
$postId = $_GET['post']; |
| 67 |
$post = get_post( $postId ); |
| 68 |
|
| 69 |
if ( !$post ) { |
| 70 |
wp_die( 'Invalid post ID' ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( $post && !current_user_can( 'edit_post', $postId ) ) { |
| 74 |
wp_die( 'You do not have permission to duplicate this post.' ); |
| 75 |
} |
| 76 |
|
| 77 |
$newPost = [ |
| 78 |
'post_title' => $post->post_title . '(copy)', |
| 79 |
'post_content' => $post->post_content, |
| 80 |
'post_status' => $post->post_status, |
| 81 |
'post_type' => $post->post_type, |
| 82 |
]; |
| 83 |
|
| 84 |
$newPostId = wp_insert_post( $newPost ); |
| 85 |
wp_redirect( admin_url( "post.php?action=edit&post={$newPostId}" ) ); |
| 86 |
exit; |
| 87 |
} |
| 88 |
|
| 89 |
function manageBTNBlockPostsColumns( $defaults ) { |
| 90 |
unset( $defaults['date'] ); |
| 91 |
$defaults['shortcode'] = 'ShortCode'; |
| 92 |
$defaults['date'] = 'Date'; |
| 93 |
return $defaults; |
| 94 |
} |
| 95 |
|
| 96 |
function manageBTNBlockPostsCustomColumns( $column_name, $post_ID ) { |
| 97 |
if ( $column_name == 'shortcode' ) { |
| 98 |
echo "<div class='bPlAdminShortcode' id='bPlAdminShortcode-$post_ID'> |
| 99 |
<input value='[btn_block id=$post_ID]' onclick='copyBPlAdminShortcode($post_ID)'> |
| 100 |
<span class='tooltip'>Copy To Clipboard</span> |
| 101 |
</div>"; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
function onAddShortcode( $atts ) { |
| 106 |
$post_id = $atts['id']; |
| 107 |
$post = get_post( $post_id ); |
| 108 |
|
| 109 |
if ( !$post ) { |
| 110 |
return ''; |
| 111 |
} |
| 112 |
|
| 113 |
if ( post_password_required( $post ) ) { |
| 114 |
return get_the_password_form( $post ); |
| 115 |
} |
| 116 |
|
| 117 |
switch ( $post->post_status ) { |
| 118 |
case 'publish': |
| 119 |
return $this->displayContent( $post ); |
| 120 |
|
| 121 |
case 'private': |
| 122 |
if (current_user_can('read_private_posts')) { |
| 123 |
return $this->displayContent( $post ); |
| 124 |
} |
| 125 |
return ''; |
| 126 |
|
| 127 |
case 'draft': |
| 128 |
case 'pending': |
| 129 |
case 'future': |
| 130 |
if ( current_user_can( 'edit_post', $post_id ) ) { |
| 131 |
return $this->displayContent( $post ); |
| 132 |
} |
| 133 |
return ''; |
| 134 |
|
| 135 |
default: |
| 136 |
return ''; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function displayContent( $post ){ |
| 141 |
$blocks = parse_blocks( $post->post_content ); |
| 142 |
return render_block( $blocks[0] ); |
| 143 |
} |
| 144 |
|
| 145 |
function useBlockEditorForPost( $use, $post ){ |
| 146 |
if ( $this->post_type === $post->post_type ) { |
| 147 |
return true; |
| 148 |
} |
| 149 |
return $use; |
| 150 |
} |
| 151 |
} |
| 152 |
new BTNCustomPost(); |
| 153 |
} |