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