column
10 years ago
storage_model
10 years ago
third_party
10 years ago
addons.php
10 years ago
column.php
10 years ago
review_notice.php
10 years ago
settings.php
10 years ago
storage_model.php
10 years ago
third_party.php
10 years ago
upgrade.php
10 years ago
utility.php
10 years ago
column.php
1640 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * CPAC_Column class |
| 5 | * |
| 6 | * @since 2.0 |
| 7 | * |
| 8 | * @param object $storage_model CPAC_Storage_Model |
| 9 | */ |
| 10 | class CPAC_Column { |
| 11 | |
| 12 | /** |
| 13 | * A Storage Model can be a Posttype, User, Comment, Link or Media storage type. |
| 14 | * |
| 15 | * @since 2.0 |
| 16 | * @var CPAC_Storage_Model $storage_model contains a CPAC_Storage_Model object which the column belongs too. |
| 17 | */ |
| 18 | public $storage_model; |
| 19 | |
| 20 | /** |
| 21 | * @since 2.0 |
| 22 | * @var array $options contains the user set options for the CPAC_Column object. |
| 23 | */ |
| 24 | public $options = array(); |
| 25 | |
| 26 | /** |
| 27 | * @since 2.0 |
| 28 | * @var object $options_default contains the options for the CPAC_Column object before they are populated with user input. |
| 29 | */ |
| 30 | protected $options_default; |
| 31 | |
| 32 | /** |
| 33 | * @since 2.0 |
| 34 | * @var array $properties describes the fixed properties for the CPAC_Column object. |
| 35 | */ |
| 36 | public $properties = array(); |
| 37 | |
| 38 | /** |
| 39 | * @since 2.4.7 |
| 40 | */ |
| 41 | protected $filtering_model; |
| 42 | |
| 43 | /** |
| 44 | * @since 2.4.8 |
| 45 | */ |
| 46 | protected $editable_model; |
| 47 | |
| 48 | /** |
| 49 | * @since 2.0 |
| 50 | * |
| 51 | * @param int $id ID |
| 52 | * |
| 53 | * @return string Value |
| 54 | */ |
| 55 | public function get_value( $id ) { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the raw, underlying value for the column |
| 60 | * Not suitable for direct display, use get_value() for that |
| 61 | * |
| 62 | * @since 2.0.3 |
| 63 | * |
| 64 | * @param int $id ID |
| 65 | * |
| 66 | * @return mixed Value |
| 67 | */ |
| 68 | public function get_raw_value( $id ) { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 2.0 |
| 73 | */ |
| 74 | protected function display_settings() { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Overwrite this function in child class to sanitize |
| 79 | * user submitted values. |
| 80 | * |
| 81 | * @since 2.0 |
| 82 | * |
| 83 | * @param $options array User submitted column options |
| 84 | * |
| 85 | * @return array Options |
| 86 | */ |
| 87 | protected function sanitize_options( $options ) { |
| 88 | |
| 89 | if ( isset( $options['date_format'] ) ) { |
| 90 | $options['date_format'] = trim( $options['date_format'] ); |
| 91 | } |
| 92 | |
| 93 | if ( isset( $options['width'] ) ) { |
| 94 | $options['width'] = trim( $options['width'] ); |
| 95 | if ( ! is_numeric( $options['width'] ) ) { |
| 96 | $options['width'] = ''; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $options; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Overwrite this function in child class. |
| 105 | * Determine whether this column type should be available |
| 106 | * |
| 107 | * @since 2.2 |
| 108 | * |
| 109 | * @return bool Whether the column type should be available |
| 110 | */ |
| 111 | public function apply_conditional() { |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Overwrite this function in child class. |
| 118 | * Adds (optional) scripts to the listings screen. |
| 119 | * |
| 120 | * @since 2.3.4 |
| 121 | */ |
| 122 | public function scripts() { |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * An object copy (clone) is created for creating multiple column instances. |
| 127 | * |
| 128 | * @since 2.0 |
| 129 | */ |
| 130 | public function __clone() { |
| 131 | |
| 132 | // Force a copy of this->object, otherwise it will point to same object. |
| 133 | $this->options = clone $this->options; |
| 134 | $this->properties = clone $this->properties; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @since 2.0 |
| 139 | * |
| 140 | * @param object $storage_model CPAC_Storage_Model |
| 141 | */ |
| 142 | public function __construct( CPAC_Storage_Model $storage_model ) { |
| 143 | |
| 144 | $this->storage_model = $storage_model; |
| 145 | |
| 146 | $this->init(); |
| 147 | $this->after_setup(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @since 2.2 |
| 152 | */ |
| 153 | public function init() { |
| 154 | |
| 155 | // Default properties |
| 156 | $default_properties = array( |
| 157 | 'clone' => null, // Unique clone ID |
| 158 | 'type' => null, // Unique type |
| 159 | 'name' => null, // Unique name |
| 160 | 'label' => null, // Label which describes this column. |
| 161 | 'classes' => null, // Custom CSS classes for this column. |
| 162 | 'hide_label' => false, // Should the Label be hidden? |
| 163 | 'is_registered' => true, // Should the column be registered based on conditional logic, example usage see: 'post/page-template.php' |
| 164 | 'is_cloneable' => true, // Should the column be cloneable |
| 165 | 'default' => false, // Is this a WP default column, |
| 166 | 'group' => 'custom', |
| 167 | 'hidden' => false, |
| 168 | 'use_before_after' => false |
| 169 | ); |
| 170 | |
| 171 | // @since 2.4.7 |
| 172 | $default_properties = apply_filters( 'cac/column/default_properties', $default_properties ); |
| 173 | |
| 174 | foreach ( $default_properties as $property => $value ) { |
| 175 | $this->properties[ $property ] = $value; |
| 176 | } |
| 177 | |
| 178 | // Default options |
| 179 | $default_options = array( |
| 180 | 'before' => '', // Before field |
| 181 | 'after' => '', // After field |
| 182 | 'width' => null, // Width for this column. |
| 183 | 'width_unit' => '%', // Unit for width; pecentage (%) or pixels (px). |
| 184 | 'state' => 'off' // Active state for this column. |
| 185 | ); |
| 186 | |
| 187 | /** |
| 188 | * Filter the default options for a column instance, such as label and width |
| 189 | * |
| 190 | * @since 2.2 |
| 191 | * |
| 192 | * @param array $default_options Default column options |
| 193 | * @param CPAC_Storage_Model $storage_model Storage Model class instance |
| 194 | */ |
| 195 | $default_options = apply_filters( 'cac/column/default_options', $default_options ); // do not pass $this because object is not ready |
| 196 | |
| 197 | foreach ( $default_options as $option => $value ) { |
| 198 | $this->options[ $option ] = $value; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * After Setup |
| 204 | * |
| 205 | */ |
| 206 | public function after_setup() { |
| 207 | |
| 208 | // Column name defaults to column type |
| 209 | if ( ! isset( $this->properties['name'] ) ) { |
| 210 | $this->properties['name'] = $this->properties['type']; |
| 211 | } |
| 212 | |
| 213 | // Check whether the column should be available |
| 214 | $this->properties['is_registered'] = $this->apply_conditional(); |
| 215 | |
| 216 | /** |
| 217 | * Filter the properties of a column type, such as type and is_cloneable |
| 218 | * Property $column_instance added in Admin Columns 2.2 |
| 219 | * |
| 220 | * @since 2.0 |
| 221 | * |
| 222 | * @param array $properties Column properties |
| 223 | * @param CPAC_Storage_Model $storage_model Storage Model class instance |
| 224 | */ |
| 225 | $this->properties = apply_filters( 'cac/column/properties', $this->properties, $this ); // do not pass $this because object is not ready |
| 226 | |
| 227 | /** |
| 228 | * Filter the properties of a column type for a specific storage model |
| 229 | * Property $column_instance added in Admin Columns 2.2 |
| 230 | * |
| 231 | * @since 2.0 |
| 232 | * @see Filter cac/column/properties |
| 233 | */ |
| 234 | $this->properties = apply_filters( "cac/column/properties/storage_key={$this->storage_model->key}", $this->properties, $this ); // do not pass $this because object is not ready |
| 235 | |
| 236 | // Column label defaults to column type label |
| 237 | if ( ! isset( $this->options['label'] ) ) { |
| 238 | $this->options['label'] = $this->properties['label']; |
| 239 | } |
| 240 | |
| 241 | // Convert properties and options arrays to object |
| 242 | $this->options = (object) $this->options; |
| 243 | $this->properties = (object) $this->properties; |
| 244 | |
| 245 | // Filters |
| 246 | foreach ( $this->properties as $name => $value ) { |
| 247 | $this->properties->{$name} = apply_filters( "cac/column/properties/{$name}", $value, $this ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param string $property |
| 253 | * |
| 254 | * @return mixed $value |
| 255 | */ |
| 256 | public function set_properties( $property, $value ) { |
| 257 | $this->properties->{$property} = $value; |
| 258 | |
| 259 | return $this; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param string $option |
| 264 | * |
| 265 | * @return mixed $value |
| 266 | */ |
| 267 | public function set_options( $option, $value ) { |
| 268 | $this->options->{$option} = $value; |
| 269 | |
| 270 | return $this; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @since 2.4.7 |
| 275 | */ |
| 276 | public function set_filter( $filtering_model ) { |
| 277 | $this->filtering_model = $filtering_model; |
| 278 | |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @since 2.4.7 |
| 284 | */ |
| 285 | public function get_filter() { |
| 286 | return $this->filtering_model; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @since 2.4.8 |
| 291 | */ |
| 292 | public function set_editable( $editable_model ) { |
| 293 | $this->editable_model = $editable_model; |
| 294 | |
| 295 | return $this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @since 2.4.8 |
| 300 | */ |
| 301 | public function get_editable() { |
| 302 | return $this->editable_model; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @param int $id |
| 307 | * |
| 308 | * @return object |
| 309 | */ |
| 310 | public function set_clone( $id = null ) { |
| 311 | |
| 312 | if ( $id !== null && $id > 0 ) { |
| 313 | $this->properties->name = "{$this->properties->type}-{$id}"; |
| 314 | $this->properties->clone = $id; |
| 315 | } |
| 316 | |
| 317 | return $this; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @since 2.4.9 |
| 322 | */ |
| 323 | public function get_display_value( $id ) { |
| 324 | $value = $this->get_value( $id ); |
| 325 | |
| 326 | // add before and after string |
| 327 | if ( $value ) { |
| 328 | $value = $this->get_before() . $value . $this->get_after(); |
| 329 | } |
| 330 | |
| 331 | return $value; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @since 1.0 |
| 336 | */ |
| 337 | public function get_before() { |
| 338 | return isset( $this->options->before ) ? stripslashes( $this->options->before ) : false; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @since 1.0 |
| 343 | */ |
| 344 | public function get_after() { |
| 345 | return isset( $this->options->after ) ? stripslashes( $this->options->after ) : false; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Get the type of the column. |
| 350 | * |
| 351 | * @since 2.3.4 |
| 352 | */ |
| 353 | public function get_type() { |
| 354 | return $this->properties->type; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Get the name of the column. |
| 359 | * |
| 360 | * @since 2.3.4 |
| 361 | */ |
| 362 | public function get_name() { |
| 363 | return $this->properties->name; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Get the type of the column. |
| 368 | * |
| 369 | * @since 2.4.9 |
| 370 | */ |
| 371 | public function get_type_label() { |
| 372 | return $this->properties->label; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Get the column options set by the user |
| 377 | * |
| 378 | * @since 2.3.4 |
| 379 | * @return object Column options set by user |
| 380 | */ |
| 381 | public function get_options() { |
| 382 | return $this->options; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Get a single column option |
| 387 | * |
| 388 | * @since 2.3.4 |
| 389 | * @return array Column options set by user |
| 390 | */ |
| 391 | public function get_option( $name ) { |
| 392 | return isset( $this->options->{$name} ) ? $this->options->{$name} : false; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Get a single column option |
| 397 | * |
| 398 | * @since 2.4.8 |
| 399 | * @return array Column options set by user |
| 400 | */ |
| 401 | public function get_property( $name ) { |
| 402 | return isset( $this->properties->{$name} ) ? $this->properties->{$name} : false; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Checks column type |
| 407 | * |
| 408 | * @since 2.3.4 |
| 409 | * |
| 410 | * @param string $type Column type. Also work without the 'column-' prefix. Example 'column-meta' or 'meta'. |
| 411 | * |
| 412 | * @return bool Matches column type |
| 413 | */ |
| 414 | public function is_type( $type ) { |
| 415 | return ( $type === $this->get_type() ) || ( 'column-' . $type === $this->get_type() ); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @since 2.1.1 |
| 420 | */ |
| 421 | public function get_post_type() { |
| 422 | return $this->storage_model->get_post_type(); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @since 2.3.4 |
| 427 | */ |
| 428 | public function get_storage_model() { |
| 429 | return $this->storage_model; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @since 2.3.4 |
| 434 | */ |
| 435 | public function get_storage_model_type() { |
| 436 | return $this->storage_model->get_type(); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @since 2.3.4 |
| 441 | */ |
| 442 | public function get_storage_model_meta_type() { |
| 443 | return $this->storage_model->get_meta_type(); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @param string $field_key |
| 448 | * |
| 449 | * @return void |
| 450 | */ |
| 451 | public function attr_name( $field_name ) { |
| 452 | echo $this->get_attr_name( $field_name ); |
| 453 | } |
| 454 | |
| 455 | public function get_attr_name( $field_name ) { |
| 456 | return "{$this->storage_model->key}[{$this->properties->name}][{$field_name}]"; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @param string $field_key |
| 461 | * |
| 462 | * @return string Attribute Name |
| 463 | */ |
| 464 | public function get_attr_id( $field_name ) { |
| 465 | return "cpac-{$this->storage_model->key}-{$this->properties->name}-{$field_name}"; |
| 466 | } |
| 467 | |
| 468 | public function attr_id( $field_name ) { |
| 469 | echo $this->get_attr_id( $field_name ); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @since 2.0 |
| 474 | */ |
| 475 | public function sanitize_label() { |
| 476 | // check if original label has changed. Example WPML adds a language column, the column heading will have to display the added flag. |
| 477 | if ( $this->properties->hide_label && $this->properties->label !== $this->options->label ) { |
| 478 | $this->options->label = $this->properties->label; |
| 479 | } |
| 480 | |
| 481 | // replace urls, so export will not have to deal with them |
| 482 | $this->options->label = stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) ); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * @since 2.0 |
| 487 | * |
| 488 | * @param $options array User submitted column options |
| 489 | * |
| 490 | * @return array Options |
| 491 | */ |
| 492 | public function sanitize_storage( $options ) { |
| 493 | |
| 494 | // excerpt length must be numeric, else we will return it's default |
| 495 | if ( isset( $options['excerpt_length'] ) ) { |
| 496 | $options['excerpt_length'] = trim( $options['excerpt_length'] ); |
| 497 | if ( empty( $options['excerpt_length'] ) || ! is_numeric( $options['excerpt_length'] ) ) { |
| 498 | $options['excerpt_length'] = $this->options_default->excerpt_length; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | if ( ! empty( $options['label'] ) ) { |
| 503 | $options['label'] = str_replace( 'data:', '%%data%%', $options['label'] ); // Temporary replace data: urls for image sources. Replace it back later |
| 504 | |
| 505 | // Label can not contains the character ":"" and "'", because |
| 506 | // CPAC_Column::get_sanitized_label() will return an empty string |
| 507 | // and make an exception for site_url() |
| 508 | if ( false === strpos( $options['label'], site_url() ) ) { |
| 509 | $options['label'] = str_replace( ':', '', $options['label'] ); |
| 510 | $options['label'] = str_replace( "'", '', $options['label'] ); |
| 511 | } |
| 512 | $options['label'] = str_replace( '%%data%%', 'data:', $options['label'] ); // Enable data:image url's |
| 513 | } |
| 514 | |
| 515 | // used by child classes for additional sanitizing |
| 516 | $options = $this->sanitize_options( $options ); |
| 517 | |
| 518 | return $options; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * @since 2.0 |
| 523 | */ |
| 524 | public function get_label() { |
| 525 | |
| 526 | /** |
| 527 | * Filter the column instance label |
| 528 | * |
| 529 | * @since 2.0 |
| 530 | * |
| 531 | * @param string $label Column instance label |
| 532 | * @param CPAC_Column $column_instance Column class instance |
| 533 | */ |
| 534 | return apply_filters( 'cac/column/settings_label', stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) ), $this ); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Sanitizes label using intern wordpress function esc_url so it matches the label sorting url. |
| 539 | * |
| 540 | * @since 1.0 |
| 541 | * |
| 542 | * @param string $string |
| 543 | * |
| 544 | * @return string Sanitized string |
| 545 | */ |
| 546 | public function get_sanitized_label() { |
| 547 | |
| 548 | $string = $this->options->label; |
| 549 | $string = strip_tags( $string ); |
| 550 | $string = preg_replace( "/[^a-zA-Z0-9]+/", "", $string ); |
| 551 | $string = str_replace( 'http://', '', $string ); |
| 552 | $string = str_replace( 'https://', '', $string ); |
| 553 | |
| 554 | return $string; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @since 1.3.1 |
| 559 | */ |
| 560 | protected function get_shorten_url( $url = '' ) { |
| 561 | if ( ! $url ) { |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | return "<a title='{$url}' href='{$url}'>" . url_shorten( $url ) . "</a>"; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * @since 1.3 |
| 570 | */ |
| 571 | public function strip_trim( $string ) { |
| 572 | return trim( strip_tags( $string ) ); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * @since 2.2.1 |
| 577 | */ |
| 578 | protected function get_term_field( $field, $term_id, $taxonomy ) { |
| 579 | $term_field = get_term_field( $field, $term_id, $taxonomy, 'display' ); |
| 580 | if ( is_wp_error( $term_field ) ) { |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | return $term_field; |
| 585 | } |
| 586 | |
| 587 | // since 2.4.8 |
| 588 | public function get_raw_post_field( $field, $id ) { |
| 589 | global $wpdb; |
| 590 | |
| 591 | return $id ? $wpdb->get_var( $wpdb->prepare( "SELECT " . $wpdb->_real_escape( $field ) . " FROM {$wpdb->posts} WHERE ID = %d LIMIT 1", $id ) ) : false; |
| 592 | } |
| 593 | |
| 594 | // since 2.4.8 |
| 595 | public function get_post_title( $id ) { |
| 596 | return esc_html( $this->get_raw_post_field( 'post_title', $id ) ); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * @since 1.0 |
| 601 | * |
| 602 | * @param int $post_id Post ID |
| 603 | * |
| 604 | * @return string Post Excerpt. |
| 605 | */ |
| 606 | protected function get_post_excerpt( $post_id, $words ) { |
| 607 | global $post; |
| 608 | |
| 609 | $save_post = $post; |
| 610 | $post = get_post( $post_id ); |
| 611 | |
| 612 | setup_postdata( $post ); |
| 613 | |
| 614 | $excerpt = get_the_excerpt(); |
| 615 | $post = $save_post; |
| 616 | |
| 617 | if ( $post ) { |
| 618 | setup_postdata( $post ); |
| 619 | } |
| 620 | |
| 621 | $output = $this->get_shortened_string( $excerpt, $words ); |
| 622 | |
| 623 | return $output; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * @see wp_trim_words(); |
| 628 | * @since 1.0 |
| 629 | * @return string Trimmed text. |
| 630 | */ |
| 631 | protected function get_shortened_string( $text = '', $num_words = 30, $more = null ) { |
| 632 | if ( ! $text ) { |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | return wp_trim_words( $text, $num_words, $more ); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * @since 1.3.1 |
| 641 | * |
| 642 | * @param string $name |
| 643 | * @param string $title |
| 644 | * |
| 645 | * @return string HTML img element |
| 646 | */ |
| 647 | public function get_asset_image( $name = '', $title = '' ) { |
| 648 | |
| 649 | if ( ! $name ) { |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | return sprintf( "<img alt='' src='%s' title='%s'/>", CPAC_URL . "assets/images/{$name}", esc_attr( $title ) ); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * @since 3.4.4 |
| 658 | */ |
| 659 | public function get_user_postcount( $user_id, $post_type ) { |
| 660 | global $wpdb; |
| 661 | $sql = " |
| 662 | SELECT COUNT(ID) |
| 663 | FROM {$wpdb->posts} |
| 664 | WHERE post_status = 'publish' |
| 665 | AND post_author = %d |
| 666 | AND post_type = %s |
| 667 | "; |
| 668 | |
| 669 | return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $post_type ) ); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * @since 1.2.0 |
| 674 | * |
| 675 | * @param string $url |
| 676 | * |
| 677 | * @return bool |
| 678 | */ |
| 679 | protected function is_image_url( $url ) { |
| 680 | |
| 681 | if ( ! is_string( $url ) ) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | $validExt = array( '.jpg', '.jpeg', '.gif', '.png', '.bmp' ); |
| 686 | $ext = strrchr( $url, '.' ); |
| 687 | |
| 688 | return in_array( $ext, $validExt ); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * @since 1.0 |
| 693 | * @return array Image Sizes. |
| 694 | */ |
| 695 | public function get_all_image_sizes() { |
| 696 | $image_sizes = array( |
| 697 | 'thumbnail' => __( "Thumbnail", 'codepress-admin-columns' ), |
| 698 | 'medium' => __( "Medium", 'codepress-admin-columns' ), |
| 699 | 'large' => __( "Large", 'codepress-admin-columns' ), |
| 700 | 'full' => __( "Full", 'codepress-admin-columns' ) |
| 701 | ); |
| 702 | |
| 703 | foreach ( get_intermediate_image_sizes() as $size ) { |
| 704 | if ( ! isset( $image_sizes[ $size ] ) ) { |
| 705 | $image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) ); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | return $image_sizes; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * @since 2.2.6 |
| 714 | */ |
| 715 | public function get_terms_for_display( $term_ids, $taxonomy ) { |
| 716 | if ( empty( $term_ids ) ) { |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | $values = array(); |
| 721 | $term_ids = (array) $term_ids; |
| 722 | if ( $term_ids && ! is_wp_error( $term_ids ) ) { |
| 723 | $post_type = $this->get_post_type(); |
| 724 | foreach ( $term_ids as $term_id ) { |
| 725 | $term = get_term( $term_id, $taxonomy ); |
| 726 | $title = esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, 'edit' ) ); |
| 727 | |
| 728 | $filter_key = $term->taxonomy; |
| 729 | if ( 'category' === $term->taxonomy ) { |
| 730 | $filter_key = 'category_name'; |
| 731 | } |
| 732 | |
| 733 | $link = "<a href='edit.php?post_type={$post_type}&{$filter_key}={$term->slug}'>{$title}</a>"; |
| 734 | if ( $post_type == 'attachment' ) { |
| 735 | $link = "<a href='upload.php?taxonomy={$filter_key}&term={$term->slug}'>{$title}</a>"; |
| 736 | } |
| 737 | |
| 738 | $values[] = $link; |
| 739 | } |
| 740 | } |
| 741 | if ( ! $values ) { |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | return implode( ', ', $values ); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * @since 2.0 |
| 750 | * |
| 751 | * @param string $name |
| 752 | * |
| 753 | * @return array Image Sizes |
| 754 | */ |
| 755 | public function get_image_size_by_name( $name = '' ) { |
| 756 | |
| 757 | if ( ! $name || is_array( $name ) ) { |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | global $_wp_additional_image_sizes; |
| 762 | |
| 763 | if ( ! isset( $_wp_additional_image_sizes[ $name ] ) ) { |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | return $_wp_additional_image_sizes[ $name ]; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @see image_resize() |
| 772 | * @since 2.0 |
| 773 | * @return string Image URL |
| 774 | */ |
| 775 | public function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { |
| 776 | $resized = false; |
| 777 | $editor = wp_get_image_editor( $file ); |
| 778 | |
| 779 | if ( is_wp_error( $editor ) ) { |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | $editor->set_quality( $jpeg_quality ); |
| 784 | |
| 785 | $resized = $editor->resize( $max_w, $max_h, $crop ); |
| 786 | if ( is_wp_error( $resized ) ) { |
| 787 | return false; |
| 788 | } |
| 789 | |
| 790 | $dest_file = $editor->generate_filename( $suffix, $dest_path ); |
| 791 | |
| 792 | $saved = $editor->save( $dest_file ); |
| 793 | |
| 794 | if ( is_wp_error( $saved ) ) { |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | $resized = $dest_file; |
| 799 | |
| 800 | return $resized; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @since: 2.2.6 |
| 805 | * |
| 806 | */ |
| 807 | public function get_color_for_display( $color_hex ) { |
| 808 | if ( ! $color_hex ) { |
| 809 | return false; |
| 810 | } |
| 811 | $text_color = $this->get_text_color( $color_hex ); |
| 812 | |
| 813 | return "<div class='cpac-color'><span style='background-color:{$color_hex};color:{$text_color}'>{$color_hex}</span></div>"; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Determines text color absed on bakground coloring. |
| 818 | * |
| 819 | * @since 1.0 |
| 820 | */ |
| 821 | public function get_text_color( $bg_color ) { |
| 822 | |
| 823 | $rgb = $this->hex2rgb( $bg_color ); |
| 824 | |
| 825 | return $rgb && ( ( $rgb[0] * 0.299 + $rgb[1] * 0.587 + $rgb[2] * 0.114 ) < 186 ) ? '#ffffff' : '#333333'; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Convert hex to rgb |
| 830 | * |
| 831 | * @since 1.0 |
| 832 | */ |
| 833 | public function hex2rgb( $hex ) { |
| 834 | $hex = str_replace( "#", "", $hex ); |
| 835 | |
| 836 | if ( strlen( $hex ) == 3 ) { |
| 837 | $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); |
| 838 | $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); |
| 839 | $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); |
| 840 | } else { |
| 841 | $r = hexdec( substr( $hex, 0, 2 ) ); |
| 842 | $g = hexdec( substr( $hex, 2, 2 ) ); |
| 843 | $b = hexdec( substr( $hex, 4, 2 ) ); |
| 844 | } |
| 845 | $rgb = array( $r, $g, $b ); |
| 846 | |
| 847 | return $rgb; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Count the number of words in a string (multibyte-compatible) |
| 852 | * |
| 853 | * @since 2.3 |
| 854 | * |
| 855 | * @param string $input Input string |
| 856 | * |
| 857 | * @return int Number of words |
| 858 | */ |
| 859 | public function str_count_words( $input ) { |
| 860 | |
| 861 | $patterns = array( |
| 862 | 'strip' => '/<[a-zA-Z\/][^<>]*>/', |
| 863 | 'clean' => '/[0-9.(),;:!?%#$¿\'"_+=\\/-]+/', |
| 864 | 'w' => '/\S\s+/', |
| 865 | 'c' => '/\S/' |
| 866 | ); |
| 867 | |
| 868 | $type = 'w'; |
| 869 | |
| 870 | $input = preg_replace( $patterns['strip'], ' ', $input ); |
| 871 | $input = preg_replace( '/ | /i', ' ', $input ); |
| 872 | $input = preg_replace( $patterns['clean'], '', $input ); |
| 873 | |
| 874 | if ( ! strlen( preg_replace( '/\s/', '', $input ) ) ) { |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | return preg_match_all( $patterns[ $type ], $input, $matches ) + 1; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * @since 1.0 |
| 883 | * |
| 884 | * @param mixed $meta Image files or Image ID's |
| 885 | * @param array $args |
| 886 | * |
| 887 | * @return array HTML img elements |
| 888 | */ |
| 889 | public function get_thumbnails( $images, $args = array() ) { |
| 890 | |
| 891 | if ( empty( $images ) || 'false' == $images ) { |
| 892 | return array(); |
| 893 | } |
| 894 | |
| 895 | // turn string to array |
| 896 | if ( is_string( $images ) || is_numeric( $images ) ) { |
| 897 | if ( strpos( $images, ',' ) !== false ) { |
| 898 | $images = array_filter( explode( ',', $this->strip_trim( str_replace( ' ', '', $images ) ) ) ); |
| 899 | } else { |
| 900 | $images = array( $images ); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Image size |
| 905 | $defaults = array( |
| 906 | 'image_size' => 'cpac-custom', |
| 907 | 'image_size_w' => 80, |
| 908 | 'image_size_h' => 80, |
| 909 | ); |
| 910 | $args = wp_parse_args( $args, $defaults ); |
| 911 | |
| 912 | extract( $args ); |
| 913 | |
| 914 | $thumbnails = array(); |
| 915 | foreach ( $images as $value ) { |
| 916 | |
| 917 | if ( $this->is_image_url( $value ) ) { |
| 918 | |
| 919 | // get dimensions from image_size |
| 920 | if ( $sizes = $this->get_image_size_by_name( $image_size ) ) { |
| 921 | $image_size_w = $sizes['width']; |
| 922 | $image_size_h = $sizes['height']; |
| 923 | } |
| 924 | |
| 925 | $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $value ); |
| 926 | |
| 927 | if ( is_file( $image_path ) ) { |
| 928 | |
| 929 | // try to resize image |
| 930 | if ( $resized = $this->image_resize( $image_path, $image_size_w, $image_size_h, true ) ) { |
| 931 | $thumbnails[] = "<img src='" . str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized ) . "' alt='' width='{$image_size_w}' height='{$image_size_h}' />"; |
| 932 | } // return full image with maxed dimensions |
| 933 | else { |
| 934 | $thumbnails[] = "<img src='{$value}' alt='' style='max-width:{$image_size_w}px;max-height:{$image_size_h}px' />"; |
| 935 | } |
| 936 | } |
| 937 | } // Media Attachment |
| 938 | elseif ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) { |
| 939 | |
| 940 | $src = ''; |
| 941 | $width = ''; |
| 942 | $height = ''; |
| 943 | |
| 944 | if ( ! $image_size || 'cpac-custom' == $image_size ) { |
| 945 | $width = $image_size_w; |
| 946 | $height = $image_size_h; |
| 947 | |
| 948 | // to make sure wp_get_attachment_image_src() get the image with matching dimensions. |
| 949 | $image_size = array( $width, $height ); |
| 950 | } |
| 951 | |
| 952 | // Is Image |
| 953 | if ( $attributes = wp_get_attachment_image_src( $value, $image_size ) ) { |
| 954 | |
| 955 | $src = $attributes[0]; |
| 956 | $width = $attributes[1]; |
| 957 | $height = $attributes[2]; |
| 958 | |
| 959 | // image size by name |
| 960 | if ( $sizes = $this->get_image_size_by_name( $image_size ) ) { |
| 961 | $width = $sizes['width']; |
| 962 | $height = $sizes['height']; |
| 963 | } |
| 964 | } // Is File, use icon |
| 965 | elseif ( $attributes = wp_get_attachment_image_src( $value, $image_size, true ) ) { |
| 966 | $src = $attributes[0]; |
| 967 | |
| 968 | if ( $sizes = $this->get_image_size_by_name( $image_size ) ) { |
| 969 | $width = $sizes['width']; |
| 970 | $height = $sizes['height']; |
| 971 | } |
| 972 | } |
| 973 | if ( is_array( $image_size ) ) { |
| 974 | $width = $image_size_w; |
| 975 | $height = $image_size_h; |
| 976 | |
| 977 | $thumbnails[] = "<span class='cpac-column-value-image' style='width:{$width}px;height:{$height}px; background-size: cover; background-image: url({$src}); background-position: center;'></span>"; |
| 978 | |
| 979 | } else { |
| 980 | $max = max( array( $width, $height ) ); |
| 981 | $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='{$src}' alt=''/></span>"; |
| 982 | } |
| 983 | |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return $thumbnails; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Implode for multi dimensional array |
| 992 | * |
| 993 | * @since 1.0 |
| 994 | * |
| 995 | * @param string $glue |
| 996 | * @param array $pieces |
| 997 | * |
| 998 | * @return string Imploded array |
| 999 | */ |
| 1000 | public function recursive_implode( $glue, $pieces ) { |
| 1001 | foreach ( $pieces as $r_pieces ) { |
| 1002 | if ( is_array( $r_pieces ) ) { |
| 1003 | $retVal[] = $this->recursive_implode( $glue, $r_pieces ); |
| 1004 | } else { |
| 1005 | $retVal[] = $r_pieces; |
| 1006 | } |
| 1007 | } |
| 1008 | if ( isset( $retVal ) && is_array( $retVal ) ) { |
| 1009 | return implode( $glue, $retVal ); |
| 1010 | } |
| 1011 | |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Get timestamp |
| 1017 | * |
| 1018 | * @since 2.0 |
| 1019 | * |
| 1020 | * @param string $date |
| 1021 | * |
| 1022 | * @return string Formatted date |
| 1023 | */ |
| 1024 | public function get_timestamp( $date ) { |
| 1025 | |
| 1026 | if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) ) { |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch. |
| 1031 | // See http://api.jqueryui.com/datepicker/#utility-formatDate |
| 1032 | // credits: nmarks |
| 1033 | if ( is_numeric( $date ) ) { |
| 1034 | $length = strlen( trim( $date ) ); |
| 1035 | |
| 1036 | // Dates before / around September 8th, 2001 are saved as 9 numbers * 1000 resulting in 12 numbers to store the time. |
| 1037 | // Dates after September 8th are saved as 10 numbers * 1000, resulting in 13 numbers. |
| 1038 | // For example the ACF Date and Time Picker uses this format. |
| 1039 | // credits: Ben C |
| 1040 | if ( 12 === $length || 13 === $length ) { |
| 1041 | $date = round( $date / 1000 ); // remove the ms |
| 1042 | } |
| 1043 | |
| 1044 | // Date format: yyyymmdd ( often used by ACF ) must start with 19xx or 20xx and is 8 long |
| 1045 | // @todo: in theory a numeric string of 8 can also be a unixtimestamp; no conversion would be needed |
| 1046 | if ( 8 === $length && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) { |
| 1047 | $date = strtotime( $date ); |
| 1048 | } |
| 1049 | } // Parse with strtotime if it's not numeric |
| 1050 | else { |
| 1051 | $date = strtotime( $date ); |
| 1052 | } |
| 1053 | |
| 1054 | return $date; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * @since 1.3.1 |
| 1059 | * |
| 1060 | * @param string $date |
| 1061 | * |
| 1062 | * @return string Formatted date |
| 1063 | */ |
| 1064 | public function get_date( $date, $format = '' ) { |
| 1065 | |
| 1066 | if ( ! $date = $this->get_timestamp( $date ) ) { |
| 1067 | return false; |
| 1068 | } |
| 1069 | if ( ! $format ) { |
| 1070 | $format = get_option( 'date_format' ); |
| 1071 | } |
| 1072 | |
| 1073 | return date_i18n( $format, $date ); |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * @since 1.3.1 |
| 1078 | * |
| 1079 | * @param string $date |
| 1080 | * |
| 1081 | * @return string Formatted time |
| 1082 | */ |
| 1083 | protected function get_time( $date, $format = '' ) { |
| 1084 | |
| 1085 | if ( ! $date = $this->get_timestamp( $date ) ) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | if ( ! $format ) { |
| 1089 | $format = get_option( 'time_format' ); |
| 1090 | } |
| 1091 | |
| 1092 | return date_i18n( $format, $date ); |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Get display name. |
| 1097 | * |
| 1098 | * Can also be used by addons. |
| 1099 | * |
| 1100 | * @since 2.0 |
| 1101 | */ |
| 1102 | public function get_display_name( $user_id ) { |
| 1103 | |
| 1104 | if ( ! $userdata = get_userdata( $user_id ) ) { |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | $name = ''; |
| 1109 | |
| 1110 | if ( ! empty( $this->options->display_author_as ) ) { |
| 1111 | |
| 1112 | $display_as = $this->options->display_author_as; |
| 1113 | |
| 1114 | if ( 'first_last_name' == $display_as ) { |
| 1115 | $first = ! empty( $userdata->first_name ) ? $userdata->first_name : ''; |
| 1116 | $last = ! empty( $userdata->last_name ) ? " {$userdata->last_name}" : ''; |
| 1117 | $name = $first . $last; |
| 1118 | } elseif ( ! empty( $userdata->{$display_as} ) ) { |
| 1119 | $name = $userdata->{$display_as}; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | // default to display_name |
| 1124 | if ( ! $name ) { |
| 1125 | $name = $userdata->display_name; |
| 1126 | } |
| 1127 | |
| 1128 | return $name; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * @since 2.0 |
| 1133 | * |
| 1134 | * @param string $field_key |
| 1135 | * |
| 1136 | * @return string Attribute Name |
| 1137 | */ |
| 1138 | public function label_view( $label, $description = '', $pointer = '' ) { |
| 1139 | ?> |
| 1140 | <td class="label"> |
| 1141 | <label for="<?php $this->attr_id( $pointer ); ?>"> |
| 1142 | <?php echo stripslashes( $label ); ?> |
| 1143 | <?php if ( $description ) : ?><p class="description"><?php echo $description; ?></p><?php endif; ?> |
| 1144 | </label> |
| 1145 | </td> |
| 1146 | <?php |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * @since 2.0 |
| 1151 | */ |
| 1152 | public function display_field_date_format() { |
| 1153 | |
| 1154 | $field_key = 'date_format'; |
| 1155 | $label = __( 'Date Format', 'codepress-admin-columns' ); |
| 1156 | $description = __( 'This will determine how the date will be displayed.', 'codepress-admin-columns' ); |
| 1157 | |
| 1158 | ?> |
| 1159 | <tr class="column_<?php echo $field_key; ?>"> |
| 1160 | <?php $this->label_view( $label, $description, $field_key ); ?> |
| 1161 | <td class="input"> |
| 1162 | <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->get_option( 'date_format' ); ?>" placeholder="<?php _e( 'Example:', 'codepress-admin-columns' ); ?> d M Y H:i"/> |
| 1163 | |
| 1164 | <p class="description"> |
| 1165 | <?php printf( __( "Leave empty for WordPress date format, change your <a href='%s'>default date format here</a>.", 'codepress-admin-columns' ), admin_url( 'options-general.php' ) . '#date_format_custom_radio' ); ?> |
| 1166 | <a target='_blank' href='http://codex.wordpress.org/Formatting_Date_and_Time'><?php _e( 'Documentation on date and time formatting.', 'codepress-admin-columns' ); ?></a> |
| 1167 | </p> |
| 1168 | </td> |
| 1169 | </tr> |
| 1170 | |
| 1171 | <?php |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * @since 2.0 |
| 1176 | */ |
| 1177 | public function display_field_excerpt_length() { |
| 1178 | |
| 1179 | $field_key = 'excerpt_length'; |
| 1180 | $label = __( 'Excerpt length', 'codepress-admin-columns' ); |
| 1181 | $description = __( 'Number of words', 'codepress-admin-columns' ); |
| 1182 | |
| 1183 | ?> |
| 1184 | <tr class="column_<?php echo $field_key; ?>"> |
| 1185 | <?php $this->label_view( $label, $description, $field_key ); ?> |
| 1186 | <td class="input"> |
| 1187 | <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->excerpt_length; ?>"/> |
| 1188 | </td> |
| 1189 | </tr> |
| 1190 | <?php |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * @since 2.4.9 |
| 1195 | */ |
| 1196 | public function display_field_link_label() { |
| 1197 | $field_key = 'link_label'; |
| 1198 | $label = __( 'Link label', 'codepress-admin-columns' ); |
| 1199 | $description = __( 'Leave blank to display the url', 'codepress-admin-columns' ); |
| 1200 | |
| 1201 | ?> |
| 1202 | <tr class="column_<?php echo $field_key; ?>"> |
| 1203 | <?php $this->label_view( $label, $description, $field_key ); ?> |
| 1204 | <td class="input"> |
| 1205 | <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->link_label; ?>"/> |
| 1206 | </td> |
| 1207 | </tr> |
| 1208 | <?php |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * @since 2.0 |
| 1213 | */ |
| 1214 | public function display_field_preview_size() { |
| 1215 | |
| 1216 | $field_key = 'image_size'; |
| 1217 | $label = __( 'Preview size', 'codepress-admin-columns' ); |
| 1218 | |
| 1219 | ?> |
| 1220 | <tr class="column_<?php echo $field_key; ?>"> |
| 1221 | |
| 1222 | <?php $this->label_view( $label, '', $field_key ); ?> |
| 1223 | |
| 1224 | <td class="input"> |
| 1225 | <?php foreach ( $sizes = $this->get_all_image_sizes() as $id => $image_label ) : ?> |
| 1226 | <label for="<?php $this->attr_id( $field_key ); ?>-<?php echo $id ?>" class="custom-size"> |
| 1227 | <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 ); ?>> |
| 1228 | <?php echo $image_label; ?> |
| 1229 | </label> |
| 1230 | <?php endforeach; ?> |
| 1231 | |
| 1232 | <div class="custom_image_size"> |
| 1233 | <label for="<?php $this->attr_id( $field_key ); ?>-custom" class="custom-size image-size-custom"> |
| 1234 | <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', 'codepress-admin-columns' ); ?> |
| 1235 | </label> |
| 1236 | <label for="<?php $this->attr_id( $field_key ); ?>-w" |
| 1237 | class="custom-size-w<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>"> |
| 1238 | <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', 'codepress-admin-columns' ); ?> |
| 1239 | </label> |
| 1240 | <label for="<?php $this->attr_id( $field_key ); ?>-h" |
| 1241 | class="custom-size-h<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>"> |
| 1242 | <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', 'codepress-admin-columns' ); ?> |
| 1243 | </label> |
| 1244 | </div> |
| 1245 | </td> |
| 1246 | </tr> |
| 1247 | <?php |
| 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * @since 2.1.1 |
| 1252 | */ |
| 1253 | public function display_field_before_after() { |
| 1254 | $this->display_field_text( 'before', __( "Before", 'codepress-admin-columns' ), __( 'This text will appear before the custom field value.', 'codepress-admin-columns' ) ); |
| 1255 | $this->display_field_text( 'after', __( "After", 'codepress-admin-columns' ), __( 'This text will appear after the custom field value.', 'codepress-admin-columns' ) ); |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * @since 2.3.2 |
| 1260 | */ |
| 1261 | public function display_field_user_format() { |
| 1262 | |
| 1263 | $nametypes = array( |
| 1264 | 'display_name' => __( 'Display Name', 'codepress-admin-columns' ), |
| 1265 | 'first_name' => __( 'First Name', 'codepress-admin-columns' ), |
| 1266 | 'last_name' => __( 'Last Name', 'codepress-admin-columns' ), |
| 1267 | 'nickname' => __( 'Nickname', 'codepress-admin-columns' ), |
| 1268 | 'user_login' => __( 'User Login', 'codepress-admin-columns' ), |
| 1269 | 'user_email' => __( 'User Email', 'codepress-admin-columns' ), |
| 1270 | 'ID' => __( 'User ID', 'codepress-admin-columns' ), |
| 1271 | 'first_last_name' => __( 'First and Last Name', 'codepress-admin-columns' ), |
| 1272 | ); |
| 1273 | |
| 1274 | $this->display_field_select( 'display_author_as', __( 'Display format', 'codepress-admin-columns' ), $nametypes, __( 'This is the format of the author name.', 'codepress-admin-columns' ) ); |
| 1275 | } |
| 1276 | |
| 1277 | /** |
| 1278 | * @since 2.3.4 |
| 1279 | * |
| 1280 | * @param string $name Name of the column option |
| 1281 | * @param string $label Label |
| 1282 | * @param array $options Select options |
| 1283 | * @param strong $description (optional) Description below the label |
| 1284 | * @param string $optional_toggle_id (optional) Toggle ID will hide the row untill the toggle is triggered |
| 1285 | * @param boolean $refresh This will JS refresh the column on change. |
| 1286 | */ |
| 1287 | public function display_field_select( $name, $label, $options = array(), $description = '', $optional_toggle_id = '', $js_refresh = false ) { |
| 1288 | $current = $this->get_option( $name ); |
| 1289 | $data_optional = $optional_toggle_id ? ' data-additional-option-id="' . $this->get_attr_id( $optional_toggle_id ) . '"' : ''; |
| 1290 | $data_refresh = $js_refresh ? ' data-refresh="1"' : ''; |
| 1291 | ?> |
| 1292 | <tr class="column-<?php echo $name; ?>"<?php echo $data_optional; ?><?php echo $data_refresh; ?>> |
| 1293 | <?php $this->label_view( $label, $description, $name ); ?> |
| 1294 | <td class="input"> |
| 1295 | <select name="<?php $this->attr_name( $name ); ?>" id="<?php $this->attr_id( $name ); ?>"> |
| 1296 | <?php foreach ( $options as $key => $label ) : ?> |
| 1297 | <option value="<?php echo $key; ?>"<?php selected( $key, $current ); ?>><?php echo $label; ?></option> |
| 1298 | <?php endforeach; ?> |
| 1299 | </select> |
| 1300 | </td> |
| 1301 | </tr> |
| 1302 | <?php |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * @since 2.3.4 |
| 1307 | * |
| 1308 | * @param string $name Name of the column option |
| 1309 | * @param string $label Label |
| 1310 | * @param array $options Select options |
| 1311 | * @param strong $description (optional) Description below the label |
| 1312 | */ |
| 1313 | public function display_field_text( $name, $label, $description = '' ) { |
| 1314 | ?> |
| 1315 | <tr class="column-<?php echo $name; ?>"> |
| 1316 | <?php $this->label_view( $label, $description, $name ); ?> |
| 1317 | <td class="input"> |
| 1318 | <input type="text" name="<?php $this->attr_name( $name ); ?>" id="<?php $this->attr_id( $name ); ?>" value="<?php echo esc_attr( stripslashes( $this->get_option( $name ) ) ); ?>"/> |
| 1319 | </td> |
| 1320 | </tr> |
| 1321 | <?php |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * @since 2.4.8 |
| 1326 | * |
| 1327 | * @param string $name Name of the column option |
| 1328 | * @param string $value |
| 1329 | */ |
| 1330 | public function display_field_hidden( $name, $value = '' ) { |
| 1331 | ?> |
| 1332 | <tr class="column-<?php echo $name; ?> hidden"> |
| 1333 | <td class="input"> |
| 1334 | <input type="hidden" name="<?php $this->attr_name( $name ); ?>" value="<?php echo esc_attr( $value ); ?>"/> |
| 1335 | </td> |
| 1336 | </tr> |
| 1337 | <?php |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * @since 2.4.7 |
| 1342 | * |
| 1343 | * @param string $name Name of the column option |
| 1344 | * @param string $label Label |
| 1345 | * @param array $options Select options |
| 1346 | * @param strong $description (optional) Description below the label |
| 1347 | * @param string $optional_toggle_id (optional) Toggle ID will hide the row untill the toggle is triggered |
| 1348 | */ |
| 1349 | public function display_field_radio( $name, $label, $options = array(), $description = '', $optional_toggle_id = '' ) { |
| 1350 | $current = $this->get_option( $name ); |
| 1351 | $data_optional = $optional_toggle_id ? ' data-additional-option-id="' . $this->get_attr_id( $optional_toggle_id ) . '"' : ''; |
| 1352 | ?> |
| 1353 | <tr class="column-<?php echo $name; ?>" <?php echo $data_optional; ?>> |
| 1354 | <?php $this->label_view( $label, $description, $name ); ?> |
| 1355 | <td class="input"> |
| 1356 | <?php foreach ( $options as $key => $label ) : ?> |
| 1357 | <label> |
| 1358 | <input type="radio" name="<?php $this->attr_name( $name ); ?>" id="<?php $this->attr_id( $name . '-' . $key ); ?>" value="<?php echo $key; ?>"<?php checked( $key, $current ); ?>> |
| 1359 | <?php echo $label; ?> |
| 1360 | </label> |
| 1361 | <?php endforeach; ?> |
| 1362 | </select> |
| 1363 | </td> |
| 1364 | </tr> |
| 1365 | <?php |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * @since 2.0 |
| 1370 | * |
| 1371 | * @param array Column Objects |
| 1372 | * |
| 1373 | * @return string HTML List |
| 1374 | */ |
| 1375 | public function get_column_list( $columns = array(), $label = '' ) { |
| 1376 | |
| 1377 | if ( empty( $columns ) ) { |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | // sort by alphabet |
| 1382 | $_columns = array(); |
| 1383 | |
| 1384 | foreach ( $columns as $column ) { |
| 1385 | if ( $column->properties->hidden ) { |
| 1386 | continue; |
| 1387 | } |
| 1388 | |
| 1389 | $_columns[ $column->properties->type ] = ( 0 === strlen( strip_tags( $column->properties->label ) ) ) ? ucfirst( $column->properties->type ) : $column->properties->label; |
| 1390 | } |
| 1391 | |
| 1392 | asort( $_columns ); |
| 1393 | |
| 1394 | $list = "<optgroup label='{$label}'>"; |
| 1395 | foreach ( $_columns as $type => $label ) { |
| 1396 | $selected = selected( $this->properties->type, $type, false ); |
| 1397 | $list .= "<option value='{$type}'{$selected}>{$label}</option>"; |
| 1398 | } |
| 1399 | $list .= "</optgroup>"; |
| 1400 | |
| 1401 | return $list; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * @since 2.0 |
| 1406 | */ |
| 1407 | public function display() { |
| 1408 | |
| 1409 | $classes = implode( ' ', array_filter( array( "cpac-box-{$this->properties->type}", $this->properties->classes ) ) ); |
| 1410 | |
| 1411 | // column list |
| 1412 | $column_list = ''; |
| 1413 | |
| 1414 | $groups = $this->storage_model->get_column_type_groups(); |
| 1415 | foreach ( $groups as $group => $label ) { |
| 1416 | $column_list .= $this->get_column_list( $this->storage_model->column_types[ $group ], $label ); |
| 1417 | } |
| 1418 | |
| 1419 | // clone attribute |
| 1420 | $data_clone = $this->properties->is_cloneable ? " data-clone='{$this->properties->clone}'" : ''; |
| 1421 | |
| 1422 | ?> |
| 1423 | <div class="cpac-column <?php echo $classes; ?>" data-type="<?php echo $this->properties->type; ?>"<?php echo $data_clone; ?>> |
| 1424 | <input type="hidden" class="column-name" name="<?php echo $this->attr_name( 'column-name' ); ?>" value="<?php echo esc_attr( $this->properties->name ); ?>"/> |
| 1425 | <input type="hidden" class="type" name="<?php echo $this->attr_name( 'type' ); ?>" value="<?php echo $this->properties->type; ?>"/> |
| 1426 | <input type="hidden" class="clone" name="<?php echo $this->attr_name( 'clone' ); ?>" value="<?php echo $this->properties->clone; ?>"/> |
| 1427 | |
| 1428 | <div class="column-meta"> |
| 1429 | <table class="widefat"> |
| 1430 | <tbody> |
| 1431 | <tr> |
| 1432 | <td class="column_sort"></td> |
| 1433 | <td class="column_label"> |
| 1434 | <div class="inner"> |
| 1435 | <div class="meta"> |
| 1436 | |
| 1437 | <span title="<?php echo esc_attr( __( 'width', 'codepress-admin-columns' ) ); ?>" class="width" data-indicator-id=""> |
| 1438 | <?php echo ! empty( $this->options->width ) ? $this->options->width . $this->options->width_unit : ''; ?> |
| 1439 | </span> |
| 1440 | |
| 1441 | <?php |
| 1442 | /** |
| 1443 | * Fires in the meta-element for column options, which is displayed right after the column label |
| 1444 | * |
| 1445 | * @since 2.0 |
| 1446 | * |
| 1447 | * @param CPAC_Column $column_instance Column class instance |
| 1448 | */ |
| 1449 | do_action( 'cac/column/settings_meta', $this ); |
| 1450 | |
| 1451 | /** |
| 1452 | * @deprecated 2.2 Use cac/column/settings_meta instead |
| 1453 | */ |
| 1454 | do_action( 'cac/column/label', $this ); |
| 1455 | ?> |
| 1456 | |
| 1457 | </div> |
| 1458 | <a class="toggle" href="javascript:;"><?php echo stripslashes( $this->get_label() ); ?></a> |
| 1459 | <a class="edit-button" href="javascript:;"><?php _e( 'Edit', 'codepress-admin-columns' ); ?></a> |
| 1460 | <?php if ( $this->properties->is_cloneable ) : ?> |
| 1461 | <a class="clone-button" href="#"><?php _e( 'Clone', 'codepress-admin-columns' ); ?></a> |
| 1462 | <?php endif; ?> |
| 1463 | <a class="remove-button" href="javascript:;"><?php _e( 'Remove', 'codepress-admin-columns' ); ?></a> |
| 1464 | </div> |
| 1465 | </td> |
| 1466 | <td class="column_type"> |
| 1467 | <div class="inner"> |
| 1468 | <a href="#"><?php echo stripslashes( $this->properties->label ); ?></a> |
| 1469 | </div> |
| 1470 | </td> |
| 1471 | <td class="column_edit"></td> |
| 1472 | </tr> |
| 1473 | </tbody> |
| 1474 | </table> |
| 1475 | </div><!--.column-meta--> |
| 1476 | |
| 1477 | <div class="column-form"> |
| 1478 | <table class="widefat"> |
| 1479 | <tbody> |
| 1480 | <tr class="column_type"> |
| 1481 | <?php $this->label_view( __( 'Type', 'codepress-admin-columns' ), __( 'Choose a column type.', 'codepress-admin-columns' ) . '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->properties->type . '</em><em>' . __( 'Name', 'codepress-admin-columns' ) . ': ' . $this->properties->name . '</em>', 'type' ); ?> |
| 1482 | <td class="input"> |
| 1483 | <select name="<?php $this->attr_name( 'type' ); ?>" id="<?php $this->attr_id( 'type' ); ?>"> |
| 1484 | <?php echo $column_list; ?> |
| 1485 | </select> |
| 1486 | |
| 1487 | <div class="msg"></div> |
| 1488 | </td> |
| 1489 | </tr><!--.column_label--> |
| 1490 | |
| 1491 | <tr class="column_label<?php echo $this->properties->hide_label ? ' hidden' : ''; ?>"> |
| 1492 | <?php $this->label_view( __( 'Label', 'codepress-admin-columns' ), __( 'This is the name which will appear as the column header.', 'codepress-admin-columns' ), 'label' ); ?> |
| 1493 | <td class="input"> |
| 1494 | <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 ); ?>"/> |
| 1495 | </td> |
| 1496 | </tr><!--.column_label--> |
| 1497 | |
| 1498 | <tr class="column_width"> |
| 1499 | <?php $this->label_view( __( 'Width', 'codepress-admin-columns' ), '', 'width' ); ?> |
| 1500 | <td class="input"> |
| 1501 | <div class="description" title="<?php _e( 'default', 'codepress-admin-columns' ); ?>"> |
| 1502 | <input class="width" type="text" placeholder="<?php _e( 'auto', 'codepress-admin-columns' ); ?>" name="<?php $this->attr_name( 'width' ); ?>" id="<?php $this->attr_id( 'width' ); ?>" value="<?php echo $this->options->width; ?>"/> |
| 1503 | <span class="unit"><?php echo $this->options->width_unit; ?></span> |
| 1504 | </div> |
| 1505 | <div class="width-slider"></div> |
| 1506 | |
| 1507 | <div class="unit-select"> |
| 1508 | <label for="<?php $this->attr_id( 'width_unit_px' ); ?>"> |
| 1509 | <input type="radio" class="unit" name="<?php $this->attr_name( 'width_unit' ); ?>" id="<?php $this->attr_id( 'width_unit_px' ); ?>" value="px"<?php checked( $this->options->width_unit, 'px' ); ?>/>px |
| 1510 | </label> |
| 1511 | <label for="<?php $this->attr_id( 'width_unit_perc' ); ?>"> |
| 1512 | <input type="radio" class="unit" name="<?php $this->attr_name( 'width_unit' ); ?>" id="<?php $this->attr_id( 'width_unit_perc' ); ?>" value="%"<?php checked( $this->options->width_unit, '%' ); ?>/>% |
| 1513 | </label> |
| 1514 | </div> |
| 1515 | |
| 1516 | </td> |
| 1517 | </tr><!--.column_width--> |
| 1518 | |
| 1519 | <?php |
| 1520 | /** |
| 1521 | * Fires directly before the custom options for a column are displayed in the column form |
| 1522 | * |
| 1523 | * @since 2.0 |
| 1524 | * |
| 1525 | * @param CPAC_Column $column_instance Column class instance |
| 1526 | */ |
| 1527 | do_action( 'cac/column/settings_before', $this ); |
| 1528 | ?> |
| 1529 | |
| 1530 | <?php |
| 1531 | /** |
| 1532 | * Load specific column settings. |
| 1533 | * |
| 1534 | */ |
| 1535 | $this->display_settings(); |
| 1536 | |
| 1537 | ?> |
| 1538 | |
| 1539 | <?php |
| 1540 | /** |
| 1541 | * Load before and after fields for custom columns. |
| 1542 | * |
| 1543 | */ |
| 1544 | if ( $this->properties->use_before_after && ! $this->properties->default ) { |
| 1545 | $this->display_field_before_after(); |
| 1546 | } |
| 1547 | ?> |
| 1548 | |
| 1549 | <?php |
| 1550 | /** |
| 1551 | * Fires directly after the custom options for a column are displayed in the column form |
| 1552 | * |
| 1553 | * @since 2.0 |
| 1554 | * |
| 1555 | * @param CPAC_Column $column_instance Column class instance |
| 1556 | */ |
| 1557 | do_action( 'cac/column/settings_after', $this ); |
| 1558 | ?> |
| 1559 | |
| 1560 | <tr class="column_action"> |
| 1561 | <td colspan="2"> |
| 1562 | <p> |
| 1563 | <?php if ( $this->properties->is_cloneable ) : ?> |
| 1564 | <a class="clone-button" href="#"><?php _e( 'Clone', 'codepress-admin-columns' ); ?></a> |
| 1565 | <?php endif; ?> |
| 1566 | <a href="javascript:;" class="remove-button"><?php _e( 'Remove' ); ?></a> |
| 1567 | </p> |
| 1568 | </td> |
| 1569 | </tr> |
| 1570 | |
| 1571 | </tbody> |
| 1572 | </table> |
| 1573 | </div><!--.column-form--> |
| 1574 | </div><!--.cpac-column--> |
| 1575 | <?php |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * Display settings field for post property to display |
| 1580 | * |
| 1581 | * @since 2.4.7 |
| 1582 | */ |
| 1583 | public function display_field_post_property_display() { |
| 1584 | $this->display_field_select( |
| 1585 | 'post_property_display', |
| 1586 | __( 'Property To Display', 'codepress-admin-columns' ), |
| 1587 | array( |
| 1588 | 'title' => __( 'Title' ), // default |
| 1589 | 'id' => __( 'ID' ), |
| 1590 | 'author' => __( 'Author' ) |
| 1591 | ), |
| 1592 | __( 'Post property to display for related post(s).', 'codepress-admin-columns' ) |
| 1593 | ); |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * Display settings field for the page the posts should link to |
| 1598 | * |
| 1599 | * @since 2.4.7 |
| 1600 | */ |
| 1601 | public function display_field_post_link_to() { |
| 1602 | $this->display_field_select( |
| 1603 | 'post_link_to', |
| 1604 | __( 'Link To', 'codepress-admin-columns' ), |
| 1605 | array( |
| 1606 | '' => __( 'None' ), |
| 1607 | 'edit_post' => __( 'Edit Post' ), |
| 1608 | 'view_post' => __( 'View Post' ), |
| 1609 | 'edit_author' => __( 'Edit Post Author', 'codepress-admin-columns' ), |
| 1610 | 'view_author' => __( 'View Public Post Author Page', 'codepress-admin-columns' ) |
| 1611 | ), |
| 1612 | __( 'Page the posts should link to.', 'codepress-admin-columns' ) |
| 1613 | ); |
| 1614 | } |
| 1615 | |
| 1616 | /** |
| 1617 | * @since 2.4.7 |
| 1618 | */ |
| 1619 | function display_settings_placeholder( $url ) { ?> |
| 1620 | <div class="is-disabled"> |
| 1621 | <p> |
| 1622 | <strong><?php printf( __( "The %s column is only available in Admin Columns Pro - Business or Developer.", 'codepress-admin-columns' ), $this->get_label() ); ?></strong> |
| 1623 | </p> |
| 1624 | |
| 1625 | <p> |
| 1626 | <?php printf( __( "If you have a business or developer licence please download & install your %s add-on from the <a href='%s'>add-ons tab</a>.", 'codepress-admin-columns' ), $this->get_label(), admin_url( 'options-general.php?page=codepress-admin-columns&tab=addons' ) ); ?> |
| 1627 | </p> |
| 1628 | |
| 1629 | <p> |
| 1630 | <?php printf( __( "Admin Columns Pro offers full %s integration, allowing you to easily display and edit %s fields from within your overview.", 'codepress-admin-columns' ), $this->get_label(), $this->get_label() ); ?> |
| 1631 | </p> |
| 1632 | <a href="<?php echo add_query_arg( array( |
| 1633 | 'utm_source' => 'plugin-installation', |
| 1634 | 'utm_medium' => $this->get_type(), |
| 1635 | 'utm_campaign' => 'plugin-installation' |
| 1636 | ), $url ); ?>" class="button button-primary"><?php _e( 'Find out more', 'codepress-admin-columns' ); ?></a> |
| 1637 | </div> |
| 1638 | <?php |
| 1639 | } |
| 1640 | } |