BeforeAfter
8 years ago
ActionIcons.php
8 years ago
AttachmentDisplay.php
8 years ago
BeforeAfter.php
8 years ago
CharacterLimit.php
8 years ago
CommentCount.php
8 years ago
CustomField.php
8 years ago
CustomFieldType.php
8 years ago
Date.php
8 years ago
ExifData.php
8 years ago
Image.php
8 years ago
Images.php
8 years ago
Label.php
8 years ago
LinkLabel.php
8 years ago
LinkToMenu.php
8 years ago
Message.php
8 years ago
Meta.php
8 years ago
MissingImageSize.php
8 years ago
NumberOfItems.php
8 years ago
Password.php
8 years ago
PathScope.php
8 years ago
Post.php
8 years ago
PostFormatIcon.php
8 years ago
PostLink.php
8 years ago
PostType.php
8 years ago
Separator.php
8 years ago
StatusIcon.php
8 years ago
StringLimit.php
8 years ago
Taxonomy.php
8 years ago
Term.php
8 years ago
Toggle.php
8 years ago
Type.php
8 years ago
User.php
8 years ago
Width.php
8 years ago
WordLimit.php
8 years ago
WordsPerMinute.php
8 years ago
CustomFieldType.php
304 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Settings_Column_CustomFieldType extends AC_Settings_Column |
| 8 | implements AC_Settings_FormatValueInterface { |
| 9 | |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | private $field_type; |
| 14 | |
| 15 | protected function define_options() { |
| 16 | return array( 'field_type' ); |
| 17 | } |
| 18 | |
| 19 | public function get_dependent_settings() { |
| 20 | $settings = array(); |
| 21 | |
| 22 | switch ( $this->get_field_type() ) { |
| 23 | |
| 24 | case 'date' : |
| 25 | $settings[] = new AC_Settings_Column_Date( $this->column ); |
| 26 | |
| 27 | break; |
| 28 | case 'image' : |
| 29 | case 'library_id' : |
| 30 | $settings[] = new AC_Settings_Column_Image( $this->column ); |
| 31 | |
| 32 | break; |
| 33 | case 'excerpt' : |
| 34 | $settings[] = new AC_Settings_Column_StringLimit( $this->column ); |
| 35 | |
| 36 | break; |
| 37 | case 'link' : |
| 38 | $settings[] = new AC_Settings_Column_LinkLabel( $this->column ); |
| 39 | |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | return $settings; |
| 44 | } |
| 45 | |
| 46 | public function create_view() { |
| 47 | $select = $this->create_element( 'select' ); |
| 48 | |
| 49 | $select->set_attribute( 'data-refresh', 'column' ) |
| 50 | ->set_options( $this->get_grouped_options() ) |
| 51 | ->set_description( $this->get_description() ); |
| 52 | |
| 53 | $tooltip = __( 'This will determine how the value will be displayed.', 'codepress-admin-columns' ); |
| 54 | |
| 55 | if ( null !== $this->get_field_type() ) { |
| 56 | $tooltip .= '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->get_field_type() . '</em>'; |
| 57 | } |
| 58 | |
| 59 | $view = new AC_View( array( |
| 60 | 'label' => __( 'Field Type', 'codepress-admin-columns' ), |
| 61 | 'tooltip' => $tooltip, |
| 62 | 'setting' => $select, |
| 63 | ) ); |
| 64 | |
| 65 | return $view; |
| 66 | } |
| 67 | |
| 68 | private function get_description_object_ids( $input ) { |
| 69 | $description = sprintf( __( "Uses the id from a %s to display information about it.", 'codepress-admin-columns' ), '<em>' . $input . '</em>' ); |
| 70 | $description .= ' ' . __( "Multiple ids should be separated by a comma.", 'codepress-admin-columns' ); |
| 71 | |
| 72 | return $description; |
| 73 | } |
| 74 | |
| 75 | public function get_description() { |
| 76 | $description = false; |
| 77 | |
| 78 | switch ( $this->get_field_type() ) { |
| 79 | case 'title_by_id' : |
| 80 | $description = $this->get_description_object_ids( __( "Post Type", 'codepress-admin-columns' ) ); |
| 81 | |
| 82 | break; |
| 83 | case 'user_by_id' : |
| 84 | $description = $this->get_description_object_ids( __( "User", 'codepress-admin-columns' ) ); |
| 85 | |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | return $description; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get possible field types |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | protected function get_field_type_options() { |
| 98 | $grouped_types = array( |
| 99 | 'basic' => array( |
| 100 | 'color' => __( 'Color', 'codepress-admin-columns' ), |
| 101 | 'date' => __( 'Date', 'codepress-admin-columns' ), |
| 102 | 'excerpt' => __( 'Text' ), |
| 103 | 'image' => __( 'Image', 'codepress-admin-columns' ), |
| 104 | 'link' => __( 'Url', 'codepress-admin-columns' ), |
| 105 | 'numeric' => __( 'Number', 'codepress-admin-columns' ), |
| 106 | ), |
| 107 | 'choice' => array( |
| 108 | 'has_content' => __( 'Has Content', 'codepress-admin-columns' ), |
| 109 | 'checkmark' => __( 'True / False', 'codepress-admin-columns' ), |
| 110 | ), |
| 111 | 'relational' => array( |
| 112 | 'library_id' => __( 'Media', 'codepress-admin-columns' ), |
| 113 | 'title_by_id' => __( 'Post', 'codepress-admin-columns' ), |
| 114 | 'user_by_id' => __( 'User', 'codepress-admin-columns' ), |
| 115 | ), |
| 116 | 'multiple' => array( |
| 117 | 'count' => __( 'Number of Fields', 'codepress-admin-columns' ), |
| 118 | 'array' => __( 'Multiple Values', 'codepress-admin-columns' ), |
| 119 | ), |
| 120 | ); |
| 121 | |
| 122 | /** |
| 123 | * Filter the available custom field types for the meta (custom field) field |
| 124 | * |
| 125 | * @since 3.0 |
| 126 | * |
| 127 | * @param array $field_types Available custom field types ([type] => [label]) |
| 128 | */ |
| 129 | $grouped_types['custom'] = apply_filters( 'ac/column/custom_field/field_types', array() ); |
| 130 | |
| 131 | foreach ( $grouped_types as $k => $fields ) { |
| 132 | natcasesort( $grouped_types[ $k ] ); |
| 133 | } |
| 134 | |
| 135 | return $grouped_types; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return array |
| 140 | */ |
| 141 | private function get_grouped_options() { |
| 142 | $field_types = $this->get_field_type_options(); |
| 143 | |
| 144 | foreach ( $field_types as $fields ) { |
| 145 | asort( $fields ); |
| 146 | } |
| 147 | |
| 148 | $groups = array( |
| 149 | 'basic' => __( 'Basic', 'codepress-admin-columns' ), |
| 150 | 'relational' => __( 'Relational', 'codepress-admin-columns' ), |
| 151 | 'choice' => __( 'Choice', 'codepress-admin-columns' ), |
| 152 | 'multiple' => __( 'Multiple', 'codepress-admin-columns' ), |
| 153 | 'custom' => __( 'Custom', 'codepress-admin-columns' ), |
| 154 | ); |
| 155 | |
| 156 | $grouped_options = array(); |
| 157 | foreach ( $field_types as $group => $fields ) { |
| 158 | |
| 159 | if ( ! $fields ) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | $grouped_options[ $group ]['title'] = $groups[ $group ]; |
| 164 | $grouped_options[ $group ]['options'] = $fields; |
| 165 | } |
| 166 | |
| 167 | // Default option comes first |
| 168 | $grouped_options = array_merge( array( '' => __( 'Default', 'codepress-admin-columns' ) ), $grouped_options ); |
| 169 | |
| 170 | return $grouped_options; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param string|array $string |
| 175 | * |
| 176 | * @return array |
| 177 | */ |
| 178 | private function get_values_from_array_or_string( $string ) { |
| 179 | $string = ac_helper()->array->implode_recursive( ',', $string ); |
| 180 | |
| 181 | return ac_helper()->string->comma_separated_to_array( $string ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param string|array $string |
| 186 | * |
| 187 | * @return array |
| 188 | */ |
| 189 | private function get_ids_from_array_or_string( $string ) { |
| 190 | $string = ac_helper()->array->implode_recursive( ',', $string ); |
| 191 | |
| 192 | return ac_helper()->string->string_to_array_integers( $string ); |
| 193 | } |
| 194 | |
| 195 | public function format( $value, $original_value ) { |
| 196 | |
| 197 | switch ( $this->get_field_type() ) { |
| 198 | |
| 199 | case 'date' : |
| 200 | if ( $timestamp = ac_helper()->date->strtotime( $value ) ) { |
| 201 | $value = date( 'c', $timestamp ); |
| 202 | } |
| 203 | |
| 204 | break; |
| 205 | |
| 206 | case "title_by_id" : |
| 207 | $values = array(); |
| 208 | foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) { |
| 209 | $post = get_post( $id ); |
| 210 | $values[] = ac_helper()->html->link( get_edit_post_link( $post ), $post->post_title ); |
| 211 | } |
| 212 | |
| 213 | $value = implode( ac_helper()->html->divider(), $values ); |
| 214 | break; |
| 215 | |
| 216 | case "user_by_id" : |
| 217 | $values = array(); |
| 218 | foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) { |
| 219 | $user = get_userdata( $id ); |
| 220 | $values[] = ac_helper()->html->link( get_edit_user_link( $id ), ac_helper()->user->get_display_name( $user ) ); |
| 221 | } |
| 222 | |
| 223 | $value = implode( ac_helper()->html->divider(), $values ); |
| 224 | |
| 225 | break; |
| 226 | case 'image': |
| 227 | $value = new AC_Collection( $this->get_values_from_array_or_string( $value ) ); |
| 228 | |
| 229 | break; |
| 230 | case 'library_id' : |
| 231 | $value = new AC_Collection( $this->get_ids_from_array_or_string( $value ) ); |
| 232 | |
| 233 | break; |
| 234 | case "checkmark" : |
| 235 | $is_true = ! empty( $value ) && 'false' !== $value && '0' !== $value; |
| 236 | |
| 237 | if ( $is_true ) { |
| 238 | $value = ac_helper()->icon->dashicon( array( 'icon' => 'yes', 'class' => 'green' ) ); |
| 239 | } else { |
| 240 | $value = ac_helper()->icon->dashicon( array( 'icon' => 'no-alt', 'class' => 'red' ) ); |
| 241 | } |
| 242 | |
| 243 | break; |
| 244 | case "color" : |
| 245 | |
| 246 | if ( $value && is_scalar( $value ) ) { |
| 247 | $value = ac_helper()->string->get_color_block( $value ); |
| 248 | } else { |
| 249 | $value = false; |
| 250 | } |
| 251 | |
| 252 | break; |
| 253 | case "count" : |
| 254 | |
| 255 | if ( $this->column instanceof AC_Column_Meta ) { |
| 256 | $value = $this->column->get_meta_value( $original_value, $this->column->get_meta_key(), false ); |
| 257 | |
| 258 | if ( $value ) { |
| 259 | if ( 1 === count( $value ) && is_array( $value[0] ) ) { |
| 260 | |
| 261 | // Value contains a single serialized array with multiple values |
| 262 | $value = count( $value[0] ); |
| 263 | } else { |
| 264 | |
| 265 | // Count multiple usage of meta keys |
| 266 | $value = count( $value ); |
| 267 | } |
| 268 | } else { |
| 269 | $value = false; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | break; |
| 274 | case "has_content" : |
| 275 | $value = ac_helper()->icon->yes_or_no( $value, $value ); |
| 276 | |
| 277 | break; |
| 278 | default : |
| 279 | $value = ac_helper()->array->implode_recursive( __( ', ' ), $value ); |
| 280 | } |
| 281 | |
| 282 | return $value; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @return string |
| 287 | */ |
| 288 | public function get_field_type() { |
| 289 | return $this->field_type; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @param string $field_type |
| 294 | * |
| 295 | * @return bool |
| 296 | */ |
| 297 | public function set_field_type( $field_type ) { |
| 298 | $this->field_type = $field_type; |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | } |
| 304 |