| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Admin_Client_Columns |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
use UpStream\Plugins\CustomFields\Model; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'UpStream_Admin_Client_Columns' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Admin columns |
| 19 |
* |
| 20 |
* @version 0.1.0 |
| 21 |
*/ |
| 22 |
class UpStream_Admin_Client_Columns { |
| 23 |
|
| 24 |
/** |
| 25 |
* Label |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
private $label; |
| 30 |
|
| 31 |
/** |
| 32 |
* Label Plural |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
private $label_plural; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor |
| 40 |
* |
| 41 |
* @since 0.1.0 |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
$this->label = upstream_client_label(); |
| 45 |
$this->label_plural = upstream_client_label_plural(); |
| 46 |
|
| 47 |
return $this->hooks(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Hooks |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function hooks() { |
| 56 |
add_filter( 'manage_client_posts_columns', array( $this, 'client_columns' ) ); |
| 57 |
add_action( 'manage_client_posts_custom_column', array( $this, 'client_data' ), 10, 2 ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set columns for client |
| 62 |
* |
| 63 |
* @param mixed $defaults Defaults. |
| 64 |
*/ |
| 65 |
public function client_columns( $defaults ) { |
| 66 |
$post_type = 'client'; |
| 67 |
|
| 68 |
$columns = array(); |
| 69 |
$taxonomies = array(); |
| 70 |
|
| 71 |
/* Get taxonomies that should appear in the manage posts table. */ |
| 72 |
$taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 73 |
$taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' ); |
| 74 |
|
| 75 |
/* Allow devs to filter the taxonomy columns. */ |
| 76 |
$taxonomies = apply_filters( |
| 77 |
'manage_taxonomies_for_upstream_client_columns', |
| 78 |
$taxonomies, |
| 79 |
$post_type |
| 80 |
); |
| 81 |
$taxonomies = array_filter( $taxonomies, 'taxonomy_exists' ); |
| 82 |
|
| 83 |
/* Loop through each taxonomy and add it as a column. */ |
| 84 |
foreach ( $taxonomies as $taxonomy ) { |
| 85 |
$columns[ 'taxonomy-' . $taxonomy ] = get_taxonomy( $taxonomy )->labels->name; |
| 86 |
} |
| 87 |
$defaults['title'] = $this->label; |
| 88 |
$defaults['id'] = __( 'ID', 'upstream' ); |
| 89 |
$defaults['logo'] = __( 'Logo', 'upstream' ); |
| 90 |
$defaults['website'] = __( 'Website', 'upstream' ); |
| 91 |
$defaults['phone'] = __( 'Phone', 'upstream' ); |
| 92 |
$defaults['address'] = __( 'Address', 'upstream' ); |
| 93 |
$defaults['users'] = __( 'Users', 'upstream' ); |
| 94 |
|
| 95 |
if ( is_plugin_active( 'upstream-custom-fields/upstream-custom-fields.php' ) || |
| 96 |
is_plugin_active( 'UpStream-Custom-Fields/upstream-custom-fields.php' ) |
| 97 |
) { |
| 98 |
$rowset = Model::fetchColumnFieldsForType( 'client', false ); |
| 99 |
|
| 100 |
if ( count( $rowset ) > 0 ) { |
| 101 |
foreach ( $rowset as $row ) { |
| 102 |
$defaults[ $row->name ] = $row->label; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $defaults; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Client Data |
| 112 |
* |
| 113 |
* @param string $column_name Column Name. |
| 114 |
* @param int $post_id Post Id. |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function client_data( $column_name, $post_id ) { |
| 118 |
$client = new UpStream_Client( $post_id ); |
| 119 |
$column_value = null; |
| 120 |
|
| 121 |
if ( 'logo' === $column_name ) { |
| 122 |
$logo_id = $client->get_meta( 'logo_id' ); |
| 123 |
if ( ! empty( $logo_id ) ) { |
| 124 |
$logo_img_url = wp_get_attachment_image_src( $logo_id ); |
| 125 |
$column_value = '<img height="50" src="' . esc_url( $logo_img_url[0] ) . '" />'; |
| 126 |
} |
| 127 |
} elseif ( 'id' === $column_name ) { |
| 128 |
$column_value = $post_id; |
| 129 |
} elseif ( 'website' === $column_name ) { |
| 130 |
$website = $client->get_meta( 'website' ); |
| 131 |
if ( ! empty( $website ) ) { |
| 132 |
$column_value = '<a href="' . esc_url( $website ) . '" target="_blank" rel="noopener noreferer">' . esc_html( $website ) . '</a>'; |
| 133 |
} |
| 134 |
} elseif ( 'phone' === $column_name ) { |
| 135 |
$phone = $client->get_meta( 'phone' ); |
| 136 |
if ( ! empty( $phone ) ) { |
| 137 |
$column_value = esc_html( $phone ); |
| 138 |
} |
| 139 |
} elseif ( 'address' === $column_name ) { |
| 140 |
$address = $client->get_meta( 'address' ); |
| 141 |
if ( ! empty( $address ) ) { |
| 142 |
$column_value = wp_kses_post( wpautop( $address ) ); |
| 143 |
} |
| 144 |
} elseif ( 'users' === $column_name ) { |
| 145 |
$client_users = (array) upstream_get_client_users( $post_id ); |
| 146 |
if ( count( $client_users ) > 0 ) { |
| 147 |
upstream_client_render_users_column( upstream_get_client_users( $post_id ) ); |
| 148 |
|
| 149 |
return; |
| 150 |
} |
| 151 |
} else { |
| 152 |
if ( is_plugin_active( 'upstream-custom-fields/upstream-custom-fields.php' ) || |
| 153 |
is_plugin_active( 'UpStream-Custom-Fields/upstream-custom-fields.php' ) ) { |
| 154 |
$rowset = Model::fetchColumnFieldsForType( 'client', false ); |
| 155 |
|
| 156 |
if ( count( $rowset ) > 0 ) { |
| 157 |
foreach ( $rowset as $row ) { |
| 158 |
if ( $row->name === $column_name ) { |
| 159 |
$values = array_filter( (array) $row->getValue( $post_id ) ); |
| 160 |
$column_value = esc_html( implode( ', ', $values ) ); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if ( ! empty( $column_value ) ) { |
| 169 |
echo wp_kses_post( $column_value ); |
| 170 |
} else { |
| 171 |
echo wp_kses_post( |
| 172 |
'<i style="color: #CCC;">' . __( |
| 173 |
'none', |
| 174 |
'upstream' |
| 175 |
) . '</i>' |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
new UpStream_Admin_Client_Columns(); |
| 182 |
|
| 183 |
endif; |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Renders the client users list column value. |
| 188 |
* |
| 189 |
* @since 1.0.0 |
| 190 |
* |
| 191 |
* @param array $users_list Array of client users. |
| 192 |
*/ |
| 193 |
function upstream_client_render_users_column( $users_list ) { |
| 194 |
$users_list = (array) $users_list; |
| 195 |
$users_list_count = count( $users_list ); |
| 196 |
|
| 197 |
if ( 0 === $users_list_count ) { |
| 198 |
echo '<i>' . esc_html__( 'none', 'upstream' ) . '</i>'; |
| 199 |
} else { |
| 200 |
$user_index = 0; |
| 201 |
foreach ( $users_list as $user ) { |
| 202 |
echo esc_html( $user['name'] ) . '<br/>'; |
| 203 |
|
| 204 |
if ( 2 === $user_index ) { |
| 205 |
echo sprintf( |
| 206 |
'<i>' . |
| 207 |
// translators: %1$s: Users List Count. |
| 208 |
// translators: %2$s: User label. |
| 209 |
esc_html__( '+%1$s more %2$s', 'upstream' ) |
| 210 |
. '</i>', |
| 211 |
esc_html( $users_list_count ), |
| 212 |
esc_html( $users_list_count > 1 ? __( 'users', 'upstream' ) : __( 'user', 'upstream' ) ) |
| 213 |
); |
| 214 |
break; |
| 215 |
} |
| 216 |
$user_index++; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|