public function add_menu_pages() { global $_registered_pages; $menu_position = 12; $menu_position = apply_filters('mvc_menu_position', $menu_position); $admin_pages = MvcConfiguration::get('AdminPages'); foreach ($this->admin_controller_names as $controller_name) { if (isset($admin_pages[$controller_name])) { if (empty($admin_pages[$controller_name]) || !$admin_pages[$controller_name]) { continue; } $pages = $admin_pages[$controller_name]; } else { $pages = null; } $processed_pages = $this->process_admin_pages($controller_name, $pages); $hide_menu = isset($pages['hide_menu']) ? $pages['hide_menu'] : false; if (!$hide_menu) { $menu_icon = 'dashicons-admin-generic'; /* check if there is a corresponding model with a menu_icon post type argument */ try { $model_name = MvcInflector::singularize(MvcInflector::camelize($controller_name)); $model = mvc_model($model_name); if (isset($model->wp_post['post_type']['args']['menu_icon'])) { $menu_icon = $model->wp_post['post_type']['args']['menu_icon']; } } catch (Exception $e) { //not every controller must have a corresponding model, continue silently } $controller_titleized = MvcInflector::titleize($controller_name); $admin_controller_name = 'admin_' . $controller_name; $top_level_handle = 'mvc_' . $controller_name; $method = $admin_controller_name . '_index'; $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $admin_controller_name . '", "action" => "index"));'); $capability = $this->admin_controller_capabilities[$controller_name]; add_menu_page($controller_titleized, $controller_titleized, $capability, $top_level_handle, array($this->dispatcher, $method), $menu_icon, $menu_position); foreach ($processed_pages as $key => $admin_page) { $method = $admin_controller_name . '_' . $admin_page['action']; if (!method_exists($this->dispatcher, $method)) { $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $admin_controller_name . '", "action" => "' . $admin_page['action'] . '"));'); } $page_handle = $top_level_handle . '-' . $key; $parent_slug = empty($admin_page['parent_slug']) ? $top_level_handle : $admin_page['parent_slug']; if ($admin_page['in_menu']) { add_submenu_page($parent_slug, $admin_page['label'] . ' ‹ ' . $controller_titleized, $admin_page['label'], $admin_page['capability'], $page_handle, array($this->dispatcher, $method)); } else { // It looks like there isn't a more native way of creating an admin page without // having it show up in the menu, but if there is, it should be implemented here. // To do: set up capability handling and page title handling for these pages that aren't in the menu $hookname = get_plugin_page_hookname($page_handle, ''); if (!empty($hookname)) { add_action($hookname, array($this->dispatcher, $method)); } $_registered_pages[$hookname] = true; } } $menu_position++; } } }
public function public_url($options = array()) { $routes = self::get_public_routes(); $controller = $options['controller']; $action = empty($options['action']) ? 'index' : $options['action']; $matched_route = null; if (!empty($options['object']) && is_object($options['object'])) { $model_name = MvcInflector::camelize(MvcInflector::singularize($controller)); $model = MvcModelRegistry::get_model($model_name); if (!empty($model) && method_exists($model, 'to_url')) { $url = site_url('/'); $url .= $model->to_url($options['object']); return $url; } if (empty($options['id']) && !empty($options['object']->__id)) { $options['id'] = $options['object']->__id; } } foreach ($routes as $route) { $route_path = $route[0]; $route_defaults = $route[1]; if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) { if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) { $matched_route = $route; } } } $url = site_url('/'); if ($matched_route) { $path_pattern = $matched_route[0]; preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER); $path = $path_pattern; foreach ($matches as $match) { $pattern = $match[0]; $option_key = $match[1]; if (isset($options[$option_key])) { $value = $options[$option_key]; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); } } $path = rtrim($path, '/') . '/'; $url .= $path; } else { $url .= $options['controller'] . '/'; if (!empty($options['action']) && $options['action'] != 'show') { $url .= $options['action'] . '/'; } if (!empty($options['id'])) { $url .= $options['id'] . '/'; } } return $url; }
private function set_meta() { $model = get_class($this); $model = preg_replace('/Controller$/', '', $model); $this->name = MvcInflector::underscore($model); $this->views_path = ''; if (preg_match('/^Admin[A-Z]/', $model)) { $this->views_path = 'admin/'; $model = preg_replace('/^Admin/', '', $model); } $model = MvcInflector::singularize($model); $this->views_path .= MvcInflector::tableize($model) . '/'; $this->model_name = $model; // To do: remove the necessity of this redundancy if (class_exists($model)) { $model_instance = new $model(); $this->model = $model_instance; $this->{$model} = $model_instance; } }
public function public_url($options = array()) { $options = apply_filters('mvc_before_public_url', $options); $defaults = array('action' => 'index', 'controller' => null); $options = array_merge($defaults, $options); $routes = self::get_public_routes(); $controller = $options['controller']; $action = $options['action']; $matched_route = null; if (!empty($options['object']) && is_object($options['object'])) { if (!empty($options['object']->__model_name) && !$controller) { $model_name = $options['object']->__model_name; $controller = MvcInflector::tableize($model_name); } else { if ($controller) { $model_name = MvcInflector::camelize(MvcInflector::singularize($controller)); } else { MvcError::warning('Incomplete arguments for MvcRouter::public_url().'); } } $model = MvcModelRegistry::get_model($model_name); if (!empty($model) && method_exists($model, 'to_url')) { $url = site_url('/'); $method = new ReflectionMethod(get_class($model), 'to_url'); $parameter_count = $method->getNumberOfParameters(); if ($parameter_count == 2) { $url .= $model->to_url($options['object'], $options); } else { $url .= $model->to_url($options['object']); } return $url; } if (empty($options['id']) && !empty($options['object']->__id)) { $options['id'] = $options['object']->__id; } } foreach ($routes as $route) { $route_path = $route[0]; $route_defaults = $route[1]; if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) { if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) { $matched_route = $route; } } } $url = site_url('/'); if ($matched_route) { $path_pattern = $matched_route[0]; preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER); $path = $path_pattern; foreach ($matches as $match) { $pattern = $match[0]; $option_key = $match[1]; if (isset($options[$option_key])) { $value = $options[$option_key]; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); } else { if (isset($options['object']) && is_object($options['object'])) { if (isset($options['object']->{$option_key})) { $value = $options['object']->{$option_key}; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); $path = rtrim($path, '.*?'); } } } } $path = rtrim($path, '/') . '/'; $url .= $path; } else { $url .= $controller . '/'; if (!empty($action) && !in_array($action, array('show', 'index'))) { $url .= $action . '/'; } if (!empty($options['id'])) { $url .= $options['id'] . '/'; } } return $url; }