PluginProbe
Admin Columns / 2.5.6.3
Admin Columns v2.5.6.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 in Admin Columns 2.5.6.3, at codepress-admin-columns.php

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