| 1 |
<?php |
| 2 |
/** |
| 3 |
* BPLDE Document Library CPT. |
| 4 |
* |
| 5 |
* @package DocumentEmbedder |
| 6 |
*/ |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
if (!class_exists('BPLDE_Document_Library_CPT')) { |
| 13 |
class BPLDE_Document_Library_CPT |
| 14 |
{ |
| 15 |
|
| 16 |
protected static $_instance = null; |
| 17 |
protected $post_type = 'document_library'; |
| 18 |
|
| 19 |
public static function instance() |
| 20 |
{ |
| 21 |
if (self::$_instance === null) { |
| 22 |
self::$_instance = new self(); |
| 23 |
} |
| 24 |
return self::$_instance; |
| 25 |
} |
| 26 |
|
| 27 |
private function __construct() |
| 28 |
{ |
| 29 |
add_action('init', [$this, 'register_cpt']); |
| 30 |
add_filter('use_block_editor_for_post_type', [$this, 'disable_block_editor'], 10, 2); |
| 31 |
add_action('admin_head', [$this, 'cleanup_admin_ui']); |
| 32 |
add_action('edit_form_after_title', [$this, 'render_react_root']); |
| 33 |
add_action('init', [$this, 'remove_title_from_cpt']); |
| 34 |
add_filter("manage_{$this->post_type}_posts_columns", [$this, 'postTypeColumns'], 1); |
| 35 |
add_action("manage_{$this->post_type}_posts_custom_column", [$this, 'postTypeContent'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register Document Library CPT |
| 40 |
*/ |
| 41 |
public function register_cpt() |
| 42 |
{ |
| 43 |
register_post_type('document_library', [ |
| 44 |
'labels' => [ |
| 45 |
'name' => __('Document Library', 'document-emberdder'), |
| 46 |
'singular_name' => __('Document Library', 'document-emberdder'), |
| 47 |
'add_new_item' => __('Add New Library', 'document-emberdder'), |
| 48 |
'edit_item' => __('Edit Library', 'document-emberdder'), |
| 49 |
], |
| 50 |
'public' => false, |
| 51 |
'show_ui' => true, |
| 52 |
'show_in_menu' => 'edit.php?post_type=ppt_viewer', |
| 53 |
'supports' => ['title'], |
| 54 |
'capability_type' => 'post', |
| 55 |
'show_in_rest' => true, |
| 56 |
]); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Disable Gutenberg |
| 61 |
*/ |
| 62 |
public function disable_block_editor($use, $post_type) |
| 63 |
{ |
| 64 |
if ($post_type === 'document_library') { |
| 65 |
return false; |
| 66 |
} |
| 67 |
return $use; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Remove default WP UI junk |
| 72 |
*/ |
| 73 |
public function cleanup_admin_ui() { |
| 74 |
$screen = get_current_screen(); |
| 75 |
|
| 76 |
if (!$screen || $screen->post_type !== 'document_library') { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
remove_meta_box('submitdiv', 'document_library', 'side'); |
| 81 |
remove_meta_box('slugdiv', 'document_library', 'normal'); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Render React root instead of editor |
| 86 |
*/ |
| 87 |
public function render_react_root($post) { |
| 88 |
if ($post->post_type !== 'document_library') { |
| 89 |
return; |
| 90 |
} |
| 91 |
?> |
| 92 |
<style> |
| 93 |
#wpcontent, |
| 94 |
#poststuff { |
| 95 |
padding: 0; |
| 96 |
} |
| 97 |
|
| 98 |
.wrap, |
| 99 |
#post-body { |
| 100 |
margin: 0 !important; |
| 101 |
} |
| 102 |
|
| 103 |
.wp-heading-inline, |
| 104 |
.postbox-container, |
| 105 |
#screen-meta-links, |
| 106 |
.page-title-action { |
| 107 |
display: none !important; |
| 108 |
} |
| 109 |
|
| 110 |
#poststuff h2 { |
| 111 |
font-size: 18px; |
| 112 |
padding: 0 !important; |
| 113 |
margin: 0 !important; |
| 114 |
} |
| 115 |
</style> |
| 116 |
<div id="bpldeDocumentLibraryWrapper" data-post-id="<?php echo esc_attr($post->ID); ?>" data-is-premium="0"> |
| 117 |
</div> |
| 118 |
<?php |
| 119 |
} |
| 120 |
|
| 121 |
function remove_title_from_cpt() { |
| 122 |
remove_post_type_support('document_library', 'title'); |
| 123 |
} |
| 124 |
|
| 125 |
public function postTypeColumns($columns) |
| 126 |
{ |
| 127 |
$new = [ |
| 128 |
'cb' => $columns['cb'], |
| 129 |
'title' => $columns['title'], |
| 130 |
'shortcode' => 'Shortcode', |
| 131 |
'date' => $columns['date'], |
| 132 |
]; |
| 133 |
return $new; |
| 134 |
} |
| 135 |
|
| 136 |
public function postTypeContent($column_name, $post_id) |
| 137 |
{ |
| 138 |
switch ($column_name) { |
| 139 |
case 'shortcode': |
| 140 |
echo '<div class="bplde_front_shortcode"><input style="text-align: center; border: none; outline: none; background-color: #2664eb; color: #fff; padding: 4px 10px; border-radius: 3px;" value="[document_library id=' . esc_attr($post_id) . ']" ><span class="htooltip">Copy To Clipboard</span></div>'; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
BPLDE_Document_Library_CPT::instance(); |
| 148 |
|