column
11 years ago
storage_model
11 years ago
addons.php
11 years ago
column.php
11 years ago
settings.php
11 years ago
storage_model.php
11 years ago
third_party.php
11 years ago
upgrade.php
11 years ago
utility.php
11 years ago
storage_model.php
938 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Storage Model |
| 5 | * |
| 6 | * @since 2.0 |
| 7 | */ |
| 8 | abstract class CPAC_Storage_Model { |
| 9 | |
| 10 | /** |
| 11 | * @since 2.0 |
| 12 | */ |
| 13 | public $label; |
| 14 | |
| 15 | /** |
| 16 | * Identifier for Storage Model; Posttype etc. |
| 17 | * |
| 18 | * @since 2.0 |
| 19 | */ |
| 20 | public $key; |
| 21 | |
| 22 | /** |
| 23 | * Type of storage model; Post, Media, User or Comments |
| 24 | * |
| 25 | * @since 2.0 |
| 26 | */ |
| 27 | public $type; |
| 28 | |
| 29 | /** |
| 30 | * Meta type of storage model; post, user, comment. Mostly used for custom field data. |
| 31 | * |
| 32 | * @since 3.0 |
| 33 | */ |
| 34 | public $meta_type; |
| 35 | |
| 36 | /** |
| 37 | * Groups the storage model in the menu. |
| 38 | * |
| 39 | * @since 2.0 |
| 40 | */ |
| 41 | public $menu_type; |
| 42 | |
| 43 | /** |
| 44 | * @since 2.0 |
| 45 | * @var string |
| 46 | */ |
| 47 | public $page; |
| 48 | |
| 49 | /** |
| 50 | * Uses PHP export to display settings |
| 51 | * |
| 52 | * @since 2.0 |
| 53 | * @var string |
| 54 | */ |
| 55 | private $php_export = false; |
| 56 | |
| 57 | /** |
| 58 | * @since 2.0.1 |
| 59 | * @var array |
| 60 | */ |
| 61 | protected $columns_filepath; |
| 62 | |
| 63 | /** |
| 64 | * @since 2.0.1 |
| 65 | * @var array |
| 66 | */ |
| 67 | public $columns = array(); |
| 68 | |
| 69 | /** |
| 70 | * @since 2.1.0 |
| 71 | * @var array |
| 72 | */ |
| 73 | public $custom_columns = array(); |
| 74 | |
| 75 | /** |
| 76 | * @since 2.1.0 |
| 77 | * @var array |
| 78 | */ |
| 79 | public $default_columns = array(); |
| 80 | |
| 81 | /** |
| 82 | * @since 2.2 |
| 83 | * @var array |
| 84 | */ |
| 85 | public $stored_columns = NULL; |
| 86 | |
| 87 | /** |
| 88 | * @since 2.2 |
| 89 | * @var array |
| 90 | */ |
| 91 | public $column_types = array(); |
| 92 | |
| 93 | /** |
| 94 | * @since 2.0 |
| 95 | * @return array Column Name | Column Label |
| 96 | */ |
| 97 | abstract function get_default_columns(); |
| 98 | |
| 99 | /** |
| 100 | * @since 2.2 |
| 101 | */ |
| 102 | function __construct() { |
| 103 | |
| 104 | // set columns paths |
| 105 | $this->set_columns_filepath(); |
| 106 | |
| 107 | // Populate columns for this screen. |
| 108 | add_action( 'admin_init', array( $this, 'set_columns_on_current_screen' ) ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Checks if menu type is currently viewed |
| 113 | * |
| 114 | * @since 1.0 |
| 115 | * @param string $key |
| 116 | * @return bool |
| 117 | */ |
| 118 | public function is_menu_type_current( $first_posttpe ) { |
| 119 | |
| 120 | // display the page that was being viewed before saving |
| 121 | if ( ! empty( $_REQUEST['cpac_key'] ) ) { |
| 122 | if ( $_REQUEST['cpac_key'] == $this->key ) { |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // settings page has not yet been saved |
| 127 | } elseif ( $first_posttpe == $this->key ) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @since 2.0 |
| 136 | * @return array |
| 137 | */ |
| 138 | public function get_meta_keys( $add_hidden_meta = false ) { |
| 139 | global $wpdb; |
| 140 | |
| 141 | $keys = array(); |
| 142 | |
| 143 | $fields = $this->get_meta(); |
| 144 | |
| 145 | if ( is_wp_error( $fields ) || empty( $fields ) ) { |
| 146 | $keys = false; |
| 147 | } |
| 148 | |
| 149 | if ( $fields ) { |
| 150 | foreach ( $fields as $field ) { |
| 151 | |
| 152 | // give hidden fields a prefix for identifaction |
| 153 | if ( $add_hidden_meta && "_" == substr( $field[0], 0, 1 ) ) { |
| 154 | $keys[] = 'cpachidden' . $field[0]; |
| 155 | } |
| 156 | |
| 157 | // non hidden fields are saved as is |
| 158 | elseif ( "_" != substr( $field[0], 0, 1 ) ) { |
| 159 | $keys[] = $field[0]; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Filter the available custom field meta keys |
| 166 | * If showing hidden fields is enabled, they are prefixed with "cpachidden" in the list |
| 167 | * |
| 168 | * @since 2.0 |
| 169 | * |
| 170 | * @param array $keys Available custom field keys |
| 171 | * @param CPAC_Storage_Model $storage_model Storage model class instance |
| 172 | */ |
| 173 | $keys = apply_filters( 'cac/storage_model/meta_keys', $keys, $this ); |
| 174 | |
| 175 | /** |
| 176 | * Filter the available custom field meta keys for this storage model type |
| 177 | * |
| 178 | * @since 2.0 |
| 179 | * @see Filter cac/storage_model/meta_keys |
| 180 | */ |
| 181 | return apply_filters( "cac/storage_model/meta_keys/storage_key={$this->key}", $keys, $this ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @since 2.0 |
| 186 | * @param array $fields Custom fields. |
| 187 | * @return array Custom fields. |
| 188 | */ |
| 189 | protected function add_hidden_meta( $fields ) { |
| 190 | if ( ! $fields ) |
| 191 | return false; |
| 192 | |
| 193 | $combined_fields = array(); |
| 194 | |
| 195 | // filter out hidden meta fields |
| 196 | foreach ( $fields as $field ) { |
| 197 | |
| 198 | // give hidden fields a prefix for identifaction |
| 199 | if ( "_" == substr( $field[0], 0, 1 ) ) { |
| 200 | $combined_fields[] = 'cpachidden'.$field[0]; |
| 201 | } |
| 202 | |
| 203 | // non hidden fields are saved as is |
| 204 | elseif ( "_" != substr( $field[0], 0, 1 ) ) { |
| 205 | $combined_fields[] = $field[0]; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if ( empty( $combined_fields ) ) |
| 210 | return false; |
| 211 | |
| 212 | return $combined_fields; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @since 2.0 |
| 217 | */ |
| 218 | public function restore() { |
| 219 | |
| 220 | delete_option( "cpac_options_{$this->key}" ); |
| 221 | |
| 222 | cpac_admin_message( "<strong>{$this->label}</strong> " . __( 'settings succesfully restored.', 'cpac' ), 'updated' ); |
| 223 | |
| 224 | // refresh columns otherwise the removed columns will still display |
| 225 | $this->set_columns_on_current_screen(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @since 2.0 |
| 230 | */ |
| 231 | public function store( $columns = '' ) { |
| 232 | |
| 233 | if ( ! empty( $_POST[ $this->key ] ) ) { |
| 234 | $columns = array_filter( $_POST[ $this->key ] ); |
| 235 | } |
| 236 | |
| 237 | if ( ! $columns ) { |
| 238 | cpac_admin_message( __( 'No columns settings available.', 'cpac' ), 'error' ); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // sanitize user inputs |
| 243 | foreach ( $columns as $name => $options ) { |
| 244 | if ( $_column = $this->get_column_by_name( $name ) ) { |
| 245 | $columns[ $name ] = $_column->sanitize_storage( $options ); |
| 246 | } |
| 247 | |
| 248 | // Santize Label: Need to replace the url for images etc, so we do not have url problem on exports |
| 249 | // this can not be done by CPAC_Column::sanitize_storage() because 3rd party plugins are not available there |
| 250 | $columns[ $name ]['label'] = stripslashes( str_replace( site_url(), '[cpac_site_url]', trim( $columns[ $name ]['label'] ) ) ); |
| 251 | } |
| 252 | |
| 253 | // store columns |
| 254 | $result = update_option( "cpac_options_{$this->key}", $columns ); |
| 255 | $result_default = update_option( "cpac_options_{$this->key}_default", array_keys( $this->get_default_columns() ) ); |
| 256 | |
| 257 | // error |
| 258 | if( ! $result && ! $result_default ) { |
| 259 | cpac_admin_message( sprintf( __( 'You are trying to store the same settings for %s.', 'cpac' ), "<strong>{$this->label}</strong>" ), 'error' ); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | cpac_admin_message( sprintf( __( 'Settings for %s updated succesfully.', 'cpac' ), "<strong>{$this->label}</strong>" ), 'updated' ); |
| 264 | |
| 265 | // refresh columns otherwise the newly added columns will not be displayed |
| 266 | $this->set_columns_on_current_screen(); |
| 267 | |
| 268 | /** |
| 269 | * Fires after a new column setup is stored in the database |
| 270 | * Primarily used when columns are saved through the Admin Columns settings screen |
| 271 | * |
| 272 | * @since 2.2.9 |
| 273 | * |
| 274 | * @param array $columns List of columns ([columnid] => (array) [column properties]) |
| 275 | * @param CPAC_Storage_Model $storage_model_instance Storage model instance |
| 276 | */ |
| 277 | do_action( 'cac/storage_model/columns_stored', $columns, $this ); |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Goes through all files in 'classes/column' and includes each file. |
| 284 | * |
| 285 | * @since 2.0.1 |
| 286 | * @return array Column Classnames | Filepaths |
| 287 | */ |
| 288 | public function set_columns_filepath() { |
| 289 | |
| 290 | $columns = array( |
| 291 | 'CPAC_Column_Custom_Field' => CPAC_DIR . 'classes/column/custom-field.php', |
| 292 | 'CPAC_Column_Taxonomy' => CPAC_DIR . 'classes/column/taxonomy.php', |
| 293 | 'CPAC_Column_Used_By_Menu' => CPAC_DIR . 'classes/column/used-by-menu.php' |
| 294 | ); |
| 295 | |
| 296 | // Display ACF placeholder |
| 297 | if ( class_exists('acf') && ! class_exists( 'CAC_Addon_Pro' ) ) { |
| 298 | $columns[ 'CPAC_Column_ACF_Placeholder' ] = CPAC_DIR . 'classes/column/acf-placeholder.php'; |
| 299 | } |
| 300 | |
| 301 | // Directory to iterate |
| 302 | $columns_dir = CPAC_DIR . 'classes/column/' . $this->type; |
| 303 | if ( is_dir( $columns_dir ) ) { |
| 304 | $iterator = new DirectoryIterator( $columns_dir ); |
| 305 | foreach( $iterator as $leaf ) { |
| 306 | |
| 307 | if ( $leaf->isDot() || $leaf->isDir() ) { |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | // only allow php files, exclude .SVN .DS_STORE and such |
| 312 | if ( substr( $leaf->getFilename(), -4 ) !== '.php' ) { |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | // build classname from filename |
| 317 | $class_name = 'CPAC_Column_' . ucfirst( $this->type ) . '_' . implode( '_', array_map( 'ucfirst', explode( '-', basename( $leaf->getFilename(), '.php' ) ) ) ); |
| 318 | |
| 319 | // classname | filepath |
| 320 | $columns[ $class_name ] = $leaf->getPathname(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Filter the available custom column types |
| 326 | * Use this to register a custom column type |
| 327 | * |
| 328 | * @since 2.0 |
| 329 | * @param array $columns Available custom columns ([class_name] => [class file path]) |
| 330 | * @param CPAC_Storage_Model $storage_model Storage model class instance |
| 331 | */ |
| 332 | $columns = apply_filters( 'cac/columns/custom', $columns, $this ); |
| 333 | |
| 334 | /** |
| 335 | * Filter the available custom column types for a specific type |
| 336 | * |
| 337 | * @since 2.0 |
| 338 | * @see Filter cac/columns/custom |
| 339 | */ |
| 340 | $columns = apply_filters( 'cac/columns/custom/type=' . $this->type, $columns, $this ); |
| 341 | |
| 342 | /** |
| 343 | * Filter the available custom column types for a specific type |
| 344 | * |
| 345 | * @since 2.0 |
| 346 | * @see Filter cac/columns/custom |
| 347 | */ |
| 348 | $columns = apply_filters( 'cac/columns/custom/post_type=' . $this->key, $columns, $this ); |
| 349 | |
| 350 | $this->columns_filepath = $columns; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @since 2.0 |
| 355 | * @param $column_name |
| 356 | * @param $label |
| 357 | * @return object CPAC_Column |
| 358 | */ |
| 359 | public function create_column_instance( $column_name, $label ) { |
| 360 | |
| 361 | // create column instance |
| 362 | $column = new CPAC_Column( $this ); |
| 363 | |
| 364 | $column |
| 365 | ->set_properties( 'type', $column_name ) |
| 366 | ->set_properties( 'name', $column_name ) |
| 367 | ->set_properties( 'label', $label ) |
| 368 | ->set_properties( 'is_cloneable', false ) |
| 369 | ->set_properties( 'default', true ) |
| 370 | ->set_properties( 'group', 'default' ) |
| 371 | ->set_options( 'label', $label ) |
| 372 | ->set_options( 'state', 'on' ); |
| 373 | |
| 374 | // Hide Label when it contains HTML elements |
| 375 | if( strlen( $label ) != strlen( strip_tags( $label ) ) ) { |
| 376 | $column->set_properties( 'hide_label', true ); |
| 377 | } |
| 378 | |
| 379 | // Label empty? Use it's column_name |
| 380 | if ( ! $label ) { |
| 381 | $column->set_properties( 'label', ucfirst( $column_name ) ); |
| 382 | } |
| 383 | |
| 384 | return $column; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @since 2.0 |
| 389 | * @return array Column Type | Column Instance |
| 390 | */ |
| 391 | public function get_default_registered_columns() { |
| 392 | |
| 393 | $columns = array(); |
| 394 | |
| 395 | // Default columns |
| 396 | foreach ( $this->get_default_columns() as $column_name => $label ) { |
| 397 | |
| 398 | // checkboxes are mandatory |
| 399 | if ( 'cb' == $column_name ) { |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | $column = $this->create_column_instance( $column_name, $label ); |
| 404 | |
| 405 | $columns[ $column->properties->name ] = $column; |
| 406 | } |
| 407 | |
| 408 | do_action( "cac/columns/registered/default", $columns, $this ); |
| 409 | do_action( "cac/columns/registered/default/storage_key={$this->key}", $columns, $this ); |
| 410 | |
| 411 | return $columns; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @since 2.0 |
| 416 | * @return array Column Type | Column Instance |
| 417 | */ |
| 418 | public function get_custom_registered_columns() { |
| 419 | |
| 420 | $columns = array(); |
| 421 | |
| 422 | foreach ( $this->columns_filepath as $classname => $path ) { |
| 423 | include_once $path; |
| 424 | |
| 425 | if ( ! class_exists( $classname ) ) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | $column = new $classname( $this ); |
| 430 | |
| 431 | // exlude columns that are not registered based on conditional logic within the child column |
| 432 | if ( ! $column->properties->is_registered ) { |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | $columns[ $column->properties->type ] = $column; |
| 437 | } |
| 438 | |
| 439 | do_action( "cac/columns/registered/custom", $columns, $this ); |
| 440 | do_action( "cac/columns/registered/custom/storage_key={$this->key}", $columns, $this ); |
| 441 | |
| 442 | return $columns; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * @since 1.0 |
| 447 | * @param string $key |
| 448 | * @return array Column options |
| 449 | */ |
| 450 | public function get_default_stored_columns() { |
| 451 | |
| 452 | if ( ! $columns = get_option( "cpac_options_{$this->key}_default" ) ) { |
| 453 | return array(); |
| 454 | } |
| 455 | |
| 456 | return $columns; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @since 1.0 |
| 461 | * @return array Column options |
| 462 | */ |
| 463 | public function get_stored_columns() { |
| 464 | |
| 465 | $columns = $this->stored_columns; |
| 466 | |
| 467 | if ( $this->stored_columns === NULL ) { |
| 468 | $columns = $this->get_database_columns(); |
| 469 | } |
| 470 | |
| 471 | $columns = apply_filters( 'cpac/storage_model/stored_columns', $columns, $this ); |
| 472 | $columns = apply_filters( 'cpac/storage_model/stored_columns/storage_key=' . $this->key, $columns, $this ); |
| 473 | |
| 474 | if ( ! $columns ) { |
| 475 | return array(); |
| 476 | } |
| 477 | |
| 478 | return $columns; |
| 479 | } |
| 480 | |
| 481 | public function get_database_columns() { |
| 482 | return get_option( "cpac_options_{$this->key}" ); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Set stopred column by 3rd party plugins |
| 487 | * |
| 488 | * @since 2.3 |
| 489 | */ |
| 490 | public function set_stored_columns( $columns ) { |
| 491 | $this->stored_columns = $columns; |
| 492 | |
| 493 | // columns settings are set by external plugin |
| 494 | $this->php_export = true; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Are column set by third party plugin |
| 499 | * |
| 500 | * @since 2.3.4 |
| 501 | */ |
| 502 | public function is_using_php_export() { |
| 503 | return $this->php_export; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * @since 2.1.1 |
| 508 | */ |
| 509 | public function get_post_type() { |
| 510 | return isset( $this->post_type ) ? $this->post_type : false; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * @since 2.3.4 |
| 515 | */ |
| 516 | public function get_type() { |
| 517 | return $this->type; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * @since 2.3.4 |
| 522 | */ |
| 523 | public function get_meta_type() { |
| 524 | return $this->meta_type; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Only set columns on current screens |
| 529 | * |
| 530 | * @since 2.2.6 |
| 531 | */ |
| 532 | public function set_columns_on_current_screen() { |
| 533 | |
| 534 | if ( ! $this->is_doing_ajax() && ! $this->is_columns_screen() && ! $this->is_settings_page() ) { |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | $this->set_columns(); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * @since 2.0.2 |
| 543 | */ |
| 544 | public function set_columns() { |
| 545 | |
| 546 | do_action( 'cac/set_columns', $this ); |
| 547 | |
| 548 | $this->custom_columns = $this->get_custom_registered_columns(); |
| 549 | $this->default_columns = $this->get_default_registered_columns(); |
| 550 | $this->column_types = $this->get_grouped_column_types(); |
| 551 | $this->columns = $this->get_columns(); |
| 552 | } |
| 553 | |
| 554 | public function get_grouped_column_types() { |
| 555 | |
| 556 | $types = array(); |
| 557 | $groups = array_keys( $this->get_column_type_groups() ); |
| 558 | |
| 559 | $columns = array_merge( $this->default_columns, $this->custom_columns ); |
| 560 | |
| 561 | foreach ( $groups as $group ) { |
| 562 | $grouptypes = array(); |
| 563 | |
| 564 | foreach ( $columns as $index => $column ) { |
| 565 | if ( $column->properties->group == $group ) { |
| 566 | $grouptypes[ $index ] = $column; |
| 567 | unset( $columns[ $index ] ); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | $types[ $group ] = $grouptypes; |
| 572 | } |
| 573 | |
| 574 | return $types; |
| 575 | } |
| 576 | |
| 577 | public function get_column_type_groups() { |
| 578 | |
| 579 | $groups = array( |
| 580 | 'custom' => __( 'Custom', 'cpac' ), |
| 581 | 'default' => __( 'Default', 'cpac' ) |
| 582 | ); |
| 583 | |
| 584 | /** |
| 585 | * Filter the available column type groups |
| 586 | * |
| 587 | * @since 2.2 |
| 588 | * |
| 589 | * @param array $groups Available groups ([groupid] => [label]) |
| 590 | * @param CPAC_Storage_Model $storage_model_instance Storage model class instance |
| 591 | */ |
| 592 | $groups = apply_filters( "cac/storage_model/column_type_groups", $groups, $this ); |
| 593 | $groups = apply_filters( "cac/storage_model/column_type_groups/storage_key={$this->key}", $groups, $this ); |
| 594 | |
| 595 | // Integrations first |
| 596 | krsort( $groups ); |
| 597 | |
| 598 | return $groups; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * @since 2.0.2 |
| 603 | */ |
| 604 | public function get_registered_columns() { |
| 605 | $types = array(); |
| 606 | foreach ( $this->column_types as $grouptypes ) { |
| 607 | $types = array_merge( $types, $grouptypes ); |
| 608 | } |
| 609 | return $types; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * @since 2.3.4 |
| 614 | * @param string Column Type |
| 615 | */ |
| 616 | public function get_registered_column( $column_type ) { |
| 617 | $columns = $this->get_registered_columns(); |
| 618 | return isset( $columns[ $column_type ] ) ? $columns[ $column_type ] : false; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * @since 2.0 |
| 623 | */ |
| 624 | public function get_columns() { |
| 625 | |
| 626 | do_action( 'cac/get_columns', $this ); |
| 627 | |
| 628 | $columns = array(); |
| 629 | |
| 630 | // get columns |
| 631 | $default_columns = $this->get_default_columns(); |
| 632 | |
| 633 | // @todo check if this solves the issue with not displaying value when using "manage_{$post_type}_posts_columns" at CPAC_Storage_Model_Post |
| 634 | $registered_columns = $this->get_registered_columns(); |
| 635 | |
| 636 | if ( $stored_columns = $this->get_stored_columns() ) { |
| 637 | $stored_names = array(); |
| 638 | |
| 639 | foreach ( $stored_columns as $name => $options ) { |
| 640 | if ( ! isset( $options['type'] ) ) { |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | $stored_names[] = $name; |
| 645 | |
| 646 | // In case of a disabled plugin, we will skip column. |
| 647 | // This means the stored column type is not available anymore. |
| 648 | if ( ! in_array( $options['type'], array_keys( $registered_columns ) ) ) { |
| 649 | continue; |
| 650 | } |
| 651 | |
| 652 | // add an clone number which defines the instance |
| 653 | $column = clone $registered_columns[ $options['type'] ]; |
| 654 | $column->set_clone( $options['clone'] ); |
| 655 | |
| 656 | // preload options when php export is being used |
| 657 | $preload = $this->is_using_php_export() ? $options : false; |
| 658 | |
| 659 | // repopulate the options, so they contains the right stored options |
| 660 | $column->populate_options( $preload ); |
| 661 | |
| 662 | $column->sanitize_label(); |
| 663 | |
| 664 | $columns[ $name ] = $column; |
| 665 | } |
| 666 | |
| 667 | // In case of an enabled plugin, we will add that column. |
| 668 | // When $diff contains items, it means a default column has not been stored. |
| 669 | if ( $diff = array_diff( array_keys( $default_columns ), $this->get_default_stored_columns() ) ) { |
| 670 | foreach ( $diff as $name ) { |
| 671 | // because of the filter "manage_{$post_type}_posts_columns" the columns |
| 672 | // that are being added by CPAC will also appear in the $default_columns. |
| 673 | // this will filter out those columns. |
| 674 | if ( isset( $columns[ $name ] ) ) { |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | // is the column registered? |
| 679 | if ( ! isset( $registered_columns[ $name ] ) ) { |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | $columns[ $name ] = clone $registered_columns[ $name ]; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | // When nothing has been saved yet, we return the default WP columns. |
| 688 | else { |
| 689 | foreach ( array_keys( $default_columns ) as $name ) { |
| 690 | if ( isset( $registered_columns[ $name ] ) ) { |
| 691 | $columns[ $name ] = clone $registered_columns[ $name ]; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Filter the columns that should be loaded if there were no stored columns |
| 697 | * |
| 698 | * @since 2.2.4 |
| 699 | * |
| 700 | * @param array $columns List of columns ([column name] => [column instance]) |
| 701 | * @param CPAC_Storage_Model $storage_model_instance Storage model class instance |
| 702 | */ |
| 703 | $columns = apply_filters( 'cpac/storage_model/columns_default', $columns, $this ); |
| 704 | } |
| 705 | |
| 706 | do_action( "cac/columns", $columns ); |
| 707 | do_action( "cac/columns/storage_key={$this->key}", $columns ); |
| 708 | |
| 709 | return $columns; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * @since 2.0 |
| 714 | */ |
| 715 | public function get_column_by_name( $name ) { |
| 716 | |
| 717 | if ( ! isset( $this->columns[ $name ] ) ) { |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | return $this->columns[ $name ]; |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * @since 2.0 |
| 726 | */ |
| 727 | public function add_headings( $columns ) { |
| 728 | |
| 729 | // only add headings on overview screens, to prevent deactivating columns on the column settings screen |
| 730 | if ( ! $this->is_columns_screen() ) { |
| 731 | return $columns; |
| 732 | } |
| 733 | |
| 734 | if ( ! ( $stored_columns = $this->get_stored_columns() ) ) { |
| 735 | return $columns; |
| 736 | } |
| 737 | |
| 738 | $column_headings = array(); |
| 739 | |
| 740 | // add mandatory checkbox |
| 741 | if ( isset( $columns['cb'] ) ) { |
| 742 | $column_headings['cb'] = $columns['cb']; |
| 743 | } |
| 744 | |
| 745 | // add active stored headings |
| 746 | foreach ( $stored_columns as $column_name => $options ) { |
| 747 | |
| 748 | // Label needs stripslashes() for HTML tagged labels, like icons and checkboxes |
| 749 | $label = stripslashes( $options['label'] ); |
| 750 | |
| 751 | /** |
| 752 | * Filter the stored column headers label for use in a WP_List_Table |
| 753 | * Label needs stripslashes() for HTML tagged labels, like icons and checkboxes |
| 754 | * |
| 755 | * @since 2.0 |
| 756 | * @param string $label Label |
| 757 | * @param string $column_name Column name |
| 758 | * @param array $options Column options |
| 759 | * @param CPAC_Storage_Model $storage_model Storage model class instance |
| 760 | */ |
| 761 | $label = apply_filters( 'cac/headings/label', $label, $column_name, $options, $this ); |
| 762 | $label = str_replace( '[cpac_site_url]', site_url(), $label ); |
| 763 | |
| 764 | $column_headings[ $column_name ] = $label; |
| 765 | } |
| 766 | |
| 767 | // Add 3rd party columns that have ( or could ) not been stored. |
| 768 | // For example when a plugin has been activated after storing column settings. |
| 769 | // When $diff contains items, it means an available column has not been stored. |
| 770 | if ( ! $this->is_using_php_export() && ( $diff = array_diff( array_keys( $columns ), $this->get_default_stored_columns() ) ) ) { |
| 771 | foreach ( $diff as $column_name ) { |
| 772 | $column_headings[ $column_name ] = $columns[ $column_name ]; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return $column_headings; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * @since 2.0 |
| 781 | * @return string Link |
| 782 | */ |
| 783 | protected function get_screen_link() { |
| 784 | |
| 785 | return is_network_admin() ? network_admin_url( $this->page . '.php' ) : admin_url( $this->page . '.php' ); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * @since 2.0 |
| 790 | */ |
| 791 | public function screen_link() { |
| 792 | |
| 793 | if ( $link = $this->get_screen_link() ) { |
| 794 | echo '<a href="' . $link . '" class="add-new-h2">' . __('View', 'cpac') . '</a>'; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * @since 2.0 |
| 800 | */ |
| 801 | public function get_edit_link() { |
| 802 | |
| 803 | return add_query_arg( array( 'page' => 'codepress-admin-columns', 'cpac_key' => $this->key ), admin_url( 'options-general.php' ) ); |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Whether this request is an AJAX request and marked as admin-column-ajax request. |
| 808 | * Mark your admin columns ajax request with plugin_id : 'cpac'. |
| 809 | * |
| 810 | * @since 2.0.5 |
| 811 | * @return boolean |
| 812 | */ |
| 813 | public function is_doing_ajax() { |
| 814 | |
| 815 | return cac_is_doing_ajax(); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * @since 2.0.3 |
| 820 | * @global string $pagenow |
| 821 | * @global object $current_screen |
| 822 | * @return boolean |
| 823 | */ |
| 824 | public function is_columns_screen() { |
| 825 | |
| 826 | global $pagenow; |
| 827 | |
| 828 | if ( $this->page . '.php' != $pagenow ) { |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | // posttypes |
| 833 | if ( 'post' == $this->type ) { |
| 834 | $post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : $this->type; |
| 835 | |
| 836 | if ( $this->key != $post_type ) { |
| 837 | return false; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // taxonomy |
| 842 | if ( 'taxonomy' == $this->type ) { |
| 843 | $taxonomy = isset( $_GET['taxonomy'] ) ? $_GET['taxonomy'] : ''; |
| 844 | |
| 845 | if ( $this->taxonomy != $taxonomy ) { |
| 846 | return false; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | // users |
| 851 | if ( 'wp-users' == $this->key && is_network_admin() ) { |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Checks if the current page is the settings page |
| 860 | * |
| 861 | * @since 2.0.2 |
| 862 | * @global string $pagenow |
| 863 | * @global string $plugin_page |
| 864 | * @return boolean |
| 865 | */ |
| 866 | public function is_settings_page() { |
| 867 | global $pagenow, $plugin_page; |
| 868 | |
| 869 | return 'options-general.php' == $pagenow && ! empty( $plugin_page ) && 'codepress-admin-columns' == $plugin_page; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * @since 2.3.2 |
| 874 | */ |
| 875 | public function delete_general_option() { |
| 876 | delete_option( 'cpac_general_options' ); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * @since 2.1.1 |
| 881 | */ |
| 882 | public function get_general_option( $option ) { |
| 883 | $options = get_option( 'cpac_general_options' ); |
| 884 | |
| 885 | if ( ! isset( $options[ $option ] ) ) |
| 886 | return false; |
| 887 | |
| 888 | return $options[ $option ]; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * @since 3.1.2 |
| 893 | * @param $id Cache ID |
| 894 | * @param $column_name Column property name |
| 895 | * @return string MD5 Cache ID |
| 896 | */ |
| 897 | public function get_cache_id( $id, $column_name ) { |
| 898 | return md5( $this->key . $id . $column_name ); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * @since 3.1.2 |
| 903 | * @param $id Cache ID |
| 904 | * @param $column_name Column property name |
| 905 | * @param $cache_object Cache Object |
| 906 | */ |
| 907 | public function set_cache( $id, $column_name, $cache_object ) { |
| 908 | if ( empty( $cache_object ) ) { |
| 909 | return false; |
| 910 | } |
| 911 | set_transient( $this->get_cache_id( $id, $column_name ), $cache_object, 3600 * 24 * 7 ); // 7 days |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * @since 3.1.2 |
| 916 | * @param $id Cache ID ( could be a name of an addon for example ) |
| 917 | * @param $column_name Column property name |
| 918 | * @return false | mixed Returns either false or the cached objects |
| 919 | */ |
| 920 | public function get_cache( $id, $column_name ) { |
| 921 | $cache = get_transient( $this->get_cache_id( $id, $column_name ) ); |
| 922 | |
| 923 | if ( empty( $cache ) ) { |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | return $cache; |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * @since 3.1.2 |
| 932 | * @param $id Cache ID |
| 933 | * @param $column_name Column property name |
| 934 | */ |
| 935 | public function delete_cache( $id, $column_name ) { |
| 936 | delete_transient( $this->get_cache_id( $id, $column_name ) ); |
| 937 | } |
| 938 | } |