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