| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Classes; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Admin\AdminSettings; |
| 6 |
use WPAdminify\Inc\Admin\AdminSettingsModel; |
| 7 |
|
| 8 |
// no direct access allowed |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Post Columns: Featured Image and ID |
| 15 |
* |
| 16 |
* @package WP Adminify |
| 17 |
* |
| 18 |
* @author WP Adminify <support@wpadminify.com> |
| 19 |
*/ |
| 20 |
|
| 21 |
class CustomAdminColumns extends AdminSettingsModel { |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
$this->options = (array) AdminSettings::get_instance()->get(); |
| 25 |
add_action( 'admin_init', [ $this, 'adminify_taxonomy_id_column' ] ); |
| 26 |
add_action( 'admin_init', [ $this, 'adminify_comments_id_column' ] ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Taxonomy ID Column |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function adminify_taxonomy_id_column() { |
| 35 |
// If not true then return |
| 36 |
if ( empty( $this->options['taxonomy_id_column'] ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
// Restrict the custom column to specific tax_types |
| 40 |
$tax_types = [ |
| 41 |
'category', |
| 42 |
'post_tag', |
| 43 |
'product_cat', |
| 44 |
'product_tag', |
| 45 |
'page_category', |
| 46 |
'page_tag', |
| 47 |
'recipe_category', |
| 48 |
'recipe_tag', |
| 49 |
'recipe_ingredient', |
| 50 |
'recipe_feature', |
| 51 |
'recipe_cuisine', |
| 52 |
'portfolio_category', |
| 53 |
'portfolio_tag', |
| 54 |
'portfolio_client', |
| 55 |
]; |
| 56 |
|
| 57 |
if ( empty( $tax_types ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Add custom column filter and action |
| 62 |
foreach ( $tax_types as $taxonomy ) { |
| 63 |
add_action( "manage_edit-{$taxonomy}_columns", [ $this, 'adminify_taxonomy_id_column_head' ] ); |
| 64 |
add_filter( "manage_edit-{$taxonomy}_sortable_columns", [ $this, 'adminify_taxonomy_id_column_head' ] ); |
| 65 |
add_filter( "manage_{$taxonomy}_custom_column", [ $this, 'adminify_taxonomy_id_column_content' ], 11, 3 ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Taxonomy ID Head |
| 71 |
* |
| 72 |
* @param [type] $column |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function adminify_taxonomy_id_column_head( $column ) { |
| 77 |
$column['tax_id'] = esc_html__( 'ID', 'adminify' ); |
| 78 |
return $column; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Taxonomy Column Content |
| 83 |
* |
| 84 |
* @param [type] $value |
| 85 |
* @param [type] $name |
| 86 |
* @param [type] $id |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
function adminify_taxonomy_id_column_content( $value, $name, $id ) { |
| 91 |
return 'tax_id' === $name ? $id : $value; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Comment ID Columns |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function adminify_comments_id_column() { |
| 101 |
// If not true then return |
| 102 |
if ( empty( $this->options['comment_id_column'] ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
add_filter( 'manage_edit-comments_columns', [ $this, 'adminify_add_comments_columns' ] ); |
| 106 |
add_action( 'manage_comments_custom_column', [ $this, 'adminify_add_comment_columns_content' ], 10, 2 ); |
| 107 |
} |
| 108 |
|
| 109 |
public function adminify_add_comments_columns( $columns ) { |
| 110 |
$comment_columns = [ |
| 111 |
'adminify_comment_id' => __( 'ID', 'adminify' ), |
| 112 |
'adminify_parent_id' => __( 'Parent ID', 'adminify' ), |
| 113 |
]; |
| 114 |
$columns = array_slice( $columns, 0, 3, true ) + $comment_columns + array_slice( $columns, 3, null, true ); |
| 115 |
// return the result |
| 116 |
return $columns; |
| 117 |
} |
| 118 |
|
| 119 |
public function adminify_add_comment_columns_content( $column, $comment_ID ) { |
| 120 |
global $comment; |
| 121 |
switch ( $column ) : |
| 122 |
case 'adminify_comment_id': |
| 123 |
echo esc_html( $comment_ID ); // or echo $comment->comment_ID. |
| 124 |
break; |
| 125 |
case 'adminify_parent_id': |
| 126 |
echo esc_html( $comment->comment_parent ); // this will be printed inside the column |
| 127 |
break; |
| 128 |
endswitch; |
| 129 |
} |
| 130 |
} |
| 131 |
|