codepress-admin-columns.php
| 1 | <?php |
| 2 | /* |
| 3 | |
| 4 | Plugin Name: Codepress Admin Columns |
| 5 | Version: 2.2.1.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.1.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.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.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 | // Load scripts |
| 109 | $this->init_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.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 |
| 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 | $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 159 | |
| 160 | /** |
| 161 | * Filter whether the current request should be marked as an AJAX request |
| 162 | * Useful for custom AJAX calls |
| 163 | * |
| 164 | * @since 2.2 |
| 165 | * @param bool $doing_ajax Whether the current request is an AJAX request |
| 166 | */ |
| 167 | $doing_ajax = apply_filters( 'cac/is_doing_ajax', $doing_ajax ); |
| 168 | |
| 169 | return $doing_ajax; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Whether this request is a columns screen (i.e. a content overview page) |
| 174 | * |
| 175 | * @since 2.2 |
| 176 | * @return bool Returns true if the current screen is a columns screen, false otherwise |
| 177 | */ |
| 178 | function is_columns_screen() { |
| 179 | |
| 180 | global $pagenow; |
| 181 | |
| 182 | $columns_screen = in_array( $pagenow, array( 'edit.php', 'upload.php', 'link-manager.php', 'edit-comments.php', 'users.php', 'edit-tags.php' ) ); |
| 183 | |
| 184 | /** |
| 185 | * Filter whether the current screen is a columns screen (i.e. a content overview page) |
| 186 | * Useful for advanced used with custom content overview pages |
| 187 | * |
| 188 | * @since 2.2 |
| 189 | * @param bool $columns_screen Whether the current request is a columns screen |
| 190 | */ |
| 191 | $columns_screen = apply_filters( 'cac/is_columns_screen', $columns_screen ); |
| 192 | |
| 193 | return $columns_screen; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Whether the current screen is the Admin Columns settings screen |
| 198 | * |
| 199 | * @since 2.2 |
| 200 | * @return bool True if the current screen is the settings screen, false otherwise |
| 201 | */ |
| 202 | function is_settings_screen() { |
| 203 | |
| 204 | global $pagenow; |
| 205 | |
| 206 | if ( ! ( 'options-general.php' === $pagenow && isset( $_GET['page'] ) && ( 'codepress-admin-columns' === $_GET['page'] ) ) ) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Whether the current screen is a screen in which Admin Columns is used |
| 215 | * Used to check whether storage models should be loaded |
| 216 | * |
| 217 | * @since 2.2 |
| 218 | * @return bool Whether the current screen is an Admin Columns screen |
| 219 | */ |
| 220 | function is_cac_screen() { |
| 221 | /** |
| 222 | * Filter whether the current screen is a screen in which Admin Columns is active |
| 223 | * |
| 224 | * @since 2.2 |
| 225 | * @param bool $is_cac_screen Whether the current screen is an Admin Columns screen |
| 226 | */ |
| 227 | return apply_filters( 'cac/is_cac_screen', $this->is_columns_screen() || $this->is_doing_ajax() || $this->is_settings_screen() ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @since 2.1.1 |
| 232 | */ |
| 233 | public function init_scripts() { |
| 234 | |
| 235 | add_action( 'admin_head', array( $this, 'global_head_scripts') ); |
| 236 | |
| 237 | if ( $this->is_columns_screen() ) { |
| 238 | add_action( 'admin_enqueue_scripts' , array( $this, 'column_styles') ); |
| 239 | add_filter( 'admin_body_class', array( $this, 'admin_class' ) ); |
| 240 | add_action( 'admin_head', array( $this, 'admin_scripts') ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Add capabilty to administrator to manage admin columns. |
| 246 | * You can use the capability 'manage_admin_columns' to grant other roles this privilidge as well. |
| 247 | * |
| 248 | * @since 2.0.4 |
| 249 | */ |
| 250 | public function set_capabilities() { |
| 251 | if ( $role = get_role( 'administrator' ) ) { |
| 252 | $role->add_cap( 'manage_admin_columns' ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @since 2.0.0 |
| 258 | */ |
| 259 | public function set_storage_models() { |
| 260 | |
| 261 | if ( ! $this->is_cac_screen() ) |
| 262 | return; |
| 263 | |
| 264 | $storage_models = array(); |
| 265 | |
| 266 | // include parent and childs |
| 267 | require_once CPAC_DIR . 'classes/column.php'; |
| 268 | require_once CPAC_DIR . 'classes/storage_model.php'; |
| 269 | require_once CPAC_DIR . 'classes/storage_model/post.php'; |
| 270 | require_once CPAC_DIR . 'classes/storage_model/user.php'; |
| 271 | require_once CPAC_DIR . 'classes/storage_model/media.php'; |
| 272 | require_once CPAC_DIR . 'classes/storage_model/comment.php'; |
| 273 | require_once CPAC_DIR . 'classes/storage_model/link.php'; |
| 274 | |
| 275 | foreach ( $this->get_post_types() as $post_type ) { |
| 276 | $storage_model = new CPAC_Storage_Model_Post( $post_type ); |
| 277 | $storage_models[ $storage_model->key ] = $storage_model; |
| 278 | } |
| 279 | |
| 280 | $storage_model = new CPAC_Storage_Model_User(); |
| 281 | $storage_models[ $storage_model->key ] = $storage_model; |
| 282 | |
| 283 | $storage_model = new CPAC_Storage_Model_Media(); |
| 284 | $storage_models[ $storage_model->key ] = $storage_model; |
| 285 | |
| 286 | $storage_model = new CPAC_Storage_Model_Comment(); |
| 287 | $storage_models[ $storage_model->key ] = $storage_model; |
| 288 | |
| 289 | if ( apply_filters( 'pre_option_link_manager_enabled', false ) ) { // as of 3.5 link manager is removed |
| 290 | $storage_model = new CPAC_Storage_Model_Link(); |
| 291 | $storage_models[ $storage_model->key ] = $storage_model; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Filter the available storage models |
| 296 | * Used by external plugins to add additional storage models |
| 297 | * |
| 298 | * @since 2.0.0 |
| 299 | * @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") |
| 300 | */ |
| 301 | $this->storage_models = apply_filters( 'cac/storage_models', $storage_models ); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @since 2.0.0 |
| 306 | * @return array|false object Storage Model |
| 307 | */ |
| 308 | public function get_storage_model( $key ) { |
| 309 | |
| 310 | if ( isset( $this->storage_models[ $key ] ) ) { |
| 311 | return $this->storage_models[ $key ]; |
| 312 | } |
| 313 | |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @since 1.0.0 |
| 319 | * @return array Posttypes |
| 320 | */ |
| 321 | public function get_post_types() { |
| 322 | |
| 323 | $post_types = array(); |
| 324 | |
| 325 | if ( post_type_exists( 'post' ) ) |
| 326 | $post_types['post'] = 'post'; |
| 327 | |
| 328 | if ( post_type_exists( 'page' ) ) |
| 329 | $post_types['page'] = 'page'; |
| 330 | |
| 331 | $post_types = array_merge( $post_types, get_post_types( array( |
| 332 | '_builtin' => false, |
| 333 | 'show_ui' => true |
| 334 | ))); |
| 335 | |
| 336 | /** |
| 337 | * Filter the post types for which Admin Columns is active |
| 338 | * |
| 339 | * @since 2.0.0 |
| 340 | * @param array $post_types List of active post type names |
| 341 | */ |
| 342 | return apply_filters( 'cac/post_types', $post_types ); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @since 1.0.0 |
| 347 | */ |
| 348 | function add_settings_link( $links, $file ) { |
| 349 | |
| 350 | if ( $file != plugin_basename( __FILE__ ) ) { |
| 351 | return $links; |
| 352 | } |
| 353 | |
| 354 | array_unshift( $links, '<a href="' . admin_url("options-general.php") . '?page=codepress-admin-columns">' . __( 'Settings' ) . '</a>' ); |
| 355 | return $links; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @since 1.0.0 |
| 360 | */ |
| 361 | public function column_styles() { |
| 362 | |
| 363 | wp_enqueue_style( 'cpac-columns', CPAC_URL . 'assets/css/column.css', array(), CPAC_VERSION, 'all' ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Adds a body class which is used to set individual column widths |
| 368 | * |
| 369 | * @since 1.4.0 |
| 370 | * @param string $classes body classes |
| 371 | * @return string |
| 372 | */ |
| 373 | function admin_class( $classes ) { |
| 374 | |
| 375 | if ( $this->storage_models ) { |
| 376 | foreach ( $this->storage_models as $storage_model ) { |
| 377 | if ( $storage_model->is_columns_screen() ) { |
| 378 | $classes .= " cp-{$storage_model->key}"; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return $classes; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Admin CSS to hide upgrade menu and place icon |
| 388 | * |
| 389 | * @since 1.4.0 |
| 390 | */ |
| 391 | function global_head_scripts() { ?> |
| 392 | <style type="text/css"> |
| 393 | #menu-settings a[href="options-general.php?page=cpac-upgrade"] { display: none; } |
| 394 | </style> |
| 395 | <?php |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Admin CSS for Column width and Settings Icon |
| 400 | * |
| 401 | * @since 1.4.0 |
| 402 | */ |
| 403 | function admin_scripts() { |
| 404 | |
| 405 | $css_column_width = ''; |
| 406 | $edit_link = ''; |
| 407 | |
| 408 | if ( $this->storage_models ) { |
| 409 | foreach ( $this->storage_models as $storage_model ) { |
| 410 | |
| 411 | if ( ! $storage_model->is_columns_screen() ) |
| 412 | continue; |
| 413 | |
| 414 | // CSS: columns width |
| 415 | if ( $columns = $storage_model->get_stored_columns() ) { |
| 416 | foreach ( $columns as $name => $options ) { |
| 417 | |
| 418 | if ( ! empty( $options['width'] ) && is_numeric( $options['width'] ) && $options['width'] > 0 ) { |
| 419 | $css_column_width .= ".cp-{$storage_model->key} .wrap table th.column-{$name} { width: {$options['width']}% !important; }"; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // JS: edit button |
| 425 | $general_options = get_option( 'cpac_general_options' ); |
| 426 | if ( current_user_can( 'manage_admin_columns' ) && isset( $general_options['show_edit_button'] ) && '1' === $general_options['show_edit_button'] ) { |
| 427 | $edit_link = $storage_model->get_edit_link(); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | ?> |
| 432 | <?php if ( $css_column_width ) : ?> |
| 433 | <style type="text/css"> |
| 434 | <?php echo $css_column_width; ?> |
| 435 | </style> |
| 436 | <?php endif; ?> |
| 437 | <?php if ( $edit_link ) : ?> |
| 438 | <script type="text/javascript"> |
| 439 | jQuery(document).ready(function() { |
| 440 | jQuery('.tablenav.top .actions:last').append('<a href="<?php echo $edit_link; ?>" class="cpac-edit add-new-h2"><?php _e( 'Edit columns', 'cpac' ); ?></a>'); |
| 441 | }); |
| 442 | </script> |
| 443 | <?php endif; ?> |
| 444 | |
| 445 | <?php |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Get admin columns settings class instance |
| 450 | * |
| 451 | * @since 2.2 |
| 452 | * @return CPAC_Settings Settings class instance |
| 453 | */ |
| 454 | public function settings() { |
| 455 | |
| 456 | return $this->_settings; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Get admin columns add-ons class instance |
| 461 | * |
| 462 | * @since 2.2 |
| 463 | * @return CPAC_Addons Add-ons class instance |
| 464 | */ |
| 465 | public function addons() { |
| 466 | |
| 467 | return $this->_addons; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Init Class Codepress_Admin_Columns ( sets Global for backwards compatibility. ) |
| 473 | * |
| 474 | * @since 1.0.0 |
| 475 | */ |
| 476 | $GLOBALS['cpac'] = new CPAC(); |
| 477 | |
| 478 |