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