Post.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 object |
| 12 | * |
| 13 | * @since 6.1.0 Removed support for the $suppressFilters flag |
| 14 | * @since 6.0.1 Added new method isDefined that is used to determine if access option |
| 15 | * is defined |
| 16 | * @since 6.0.0 Initial implementation of the class |
| 17 | * |
| 18 | * @package AAM |
| 19 | * @version 6.1.0 |
| 20 | */ |
| 21 | class AAM_Core_Object_Post extends AAM_Core_Object |
| 22 | { |
| 23 | |
| 24 | /** |
| 25 | * Type of object |
| 26 | * |
| 27 | * @version 6.0.0 |
| 28 | */ |
| 29 | const OBJECT_TYPE = 'post'; |
| 30 | |
| 31 | /** |
| 32 | * WP Post object |
| 33 | * |
| 34 | * @var WP_Post |
| 35 | * |
| 36 | * @access private |
| 37 | * @version 6.0.0 |
| 38 | */ |
| 39 | private $_post = null; |
| 40 | |
| 41 | /** |
| 42 | * Constructor |
| 43 | * |
| 44 | * @param AAM_Core_Subject $subject |
| 45 | * @param WP_Post|Int $post |
| 46 | * |
| 47 | * @return void |
| 48 | * |
| 49 | * @since 6.1.0 Removed support for the $suppressFilters flag |
| 50 | * @since 6.0.0 Initial implementation of the method |
| 51 | * |
| 52 | * @access public |
| 53 | * @version 6.1.0 |
| 54 | */ |
| 55 | public function __construct(AAM_Core_Subject $subject, $post) |
| 56 | { |
| 57 | $this->setSubject($subject); |
| 58 | |
| 59 | // Make sure that we are dealing with WP_Post object |
| 60 | // This is done to remove redundant calls to the database on the backend view |
| 61 | if (is_a($post, 'WP_Post')) { |
| 62 | $this->setPost($post); |
| 63 | } elseif (is_numeric($post)) { |
| 64 | $this->setPost(get_post($post)); |
| 65 | } |
| 66 | |
| 67 | // Making sure that we actually have post, otherwise just initiate with dummy |
| 68 | if (is_a($this->getPost(), 'WP_Post')) { |
| 69 | $this->setId($this->getPost()->ID); |
| 70 | } else { |
| 71 | $this->setPost(new WP_Post((object) array('ID' => 0))); |
| 72 | $this->setId(0); |
| 73 | } |
| 74 | |
| 75 | $this->initialize(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get WP post property |
| 80 | * |
| 81 | * @param string $name |
| 82 | * |
| 83 | * @return mixed |
| 84 | * |
| 85 | * @access public |
| 86 | * @version 6.0.0 |
| 87 | */ |
| 88 | public function __get($name) |
| 89 | { |
| 90 | $post = $this->getPost(); |
| 91 | |
| 92 | return (property_exists($post, $name) ? $post->$name : null); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @inheritDoc |
| 97 | * |
| 98 | * @since 6.1.0 Removed support for the $suppressFilters flag |
| 99 | * @since 6.0.0 Initial implementation of the method |
| 100 | * |
| 101 | * @version 6.1.0 |
| 102 | */ |
| 103 | protected function initialize() |
| 104 | { |
| 105 | // Read direct access settings - those that are explicitly defined for the |
| 106 | // post |
| 107 | $option = $this->getSubject()->readOption( |
| 108 | self::OBJECT_TYPE, $this->ID . '|' . $this->post_type |
| 109 | ); |
| 110 | |
| 111 | $this->determineOverwritten($option); |
| 112 | |
| 113 | // Trigger custom functionality that may populate the post access options |
| 114 | // after initial setup. Typically is used by third party functionality and |
| 115 | // premium AAM plugins. |
| 116 | $this->setOption( |
| 117 | apply_filters('aam_post_object_option_filter', $option, $this) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Set Post |
| 123 | * |
| 124 | * @param WP_Post|stdClass $post |
| 125 | * |
| 126 | * @return void |
| 127 | * |
| 128 | * @access protected |
| 129 | * @version 6.0.0 |
| 130 | */ |
| 131 | protected function setPost($post) |
| 132 | { |
| 133 | $this->_post = $post; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check if particular access property is enabled |
| 138 | * |
| 139 | * Examples of such a access property is "restricted", "hidden", etc. |
| 140 | * |
| 141 | * @param string $property |
| 142 | * |
| 143 | * @return boolean |
| 144 | * |
| 145 | * @access public |
| 146 | * @version 6.0.0 |
| 147 | */ |
| 148 | public function is($property) |
| 149 | { |
| 150 | $result = false; |
| 151 | $option = $this->getOption(); |
| 152 | |
| 153 | if (array_key_exists($property, $option)) { |
| 154 | if (is_bool($option[$property])) { |
| 155 | $result = $option[$property]; |
| 156 | } else { |
| 157 | $result = !empty($option[$property]['enabled']); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Determine if property is defined |
| 166 | * |
| 167 | * This is useful for managing access to commenting system |
| 168 | * |
| 169 | * @param string $property |
| 170 | * |
| 171 | * @return boolean |
| 172 | * |
| 173 | * @access public |
| 174 | * @see AAM_Service_Content::initializeHooks |
| 175 | * |
| 176 | * @version 6.0.1 |
| 177 | */ |
| 178 | public function isDefined($property) |
| 179 | { |
| 180 | return array_key_exists($property, $this->getOption()); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Check if particular action is allowed |
| 185 | * |
| 186 | * This is alias for the AAM_Core_Object_Post::is($property) method and is used |
| 187 | * only to improve code readability. Example of such action is "edit", "publish", |
| 188 | * etc. |
| 189 | * |
| 190 | * @param string $property |
| 191 | * |
| 192 | * @return boolean |
| 193 | * |
| 194 | * @access public |
| 195 | * @version 6.0.0 |
| 196 | */ |
| 197 | public function isAllowedTo($property) |
| 198 | { |
| 199 | return !$this->is($property); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Check if particular access option is enabled |
| 204 | * |
| 205 | * This is alias for the AAM_Core_Object_Post::is($property) method and is used |
| 206 | * only to improve code readability. Example of such action is "teaser", |
| 207 | * "origin", etc. |
| 208 | * |
| 209 | * @param string $property |
| 210 | * |
| 211 | * @return boolean |
| 212 | * |
| 213 | * @access public |
| 214 | * @version 6.0.0 |
| 215 | */ |
| 216 | public function has($property) |
| 217 | { |
| 218 | return $this->is($property); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get WP Post |
| 223 | * |
| 224 | * @return WP_Post |
| 225 | * |
| 226 | * @access public |
| 227 | * @version 6.0.0 |
| 228 | */ |
| 229 | public function getPost() |
| 230 | { |
| 231 | return $this->_post; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Save access settings |
| 236 | * |
| 237 | * @return boolean |
| 238 | * |
| 239 | * @since 6.1.0 Using explicit options to store settings |
| 240 | * @since 6.0.0 Initial implementation of the method |
| 241 | * |
| 242 | * @access public |
| 243 | * @version 6.1.0 |
| 244 | */ |
| 245 | public function save() |
| 246 | { |
| 247 | return $this->getSubject()->updateOption( |
| 248 | $this->getExplicitOption(), |
| 249 | self::OBJECT_TYPE, |
| 250 | $this->ID . '|' . $this->post_type |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Reset access settings |
| 256 | * |
| 257 | * @return boolean |
| 258 | * |
| 259 | * @access public |
| 260 | * @version 6.0.0 |
| 261 | */ |
| 262 | public function reset() |
| 263 | { |
| 264 | return $this->getSubject()->deleteOption( |
| 265 | self::OBJECT_TYPE, $this->ID . '|' . $this->post_type |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | } |