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