| 1 |
<?php |
| 2 |
|
| 3 |
class BPLDocumentLibrary { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
add_action('init', [$this, 'register_post_type']); |
| 7 |
add_action('plugins_loaded', [$this, 'load_dependencies']); |
| 8 |
add_action('admin_enqueue_scripts', [$this, 'bplde_admin_scripts']); |
| 9 |
add_shortcode('document_library', [$this, 'bplde_document_library_shortcode']); |
| 10 |
|
| 11 |
// AJAX actions |
| 12 |
add_action('wp_ajax_bplde_save_document_library', [$this, 'bplde_save_document_library']); |
| 13 |
add_action('wp_ajax_bplde_get_single', [$this, 'bplde_get_single']); |
| 14 |
add_action('wp_ajax_bplde_delete_document_library', [$this, 'bplde_delete_document_library']); |
| 15 |
add_action('wp_ajax_bplde_get_all', [$this, 'bplde_get_all']); |
| 16 |
} |
| 17 |
|
| 18 |
public function load_dependencies() { |
| 19 |
require_once BPLDE_PLUGIN_PATH . 'document-library-block.php'; |
| 20 |
require_once BPLDE_PLUGIN_PATH . 'includes/functions.php'; |
| 21 |
require_once BPLDE_PLUGIN_PATH . 'includes/DocumentLibrary/DocumentLibrary.php'; |
| 22 |
} |
| 23 |
|
| 24 |
public function register_post_type() { |
| 25 |
register_post_type('document_library', [ |
| 26 |
'label' => 'Document Library', |
| 27 |
'public' => true, |
| 28 |
'menu_position'=> 20, |
| 29 |
'supports' => ['title'], |
| 30 |
'show_in_rest' => true, |
| 31 |
'show_in_menu' => false, |
| 32 |
]); |
| 33 |
} |
| 34 |
|
| 35 |
public function bplde_admin_scripts($screen) { |
| 36 |
if ($screen === 'ppt_viewer_page_document-library') { |
| 37 |
$current_user_id = get_current_user_id(); |
| 38 |
$nickname = get_user_meta($current_user_id, 'nickname', true); |
| 39 |
|
| 40 |
wp_enqueue_script( |
| 41 |
'bplde-all-library-script', |
| 42 |
BPLDE_PLUGIN_DIR . 'build/all-library.js', |
| 43 |
['react', 'react-dom', 'wp-media-utils', 'wp-components'], |
| 44 |
BPLDE_VER, |
| 45 |
true |
| 46 |
); |
| 47 |
wp_enqueue_style( |
| 48 |
'bplde-all-library-style', |
| 49 |
BPLDE_PLUGIN_DIR. 'build/all-library.css', |
| 50 |
['wp-components'], |
| 51 |
BPLDE_VER |
| 52 |
); |
| 53 |
|
| 54 |
wp_localize_script('bplde-all-library-script', 'bpldeSettings', [ |
| 55 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 56 |
'athorName' => $nickname, |
| 57 |
'adminUrl' => admin_url(), |
| 58 |
'nonce' => wp_create_nonce('bplde_nonce'), |
| 59 |
]); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function bplde_save_document_library() { |
| 64 |
check_ajax_referer('bplde_nonce', 'nonce'); |
| 65 |
|
| 66 |
if (!current_user_can('edit_posts')) { |
| 67 |
wp_send_json_error(['message' => 'Unauthorized.']); |
| 68 |
} |
| 69 |
|
| 70 |
$id = isset($_POST['id']) ? intval($_POST['id']) : 0; |
| 71 |
$title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : 'Untitled'; |
| 72 |
$settings = isset($_POST['settings']) ? json_decode(stripslashes($_POST['settings']), true) : []; |
| 73 |
|
| 74 |
$post_data = [ |
| 75 |
'post_title' => $title, |
| 76 |
'post_type' => 'document_library', |
| 77 |
'post_status' => 'publish', |
| 78 |
]; |
| 79 |
|
| 80 |
if ($id > 0) { |
| 81 |
$post_data['ID'] = $id; |
| 82 |
$result = wp_update_post($post_data, true); |
| 83 |
} else { |
| 84 |
$result = wp_insert_post($post_data, true); |
| 85 |
} |
| 86 |
|
| 87 |
if (is_wp_error($result)) { |
| 88 |
wp_send_json_error(['message' => $result->get_error_message()]); |
| 89 |
} else { |
| 90 |
update_post_meta($result, 'bplde_settings', $settings); |
| 91 |
|
| 92 |
wp_send_json_success([ |
| 93 |
'id' => $result, |
| 94 |
'settings' => $settings, |
| 95 |
'created' => get_the_date('Y/m/d \a\t g:i a', $result) |
| 96 |
]); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function bplde_get_single() { |
| 101 |
check_ajax_referer('bplde_nonce', 'nonce'); |
| 102 |
|
| 103 |
if (!current_user_can('edit_posts')) { |
| 104 |
wp_send_json_error(['message' => 'Unauthorized.']); |
| 105 |
} |
| 106 |
|
| 107 |
$id = intval($_GET['id'] ?? 0); |
| 108 |
|
| 109 |
if (!$id) { |
| 110 |
wp_send_json_error(['message' => 'Invalid ID']); |
| 111 |
} |
| 112 |
|
| 113 |
$post = get_post($id); |
| 114 |
if (!$post) { |
| 115 |
wp_send_json_error(['message' => 'Post not found']); |
| 116 |
} |
| 117 |
|
| 118 |
$settings = get_post_meta($id, 'bplde_settings', true); |
| 119 |
|
| 120 |
wp_send_json_success([ |
| 121 |
'id' => $id, |
| 122 |
'title' => $post->post_title, |
| 123 |
'settings' => $settings, |
| 124 |
'created' => get_the_date('Y/m/d \a\t g:i a', $id) |
| 125 |
]); |
| 126 |
} |
| 127 |
|
| 128 |
public function bplde_get_all() { |
| 129 |
check_ajax_referer('bplde_nonce', 'nonce'); |
| 130 |
|
| 131 |
if (!current_user_can('edit_posts')) { |
| 132 |
wp_send_json_error(['message' => 'Unauthorized.']); |
| 133 |
} |
| 134 |
|
| 135 |
$query = new WP_Query([ |
| 136 |
'post_type' => 'document_library', |
| 137 |
'post_status' => 'publish', |
| 138 |
'posts_per_page' => -1, |
| 139 |
]); |
| 140 |
|
| 141 |
$items = []; |
| 142 |
foreach ($query->posts as $post) { |
| 143 |
$settings = get_post_meta($post->ID, 'bplde_settings', true); |
| 144 |
|
| 145 |
$items[] = [ |
| 146 |
'id' => $post->ID, |
| 147 |
'title' => $post->post_title, |
| 148 |
'settings' => $settings, |
| 149 |
'created' => get_the_date('Y/m/d \a\t g:i a', $post) |
| 150 |
]; |
| 151 |
} |
| 152 |
|
| 153 |
wp_send_json_success($items); |
| 154 |
} |
| 155 |
|
| 156 |
public function bplde_delete_document_library() { |
| 157 |
check_ajax_referer('bplde_nonce', 'nonce'); |
| 158 |
|
| 159 |
if (!current_user_can('delete_posts')) { |
| 160 |
wp_send_json_error(['message' => 'Unauthorized.']); |
| 161 |
} |
| 162 |
|
| 163 |
$id = intval($_POST['id'] ?? 0); |
| 164 |
if (!$id) { |
| 165 |
wp_send_json_error(['message' => 'Invalid ID']); |
| 166 |
} |
| 167 |
|
| 168 |
wp_delete_post($id, true); |
| 169 |
wp_send_json_success(); |
| 170 |
} |
| 171 |
|
| 172 |
public function bplde_document_library_shortcode($atts) { |
| 173 |
$atts = shortcode_atts(['id' => 0], $atts); |
| 174 |
$id = (int) $atts['id']; |
| 175 |
|
| 176 |
$post = get_post($id); |
| 177 |
|
| 178 |
if (!$post || $post->post_type !== 'document_library') { |
| 179 |
return '<p>Document Library not found.</p>'; |
| 180 |
} |
| 181 |
|
| 182 |
$block = [ |
| 183 |
'blockName' => 'bpldl/document-library', |
| 184 |
'attrs' => ['selectedPostId' => $id], |
| 185 |
'innerBlocks' => [], |
| 186 |
'innerHTML' => '', |
| 187 |
'innerContent' => [], |
| 188 |
]; |
| 189 |
|
| 190 |
return render_block($block); |
| 191 |
} |
| 192 |
} |
| 193 |
|