Visibility.php
| 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 | * Post visibility object |
| 12 | * |
| 13 | * @since 6.1.0 Refactored implementation to fix merging bugs and improve inheritance |
| 14 | * mechanism |
| 15 | * @since 6.0.0 Initial implementation of the class |
| 16 | * |
| 17 | * @package AAM |
| 18 | * @version 6.1.0 |
| 19 | */ |
| 20 | class AAM_Core_Object_Visibility extends AAM_Core_Object |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * Type of object |
| 25 | * |
| 26 | * @version 6.0.0 |
| 27 | */ |
| 28 | const OBJECT_TYPE = 'visibility'; |
| 29 | |
| 30 | /** |
| 31 | * List of properties that are responsible for visibility |
| 32 | * |
| 33 | * @var array |
| 34 | * |
| 35 | * @access protected |
| 36 | * @version 6.0.0 |
| 37 | */ |
| 38 | protected $accessProperties = array(); |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * @param AAM_Core_Subject $subject |
| 44 | * @param mixed $id |
| 45 | * |
| 46 | * @return void |
| 47 | * |
| 48 | * @since 6.1.0 Removed support for the $suppressFilters flag |
| 49 | * @since 6.0.0 Initial implementation of the method |
| 50 | * |
| 51 | * @access public |
| 52 | * @version 6.1.0 |
| 53 | */ |
| 54 | public function __construct(AAM_Core_Subject $subject, $id = null) |
| 55 | { |
| 56 | $this->setSubject($subject); |
| 57 | $this->setId($id); |
| 58 | |
| 59 | // Determine post access properties that are responsible for the post |
| 60 | // visibility |
| 61 | $this->accessProperties = apply_filters( |
| 62 | 'aam_visibility_options_filter', array('hidden') |
| 63 | ); |
| 64 | |
| 65 | // Initialize the object |
| 66 | $this->initialize(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | * |
| 72 | * @since 6.1.0 Removed support for the $suppressFilters flag |
| 73 | * @since 6.0.0 Initial implementation of the method |
| 74 | * |
| 75 | * @version 6.1.0 |
| 76 | */ |
| 77 | protected function initialize() |
| 78 | { |
| 79 | $posts = $this->getSubject()->readOption(AAM_Core_Object_Post::OBJECT_TYPE); |
| 80 | |
| 81 | foreach ($posts as $id => $settings) { |
| 82 | $this->pushOptions('post', $id, $settings); |
| 83 | } |
| 84 | |
| 85 | // Initialize post visibility option. This hooks is used by Access Policy |
| 86 | // service as well as Plus Package to populate visibility list |
| 87 | do_action('aam_visibility_object_init_action', $this); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Push visibility option to the registry |
| 92 | * |
| 93 | * @param string $object |
| 94 | * @param mixed $id |
| 95 | * @param array $options |
| 96 | * |
| 97 | * @return array |
| 98 | * |
| 99 | * @since 6.1.0 Changed the way visibility options are indexed (used to be as |
| 100 | * multi-dimensional array and now it is key/value pairs) |
| 101 | * @since 6.0.0 Initial implementation of the method |
| 102 | * |
| 103 | * @access public |
| 104 | * @version 6.1.0 |
| 105 | */ |
| 106 | public function pushOptions($object, $id, $options) |
| 107 | { |
| 108 | $option = $this->getOption(); |
| 109 | $filtered = array(); |
| 110 | |
| 111 | foreach ($options as $key => $value) { |
| 112 | if (in_array($key, $this->accessProperties, true)) { |
| 113 | $filtered[$key] = $value; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (empty($filtered)) { |
| 118 | $filtered = array_combine( |
| 119 | $this->accessProperties, |
| 120 | array_fill(0, count($this->accessProperties), false) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | if (!isset($option["{$object}/{$id}"])) { |
| 125 | $option["{$object}/{$id}"] = $filtered; |
| 126 | } else { |
| 127 | $option["{$object}/{$id}"] = array_replace( |
| 128 | $filtered, $option["{$object}/{$id}"] |
| 129 | ); |
| 130 | } |
| 131 | $this->setOption($option); |
| 132 | |
| 133 | return $filtered; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get visibility segment |
| 138 | * |
| 139 | * @param string $segment |
| 140 | * |
| 141 | * @return array |
| 142 | * |
| 143 | * @since 6.1.0 Changed the way visibility options are fetched (used to be as |
| 144 | * multi-dimensional array and now it is key/value pairs) |
| 145 | * @since 6.0.0 Initial implementation of the method |
| 146 | * |
| 147 | * @access public |
| 148 | * @version 6.1.0 |
| 149 | */ |
| 150 | public function getSegment($segment) |
| 151 | { |
| 152 | $response = array(); |
| 153 | |
| 154 | foreach($this->getOption() as $key => $value) { |
| 155 | if (strpos($key, "{$segment}/") === 0) { |
| 156 | $response[str_replace("{$segment}/", '', $key)] = $value; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return $response; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Merge visibility settings |
| 165 | * |
| 166 | * @param array $options |
| 167 | * |
| 168 | * @return array |
| 169 | * |
| 170 | * @since 6.1.0 Fixed bug with incorrectly merged settings for users with multiple |
| 171 | * roles |
| 172 | * @since 6.0.0 Initial implementation of the method |
| 173 | * |
| 174 | * @access public |
| 175 | * @version 6.1.0 |
| 176 | */ |
| 177 | public function mergeOption($options) |
| 178 | { |
| 179 | $these_options = $this->getOption(); |
| 180 | $keys = array_unique(array_merge( |
| 181 | array_keys($options), array_keys($this->getOption()) |
| 182 | )); |
| 183 | |
| 184 | $merged = array(); |
| 185 | |
| 186 | // Iterate over each unique key end merge settings accordingly |
| 187 | foreach($keys as $key) { |
| 188 | $merged[$key] = AAM::api()->mergeSettings( |
| 189 | (isset($options[$key]) ? $options[$key] : array()), |
| 190 | (isset($these_options[$key]) ? $these_options[$key] : array()), |
| 191 | AAM_Core_Object_Post::OBJECT_TYPE |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | return $merged; |
| 196 | } |
| 197 | |
| 198 | } |