Feature
9 years ago
View
9 years ago
phtml
9 years ago
Feature.php
9 years ago
Filter.php
9 years ago
Manager.php
9 years ago
View.php
9 years ago
View.php
261 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 view manager |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Backend_View { |
| 17 | |
| 18 | /** |
| 19 | * Instance of itself |
| 20 | * |
| 21 | * @var AAM_Backend_View |
| 22 | * |
| 23 | * @access private |
| 24 | */ |
| 25 | private static $_instance = null; |
| 26 | |
| 27 | /** |
| 28 | * Current Subject |
| 29 | * |
| 30 | * @var AAM_Core_Subject |
| 31 | * |
| 32 | * @access private |
| 33 | */ |
| 34 | private static $_subject = null; |
| 35 | |
| 36 | /** |
| 37 | * Construct the view object |
| 38 | * |
| 39 | * @return void |
| 40 | * |
| 41 | * @access protected |
| 42 | */ |
| 43 | protected function __construct() { |
| 44 | $classname = 'AAM_Core_Subject_' . ucfirst( |
| 45 | AAM_Core_Request::request('subject') |
| 46 | ); |
| 47 | if (class_exists($classname)) { |
| 48 | $this->setSubject(new $classname( |
| 49 | AAM_Core_Request::request('subjectId') |
| 50 | )); |
| 51 | } |
| 52 | |
| 53 | //register default features |
| 54 | AAM_Backend_Feature_Menu::register(); |
| 55 | AAM_Backend_Feature_Metabox::register(); |
| 56 | AAM_Backend_Feature_Capability::register(); |
| 57 | AAM_Backend_Feature_Post::register(); |
| 58 | AAM_Backend_Feature_Redirect::register(); |
| 59 | AAM_Backend_Feature_Teaser::register(); |
| 60 | AAM_Backend_Feature_LoginRedirect::register(); |
| 61 | AAM_Backend_Feature_Extension::register(); |
| 62 | AAM_Backend_Feature_Security::register(); |
| 63 | AAM_Backend_Feature_Utility::register(); |
| 64 | AAM_Backend_Feature_Contact::register(); |
| 65 | |
| 66 | //feature registration hook |
| 67 | do_action('aam-feature-registration'); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Run the Manager |
| 72 | * |
| 73 | * @return string |
| 74 | * |
| 75 | * @access public |
| 76 | */ |
| 77 | public function renderPage() { |
| 78 | ob_start(); |
| 79 | require_once(dirname(__FILE__) . '/phtml/index.phtml'); |
| 80 | $content = ob_get_contents(); |
| 81 | ob_end_clean(); |
| 82 | |
| 83 | return $content; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Run the Manager |
| 88 | * |
| 89 | * @return string |
| 90 | * |
| 91 | * @access public |
| 92 | */ |
| 93 | public function renderMetabox() { |
| 94 | global $post; |
| 95 | |
| 96 | if (is_a($post, 'WP_Post')) { |
| 97 | $url = admin_url('admin.php?page=aam&oid=' . $post->ID . '#post'); |
| 98 | |
| 99 | ob_start(); |
| 100 | require_once(dirname(__FILE__) . '/phtml/metabox.phtml'); |
| 101 | $content = ob_get_contents(); |
| 102 | ob_end_clean(); |
| 103 | } else { |
| 104 | $content = null; |
| 105 | } |
| 106 | |
| 107 | return $content; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Process the ajax call |
| 112 | * |
| 113 | * @return string |
| 114 | * |
| 115 | * @access public |
| 116 | */ |
| 117 | public function processAjax() { |
| 118 | $response = null; |
| 119 | |
| 120 | $action = AAM_Core_Request::request('sub_action'); |
| 121 | $parts = explode('.', $action); |
| 122 | |
| 123 | if (method_exists($this, $parts[0])) { |
| 124 | $response = call_user_func(array($this, $parts[0])); |
| 125 | } elseif (count($parts) == 2) { //cover the Model.method pattern |
| 126 | try { |
| 127 | $classname = 'AAM_Backend_Feature_' . $parts[0]; |
| 128 | if (class_exists($classname)) { |
| 129 | $response = call_user_func(array(new $classname, $parts[1])); |
| 130 | } |
| 131 | } catch (Exception $e) { |
| 132 | $response = $e->getMessage(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return apply_filters( |
| 137 | 'aam-ajax-filter', $response, $this->getSubject(), $action |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Render the Main Control Area |
| 143 | * |
| 144 | * @return void |
| 145 | * |
| 146 | * @access public |
| 147 | */ |
| 148 | public function renderContent() { |
| 149 | ob_start(); |
| 150 | require_once(dirname(__FILE__) . '/phtml/main-panel.phtml'); |
| 151 | $content = ob_get_contents(); |
| 152 | ob_end_clean(); |
| 153 | |
| 154 | return $content; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * |
| 159 | * @param type $partial |
| 160 | * @return type |
| 161 | */ |
| 162 | public function loadPartial($partial) { |
| 163 | ob_start(); |
| 164 | require_once(dirname(__FILE__) . '/phtml/partial/' . $partial); |
| 165 | $content = ob_get_contents(); |
| 166 | ob_end_clean(); |
| 167 | |
| 168 | return $content; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Save AAM options |
| 173 | * |
| 174 | * Important notice! This function excepts "value" to be only boolean value |
| 175 | * |
| 176 | * @return string |
| 177 | * |
| 178 | * @access public |
| 179 | */ |
| 180 | public function save() { |
| 181 | $object = trim(AAM_Core_Request::post('object')); |
| 182 | $objectId = intval(AAM_Core_Request::post('objectId', 0)); |
| 183 | |
| 184 | $param = AAM_Core_Request::post('param'); |
| 185 | $value = filter_var( |
| 186 | AAM_Core_Request::post('value'), FILTER_VALIDATE_BOOLEAN |
| 187 | ); |
| 188 | |
| 189 | $result = $this->getSubject()->save($param, $value, $object, $objectId); |
| 190 | |
| 191 | return json_encode(array('status' => ($result ? 'success' : 'failure'))); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * |
| 196 | * @return type |
| 197 | */ |
| 198 | public function switchToUser() { |
| 199 | $user = new WP_User(AAM_Core_Request::post('user')); |
| 200 | $max = AAM_Core_API::maxLevel(wp_get_current_user()->allcaps); |
| 201 | |
| 202 | if ($max >= AAM_Core_API::maxLevel($user->allcaps)) { |
| 203 | AAM_Core_API::updateOption( |
| 204 | 'aam-user-switch-' . $user->ID, get_current_user_id() |
| 205 | ); |
| 206 | |
| 207 | wp_clear_auth_cookie(); |
| 208 | wp_set_auth_cookie( $user->ID, true ); |
| 209 | wp_set_current_user( $user->ID ); |
| 210 | |
| 211 | $response = array('status' => 'success', 'redirect' => admin_url()); |
| 212 | } else { |
| 213 | $response = array( |
| 214 | 'status' => 'failure', |
| 215 | 'reason' => 'You are not allowed to switch to this user' |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | return json_encode($response); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get Subject |
| 224 | * |
| 225 | * @return AAM_Core_Subject |
| 226 | * |
| 227 | * @access public |
| 228 | */ |
| 229 | public static function getSubject() { |
| 230 | return self::$_subject; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Set Subject |
| 235 | * |
| 236 | * @param AAM_Core_Subject $subject |
| 237 | * |
| 238 | * @return void |
| 239 | * |
| 240 | * @access public |
| 241 | */ |
| 242 | protected function setSubject(AAM_Core_Subject $subject) { |
| 243 | self::$_subject = $subject; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get instance of itself |
| 248 | * |
| 249 | * @return AAM_Backend_View |
| 250 | * |
| 251 | * @access public |
| 252 | */ |
| 253 | public static function getInstance() { |
| 254 | if (is_null(self::$_instance)) { |
| 255 | self::$_instance = new self; |
| 256 | } |
| 257 | |
| 258 | return self::$_instance; |
| 259 | } |
| 260 | |
| 261 | } |