codepress-admin-columns
Last commit date
assets
12 years ago
classes
12 years ago
languages
12 years ago
codepress-admin-columns.php
12 years ago
readme.txt
12 years ago
screenshot-1.png
12 years ago
screenshot-2.png
12 years ago
screenshot-3.png
12 years ago
screenshot-4.png
12 years ago
screenshot-5.png
12 years ago
screenshot-6.png
12 years ago
screenshot-7.png
12 years ago
screenshot-8.png
12 years ago
codepress-admin-columns.php
361 lines
| 1 | <?php |
| 2 | /* |
| 3 | |
| 4 | Plugin Name: Codepress Admin Columns |
| 5 | Version: 2.0.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-2013 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' ) ) exit; // Exit if accessed directly |
| 31 | |
| 32 | define( 'CPAC_VERSION', '2.0.1' ); // current plugin version |
| 33 | define( 'CPAC_UPGRADE_VERSION', '2.0.0' ); // this is the latest version which requires an upgrade |
| 34 | define( 'CPAC_URL', plugin_dir_url( __FILE__ ) ); |
| 35 | define( 'CPAC_DIR', plugin_dir_path( __FILE__ ) ); |
| 36 | |
| 37 | // only run plugin in the admin interface |
| 38 | if ( ! is_admin() ) |
| 39 | return false; |
| 40 | |
| 41 | /** |
| 42 | * Dependencies |
| 43 | * |
| 44 | * @since 1.3.0 |
| 45 | */ |
| 46 | require_once CPAC_DIR . 'classes/utility.php'; |
| 47 | require_once CPAC_DIR . 'classes/deprecated.php'; |
| 48 | require_once CPAC_DIR . 'classes/third_party.php'; |
| 49 | require_once CPAC_DIR . 'classes/api.php'; |
| 50 | |
| 51 | /** |
| 52 | * The Codepress Admin Columns Class |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | * |
| 56 | */ |
| 57 | class CPAC { |
| 58 | |
| 59 | public $storage_models; |
| 60 | |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | function __construct() { |
| 67 | add_action( 'wp_loaded', array( $this, 'init') ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Initialize plugin. |
| 72 | * |
| 73 | * Loading sequence is determined and intialized. |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function init() { |
| 78 | |
| 79 | // translations |
| 80 | load_plugin_textdomain( 'cpac', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 81 | |
| 82 | // styling & scripts |
| 83 | add_action( 'admin_enqueue_scripts' , array( $this, 'column_styles') ); |
| 84 | add_filter( 'admin_body_class', array( $this, 'admin_class' ) ); |
| 85 | add_action( 'admin_head', array( $this, 'admin_scripts') ); |
| 86 | |
| 87 | // add settings link |
| 88 | add_filter( 'plugin_action_links', array( $this, 'add_settings_link'), 1, 2); |
| 89 | |
| 90 | // add capabilty to administrator to manage admin columns |
| 91 | // note to devs: you can use this to grant other roles this privilidge as well. |
| 92 | $role = get_role( 'administrator' ); |
| 93 | $role->add_cap( 'manage_admin_columns' ); |
| 94 | |
| 95 | // set storage models |
| 96 | $this->set_storage_models(); |
| 97 | |
| 98 | // ini controllers |
| 99 | $this->init_controllers(); |
| 100 | |
| 101 | // for third party plugins |
| 102 | do_action( 'cac/loaded', $this ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get storage models |
| 107 | * |
| 108 | * @since 2.0.0 |
| 109 | * |
| 110 | */ |
| 111 | private function set_storage_models() { |
| 112 | |
| 113 | $storage_models = array(); |
| 114 | |
| 115 | // include parent and childs |
| 116 | require_once CPAC_DIR . 'classes/column.php'; |
| 117 | require_once CPAC_DIR . 'classes/storage_model.php'; |
| 118 | require_once CPAC_DIR . 'classes/storage_model/post.php'; |
| 119 | require_once CPAC_DIR . 'classes/storage_model/user.php'; |
| 120 | require_once CPAC_DIR . 'classes/storage_model/media.php'; |
| 121 | require_once CPAC_DIR . 'classes/storage_model/comment.php'; |
| 122 | require_once CPAC_DIR . 'classes/storage_model/link.php'; |
| 123 | |
| 124 | // add Posts |
| 125 | foreach ( $this->get_post_types() as $post_type ) { |
| 126 | $storage_model = new CPAC_Storage_Model_Post( $post_type ); |
| 127 | $storage_models[ $storage_model->key ] = $storage_model; |
| 128 | } |
| 129 | |
| 130 | // add User |
| 131 | $storage_model = new CPAC_Storage_Model_User(); |
| 132 | $storage_models[ $storage_model->key ] = $storage_model; |
| 133 | |
| 134 | // add Media |
| 135 | $storage_model = new CPAC_Storage_Model_Media(); |
| 136 | $storage_models[ $storage_model->key ] = $storage_model; |
| 137 | |
| 138 | // add Comment |
| 139 | $storage_model = new CPAC_Storage_Model_Comment(); |
| 140 | $storage_models[ $storage_model->key ] = $storage_model; |
| 141 | |
| 142 | // add Link |
| 143 | if ( apply_filters( 'pre_option_link_manager_enabled', false ) ) { // as of 3.5 link manager is removed |
| 144 | $storage_model = new CPAC_Storage_Model_Link(); |
| 145 | $storage_models[ $storage_model->key ] = $storage_model; |
| 146 | } |
| 147 | |
| 148 | // Hook to add more models |
| 149 | do_action( 'cac/storage_models', $storage_models ); |
| 150 | |
| 151 | $this->storage_models = $storage_models; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get storage model |
| 156 | * |
| 157 | * @since 2.0.0 |
| 158 | * |
| 159 | * @return array object Storage Model |
| 160 | */ |
| 161 | public function get_storage_model( $key ) { |
| 162 | |
| 163 | if ( ! isset( $this->storage_models[ $key ] ) ) |
| 164 | return false; |
| 165 | |
| 166 | return $this->storage_models[ $key ]; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Init controllers |
| 171 | * |
| 172 | * @since 2.0.0 |
| 173 | * |
| 174 | */ |
| 175 | function init_controllers() { |
| 176 | |
| 177 | do_action( 'cac/controllers', $this ); |
| 178 | |
| 179 | // Settings |
| 180 | include_once CPAC_DIR . 'classes/settings.php'; |
| 181 | new CPAC_Settings( $this ); |
| 182 | |
| 183 | // Upgrade |
| 184 | require_once CPAC_DIR . 'classes/upgrade.php'; |
| 185 | new CPAC_Upgrade( $this ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get post types - Utility Method |
| 190 | * |
| 191 | * @since 1.0.0 |
| 192 | * |
| 193 | * @return array Posttypes |
| 194 | */ |
| 195 | public function get_post_types() { |
| 196 | |
| 197 | $post_types = array(); |
| 198 | |
| 199 | if ( post_type_exists( 'post' ) ) |
| 200 | $post_types['post'] = 'post'; |
| 201 | |
| 202 | if ( post_type_exists( 'page' ) ) |
| 203 | $post_types['page'] = 'page'; |
| 204 | |
| 205 | $post_types = array_merge( $post_types, get_post_types( array( |
| 206 | '_builtin' => false, |
| 207 | 'show_ui' => true |
| 208 | ))); |
| 209 | |
| 210 | return apply_filters( 'cac/post_types', $post_types ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Add Settings link to plugin page |
| 215 | * |
| 216 | * @since 1.0.0 |
| 217 | * |
| 218 | * @param string $links All settings links. |
| 219 | * @param string $file Plugin filename. |
| 220 | * @return string Link to settings page |
| 221 | */ |
| 222 | function add_settings_link( $links, $file ) { |
| 223 | |
| 224 | if ( $file != plugin_basename( __FILE__ ) ) |
| 225 | return $links; |
| 226 | |
| 227 | array_unshift( $links, '<a href="' . admin_url("options-general.php") . '?page=codepress-admin-columns">' . __( 'Settings' ) . '</a>' ); |
| 228 | return $links; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Register column css |
| 233 | * |
| 234 | * @since 1.0.0 |
| 235 | */ |
| 236 | public function column_styles() { |
| 237 | |
| 238 | global $pagenow; |
| 239 | |
| 240 | if ( in_array( $pagenow, array( 'edit.php', 'upload.php', 'link-manager.php', 'edit-comments.php', 'users.php' ) ) ) { |
| 241 | wp_enqueue_style( 'cpac-columns', CPAC_URL . 'assets/css/column.css', array(), CPAC_VERSION, 'all' ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Admin body class |
| 247 | * |
| 248 | * Adds a body class which is used to set individual column widths |
| 249 | * |
| 250 | * @since 1.4.0 |
| 251 | * |
| 252 | * @param string $classes body classes |
| 253 | * @return string |
| 254 | */ |
| 255 | function admin_class( $classes ) { |
| 256 | |
| 257 | global $current_screen; |
| 258 | |
| 259 | // we dont need the 'edit-' part |
| 260 | $screen = str_replace( 'edit-', '', $current_screen->id ); |
| 261 | |
| 262 | // media library exception |
| 263 | if ( $current_screen->base == 'upload' && $current_screen->id == 'upload' ) { |
| 264 | $screen = 'media'; |
| 265 | } |
| 266 | |
| 267 | // link exception |
| 268 | if ( $current_screen->base == 'link-manager' && $current_screen->id == 'link-manager' ) { |
| 269 | $screen = 'links'; |
| 270 | } |
| 271 | |
| 272 | // loop the available types |
| 273 | foreach ( $this->storage_models as $storage_model ) { |
| 274 | |
| 275 | // match against screen or wp-screen |
| 276 | if ( $storage_model->key == $screen || $storage_model->key == "wp-{$screen}" ) |
| 277 | $classes .= " cp-{$storage_model->key}"; |
| 278 | } |
| 279 | |
| 280 | return $classes; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /** |
| 285 | * Admin CSS for Column width and Settings Icon |
| 286 | * |
| 287 | * @since 1.4.0 |
| 288 | */ |
| 289 | function admin_scripts() { |
| 290 | |
| 291 | global $pagenow, $current_screen; |
| 292 | |
| 293 | // CSS column widths |
| 294 | $css_column_width = ''; |
| 295 | |
| 296 | // JS |
| 297 | $edit_link = ''; |
| 298 | |
| 299 | foreach ( $this->storage_models as $storage_model ) { |
| 300 | |
| 301 | if ( $storage_model->page . '.php' !== $pagenow ) |
| 302 | continue; |
| 303 | |
| 304 | // CSS: columns width |
| 305 | if ( $columns = $storage_model->get_stored_columns() ) { |
| 306 | foreach ( $columns as $name => $options ) { |
| 307 | |
| 308 | if ( ! empty( $options['width'] ) && is_numeric( $options['width'] ) && $options['width'] > 0 ) { |
| 309 | $css_column_width .= ".cp-{$storage_model->key} .wrap table th.column-{$name} { width: {$options['width']}% !important; }"; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // JS: edit button |
| 315 | if ( |
| 316 | // All types except Posts |
| 317 | empty( $current_screen->post_type ) || |
| 318 | // Posts |
| 319 | ( ! empty( $current_screen->post_type ) && $storage_model->key == $current_screen->post_type ) |
| 320 | ) |
| 321 | { |
| 322 | $edit_link = $storage_model->get_edit_link(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | ?> |
| 327 | <style type="text/css"> |
| 328 | <?php echo $css_column_width; ?> |
| 329 | #adminmenu #toplevel_page_codepress-admin-columns .wp-menu-image { |
| 330 | background: transparent url("<?php echo CPAC_URL; ?>assets/images/icon_20.png") no-repeat 6px -24px; |
| 331 | } |
| 332 | #adminmenu #toplevel_page_codepress-admin-columns:hover .wp-menu-image, |
| 333 | #adminmenu #toplevel_page_codepress-admin-columns.wp-menu-open .wp-menu-image { |
| 334 | background-position: 6px 6px; |
| 335 | } |
| 336 | #menu-settings a[href="options-general.php?page=cpac-upgrade"], |
| 337 | #adminmenu #toplevel_page_codepress-admin-columns a[href="admin.php?page=cpac-upgrade"] { |
| 338 | display: none; |
| 339 | } |
| 340 | .cpac-edit { margin-right: 3px; vertical-align: middle; } |
| 341 | </style> |
| 342 | |
| 343 | <?php if ( current_user_can( 'manage_admin_columns' ) && $edit_link ) : ?> |
| 344 | <script type="text/javascript"> |
| 345 | jQuery(document).ready(function() { |
| 346 | jQuery('.tablenav.top .actions:last').append('<a href="<?php echo $edit_link; ?>" class="cpac-edit add-new-h2"><?php _e( 'Edit columns', 'cpac' ); ?></a>'); |
| 347 | }); |
| 348 | </script> |
| 349 | <?php |
| 350 | endif; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Init Class Codepress_Admin_Columns |
| 356 | * |
| 357 | * @since 1.0.0 |
| 358 | */ |
| 359 | $cpac = new CPAC(); |
| 360 | |
| 361 |