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