class-foo-friendly-dates.php
2 days ago
class-foo-plugin-base.php
2 days ago
class-foo-plugin-file-locator.php
2 days ago
class-foo-plugin-metabox-sanity.php
2 days ago
class-foo-plugin-options.php
2 days ago
class-foo-plugin-settings.php
2 days ago
class-foo-plugin-textdomain.php
2 days ago
index.php
2 days ago
class-foo-plugin-metabox-sanity.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Foo Metabox Sanity allows plugin authors to whitelist metaboxes for your custom post types |
| 9 | * Original code and idea by Thomas Griffin - https://github.com/thomasgriffin/metabox-sanity |
| 10 | * |
| 11 | * @package Foo_Plugin_Base |
| 12 | * @version 1.0.0 |
| 13 | * @author Brad Vincent |
| 14 | * @copyright Copyright (c) 2014, Brad Vincent |
| 15 | * @license http://opensource.org/licenses/gpl-2.0.php GPL v2 or later |
| 16 | * @link https://github.com/fooplugins/Foo_Plugin_Base |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Copyright 2014 Brad Vincent (fooplugins.com) |
| 21 | |
| 22 | This program is free software; you can redistribute it and/or modify |
| 23 | it under the terms of the GNU General Public License, version 2, as |
| 24 | published by the Free Software Foundation. |
| 25 | |
| 26 | This program is distributed in the hope that it will be useful, |
| 27 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | GNU General Public License for more details. |
| 30 | |
| 31 | You should have received a copy of the GNU General Public License |
| 32 | along with this program; if not, write to the Free Software |
| 33 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 34 | */ |
| 35 | |
| 36 | if ( !class_exists( 'Foo_Plugin_Metabox_Sanity_v1' ) ) { |
| 37 | /** |
| 38 | * Provides an intuitive way for plugins to whitelist |
| 39 | * metaboxes on their custom post type interfaces. |
| 40 | * |
| 41 | * @since 2.1.0 |
| 42 | * |
| 43 | * @package Foo_Plugin_Base |
| 44 | * @author Brad Vincent |
| 45 | */ |
| 46 | class Foo_Plugin_Metabox_Sanity_v1 { |
| 47 | |
| 48 | protected $plugin_slug; |
| 49 | |
| 50 | public function __construct($plugin_slug) { |
| 51 | $this->plugin_slug = $plugin_slug; |
| 52 | |
| 53 | add_action( 'add_meta_boxes', array($this, 'maintain_sanity'), 999 ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * TODO |
| 58 | */ |
| 59 | function maintain_sanity() { |
| 60 | |
| 61 | $metabox_sanity_config = apply_filters( $this->plugin_slug . '_metabox_sanity', false ); |
| 62 | |
| 63 | // If there is no data, return. |
| 64 | if ( empty($metabox_sanity_config) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Bring the global metaboxes array into scope. |
| 69 | global $wp_meta_boxes; |
| 70 | |
| 71 | // Loop through each post type and start whitelisting metaboxes! |
| 72 | foreach ( $metabox_sanity_config as $post_type => $data ) { |
| 73 | // If no contexts or priorities have been specified, do nothing. |
| 74 | if ( empty($data['contexts']) || empty($data['priorities']) ) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | // Now loop through each context that has been assigned to the post type. |
| 79 | foreach ( (array) $data['contexts'] as $context ) { |
| 80 | // Now loop through each priority within the context. |
| 81 | foreach ( (array) $data['priorities'] as $priority ) { |
| 82 | if ( isset($wp_meta_boxes[$post_type][$context][$priority]) ) { |
| 83 | // Loop through each priority and check the data to determine whether the metabox will stay or go. |
| 84 | foreach ( (array) $wp_meta_boxes[$post_type][$context][$priority] as $id => $metabox_data ) { |
| 85 | // If the metabox ID matches one to pass over, whitelist it and allow it to be registered. |
| 86 | if ( in_array( $id, (array) $data['whitelist'] ) ) { |
| 87 | unset($data['whitelist'][$id]); |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | // Otherwise, loop over the IDs to skip and see if there is a relevant match to whitelist. |
| 92 | foreach ( (array) $data['whitelist'] as $skip ) { |
| 93 | if ( preg_match( '#^' . $id . '#i', $skip ) ) { |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // The metabox is not allowed on this screen. Prevent it from being registered. |
| 99 | unset($wp_meta_boxes[$post_type][$context][$priority][$id]); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 |