PostTypeCustomFields.php
2 years ago
PostTypeRepository.php
2 years ago
RegisterPostTypes.php
8 years ago
PostTypeCustomFields.php
110 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\PostType; |
| 3 | |
| 4 | /** |
| 5 | * Enables the filtering of custom fields in the quick edit and bulk edit interfaces |
| 6 | * Basic field types currently supported : text|date|select|url |
| 7 | * |
| 8 | * Filter should return an array of fields |
| 9 | * |
| 10 | * Example: |
| 11 | * $fields = [ |
| 12 | [ |
| 13 | 'key' => 'custom_field', |
| 14 | 'label' => __('Custom Field', 'text-domain'), |
| 15 | 'type' => 'date', // date|text|select |
| 16 | 'format' => 'm/d/Y', // Required for date |
| 17 | 'required' => false, |
| 18 | 'validation_message' => __('A deadline is required.'), |
| 19 | 'choices' => [] // For select type |
| 20 | ] |
| 21 | ]; |
| 22 | */ |
| 23 | class PostTypeCustomFields |
| 24 | { |
| 25 | public function outputBulkEditFields($post_type, $column = 'left') |
| 26 | { |
| 27 | $fields = apply_filters('nestedpages_bulkedit_custom_fields', [], $post_type, $column); |
| 28 | if ( empty($fields) ) return; |
| 29 | $out = ''; |
| 30 | foreach ( $fields as $field ){ |
| 31 | $method = $field['type'] . 'Field'; |
| 32 | if ( method_exists($this, $method) ) $out .= $this->$method($field); |
| 33 | } |
| 34 | return $out; |
| 35 | } |
| 36 | |
| 37 | public function outputQuickEditFields($post_type, $column = 'left') |
| 38 | { |
| 39 | $fields = apply_filters('nestedpages_quickedit_custom_fields', [], $post_type, $column); |
| 40 | if ( empty($fields) ) return; |
| 41 | $out = ''; |
| 42 | foreach ( $fields as $field ){ |
| 43 | $method = $field['type'] . 'Field'; |
| 44 | if ( method_exists($this, $method) ) $out .= $this->$method($field); |
| 45 | } |
| 46 | return $out; |
| 47 | } |
| 48 | |
| 49 | public function dateField($field) |
| 50 | { |
| 51 | $out = '<div class="form-control np-datepicker-container">'; |
| 52 | $out .= '<label>' . $field['label'] . '</label>'; |
| 53 | $out .= '<div class="datetime"><input type="text" data-datepicker-format="' . $field['format_datepicker'] . '" name="np_custom_' . $field['key'] . '" class="np_datepicker full" value="" data-np-custom-field="' . $field['key'] . '" /></div>'; |
| 54 | $out .= '</div>'; |
| 55 | return $out; |
| 56 | } |
| 57 | |
| 58 | public function textField($field) |
| 59 | { |
| 60 | $out = '<div class="form-control">'; |
| 61 | $out .= '<label>' . $field['label'] . '</label>'; |
| 62 | $out .= '<input type="text" name="np_custom_' . $field['key'] . '" value="" data-np-custom-field="' . $field['key'] . '" />'; |
| 63 | $out .= '</div>'; |
| 64 | return $out; |
| 65 | } |
| 66 | |
| 67 | public function urlField($field) |
| 68 | { |
| 69 | $out = '<div class="form-control">'; |
| 70 | $out .= '<label>' . $field['label'] . '</label>'; |
| 71 | $out .= '<input type="text" name="np_custom_nptype_url_nptype_' . $field['key'] . '" value="" data-np-custom-field="' . $field['key'] . '" />'; |
| 72 | $out .= '</div>'; |
| 73 | return $out; |
| 74 | } |
| 75 | |
| 76 | public function selectField($field) |
| 77 | { |
| 78 | $out = '<div class="form-control">'; |
| 79 | $out .= '<label>' . $field['label'] . '</label>'; |
| 80 | $out .= '<select name="np_custom_nptype_select_nptype_' . $field['key'] . '" value="" data-np-custom-field="' . $field['key'] . '">'; |
| 81 | foreach ( $field['choices'] as $key => $label ){ |
| 82 | $out .= '<option value="' . $key . '">' . $label . '</option>'; |
| 83 | } |
| 84 | $out .= '</select>'; |
| 85 | $out .= '</div>'; |
| 86 | return $out; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Output the data attributes for the post in quick edit for initial population |
| 91 | */ |
| 92 | public function dataAttributes($post, $post_type) |
| 93 | { |
| 94 | $custom_fields_left = apply_filters('nestedpages_quickedit_custom_fields', [], $post_type, $column = 'left'); |
| 95 | $custom_fields_right = apply_filters('nestedpages_quickedit_custom_fields', [], $post_type, $column = 'right'); |
| 96 | $custom_fields = array_merge($custom_fields_left, $custom_fields_right); |
| 97 | $out = ''; |
| 98 | if ( empty($custom_fields) ) return $out; |
| 99 | foreach ( $custom_fields as $field ) : |
| 100 | $custom_value = ( isset($post->meta[$field['key']]) ) ? $post->meta[$field['key']] : null; |
| 101 | if ( $custom_value ) : |
| 102 | $value = $custom_value[0]; |
| 103 | if ( $field['type'] == 'url' && $value !== '' ) $value = esc_url($value); |
| 104 | if ( $field['type'] == 'date' && $field['format_save'] && $value !== '' ) $value = date($field['format_save'], strtotime($value)); |
| 105 | $out .= ' data-npcustom-' . $field['key'] . '="' . $value . '"'; |
| 106 | endif; |
| 107 | endforeach; |
| 108 | return $out; |
| 109 | } |
| 110 | } |