404Redirect.php
8 years ago
Capability.php
8 years ago
LoginRedirect.php
8 years ago
LogoutRedirect.php
8 years ago
Menu.php
8 years ago
Metabox.php
8 years ago
Post.php
8 years ago
Redirect.php
8 years ago
Route.php
8 years ago
Post.php
579 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_Main_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 | $all = AAM_Core_Config::get('manage-hidden-post-types', false); |
| 78 | |
| 79 | foreach (get_post_types(array(), 'objects') as $type) { |
| 80 | if (($all || $type->public) |
| 81 | && (empty($s) || stripos($type->labels->name, $s) !== false)) { |
| 82 | $filtered[] = $type; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return (object) array( |
| 87 | 'total' => count($list), |
| 88 | 'filtered' => count($filtered), |
| 89 | 'records' => array_slice($filtered, $start, $length) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get post type children |
| 95 | * |
| 96 | * Retrieve list of all posts and terms that belong to specified post type |
| 97 | * |
| 98 | * @param string $type |
| 99 | * |
| 100 | * @return array |
| 101 | * |
| 102 | * @access protected |
| 103 | */ |
| 104 | protected function retrieveTypeContent($type) { |
| 105 | $list = $this->prepareContentList($type); |
| 106 | $response = array( |
| 107 | 'data' => array(), |
| 108 | 'recordsTotal' => $list->total, |
| 109 | 'recordsFiltered' => $list->filtered |
| 110 | ); |
| 111 | |
| 112 | foreach($list->records as $record) { |
| 113 | if (isset($record->ID)) { //this is post |
| 114 | $link = get_edit_post_link($record->ID, 'link'); |
| 115 | $response['data'][] = array( |
| 116 | $record->ID, |
| 117 | $link, |
| 118 | 'post', |
| 119 | get_the_title($record), |
| 120 | apply_filters( |
| 121 | 'aam-post-row-actions-filter', |
| 122 | 'manage' . ($link ? ',edit' : ''), |
| 123 | $record |
| 124 | ), |
| 125 | //get_post_permalink($record) |
| 126 | ); |
| 127 | } else { //term |
| 128 | $response['data'][] = array( |
| 129 | $record->term_id . '|' . $record->taxonomy, |
| 130 | get_edit_term_link($record->term_id, $record->taxonomy), |
| 131 | 'term', |
| 132 | $record->name, |
| 133 | apply_filters('aam-term-row-actions-filter', 'manage,edit', $record) |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | return $response; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * |
| 144 | * @return type |
| 145 | */ |
| 146 | protected function prepareContentList($type) { |
| 147 | $list = array(); |
| 148 | //filters |
| 149 | $s = AAM_Core_Request::post('search.value'); |
| 150 | $length = AAM_Core_Request::post('length'); |
| 151 | $start = AAM_Core_Request::post('start'); |
| 152 | |
| 153 | //calculate how many term and/or posts we need to fetch |
| 154 | $paging = $this->getFetchPagination($type, $s, $start, $length); |
| 155 | |
| 156 | //first retrieve all hierarchical terms that belong to Post Type |
| 157 | if ($paging['terms']) { |
| 158 | $list = $this->retrieveTermList( |
| 159 | $this->getTypeTaxonomies($type), |
| 160 | $s, |
| 161 | $paging['term_offset'], |
| 162 | $paging['terms'] |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | //retrieve all posts |
| 167 | if ($paging['posts']) { |
| 168 | $list = array_merge( |
| 169 | $list, |
| 170 | $this->retrievePostList( |
| 171 | $type, $s, $paging['post_offset'], $paging['posts'] |
| 172 | ) |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | return (object) array( |
| 177 | 'total' => $paging['total'], |
| 178 | 'filtered' => $paging['total'], |
| 179 | 'records' => $list |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * |
| 185 | * @param type $type |
| 186 | * @return type |
| 187 | */ |
| 188 | protected function getTypeTaxonomies($type) { |
| 189 | $list = array(); |
| 190 | |
| 191 | foreach (get_object_taxonomies($type) as $name) { |
| 192 | if (is_taxonomy_hierarchical($name)) { |
| 193 | //get all terms that have no parent category |
| 194 | $list[] = $name; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return $list; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * |
| 203 | * @param type $type |
| 204 | * @param type $search |
| 205 | * @param type $offset |
| 206 | * @param type $limit |
| 207 | * @return type |
| 208 | */ |
| 209 | protected function getFetchPagination($type, $search, $offset, $limit) { |
| 210 | $result = array('terms' => 0, 'posts' => 0, 'term_offset' => $offset); |
| 211 | |
| 212 | //get terms count |
| 213 | $taxonomy = $this->getTypeTaxonomies($type); |
| 214 | |
| 215 | if (!empty($taxonomy)) { |
| 216 | $terms = get_terms(array( |
| 217 | 'fields' => 'count', |
| 218 | 'search' => $search, |
| 219 | 'hide_empty' => false, |
| 220 | 'taxonomy' => $taxonomy |
| 221 | )); |
| 222 | } else { |
| 223 | $terms = 0; |
| 224 | } |
| 225 | |
| 226 | //get posts count |
| 227 | $posts = $this->getPostCount($type, $search); |
| 228 | |
| 229 | if ($offset < $terms) { |
| 230 | if ($terms - $limit >= $offset) { |
| 231 | $result['terms'] = $limit; |
| 232 | } else { |
| 233 | $result['terms'] = $terms - $offset; |
| 234 | $result['posts'] = $limit - $result['terms']; |
| 235 | } |
| 236 | } else { |
| 237 | $result['posts'] = $limit; |
| 238 | } |
| 239 | |
| 240 | $result['total'] = $terms + $posts; |
| 241 | $result['post_offset'] = ($offset ? $offset - $terms : 0); |
| 242 | |
| 243 | return $result; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * |
| 248 | * @global type $wpdb |
| 249 | * @param type $type |
| 250 | * @param type $search |
| 251 | * @return type |
| 252 | */ |
| 253 | protected function getPostCount($type, $search) { |
| 254 | global $wpdb; |
| 255 | |
| 256 | $query = "SELECT COUNT( * ) AS total FROM {$wpdb->posts} "; |
| 257 | $query .= "WHERE (post_type = %s) AND (post_title LIKE %s)"; |
| 258 | |
| 259 | $args = array($type, "{$search}%"); |
| 260 | |
| 261 | foreach (get_post_stati(array( 'exclude_from_search' => true)) as $status ) { |
| 262 | $query .= " AND ({$wpdb->posts}.post_status <> %s)"; |
| 263 | $args[] = $status; |
| 264 | } |
| 265 | |
| 266 | return $wpdb->get_var($wpdb->prepare($query, $args)); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Retrieve term list |
| 271 | * |
| 272 | * @param array $taxonomies |
| 273 | * |
| 274 | * @return array |
| 275 | * |
| 276 | * @access protected |
| 277 | */ |
| 278 | protected function retrieveTermList($taxonomies, $search, $offset, $limit) { |
| 279 | $args = array( |
| 280 | 'fields' => 'all', |
| 281 | 'hide_empty' => false, |
| 282 | 'search' => $search, |
| 283 | 'taxonomy' => $taxonomies, |
| 284 | 'offset' => $offset, |
| 285 | 'number' => $limit |
| 286 | ); |
| 287 | |
| 288 | return get_terms($args); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * |
| 293 | * @param type $type |
| 294 | * @param type $search |
| 295 | * @param type $offset |
| 296 | * @param type $limit |
| 297 | * @return type |
| 298 | */ |
| 299 | protected function retrievePostList($type, $search, $offset, $limit) { |
| 300 | return get_posts(array( |
| 301 | 'post_type' => $type, |
| 302 | 'category' => 0, |
| 303 | 's' => $search, |
| 304 | 'offset' => $offset, |
| 305 | 'numberposts' => $limit, |
| 306 | 'post_status' => 'any', |
| 307 | 'fields' => 'all' |
| 308 | )); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Prepare response |
| 313 | * |
| 314 | * @param array $response |
| 315 | * |
| 316 | * @return string |
| 317 | * |
| 318 | * @access protected |
| 319 | */ |
| 320 | protected function wrapTable($response) { |
| 321 | $response['draw'] = AAM_Core_Request::request('draw'); |
| 322 | |
| 323 | return json_encode($response); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * |
| 328 | * @return type |
| 329 | */ |
| 330 | public function autocomplete() { |
| 331 | $res = array(); |
| 332 | $list = get_posts(array( |
| 333 | 'post_type' => AAM_Core_Request::post('type'), |
| 334 | 'category' => 0, |
| 335 | 's' => AAM_Core_Request::post('s'), |
| 336 | 'numberposts' => 10, |
| 337 | 'post_status' => 'any', |
| 338 | 'fields' => 'all' |
| 339 | )); |
| 340 | |
| 341 | if (count($list)) { |
| 342 | foreach($list as $post) { |
| 343 | $res[] = "{$post->ID}|{$post->post_title}"; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return json_encode($res); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Get Post or Term access |
| 352 | * |
| 353 | * @return string |
| 354 | * |
| 355 | * @access public |
| 356 | */ |
| 357 | public function getAccess() { |
| 358 | $type = trim(AAM_Core_Request::post('type')); |
| 359 | $id = AAM_Core_Request::post('id'); |
| 360 | $access = $metadata = array(); |
| 361 | $object = AAM_Backend_Subject::getInstance()->getObject($type, $id); |
| 362 | |
| 363 | //prepare the response object |
| 364 | if (is_a($object, 'AAM_Core_Object')) { |
| 365 | foreach($object->getOption() as $key => $value) { |
| 366 | if (is_bool($value) || in_array($value, array('0', '1'))) { |
| 367 | $access[$key] = ($value ? 1 : 0); //TODO - to support legacy |
| 368 | } else { |
| 369 | $access[$key] = $value; |
| 370 | } |
| 371 | } |
| 372 | $metadata = array('overwritten' => $object->isOverwritten()); |
| 373 | } |
| 374 | |
| 375 | return json_encode(array( |
| 376 | 'access' => $access, |
| 377 | 'meta' => $metadata, |
| 378 | 'preview' => $this->preparePreviewValues($access) |
| 379 | )); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * |
| 384 | * @param type $options |
| 385 | * @return type |
| 386 | */ |
| 387 | protected function preparePreviewValues($options) { |
| 388 | $previews = array(); |
| 389 | |
| 390 | foreach($options as $option => $value) { |
| 391 | $previews[$option] = $this->getPreviewValue($option, $value); |
| 392 | } |
| 393 | |
| 394 | return $previews; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * |
| 399 | * @param type $option |
| 400 | * @param type $val |
| 401 | * @return type |
| 402 | */ |
| 403 | protected function getPreviewValue($option, $val) { |
| 404 | switch($option) { |
| 405 | case 'frontend.teaser': |
| 406 | $str = strip_tags($val); |
| 407 | $preview = (strlen($str) > 25 ? substr($str, 0, 22) . '...' : $str); |
| 408 | break; |
| 409 | |
| 410 | default: |
| 411 | $preview = apply_filters( |
| 412 | 'aam-post-option-preview-filter', $val, $option |
| 413 | ); |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | return $preview; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Save post properties |
| 422 | * |
| 423 | * @return string |
| 424 | * |
| 425 | * @access public |
| 426 | */ |
| 427 | public function save() { |
| 428 | $subject = AAM_Backend_Subject::getInstance(); |
| 429 | |
| 430 | $object = trim(AAM_Core_Request::post('object')); |
| 431 | $id = AAM_Core_Request::post('objectId', null); |
| 432 | |
| 433 | $param = AAM_Core_Request::post('param'); |
| 434 | $value = AAM_Core_Request::post('value'); |
| 435 | |
| 436 | if (strpos($param, '.expire_datetime') !== false) { |
| 437 | $value = date('Y-m-d H:i:s', strtotime($value)); |
| 438 | } |
| 439 | |
| 440 | //clear cache |
| 441 | AAM_Core_API::clearCache(); |
| 442 | |
| 443 | $result = $subject->save($param, $value, $object, $id); |
| 444 | |
| 445 | return json_encode(array( |
| 446 | 'status' => ($result ? 'success' : 'failure'), |
| 447 | 'value' => $value, |
| 448 | 'preview' => $this->getPreviewValue($param, $value) |
| 449 | )); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Reset the object settings |
| 454 | * |
| 455 | * @return string |
| 456 | * |
| 457 | * @access public |
| 458 | */ |
| 459 | public function reset() { |
| 460 | $type = trim(AAM_Core_Request::post('type')); |
| 461 | $id = AAM_Core_Request::post('id', 0); |
| 462 | |
| 463 | $object = AAM_Backend_Subject::getInstance()->getObject($type, $id); |
| 464 | if ($object instanceof AAM_Core_Object) { |
| 465 | $result = $object->reset(); |
| 466 | //clear cache |
| 467 | AAM_Core_API::clearCache(); |
| 468 | } else { |
| 469 | $result = false; |
| 470 | } |
| 471 | |
| 472 | return json_encode(array('status' => ($result ? 'success' : 'failure'))); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * @inheritdoc |
| 477 | */ |
| 478 | public static function getTemplate() { |
| 479 | return 'main/post.phtml'; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * |
| 484 | * @staticvar type $list |
| 485 | * @param type $area |
| 486 | * @return type |
| 487 | */ |
| 488 | public static function getAccessOptionList($area) { |
| 489 | static $cache = null; |
| 490 | |
| 491 | if (is_null($cache)) { |
| 492 | $cache = AAM_Backend_View_PostOptionList::get(); |
| 493 | } |
| 494 | |
| 495 | $subject = AAM_Backend_Subject::getInstance()->getUID(); |
| 496 | $list = apply_filters( |
| 497 | 'aam-post-access-options-filter', $cache[$area], $area |
| 498 | ); |
| 499 | |
| 500 | $filtered = array(); |
| 501 | foreach($list as $option => $data) { |
| 502 | $add = empty($data['exclude']) || !in_array($subject, $data['exclude']); |
| 503 | |
| 504 | if ($add) { |
| 505 | $add = empty($data['config']) || AAM_Core_Config::get($data['config'], true); |
| 506 | } |
| 507 | |
| 508 | if ($add) { |
| 509 | $filtered[$option] = $data; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return $filtered; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * |
| 518 | * @param type $renderBackButton |
| 519 | * @param type $extraClass |
| 520 | */ |
| 521 | public static function renderAccessForm() { |
| 522 | ob_start(); |
| 523 | require_once( |
| 524 | AAM_BASEDIR . '/Application/Backend/phtml/partial/post-access-form.phtml' |
| 525 | ); |
| 526 | $content = ob_get_contents(); |
| 527 | ob_end_clean(); |
| 528 | |
| 529 | return $content; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * |
| 534 | * @return type |
| 535 | */ |
| 536 | public static function getCurrentObject() { |
| 537 | $object = (object) array( |
| 538 | 'id' => urldecode(AAM_Core_Request::request('oid')), |
| 539 | 'type' => AAM_Core_Request::request('otype') |
| 540 | ); |
| 541 | |
| 542 | if ($object->id) { |
| 543 | if (strpos($object->id, '|') !== false) { //term |
| 544 | $part = explode('|', $object->id); |
| 545 | $object->term = get_term($part[0], $part[1]); |
| 546 | } else { |
| 547 | $object->post = get_post($object->id); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return $object; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Register Posts & Pages feature |
| 556 | * |
| 557 | * @return void |
| 558 | * |
| 559 | * @access public |
| 560 | */ |
| 561 | public static function register() { |
| 562 | AAM_Backend_Feature::registerFeature((object) array( |
| 563 | 'uid' => 'post', |
| 564 | 'position' => 20, |
| 565 | 'title' => __('Posts & Terms', AAM_KEY), |
| 566 | 'capability' => 'aam_manage_posts', |
| 567 | 'type' => 'main', |
| 568 | 'subjects' => array( |
| 569 | AAM_Core_Subject_Role::UID, |
| 570 | AAM_Core_Subject_User::UID, |
| 571 | AAM_Core_Subject_Visitor::UID, |
| 572 | AAM_Core_Subject_Default::UID |
| 573 | ), |
| 574 | 'option' => 'backend-access-control,frontend-access-control', |
| 575 | 'view' => __CLASS__ |
| 576 | )); |
| 577 | } |
| 578 | |
| 579 | } |