Cache.php
7 years ago
Capability.php
7 years ago
LoginRedirect.php
7 years ago
LogoutRedirect.php
7 years ago
Menu.php
7 years ago
Metabox.php
7 years ago
Post.php
7 years ago
Redirect.php
7 years ago
Route.php
7 years ago
Toolbar.php
7 years ago
Visibility.php
7 years ago
Visibility.php
151 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 | * Post visibility object |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Core_Object_Visibility extends AAM_Core_Object { |
| 17 | |
| 18 | /** |
| 19 | * Constructor |
| 20 | * |
| 21 | * @param AAM_Core_Subject $subject |
| 22 | * |
| 23 | * @return void |
| 24 | * |
| 25 | * @access public |
| 26 | */ |
| 27 | public function __construct(AAM_Core_Subject $subject) { |
| 28 | parent::__construct($subject); |
| 29 | |
| 30 | $this->initialize(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @global type $wpdb |
| 36 | */ |
| 37 | public function initialize() { |
| 38 | global $wpdb; |
| 39 | |
| 40 | $subject = $this->getSubject(); |
| 41 | |
| 42 | // Read cache first |
| 43 | $option = $subject->getObject('cache')->get('visibility'); |
| 44 | if ($option === false) { //if false, then the cache is empty but exists |
| 45 | $option = array(); |
| 46 | } elseif (empty($option)) { |
| 47 | $query = "SELECT pm.`post_id`, pm.`meta_value`, p.`post_type` FROM {$wpdb->postmeta} AS pm "; |
| 48 | $query .= "LEFT JOIN {$wpdb->posts} AS p ON (pm.`post_id` = p.ID) "; |
| 49 | $query .= "WHERE pm.`meta_key` = %s"; |
| 50 | |
| 51 | if ($wpdb->query($wpdb->prepare($query, $this->getOptionName('post')))) { |
| 52 | foreach($wpdb->last_result as $row) { |
| 53 | $settings = maybe_unserialize($row->meta_value); |
| 54 | $this->pushOptions('post', $row->post_id . '|' . $row->post_type, $settings); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Override the frontend.list for current post |
| 59 | $post = AAM_Core_API::getCurrentPost(); // current post |
| 60 | if ($post) { |
| 61 | $option = $this->getOption(); |
| 62 | $option['post'][$post->ID . '|' . $post->post_type]['frontend.list'] = 0; |
| 63 | $this->setOption($option); |
| 64 | } |
| 65 | |
| 66 | do_action('aam-visibility-initialize-action', $this); |
| 67 | |
| 68 | // inherit settings from parent |
| 69 | $option = $subject->inheritFromParent('visibility', 0); |
| 70 | if (!empty($option)) { |
| 71 | $option = array_replace_recursive($option, $this->getOption()); |
| 72 | } else { |
| 73 | $option = $this->getOption(); |
| 74 | } |
| 75 | |
| 76 | if (in_array($subject::UID, array('user', 'visitor'))) { |
| 77 | // $subject->getObject('cache')->add( |
| 78 | // 'visibility', 0, empty($option) ? false : $option |
| 79 | // ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $this->setOption($option); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | * @param type $object |
| 89 | * @param type $id |
| 90 | * @param type $options |
| 91 | * @return type |
| 92 | */ |
| 93 | public function pushOptions($object, $id, $options) { |
| 94 | $filtered = array(); |
| 95 | $listOptions = apply_filters( |
| 96 | 'aam-post-list-options-filter', |
| 97 | array('frontend.list', 'backend.list', 'api.list') |
| 98 | ); |
| 99 | |
| 100 | foreach($options as $key => $value) { |
| 101 | if (in_array($key, $listOptions)) { |
| 102 | $filtered[$key] = $value; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (empty($filtered)) { |
| 107 | $filtered = array_combine( |
| 108 | $listOptions, |
| 109 | array_fill(0, count($listOptions), 0) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $option = $this->getOption(); |
| 114 | if (!isset($option[$object][$id])) { |
| 115 | $option[$object][$id] = $filtered; |
| 116 | } |
| 117 | $this->setOption($option); |
| 118 | |
| 119 | return $filtered; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * |
| 124 | * @param type $object |
| 125 | * @param type $id |
| 126 | * @return type |
| 127 | */ |
| 128 | public function has($object, $id = null) { |
| 129 | $option = $this->getOption(); |
| 130 | |
| 131 | return (is_null($id) ? isset($option[$object]) : isset($option[$object][$id])); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Generate option name |
| 136 | * |
| 137 | * @return string |
| 138 | * |
| 139 | * @access protected |
| 140 | */ |
| 141 | protected function getOptionName($object) { |
| 142 | $subject = $this->getSubject(); |
| 143 | |
| 144 | //prepare option name |
| 145 | $meta_key = 'aam-' . $object . '-access-' . $subject->getUID(); |
| 146 | $meta_key .= ($subject->getId() ? $subject->getId() : ''); |
| 147 | |
| 148 | return $meta_key; |
| 149 | } |
| 150 | |
| 151 | } |