comment
10 years ago
link
10 years ago
media
10 years ago
post
10 years ago
user
10 years ago
acf-placeholder.php
10 years ago
actions.php
10 years ago
custom-field.php
10 years ago
default.php
10 years ago
taxonomy.php
10 years ago
used-by-menu.php
10 years ago
wc-placeholder.php
10 years ago
custom-field.php
462 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom field column, displaying the contents of meta fields. |
| 4 | * Suited for all storage models supporting WordPress' default way of handling meta data. |
| 5 | * |
| 6 | * Supports different types of meta fields, including dates, serialized data, linked content, |
| 7 | * and boolean values. |
| 8 | * |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | class CPAC_Column_Custom_Field extends CPAC_Column { |
| 12 | |
| 13 | /** |
| 14 | * @see CPAC_Column::init() |
| 15 | * @since 2.2.1 |
| 16 | */ |
| 17 | public function init() { |
| 18 | |
| 19 | parent::init(); |
| 20 | |
| 21 | // Properties |
| 22 | $this->properties['type'] = 'column-meta'; |
| 23 | $this->properties['label'] = __( 'Custom Field', 'codepress-admin-columns' ); |
| 24 | $this->properties['classes'] = 'cpac-box-metafield'; |
| 25 | $this->properties['is_cloneable'] = true; |
| 26 | $this->properties['group'] = 'custom-field'; |
| 27 | |
| 28 | // Options |
| 29 | $this->options['field'] = ''; |
| 30 | $this->options['field_type'] = ''; |
| 31 | $this->options['before'] = ''; |
| 32 | $this->options['after'] = ''; |
| 33 | |
| 34 | $this->options['image_size'] = ''; |
| 35 | $this->options['image_size_w'] = 80; |
| 36 | $this->options['image_size_h'] = 80; |
| 37 | |
| 38 | $this->options['excerpt_length'] = 15; |
| 39 | |
| 40 | $this->options['date_format'] = ''; |
| 41 | $this->options['date_save_format'] = ''; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @since 3.2.1 |
| 46 | */ |
| 47 | public function is_field_type( $type ) { |
| 48 | return $type === $this->get_field_type(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @since 3.2.1 |
| 53 | */ |
| 54 | public function is_field( $field ) { |
| 55 | return $field === $this->get_field(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 3.2.1 |
| 60 | */ |
| 61 | public function get_field_type() { |
| 62 | return $this->options->field_type; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @since 3.2.1 |
| 67 | */ |
| 68 | public function get_field() { |
| 69 | return $this->get_field_key(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @see CPAC_Column::sanitize_options() |
| 74 | * @since 1.0 |
| 75 | */ |
| 76 | public function sanitize_options( $options ) { |
| 77 | |
| 78 | if ( empty( $options['date_format'] ) ) { |
| 79 | $options['date_format'] = get_option( 'date_format' ); |
| 80 | } |
| 81 | |
| 82 | return $options; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get Custom FieldType Options - Value method |
| 87 | * |
| 88 | * @since 1.0 |
| 89 | * |
| 90 | * @return array Customfield types. |
| 91 | */ |
| 92 | public function get_custom_field_types() { |
| 93 | |
| 94 | $custom_field_types = array( |
| 95 | '' => __( 'Default', 'codepress-admin-columns' ), |
| 96 | 'checkmark' => __( 'Checkmark (true/false)', 'codepress-admin-columns' ), |
| 97 | 'color' => __( 'Color', 'codepress-admin-columns' ), |
| 98 | 'count' => __( 'Counter', 'codepress-admin-columns' ), |
| 99 | 'date' => __( 'Date', 'codepress-admin-columns' ), |
| 100 | 'excerpt' => __( 'Excerpt'), |
| 101 | 'image' => __( 'Image', 'codepress-admin-columns' ), |
| 102 | 'library_id' => __( 'Media Library', 'codepress-admin-columns' ), |
| 103 | 'array' => __( 'Multiple Values', 'codepress-admin-columns' ), |
| 104 | 'numeric' => __( 'Numeric', 'codepress-admin-columns' ), |
| 105 | 'title_by_id' => __( 'Post Title (Post ID\'s)', 'codepress-admin-columns' ), |
| 106 | 'user_by_id' => __( 'Username (User ID\'s)', 'codepress-admin-columns' ), |
| 107 | 'term_by_id' => __( 'Term Name (Term ID\'s)', 'codepress-admin-columns' ), |
| 108 | ); |
| 109 | |
| 110 | // deprecated. do not use, will be removed. |
| 111 | $custom_field_types = apply_filters( 'cpac_custom_field_types', $custom_field_types ); |
| 112 | |
| 113 | /** |
| 114 | * Filter the available custom field types for the meta (custom field) field |
| 115 | * |
| 116 | * @since 2.0 |
| 117 | * |
| 118 | * @param array $custom_field_types Available custom field types ([type] => [label]) |
| 119 | */ |
| 120 | $custom_field_types = apply_filters( 'cac/column/meta/types', $custom_field_types ); |
| 121 | |
| 122 | return $custom_field_types; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get First ID from array |
| 127 | * |
| 128 | * @since 1.0 |
| 129 | * |
| 130 | * @param string $meta |
| 131 | * @return string Titles |
| 132 | */ |
| 133 | public function get_ids_from_meta( $meta ) { |
| 134 | |
| 135 | //remove white spaces and strip tags |
| 136 | $meta = $this->strip_trim( str_replace( ' ','', $meta ) ); |
| 137 | |
| 138 | // var |
| 139 | $ids = array(); |
| 140 | |
| 141 | // check for multiple id's |
| 142 | if ( strpos( $meta, ',' ) !== false ) { |
| 143 | $ids = explode( ',', $meta ); |
| 144 | } |
| 145 | elseif ( is_numeric( $meta ) ) { |
| 146 | $ids[] = $meta; |
| 147 | } |
| 148 | |
| 149 | return $ids; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get Title by ID - Value method |
| 154 | * |
| 155 | * @since 1.0 |
| 156 | * |
| 157 | * @param string $meta |
| 158 | * @return string Titles |
| 159 | */ |
| 160 | private function get_titles_by_id( $ids ) { |
| 161 | |
| 162 | $titles = array(); |
| 163 | |
| 164 | // display title with link |
| 165 | if ( $ids = $this->get_ids_from_meta( $ids ) ) { |
| 166 | foreach ( (array) $ids as $id ) { |
| 167 | |
| 168 | if ( ! is_numeric( $id ) ) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | if ( $title = $this->get_post_title( $id ) ) { |
| 173 | $link = get_edit_post_link( $id ); |
| 174 | $titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return implode('<span class="cpac-divider"></span>', $titles); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get Users by ID - Value method |
| 184 | * |
| 185 | * @since 1.0 |
| 186 | * |
| 187 | * @param string $meta |
| 188 | * @return string Users |
| 189 | */ |
| 190 | private function get_users_by_id( $meta ) { |
| 191 | |
| 192 | $names = array(); |
| 193 | |
| 194 | // display username |
| 195 | if ( $ids = $this->get_ids_from_meta( $meta ) ) { |
| 196 | foreach ( (array) $ids as $id ) { |
| 197 | if ( ! is_numeric( $id ) ) continue; |
| 198 | |
| 199 | $userdata = get_userdata( $id ); |
| 200 | if ( is_object( $userdata ) && ! empty( $userdata->display_name ) ) { |
| 201 | |
| 202 | // link |
| 203 | $link = get_edit_user_link( $id ); |
| 204 | |
| 205 | $names[] = $link ? "<a href='{$link}'>{$userdata->display_name}</a>" : $userdata->display_name; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return implode( '<span class="cpac-divider"></span>', $names ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get Terms by ID - Value method |
| 215 | * |
| 216 | * @since 2.3.2 |
| 217 | * |
| 218 | * @param array $meta_value Term ID's |
| 219 | * @return string Terms |
| 220 | */ |
| 221 | public function get_terms_by_id( $meta_value ) { |
| 222 | // as used by Pods, @todo |
| 223 | if ( ! is_array( $meta_value) || ! isset( $meta_value['term_id'] ) || ! isset( $meta_value['taxonomy'] ) ) { |
| 224 | return false; |
| 225 | } |
| 226 | return $this->get_terms_for_display( $meta_value['term_id'], $meta_value['taxonomy'] ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get meta value |
| 231 | * |
| 232 | * @since 2.0 |
| 233 | * |
| 234 | * @param string $meta Contains Meta Value |
| 235 | * @param int $id Optional Object ID |
| 236 | * @return string Users |
| 237 | */ |
| 238 | public function get_value_by_meta( $meta, $id = null ) { |
| 239 | |
| 240 | switch ( $this->options->field_type ) : |
| 241 | |
| 242 | case "image" : |
| 243 | case "library_id" : |
| 244 | $meta = implode( $this->get_thumbnails( $meta, array( |
| 245 | 'image_size' => $this->options->image_size, |
| 246 | 'image_size_w' => $this->options->image_size_w, |
| 247 | 'image_size_h' => $this->options->image_size_h, |
| 248 | ))); |
| 249 | break; |
| 250 | |
| 251 | case "excerpt" : |
| 252 | $meta = $this->get_shortened_string( $meta, $this->options->excerpt_length ); |
| 253 | break; |
| 254 | |
| 255 | case "date" : |
| 256 | $meta = $this->get_date( $meta, $this->options->date_format ); |
| 257 | break; |
| 258 | |
| 259 | case "title_by_id" : |
| 260 | $meta = $this->get_titles_by_id( $meta ); |
| 261 | break; |
| 262 | |
| 263 | case "user_by_id" : |
| 264 | $meta = $this->get_users_by_id( $meta ); |
| 265 | break; |
| 266 | |
| 267 | case "term_by_id" : |
| 268 | $meta = $this->get_terms_by_id( $this->get_raw_value( $id ) ); |
| 269 | break; |
| 270 | |
| 271 | case "checkmark" : |
| 272 | $checkmark = $this->get_asset_image( 'checkmark.png' ); |
| 273 | |
| 274 | if ( empty( $meta ) || 'false' === $meta || '0' === $meta ) { |
| 275 | $checkmark = ''; |
| 276 | } |
| 277 | |
| 278 | $meta = $checkmark; |
| 279 | break; |
| 280 | |
| 281 | case "color" : |
| 282 | if ( ! empty( $meta ) ) { |
| 283 | $meta = $this->get_color_for_display( $meta ); |
| 284 | } |
| 285 | break; |
| 286 | |
| 287 | case "count" : |
| 288 | if ( $count = $this->get_raw_value( $id, false ) ) { |
| 289 | $meta = count( $count ); |
| 290 | } |
| 291 | break; |
| 292 | |
| 293 | endswitch; |
| 294 | |
| 295 | return $meta; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Get Field key |
| 300 | * |
| 301 | * @since 2.0.3 |
| 302 | * |
| 303 | * @param string Custom Field Key |
| 304 | */ |
| 305 | public function get_field_key() { |
| 306 | |
| 307 | return substr( $this->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $this->options->field ) : $this->options->field; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get meta by ID |
| 312 | * |
| 313 | * @since 1.0 |
| 314 | * |
| 315 | * @param int $id ID |
| 316 | * @return string Meta Value |
| 317 | */ |
| 318 | public function get_meta_by_id( $id ) { |
| 319 | |
| 320 | $meta = $this->get_raw_value( $id ); |
| 321 | |
| 322 | // try to turn any array into a comma seperated string for further use |
| 323 | if ( ( 'array' == $this->options->field_type && is_array( $meta ) ) || is_array( $meta ) ) { |
| 324 | $meta = $this->recursive_implode( ', ', $meta ); |
| 325 | } |
| 326 | |
| 327 | if ( ! is_string( $meta ) && ! is_numeric( $meta ) ) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | return $meta; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @see CPAC_Column::get_raw_value() |
| 336 | * @since 2.0.3 |
| 337 | */ |
| 338 | public function get_raw_value( $id, $single = true ) { |
| 339 | |
| 340 | $raw_value = ''; |
| 341 | |
| 342 | if ( $field_key = $this->get_field_key() ) { |
| 343 | $raw_value = get_metadata( $this->storage_model->meta_type, $id, $field_key, $single ); |
| 344 | } |
| 345 | |
| 346 | return apply_filters( 'cac/column/meta/raw_value', $raw_value, $id, $field_key, $this ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @see CPAC_Column::get_value() |
| 351 | * @since 1.0 |
| 352 | */ |
| 353 | public function get_value( $id ) { |
| 354 | |
| 355 | $value = ''; |
| 356 | |
| 357 | if ( $meta = $this->get_meta_by_id( $id ) ) { |
| 358 | $value = $this->get_value_by_meta( $meta, $id ); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Filter the display value for Custom Field columns |
| 363 | * |
| 364 | * @param mixed $value Custom field value |
| 365 | * @param int $id Object ID |
| 366 | * @param object $this Column instance |
| 367 | */ |
| 368 | $value = apply_filters( 'cac/column/meta/value', $value, $id, $this ); |
| 369 | |
| 370 | $before = $this->get_before(); |
| 371 | $after = $this->get_after(); |
| 372 | |
| 373 | // add before and after string |
| 374 | if ( $value ) { |
| 375 | $value = "{$before}{$value}{$after}"; |
| 376 | } |
| 377 | |
| 378 | return $value; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @since 2.4.7 |
| 383 | */ |
| 384 | public function get_meta_keys() { |
| 385 | return $this->storage_model->get_meta_keys(); |
| 386 | } |
| 387 | |
| 388 | public function get_meta_keys_list() { |
| 389 | $list = false; |
| 390 | |
| 391 | if ( $keys = $this->get_meta_keys() ) { |
| 392 | $lists = array(); |
| 393 | foreach ( $keys as $field ) { |
| 394 | if ( substr( $field, 0, 10 ) == "cpachidden" ) { |
| 395 | $lists['hidden'][] = $field; |
| 396 | } else { |
| 397 | $lists['public'][] = $field; |
| 398 | } |
| 399 | } |
| 400 | krsort( $lists ); // public first |
| 401 | |
| 402 | $list = '<select name="' . $this->get_attr_name( 'field' ) . '" id="' . $this->get_attr_id( 'field' ) . '">'; |
| 403 | foreach ( $lists as $type => $fields ) { |
| 404 | $list .= "<optgroup label='" . ( 'hidden' == $type ? __( 'Hidden Custom Fields', 'codepress-admin-columns' ) : __( 'Custom Fields', 'codepress-admin-columns' ) ) . "'>"; |
| 405 | foreach ( $fields as $field ) { |
| 406 | $list .= "<option value='{$field}'" . selected( $field, $this->options->field, false ) . ">" . str_replace( 'cpachidden', '', $field ) . "</option>"; |
| 407 | } |
| 408 | $list .= "</optgroup>"; |
| 409 | } |
| 410 | $list .= '</select>'; |
| 411 | } |
| 412 | |
| 413 | return $list; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @see CPAC_Column::display_settings() |
| 418 | * @since 1.0 |
| 419 | */ |
| 420 | public function display_settings() { ?> |
| 421 | <tr class="column_field"> |
| 422 | <?php $this->label_view( __( "Custom Field", 'codepress-admin-columns' ), __( "Select your custom field.", 'codepress-admin-columns' ), 'field' ); ?> |
| 423 | <td class="input"> |
| 424 | <?php |
| 425 | if ( $list = $this->get_meta_keys_list() ) { |
| 426 | echo $list; |
| 427 | } |
| 428 | else { |
| 429 | _e( 'No custom fields available.', 'codepress-admin-columns' ); ?> <?php printf( __( 'Please create a %s item first.', 'codepress-admin-columns' ), '<strong>' . $this->storage_model->singular_label . '</strong>' ); |
| 430 | } |
| 431 | ?> |
| 432 | </td> |
| 433 | </tr> |
| 434 | |
| 435 | <tr class="column_field_type" data-refresh="1"> |
| 436 | <?php $this->label_view( __( "Field Type", 'codepress-admin-columns' ), __( 'This will determine how the value will be displayed.', 'codepress-admin-columns' ) . '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->options->field_type . '</em>', 'field_type' ); ?> |
| 437 | <td class="input"> |
| 438 | <select name="<?php $this->attr_name( 'field_type' ); ?>" id="<?php $this->attr_id( 'field_type' ); ?>"> |
| 439 | <?php foreach ( $this->get_custom_field_types() as $fieldkey => $fieldtype ) : ?> |
| 440 | <option value="<?php echo $fieldkey ?>"<?php selected( $fieldkey, $this->options->field_type ) ?>><?php echo $fieldtype; ?></option> |
| 441 | <?php endforeach; ?> |
| 442 | </select> |
| 443 | </td> |
| 444 | </tr> |
| 445 | |
| 446 | <?php |
| 447 | switch ( $this->options->field_type ) { |
| 448 | case 'date': |
| 449 | $this->display_field_date_format(); |
| 450 | break; |
| 451 | case 'image': |
| 452 | case 'library_id': |
| 453 | $this->display_field_preview_size(); |
| 454 | break; |
| 455 | case 'excerpt': |
| 456 | $this->display_field_excerpt_length(); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | $this->display_field_before_after(); |
| 461 | } |
| 462 | } |