PluginProbe
Admin Columns / 2.4.3
Admin Columns v2.4.3
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
581 lines 15.8 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.4.3
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.3' ); // 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 // Settings
126 include_once CPAC_DIR . 'classes/review_notice.php';
127 new CPAC_Review_Notice( $this );
128 }
129
130 /**
131 * Fire callbacks for admin columns setup completion
132 *
133 * @since 2.2
134 */
135 public function after_setup() {
136
137 /**
138 * Fires when Admin Columns is fully loaded
139 * Use this for setting up addon functionality
140 *
141 * @since 2.0
142 * @param CPAC $cpac_instance Main Admin Columns plugin class instance
143 */
144 do_action( 'cac/loaded', $this );
145 }
146
147 /**
148 * @since 2.2
149 * @uses load_plugin_textdomain()
150 */
151 public function localize() {
152
153 load_plugin_textdomain( 'cpac', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
154 }
155
156 /**
157 * @since 2.2.4
158 */
159 public function scripts() {
160
161 $minified = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
162
163 wp_register_script( 'cpac-admin-columns', CPAC_URL . "assets/js/admin-columns{$minified}.js", array( 'jquery', 'jquery-qtip2' ), CPAC_VERSION );
164 wp_register_script( 'jquery-qtip2', CPAC_URL . "external/qtip2/jquery.qtip{$minified}.js", array( 'jquery' ), CPAC_VERSION );
165 wp_register_style( 'jquery-qtip2', CPAC_URL . "external/qtip2/jquery.qtip{$minified}.css", array(), CPAC_VERSION, 'all' );
166 wp_register_style( 'cpac-columns', CPAC_URL . "assets/css/column{$minified}.css", array(), CPAC_VERSION, 'all' );
167
168 if ( $this->is_columns_screen() ) {
169 add_filter( 'admin_body_class', array( $this, 'admin_class' ) );
170 add_action( 'admin_head', array( $this, 'admin_scripts') );
171
172 wp_enqueue_script( 'cpac-admin-columns' );
173 wp_enqueue_style( 'jquery-qtip2' );
174 wp_enqueue_style( 'cpac-columns' );
175 }
176 }
177
178 /**
179 * Add capabilty to administrator to manage admin columns.
180 * You can use the capability 'manage_admin_columns' to grant other roles this privilidge as well.
181 *
182 * @since 2.0.4
183 */
184 public function set_capabilities() {
185 if ( $role = get_role( 'administrator' ) ) {
186 $role->add_cap( 'manage_admin_columns' );
187 }
188 }
189
190 /**
191 * Load the storage models if the current screen is a columns screen
192 *
193 * @since 2.2.7
194 */
195 public function maybe_set_storage_models() {
196
197 if ( ! $this->is_cac_screen() ) {
198 return;
199 }
200
201 $this->set_storage_models();
202 }
203
204 /**
205 * Load the php exported settings
206 *
207 * @since 2.3.5
208 */
209 public function maybe_load_php_export() {
210 global $_cac_exported_columns;
211 if ( $_cac_exported_columns ) {
212 foreach( $_cac_exported_columns as $model => $columns ) {
213 if ( $storage_model = $this->get_storage_model( $model ) ) {
214 $storage_model->set_stored_columns( $columns );
215 }
216 }
217 }
218 }
219
220 /**
221 * Load the storage models, storing them in the storage_models property of this object
222 *
223 * @since 2.0
224 */
225 public function set_storage_models() {
226
227 $storage_models = array();
228
229 // Load storage model class files and column base class files
230 require_once CPAC_DIR . 'classes/column.php';
231 require_once CPAC_DIR . 'classes/column/default.php';
232 require_once CPAC_DIR . 'classes/column/actions.php';
233 require_once CPAC_DIR . 'classes/storage_model.php';
234 require_once CPAC_DIR . 'classes/storage_model/post.php';
235 require_once CPAC_DIR . 'classes/storage_model/user.php';
236 require_once CPAC_DIR . 'classes/storage_model/media.php';
237 require_once CPAC_DIR . 'classes/storage_model/comment.php';
238 require_once CPAC_DIR . 'classes/storage_model/link.php';
239
240 // Create a storage model per post type
241 foreach ( $this->get_post_types() as $post_type ) {
242 $storage_model = new CPAC_Storage_Model_Post( $post_type );
243 $storage_models[ $storage_model->key ] = $storage_model;
244 }
245
246 // Create other storage models
247 $storage_model = new CPAC_Storage_Model_User();
248 $storage_models[ $storage_model->key ] = $storage_model;
249
250 $storage_model = new CPAC_Storage_Model_Media();
251 $storage_models[ $storage_model->key ] = $storage_model;
252
253 $storage_model = new CPAC_Storage_Model_Comment();
254 $storage_models[ $storage_model->key ] = $storage_model;
255
256 if ( apply_filters( 'pre_option_link_manager_enabled', false ) ) { // as of 3.5 link manager is removed
257 $storage_model = new CPAC_Storage_Model_Link();
258 $storage_models[ $storage_model->key ] = $storage_model;
259 }
260
261 /**
262 * Filter the available storage models
263 * Used by external plugins to add additional storage models
264 *
265 * @since 2.0
266 * @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")
267 * @param object $this CPAC
268 */
269 $this->storage_models = apply_filters( 'cac/storage_models', $storage_models, $this );
270 }
271
272 /**
273 * Retrieve a storage model object based on its key
274 *
275 * @since 2.0
276 *
277 * @param string $key Storage model key (e.g. post, page, wp-users)
278 * @return bool|CPAC_Storage_Model Storage Model object (or false, on failure)
279 */
280 public function get_storage_model( $key ) {
281
282 if ( isset( $this->storage_models[ $key ] ) ) {
283 return $this->storage_models[ $key ];
284 }
285
286 return false;
287 }
288
289 /**
290 * Get storage model object of currently active storage model
291 * On the users overview page, for example, this returns the CPAC_Storage_Model_User object
292 *
293 * @since 2.2.4
294 *
295 * @return CPAC_Storage_Model
296 */
297 public function get_current_storage_model() {
298
299 if ( $this->storage_models ) {
300 foreach ( $this->storage_models as $storage_model ) {
301 if ( $storage_model->is_columns_screen() ) {
302 return $storage_model;
303 }
304 }
305 }
306 }
307
308 /**
309 * Get a list of post types for which Admin Columns is active
310 *
311 * @since 1.0
312 *
313 * @return array List of post type keys (e.g. post, page)
314 */
315 public function get_post_types() {
316
317 $post_types = array();
318
319 if ( post_type_exists( 'post' ) ) {
320 $post_types['post'] = 'post';
321 }
322
323 if ( post_type_exists( 'page' ) ) {
324 $post_types['page'] = 'page';
325 }
326
327 $post_types = array_merge( $post_types, get_post_types( array(
328 '_builtin' => false,
329 'show_ui' => true
330 ) ) );
331
332 /**
333 * Filter the post types for which Admin Columns is active
334 *
335 * @since 2.0
336 * @param array $post_types List of active post type names
337 */
338 return apply_filters( 'cac/post_types', $post_types );
339 }
340
341 /**
342 * Get a list of taxonomies supported by Admin Columns
343 *
344 * @since 1.0
345 *
346 * @return array List of taxonomies
347 */
348 public function get_taxonomies() {
349
350 $taxonomies = get_taxonomies( array( 'public' => true ) );
351
352 if ( isset( $taxonomies['post_format'] ) ) {
353 unset( $taxonomies['post_format'] );
354 }
355
356 /**
357 * Filter the post types for which Admin Columns is active
358 *
359 * @since 2.0
360 * @param array $post_types List of active post type names
361 */
362 return apply_filters( 'cac/taxonomies', $taxonomies );
363 }
364
365 /**
366 * Add a settings link to the Admin Columns entry in the plugin overview screen
367 *
368 * @since 1.0
369 * @see filter:plugin_action_links
370 */
371 public function add_settings_link( $links, $file ) {
372
373 if ( $file != plugin_basename( __FILE__ ) ) {
374 return $links;
375 }
376
377 array_unshift( $links, '<a href="' . esc_url( admin_url( "options-general.php?page=codepress-admin-columns" ) ) . '">' . __( 'Settings' ) . '</a>' );
378
379 return $links;
380 }
381
382 /**
383 * Adds a body class which is used to set individual column widths
384 *
385 * @since 1.4.0
386 *
387 * @param string $classes body classes
388 * @return string
389 */
390 public function admin_class( $classes ) {
391
392 if ( $storage_model = $this->get_current_storage_model() ) {
393 $classes .= " cp-{$storage_model->key}";
394 }
395
396 return $classes;
397 }
398
399 /**
400 * Admin CSS for Column width and Settings Icon
401 *
402 * @since 1.4.0
403 */
404 public function admin_scripts() {
405
406 if ( ! ( $storage_model = $this->get_current_storage_model() ) ) {
407 return;
408 }
409
410 $css_column_width = '';
411 $edit_link = '';
412
413 // CSS: columns width
414 if ( $columns = $storage_model->get_stored_columns() ) {
415 foreach ( $columns as $name => $options ) {
416
417 if ( ! empty( $options['width'] ) && is_numeric( $options['width'] ) && $options['width'] > 0 ) {
418 $unit = isset( $options['width_unit'] ) ? $options['width_unit'] : '%';
419 $css_column_width .= ".cp-{$storage_model->key} .wrap table th.column-{$name} { width: {$options['width']}{$unit} !important; }";
420 }
421
422 // Load custom column scripts, used by 3rd party columns
423 if ( $column = $storage_model->get_column_by_name( $name ) ) {
424 $column->scripts();
425 }
426 }
427 }
428
429 // JS: edit button
430 $general_options = get_option( 'cpac_general_options' );
431 if ( current_user_can( 'manage_admin_columns' ) && ( ! isset( $general_options['show_edit_button'] ) || '1' === $general_options['show_edit_button'] ) ) {
432 $edit_link = $storage_model->get_edit_link();
433 }
434
435 ?>
436 <?php if ( $css_column_width ) : ?>
437 <style type="text/css">
438 <?php echo $css_column_width; ?>
439 </style>
440 <?php endif; ?>
441 <?php if ( $edit_link ) : ?>
442 <script type="text/javascript">
443 jQuery(document).ready(function() {
444 jQuery('.tablenav.top .actions:last').append('<a href="<?php echo $edit_link; ?>" class="cpac-edit add-new-h2"><?php _e( 'Edit columns', 'cpac' ); ?></a>');
445 });
446 </script>
447 <?php endif; ?>
448
449 <?php
450
451 /**
452 * Add header scripts that only apply to column screens.
453 * @since 2.3.5
454 * @param object CPAC Main Class
455 */
456 do_action( 'cac/admin_head', $storage_model, $this );
457 }
458
459 /**
460 * Whether this request is an AJAX request and marked as admin-column-ajax or inline-save request.
461 *
462 * @since 2.2
463 * @return bool Returns true if in an AJAX request, false otherwise
464 */
465 public function is_doing_ajax() {
466
467 return cac_is_doing_ajax();
468 }
469
470 /**
471 * Whether this request is a columns screen (i.e. a content overview page)
472 *
473 * @since 2.2
474 * @return bool Returns true if the current screen is a columns screen, false otherwise
475 */
476 public function is_columns_screen() {
477
478 global $pagenow;
479
480 $columns_screen = in_array( $pagenow, array( 'edit.php', 'upload.php', 'link-manager.php', 'edit-comments.php', 'users.php', 'edit-tags.php' ) );
481
482 /**
483 * Filter whether the current screen is a columns screen (i.e. a content overview page)
484 * Useful for advanced used with custom content overview pages
485 *
486 * @since 2.2
487 * @param bool $columns_screen Whether the current request is a columns screen
488 */
489 $columns_screen = apply_filters( 'cac/is_columns_screen', $columns_screen );
490
491 return $columns_screen;
492 }
493
494 /**
495 * Whether the current screen is the Admin Columns settings screen
496 *
497 * @since 2.2
498 * @param strong $tab Specifies a tab screen (optional)
499 * @return bool True if the current screen is the settings screen, false otherwise
500 */
501 public function is_settings_screen( $tab = '' ) {
502
503 global $pagenow;
504
505 if ( ! ( 'options-general.php' === $pagenow && isset( $_GET['page'] ) && ( 'codepress-admin-columns' === $_GET['page'] ) ) ) {
506 return false;
507 }
508
509 if ( $tab && ( empty( $_GET['tab'] ) || ( isset( $_GET['tab'] ) && $tab !== $_GET['tab'] ) ) ) {
510 return false;
511 }
512
513 return true;
514 }
515
516 /**
517 * Whether the current screen is a screen in which Admin Columns is used
518 * Used to check whether storage models should be loaded
519 *
520 * @since 2.2
521 * @return bool Whether the current screen is an Admin Columns screen
522 */
523 public function is_cac_screen() {
524
525 /**
526 * Filter whether the current screen is a screen in which Admin Columns is active
527 *
528 * @since 2.2
529 * @param bool $is_cac_screen Whether the current screen is an Admin Columns screen
530 */
531 return apply_filters( 'cac/is_cac_screen', $this->is_columns_screen() || $this->is_doing_ajax() || $this->is_settings_screen() );
532 }
533
534 /**
535 * Get admin columns settings class instance
536 *
537 * @since 2.2
538 * @return CPAC_Settings Settings class instance
539 */
540 public function settings() {
541
542 return $this->_settings;
543 }
544
545 /**
546 * Get admin columns add-ons class instance
547 *
548 * @since 2.2
549 * @return CPAC_Addons Add-ons class instance
550 */
551 public function addons() {
552
553 return $this->_addons;
554 }
555
556 /**
557 * Get admin columns upgrade class instance
558 *
559 * @since 2.2.7
560 * @return CPAC_Upgrade Upgrade class instance
561 */
562 public function upgrade() {
563
564 return $this->_upgrade;
565 }
566 }
567
568 /**
569 * Admin Columns class (global for backwards compatibility)
570 *
571 * @since 1.0
572 * @deprecated 2.2.7 Use filter cac/loaded instead.
573 */
574 global $cpac;
575
576 /**
577 * Initialize Admin Columns class
578 *
579 * @since 1.0
580 */
581 $cpac = new CPAC();