| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataProjects\List_Table |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataProjects\List_Table { |
| 10 |
|
| 11 |
/** |
| 12 |
* Class WPDP_List_Table |
| 13 |
* |
| 14 |
* Overwrites WPDP_List_Table_Lookup. Disables insert, update, delete and import depending on given arguments. |
| 15 |
* |
| 16 |
* @see WPDP_List_Table_Lookup |
| 17 |
* |
| 18 |
* @author Peter Schulz |
| 19 |
* @since 2.0.0 |
| 20 |
*/ |
| 21 |
class WPDP_List_Table extends WPDP_List_Table_Lookup { |
| 22 |
|
| 23 |
protected $image_columns = array(); |
| 24 |
protected $attachment_columns = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* WPDP_List_Table constructor |
| 28 |
* |
| 29 |
* @param array $args See WPDA_List_Table for argument list |
| 30 |
*/ |
| 31 |
public function __construct( array $args = array() ) { |
| 32 |
if ( isset( $args['where_clause'] ) && '' !== $args['where_clause'] ) { |
| 33 |
$this->where = $args['where_clause']; |
| 34 |
} |
| 35 |
|
| 36 |
if ( isset( $args['orderby_clause'] ) && '' !== $args['orderby_clause'] ) { |
| 37 |
$this->orderby = $args['orderby_clause']; |
| 38 |
} |
| 39 |
|
| 40 |
$args['pid'] = isset( $args['pid'] ) ? $args['pid'] : ''; |
| 41 |
|
| 42 |
parent::__construct( $args ); |
| 43 |
|
| 44 |
// Save image and attachment items in named array for quick access |
| 45 |
if ( null != $this->column_options_listtable ) { |
| 46 |
foreach ( $this->column_options_listtable as $column_option ) { |
| 47 |
if ( isset( $column_option->item_type ) ) { |
| 48 |
if ( 'image' === $column_option->item_type ) { |
| 49 |
$this->image_columns[ $column_option->column_name ] = true; |
| 50 |
} |
| 51 |
if ( 'attachment' === $column_option->item_type ) { |
| 52 |
$this->attachment_columns[ $column_option->column_name ] = true; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Overwrite method to process images and attachments |
| 61 |
* |
| 62 |
* @param array $item |
| 63 |
* @param string $column_name |
| 64 |
* |
| 65 |
* @return mixed|string |
| 66 |
*/ |
| 67 |
public function column_default( $item, $column_name ) { |
| 68 |
// Is item an image? |
| 69 |
if ( isset( $this->image_columns[ $column_name ] ) ) { |
| 70 |
if ( null === $item[ $column_name ] || '' === $item[ $column_name ] ) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$image_ids = explode( ',', $item[ $column_name ] ); // phpcs:ignore -- 8.1 proof |
| 75 |
$image_src = ''; |
| 76 |
|
| 77 |
foreach ( $image_ids as $image_id ) { |
| 78 |
$url = wp_get_attachment_url( esc_attr( $image_id ) ); |
| 79 |
if ( false !== $url ) { |
| 80 |
$image_src .= '' !== $image_src ? '<br/>' : ''; |
| 81 |
$image_src .= sprintf( '<img src="%s" width="100%%">', esc_url_raw( $url ) ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $image_src; |
| 86 |
} |
| 87 |
|
| 88 |
// Is item an attachment? |
| 89 |
if ( isset( $this->attachment_columns[ $column_name ] ) ) { |
| 90 |
if ( null === $item[ $column_name ] || '' === $item[ $column_name ] ) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
$media_ids = explode( ',', $item[ $column_name ] ); // phpcs:ignore -- 8.1 proof |
| 95 |
$media_links = ''; |
| 96 |
|
| 97 |
foreach ( $media_ids as $media_id ) { |
| 98 |
$url = wp_get_attachment_url( esc_attr( $media_id ) ); |
| 99 |
if ( false !== $url ) { |
| 100 |
$mime_type = get_post_mime_type( $media_id ); |
| 101 |
if ( false !== $mime_type ) { |
| 102 |
$mime_ext = strpos( $mime_type, '/' ); |
| 103 |
if ( false !== $mime_ext ) { |
| 104 |
$mime_type = substr( $mime_type, $mime_ext + 1 ); |
| 105 |
} |
| 106 |
$title = get_the_title( esc_attr( $media_id ) ); |
| 107 |
$media_links .= '' !== $media_links ? '<br/>' : ''; |
| 108 |
$media_links .= sprintf( '<span class="dashicons dashicons-external"></span><a href="%s" title="%s" class="wpda_tooltip" target="_blank">%s</a>', esc_url_raw( $url ), esc_attr( $title ), esc_html( $mime_type ) ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $media_links; |
| 114 |
} |
| 115 |
|
| 116 |
return parent::column_default( $item, $column_name ); |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|