private function get_plugin_path($plugin_name) { $plugin_underscored = MvcInflector::underscore($plugin_name); $plugin_hyphenized = str_replace('_', '-', $plugin_underscored); $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_hyphenized . '/'; return $plugin_path; }
private function dispatch($args) { MvcConfiguration::set('ExecutionContext', 'shell'); echo Console_Color::convert("\n%P%UWelcome to WP MVC Console!%n%n\n\n"); $shell_name = 'help'; if (!empty($args[1])) { $shell_name = $args[1]; } $shell_title = MvcInflector::camelize($shell_name); $shell_name .= '_shell'; $shell_class_name = MvcInflector::camelize($shell_name); $shell_path = 'shells/' . $shell_name . '.php'; $shell_exists = $this->file_includer->include_first_app_file_or_core_file($shell_path); if (!$shell_exists) { echo 'Sorry, a shell named "' . $shell_name . '" couldn\'t be found in any of the MVC plugins.'; echo "\n"; echo 'Please make sure a shell class exists in "app/' . $shell_path . '", or execute "./wpmvc" to see a list of available shells.'; echo "\n"; die; } $args = array_slice($args, 2); if (empty($args[0])) { $args = array('main'); } $shell = new $shell_class_name($args); $method = $args[0]; $args = array_slice($args, 1); if ($shell_name != 'help_shell') { $shell->out(Console_Color::convert("\n%_[Running " . $shell_title . "::" . $method . "]%n")); } $shell->{$method}($args); if ($shell_name != 'help_shell') { $shell->out(Console_Color::convert("\n%_[Complete]%n")); } }
function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $object_id = empty($options['id']) ? null : $options['id']; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $params = $_REQUEST; $params = self::escape_params($params); if (is_admin()) { unset($params['page']); } else { if (empty($params['id']) && !empty($object_id)) { $params['id'] = $object_id; } } $controller->params = $params; $controller->set('this', $controller); $controller->{$action}(); $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action); } }
protected function load_model($model_name) { $model_underscore = MvcInflector::underscore($model_name); $this->file_includer->require_first_app_file_or_core_file('models/' . $model_underscore . '.php'); if (class_exists($model_name)) { $this->{$model_name} = new $model_name(); } }
static function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } // If we can access the response from our controller's actions (methods) // we can use them (the actions) as Widgets in the both sides - outside of wp-mvc plugin and inside of it. // The action should return whatever you want, except $this->render_view('view_name'). // Example: Let's say we have 'UserController' and action 'list' and we want somewhere in Wordpress or another // plugin to get all users and to reuse UserController list method, so: // class UserController extends MvcPublicController { // public function list() { // $this->view_rendered = true; // $this->set_objects(); // $response = this->render_to_string('users/list'); // return $response; // } // } // and where we want to reuse it, we can get it in the following way: // $widget = MvcDispatcher::dispatch(array( // 'controller' => 'users', // 'action' => 'list' // )); $response = $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } return $response; }
static function add_object($key, &$object) { $_this =& MvcObjectRegistry::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__objects[$key])) { $_this->__objects[$key] =& $object; return true; } return false; }
public function add_settings($key, &$settings) { $_this =& self::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__settings[$key])) { $_this->__settings[$key] = $settings; return true; } return false; }
public function add_model($key, &$model) { $_this =& self::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__models[$key])) { $_this->__models[$key] = $model; return true; } return false; }
protected function init_settings() { if (empty($this->settings)) { $this->settings = array(); } $settings = array(); foreach ($this->settings as $key => $setting) { $defaults = array('key' => $key, 'type' => 'text', 'label' => MvcInflector::titleize($key), 'value' => null, 'value_method' => null, 'default' => null, 'default_method' => null, 'options' => null, 'options_method' => null); $setting = array_merge($defaults, $setting); $settings[$key] = $setting; } $this->settings = $settings; }
public function delete() { $this->verify_id_param(); $this->set_object(); if (!empty($this->object)) { $this->model->delete($this->params['id']); $this->flash('notice', 'Successfully deleted!'); } else { $this->flash('warning', 'A ' . MvcInflector::humanize($this->model->name) . ' with ID "' . $this->params['id'] . '" couldn\'t be found.'); } $url = MvcRouter::admin_url(array('controller' => $this->name, 'action' => 'index')); $this->redirect($url); }
function mvc_render_to_string($view, $vars = array()) { $view_pieces = explode('/', $view); $model_tableized = $view_pieces[0]; $model_camelized = MvcInflector::camelize($model_tableized); $controller_name = $model_camelized . 'Controller'; if (!class_exists($controller_name)) { $controller_name = 'MvcPublicController'; } $controller = new $controller_name(); $controller->init(); $controller->set($vars); $string = $controller->render_to_string($view); return $string; }
private function dispatch($args) { MvcConfiguration::set('ExecutionContext', 'shell'); if (empty($args[1])) { MvcError::fatal('Please provide the name of the shell as the first argument.'); } $shell_name = $args[1]; $shell_name .= '_shell'; $shell_class_name = MvcInflector::camelize($shell_name); $this->file_includer->require_first_app_file_or_core_file('shells/' . $shell_name . '.php'); $args = array_slice($args, 2); if (empty($args[0])) { $args = array('main'); } $shell = new $shell_class_name($args); $method = $args[0]; $args = array_slice($args, 1); $shell->{$method}($args); }
/** * Get a list of the available shells. * Also is executed if the wpmvc console is run with no arguments. * * @param mixed $args */ public function main($args) { $shells = $this->get_available_shells(); $this->out('Available Shells:'); $table = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, ' ', 1, null, true); foreach ($shells as $plugin => $shells) { $plugin_label = MvcInflector::camelize(MvcInflector::underscore($plugin)); for ($i = 0; $i < count($shells); $i++) { if ($i > 0) { $plugin_label = ' '; } $shell_name = MvcInflector::camelize($shells[$i]); $table->addRow(array($plugin_label, Console_Color::convert('%_' . $shell_name . '%n'))); } $table->addSeparator(); } $this->out($table->getTable()); $this->out('To get information about a shell try:'); $this->out("\n\twpmvc help shell <name_of_shell>"); }
public function set_wp_title($original_title) { $separator = ' | '; $controller_name = MvcInflector::titleize($this->name); $object_name = null; $object = null; if ($this->action) { if ($this->action == 'show' && is_object($this->object)) { $object = $this->object; if (!empty($this->object->__name)) { $object_name = $this->object->__name; } } } $pieces = array($object_name, $controller_name); $pieces = array_filter($pieces); $title = implode($separator, $pieces); $title = $title . $separator; $title_options = apply_filters('mvc_page_title', array('controller' => $controller_name, 'action' => $this->action, 'object_name' => $object_name, 'object' => $object, 'title' => $title)); $title = $title_options['title']; return $title; }
function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } }
public function __get($property_name) { if (!empty($this->__settings['properties'][$property_name])) { $property = $this->__settings['properties'][$property_name]; if ($property['type'] == 'association') { $objects = $this->get_associated_objects($property['association']); $this->{$property_name} = $objects; return $this->{$property_name}; } } if ($this->__settings['model']['name'] == 'MvcPost') { if (!empty($this->post_type)) { if (substr($this->post_type, 0, 4) == 'mvc_') { $model_name = MvcInflector::camelize(substr($this->post_type, 4)); $model = MvcModelRegistry::get_model($model_name); $object = $model->find_one(array('post_id' => $this->ID, 'recursive' => 0)); $this->{$property_name} = $object; return $this->{$property_name}; } } } $class = empty($this->__model_name) ? 'MvcModelObject' : $this->__model_name; MvcError::warning('Undefined property: ' . $class . '::' . $property_name . '.'); }
<?php $formatted_time = preg_replace('/:00$/', '', $object->time); ?> <h2><?php echo MvcInflector::titleize($this->action); ?> <?php echo MvcInflector::titleize($model->name); ?> </h2> <?php echo $this->form->create($model->name); echo $this->form->belongs_to_dropdown('Venue', $venues, array('style' => 'width: 200px;', 'empty' => true)); echo $this->form->input('date', array('label' => 'Date (YYYY-MM-DD)')); echo $this->form->input('time', array('label' => 'Time (24-hour clock)', 'value' => $formatted_time)); echo $this->form->input('description'); echo $this->form->has_many_dropdown('Speaker', $speakers, array('style' => 'width: 200px;', 'empty' => true)); echo $this->form->input('is_public'); echo $this->form->end('Update');
public function add_settings_pages() { $this->init_settings(); foreach ($this->settings as $settings_name => $settings) { $title = MvcInflector::titleize($settings_name); $title = str_replace(' Settings', '', $title); $instance = MvcSettingsRegistry::get_settings($settings_name); add_options_page($title, $title, 'manage_options', $instance->key, array($instance, 'page')); } }
private function process_message($message, $field) { $titleized_field = MvcInflector::titleize($field); $message = str_replace('{field}', $titleized_field, $message); return $message; }
public function admin_page_param($options = array()) { if (is_string($options)) { $options = array('model' => $options); } if (!empty($options['model'])) { return 'mvc_' . MvcInflector::tableize($options['model']); } return false; }
protected function init_default_columns() { if (empty($this->default_columns)) { MvcError::fatal('No columns defined for this view. Please define them in the controller, like this: <pre> class ' . MvcInflector::camelize($this->name) . 'Controller extends MvcAdminController { var $default_columns = array(\'id\', \'name\'); } </pre>'); } $admin_columns = array(); foreach ($this->default_columns as $key => $value) { if (is_array($value)) { if (!isset($value['label'])) { $value['label'] = MvcInflector::titleize($key); } } else { if (is_integer($key)) { $key = $value; if ($value == 'id') { $value = array('label' => 'ID'); } else { $value = array('label' => MvcInflector::titleize($value)); } } else { $value = array('label' => $value); } } $value['key'] = $key; $admin_columns[$key] = $value; } $this->default_columns = $admin_columns; }
public function admin_actions_cell($model, $object) { $links = array(); $object_name = empty($object->__name) ? 'Item #' . $object->__id : $object->__name; $encoded_object_name = $this->esc_attr($object_name); $controller = MvcInflector::tableize($model->name); $links[] = '<a href="' . MvcRouter::admin_url(array('controller' => $controller, 'action' => 'edit', 'object' => $object)) . '" title="Edit ' . $encoded_object_name . '">Edit</a>'; $links[] = '<a href="' . MvcRouter::public_url(array('controller' => $controller, 'action' => 'show', 'object' => $object)) . '" title="View ' . $encoded_object_name . '">View</a>'; $links[] = '<a href="' . MvcRouter::admin_url(array('controller' => $controller, 'action' => 'delete', 'object' => $object)) . '" title="Delete ' . $encoded_object_name . '" onclick="return confirm('Are you sure you want to delete ' . $encoded_object_name . '?');">Delete</a>'; $html = implode(' | ', $links); return '<td>' . $html . '</td>'; }
<h2><?php echo MvcInflector::pluralize_titleize($model->name); ?> </h2> <form id="posts-filter" action="<?php echo MvcRouter::admin_url(); ?> " method="get"> <p class="search-box"> <label class="screen-reader-text" for="post-search-input">Search:</label> <input type="hidden" name="page" value="<?php echo MvcRouter::admin_page_param($model->name); ?> " /> <input type="text" name="q" value="<?php echo empty($params['q']) ? '' : $params['q']; ?> " /> <input type="submit" value="Search" class="button" /> </p> </form> <div class="tablenav"> <div class="tablenav-pages"> <?php echo paginate_links($pagination);
public function add_menu_pages() { global $_registered_pages; $menu_position = 12; $menu_position = apply_filters('mvc_menu_position', $menu_position); foreach ($this->model_names as $model_name) { $model = MvcModelRegistry::get_model($model_name); if (!$model->hide_menu) { $tableized = MvcInflector::tableize($model_name); $pluralized = MvcInflector::pluralize($model_name); $titleized = MvcInflector::titleize($model_name); $pluralize_titleized = MvcInflector::pluralize_titleize($model_name); $controller_name = 'admin_' . $tableized; $top_level_handle = 'mvc_' . $tableized; $admin_pages = $model->admin_pages; $method = $controller_name . '_index'; $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "index"));'); add_menu_page($pluralize_titleized, $pluralize_titleized, 'administrator', $top_level_handle, array($this->dispatcher, $method), null, $menu_position); foreach ($admin_pages as $key => $admin_page) { $method = $controller_name . '_' . $admin_page['action']; if (!method_exists($this->dispatcher, $method)) { $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "' . $admin_page['action'] . '"));'); } $page_handle = $top_level_handle . '-' . $key; if ($admin_page['in_menu']) { add_submenu_page($top_level_handle, $admin_page['label'] . ' ‹ ' . $pluralize_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++; } } }
protected function init_properties() { $this->properties = array(); foreach ($this->associations as $association_name => $association) { $property_name = null; if ($association['type'] == 'belongs_to') { $property_name = MvcInflector::underscore($association_name); } else { if (in_array($association['type'], array('has_many', 'has_and_belongs_to_many'))) { $property_name = MvcInflector::tableize($association_name); } } if ($property_name) { $this->properties[$property_name] = array('type' => 'association', 'association' => $association); } } }
public function init_settings_defaults($file_path) { $app_directory = dirname($file_path) . '/app/'; $settings_directory = $app_directory . 'settings/'; $settings_files = $this->file_includer->require_php_files_in_directory($settings_directory); if (!empty($settings_files)) { foreach ($settings_files as $file_path) { $settings_class = MvcInflector::class_name_from_filename(basename($file_path)); $settings = new $settings_class(); $existing_values = get_option($settings->key); if (!$existing_values) { $values = array(); foreach ($settings->settings as $key => $setting) { $value = null; if (!empty($setting['default'])) { $value = $setting['default']; } else { if (!empty($setting['default_method'])) { $value = $this->{$setting['default_method']}(); } } $values[$key] = $value; } update_option($settings->key, $values); } } } }
protected function load_settings() { $settings_names = array(); foreach ($this->plugin_app_paths as $plugin_app_path) { $settings_filenames = $this->file_includer->require_php_files_in_directory($plugin_app_path . 'settings/'); foreach ($settings_filenames as $filename) { $settings_names[] = MvcInflector::class_name_from_filename($filename); } } $this->settings_names = $settings_names; }
private function input_name($field_name) { return 'data[' . $this->model_name . '][' . MvcInflector::underscore($field_name) . ']'; }
public static function pluralize_titleize($string) { $string = MvcInflector::pluralize(MvcInflector::titleize($string)); return $string; }
protected function init_associations() { if (!empty($this->belongs_to)) { foreach ($this->belongs_to as $key => $value) { $config = null; if (is_string($value)) { $association = $value; $config = array('type' => 'belongs_to', 'name' => $association, 'class' => $association, 'foreign_key' => MvcInflector::underscore($association) . '_id', 'includes' => null); } else { if (is_string($key) && is_array($value)) { $association = $key; $config = array('type' => 'belongs_to', 'name' => empty($value['name']) ? $association : $value['name'], 'class' => empty($value['class']) ? $association : $value['class'], 'foreign_key' => empty($value['foreign_key']) ? MvcInflector::underscore($association) . '_id' : $value['foreign_key'], 'includes' => null); } } if (!empty($config)) { if (!is_array($this->associations)) { $this->associations = array(); } $this->associations[$association] = $config; } } } if (!empty($this->has_many)) { foreach ($this->has_many as $association) { if (is_string($association)) { if (!is_array($this->associations)) { $this->associations = array(); } $config = array('type' => 'has_many', 'name' => $association, 'class' => $association, 'foreign_key' => MvcInflector::underscore($this->name) . '_id', 'includes' => null); $this->associations[$association] = $config; } } } if (!empty($this->has_and_belongs_to_many)) { foreach ($this->has_and_belongs_to_many as $association_name => $association) { if (!is_array($this->associations)) { $this->associations = array(); } if (isset($association['fields'])) { foreach ($association['fields'] as $key => $field) { $association['fields'][$key] = $association_name . '.' . $field; } } $config = array('type' => 'has_and_belongs_to_many', 'name' => $association_name, 'class' => $association_name, 'foreign_key' => isset($association['foreign_key']) ? $association['foreign_key'] : MvcInflector::underscore($this->name) . '_id', 'association_foreign_key' => isset($association['association_foreign_key']) ? $association['association_foreign_key'] : MvcInflector::underscore($association_name) . '_id', 'join_table' => $this->process_table_name($association['join_table']), 'fields' => isset($association['fields']) ? $association['fields'] : null, 'includes' => isset($association['includes']) ? $association['includes'] : null); $this->associations[$association_name] = $config; } } }