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