| 1 |
<?php |
| 2 |
|
| 3 |
class PostLockdown_StatusColumn { |
| 4 |
const COLUMN_KEY = 'postlockdown_status'; |
| 5 |
|
| 6 |
/** @var PostLockdown */ |
| 7 |
private $postlockdown; |
| 8 |
|
| 9 |
public function __construct( PostLockdown $postlockdown ) { |
| 10 |
$this->postlockdown = $postlockdown; |
| 11 |
add_action( 'admin_init', array( $this, '_set_post_type_hooks' ) ); |
| 12 |
add_action( 'admin_head', array( $this, '_column_output_style' ) ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Callback for the 'admin_init' hook. |
| 17 |
* |
| 18 |
*/ |
| 19 |
public function _set_post_type_hooks() { |
| 20 |
$column_hidden = apply_filters( 'postlockdown_column_hidden_default', true ); |
| 21 |
|
| 22 |
foreach ( $this->postlockdown->get_post_types() as $post_type ) { |
| 23 |
add_filter( "manage_{$post_type}_posts_columns", array( $this, '_column_add' ) ); |
| 24 |
add_action( "manage_{$post_type}_posts_custom_column", array( $this, '_column_output' ), 10, 2 ); |
| 25 |
|
| 26 |
if ( $column_hidden ) { |
| 27 |
/* |
| 28 |
* Credit: Yoast SEO |
| 29 |
* |
| 30 |
* Use the `get_user_option_{$option}` filter to change the output of the get_user_option |
| 31 |
* function for the `manage{$screen}columnshidden` option, which is based on the current |
| 32 |
* admin screen. The admin screen we want to target is the `edit-{$post_type}` screen. |
| 33 |
*/ |
| 34 |
$filter = sprintf( 'get_user_option_%s', sprintf( 'manage%scolumnshidden', 'edit-' . $post_type ) ); |
| 35 |
add_filter( $filter, array( $this, '_column_hidden' ), 10, 3 ); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Filter for all 'get_user_option_manageedit-{$post_type}columnshidden' hooks |
| 42 |
* added in the _set_post_type_hooks() method |
| 43 |
* @see PostLockdown_StatusColumn::_set_post_type_hooks() |
| 44 |
* |
| 45 |
* Hides the status column for the user if they haven't already hidden any columns |
| 46 |
* on the current screen. |
| 47 |
* |
| 48 |
* @param $result |
| 49 |
* @param $option |
| 50 |
* @param WP_User $user |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function _column_hidden( $result, $option, $user ) { |
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
$prefix = $wpdb->get_blog_prefix(); |
| 58 |
if ( ! $user->has_prop( $prefix . $option ) && ! $user->has_prop( $option ) ) { |
| 59 |
if ( ! is_array( $result ) ) { |
| 60 |
$result = array(); |
| 61 |
} |
| 62 |
|
| 63 |
$result[] = self::COLUMN_KEY; |
| 64 |
} |
| 65 |
|
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Filter for the manage_{$post_type}_posts_columns hook. |
| 71 |
* |
| 72 |
* Adds the plugin's status column to all post list tables. |
| 73 |
* |
| 74 |
* @param array $columns |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function _column_add( $columns ) { |
| 79 |
$label = apply_filters( 'postlockdown_column_label', 'Post Lockdown' ); |
| 80 |
|
| 81 |
$new_columns = array(); |
| 82 |
$column_added = false; |
| 83 |
|
| 84 |
foreach ( $columns as $key => $column ) { |
| 85 |
$new_columns[ $key ] = $column; |
| 86 |
|
| 87 |
if ( ! $column_added && ( 'title' === $key || 'name' === $key ) ) { |
| 88 |
$new_columns[ self::COLUMN_KEY ] = $label; |
| 89 |
|
| 90 |
$column_added = true; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! $column_added ) { |
| 95 |
$new_columns[ self::COLUMN_KEY ] = $label; |
| 96 |
} |
| 97 |
|
| 98 |
return $new_columns; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Callback for the manage_{$post_type}_posts_custom_column hook. |
| 103 |
* |
| 104 |
* Prints the relevant output, if any, for the status column. |
| 105 |
* |
| 106 |
* @param string $column |
| 107 |
* @param int $post_id |
| 108 |
*/ |
| 109 |
public function _column_output( $column, $post_id ) { |
| 110 |
if ( self::COLUMN_KEY !== $column ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$html = ''; |
| 115 |
$status = false; |
| 116 |
if ( $this->postlockdown->is_post_locked( $post_id ) ) { |
| 117 |
$html = '<span title="Locked - Cannot be edited, trashed or deleted" class="dashicons dashicons-lock"></span> Locked'; |
| 118 |
$status = 'locked'; |
| 119 |
} elseif ( $this->postlockdown->is_post_protected( $post_id ) ) { |
| 120 |
$html = '<span title="Protected - Cannot be trashed or deleted" class="dashicons dashicons-unlock"></span> Protected'; |
| 121 |
$status = 'protected'; |
| 122 |
} |
| 123 |
|
| 124 |
if ( false !== $status ) { |
| 125 |
$html = apply_filters( 'postlockdown_column_html', $html, $status, $post_id ); |
| 126 |
echo $html; // xss ok |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function _column_output_style() { |
| 131 |
global $pagenow; |
| 132 |
|
| 133 |
if ( 'edit.php' !== $pagenow ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
?> |
| 138 |
<style id="postlockdown-column-styles"> |
| 139 |
.fixed .column-postlockdown_status { |
| 140 |
width: 10%; |
| 141 |
} |
| 142 |
|
| 143 |
.fixed td.column-postlockdown_status .dashicons { |
| 144 |
color: #444; |
| 145 |
font-size: 22px; |
| 146 |
} |
| 147 |
</style> |
| 148 |
<?php |
| 149 |
} |
| 150 |
} |
| 151 |
|