column
12 years ago
storage_model
12 years ago
addons.php
12 years ago
column.php
12 years ago
settings.php
12 years ago
storage_model.php
12 years ago
third_party.php
12 years ago
upgrade.php
12 years ago
utility.php
12 years ago
column.php
1038 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CPAC_Column class |
| 4 | * |
| 5 | * @since 2.0 |
| 6 | * |
| 7 | * @param object $storage_model CPAC_Storage_Model |
| 8 | */ |
| 9 | class CPAC_Column { |
| 10 | |
| 11 | /** |
| 12 | * A Storage Model can be a Posttype, User, Comment, Link or Media storage type. |
| 13 | * |
| 14 | * @since 2.0 |
| 15 | * @var CPAC_Storage_Model $storage_model contains a CPAC_Storage_Model object which the column belongs too. |
| 16 | */ |
| 17 | public $storage_model; |
| 18 | |
| 19 | /** |
| 20 | * @since 2.0 |
| 21 | * @var array $options contains the user set options for the CPAC_Column object. |
| 22 | */ |
| 23 | public $options = array(); |
| 24 | |
| 25 | /** |
| 26 | * @since 2.0 |
| 27 | * @var object $options_default contains the options for the CPAC_Column object before they are populated with user input. |
| 28 | */ |
| 29 | protected $options_default; |
| 30 | |
| 31 | /** |
| 32 | * @since 2.0 |
| 33 | * @var array $properties describes the fixed properties for the CPAC_Column object. |
| 34 | */ |
| 35 | public $properties = array(); |
| 36 | |
| 37 | /** |
| 38 | * @since 2.0 |
| 39 | * |
| 40 | * @param int $id ID |
| 41 | * @return string Value |
| 42 | */ |
| 43 | public function get_value( $id ) {} |
| 44 | |
| 45 | /** |
| 46 | * Get the raw, underlying value for the column |
| 47 | * Not suitable for direct display, use get_value() for that |
| 48 | * |
| 49 | * @since 2.0.3 |
| 50 | * |
| 51 | * @param int $id ID |
| 52 | * @return mixed Value |
| 53 | */ |
| 54 | public function get_raw_value( $id ) {} |
| 55 | |
| 56 | /** |
| 57 | * @since 2.0 |
| 58 | */ |
| 59 | protected function display_settings() {} |
| 60 | |
| 61 | /** |
| 62 | * Overwrite this function in child class to sanitize |
| 63 | * user submitted values. |
| 64 | * |
| 65 | * @since 2.0 |
| 66 | * |
| 67 | * @param $options array User submitted column options |
| 68 | * @return array Options |
| 69 | */ |
| 70 | protected function sanitize_options( $options ) { |
| 71 | |
| 72 | if ( isset( $options['date_format'] ) ) { |
| 73 | $options['date_format'] = trim( $options['date_format'] ); |
| 74 | } |
| 75 | |
| 76 | return $options; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Determine whether this column type should be available |
| 81 | * |
| 82 | * @since 2.3 |
| 83 | * |
| 84 | * @return bool Whether the column type should be available |
| 85 | */ |
| 86 | public function apply_conditional() { |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * An object copy (clone) is created for creating multiple column instances. |
| 93 | * |
| 94 | * @since 2.0 |
| 95 | */ |
| 96 | public function __clone() { |
| 97 | |
| 98 | // Force a copy of this->object, otherwise it will point to same object. |
| 99 | $this->options = clone $this->options; |
| 100 | $this->properties = clone $this->properties; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @since 2.0 |
| 105 | * |
| 106 | * @param object $storage_model CPAC_Storage_Model |
| 107 | */ |
| 108 | public function __construct( CPAC_Storage_Model $storage_model ) { |
| 109 | |
| 110 | $this->storage_model = $storage_model; |
| 111 | |
| 112 | $this->init(); |
| 113 | $this->after_setup(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @since 2.2 |
| 118 | */ |
| 119 | public function init() { |
| 120 | |
| 121 | // Default properties |
| 122 | $default_properties = array( |
| 123 | 'clone' => null, // Unique clone ID |
| 124 | 'type' => null, // Unique type |
| 125 | 'name' => null, // Unique name |
| 126 | 'label' => null, // Label which describes this column. |
| 127 | 'classes' => null, // Custom CSS classes for this column. |
| 128 | 'hide_label' => false, // Should the Label be hidden? |
| 129 | 'is_registered' => true, // Should the column be registered based on conditional logic, example usage see: 'post/page-template.php' |
| 130 | 'is_cloneable' => true, // Should the column be cloneable |
| 131 | 'default' => false, // Is this a WP default column, |
| 132 | 'group' => 'custom' |
| 133 | ); |
| 134 | |
| 135 | foreach ( $default_properties as $property => $value ) { |
| 136 | $this->properties[ $property ] = $value; |
| 137 | } |
| 138 | |
| 139 | // Default options |
| 140 | $default_options = array( |
| 141 | 'width' => null, // Width for this column. |
| 142 | 'state' => 'off' // Active state for this column. |
| 143 | ); |
| 144 | |
| 145 | /** |
| 146 | * Filter the default options for a column instance, such as label and width |
| 147 | * |
| 148 | * @since 2.2 |
| 149 | * @param array $default_options Default column options |
| 150 | * @param CPAC_Column $column_instance Column class instance |
| 151 | */ |
| 152 | $default_options = apply_filters( 'cac/column/default_options', $default_options, $this ); |
| 153 | |
| 154 | foreach ( $default_options as $option => $value ) { |
| 155 | $this->options[ $option ] = $value; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public function after_setup() { |
| 160 | |
| 161 | // Column name defaults to column type |
| 162 | if ( ! isset( $this->properties['name'] ) ) { |
| 163 | $this->properties['name'] = $this->properties['type']; |
| 164 | } |
| 165 | |
| 166 | // Check whether the column should be available |
| 167 | if ( ! isset( $this->properties['is_registered'] ) ) { |
| 168 | $this->properties['is_registered'] = $this->apply_conditional(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Filter the properties of a column type, such as type and is_cloneable |
| 173 | * Property $column_instance added in Admin Columns 2.2 |
| 174 | * |
| 175 | * @since 2.0 |
| 176 | * @param array $properties Column properties |
| 177 | * @param CPAC_Column $column_instance Column class instance |
| 178 | */ |
| 179 | $this->properties = apply_filters( 'cac/column/properties', $this->properties, $this ); |
| 180 | |
| 181 | /** |
| 182 | * Filter the properties of a column type for a specific storage model |
| 183 | * Property $column_instance added in Admin Columns 2.2 |
| 184 | * |
| 185 | * @since 2.0 |
| 186 | * @see Filter cac/column/properties |
| 187 | */ |
| 188 | $this->properties = apply_filters( "cac/column/properties/storage_key={$this->storage_model->key}", $this->properties, $this ); |
| 189 | |
| 190 | // Column label defaults to column type label |
| 191 | if ( ! isset( $this->options['label'] ) ) { |
| 192 | $this->options['label'] = $this->properties['label']; |
| 193 | } |
| 194 | |
| 195 | // Convert properties and options arrays to object |
| 196 | $this->options = (object) $this->options; |
| 197 | $this->properties = (object) $this->properties; |
| 198 | |
| 199 | // Read options from database |
| 200 | $this->populate_options(); |
| 201 | |
| 202 | $this->sanitize_label(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Populate Options |
| 207 | * Added $options parameter in 2.2 |
| 208 | * |
| 209 | * @since 2.0 |
| 210 | * @param array $options Optional. Options to populate the storage model with. Defaults to options from database. |
| 211 | */ |
| 212 | public function populate_options( $options = NULL ) { |
| 213 | $this->options = (object) array_merge( (array) $this->options, is_array( $options ) ? $options : $this->read() ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param string $property |
| 218 | * @return mixed $value |
| 219 | */ |
| 220 | public function set_properties( $property, $value ) { |
| 221 | $this->properties->{$property} = $value; |
| 222 | |
| 223 | return $this; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param string $option |
| 228 | * @return mixed $value |
| 229 | */ |
| 230 | public function set_options( $option, $value ) { |
| 231 | $this->options->{$option} = $value; |
| 232 | |
| 233 | return $this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @param int $id |
| 238 | * @return object |
| 239 | */ |
| 240 | public function set_clone( $id = null ) { |
| 241 | |
| 242 | if ( $id !== null && $id > 0 ) { |
| 243 | $this->properties->name = "{$this->properties->type}-{$id}"; |
| 244 | $this->properties->clone = $id; |
| 245 | } |
| 246 | |
| 247 | return $this; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @param string $field_key |
| 252 | * @return void |
| 253 | */ |
| 254 | public function attr_name( $field_name ) { |
| 255 | echo "{$this->storage_model->key}[{$this->properties->name}][{$field_name}]"; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @param string $field_key |
| 260 | * @return string Attribute Name |
| 261 | */ |
| 262 | public function attr_id( $field_name ) { |
| 263 | echo "cpac-{$this->storage_model->key}-{$this->properties->name}-{$field_name}"; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @since 2.0 |
| 268 | * @return array Column options |
| 269 | */ |
| 270 | public function read() { |
| 271 | $options = (array) get_option( "cpac_options_{$this->storage_model->key}" ); |
| 272 | |
| 273 | if ( empty( $options[ $this->properties->name ] ) ) { |
| 274 | return array(); |
| 275 | } |
| 276 | |
| 277 | return $options[ $this->properties->name ]; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @since 2.0 |
| 282 | */ |
| 283 | public function sanitize_label() { |
| 284 | // check if original label has changed. Example WPML adds a language column, the column heading will have to display the added flag. |
| 285 | if ( $this->properties->hide_label && $this->properties->label !== $this->options->label ) { |
| 286 | $this->options->label = $this->properties->label; |
| 287 | } |
| 288 | |
| 289 | // replace urls, so export will not have to deal with them |
| 290 | $this->options->label = stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) ); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @since 2.0 |
| 295 | * @param $options array User submitted column options |
| 296 | * @return array Options |
| 297 | */ |
| 298 | public function sanitize_storage( $options ) { |
| 299 | |
| 300 | // excerpt length must be numeric, else we will return it's default |
| 301 | if ( isset( $options['excerpt_length'] ) ) { |
| 302 | $options['excerpt_length'] = trim( $options['excerpt_length'] ); |
| 303 | if ( empty( $options['excerpt_length'] ) || ! is_numeric( $options['excerpt_length'] ) ) { |
| 304 | $options['excerpt_length'] = $this->options_default->excerpt_length; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if ( ! empty( $options['label'] ) ) { |
| 309 | |
| 310 | // Label can not contains the character ':', because |
| 311 | // CPAC_Column::get_sanitized_label() will return an empty string |
| 312 | $options['label'] = str_replace( ':', '', $options['label'] ); |
| 313 | } |
| 314 | |
| 315 | // used by child classes for additional sanitizing |
| 316 | $options = $this->sanitize_options( $options ); |
| 317 | |
| 318 | return $options; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @since 2.0 |
| 323 | */ |
| 324 | function get_label() { |
| 325 | |
| 326 | /** |
| 327 | * Filter the column instance label |
| 328 | * |
| 329 | * @since 2.0 |
| 330 | * |
| 331 | * @param string $label Column instance label |
| 332 | * @param CPAC_Column $column_instance Column class instance |
| 333 | */ |
| 334 | return apply_filters( 'cac/column/settings_label', stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) ), $this ); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Sanitizes label using intern wordpress function esc_url so it matches the label sorting url. |
| 339 | * |
| 340 | * @since 1.0 |
| 341 | * @param string $string |
| 342 | * @return string Sanitized string |
| 343 | */ |
| 344 | public function get_sanitized_label() { |
| 345 | $string = esc_url( $this->options->label ); |
| 346 | $string = str_replace( 'http://', '', $string ); |
| 347 | $string = str_replace( 'https://', '', $string ); |
| 348 | |
| 349 | return $string; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * @since 2.2 |
| 354 | * @param $id Cache ID |
| 355 | * @return string MD5 Cache ID |
| 356 | */ |
| 357 | function get_cache_id( $id ) { |
| 358 | return md5( $this->storage_model->key . $this->properties->name . $id ); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * @since 2.0 |
| 363 | * @param $id Cache ID |
| 364 | * @param $cache_object Cache Object |
| 365 | */ |
| 366 | function set_cache( $id, $cache_object ) { |
| 367 | |
| 368 | if ( empty( $cache_object ) ) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | set_transient( $this->get_cache_id( $id ), $cache_object ); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * @since 2.0 |
| 377 | * @param $id Cache ID ( could be a name of an addon for example ) |
| 378 | * @return false | mixed Returns either false or the cached objects |
| 379 | */ |
| 380 | function get_cache( $id ) { |
| 381 | $cache = get_transient( $this->get_cache_id( $id ) ); |
| 382 | |
| 383 | if ( empty( $cache ) ) { |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | return $cache; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @since 2.0 |
| 392 | * @param $id Cache ID |
| 393 | */ |
| 394 | function delete_cache( $id ) { |
| 395 | delete_transient( $this->get_cache_id( $id ) ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @since 1.3.1 |
| 400 | */ |
| 401 | protected function get_shorten_url( $url = '' ) { |
| 402 | if ( ! $url ) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return "<a title='{$url}' href='{$url}'>" . url_shorten( $url ) . "</a>"; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * @since 1.3 |
| 411 | */ |
| 412 | protected function strip_trim( $string ) { |
| 413 | return trim( strip_tags( $string ) ); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @since 2.2.1 |
| 418 | */ |
| 419 | protected function get_term_field( $field, $term_id, $taxonomy ) { |
| 420 | $term_field = get_term_field( $field, $term_id, $taxonomy, 'display' ); |
| 421 | if ( is_wp_error( $term_field ) ) { |
| 422 | return false; |
| 423 | } |
| 424 | return $term_field; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * @since 1.0 |
| 429 | * @param int $post_id Post ID |
| 430 | * @return string Post Excerpt. |
| 431 | */ |
| 432 | protected function get_post_excerpt( $post_id, $words ) { |
| 433 | global $post; |
| 434 | |
| 435 | $save_post = $post; |
| 436 | $post = get_post( $post_id ); |
| 437 | |
| 438 | setup_postdata( $post ); |
| 439 | |
| 440 | $excerpt = get_the_excerpt(); |
| 441 | $post = $save_post; |
| 442 | |
| 443 | if ( $post ) { |
| 444 | setup_postdata( $post ); |
| 445 | } |
| 446 | |
| 447 | $output = $this->get_shortened_string( $excerpt, $words ); |
| 448 | |
| 449 | return $output; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * @see wp_trim_words(); |
| 454 | * @since 1.0 |
| 455 | * @return string Trimmed text. |
| 456 | */ |
| 457 | protected function get_shortened_string( $text = '', $num_words = 30, $more = null ) { |
| 458 | if ( ! $text ) { |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | return wp_trim_words( $text, $num_words, $more ); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @since 1.3.1 |
| 467 | * @param string $name |
| 468 | * @param string $title |
| 469 | * @return string HTML img element |
| 470 | */ |
| 471 | public function get_asset_image( $name = '', $title = '' ) { |
| 472 | |
| 473 | if ( ! $name ) { |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | return sprintf( "<img alt='' src='%s' title='%s'/>", CPAC_URL . "assets/images/{$name}", esc_attr( $title ) ); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @since 1.2.0 |
| 482 | * @param string $url |
| 483 | * @return bool |
| 484 | */ |
| 485 | protected function is_image( $url ) { |
| 486 | |
| 487 | if ( ! is_string( $url ) ) { |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | $validExt = array('.jpg', '.jpeg', '.gif', '.png', '.bmp'); |
| 492 | $ext = strrchr( $url, '.' ); |
| 493 | |
| 494 | return in_array( $ext, $validExt ); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @since 1.0 |
| 499 | * @return array Image Sizes. |
| 500 | */ |
| 501 | public function get_all_image_sizes() { |
| 502 | $image_sizes = array( |
| 503 | 'thumbnail' => __( "Thumbnail", 'cpac' ), |
| 504 | 'medium' => __( "Medium", 'cpac' ), |
| 505 | 'large' => __( "Large", 'cpac' ), |
| 506 | 'full' => __( "Full", 'cpac' ) |
| 507 | ); |
| 508 | |
| 509 | foreach( get_intermediate_image_sizes() as $size ) { |
| 510 | if ( ! isset( $image_sizes[$size] ) ) { |
| 511 | $image_sizes[$size] = ucwords( str_replace( '-', ' ', $size) ); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return $image_sizes; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * @since 2.0 |
| 520 | * @param string $name |
| 521 | * @return array Image Sizes |
| 522 | */ |
| 523 | public function get_image_size_by_name( $name = '' ) { |
| 524 | |
| 525 | if ( ! $name || is_array( $name ) ) { |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | global $_wp_additional_image_sizes; |
| 530 | |
| 531 | if ( ! isset( $_wp_additional_image_sizes[ $name ] ) ) { |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | return $_wp_additional_image_sizes[ $name ]; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * @see image_resize() |
| 540 | * @since 2.0 |
| 541 | * @return string Image URL |
| 542 | */ |
| 543 | public function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { |
| 544 | $resized = false; |
| 545 | $editor = wp_get_image_editor( $file ); |
| 546 | |
| 547 | if ( is_wp_error( $editor ) ) |
| 548 | return false; |
| 549 | |
| 550 | $editor->set_quality( $jpeg_quality ); |
| 551 | |
| 552 | $resized = $editor->resize( $max_w, $max_h, $crop ); |
| 553 | if ( is_wp_error( $resized ) ) |
| 554 | return false; |
| 555 | |
| 556 | $dest_file = $editor->generate_filename( $suffix, $dest_path ); |
| 557 | |
| 558 | $saved = $editor->save( $dest_file ); |
| 559 | |
| 560 | if ( is_wp_error( $saved ) ) |
| 561 | return false; |
| 562 | |
| 563 | $resized = $dest_file; |
| 564 | |
| 565 | return $resized; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * @since 1.0 |
| 570 | * @param mixed $meta Image files or Image ID's |
| 571 | * @param array $args |
| 572 | * @return array HTML img elements |
| 573 | */ |
| 574 | public function get_thumbnails( $images, $args = array() ) { |
| 575 | $thumbnails = array(); |
| 576 | |
| 577 | if ( empty( $images ) || 'false' == $images ) { |
| 578 | return $thumbnails; |
| 579 | } |
| 580 | |
| 581 | // turn string to array |
| 582 | if ( is_string( $images ) ) { |
| 583 | if ( strpos( $images, ',' ) !== false ) { |
| 584 | $images = array_filter( explode( ',', $this->strip_trim( str_replace( ' ', '', $images ) ) ) ); |
| 585 | } |
| 586 | else { |
| 587 | $images = array( $images ); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Image size |
| 592 | $defaults = array( |
| 593 | 'image_size' => 'cpac-custom', |
| 594 | 'image_size_w' => 80, |
| 595 | 'image_size_h' => 80, |
| 596 | ); |
| 597 | $args = wp_parse_args( $args, $defaults ); |
| 598 | |
| 599 | extract( $args ); |
| 600 | |
| 601 | foreach( $images as $value ) { |
| 602 | if ( $this->is_image( $value ) ) { |
| 603 | |
| 604 | // get dimensions from image_size |
| 605 | if ( $sizes = $this->get_image_size_by_name( $image_size ) ) { |
| 606 | $image_size_w = $sizes['width']; |
| 607 | $image_size_h = $sizes['height']; |
| 608 | } |
| 609 | |
| 610 | $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $value ); |
| 611 | |
| 612 | if ( is_file( $image_path ) ) { |
| 613 | |
| 614 | // try to resize image |
| 615 | if ( $resized = $this->image_resize( $image_path, $image_size_w, $image_size_h, true ) ) { |
| 616 | $thumbnails[] = "<img src='" . str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized ) . "' alt='' width='{$image_size_w}' height='{$image_size_h}' />"; |
| 617 | } |
| 618 | |
| 619 | // return full image with maxed dimensions |
| 620 | else { |
| 621 | $thumbnails[] = "<img src='{$value}' alt='' style='max-width:{$image_size_w}px;max-height:{$image_size_h}px' />"; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // Media Attachment |
| 627 | elseif ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) { |
| 628 | |
| 629 | if ( ! $image_size || 'cpac-custom' == $image_size ) { |
| 630 | $width = $image_size_w; |
| 631 | $height = $image_size_h; |
| 632 | |
| 633 | // to make sure wp_get_attachment_image_src() get the image with matching dimensions. |
| 634 | $image_size = array( $width, $height ); |
| 635 | } |
| 636 | |
| 637 | // image attributes |
| 638 | $attributes = wp_get_attachment_image_src( $value, $image_size ); |
| 639 | $src = $attributes[0]; |
| 640 | $width = $attributes[1]; |
| 641 | $height = $attributes[2]; |
| 642 | |
| 643 | // image size by name |
| 644 | if ( $sizes = $this->get_image_size_by_name( $image_size ) ) { |
| 645 | $width = $sizes['width']; |
| 646 | $height = $sizes['height']; |
| 647 | } |
| 648 | |
| 649 | // maximum dimensions |
| 650 | $max = max( array( $width, $height ) ); |
| 651 | |
| 652 | $thumbnails[] = "<span class='cpac-column-value-image' style='width:{$width}px;height:{$height}px;'><img style='max-width:{$max}px;max-height:{$max}px;' src='{$attributes[0]}' alt=''/></span>"; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return $thumbnails; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Implode for multi dimensional array |
| 661 | * |
| 662 | * @since 1.0 |
| 663 | * @param string $glue |
| 664 | * @param array $pieces |
| 665 | * @return string Imploded array |
| 666 | */ |
| 667 | protected function recursive_implode( $glue, $pieces ) { |
| 668 | foreach( $pieces as $r_pieces ) { |
| 669 | if ( is_array( $r_pieces ) ) { |
| 670 | $retVal[] = $this->recursive_implode( $glue, $r_pieces ); |
| 671 | } |
| 672 | else { |
| 673 | $retVal[] = $r_pieces; |
| 674 | } |
| 675 | } |
| 676 | if ( isset($retVal) && is_array( $retVal ) ) { |
| 677 | return implode( $glue, $retVal ); |
| 678 | } |
| 679 | |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Get timestamp |
| 685 | * |
| 686 | * @since 2.0 |
| 687 | * @param string $date |
| 688 | * @return string Formatted date |
| 689 | */ |
| 690 | private function get_timestamp( $date ) { |
| 691 | |
| 692 | if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) ) { |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch |
| 697 | // See http://api.jqueryui.com/datepicker/#utility-formatDate |
| 698 | // credits: nmarks |
| 699 | if ( is_numeric( $date ) && 13 === strlen( trim( $date ) ) ) { |
| 700 | $date = substr( $date, 0, -3 ); |
| 701 | } |
| 702 | |
| 703 | // Parse with strtotime if it's: |
| 704 | // - not numeric ( like a unixtimestamp ) |
| 705 | // - date format: yyyymmdd ( format used by ACF ) must start with 19xx or 20xx and is 8 long |
| 706 | |
| 707 | // @todo: in theory a numeric string of 8 can also be a unixtimestamp. |
| 708 | // we need to replace this with an option to mark a date as unixtimestamp. |
| 709 | if ( ! is_numeric( $date ) || ( is_numeric( $date ) && strlen( trim( $date ) ) == 8 && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) ) { |
| 710 | $date = strtotime( $date ); |
| 711 | } |
| 712 | |
| 713 | return $date; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * @since 1.3.1 |
| 718 | * @param string $date |
| 719 | * @return string Formatted date |
| 720 | */ |
| 721 | protected function get_date( $date, $format = '' ) { |
| 722 | |
| 723 | if ( ! $date = $this->get_timestamp( $date ) ) { |
| 724 | return false; |
| 725 | } |
| 726 | if ( ! $format ) { |
| 727 | $format = get_option( 'date_format' ); |
| 728 | } |
| 729 | |
| 730 | return date_i18n( $format, $date ); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * @since 1.3.1 |
| 735 | * @param string $date |
| 736 | * @return string Formatted time |
| 737 | */ |
| 738 | protected function get_time( $date, $format = '' ) { |
| 739 | |
| 740 | if ( ! $date = $this->get_timestamp( $date ) ) { |
| 741 | return false; |
| 742 | } |
| 743 | if ( ! $format ) { |
| 744 | $format = get_option( 'time_format' ); |
| 745 | } |
| 746 | |
| 747 | return date_i18n( $format, $date ); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * @since 2.0 |
| 752 | * @param string $field_key |
| 753 | * @return string Attribute Name |
| 754 | */ |
| 755 | function label_view( $label, $description = '', $pointer = '' ) { |
| 756 | ?> |
| 757 | <td class="label"> |
| 758 | <label for="<?php $this->attr_id( $pointer ); ?>"> |
| 759 | <?php echo stripslashes( $label ); ?> |
| 760 | |
| 761 | <?php if( $description ) : ?><p class="description"><?php echo $description; ?></p><?php endif; ?> |
| 762 | </label> |
| 763 | </td> |
| 764 | <?php |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * @since 2.0 |
| 769 | */ |
| 770 | function display_field_date_format() { |
| 771 | |
| 772 | $field_key = 'date_format'; |
| 773 | $label = __( 'Date Format', 'cpac' ); |
| 774 | $description = __( 'This will determine how the date will be displayed.', 'cpac' ); |
| 775 | |
| 776 | ?> |
| 777 | <tr class="column_<?php echo $field_key; ?>"> |
| 778 | <?php $this->label_view( $label, $description, $field_key ); ?> |
| 779 | <td class="input"> |
| 780 | <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->date_format; ?>" placeholder="<?php _e( 'Example:', 'cpac' ); ?> d M Y H:i"/> |
| 781 | <p class="description"> |
| 782 | <?php printf( __( 'Leave empty for WordPress date format, change your <a href="%s">default date format here</a>.' , 'cpac' ), admin_url( 'options-general.php' ) . '#date_format_custom_radio' ); ?> |
| 783 | <a target='_blank' href='http://codex.wordpress.org/Formatting_Date_and_Time'><?php _e( 'Documentation on date and time formatting.', 'cpac' ); ?></a> |
| 784 | </p> |
| 785 | </td> |
| 786 | </tr> |
| 787 | |
| 788 | <?php |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * @since 2.0 |
| 793 | */ |
| 794 | function display_field_excerpt_length() { |
| 795 | |
| 796 | $field_key = 'excerpt_length'; |
| 797 | $label = __( 'Excerpt length', 'cpac' ); |
| 798 | $description = __( 'Number of words', 'cpac' ); |
| 799 | |
| 800 | ?> |
| 801 | <tr class="column_<?php echo $field_key; ?>"> |
| 802 | <?php $this->label_view( $label, $description, $field_key ); ?> |
| 803 | <td class="input"> |
| 804 | <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->excerpt_length; ?>"/> |
| 805 | </td> |
| 806 | </tr> |
| 807 | <?php |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * @since 2.0 |
| 812 | */ |
| 813 | function display_field_preview_size() { |
| 814 | |
| 815 | $field_key = 'image_size'; |
| 816 | $label = __( 'Preview size', 'cpac' ); |
| 817 | |
| 818 | ?> |
| 819 | <tr class="column_<?php echo $field_key; ?>"> |
| 820 | |
| 821 | <?php $this->label_view( $label, '', $field_key ); ?> |
| 822 | |
| 823 | <td class="input"> |
| 824 | <?php foreach ( $sizes = $this->get_all_image_sizes() as $id => $image_label ) : ?> |
| 825 | <label for="<?php $this->attr_id( $field_key ); ?>-<?php echo $id ?>" class="custom-size"> |
| 826 | <input type="radio" value="<?php echo $id; ?>" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-<?php echo $id ?>"<?php checked( $this->options->image_size, $id ); ?>> |
| 827 | <?php echo $image_label; ?> |
| 828 | </label> |
| 829 | <?php endforeach; ?> |
| 830 | |
| 831 | <div class="custom_image_size"> |
| 832 | <label for="<?php $this->attr_id( $field_key ); ?>-custom" class="custom-size image-size-custom" > |
| 833 | <input type="radio" value="cpac-custom" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-custom"<?php checked( $this->options->image_size, 'cpac-custom' ); ?>><?php _e( 'Custom', 'cpac' ); ?> |
| 834 | </label> |
| 835 | <label for="<?php $this->attr_id( $field_key ); ?>-w" class="custom-size-w<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>"> |
| 836 | <input type="text" name="<?php $this->attr_name( 'image_size_w' ); ?>" id="<?php $this->attr_id( $field_key ); ?>-w" value="<?php echo $this->options->image_size_w; ?>" /><?php _e( 'width', 'cpac' ); ?> |
| 837 | </label> |
| 838 | <label for="<?php $this->attr_id( $field_key ); ?>-h" class="custom-size-h<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>"> |
| 839 | <input type="text" name="<?php $this->attr_name( 'image_size_h' ); ?>" id="<?php $this->attr_id( $field_key ); ?>-h" value="<?php echo $this->options->image_size_h; ?>" /><?php _e( 'height', 'cpac' ); ?> |
| 840 | </label> |
| 841 | </div> |
| 842 | </td> |
| 843 | </tr> |
| 844 | <?php |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * @since 2.1.1 |
| 849 | */ |
| 850 | function display_field_before_after() { |
| 851 | ?> |
| 852 | <tr class="column_before"> |
| 853 | <?php $this->label_view( __( "Before", 'cpac' ), __( 'This text will appear before the custom field value.', 'cpac' ), 'before' ); ?> |
| 854 | <td class="input"> |
| 855 | <input type="text" class="cpac-before" name="<?php $this->attr_name( 'before' ); ?>" id="<?php $this->attr_id( 'before' ); ?>" value="<?php echo esc_attr( stripslashes( $this->options->before ) ); ?>"/> |
| 856 | </td> |
| 857 | </tr> |
| 858 | <tr class="column_after"> |
| 859 | <?php $this->label_view( __( "After", 'cpac' ), __( 'This text will appear after the custom field value.', 'cpac' ), 'after' ); ?> |
| 860 | <td class="input"> |
| 861 | <input type="text" class="cpac-after" name="<?php $this->attr_name( 'after' ); ?>" id="<?php $this->attr_id( 'after' ); ?>" value="<?php echo esc_attr( stripslashes( $this->options->after ) ); ?>"/> |
| 862 | </td> |
| 863 | </tr> |
| 864 | <?php |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * @since 2.0 |
| 869 | * @param array Column Objects |
| 870 | * @return string HTML List |
| 871 | */ |
| 872 | public function get_column_list( $columns = array(), $label = '' ) { |
| 873 | |
| 874 | if ( empty( $columns ) ) { |
| 875 | return false; |
| 876 | } |
| 877 | |
| 878 | $list = ''; |
| 879 | |
| 880 | // sort by alphabet |
| 881 | $_columns = array(); |
| 882 | foreach ( $columns as $column ) { |
| 883 | $_columns[ $column->properties->type ] = 0 === strlen( strip_tags( $column->properties->label ) ) ? ucfirst( $column->properties->type ) : $column->properties->label; |
| 884 | } |
| 885 | asort( $_columns ); |
| 886 | |
| 887 | $list = "<optgroup label='{$label}'>"; |
| 888 | foreach ( $_columns as $type => $label ){ |
| 889 | $selected = selected( $this->properties->type, $type, false ); |
| 890 | $list .= "<option value='{$type}'{$selected}>{$label}</option>"; |
| 891 | } |
| 892 | $list .= "</optgroup>"; |
| 893 | |
| 894 | return $list; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * @since 2.0 |
| 899 | */ |
| 900 | public function display() { |
| 901 | |
| 902 | $classes = implode( ' ', array_filter( array ( "cpac-box-{$this->properties->type}", $this->properties->classes ) ) ); |
| 903 | |
| 904 | // column list |
| 905 | $column_list = ''; |
| 906 | |
| 907 | $groups = $this->storage_model->get_column_type_groups(); |
| 908 | |
| 909 | foreach ( $groups as $group => $label ) { |
| 910 | $column_list .= $this->get_column_list( $this->storage_model->column_types[ $group ], $label ); |
| 911 | } |
| 912 | |
| 913 | // clone attribute |
| 914 | $data_clone = $this->properties->is_cloneable ? " data-clone='{$this->properties->clone}'" : ''; |
| 915 | |
| 916 | ?> |
| 917 | <div class="cpac-column <?php echo $classes; ?>" data-type="<?php echo $this->properties->type; ?>"<?php echo $data_clone; ?>> |
| 918 | <input type="hidden" class="column-name" name="<?php echo $this->attr_name( 'column-name' ); ?>" value="<?php echo esc_attr( $this->properties->name ); ?>" /> |
| 919 | <input type="hidden" class="type" name="<?php echo $this->attr_name( 'type' ); ?>" value="<?php echo $this->properties->type; ?>" /> |
| 920 | <input type="hidden" class="clone" name="<?php echo $this->attr_name( 'clone' ); ?>" value="<?php echo $this->properties->clone; ?>" /> |
| 921 | <div class="column-meta"> |
| 922 | <table class="widefat"> |
| 923 | <tbody> |
| 924 | <tr> |
| 925 | <td class="column_sort"></td> |
| 926 | <td class="column_label"> |
| 927 | <div class="inner"> |
| 928 | <div class="meta"> |
| 929 | |
| 930 | <?php |
| 931 | /** |
| 932 | * Fires in the meta-element for column options, which is displayed right after the column label |
| 933 | * |
| 934 | * @since 2.0 |
| 935 | * |
| 936 | * @param CPAC_Column $column_instance Column class instance |
| 937 | */ |
| 938 | do_action( 'cac/column/settings_meta', $this ); |
| 939 | |
| 940 | /** |
| 941 | * @deprecated 2.2 Use cac/column/settings_meta instead |
| 942 | */ |
| 943 | do_action( 'cac/column/label', $this ); |
| 944 | ?> |
| 945 | |
| 946 | </div> |
| 947 | <a class="toggle" href="javascript:;"><?php echo stripslashes( $this->get_label() ); ?></a> |
| 948 | <a class="edit-button" href="javascript:;"><?php _e( 'Edit', 'cpac' ); ?></a> |
| 949 | <a class="remove-button" href="javascript:;"><?php _e( 'Remove', 'cpac' ); ?></a> |
| 950 | </div> |
| 951 | </td> |
| 952 | <td class="column_type"> |
| 953 | <div class="inner"> |
| 954 | <a href="#"><?php echo stripslashes( $this->properties->label ); ?></a> |
| 955 | </div> |
| 956 | </td> |
| 957 | <td class="column_edit"></td> |
| 958 | </tr> |
| 959 | </tbody> |
| 960 | </table> |
| 961 | </div><!--.column-meta--> |
| 962 | |
| 963 | <div class="column-form"> |
| 964 | <table class="widefat"> |
| 965 | <tbody> |
| 966 | <tr class="column_type"> |
| 967 | <?php $this->label_view( __( 'Type', 'cpac' ), __( 'Choose a column type.', 'cpac' ) . '<em>' . __('ID','cpac') . ': ' . $this->properties->type . '</em>', 'type' ); ?> |
| 968 | <td class="input"> |
| 969 | <select name="<?php $this->attr_name( 'type' ); ?>" id="<?php $this->attr_id( 'type' ); ?>"> |
| 970 | <?php echo $column_list; ?> |
| 971 | </select> |
| 972 | <div class="msg"></div> |
| 973 | </td> |
| 974 | </tr><!--.column_label--> |
| 975 | |
| 976 | <tr class="column_label<?php echo $this->properties->hide_label ? ' hidden' : ''; ?>"> |
| 977 | <?php $this->label_view( __( 'Label', 'cpac' ), __( 'This is the name which will appear as the column header.', 'cpac' ), 'label' ); ?> |
| 978 | <td class="input"> |
| 979 | <input class="text" type="text" name="<?php $this->attr_name( 'label' ); ?>" id="<?php $this->attr_id( 'label' ); ?>" value="<?php echo esc_attr( $this->options->label ); //echo sanitize_text_field( $this->options->label ); ?>" /> |
| 980 | </td> |
| 981 | </tr><!--.column_label--> |
| 982 | |
| 983 | <tr class="column_width"> |
| 984 | <?php $this->label_view( __( 'Width', 'cpac' ), '', 'width' ); ?> |
| 985 | <td class="input"> |
| 986 | <div class="description width-decription" title="<?php _e( 'default', 'cpac' ); ?>"> |
| 987 | <?php echo $this->options->width > 0 ? $this->options->width . '%' : __( 'default', 'cpac' ); ?> |
| 988 | </div> |
| 989 | <div class="input-width-range"></div> |
| 990 | <input type="hidden" class="input-width" name="<?php $this->attr_name( 'width' ); ?>" id="<?php $this->attr_id( 'width' ); ?>" value="<?php echo $this->options->width; ?>" /> |
| 991 | |
| 992 | </td> |
| 993 | </tr><!--.column_width--> |
| 994 | |
| 995 | <?php |
| 996 | /** |
| 997 | * Fires directly before the custom options for a column are displayed in the column form |
| 998 | * |
| 999 | * @since 2.0 |
| 1000 | * @param CPAC_Column $column_instance Column class instance |
| 1001 | */ |
| 1002 | do_action( 'cac/column/settings_before', $this ); |
| 1003 | ?> |
| 1004 | |
| 1005 | <?php |
| 1006 | /** |
| 1007 | * Load specific column settings. |
| 1008 | * |
| 1009 | */ |
| 1010 | $this->display_settings(); |
| 1011 | |
| 1012 | ?> |
| 1013 | |
| 1014 | <?php |
| 1015 | /** |
| 1016 | * Fires directly after the custom options for a column are displayed in the column form |
| 1017 | * |
| 1018 | * @since 2.0 |
| 1019 | * @param CPAC_Column $column_instance Column class instance |
| 1020 | */ |
| 1021 | do_action( 'cac/column/settings_after', $this ); |
| 1022 | ?> |
| 1023 | |
| 1024 | <tr class="column_action"> |
| 1025 | <td colspan="2"> |
| 1026 | <p> |
| 1027 | <a href="javascript:;" class="remove-button"><?php _e( 'Remove' );?></a> |
| 1028 | </p> |
| 1029 | </td> |
| 1030 | </tr> |
| 1031 | |
| 1032 | </tbody> |
| 1033 | </table> |
| 1034 | </div><!--.column-form--> |
| 1035 | </div><!--.cpac-column--> |
| 1036 | <?php |
| 1037 | } |
| 1038 | } |