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