Last active
April 7, 2020 20:14
-
-
Save thefuxia/8e94ebb272d35d1dc54c to your computer and use it in GitHub Desktop.
Plugin Product Editor Column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: Product Editor Column | |
* Description: Adds a column to the products table backend with the name of the last editor. Requires at least PHP 5.4. | |
* Author: Fuxia | |
* Version: 2014.08.09 | |
* Licence: MIT | |
* Plugin URI: http://wordpress.stackexchange.com/a/157258/73 | |
*/ | |
namespace WPSE; | |
interface Column_Data | |
{ | |
/** | |
* @param array $columns | |
* @return array | |
*/ | |
public function add_column( Array $columns ); | |
/** | |
* @param int $post_id | |
* @return string | |
*/ | |
public function get_column_content( $post_id ); | |
/** | |
* @return string | |
*/ | |
public function get_post_type(); | |
/** | |
* @param $column_name | |
* @return bool | |
*/ | |
public function is_valid_column( $column_name ); | |
} | |
class Last_Mod_Author_Column_Data implements Column_Data | |
{ | |
/** | |
* @var string | |
*/ | |
private $post_type; | |
/** | |
* @var string | |
*/ | |
private $column_name = 'modified_author'; | |
/** | |
* @param string $post_type | |
*/ | |
public function __construct( $post_type ) | |
{ | |
$this->post_type = $post_type; | |
} | |
/** | |
* @param array $columns | |
* @return array | |
*/ | |
public function add_column( Array $columns ) | |
{ | |
$columns[ $this->column_name ] = 'Last modified by'; | |
return $columns; | |
} | |
/** | |
* @param int $post_id | |
* @return string | |
*/ | |
public function get_column_content( $post_id ) | |
{ | |
$last_id = get_post_meta( $post_id, '_edit_last', TRUE ); | |
if ( ! $last_id ) { | |
return ''; | |
} | |
$last_user = get_userdata( $last_id ); | |
return $last_user->display_name; | |
} | |
/** | |
* @return string | |
*/ | |
public function get_post_type() | |
{ | |
return $this->post_type; | |
} | |
/** | |
* @param $column_name | |
* @return bool | |
*/ | |
public function is_valid_column( $column_name ) | |
{ | |
return $this->column_name === $column_name; | |
} | |
} | |
interface Column_View | |
{ | |
/** | |
* @param string $column_name | |
* @param int $post_id | |
* @return void | |
*/ | |
public function render_column( $column_name, $post_id ); | |
} | |
class Last_Mod_Author_Column_Output implements Column_View | |
{ | |
/** | |
* @var Column_Data | |
*/ | |
private $data; | |
/** | |
* @param Column_Data $data | |
*/ | |
public function __construct( Column_Data $data ) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* @param string $column_name | |
* @param int $post_id | |
* @return void | |
*/ | |
public function render_column( $column_name, $post_id ) | |
{ | |
if ( ! $this->data->is_valid_column( $column_name ) ) | |
return; | |
$content = $this->data->get_column_content( $post_id ); | |
if ( '' === $content ) | |
print '<i>Unknown</i>'; | |
else | |
print esc_html( $content ); | |
} | |
} | |
class Controller | |
{ | |
/** | |
* @var Column_Data | |
*/ | |
private $data; | |
/** | |
* @var Column_View | |
*/ | |
private $view; | |
/** | |
* @param Column_Data $data | |
* @param Column_View $view | |
*/ | |
public function __construct( Column_Data $data, Column_View $view ) { | |
$this->data = $data; | |
$this->view = $view; | |
} | |
/** | |
* @return void | |
*/ | |
public function setup() | |
{ | |
$screen = get_current_screen(); | |
$post_type = $this->data->get_post_type(); | |
if ( ! isset ( $screen->id ) ) | |
return; | |
if ( "edit-$post_type" !== $screen->id ) | |
return; | |
add_filter( | |
"manage_{$post_type}_posts_columns", | |
[ $this->data, 'add_column' ] | |
); | |
add_action( | |
"manage_{$post_type}_posts_custom_column", | |
[ $this->view, 'render_column' ], | |
10, 2 | |
); | |
} | |
} | |
add_action( 'load-edit.php', function() { | |
$model = new Last_Mod_Author_Column_Data( 'product' ); | |
$view = new Last_Mod_Author_Column_Output( $model ); | |
$controller = new Controller( $model, $view ); | |
$controller->setup(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment