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