| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\Folders; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Utils ; |
| 6 |
use WPAdminify\Inc\Admin\AdminSettings ; |
| 7 |
if ( !defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
class Folders |
| 11 |
{ |
| 12 |
private static $postIds ; |
| 13 |
public $options ; |
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$needed_keys = [ 'folders_user_roles', 'folders_enable_for', 'folders_media' ]; |
| 17 |
$this->options = (array) array_intersect_key( AdminSettings::get_instance()->get(), array_flip( $needed_keys ) ); |
| 18 |
add_action( 'init', [ $this, 'register_folders_terms' ] ); |
| 19 |
add_action( 'admin_footer', [ $this, 'add_footer_markup' ] ); |
| 20 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 21 |
add_action( 'wp_ajax_adminify_folder', [ $this, 'handle_adminify_folder' ] ); |
| 22 |
add_action( 'ajax_query_attachments_args', [ $this, 'filter_grid' ] ); |
| 23 |
add_filter( 'pre_get_posts', [ $this, 'filter_list' ] ); |
| 24 |
$this->modify_columns_actions(); |
| 25 |
} |
| 26 |
|
| 27 |
public function get_post_types() |
| 28 |
{ |
| 29 |
$post_types = get_post_types( [ |
| 30 |
'public' => true, |
| 31 |
] ); |
| 32 |
// @todo add more configurations here |
| 33 |
return $post_types; |
| 34 |
} |
| 35 |
|
| 36 |
public function filter_list( $query ) |
| 37 |
{ |
| 38 |
global $typenow ; |
| 39 |
global $pagenow ; |
| 40 |
$post_type = $typenow; |
| 41 |
if ( empty($post_type) ) { |
| 42 |
|
| 43 |
if ( $pagenow == 'edit.php' ) { |
| 44 |
$post_type = 'post'; |
| 45 |
} else { |
| 46 |
if ( $pagenow == 'upload.php' ) { |
| 47 |
$post_type = 'attachment'; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
if ( !isset( $query->query['post_type'] ) ) { |
| 53 |
return $query; |
| 54 |
} |
| 55 |
$taxonomy = self::get_post_type_taxonomy( $post_type ); |
| 56 |
if ( !isset( $_REQUEST[$taxonomy] ) ) { |
| 57 |
return $query; |
| 58 |
} |
| 59 |
$term = sanitize_text_field( $_REQUEST[$taxonomy] ); |
| 60 |
if ( $term != '-1' ) { |
| 61 |
return $query; |
| 62 |
} |
| 63 |
unset( $query->query_vars[$taxonomy] ); |
| 64 |
$tax_query = array( |
| 65 |
'taxonomy' => $taxonomy, |
| 66 |
'operator' => 'NOT EXISTS', |
| 67 |
); |
| 68 |
$query->set( 'tax_query', array( $tax_query ) ); |
| 69 |
$query->tax_query = new \WP_Tax_Query( array( $tax_query ) ); |
| 70 |
return $query; |
| 71 |
} |
| 72 |
|
| 73 |
public function filter_grid( $args ) |
| 74 |
{ |
| 75 |
$taxonomy = self::get_post_type_taxonomy( 'attachment' ); |
| 76 |
if ( !isset( $args[$taxonomy] ) ) { |
| 77 |
return $args; |
| 78 |
} |
| 79 |
$term = sanitize_text_field( $args[$taxonomy] ); |
| 80 |
if ( $term != "-1" ) { |
| 81 |
return $args; |
| 82 |
} |
| 83 |
unset( $args[$taxonomy] ); |
| 84 |
$args['tax_query'] = array( array( |
| 85 |
'taxonomy' => $taxonomy, |
| 86 |
'operator' => 'NOT EXISTS', |
| 87 |
) ); |
| 88 |
return $args; |
| 89 |
} |
| 90 |
|
| 91 |
public function is_module_active( $post_type = null ) |
| 92 |
{ |
| 93 |
$current_screen = get_current_screen(); |
| 94 |
if ( empty($current_screen->base) || !in_array( $current_screen->base, [ 'upload', 'edit' ] ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
if ( empty($post_type) ) { |
| 98 |
if ( empty($post_type = $current_screen->post_type) ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
if ( $post_type == 'attachment' ) { |
| 103 |
return (bool) $this->options['folders_media']; |
| 104 |
} |
| 105 |
$default_post_types = [ 'post', 'page' ]; |
| 106 |
$folders_enable_for = $this->options['folders_enable_for']; |
| 107 |
$folders_enable_for = array_intersect( $folders_enable_for, $default_post_types ); |
| 108 |
if ( in_array( $post_type, $folders_enable_for ) ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
public function add_footer_markup() |
| 115 |
{ |
| 116 |
if ( $this->is_module_active() ) { |
| 117 |
echo '<div id="wp-adminify--folder-app"></div>' ; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function get_uncategorized_posts( $post_type ) |
| 122 |
{ |
| 123 |
global $wpdb ; |
| 124 |
$post_table = $wpdb->prefix . "posts"; |
| 125 |
$term_table = $wpdb->prefix . "term_relationships"; |
| 126 |
$term_taxonomy_table = $wpdb->prefix . "term_taxonomy"; |
| 127 |
$post_type_tax = self::get_post_type_taxonomy( $post_type ); |
| 128 |
|
| 129 |
if ( $post_type != "attachment" ) { |
| 130 |
$query = "SELECT COUNT(DISTINCT({$post_table}.ID)) AS total_records FROM {$post_table} WHERE 1=1 AND (\n NOT EXISTS (\n SELECT 1\n FROM {$term_table}\n INNER JOIN {$term_taxonomy_table}\n ON {$term_taxonomy_table}.term_taxonomy_id = {$term_table}.term_taxonomy_id\n WHERE {$term_taxonomy_table}.taxonomy = '%s'\n AND {$term_table}.object_id = {$post_table}.ID\n )\n ) AND {$post_table}.post_type = '%s' AND (({$post_table}.post_status = 'publish' OR {$post_table}.post_status = 'future' OR {$post_table}.post_status = 'draft' OR {$post_table}.post_status = 'private'))"; |
| 131 |
} else { |
| 132 |
$query = "SELECT COUNT(DISTINCT({$post_table}.ID)) AS total_records FROM {$post_table} WHERE 1=1 AND (\n NOT EXISTS (\n SELECT 1\n FROM {$term_table}\n INNER JOIN {$term_taxonomy_table}\n ON {$term_taxonomy_table}.term_taxonomy_id = {$term_table}.term_taxonomy_id\n WHERE {$term_taxonomy_table}.taxonomy = '%s'\n AND {$term_table}.object_id = {$post_table}.ID\n )\n ) AND {$post_table}.post_type = '%s' AND {$post_table}.post_status = 'inherit'"; |
| 133 |
} |
| 134 |
|
| 135 |
$query = $wpdb->prepare( $query, $post_type_tax, $post_type ); |
| 136 |
$total = $wpdb->get_var( $query ); |
| 137 |
return ( empty($total) ? 0 : $total ); |
| 138 |
} |
| 139 |
|
| 140 |
public function get_folders( $post_type, $post_type_tax ) |
| 141 |
{ |
| 142 |
$folders = get_terms( $post_type_tax, array( |
| 143 |
'hide_empty' => false, |
| 144 |
'pad_counts' => true, |
| 145 |
) ); |
| 146 |
foreach ( $folders as $folder ) { |
| 147 |
$count = $this->items_in_taxonomy( $folder->term_id, $post_type, $post_type_tax ); |
| 148 |
$color = get_term_meta( $folder->term_id, '_wp_adminify_fodler_color', true ); |
| 149 |
if ( empty($color) ) { |
| 150 |
$color = ''; |
| 151 |
} |
| 152 |
$folder->color = $color; |
| 153 |
$folder->count = $count; |
| 154 |
} |
| 155 |
return $folders; |
| 156 |
} |
| 157 |
|
| 158 |
public function enqueue_scripts() |
| 159 |
{ |
| 160 |
if ( !$this->is_module_active() ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
$current_screen = get_current_screen(); |
| 164 |
wp_enqueue_style( |
| 165 |
'wp-adminify--folder', |
| 166 |
WP_ADMINIFY_URL . 'assets/admin/css/wp-adminify--folder.css', |
| 167 |
[], |
| 168 |
WP_ADMINIFY_VER |
| 169 |
); |
| 170 |
$post_type = $current_screen->post_type; |
| 171 |
$post_type_tax = self::get_post_type_taxonomy( $post_type ); |
| 172 |
$data = [ |
| 173 |
'adminurl' => admin_url(), |
| 174 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 175 |
'post_type' => $post_type, |
| 176 |
'nonce' => wp_create_nonce( '__adminify-folder-secirity__' ), |
| 177 |
'post_type_tax' => $post_type_tax, |
| 178 |
'is_pro' => wp_validate_boolean( jltwp_adminify()->can_use_premium_code__premium_only() ), |
| 179 |
'pro_notice' => Utils::adminify_upgrade_pro(), |
| 180 |
]; |
| 181 |
$data = array_merge( $data, $this->refreshed_folder_data( $post_type, $post_type_tax ) ); |
| 182 |
wp_localize_script( 'wp-adminify--folder', 'wp_adminify__folder_data', $data ); |
| 183 |
wp_enqueue_script( 'wp-adminify--folder' ); |
| 184 |
} |
| 185 |
|
| 186 |
public function handle_adminify_folder() |
| 187 |
{ |
| 188 |
check_ajax_referer( '__adminify-folder-secirity__' ); |
| 189 |
|
| 190 |
if ( !empty($_POST['route']) ) { |
| 191 |
$route_handler = 'handle_' . $_POST['route']; |
| 192 |
if ( is_callable( get_class( $this ), $route_handler ) ) { |
| 193 |
$this->{$route_handler}( $_POST ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
wp_send_json_error( [ |
| 198 |
'message' => __( 'Something is wrong, no route found' ), |
| 199 |
], 400 ); |
| 200 |
} |
| 201 |
|
| 202 |
public function handle_delete_folders( $data ) |
| 203 |
{ |
| 204 |
if ( empty($data['term_ids']) || empty($data['post_type']) || empty($data['post_type_tax']) ) { |
| 205 |
wp_send_json_error( [ |
| 206 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 207 |
], 400 ); |
| 208 |
} |
| 209 |
$term_ids = $data['term_ids']; |
| 210 |
$post_type = $data['post_type']; |
| 211 |
$post_type_tax = $data['post_type_tax']; |
| 212 |
foreach ( $term_ids as $term_id ) { |
| 213 |
wp_delete_term( $term_id, $post_type_tax ); |
| 214 |
} |
| 215 |
$data = $this->refreshed_folder_data( $post_type, $post_type_tax ); |
| 216 |
wp_send_json_success( $data ); |
| 217 |
} |
| 218 |
|
| 219 |
public function handle_rename_folder( $data ) |
| 220 |
{ |
| 221 |
if ( empty($data['term_id']) || empty($data['post_type']) || empty($data['post_type_tax']) || empty($data['folder_name']) || empty($data['folder_color_tag']) ) { |
| 222 |
wp_send_json_error( [ |
| 223 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 224 |
], 400 ); |
| 225 |
} |
| 226 |
$term_id = $data['term_id']; |
| 227 |
$folder_name = $data['folder_name']; |
| 228 |
$post_type = $data['post_type']; |
| 229 |
$post_type_tax = $data['post_type_tax']; |
| 230 |
$folder_color_tag = $data['folder_color_tag']; |
| 231 |
$update = wp_update_term( $term_id, $post_type_tax, [ |
| 232 |
'name' => $folder_name, |
| 233 |
] ); |
| 234 |
if ( is_wp_error( $update ) ) { |
| 235 |
wp_send_json_success( [ |
| 236 |
'message' => $update->get_error_message(), |
| 237 |
], 202 ); |
| 238 |
} |
| 239 |
update_term_meta( $term_id, '_wp_adminify_fodler_color', $folder_color_tag ); |
| 240 |
$data = $this->refreshed_folder_data( $post_type, $post_type_tax ); |
| 241 |
wp_send_json_success( $data ); |
| 242 |
} |
| 243 |
|
| 244 |
public function handle_move_to_folder( $data ) |
| 245 |
{ |
| 246 |
if ( empty($data['post_ids']) || empty($data['folder_id']) || empty($data['post_type']) || empty($data['post_type_tax']) ) { |
| 247 |
wp_send_json_error( [ |
| 248 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 249 |
], 400 ); |
| 250 |
} |
| 251 |
$post_ids = (array) $data['post_ids']; |
| 252 |
$folder_id = sanitize_text_field( $data['folder_id'] ); |
| 253 |
$post_type = sanitize_text_field( $data['post_type'] ); |
| 254 |
$post_type_tax = sanitize_text_field( $data['post_type_tax'] ); |
| 255 |
global $mode ; |
| 256 |
$mode = ( empty($data['mode']) ? 'list' : sanitize_text_field( $data['mode'] ) ); |
| 257 |
foreach ( $post_ids as $post_id ) { |
| 258 |
|
| 259 |
if ( $folder_id == 'uncategorized' ) { |
| 260 |
wp_set_object_terms( |
| 261 |
$post_id, |
| 262 |
'', |
| 263 |
$post_type_tax, |
| 264 |
false |
| 265 |
); |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
$term = get_term( $folder_id ); |
| 270 |
if ( !empty($term) && isset( $term->slug ) ) { |
| 271 |
wp_set_object_terms( |
| 272 |
$post_id, |
| 273 |
$term->slug, |
| 274 |
$post_type_tax, |
| 275 |
true |
| 276 |
); |
| 277 |
} |
| 278 |
} |
| 279 |
$updated_rows = ''; |
| 280 |
|
| 281 |
if ( $post_type == 'attachment' ) { |
| 282 |
global $wp_query ; |
| 283 |
$args = array( |
| 284 |
'posts_per_page' => count( $post_ids ), |
| 285 |
'orderby' => 'title', |
| 286 |
'order' => 'ASC', |
| 287 |
'post_type' => $post_type, |
| 288 |
'post_status' => 'any', |
| 289 |
'post__in' => $post_ids, |
| 290 |
); |
| 291 |
$wp_query = new \WP_Query( $args ); |
| 292 |
$wp_list_table = \_get_list_table( 'WP_Media_List_Table', array( |
| 293 |
'screen' => $_POST['screen'], |
| 294 |
) ); |
| 295 |
foreach ( $post_ids as $post_id ) { |
| 296 |
ob_start(); |
| 297 |
$wp_list_table->display_rows(); |
| 298 |
$updated_rows = ob_get_clean(); |
| 299 |
} |
| 300 |
} else { |
| 301 |
$wp_list_table = \_get_list_table( 'WP_Posts_List_Table', array( |
| 302 |
'screen' => $_POST['screen'], |
| 303 |
) ); |
| 304 |
foreach ( $post_ids as $post_id ) { |
| 305 |
$level = 0; |
| 306 |
|
| 307 |
if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) { |
| 308 |
$request_post = [ get_post( $post_id ) ]; |
| 309 |
$parent = $request_post[0]->post_parent; |
| 310 |
while ( $parent > 0 ) { |
| 311 |
$parent_post = get_post( $parent ); |
| 312 |
$parent = $parent_post->post_parent; |
| 313 |
$level++; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
ob_start(); |
| 318 |
$wp_list_table->display_rows( [ get_post( $post_id ) ], $level ); |
| 319 |
$updated_rows .= ob_get_clean(); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
$data = [ |
| 324 |
'updated_rows' => $updated_rows, |
| 325 |
]; |
| 326 |
$data = array_merge( $data, $this->refreshed_folder_data( $post_type, $post_type_tax ) ); |
| 327 |
wp_send_json_success( $data ); |
| 328 |
} |
| 329 |
|
| 330 |
public function handle_refresh_folders( $data ) |
| 331 |
{ |
| 332 |
if ( empty($data['post_type']) || empty($data['post_type_tax']) ) { |
| 333 |
wp_send_json_error( [ |
| 334 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 335 |
], 400 ); |
| 336 |
} |
| 337 |
$post_type = $data['post_type']; |
| 338 |
$post_type_tax = $data['post_type_tax']; |
| 339 |
wp_send_json_success( $this->refreshed_folder_data( $post_type, $post_type_tax ) ); |
| 340 |
} |
| 341 |
|
| 342 |
public function handle_create_new_folder( $data ) |
| 343 |
{ |
| 344 |
if ( empty($data['post_type']) || empty($data['post_type_tax']) ) { |
| 345 |
wp_send_json_error( [ |
| 346 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 347 |
], 400 ); |
| 348 |
} |
| 349 |
if ( empty($data['new_folder_name']) || empty($data['folder_color_tag']) ) { |
| 350 |
wp_send_json_error( [ |
| 351 |
'message' => __( 'Something is wrong, few args are missing' ), |
| 352 |
], 400 ); |
| 353 |
} |
| 354 |
$post_type = $data['post_type']; |
| 355 |
$post_type_tax = $data['post_type_tax']; |
| 356 |
$new_folder_name = $data['new_folder_name']; |
| 357 |
$folder_color_tag = $data['folder_color_tag']; |
| 358 |
$parent_term_id = 0; |
| 359 |
if ( jltwp_adminify()->can_use_premium_code__premium_only() && !empty($data['parent_folder']) ) { |
| 360 |
$parent_term_id = $data['parent_folder']; |
| 361 |
} |
| 362 |
$insert_data = wp_insert_term( $new_folder_name, $post_type_tax, [ |
| 363 |
'parent' => $parent_term_id, |
| 364 |
] ); |
| 365 |
|
| 366 |
if ( !is_wp_error( $insert_data ) ) { |
| 367 |
update_term_meta( $insert_data['term_id'], '_wp_adminify_fodler_color', $folder_color_tag ); |
| 368 |
wp_send_json_success( $this->refreshed_folder_data( $post_type, $post_type_tax ) ); |
| 369 |
} else { |
| 370 |
wp_send_json_success( [ |
| 371 |
'message' => $insert_data->get_error_message(), |
| 372 |
], 202 ); |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
function refreshed_folder_data( $post_type, $post_type_tax ) |
| 378 |
{ |
| 379 |
remove_filter( 'pre_get_posts', [ $this, 'filter_list' ] ); |
| 380 |
return [ |
| 381 |
'folders' => $this->get_folders( $post_type, $post_type_tax ), |
| 382 |
'folder_hierarchy' => _get_term_hierarchy( $post_type_tax ), |
| 383 |
'total_posts' => wp_count_posts( $post_type ), |
| 384 |
'total_uncat_posts' => $this->get_uncategorized_posts( $post_type ), |
| 385 |
]; |
| 386 |
} |
| 387 |
|
| 388 |
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) |
| 389 |
{ |
| 390 |
$post_ID = (int) $post_ID; |
| 391 |
$post_type = get_post_type( $post_ID ); |
| 392 |
$post_status = get_post_status( $post_ID ); |
| 393 |
// If $post_categories isn't already an array, make it one. |
| 394 |
$post_categories = (array) $post_categories; |
| 395 |
|
| 396 |
if ( empty($post_categories) ) { |
| 397 |
/** |
| 398 |
* Filters post types (in addition to 'post') that require a default category. |
| 399 |
* |
| 400 |
* @since 5.5.0 |
| 401 |
* |
| 402 |
* @param string[] $post_types An array of post type names. Default empty array. |
| 403 |
*/ |
| 404 |
$default_category_post_types = apply_filters( 'default_category_post_types', array() ); |
| 405 |
// Regular posts always require a default category. |
| 406 |
$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) ); |
| 407 |
|
| 408 |
if ( in_array( $post_type, $default_category_post_types, true ) && is_object_in_taxonomy( $post_type, 'category' ) && 'auto-draft' !== $post_status ) { |
| 409 |
$post_categories = array( get_option( 'default_category' ) ); |
| 410 |
$append = false; |
| 411 |
} else { |
| 412 |
$post_categories = array(); |
| 413 |
} |
| 414 |
|
| 415 |
} elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { |
| 416 |
return true; |
| 417 |
} |
| 418 |
|
| 419 |
return wp_set_post_terms( |
| 420 |
$post_ID, |
| 421 |
$post_categories, |
| 422 |
'category', |
| 423 |
$append |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
public function items_in_taxonomy( $term_id, $post_type, $taxonomy ) |
| 428 |
{ |
| 429 |
$posts = get_posts( array( |
| 430 |
'numberposts' => -1, |
| 431 |
'post_status' => 'any', |
| 432 |
'post_type' => $post_type, |
| 433 |
'fields' => 'ids', |
| 434 |
'tax_query' => array( array( |
| 435 |
'operator' => 'IN', |
| 436 |
'taxonomy' => $taxonomy, |
| 437 |
'field' => 'term_id', |
| 438 |
'terms' => $term_id, |
| 439 |
) ), |
| 440 |
) ); |
| 441 |
return count( $posts ); |
| 442 |
} |
| 443 |
|
| 444 |
public static function get_post_type_taxonomy( $post_type ) |
| 445 |
{ |
| 446 |
if ( $post_type == 'page' ) { |
| 447 |
return 'folder'; |
| 448 |
} |
| 449 |
if ( $post_type == 'attachment' ) { |
| 450 |
$post_type = 'media'; |
| 451 |
} |
| 452 |
return $post_type . '_folder'; |
| 453 |
} |
| 454 |
|
| 455 |
public function register_folders_terms() |
| 456 |
{ |
| 457 |
$post_types = $this->get_post_types(); |
| 458 |
foreach ( $post_types as $post_type ) { |
| 459 |
$labels = array( |
| 460 |
'name' => esc_html__( 'Folders', 'wp-adminify' ), |
| 461 |
'singular_name' => esc_html__( 'Folder', 'wp-adminify' ), |
| 462 |
'all_items' => esc_html__( 'All Folders', 'wp-adminify' ), |
| 463 |
'edit_item' => esc_html__( 'Edit Folder', 'wp-adminify' ), |
| 464 |
'update_item' => esc_html__( 'Update Folder', 'wp-adminify' ), |
| 465 |
'add_new_item' => esc_html__( 'Add New Folder', 'wp-adminify' ), |
| 466 |
'new_item_name' => esc_html__( 'Add folder name', 'wp-adminify' ), |
| 467 |
'menu_name' => esc_html__( 'Folders', 'wp-adminify' ), |
| 468 |
'search_items' => esc_html__( 'Search Folders', 'wp-adminify' ), |
| 469 |
'parent_item' => esc_html__( 'Parent Folder', 'wp-adminify' ), |
| 470 |
); |
| 471 |
$args = array( |
| 472 |
'label' => esc_html__( 'Folder', 'wp-adminify' ), |
| 473 |
'labels' => $labels, |
| 474 |
'show_tagcloud' => false, |
| 475 |
'hierarchical' => true, |
| 476 |
'public' => false, |
| 477 |
'show_ui' => false, |
| 478 |
'show_in_menu' => false, |
| 479 |
'show_in_rest' => true, |
| 480 |
'show_admin_column' => true, |
| 481 |
'query_var' => true, |
| 482 |
'rewrite' => false, |
| 483 |
'capabilities' => array( |
| 484 |
'manage_terms' => 'manage_categories', |
| 485 |
'edit_terms' => 'manage_categories', |
| 486 |
'delete_terms' => 'manage_categories', |
| 487 |
'assign_terms' => 'manage_categories', |
| 488 |
), |
| 489 |
); |
| 490 |
$taxonomy = self::get_post_type_taxonomy( $post_type ); |
| 491 |
register_taxonomy( $taxonomy, $post_type, $args ); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
public function modify_columns_actions() |
| 496 |
{ |
| 497 |
$post_types = $this->get_post_types(); |
| 498 |
foreach ( $post_types as $post_type ) { |
| 499 |
|
| 500 |
if ( $post_type == 'post' ) { |
| 501 |
add_filter( 'manage_edit-post_columns', [ $this, 'manage_columns_head' ] ); |
| 502 |
add_filter( 'manage_posts_columns', [ $this, 'manage_columns_head' ] ); |
| 503 |
add_action( |
| 504 |
'manage_posts_custom_column', |
| 505 |
[ $this, 'manage_columns_content' ], |
| 506 |
10, |
| 507 |
2 |
| 508 |
); |
| 509 |
add_filter( 'bulk_actions-edit-post', [ $this, 'custom_bulk_action' ] ); |
| 510 |
} else { |
| 511 |
|
| 512 |
if ( $post_type == 'page' ) { |
| 513 |
add_filter( 'manage_edit-page_columns', [ $this, 'manage_columns_head' ] ); |
| 514 |
add_filter( 'manage_page_posts_columns', [ $this, 'manage_columns_head' ] ); |
| 515 |
add_action( |
| 516 |
'manage_page_posts_custom_column', |
| 517 |
[ $this, 'manage_columns_content' ], |
| 518 |
10, |
| 519 |
2 |
| 520 |
); |
| 521 |
add_filter( 'bulk_actions-edit-page', [ $this, 'custom_bulk_action' ] ); |
| 522 |
} else { |
| 523 |
|
| 524 |
if ( $post_type == 'attachment' ) { |
| 525 |
add_filter( 'manage_media_columns', [ $this, 'manage_columns_head' ] ); |
| 526 |
add_action( |
| 527 |
'manage_media_custom_column', |
| 528 |
[ $this, 'manage_columns_content' ], |
| 529 |
10, |
| 530 |
2 |
| 531 |
); |
| 532 |
} else { |
| 533 |
add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'manage_columns_head' ], 99999 ); |
| 534 |
add_action( |
| 535 |
'manage_' . $post_type . '_posts_custom_column', |
| 536 |
[ $this, 'manage_columns_content' ], |
| 537 |
2, |
| 538 |
2 |
| 539 |
); |
| 540 |
add_filter( 'bulk_actions-edit-' . $post_type, [ $this, 'custom_bulk_action' ] ); |
| 541 |
} |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
} |
| 546 |
|
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
public static function sanitize_options( $value, $type = '' ) |
| 551 |
{ |
| 552 |
$value = stripslashes( $value ); |
| 553 |
if ( $type == "int" ) { |
| 554 |
return filter_var( $value, FILTER_SANITIZE_NUMBER_INT ); |
| 555 |
} |
| 556 |
if ( $type == "email" ) { |
| 557 |
return filter_var( $value, FILTER_SANITIZE_EMAIL ); |
| 558 |
} |
| 559 |
return filter_var( $value, FILTER_SANITIZE_STRING ); |
| 560 |
} |
| 561 |
|
| 562 |
function manage_columns_head( $posts_columns ) |
| 563 |
{ |
| 564 |
$post_type = null; |
| 565 |
if ( isset( $_REQUEST['post_type'] ) ) { |
| 566 |
$post_type = sanitize_text_field( $_REQUEST['post_type'] ); |
| 567 |
} |
| 568 |
if ( $this->is_module_active( $post_type ) ) { |
| 569 |
return [ |
| 570 |
'adminify_move' => '<div class="adminify-move-multiple adminify-col" title="' . esc_attr__( 'Move selected items', 'wp-adminify' ) . '"><span class="dashicons dashicons-move"></span><div class="adminify-items"></div></div>', |
| 571 |
] + $posts_columns; |
| 572 |
} |
| 573 |
return $posts_columns; |
| 574 |
} |
| 575 |
|
| 576 |
function manage_columns_content( $column_name, $post_ID ) |
| 577 |
{ |
| 578 |
$post_type = null; |
| 579 |
if ( isset( $_REQUEST['post_type'] ) ) { |
| 580 |
$post_type = sanitize_text_field( $_REQUEST['post_type'] ); |
| 581 |
} |
| 582 |
$postIDs = self::$postIds; |
| 583 |
if ( !is_array( $postIDs ) ) { |
| 584 |
$postIDs = array(); |
| 585 |
} |
| 586 |
|
| 587 |
if ( !in_array( $post_ID, $postIDs ) ) { |
| 588 |
$postIDs[] = $post_ID; |
| 589 |
self::$postIds = $postIDs; |
| 590 |
// global $typenow; |
| 591 |
// $type = $typenow; |
| 592 |
if ( $this->is_module_active( $post_type ) ) { |
| 593 |
|
| 594 |
if ( $column_name == 'adminify_move' ) { |
| 595 |
$title = get_the_title(); |
| 596 |
if ( strlen( $title ) > 20 ) { |
| 597 |
$title = substr( $title, 0, 20 ) . "..."; |
| 598 |
} |
| 599 |
echo "<div class='adminify-move-file' data-id='{$post_ID}'><span class='adminify-move dashicons dashicons-move' data-id='{$post_ID}'></span><span class='adminify-move-file--title'>{$title}</span></div>" ; |
| 600 |
} |
| 601 |
|
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
} |
| 606 |
|
| 607 |
public function custom_bulk_action( $bulk_actions ) |
| 608 |
{ |
| 609 |
$bulk_actions['move_to_folder'] = __( 'Move to Folder', 'wp-adminify' ); |
| 610 |
return $bulk_actions; |
| 611 |
} |
| 612 |
|
| 613 |
} |