column
12 years ago
storage_model
12 years ago
api.php
12 years ago
column.php
12 years ago
deprecated.php
12 years ago
export_import.php
12 years ago
settings.php
12 years ago
storage_model.php
12 years ago
third_party.php
12 years ago
upgrade.php
12 years ago
utility.php
12 years ago
storage_model.php
569 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Storage Model |
| 5 | * |
| 6 | * @since 2.0.0 |
| 7 | */ |
| 8 | abstract class CPAC_Storage_Model { |
| 9 | |
| 10 | /** |
| 11 | * Label |
| 12 | * |
| 13 | * @since 2.0.0 |
| 14 | */ |
| 15 | public $label; |
| 16 | |
| 17 | /** |
| 18 | * Key |
| 19 | * |
| 20 | * Identifier for Storage Model; Posttype etc. |
| 21 | * |
| 22 | * @since 2.0.0 |
| 23 | */ |
| 24 | public $key; |
| 25 | |
| 26 | /** |
| 27 | * Type |
| 28 | * |
| 29 | * Type of storage model; Post, Media, User or Comments |
| 30 | * |
| 31 | * @since 2.0.0 |
| 32 | */ |
| 33 | public $type; |
| 34 | |
| 35 | /** |
| 36 | * Page |
| 37 | * |
| 38 | * @since 2.0.0 |
| 39 | */ |
| 40 | public $page; |
| 41 | |
| 42 | /** |
| 43 | * Custom Column |
| 44 | * |
| 45 | * @since 2.0.1 |
| 46 | */ |
| 47 | protected $custom_columns; |
| 48 | |
| 49 | /** |
| 50 | * Get default columns |
| 51 | * |
| 52 | * @since 2.0.0 |
| 53 | * |
| 54 | * @return array Column Name | Column Label |
| 55 | */ |
| 56 | abstract function get_default_columns(); |
| 57 | |
| 58 | /** |
| 59 | * Checks if menu type is currently viewed |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | * |
| 63 | * @param string $key |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function is_menu_type_current( $first_posttpe ) { |
| 67 | |
| 68 | // display the page that was being viewed before saving |
| 69 | if ( ! empty( $_REQUEST['cpac_key'] ) ) { |
| 70 | if ( $_REQUEST['cpac_key'] == $this->key ) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // settings page has not yet been saved |
| 75 | } elseif ( $first_posttpe == $this->key ) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get Meta Keys |
| 84 | * |
| 85 | * @since 2.0.0 |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public function get_meta_keys( $add_hidden_meta = false ) { |
| 90 | global $wpdb; |
| 91 | |
| 92 | $keys = array(); |
| 93 | |
| 94 | $fields = $this->get_meta(); |
| 95 | |
| 96 | if ( is_wp_error( $fields ) || empty( $fields ) ) |
| 97 | $keys = false; |
| 98 | |
| 99 | if ( $fields ) { |
| 100 | foreach ( $fields as $field ) { |
| 101 | |
| 102 | // give hidden fields a prefix for identifaction |
| 103 | if ( $add_hidden_meta && "_" == substr( $field[0], 0, 1 ) ) { |
| 104 | $keys[] = 'cpachidden' . $field[0]; |
| 105 | } |
| 106 | |
| 107 | // non hidden fields are saved as is |
| 108 | elseif ( "_" != substr( $field[0], 0, 1 ) ) { |
| 109 | $keys[] = $field[0]; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return apply_filters( "cac/meta_keys/storage_key={$this->key}", $keys, $this ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add hidden meta - Utility Method |
| 119 | * |
| 120 | * @since 2.0.0 |
| 121 | * |
| 122 | * @param array $fields Custom fields. |
| 123 | * @return array Custom fields. |
| 124 | */ |
| 125 | protected function add_hidden_meta( $fields ) { |
| 126 | if ( ! $fields ) |
| 127 | return false; |
| 128 | |
| 129 | $combined_fields = array(); |
| 130 | |
| 131 | // filter out hidden meta fields |
| 132 | foreach ( $fields as $field ) { |
| 133 | |
| 134 | // give hidden fields a prefix for identifaction |
| 135 | if ( "_" == substr( $field[0], 0, 1 ) ) { |
| 136 | $combined_fields[] = 'cpachidden'.$field[0]; |
| 137 | } |
| 138 | |
| 139 | // non hidden fields are saved as is |
| 140 | elseif ( "_" != substr( $field[0], 0, 1 ) ) { |
| 141 | $combined_fields[] = $field[0]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if ( empty( $combined_fields ) ) |
| 146 | return false; |
| 147 | |
| 148 | return $combined_fields; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Restore |
| 153 | * |
| 154 | * @since 2.0.0 |
| 155 | */ |
| 156 | function restore() { |
| 157 | |
| 158 | delete_option( "cpac_options_{$this->key}" ); |
| 159 | |
| 160 | cpac_admin_message( "<strong>{$this->label}</strong> " . __( 'settings succesfully restored.', 'cpac' ), 'updated' ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Store |
| 165 | * |
| 166 | * @since 2.0.0 |
| 167 | */ |
| 168 | function store( $columns = '' ) { |
| 169 | |
| 170 | if ( ! empty( $_POST[ $this->key ] ) ) |
| 171 | $columns = array_filter( $_POST[ $this->key ] ); |
| 172 | |
| 173 | if( ! $columns ) { |
| 174 | cpac_admin_message( __( 'No columns settings available.', 'cpac' ), 'error' ); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // sanitize user inputs |
| 179 | foreach ( $columns as $name => $options ) { |
| 180 | if ( $_column = $this->get_column_by_name( $name ) ) { |
| 181 | $columns[ $name ] = $_column->sanitize_storage( $options ); |
| 182 | } |
| 183 | |
| 184 | // Santize Label: Need to replace the url for images etc, so we do not have url problem on exports |
| 185 | // this can not be done by CPAC_Column::sanitize_storage() because 3rd party plugins are not available there |
| 186 | $columns[ $name ]['label'] = stripslashes( str_replace( site_url(), '[cpac_site_url]', trim( $options['label'] ) ) ); |
| 187 | } |
| 188 | |
| 189 | // store columns |
| 190 | $result = update_option( "cpac_options_{$this->key}", $columns ); |
| 191 | |
| 192 | // store default WP columns |
| 193 | $result_default = update_option( "cpac_options_{$this->key}_default", array_keys( $this->get_default_columns() ) ); |
| 194 | |
| 195 | // error |
| 196 | if( ! $result && ! $result_default ) { |
| 197 | cpac_admin_message( sprintf( __( 'You are trying to store the same settings for %s.', 'cpac' ), "<strong>{$this->label}</strong>" ), 'error' ); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | cpac_admin_message( sprintf( __( 'Settings for %s updated succesfully.', 'cpac' ), "<strong>{$this->label}</strong>" ), 'updated' ); |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Set custom columns |
| 208 | * |
| 209 | * Goes through all files in 'classes/column' and includes each file. |
| 210 | * |
| 211 | * since 2.0.1 |
| 212 | * |
| 213 | * @return array Column Classnames | Filepaths |
| 214 | */ |
| 215 | protected function set_custom_columns() { |
| 216 | $columns = array( |
| 217 | 'CPAC_Column_Custom_Field' => CPAC_DIR . 'classes/column/custom-field.php' |
| 218 | ); |
| 219 | |
| 220 | $iterator = new DirectoryIterator( CPAC_DIR . 'classes/column/' . $this->type ); |
| 221 | |
| 222 | foreach( $iterator as $leaf ) { |
| 223 | |
| 224 | if ( $leaf->isDot() ) continue; |
| 225 | |
| 226 | // build classname from filename |
| 227 | $class_name = implode( '_', array_map( 'ucfirst', explode( '-', basename( $leaf->getFilename(), '.php' ) ) ) ); |
| 228 | |
| 229 | $columns[ 'CPAC_Column_' . ucfirst( $this->type ) . '_' . $class_name ] = $leaf->getPathname(); |
| 230 | } |
| 231 | |
| 232 | $this->custom_columns = apply_filters( 'cac/columns/custom/type=' . $this->type, $columns, $this ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Create column instance |
| 237 | * |
| 238 | * @since 2.0.0 |
| 239 | * |
| 240 | * @param $column_name |
| 241 | * @param $label |
| 242 | * @return object CPAC_Column |
| 243 | */ |
| 244 | public function create_column_instance( $column_name, $label ) { |
| 245 | |
| 246 | // create column instance |
| 247 | $column = new CPAC_Column( $this ); |
| 248 | |
| 249 | $column |
| 250 | ->set_properties( 'type', $column_name ) |
| 251 | ->set_properties( 'name', $column_name ) |
| 252 | ->set_properties( 'label', $label ) |
| 253 | ->set_properties( 'is_cloneable', false ) |
| 254 | ->set_options( 'label', $label ) |
| 255 | ->set_options( 'state', 'on' ); |
| 256 | |
| 257 | // Hide Label when it contains HTML elements |
| 258 | if( strlen( $label ) != strlen( strip_tags( $label ) ) ) { |
| 259 | $column->set_properties( 'hide_label', true ); |
| 260 | } |
| 261 | |
| 262 | // Label empty? Use it's column_name |
| 263 | if ( ! $label ) { |
| 264 | $column->set_properties( 'label', ucfirst( $column_name ) ); |
| 265 | } |
| 266 | |
| 267 | return $column; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Get default registered columns |
| 272 | * |
| 273 | * @since 2.0.0 |
| 274 | * |
| 275 | * @return array Column Type | Column Instance |
| 276 | */ |
| 277 | function get_default_registered_columns() { |
| 278 | |
| 279 | $columns = array(); |
| 280 | |
| 281 | // Default columns |
| 282 | foreach ( $this->get_default_columns() as $column_name => $label ) { |
| 283 | |
| 284 | // checkboxes are mandatory |
| 285 | if ( 'cb' == $column_name ) |
| 286 | continue; |
| 287 | |
| 288 | $column = $this->create_column_instance( $column_name, $label ); |
| 289 | |
| 290 | $columns[ $column->properties->name ] = $column; |
| 291 | } |
| 292 | |
| 293 | do_action( "cac/columns/registered/default", $columns ); |
| 294 | do_action( "cac/columns/registered/default/storage_key={$this->key}", $columns ); |
| 295 | |
| 296 | return $columns; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get custom registered columns |
| 301 | * |
| 302 | * @since 2.0.0 |
| 303 | * |
| 304 | * @return array Column Type | Column Instance |
| 305 | */ |
| 306 | function get_custom_registered_columns() { |
| 307 | |
| 308 | $columns = array(); |
| 309 | |
| 310 | foreach ( $this->custom_columns as $classname => $path ) { |
| 311 | |
| 312 | include_once $path; |
| 313 | |
| 314 | if ( ! class_exists( $classname ) ) |
| 315 | continue; |
| 316 | |
| 317 | $column = new $classname( $this ); |
| 318 | |
| 319 | // exlude columns that are not registered based on conditional logic within the child column |
| 320 | if ( ! $column->properties->is_registered ) |
| 321 | continue; |
| 322 | |
| 323 | $columns[ $column->properties->type ] = $column; |
| 324 | } |
| 325 | |
| 326 | do_action( "cac/columns/registered/custom", $columns ); |
| 327 | do_action( "cac/columns/registered/custom/storage_key={$this->key}", $columns ); |
| 328 | |
| 329 | return $columns; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get registered columns |
| 334 | * |
| 335 | * @todo: REMOVE |
| 336 | * @since 2.0.0 |
| 337 | * |
| 338 | * @return array Column Type | Column Instance |
| 339 | */ |
| 340 | function get_registered_columns() { |
| 341 | |
| 342 | return array_merge( $this->get_custom_registered_columns(), $this->get_default_registered_columns() ); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Get default column options from DB |
| 347 | * |
| 348 | * @since 1.0.0 |
| 349 | * |
| 350 | * @paran string $key |
| 351 | * @return array Column options |
| 352 | */ |
| 353 | public function get_default_stored_columns() { |
| 354 | |
| 355 | if ( ! $columns = get_option( "cpac_options_{$this->key}_default" ) ) |
| 356 | return array(); |
| 357 | |
| 358 | return $columns; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Get column options from DB |
| 363 | * |
| 364 | * @since 1.0.0 |
| 365 | * |
| 366 | * @paran string $key |
| 367 | * @return array Column options |
| 368 | */ |
| 369 | public function get_stored_columns() { |
| 370 | |
| 371 | if ( ! $columns = get_option( "cpac_options_{$this->key}" ) ) |
| 372 | return array(); |
| 373 | |
| 374 | return $columns; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Get Columns |
| 379 | * |
| 380 | * @since 2.0.0 |
| 381 | */ |
| 382 | function get_columns() { |
| 383 | |
| 384 | // used for third party plugin support |
| 385 | do_action( 'cac/get_columns', $this ); |
| 386 | |
| 387 | $columns = array(); |
| 388 | |
| 389 | // get columns |
| 390 | $default_columns = $this->get_default_registered_columns(); |
| 391 | |
| 392 | // @todo check if this solves the issue with not displaying value when using "manage_{$post_type}_posts_columns" at CPAC_Storage_Model_Post |
| 393 | $registered_columns = array_merge( $default_columns, $this->get_custom_registered_columns() ); |
| 394 | |
| 395 | // Stored columns |
| 396 | if ( $stored_columns = $this->get_stored_columns() ) { |
| 397 | |
| 398 | $stored_names = array(); |
| 399 | |
| 400 | foreach ( $stored_columns as $name => $options ) { |
| 401 | |
| 402 | if ( ! isset( $options['type'] ) ) |
| 403 | continue; |
| 404 | |
| 405 | // remember which types has been used, so we can filter them later |
| 406 | $stored_names[] = $name; |
| 407 | |
| 408 | // In case of a disabled plugin, we will skip column. |
| 409 | // This means the stored column type is not available anymore. |
| 410 | if ( ! in_array( $options['type'], array_keys( $registered_columns ) ) ) |
| 411 | continue; |
| 412 | |
| 413 | // create clone |
| 414 | $column = clone $registered_columns[ $options['type'] ]; |
| 415 | |
| 416 | // add an clone number which defines the instance |
| 417 | $column->set_clone( $options['clone'] ); |
| 418 | |
| 419 | // repopulate the options, so they contains the right stored options |
| 420 | $column->populate_options(); |
| 421 | |
| 422 | // re-sanitize label |
| 423 | $column->sanitize_label(); |
| 424 | |
| 425 | $columns[ $name ] = $column; |
| 426 | } |
| 427 | |
| 428 | // In case of an enabled plugin, we will add that column. |
| 429 | // When $diff contains items, it means a default column has not been stored. |
| 430 | if( $diff = array_diff( array_keys( $default_columns ), $this->get_default_stored_columns() ) ) { |
| 431 | foreach( $diff as $name ) { |
| 432 | |
| 433 | // because of the filter "manage_{$post_type}_posts_columns" the columns |
| 434 | // that are being added by CPAC will also appear in the $default_columns. |
| 435 | // this will filter out those columns. |
| 436 | if ( isset( $columns[ $name ] ) ) continue; |
| 437 | |
| 438 | // is the column registered? |
| 439 | if ( ! isset( $registered_columns[ $name ] ) ) continue; |
| 440 | |
| 441 | $columns[ $name ] = clone $registered_columns[ $name ]; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // When nothing has been saved yet, we return the default WP columns. |
| 447 | else { |
| 448 | |
| 449 | foreach( array_keys( $default_columns ) as $name ) { |
| 450 | if( isset( $registered_columns[ $name ] ) ) { |
| 451 | $columns[ $name ] = clone $registered_columns[ $name ]; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | return $columns; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Get Column by name |
| 461 | * |
| 462 | * @since 2.0.0 |
| 463 | */ |
| 464 | function get_column_by_name( $name ) { |
| 465 | |
| 466 | $columns = $this->get_columns(); |
| 467 | if ( ! isset( $columns[ $name ] ) ) |
| 468 | return false; |
| 469 | |
| 470 | return $columns[ $name ]; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Add Headings |
| 475 | * |
| 476 | * @since 2.0.0 |
| 477 | */ |
| 478 | function add_headings( $columns ) { |
| 479 | |
| 480 | global $pagenow; |
| 481 | |
| 482 | // only add headings on overview screens, to prevent deactivating columns in the Storage Model. |
| 483 | if ( ! in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'upload.php', 'link-manager.php' ) ) ) |
| 484 | return $columns; |
| 485 | |
| 486 | // stored columns exists? |
| 487 | if ( ! $stored_columns = get_option( "cpac_options_{$this->key}" ) ) |
| 488 | return $columns; |
| 489 | |
| 490 | // build the headings |
| 491 | $column_headings = array(); |
| 492 | |
| 493 | // add mandatory checkbox |
| 494 | if ( isset( $columns['cb'] ) ) |
| 495 | $column_headings['cb'] = $columns['cb']; |
| 496 | |
| 497 | // add active stored headings |
| 498 | foreach( $stored_columns as $column_name => $options ) { |
| 499 | |
| 500 | // label needs stripslashes() for HTML tagged labels, like icons and checkboxes |
| 501 | $label = apply_filters( 'cac/headings/label', stripslashes( $options['label'] ), $column_name, $options, $this ); |
| 502 | |
| 503 | // maybe need site_url replacement |
| 504 | $label = str_replace( '[cpac_site_url]', site_url(), $label ); |
| 505 | |
| 506 | $column_headings[ $column_name ] = $label; |
| 507 | } |
| 508 | |
| 509 | // Add 3rd party columns that have ( or could ) not been stored. |
| 510 | // For example when a plugin has been activated after storing column settings. |
| 511 | // When $diff contains items, it means an available column has not been stored. |
| 512 | if ( $diff = array_diff( array_keys( $columns ), $this->get_default_stored_columns() ) ) { |
| 513 | foreach ( $diff as $column_name ) { |
| 514 | |
| 515 | $column_headings[ $column_name ] = $columns[ $column_name ]; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // Remove 3rd party columns that have been deactivated. |
| 520 | // While the column settings have not been stored yet. |
| 521 | // When $diff contains items, it means the default stored columns are not available anymore. |
| 522 | // @todo: check if working properly. cuurently issues with woocommerce columns |
| 523 | /* |
| 524 | if ( $diff = array_diff( $this->get_default_stored_columns(), array_keys( $columns ) ) ) { |
| 525 | //echo '<pre>'; print_r( $diff ); echo '</pre>'; |
| 526 | foreach ( $diff as $column_name ) { |
| 527 | if( isset( $column_headings[ $column_name ] ) ) |
| 528 | unset( $column_headings[ $column_name ] ); |
| 529 | } |
| 530 | }*/ |
| 531 | |
| 532 | |
| 533 | |
| 534 | return $column_headings; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get screen link |
| 539 | * |
| 540 | * @since 2.0.0 |
| 541 | * |
| 542 | * @return string Link |
| 543 | */ |
| 544 | protected function get_screen_link() { |
| 545 | |
| 546 | return admin_url( $this->page . '.php' ); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Screen Link |
| 551 | * |
| 552 | * @since 2.0.0 |
| 553 | */ |
| 554 | function screen_link() { |
| 555 | |
| 556 | echo '<a href="' . $this->get_screen_link() . '" class="add-new-h2">' . __('View', 'cpac') . '</a>'; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Screen Link |
| 561 | * |
| 562 | * @since 2.0.0 |
| 563 | */ |
| 564 | function get_edit_link() { |
| 565 | |
| 566 | return add_query_arg( array( 'page' => 'codepress-admin-columns', 'cpac_key' => $this->key ), admin_url( 'options-general.php' ) ); |
| 567 | } |
| 568 | } |
| 569 |