Abstract.php
9 years ago
Capability.php
9 years ago
Contact.php
9 years ago
Extension.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
Role.php
9 years ago
Security.php
9 years ago
Teaser.php
9 years ago
User.php
9 years ago
Utility.php
9 years ago
Post.php
397 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 | * Backend posts & pages manager |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Backend_Feature_Post extends AAM_Backend_Feature_Abstract { |
| 17 | |
| 18 | /** |
| 19 | * Get list for the table |
| 20 | * |
| 21 | * @return string |
| 22 | * |
| 23 | * @access public |
| 24 | */ |
| 25 | public function getTable() { |
| 26 | $type = trim(AAM_Core_Request::request('type')); |
| 27 | |
| 28 | if (empty($type)) { |
| 29 | $response = $this->retrieveTypeList(); |
| 30 | } else { |
| 31 | $response = $this->retrieveTypeContent($type); |
| 32 | } |
| 33 | |
| 34 | return $this->wrapTable($response); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Retrieve list of registered post types |
| 39 | * |
| 40 | * @return array |
| 41 | * |
| 42 | * @access protected |
| 43 | */ |
| 44 | protected function retrieveTypeList() { |
| 45 | $list = $this->prepareTypeList(); |
| 46 | $response = array( |
| 47 | 'data' => array(), |
| 48 | 'recordsTotal' => $list->total, |
| 49 | 'recordsFiltered' => $list->filtered |
| 50 | ); |
| 51 | |
| 52 | foreach ($list->records as $type) { |
| 53 | $response['data'][] = array( |
| 54 | $type->name, |
| 55 | null, |
| 56 | 'type', |
| 57 | $type->labels->name, |
| 58 | apply_filters('aam-type-row-actions-filter', 'drilldown,manage', $type) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | return $response; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @return type |
| 68 | */ |
| 69 | protected function prepareTypeList() { |
| 70 | $list = get_post_types(array(), 'objects'); |
| 71 | $filtered = array(); |
| 72 | |
| 73 | //filters |
| 74 | $s = AAM_Core_Request::post('search.value'); |
| 75 | $length = AAM_Core_Request::post('length'); |
| 76 | $start = AAM_Core_Request::post('start'); |
| 77 | |
| 78 | foreach (get_post_types(array(), 'objects') as $type) { |
| 79 | if ($type->public |
| 80 | && (empty($s) || stripos($type->labels->name, $s) !== false)) { |
| 81 | $filtered[] = $type; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return (object) array( |
| 86 | 'total' => count($list), |
| 87 | 'filtered' => count($filtered), |
| 88 | 'records' => array_slice($filtered, $start, $length) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get post type children |
| 94 | * |
| 95 | * Retrieve list of all posts and terms that belong to specified post type |
| 96 | * |
| 97 | * @param string $type |
| 98 | * |
| 99 | * @return array |
| 100 | * |
| 101 | * @access protected |
| 102 | */ |
| 103 | protected function retrieveTypeContent($type) { |
| 104 | $list = $this->prepareContentList($type); |
| 105 | $response = array( |
| 106 | 'data' => array(), |
| 107 | 'recordsTotal' => $list->total, |
| 108 | 'recordsFiltered' => $list->filtered |
| 109 | ); |
| 110 | |
| 111 | foreach($list->records as $record) { |
| 112 | if (isset($record->ID)) { //this is post |
| 113 | $response['data'][] = array( |
| 114 | $record->ID, |
| 115 | get_edit_post_link($record->ID, 'link'), |
| 116 | 'post', |
| 117 | $record->post_title, |
| 118 | apply_filters('aam-post-row-actions-filter', 'manage,edit', $record) |
| 119 | ); |
| 120 | } else { //term |
| 121 | $response['data'][] = array( |
| 122 | $record->term_id . '|' . $record->taxonomy, |
| 123 | get_edit_term_link($record->term_id, $record->taxonomy), |
| 124 | 'term', |
| 125 | $record->name, |
| 126 | apply_filters('aam-term-row-actions-filter', 'manage,edit', $record) |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | return $response; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * |
| 137 | * @return type |
| 138 | */ |
| 139 | protected function prepareContentList($type) { |
| 140 | $list = array(); |
| 141 | $filtered = array(); |
| 142 | |
| 143 | //filters |
| 144 | $s = AAM_Core_Request::post('search.value'); |
| 145 | $length = AAM_Core_Request::post('length'); |
| 146 | $start = AAM_Core_Request::post('start'); |
| 147 | |
| 148 | //first retrieve all hierarchical terms that belong to Post Type |
| 149 | foreach (get_object_taxonomies($type, 'objects') as $tax) { |
| 150 | if (is_taxonomy_hierarchical($tax->name)) { |
| 151 | //get all terms that have no parent category |
| 152 | $list = array_merge($list, $this->retrieveTermList($tax->name)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | //retrieve all posts |
| 157 | $list = array_merge( |
| 158 | $list, |
| 159 | get_posts(array( |
| 160 | 'post_type' => $type, 'category' => 0, |
| 161 | 'numberposts' => -1, 'post_status' => 'any' |
| 162 | )) |
| 163 | ); |
| 164 | |
| 165 | foreach($list as $row) { |
| 166 | if (!empty($s)) { |
| 167 | if (isset($row->term_id) && stripos($row->name, $s) !== false) { |
| 168 | $filtered[] = $row; |
| 169 | } elseif (isset($row->ID) && stripos($row->post_title, $s) !== false) { |
| 170 | $filtered[] = $row; |
| 171 | } |
| 172 | } else { |
| 173 | $filtered[] = $row; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return (object) array( |
| 178 | 'total' => count($list), |
| 179 | 'filtered' => count($filtered), |
| 180 | 'records' => array_slice($filtered, $start, $length) |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Retrieve term list |
| 186 | * |
| 187 | * @param string $taxonomy |
| 188 | * |
| 189 | * @return array |
| 190 | * |
| 191 | * @access protected |
| 192 | */ |
| 193 | protected function retrieveTermList($taxonomy) { |
| 194 | $response = array(); |
| 195 | |
| 196 | foreach (get_terms($taxonomy, array('hide_empty' => false)) as $term) { |
| 197 | $term->taxonomy = $taxonomy; |
| 198 | $response[] = $term; |
| 199 | } |
| 200 | |
| 201 | return $response; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Prepare response |
| 206 | * |
| 207 | * @param array $response |
| 208 | * |
| 209 | * @return string |
| 210 | * |
| 211 | * @access protected |
| 212 | */ |
| 213 | protected function wrapTable($response) { |
| 214 | $response['draw'] = AAM_Core_Request::request('draw'); |
| 215 | |
| 216 | return json_encode($response); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get Post or Term access |
| 221 | * |
| 222 | * @return string |
| 223 | * |
| 224 | * @access public |
| 225 | */ |
| 226 | public function getAccess() { |
| 227 | $type = trim(AAM_Core_Request::post('type')); |
| 228 | $id = AAM_Core_Request::post('id'); |
| 229 | |
| 230 | $object = AAM_Backend_View::getSubject()->getObject($type, $id); |
| 231 | |
| 232 | //prepare the response object |
| 233 | if ($object instanceof AAM_Core_Object) { |
| 234 | $access = $object->getOption(); |
| 235 | $metadata = array('overwritten' => $object->isOverwritten()); |
| 236 | } else { |
| 237 | $access = $metadata = array(); |
| 238 | } |
| 239 | |
| 240 | return json_encode(array('access' => $access, 'meta' => $metadata)); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Save post properties |
| 245 | * |
| 246 | * @return string |
| 247 | * |
| 248 | * @access public |
| 249 | */ |
| 250 | public function save() { |
| 251 | if ($this->checkLimit()) { |
| 252 | $subject = AAM_Backend_View::getSubject(); |
| 253 | |
| 254 | $object = trim(AAM_Core_Request::post('object')); |
| 255 | $id = AAM_Core_Request::post('objectId', null); |
| 256 | |
| 257 | $param = AAM_Core_Request::post('param'); |
| 258 | $value = AAM_Core_Request::post('value'); |
| 259 | |
| 260 | //clear cache |
| 261 | AAM_Core_Cache::clear(); |
| 262 | |
| 263 | $result = $subject->save($param, $value, $object, $id); |
| 264 | } else { |
| 265 | $result = false; |
| 266 | $error = __('You reached your limitation.', AAM_KEY); |
| 267 | } |
| 268 | |
| 269 | return json_encode(array( |
| 270 | 'status' => ($result ? 'success' : 'failure'), |
| 271 | 'error' => (empty($error) ? '' : $error) |
| 272 | )); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Reset the object settings |
| 277 | * |
| 278 | * @return string |
| 279 | * |
| 280 | * @access public |
| 281 | */ |
| 282 | public function reset() { |
| 283 | $type = trim(AAM_Core_Request::post('type')); |
| 284 | $id = AAM_Core_Request::post('id', 0); |
| 285 | |
| 286 | $object = AAM_Backend_View::getSubject()->getObject($type, $id); |
| 287 | if ($object instanceof AAM_Core_Object) { |
| 288 | $result = $object->reset(); |
| 289 | //clear cache |
| 290 | AAM_Core_Cache::clear(); |
| 291 | } else { |
| 292 | $result = false; |
| 293 | } |
| 294 | |
| 295 | return json_encode(array('status' => ($result ? 'success' : 'failure'))); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * |
| 300 | * @global type $wpdb |
| 301 | * @return type |
| 302 | */ |
| 303 | public static function checkLimit() { |
| 304 | global $wpdb; |
| 305 | |
| 306 | $limit = apply_filters('aam-post-limit', 0); |
| 307 | |
| 308 | if ($limit != -1) { |
| 309 | //count number of posts that have access saved |
| 310 | $query = "SELECT COUNT(*) as `total` FROM {$wpdb->postmeta} " |
| 311 | . "WHERE meta_key LIKE %s"; |
| 312 | |
| 313 | $row = $wpdb->get_row($wpdb->prepare($query, 'aam_post_access_%')); |
| 314 | $limit = ($row->total < 10 ? -1 : 0); |
| 315 | } |
| 316 | |
| 317 | return ($limit == -1); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @inheritdoc |
| 322 | */ |
| 323 | public static function getAccessOption() { |
| 324 | return 'feature.post.capability'; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * @inheritdoc |
| 329 | */ |
| 330 | public static function getTemplate() { |
| 331 | return 'object/post.phtml'; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * |
| 336 | * @staticvar type $list |
| 337 | * @param type $area |
| 338 | * @return type |
| 339 | */ |
| 340 | public function getAccessOptionList($area) { |
| 341 | static $list = null; |
| 342 | |
| 343 | if (is_null($list)) { |
| 344 | $list = require_once dirname(__FILE__) . '/../View/PostOptionList.php'; |
| 345 | } |
| 346 | |
| 347 | return apply_filters('aam-post-access-options-filter', $list[$area], $area); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * |
| 352 | * @return type |
| 353 | */ |
| 354 | public function getCurrentObject() { |
| 355 | $object = (object) array( |
| 356 | 'id' => urldecode(AAM_Core_Request::post('oid')), |
| 357 | 'type' => AAM_Core_Request::post('otype') |
| 358 | ); |
| 359 | |
| 360 | if ($object->id) { |
| 361 | if (strpos($object->id, '|') !== false) { //term |
| 362 | $part = explode('|', $object->id); |
| 363 | $object->term = get_term($part[0], $part[1]); |
| 364 | } else { |
| 365 | $object->post = get_post($object->id); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return $object; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Register Posts & Pages feature |
| 374 | * |
| 375 | * @return void |
| 376 | * |
| 377 | * @access public |
| 378 | */ |
| 379 | public static function register() { |
| 380 | $cap = AAM_Core_Config::get(self::getAccessOption(), 'administrator'); |
| 381 | |
| 382 | AAM_Backend_Feature::registerFeature((object) array( |
| 383 | 'uid' => 'post', |
| 384 | 'position' => 20, |
| 385 | 'title' => __('Posts & Pages', AAM_KEY), |
| 386 | 'capability' => $cap, |
| 387 | 'subjects' => array( |
| 388 | 'AAM_Core_Subject_Role', |
| 389 | 'AAM_Core_Subject_User', |
| 390 | 'AAM_Core_Subject_Visitor', |
| 391 | 'AAM_Core_Subject_Default' |
| 392 | ), |
| 393 | 'view' => __CLASS__ |
| 394 | )); |
| 395 | } |
| 396 | |
| 397 | } |