PluginProbe
Admin Columns / 2.2.8.1
Admin Columns v2.2.8.1
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 All 113 releases
codepress-admin-columns / codepress-admin-columns.php
codepress-admin-columns.php
538 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Admin Columns
4 Version: 2.2.8.1
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-2014 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 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
30
31 // Plugin information
32 define( 'CPAC_VERSION', '2.2.8.1' ); // Current plugin version
33 define( 'CPAC_UPGRADE_VERSION', '2.0.0' ); // 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 /**
43 * Dependencies
44 *
45 * @since 1.3.0
46 */
47 require_once CPAC_DIR . 'classes/utility.php';
48 require_once CPAC_DIR . 'classes/third_party.php';
49 require_once CPAC_DIR . 'includes/arrays.php';
50 require_once CPAC_DIR . 'api.php';
51
52 /**
53 * The Admin Columns Class
54 *
55 * @since 1.0
56 */
57 class CPAC {
58
59 /**
60 * Registered storage model class instances
61 * Array of CPAC_Storage_Model instances, with the storage model keys (e.g. post, page, wp-users) as keys
62 *
63 * @since 2.0
64 * @var array
65 */
66 public $storage_models;
67
68 /**
69 * Admin Columns add-ons class instance
70 *
71 * @since 2.2
72 * @access private
73 * @var CPAC_Addons
74 */
75 private $_addons;
76
77 /**
78 * Admin Columns settings class instance
79 *
80 * @since 2.2
81 * @access private
82 * @var CPAC_Settings
83 */
84 private $_settings;
85
86 /**
87 * Admin Columns plugin upgrade class instance
88 *
89 * @since 2.2.7
90 * @access private
91 * @var CPAC_Upgrade
92 */
93 private $_upgrade;
94
95 /**
96 * @since 1.0
97 */
98 function __construct() {
99
100 register_activation_hook( __FILE__, array( $this, 'set_capabilities' ) );
101
102 // Hooks
103 add_action( 'init', array( $this, 'localize' ) );
104 add_action( 'wp_loaded', array( $this, 'maybe_set_storage_models' ), 5 );
105 add_action( 'wp_loaded', array( $this, 'after_setup' ) ); // Setup callback, important to load after set_storage_models
106 add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
107 add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 1, 2 );
108
109 // Settings
110 include_once CPAC_DIR . 'classes/settings.php';
111 $this->_settings = new CPAC_Settings( $this );
112
113 // Addons
114 include_once CPAC_DIR . 'classes/addons.php';
115 $this->_addons = new CPAC_Addons( $this );
116
117 // Upgrade
118 require_once CPAC_DIR . 'classes/upgrade.php';
119 $this->_upgrade = new CPAC_Upgrade( $this );
120 }
121
122 /**
123 * Fire callbacks for admin columns setup completion
124 *
125 * @since 2.2
126 */
127 public function after_setup() {
128
129 /**
130 * Fires when Admin Columns is fully loaded
131 * Use this for setting up addon functionality
132 *
133 * @since 2.0
134 * @param CPAC $cpac_instance Main Admin Columns plugin class instance
135 */
136 do_action( 'cac/loaded', $this );
137 }
138
139 /**
140 * @since 2.2
141 * @uses load_plugin_textdomain()
142 */
143 public function localize() {
144
145 load_plugin_textdomain( 'cpac', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
146 }
147
148 /**
149 * @since 2.2.4
150 */
151 public function scripts() {
152
153 wp_register_script( 'cpac-admin-columns', CPAC_URL . 'assets/js/admin-columns.js', array( 'jquery', 'jquery-qtip2' ), CPAC_VERSION );
154 wp_register_script( 'jquery-qtip2', CPAC_URL . 'external/qtip2/jquery.qtip.min.js', array( 'jquery' ), CPAC_VERSION );
155 wp_register_style( 'jquery-qtip2', CPAC_URL . 'external/qtip2/jquery.qtip.min.css', array(), CPAC_VERSION, 'all' );
156 wp_register_style( 'cpac-columns', CPAC_URL . 'assets/css/column.css', array(), CPAC_VERSION, 'all' );
157
158 if ( $this->is_columns_screen() ) {
159 add_filter( 'admin_body_class', array( $this, 'admin_class' ) );
160 add_action( 'admin_head', array( $this, 'admin_scripts') );
161
162 wp_enqueue_script( 'cpac-admin-columns' );
163 wp_enqueue_script( 'jquery-qtip2' );
164 wp_enqueue_style( 'jquery-qtip2' );
165 wp_enqueue_style( 'cpac-columns' );
166
167 $data = array();
168
169 /*if ( $storage_model = $this->get_current_storage_model() ) {
170 $data['storage_model'] = array(
171 'is_table_header_fixed' => $storage_model->is_table_header_fixed()
172 );
173 }*/
174
175 wp_localize_script( 'cpac-admin-columns', 'CPAC', $data );
176 }
177 }
178
179 /**
180 * Add capabilty to administrator to manage admin columns.
181 * You can use the capability 'manage_admin_columns' to grant other roles this privilidge as well.
182 *
183 * @since 2.0.4
184 */
185 public function set_capabilities() {
186 if ( $role = get_role( 'administrator' ) ) {
187 $role->add_cap( 'manage_admin_columns' );
188 }
189 }
190
191 /**
192 * Load the storage models if the current screen is a columns screen
193 *
194 * @since 2.2.7
195 */
196 public function maybe_set_storage_models() {
197
198 if ( ! $this->is_cac_screen() ) {
199 return;
200 }
201
202 $this->set_storage_models();
203 }
204
205 /**
206 * Load the storage models, storing them in the storage_models property of this object
207 *
208 * @since 2.0
209 */
210 public function set_storage_models() {
211
212 $storage_models = array();
213
214 // Load storage model class files and column base class files
215 require_once CPAC_DIR . 'classes/column.php';
216 require_once CPAC_DIR . 'classes/column/default.php';
217 require_once CPAC_DIR . 'classes/column/actions.php';
218 require_once CPAC_DIR . 'classes/storage_model.php';
219 require_once CPAC_DIR . 'classes/storage_model/post.php';
220 require_once CPAC_DIR . 'classes/storage_model/user.php';
221 require_once CPAC_DIR . 'classes/storage_model/media.php';
222 require_once CPAC_DIR . 'classes/storage_model/comment.php';
223 require_once CPAC_DIR . 'classes/storage_model/link.php';
224
225 // Create a storage model per post type
226 foreach ( $this->get_post_types() as $post_type ) {
227 $storage_model = new CPAC_Storage_Model_Post( $post_type );
228 $storage_models[ $storage_model->key ] = $storage_model;
229 }
230
231 // Create other storage models
232 $storage_model = new CPAC_Storage_Model_User();
233 $storage_models[ $storage_model->key ] = $storage_model;
234
235 $storage_model = new CPAC_Storage_Model_Media();
236 $storage_models[ $storage_model->key ] = $storage_model;
237
238 $storage_model = new CPAC_Storage_Model_Comment();
239 $storage_models[ $storage_model->key ] = $storage_model;
240
241 if ( apply_filters( 'pre_option_link_manager_enabled', false ) ) { // as of 3.5 link manager is removed
242 $storage_model = new CPAC_Storage_Model_Link();
243 $storage_models[ $storage_model->key ] = $storage_model;
244 }
245
246 /**
247 * Filter the available storage models
248 * Used by external plugins to add additional storage models
249 *
250 * @since 2.0
251 * @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")
252 */
253 $this->storage_models = apply_filters( 'cac/storage_models', $storage_models );
254 }
255
256 /**
257 * Retrieve a storage model object based on its key
258 *
259 * @since 2.0
260 *
261 * @param string $key Storage model key (e.g. post, page, wp-users)
262 * @return bool|CPAC_Storage_Model Storage Model object (or false, on failure)
263 */
264 public function get_storage_model( $key ) {
265
266 if ( isset( $this->storage_models[ $key ] ) ) {
267 return $this->storage_models[ $key ];
268 }
269
270 return false;
271 }
272
273 /**
274 * Get storage model object of currently active storage model
275 * On the users overview page, for example, this returns the CPAC_Storage_Model_User object
276 *
277 * @since 2.2.4
278 *
279 * @return CPAC_Storage_Model
280 */
281 public function get_current_storage_model() {
282
283 if ( $this->storage_models ) {
284 foreach ( $this->storage_models as $storage_model ) {
285 if ( $storage_model->is_columns_screen() ) {
286 return $storage_model;
287 }
288 }
289 }
290 }
291
292 /**
293 * Get a list of post types for which Admin Columns is active
294 *
295 * @since 1.0
296 *
297 * @return array List of post type keys (e.g. post, page)
298 */
299 public function get_post_types() {
300
301 $post_types = array();
302
303 if ( post_type_exists( 'post' ) ) {
304 $post_types['post'] = 'post';
305 }
306
307 if ( post_type_exists( 'page' ) ) {
308 $post_types['page'] = 'page';
309 }
310
311 $post_types = array_merge( $post_types, get_post_types( array(
312 '_builtin' => false,
313 'show_ui' => true
314 ) ) );
315
316 /**
317 * Filter the post types for which Admin Columns is active
318 *
319 * @since 2.0
320 * @param array $post_types List of active post type names
321 */
322 return apply_filters( 'cac/post_types', $post_types );
323 }
324
325 /**
326 * Add a settings link to the Admin Columns entry in the plugin overview screen
327 *
328 * @since 1.0
329 * @see filter:plugin_action_links
330 */
331 function add_settings_link( $links, $file ) {
332
333 if ( $file != plugin_basename( __FILE__ ) ) {
334 return $links;
335 }
336
337 array_unshift( $links, '<a href="' . esc_url( admin_url( "options-general.php?page=codepress-admin-columns" ) ) . '">' . __( 'Settings' ) . '</a>' );
338
339 return $links;
340 }
341
342 /**
343 * Adds a body class which is used to set individual column widths
344 *
345 * @since 1.4.0
346 *
347 * @param string $classes body classes
348 * @return string
349 */
350 function admin_class( $classes ) {
351
352 if ( $storage_model = $this->get_current_storage_model() ) {
353 $classes .= " cp-{$storage_model->key}";
354 }
355
356 return $classes;
357 }
358
359 /**
360 * Admin CSS for Column width and Settings Icon
361 *
362 * @since 1.4.0
363 */
364 function admin_scripts() {
365
366 $css_column_width = '';
367 $edit_link = '';
368
369 if ( $this->storage_models ) {
370 foreach ( $this->storage_models as $storage_model ) {
371
372 if ( ! $storage_model->is_columns_screen() )
373 continue;
374
375 // CSS: columns width
376 if ( $columns = $storage_model->get_stored_columns() ) {
377 foreach ( $columns as $name => $options ) {
378
379 if ( ! empty( $options['width'] ) && is_numeric( $options['width'] ) && $options['width'] > 0 ) {
380 $css_column_width .= ".cp-{$storage_model->key} .wrap table th.column-{$name} { width: {$options['width']}% !important; }";
381 }
382 }
383 }
384
385 // JS: edit button
386 $general_options = get_option( 'cpac_general_options' );
387 if ( current_user_can( 'manage_admin_columns' ) && ( ! isset( $general_options['show_edit_button'] ) || '1' === $general_options['show_edit_button'] ) ) {
388 $edit_link = $storage_model->get_edit_link();
389 }
390 }
391 }
392 ?>
393 <?php if ( $css_column_width ) : ?>
394 <style type="text/css">
395 <?php echo $css_column_width; ?>
396 </style>
397 <?php endif; ?>
398 <?php if ( $edit_link ) : ?>
399 <script type="text/javascript">
400 jQuery(document).ready(function() {
401 jQuery('.tablenav.top .actions:last').append('<a href="<?php echo $edit_link; ?>" class="cpac-edit add-new-h2"><?php _e( 'Edit columns', 'cpac' ); ?></a>');
402 });
403 </script>
404 <?php endif; ?>
405
406 <?php
407 }
408
409 /**
410 * Whether this request is an AJAX request and marked as admin-column-ajax or inline-save request.
411 *
412 * @since 2.2
413 * @return bool Returns true if in an AJAX request, false otherwise
414 */
415 function is_doing_ajax() {
416
417 if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
418 return false;
419 }
420
421 if ( ( isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) ) {
422 return true;
423 }
424
425 if ( ( isset( $_POST['plugin_id'] ) && 'cpac' == $_POST['plugin_id'] ) || ( isset( $_GET['plugin_id'] ) && 'cpac' == $_GET['plugin_id'] ) ) {
426 return true;
427 }
428
429 return false;
430 }
431
432 /**
433 * Whether this request is a columns screen (i.e. a content overview page)
434 *
435 * @since 2.2
436 * @return bool Returns true if the current screen is a columns screen, false otherwise
437 */
438 function is_columns_screen() {
439
440 global $pagenow;
441
442 $columns_screen = in_array( $pagenow, array( 'edit.php', 'upload.php', 'link-manager.php', 'edit-comments.php', 'users.php', 'edit-tags.php' ) );
443
444 /**
445 * Filter whether the current screen is a columns screen (i.e. a content overview page)
446 * Useful for advanced used with custom content overview pages
447 *
448 * @since 2.2
449 * @param bool $columns_screen Whether the current request is a columns screen
450 */
451 $columns_screen = apply_filters( 'cac/is_columns_screen', $columns_screen );
452
453 return $columns_screen;
454 }
455
456 /**
457 * Whether the current screen is the Admin Columns settings screen
458 *
459 * @since 2.2
460 * @return bool True if the current screen is the settings screen, false otherwise
461 */
462 function is_settings_screen() {
463
464 global $pagenow;
465
466 if ( ! ( 'options-general.php' === $pagenow && isset( $_GET['page'] ) && ( 'codepress-admin-columns' === $_GET['page'] ) ) ) {
467 return false;
468 }
469
470 return true;
471 }
472
473 /**
474 * Whether the current screen is a screen in which Admin Columns is used
475 * Used to check whether storage models should be loaded
476 *
477 * @since 2.2
478 * @return bool Whether the current screen is an Admin Columns screen
479 */
480 function is_cac_screen() {
481
482 /**
483 * Filter whether the current screen is a screen in which Admin Columns is active
484 *
485 * @since 2.2
486 * @param bool $is_cac_screen Whether the current screen is an Admin Columns screen
487 */
488 return apply_filters( 'cac/is_cac_screen', $this->is_columns_screen() || $this->is_doing_ajax() || $this->is_settings_screen() );
489 }
490
491 /**
492 * Get admin columns settings class instance
493 *
494 * @since 2.2
495 * @return CPAC_Settings Settings class instance
496 */
497 public function settings() {
498
499 return $this->_settings;
500 }
501
502 /**
503 * Get admin columns add-ons class instance
504 *
505 * @since 2.2
506 * @return CPAC_Addons Add-ons class instance
507 */
508 public function addons() {
509
510 return $this->_addons;
511 }
512
513 /**
514 * Get admin columns upgrade class instance
515 *
516 * @since 2.2.7
517 * @return CPAC_Upgrade Upgrade class instance
518 */
519 public function upgrade() {
520
521 return $this->_upgrade;
522 }
523 }
524
525 /**
526 * Admin Columns class (global for backwards compatibility)
527 *
528 * @since 1.0
529 * @deprecated 2.2.7 Use filter cac/loaded instead.
530 */
531 global $cpac;
532
533 /**
534 * Initialize Admin Columns class
535 *
536 * @since 1.0
537 */
538 $cpac = new CPAC();