codepress-admin-columns
Last commit date
assets
11 years ago
classes
11 years ago
external
11 years ago
includes
11 years ago
languages
11 years ago
api.php
11 years ago
codepress-admin-columns.php
11 years ago
readme.txt
11 years ago
codepress-admin-columns.php
570 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Admin Columns |
| 4 | Version: 2.4 |
| 5 | Description: Customize columns on the administration screens for post(types), pages, media, comments, links and users with an easy to use drag-and-drop interface. |
| 6 | Author: AdminColumns.com |
| 7 | Author URI: http://www.admincolumns.com |
| 8 | Plugin URI: http://www.admincolumns.com |
| 9 | Text Domain: cpac |
| 10 | Domain Path: /languages |
| 11 | License: GPLv2 |
| 12 | |
| 13 | Copyright 2011-2015 AdminColumns.com info@admincolumns.com |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or modify |
| 16 | it under the terms of the GNU General Public License version 2 as published by |
| 17 | the Free Software Foundation. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | */ |
| 28 | |
| 29 | // Exit if accessed directly |
| 30 | if ( ! defined( 'ABSPATH' ) ) { |
| 31 | exit; |
| 32 | } |
| 33 | |
| 34 | // Plugin information |
| 35 | define( 'CPAC_VERSION', '2.4' ); // Current plugin version |
| 36 | define( 'CPAC_UPGRADE_VERSION', '2.0.0' ); // Latest version which requires an upgrade |
| 37 | define( 'CPAC_URL', plugin_dir_url( __FILE__ ) ); |
| 38 | define( 'CPAC_DIR', plugin_dir_path( __FILE__ ) ); |
| 39 | |
| 40 | // Only run plugin in the admin interface |
| 41 | if ( ! is_admin() ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Dependencies |
| 47 | * |
| 48 | * @since 1.3.0 |
| 49 | */ |
| 50 | require_once CPAC_DIR . 'classes/utility.php'; |
| 51 | require_once CPAC_DIR . 'classes/third_party.php'; |
| 52 | require_once CPAC_DIR . 'includes/arrays.php'; |
| 53 | require_once CPAC_DIR . 'api.php'; |
| 54 | |
| 55 | /** |
| 56 | * The Admin Columns Class |
| 57 | * |
| 58 | * @since 1.0 |
| 59 | */ |
| 60 | class CPAC { |
| 61 | |
| 62 | /** |
| 63 | * Registered storage model class instances |
| 64 | * Array of CPAC_Storage_Model instances, with the storage model keys (e.g. post, page, wp-users) as keys |
| 65 | * |
| 66 | * @since 2.0 |
| 67 | * @var array |
| 68 | */ |
| 69 | public $storage_models; |
| 70 | |
| 71 | /** |
| 72 | * Admin Columns add-ons class instance |
| 73 | * |
| 74 | * @since 2.2 |
| 75 | * @access private |
| 76 | * @var CPAC_Addons |
| 77 | */ |
| 78 | private $_addons; |
| 79 | |
| 80 | /** |
| 81 | * Admin Columns settings class instance |
| 82 | * |
| 83 | * @since 2.2 |
| 84 | * @access private |
| 85 | * @var CPAC_Settings |
| 86 | */ |
| 87 | private $_settings; |
| 88 | |
| 89 | /** |
| 90 | * Admin Columns plugin upgrade class instance |
| 91 | * |
| 92 | * @since 2.2.7 |
| 93 | * @access private |
| 94 | * @var CPAC_Upgrade |
| 95 | */ |
| 96 | private $_upgrade; |
| 97 | |
| 98 | /** |
| 99 | * @since 1.0 |
| 100 | */ |
| 101 | function __construct() { |
| 102 | |
| 103 | register_activation_hook( __FILE__, array( $this, 'set_capabilities' ) ); |
| 104 | |
| 105 | // Hooks |
| 106 | add_action( 'init', array( $this, 'localize' ) ); |
| 107 | add_action( 'wp_loaded', array( $this, 'maybe_set_storage_models' ), 5 ); |
| 108 | add_action( 'wp_loaded', array( $this, 'maybe_load_php_export' ) ); |
| 109 | add_action( 'wp_loaded', array( $this, 'after_setup' ) ); // Setup callback, important to load after set_storage_models |
| 110 | add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); |
| 111 | add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 1, 2 ); |
| 112 | |
| 113 | // Settings |
| 114 | include_once CPAC_DIR . 'classes/settings.php'; |
| 115 | $this->_settings = new CPAC_Settings( $this ); |
| 116 | |
| 117 | // Addons |
| 118 | include_once CPAC_DIR . 'classes/addons.php'; |
| 119 | $this->_addons = new CPAC_Addons( $this ); |
| 120 | |
| 121 | // Upgrade |
| 122 | require_once CPAC_DIR . 'classes/upgrade.php'; |
| 123 | $this->_upgrade = new CPAC_Upgrade( $this ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Fire callbacks for admin columns setup completion |
| 128 | * |
| 129 | * @since 2.2 |
| 130 | */ |
| 131 | public function after_setup() { |
| 132 | |
| 133 | /** |
| 134 | * Fires when Admin Columns is fully loaded |
| 135 | * Use this for setting up addon functionality |
| 136 | * |
| 137 | * @since 2.0 |
| 138 | * @param CPAC $cpac_instance Main Admin Columns plugin class instance |
| 139 | */ |
| 140 | do_action( 'cac/loaded', $this ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @since 2.2 |
| 145 | * @uses load_plugin_textdomain() |
| 146 | */ |
| 147 | public function localize() { |
| 148 | |
| 149 | load_plugin_textdomain( 'cpac', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @since 2.2.4 |
| 154 | */ |
| 155 | public function scripts() { |
| 156 | |
| 157 | wp_register_script( 'cpac-admin-columns', CPAC_URL . 'assets/js/admin-columns.js', array( 'jquery', 'jquery-qtip2' ), CPAC_VERSION ); |
| 158 | wp_register_script( 'jquery-qtip2', CPAC_URL . 'external/qtip2/jquery.qtip.min.js', array( 'jquery' ), CPAC_VERSION ); |
| 159 | wp_register_style( 'jquery-qtip2', CPAC_URL . 'external/qtip2/jquery.qtip.min.css', array(), CPAC_VERSION, 'all' ); |
| 160 | wp_register_style( 'cpac-columns', CPAC_URL . 'assets/css/column.css', array(), CPAC_VERSION, 'all' ); |
| 161 | |
| 162 | if ( $this->is_columns_screen() ) { |
| 163 | add_filter( 'admin_body_class', array( $this, 'admin_class' ) ); |
| 164 | add_action( 'admin_head', array( $this, 'admin_scripts') ); |
| 165 | |
| 166 | wp_enqueue_script( 'cpac-admin-columns' ); |
| 167 | wp_enqueue_style( 'jquery-qtip2' ); |
| 168 | wp_enqueue_style( 'cpac-columns' ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Add capabilty to administrator to manage admin columns. |
| 174 | * You can use the capability 'manage_admin_columns' to grant other roles this privilidge as well. |
| 175 | * |
| 176 | * @since 2.0.4 |
| 177 | */ |
| 178 | public function set_capabilities() { |
| 179 | if ( $role = get_role( 'administrator' ) ) { |
| 180 | $role->add_cap( 'manage_admin_columns' ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Load the storage models if the current screen is a columns screen |
| 186 | * |
| 187 | * @since 2.2.7 |
| 188 | */ |
| 189 | public function maybe_set_storage_models() { |
| 190 | |
| 191 | if ( ! $this->is_cac_screen() ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | $this->set_storage_models(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Load the php exported settings |
| 200 | * |
| 201 | * @since 2.3.5 |
| 202 | */ |
| 203 | public function maybe_load_php_export() { |
| 204 | global $_cac_exported_columns; |
| 205 | if ( $_cac_exported_columns ) { |
| 206 | foreach( $_cac_exported_columns as $model => $columns ) { |
| 207 | if ( $storage_model = $this->get_storage_model( $model ) ) { |
| 208 | $storage_model->set_stored_columns( $columns ); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Load the storage models, storing them in the storage_models property of this object |
| 216 | * |
| 217 | * @since 2.0 |
| 218 | */ |
| 219 | public function set_storage_models() { |
| 220 | |
| 221 | $storage_models = array(); |
| 222 | |
| 223 | // Load storage model class files and column base class files |
| 224 | require_once CPAC_DIR . 'classes/column.php'; |
| 225 | require_once CPAC_DIR . 'classes/column/default.php'; |
| 226 | require_once CPAC_DIR . 'classes/column/actions.php'; |
| 227 | require_once CPAC_DIR . 'classes/storage_model.php'; |
| 228 | require_once CPAC_DIR . 'classes/storage_model/post.php'; |
| 229 | require_once CPAC_DIR . 'classes/storage_model/user.php'; |
| 230 | require_once CPAC_DIR . 'classes/storage_model/media.php'; |
| 231 | require_once CPAC_DIR . 'classes/storage_model/comment.php'; |
| 232 | require_once CPAC_DIR . 'classes/storage_model/link.php'; |
| 233 | |
| 234 | // Create a storage model per post type |
| 235 | foreach ( $this->get_post_types() as $post_type ) { |
| 236 | $storage_model = new CPAC_Storage_Model_Post( $post_type ); |
| 237 | $storage_models[ $storage_model->key ] = $storage_model; |
| 238 | } |
| 239 | |
| 240 | // Create other storage models |
| 241 | $storage_model = new CPAC_Storage_Model_User(); |
| 242 | $storage_models[ $storage_model->key ] = $storage_model; |
| 243 | |
| 244 | $storage_model = new CPAC_Storage_Model_Media(); |
| 245 | $storage_models[ $storage_model->key ] = $storage_model; |
| 246 | |
| 247 | $storage_model = new CPAC_Storage_Model_Comment(); |
| 248 | $storage_models[ $storage_model->key ] = $storage_model; |
| 249 | |
| 250 | if ( apply_filters( 'pre_option_link_manager_enabled', false ) ) { // as of 3.5 link manager is removed |
| 251 | $storage_model = new CPAC_Storage_Model_Link(); |
| 252 | $storage_models[ $storage_model->key ] = $storage_model; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Filter the available storage models |
| 257 | * Used by external plugins to add additional storage models |
| 258 | * |
| 259 | * @since 2.0 |
| 260 | * @param array $storage_models List of storage model class instances ( [key] => [CPAC_Storage_Model object], where [key] is the storage key, such as "user", "post" or "my_custom_post_type") |
| 261 | * @param object $this CPAC |
| 262 | */ |
| 263 | $this->storage_models = apply_filters( 'cac/storage_models', $storage_models, $this ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Retrieve a storage model object based on its key |
| 268 | * |
| 269 | * @since 2.0 |
| 270 | * |
| 271 | * @param string $key Storage model key (e.g. post, page, wp-users) |
| 272 | * @return bool|CPAC_Storage_Model Storage Model object (or false, on failure) |
| 273 | */ |
| 274 | public function get_storage_model( $key ) { |
| 275 | |
| 276 | if ( isset( $this->storage_models[ $key ] ) ) { |
| 277 | return $this->storage_models[ $key ]; |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Get storage model object of currently active storage model |
| 285 | * On the users overview page, for example, this returns the CPAC_Storage_Model_User object |
| 286 | * |
| 287 | * @since 2.2.4 |
| 288 | * |
| 289 | * @return CPAC_Storage_Model |
| 290 | */ |
| 291 | public function get_current_storage_model() { |
| 292 | |
| 293 | if ( $this->storage_models ) { |
| 294 | foreach ( $this->storage_models as $storage_model ) { |
| 295 | if ( $storage_model->is_columns_screen() ) { |
| 296 | return $storage_model; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get a list of post types for which Admin Columns is active |
| 304 | * |
| 305 | * @since 1.0 |
| 306 | * |
| 307 | * @return array List of post type keys (e.g. post, page) |
| 308 | */ |
| 309 | public function get_post_types() { |
| 310 | |
| 311 | $post_types = array(); |
| 312 | |
| 313 | if ( post_type_exists( 'post' ) ) { |
| 314 | $post_types['post'] = 'post'; |
| 315 | } |
| 316 | |
| 317 | if ( post_type_exists( 'page' ) ) { |
| 318 | $post_types['page'] = 'page'; |
| 319 | } |
| 320 | |
| 321 | $post_types = array_merge( $post_types, get_post_types( array( |
| 322 | '_builtin' => false, |
| 323 | 'show_ui' => true |
| 324 | ) ) ); |
| 325 | |
| 326 | /** |
| 327 | * Filter the post types for which Admin Columns is active |
| 328 | * |
| 329 | * @since 2.0 |
| 330 | * @param array $post_types List of active post type names |
| 331 | */ |
| 332 | return apply_filters( 'cac/post_types', $post_types ); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Get a list of taxonomies supported by Admin Columns |
| 337 | * |
| 338 | * @since 1.0 |
| 339 | * |
| 340 | * @return array List of taxonomies |
| 341 | */ |
| 342 | public function get_taxonomies() { |
| 343 | |
| 344 | $taxonomies = get_taxonomies( array( 'public' => true ) ); |
| 345 | |
| 346 | if ( isset( $taxonomies['post_format'] ) ) { |
| 347 | unset( $taxonomies['post_format'] ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Filter the post types for which Admin Columns is active |
| 352 | * |
| 353 | * @since 2.0 |
| 354 | * @param array $post_types List of active post type names |
| 355 | */ |
| 356 | return apply_filters( 'cac/taxonomies', $taxonomies ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Add a settings link to the Admin Columns entry in the plugin overview screen |
| 361 | * |
| 362 | * @since 1.0 |
| 363 | * @see filter:plugin_action_links |
| 364 | */ |
| 365 | public function add_settings_link( $links, $file ) { |
| 366 | |
| 367 | if ( $file != plugin_basename( __FILE__ ) ) { |
| 368 | return $links; |
| 369 | } |
| 370 | |
| 371 | array_unshift( $links, '<a href="' . esc_url( admin_url( "options-general.php?page=codepress-admin-columns" ) ) . '">' . __( 'Settings' ) . '</a>' ); |
| 372 | |
| 373 | return $links; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Adds a body class which is used to set individual column widths |
| 378 | * |
| 379 | * @since 1.4.0 |
| 380 | * |
| 381 | * @param string $classes body classes |
| 382 | * @return string |
| 383 | */ |
| 384 | public function admin_class( $classes ) { |
| 385 | |
| 386 | if ( $storage_model = $this->get_current_storage_model() ) { |
| 387 | $classes .= " cp-{$storage_model->key}"; |
| 388 | } |
| 389 | |
| 390 | return $classes; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Admin CSS for Column width and Settings Icon |
| 395 | * |
| 396 | * @since 1.4.0 |
| 397 | */ |
| 398 | public function admin_scripts() { |
| 399 | |
| 400 | if ( ! ( $storage_model = $this->get_current_storage_model() ) ) { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | $css_column_width = ''; |
| 405 | $edit_link = ''; |
| 406 | |
| 407 | // CSS: columns width |
| 408 | if ( $columns = $storage_model->get_stored_columns() ) { |
| 409 | foreach ( $columns as $name => $options ) { |
| 410 | |
| 411 | if ( ! empty( $options['width'] ) && is_numeric( $options['width'] ) && $options['width'] > 0 ) { |
| 412 | $unit = isset( $options['width_unit'] ) ? $options['width_unit'] : '%'; |
| 413 | $css_column_width .= ".cp-{$storage_model->key} .wrap table th.column-{$name} { width: {$options['width']}{$unit} !important; }"; |
| 414 | } |
| 415 | |
| 416 | // Load custom column scripts, used by 3rd party columns |
| 417 | if ( $column = $storage_model->get_column_by_name( $name ) ) { |
| 418 | $column->scripts(); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // JS: edit button |
| 424 | $general_options = get_option( 'cpac_general_options' ); |
| 425 | if ( current_user_can( 'manage_admin_columns' ) && ( ! isset( $general_options['show_edit_button'] ) || '1' === $general_options['show_edit_button'] ) ) { |
| 426 | $edit_link = $storage_model->get_edit_link(); |
| 427 | } |
| 428 | |
| 429 | ?> |
| 430 | <?php if ( $css_column_width ) : ?> |
| 431 | <style type="text/css"> |
| 432 | <?php echo $css_column_width; ?> |
| 433 | </style> |
| 434 | <?php endif; ?> |
| 435 | <?php if ( $edit_link ) : ?> |
| 436 | <script type="text/javascript"> |
| 437 | jQuery(document).ready(function() { |
| 438 | jQuery('.tablenav.top .actions:last').append('<a href="<?php echo $edit_link; ?>" class="cpac-edit add-new-h2"><?php _e( 'Edit columns', 'cpac' ); ?></a>'); |
| 439 | }); |
| 440 | </script> |
| 441 | <?php endif; ?> |
| 442 | |
| 443 | <?php |
| 444 | |
| 445 | /** |
| 446 | * Add header scripts that only apply to column screens. |
| 447 | * @since 2.3.5 |
| 448 | * @param object CPAC Main Class |
| 449 | */ |
| 450 | do_action( 'cac/admin_head', $storage_model, $this ); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Whether this request is an AJAX request and marked as admin-column-ajax or inline-save request. |
| 455 | * |
| 456 | * @since 2.2 |
| 457 | * @return bool Returns true if in an AJAX request, false otherwise |
| 458 | */ |
| 459 | public function is_doing_ajax() { |
| 460 | |
| 461 | return cac_is_doing_ajax(); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Whether this request is a columns screen (i.e. a content overview page) |
| 466 | * |
| 467 | * @since 2.2 |
| 468 | * @return bool Returns true if the current screen is a columns screen, false otherwise |
| 469 | */ |
| 470 | public function is_columns_screen() { |
| 471 | |
| 472 | global $pagenow; |
| 473 | |
| 474 | $columns_screen = in_array( $pagenow, array( 'edit.php', 'upload.php', 'link-manager.php', 'edit-comments.php', 'users.php', 'edit-tags.php' ) ); |
| 475 | |
| 476 | /** |
| 477 | * Filter whether the current screen is a columns screen (i.e. a content overview page) |
| 478 | * Useful for advanced used with custom content overview pages |
| 479 | * |
| 480 | * @since 2.2 |
| 481 | * @param bool $columns_screen Whether the current request is a columns screen |
| 482 | */ |
| 483 | $columns_screen = apply_filters( 'cac/is_columns_screen', $columns_screen ); |
| 484 | |
| 485 | return $columns_screen; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Whether the current screen is the Admin Columns settings screen |
| 490 | * |
| 491 | * @since 2.2 |
| 492 | * @return bool True if the current screen is the settings screen, false otherwise |
| 493 | */ |
| 494 | public function is_settings_screen() { |
| 495 | |
| 496 | global $pagenow; |
| 497 | |
| 498 | if ( ! ( 'options-general.php' === $pagenow && isset( $_GET['page'] ) && ( 'codepress-admin-columns' === $_GET['page'] ) ) ) { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Whether the current screen is a screen in which Admin Columns is used |
| 507 | * Used to check whether storage models should be loaded |
| 508 | * |
| 509 | * @since 2.2 |
| 510 | * @return bool Whether the current screen is an Admin Columns screen |
| 511 | */ |
| 512 | public function is_cac_screen() { |
| 513 | |
| 514 | /** |
| 515 | * Filter whether the current screen is a screen in which Admin Columns is active |
| 516 | * |
| 517 | * @since 2.2 |
| 518 | * @param bool $is_cac_screen Whether the current screen is an Admin Columns screen |
| 519 | */ |
| 520 | return apply_filters( 'cac/is_cac_screen', $this->is_columns_screen() || $this->is_doing_ajax() || $this->is_settings_screen() ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Get admin columns settings class instance |
| 525 | * |
| 526 | * @since 2.2 |
| 527 | * @return CPAC_Settings Settings class instance |
| 528 | */ |
| 529 | public function settings() { |
| 530 | |
| 531 | return $this->_settings; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Get admin columns add-ons class instance |
| 536 | * |
| 537 | * @since 2.2 |
| 538 | * @return CPAC_Addons Add-ons class instance |
| 539 | */ |
| 540 | public function addons() { |
| 541 | |
| 542 | return $this->_addons; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Get admin columns upgrade class instance |
| 547 | * |
| 548 | * @since 2.2.7 |
| 549 | * @return CPAC_Upgrade Upgrade class instance |
| 550 | */ |
| 551 | public function upgrade() { |
| 552 | |
| 553 | return $this->_upgrade; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Admin Columns class (global for backwards compatibility) |
| 559 | * |
| 560 | * @since 1.0 |
| 561 | * @deprecated 2.2.7 Use filter cac/loaded instead. |
| 562 | */ |
| 563 | global $cpac; |
| 564 | |
| 565 | /** |
| 566 | * Initialize Admin Columns class |
| 567 | * |
| 568 | * @since 1.0 |
| 569 | */ |
| 570 | $cpac = new CPAC(); |