Capability.php
9 years ago
LoginRedirect.php
9 years ago
Menu.php
9 years ago
Metabox.php
9 years ago
Post.php
9 years ago
Redirect.php
9 years ago
Teaser.php
9 years ago
Metabox.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Metabox object |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Core_Object_Metabox extends AAM_Core_Object { |
| 17 | |
| 18 | /** |
| 19 | * Constructor |
| 20 | * |
| 21 | * @param AAM_Core_Subject $subject |
| 22 | * |
| 23 | * @return void |
| 24 | * |
| 25 | * @access public |
| 26 | */ |
| 27 | public function __construct(AAM_Core_Subject $subject) { |
| 28 | parent::__construct($subject); |
| 29 | |
| 30 | $option = $this->getSubject()->readOption('metabox'); |
| 31 | |
| 32 | if (empty($option)) { |
| 33 | $option = $this->getSubject()->inheritFromParent('metabox'); |
| 34 | } else { |
| 35 | $this->setOverwritten(true); |
| 36 | } |
| 37 | |
| 38 | $this->setOption($option); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @global type $wp_registered_widgets |
| 44 | * @param type $sidebar_widgets |
| 45 | * @return type |
| 46 | */ |
| 47 | public function filterFrontend($sidebar_widgets) { |
| 48 | global $wp_registered_widgets; |
| 49 | |
| 50 | if (is_array($wp_registered_widgets)) { |
| 51 | foreach ($wp_registered_widgets as $id => $widget) { |
| 52 | $callback = $this->getWidgetCallback($widget); |
| 53 | if ($this->has('widgets', $callback)) { |
| 54 | unregister_widget($callback); |
| 55 | //remove it from registered widget global var!! |
| 56 | //INFORM: Why Unregister Widget does not clear global var? |
| 57 | unset($wp_registered_widgets[$id]); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $sidebar_widgets; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @param type $widget |
| 68 | * @return type |
| 69 | */ |
| 70 | protected function getWidgetCallback($widget) { |
| 71 | if (is_object($widget['callback'][0])) { |
| 72 | $callback = get_class($widget['callback'][0]); |
| 73 | } elseif (is_string($widget['callback'][0])) { |
| 74 | $callback = $widget['callback'][0]; |
| 75 | } else { |
| 76 | $callback = null; |
| 77 | } |
| 78 | |
| 79 | return $callback; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * |
| 84 | * @global type $wp_meta_boxes |
| 85 | * @param type $screen |
| 86 | */ |
| 87 | public function filterBackend($screen) { |
| 88 | global $wp_meta_boxes; |
| 89 | |
| 90 | if (is_array($wp_meta_boxes)) { |
| 91 | foreach ($wp_meta_boxes as $screen_id => $zones) { |
| 92 | if ($screen == $screen_id) { |
| 93 | $this->filterZones($zones, $screen_id); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * |
| 101 | * @param type $zones |
| 102 | * @param type $screen_id |
| 103 | */ |
| 104 | protected function filterZones($zones, $screen_id) { |
| 105 | foreach ($zones as $zone => $priorities) { |
| 106 | foreach ($priorities as $metaboxes) { |
| 107 | $this->filterMetaboxes($zone, $metaboxes, $screen_id); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * |
| 114 | * @param type $zone |
| 115 | * @param type $metaboxes |
| 116 | * @param type $screen_id |
| 117 | */ |
| 118 | protected function filterMetaboxes($zone, $metaboxes, $screen_id) { |
| 119 | foreach (array_keys($metaboxes) as $metabox) { |
| 120 | if ($this->has($screen_id, $metabox)) { |
| 121 | remove_meta_box($metabox, $screen_id, $zone); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @inheritdoc |
| 128 | */ |
| 129 | public function save($metabox, $granted) { |
| 130 | $param = explode('|', $metabox); |
| 131 | $option = $this->getOption(); |
| 132 | |
| 133 | $option[$param[0]][$param[1]] = $granted; |
| 134 | |
| 135 | return $this->getSubject()->updateOption($option, 'metabox'); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * |
| 140 | */ |
| 141 | public function reset() { |
| 142 | return $this->getSubject()->deleteOption('metabox'); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * |
| 148 | * @param type $screen |
| 149 | * @param type $metabox |
| 150 | * @return type |
| 151 | */ |
| 152 | public function has($screen, $metabox) { |
| 153 | $options = $this->getOption(); |
| 154 | |
| 155 | return !empty($options[$screen][$metabox]); |
| 156 | } |
| 157 | |
| 158 | } |