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