comment
12 years ago
link
12 years ago
media
12 years ago
post
12 years ago
user
12 years ago
custom-field.php
12 years ago
custom-field.php
394 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * CPAC_Column_Custom_Field |
| 5 | * |
| 6 | * @since 1.0 |
| 7 | */ |
| 8 | class CPAC_Column_Custom_Field extends CPAC_Column { |
| 9 | |
| 10 | private $user_settings; |
| 11 | |
| 12 | function __construct( $storage_model ) { |
| 13 | |
| 14 | // define properties |
| 15 | $this->properties['type'] = 'column-meta'; |
| 16 | $this->properties['label'] = __( 'Custom Field', 'cpac' ); |
| 17 | $this->properties['classes'] = 'cpac-box-metafield'; |
| 18 | $this->properties['is_cloneable'] = true; |
| 19 | |
| 20 | // define additional options |
| 21 | $this->options['field'] = ''; |
| 22 | $this->options['field_type'] = ''; |
| 23 | $this->options['before'] = ''; |
| 24 | $this->options['after'] = ''; |
| 25 | |
| 26 | $this->options['image_size'] = ''; |
| 27 | $this->options['image_size_w'] = 80; |
| 28 | $this->options['image_size_h'] = 80; |
| 29 | |
| 30 | $this->options['excerpt_length'] = 15; |
| 31 | |
| 32 | $this->options['date_format'] = ''; |
| 33 | |
| 34 | // for retireving sorting preference |
| 35 | $this->user_settings = get_option( 'cpac_general_options' ); |
| 36 | |
| 37 | // call contruct |
| 38 | parent::__construct( $storage_model ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @see CPAC_Column::sanitize_options() |
| 43 | * @since 1.0 |
| 44 | */ |
| 45 | function sanitize_options( $options ) { |
| 46 | |
| 47 | if ( ! empty( $options['before'] ) ) { |
| 48 | $options['before'] = trim( $options['before'] ); |
| 49 | } |
| 50 | |
| 51 | if ( ! empty( $options['after'] ) ) { |
| 52 | $options['after'] = trim( $options['after'] ); |
| 53 | } |
| 54 | |
| 55 | if ( empty( $options['date_format'] ) ) { |
| 56 | $options['date_format'] = get_option( 'date_format' ); |
| 57 | } |
| 58 | |
| 59 | return $options; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get Custom FieldType Options - Value method |
| 64 | * |
| 65 | * @since 1.0 |
| 66 | * |
| 67 | * @return array Customfield types. |
| 68 | */ |
| 69 | public function get_custom_field_types() { |
| 70 | |
| 71 | $custom_field_types = array( |
| 72 | '' => __( 'Default'), |
| 73 | 'image' => __( 'Image', 'cpac' ), |
| 74 | 'library_id' => __( 'Media Library', 'cpac' ), |
| 75 | 'excerpt' => __( 'Excerpt'), |
| 76 | 'array' => __( 'Multiple Values', 'cpac' ), |
| 77 | 'numeric' => __( 'Numeric', 'cpac' ), |
| 78 | 'date' => __( 'Date', 'cpac' ), |
| 79 | 'title_by_id' => __( 'Post Title (Post ID\'s)', 'cpac' ), |
| 80 | 'user_by_id' => __( 'Username (User ID\'s)', 'cpac' ), |
| 81 | 'checkmark' => __( 'Checkmark (true/false)', 'cpac' ), |
| 82 | 'color' => __( 'Color', 'cpac' ), |
| 83 | ); |
| 84 | |
| 85 | return apply_filters( 'cpac_custom_field_types', $custom_field_types ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get Title by ID - Value method |
| 90 | * |
| 91 | * @since 1.0 |
| 92 | * |
| 93 | * @param string $meta |
| 94 | * @return string Titles |
| 95 | */ |
| 96 | private function get_titles_by_id( $meta ) { |
| 97 | |
| 98 | //remove white spaces and strip tags |
| 99 | $meta = $this->strip_trim( str_replace( ' ','', $meta ) ); |
| 100 | |
| 101 | // var |
| 102 | $ids = $titles = array(); |
| 103 | |
| 104 | // check for multiple id's |
| 105 | if ( strpos( $meta, ',' ) !== false ) |
| 106 | $ids = explode( ',', $meta ); |
| 107 | elseif ( is_numeric( $meta ) ) |
| 108 | $ids[] = $meta; |
| 109 | |
| 110 | // display title with link |
| 111 | if ( $ids && is_array( $ids ) ) { |
| 112 | foreach ( $ids as $id ) { |
| 113 | $title = is_numeric( $id ) ? get_the_title( $id ) : ''; |
| 114 | $link = get_edit_post_link( $id ); |
| 115 | if ( $title ) |
| 116 | $titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return implode('<span class="cpac-divider"></span>', $titles); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get Users by ID - Value method |
| 125 | * |
| 126 | * @since 1.0 |
| 127 | * |
| 128 | * @param string $meta |
| 129 | * @return string Users |
| 130 | */ |
| 131 | private function get_users_by_id( $meta ) { |
| 132 | |
| 133 | //remove white spaces and strip tags |
| 134 | $meta = $this->strip_trim( str_replace( ' ', '', $meta ) ); |
| 135 | |
| 136 | // var |
| 137 | $ids = $names = array(); |
| 138 | |
| 139 | // check for multiple id's |
| 140 | if ( strpos( $meta, ',' ) !== false ) { |
| 141 | $ids = explode( ',',$meta ); |
| 142 | } |
| 143 | elseif ( is_numeric( $meta ) ) { |
| 144 | $ids[] = $meta; |
| 145 | } |
| 146 | |
| 147 | // display username |
| 148 | if ( $ids && is_array( $ids ) ) { |
| 149 | foreach ( $ids as $id ) { |
| 150 | if ( ! is_numeric( $id ) ) |
| 151 | continue; |
| 152 | |
| 153 | $userdata = get_userdata( $id ); |
| 154 | if ( is_object( $userdata ) && ! empty( $userdata->display_name ) ) { |
| 155 | |
| 156 | // link |
| 157 | $link = get_edit_user_link( $id ); |
| 158 | |
| 159 | $names[] = $link ? "<a href='{$link}'>{$userdata->display_name}</a>" : $userdata->display_name; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return implode( '<span class="cpac-divider"></span>', $names ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get Users by ID - Value method |
| 169 | * |
| 170 | * @since 2.0.0 |
| 171 | * |
| 172 | * @param string $meta |
| 173 | * @return string Users |
| 174 | */ |
| 175 | function get_value_by_meta( $meta ) { |
| 176 | |
| 177 | switch ( $this->options->field_type ) : |
| 178 | |
| 179 | case "image" : |
| 180 | case "library_id" : |
| 181 | $meta = implode( $this->get_thumbnails( $meta, array( |
| 182 | 'image_size' => $this->options->image_size, |
| 183 | 'image_size_w' => $this->options->image_size_w, |
| 184 | 'image_size_h' => $this->options->image_size_h, |
| 185 | ))); |
| 186 | break; |
| 187 | |
| 188 | case "excerpt" : |
| 189 | $meta = $this->get_shortened_string( $meta, $this->options->excerpt_length ); |
| 190 | break; |
| 191 | |
| 192 | case "date" : |
| 193 | $meta = $this->get_date( $meta, $this->options->date_format ); |
| 194 | break; |
| 195 | |
| 196 | case "title_by_id" : |
| 197 | if ( $titles = $this->get_titles_by_id( $meta ) ) |
| 198 | $meta = $titles; |
| 199 | break; |
| 200 | |
| 201 | case "user_by_id" : |
| 202 | if ( $names = $this->get_users_by_id( $meta ) ) |
| 203 | $meta = $names; |
| 204 | break; |
| 205 | |
| 206 | case "checkmark" : |
| 207 | $checkmark = $this->get_asset_image( 'checkmark.png' ); |
| 208 | |
| 209 | if ( empty($meta) || 'false' === $meta || '0' === $meta ) { |
| 210 | $checkmark = ''; |
| 211 | } |
| 212 | |
| 213 | $meta = $checkmark; |
| 214 | break; |
| 215 | |
| 216 | case "color" : |
| 217 | if ( ! empty( $meta ) ) { |
| 218 | $text_color = $this->get_text_color( $meta ); |
| 219 | $meta = "<div class='cpac-color'><span style='background-color:{$meta};color:{$text_color}'>{$meta}</span></div>"; |
| 220 | } |
| 221 | break; |
| 222 | |
| 223 | endswitch; |
| 224 | |
| 225 | return $meta; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Determines text color absed on bakground coloring. |
| 230 | * |
| 231 | * @since 1.0 |
| 232 | */ |
| 233 | function get_text_color( $bg_color ) { |
| 234 | |
| 235 | $rgb = $this->hex2rgb( $bg_color ); |
| 236 | |
| 237 | return $rgb && ( ( $rgb[0]*0.299 + $rgb[1]*0.587 + $rgb[2]*0.114 ) < 186 ) ? '#ffffff' : '#333333'; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Convert hex to rgb |
| 242 | * |
| 243 | * @since 1.0 |
| 244 | */ |
| 245 | function hex2rgb($hex) { |
| 246 | $hex = str_replace("#", "", $hex); |
| 247 | |
| 248 | if(strlen($hex) == 3) { |
| 249 | $r = hexdec(substr($hex,0,1).substr($hex,0,1)); |
| 250 | $g = hexdec(substr($hex,1,1).substr($hex,1,1)); |
| 251 | $b = hexdec(substr($hex,2,1).substr($hex,2,1)); |
| 252 | } else { |
| 253 | $r = hexdec(substr($hex,0,2)); |
| 254 | $g = hexdec(substr($hex,2,2)); |
| 255 | $b = hexdec(substr($hex,4,2)); |
| 256 | } |
| 257 | $rgb = array($r, $g, $b); |
| 258 | |
| 259 | return $rgb; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get meta by ID |
| 264 | * |
| 265 | * @since 1.0 |
| 266 | * |
| 267 | * @param int $id ID |
| 268 | * @return string Meta Value |
| 269 | */ |
| 270 | public function get_meta_by_id( $id ) { |
| 271 | |
| 272 | // rename hidden custom fields to their original name |
| 273 | $field = substr( $this->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $this->options->field ) : $this->options->field; |
| 274 | |
| 275 | // get metadata |
| 276 | $meta = get_metadata( $this->storage_model->type, $id, $field, true ); |
| 277 | |
| 278 | // try to turn any array into a comma seperated string for further use |
| 279 | if ( ( 'array' == $this->options->field_type && is_array( $meta ) ) || is_array( $meta ) ) { |
| 280 | $meta = $this->recursive_implode( ', ', $meta ); |
| 281 | } |
| 282 | |
| 283 | if ( ! is_string( $meta ) ) |
| 284 | return false; |
| 285 | |
| 286 | return $meta; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @see CPAC_Column::get_value() |
| 291 | * @since 1.0 |
| 292 | */ |
| 293 | function get_value( $id ) { |
| 294 | |
| 295 | if ( ! $meta = $this->get_meta_by_id( $id ) ) |
| 296 | return false; |
| 297 | |
| 298 | // get value by meta |
| 299 | $meta = $this->get_value_by_meta( $meta ); |
| 300 | |
| 301 | // add before and after string |
| 302 | if ( $meta ) { |
| 303 | $meta = "{$this->options->before}{$meta}{$this->options->after}"; |
| 304 | } |
| 305 | |
| 306 | return $meta; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @see CPAC_Column::display_settings() |
| 311 | * @since 1.0 |
| 312 | */ |
| 313 | function display_settings() { |
| 314 | |
| 315 | $show_hidden_meta = isset( $this->user_settings['show_hidden'] ) && '1' === $this->user_settings['show_hidden'] ? true : false; |
| 316 | |
| 317 | ?> |
| 318 | |
| 319 | <tr class="column_field"> |
| 320 | <?php $this->label_view( __( "Custom Field", 'cpac' ), __( "Select your custom field.", 'cpac' ), 'field' ); ?> |
| 321 | <td class="input"> |
| 322 | |
| 323 | <?php if ( $meta_keys = $this->storage_model->get_meta_keys( $show_hidden_meta ) ) : ?> |
| 324 | <select name="<?php $this->attr_name( 'field' ); ?>" id="<?php $this->attr_id( 'field' ); ?>"> |
| 325 | <?php foreach ( $meta_keys as $field ) : ?> |
| 326 | <option value="<?php echo $field ?>"<?php selected( $field, $this->options->field ) ?>><?php echo substr( $field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden','', $field ) : $field; ?></option> |
| 327 | <?php endforeach; ?> |
| 328 | </select> |
| 329 | <?php else : ?> |
| 330 | <?php _e( 'No custom fields available.', 'cpac' ); ?> |
| 331 | <?php endif; ?> |
| 332 | |
| 333 | </td> |
| 334 | </tr> |
| 335 | |
| 336 | <tr class="column_field_type"> |
| 337 | <?php $this->label_view( __( "Field Type", 'cpac' ), __( 'This will determine how the value will be displayed.', 'cpac' ), 'field_type' ); ?> |
| 338 | <td class="input"> |
| 339 | <select name="<?php $this->attr_name( 'field_type' ); ?>" id="<?php $this->attr_id( 'field_type' ); ?>"> |
| 340 | <?php foreach ( $this->get_custom_field_types() as $fieldkey => $fieldtype ) : ?> |
| 341 | <option value="<?php echo $fieldkey ?>"<?php selected( $fieldkey, $this->options->field_type ) ?>><?php echo $fieldtype; ?></option> |
| 342 | <?php endforeach; ?> |
| 343 | </select> |
| 344 | </td> |
| 345 | </tr> |
| 346 | |
| 347 | <?php |
| 348 | |
| 349 | /** |
| 350 | * Add Date Format |
| 351 | * |
| 352 | */ |
| 353 | $is_hidden = in_array( $this->options->field_type, array( 'date' ) ) ? false : true; |
| 354 | |
| 355 | $this->display_field_date_format( $is_hidden ); |
| 356 | |
| 357 | /** |
| 358 | * Add Preview size |
| 359 | * |
| 360 | */ |
| 361 | $is_hidden = in_array( $this->options->field_type, array( 'image', 'library_id' ) ) ? false : true; |
| 362 | |
| 363 | $this->display_field_preview_size( $is_hidden ); |
| 364 | |
| 365 | /** |
| 366 | * Add Excerpt length |
| 367 | * |
| 368 | */ |
| 369 | $is_hidden = in_array( $this->options->field_type, array( 'excerpt' ) ) ? false : true; |
| 370 | |
| 371 | $this->display_field_excerpt_length( $is_hidden ); |
| 372 | |
| 373 | /** |
| 374 | * Before / After |
| 375 | * |
| 376 | */ |
| 377 | ?> |
| 378 | |
| 379 | <tr class="column_before"> |
| 380 | <?php $this->label_view( __( "Before", 'cpac' ), __( 'This text will appear before the custom field value.', 'cpac' ), 'before' ); ?> |
| 381 | <td class="input"> |
| 382 | <input type="text" class="cpac-before" name="<?php $this->attr_name( 'before' ); ?>" id="<?php $this->attr_id( 'before' ); ?>" value="<?php echo $this->options->before; ?>"/> |
| 383 | </td> |
| 384 | </tr> |
| 385 | <tr class="column_after"> |
| 386 | <?php $this->label_view( __( "After", 'cpac' ), __( 'This text will appear after the custom field value.', 'cpac' ), 'after' ); ?> |
| 387 | <td class="input"> |
| 388 | <input type="text" class="cpac-after" name="<?php $this->attr_name( 'after' ); ?>" id="<?php $this->attr_id( 'after' ); ?>" value="<?php echo $this->options->after; ?>"/> |
| 389 | </td> |
| 390 | </tr> |
| 391 | <?php |
| 392 | |
| 393 | } |
| 394 | } |