Habari\AdminHandler::act PHP Method

act() public method

Dispatches the request to the defined method. (ie: post_{page})
public act ( $action )
    public function act($action)
    {
        if (null === $this->handler_vars) {
            $this->handler_vars = new SuperGlobal(array());
        }
        $this->action = $action;
        $action_method = 'act_' . $action;
        $before_action_method = 'before_' . $action_method;
        $after_action_method = 'after_' . $action_method;
        if (method_exists($this, $before_action_method)) {
            $this->{$before_action_method}();
        }
        /**
         * Plugin action to allow plugins to execute before a certain
         * action is triggered
         *
         * @see ActionHandler::$action
         * @action before_act_{$action}
         */
        Plugins::act($before_action_method, $this);
        // The body of this function IS the action method, do it.
        // If I was smarter, I'd pass this code as a closure to the parent act() via a parameter for DRY, but I'm lazy, ha ha
        $page = $action == 'admin' ? $this->handler_vars['page'] : $action;
        $page = filter_var($page, FILTER_SANITIZE_STRING);
        if (isset($this->handler_vars['content_type'])) {
            $type_name = $this->handler_vars['content_type'];
            $type = Plugins::filter('post_type_display', Post::type_name($type_name), 'singular');
        } elseif ($page == 'publish' && isset($this->handler_vars['id'])) {
            $type_name = Post::type_name(Post::get(array('status' => Post::status('any'), 'id' => intval($this->handler_vars['id'])))->content_type);
            $type = Plugins::filter('post_type_display', $type_name, 'singular');
        } else {
            $type_name = '';
            $type = '';
        }
        $this->setup_admin_theme($page, $type);
        // Access check to see if the user is allowed the requested page
        Utils::check_request_method(array('GET', 'HEAD', 'POST'));
        if (!$this->access_allowed($page, $type_name)) {
            Session::error(_t('Access to that page has been denied by the administrator.'));
            $this->get_blank();
        }
        if (method_exists($this, $action_method)) {
            call_user_func(array($this, $action_method));
        } else {
            switch ($_SERVER['REQUEST_METHOD']) {
                case 'POST':
                    // Let plugins try to handle the page
                    Plugins::act('admin_theme_post_' . $page, $this, $this->theme);
                    // Handle POSTs to the admin pages
                    $fn = 'post_' . $page;
                    if (method_exists($this, $fn)) {
                        $this->{$fn}();
                    } else {
                        $classname = get_class($this);
                        _e('%1$s->%2$s() does not exist.', array($classname, $fn));
                        exit;
                    }
                    break;
                case 'GET':
                case 'HEAD':
                    // Let plugins try to handle the page
                    Plugins::act('admin_theme_get_' . $page, $this, $this->theme);
                    // Handle GETs of the admin pages
                    $fn = 'get_' . $page;
                    if (method_exists($this, $fn)) {
                        $this->{$fn}();
                        exit;
                    }
                    // If a get_ function doesn't exist, fail
                    if ($this->theme->template_exists($page)) {
                        Session::error(_t('There is no handler for this page. This is propably an error.'));
                        $this->get_blank();
                    } else {
                        // The requested console page doesn't exist
                        header('HTTP/1.1 404 Not Found', true, 404);
                        $this->get_blank(_t('The page you were looking for was not found.'));
                    }
                    break;
            }
        }
        /**
         * Plugin action to allow plugins to execute after a certain
         * action is triggered
         *
         * @see ActionHandler::$action
         * @action before_act_{$action}
         */
        Plugins::act($after_action_method);
        if (method_exists($this, $after_action_method)) {
            $this->{$after_action_method}();
        }
    }