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