admin.php
135 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Introduce special type for controllers which render pages inside admin area |
| 4 | * |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | */ |
| 7 | abstract class PMXI_Controller_Admin extends PMXI_Controller { |
| 8 | /** |
| 9 | * Admin page base url (request url without all get parameters but `page`) |
| 10 | * @var string |
| 11 | */ |
| 12 | public $baseUrl; |
| 13 | /** |
| 14 | * Parameters which is left when baseUrl is detected |
| 15 | * @var array |
| 16 | */ |
| 17 | public $baseUrlParamNames = array('page', 'pagenum', 'order', 'order_by', 'type', 's', 'f'); |
| 18 | /** |
| 19 | * Whether controller is rendered inside wordpress page |
| 20 | * @var bool |
| 21 | */ |
| 22 | public $isInline = false; |
| 23 | /** |
| 24 | * Constructor |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $remove = array_diff(array_keys($_GET), $this->baseUrlParamNames); |
| 28 | |
| 29 | $p_url = parse_url( site_url() ); |
| 30 | |
| 31 | $url = $p_url['scheme'] . '://' . $p_url['host']; |
| 32 | |
| 33 | if (!empty($_POST['is_settings_submitted'])) { // save settings form |
| 34 | $post = array( |
| 35 | 'port' => $_POST['port'] |
| 36 | ); |
| 37 | PMXI_Plugin::getInstance()->updateOption($post); |
| 38 | } |
| 39 | |
| 40 | $port = PMXI_Plugin::getInstance()->getOption('port'); |
| 41 | |
| 42 | if (!empty($port) and is_numeric($port)) $url .= ':' . $port; |
| 43 | |
| 44 | if ($remove) { |
| 45 | $this->baseUrl = $url . remove_query_arg($remove); |
| 46 | } else { |
| 47 | $this->baseUrl = $url . $_SERVER['REQUEST_URI']; |
| 48 | } |
| 49 | |
| 50 | parent::__construct(); |
| 51 | |
| 52 | // add special filter for url fields |
| 53 | $this->input->addFilter(create_function('$str', 'return "http://" == $str || "ftp://" == $str ? "" : $str;')); |
| 54 | |
| 55 | // enqueue required sripts and styles |
| 56 | global $wp_styles; |
| 57 | if ( ! is_a($wp_styles, 'WP_Styles')) |
| 58 | $wp_styles = new WP_Styles(); |
| 59 | |
| 60 | wp_enqueue_style('jquery-ui', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/redmond/jquery-ui.css'); |
| 61 | wp_enqueue_style('jquery-tipsy', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/smoothness/jquery.tipsy.css'); |
| 62 | wp_enqueue_style('pmxi-admin-style', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin.css'); |
| 63 | wp_enqueue_style('pmxi-admin-style-ie', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-ie.css'); |
| 64 | wp_enqueue_style('jquery-select2', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/select2/select2.css'); |
| 65 | wp_enqueue_style('jquery-select2', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/select2/select2-bootstrap.css'); |
| 66 | add_editor_style( WP_ALL_IMPORT_ROOT_URL . '/static/css/custom-editor-style.css' ); |
| 67 | |
| 68 | $wp_styles->add_data('pmxi-admin-style-ie', 'conditional', 'lte IE 7'); |
| 69 | wp_enqueue_style('wp-pointer'); |
| 70 | |
| 71 | if ( version_compare(get_bloginfo('version'), '3.8-RC1') >= 0 ){ |
| 72 | wp_enqueue_style('pmxi-admin-style-wp-3.8', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-wp-3.8.css'); |
| 73 | } |
| 74 | if ( version_compare(get_bloginfo('version'), '4.0-beta3') >= 0 ){ |
| 75 | wp_enqueue_style('pmxi-admin-style-wp-3.8', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-wp-4.0.css'); |
| 76 | } |
| 77 | |
| 78 | $scheme_color = get_user_option('admin_color') and is_file(PMXI_Plugin::ROOT_DIR . '/static/css/admin-colors-' . $scheme_color . '.css') or $scheme_color = 'fresh'; |
| 79 | if (is_file(PMXI_Plugin::ROOT_DIR . '/static/css/admin-colors-' . $scheme_color . '.css')) { |
| 80 | wp_enqueue_style('pmxi-admin-style-color', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-colors-' . $scheme_color . '.css'); |
| 81 | } |
| 82 | |
| 83 | wp_enqueue_script('jquery-ui-datepicker', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/ui.datepicker.js', 'jquery-ui-core'); |
| 84 | wp_enqueue_script('jquery-ui-autocomplete', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/ui.autocomplete.js', array('jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position')); |
| 85 | wp_enqueue_script('jquery-tipsy', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.tipsy.js', 'jquery'); |
| 86 | wp_enqueue_script('jquery-nestable', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.mjs.nestedSortable.js', array('jquery', 'jquery-ui-dialog', 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-tabs', 'jquery-ui-progressbar')); |
| 87 | wp_enqueue_script('jquery-moment', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/moment.js', 'jquery'); |
| 88 | wp_enqueue_script('jquery-select2', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/select2.min.js', 'jquery'); |
| 89 | wp_enqueue_script('jquery-ddslick', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.ddslick.min.js', 'jquery'); |
| 90 | wp_enqueue_script('jquery-contextmenu', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.ui-contextmenu.min.js', array('jquery', 'jquery-ui-menu')); |
| 91 | wp_enqueue_script('wp-pointer'); |
| 92 | |
| 93 | /* load plupload scripts */ |
| 94 | wp_deregister_script('swfupload-all'); |
| 95 | wp_deregister_script('swfupload-handlers'); |
| 96 | wp_enqueue_script('swfupload-handlers', site_url() . "/wp-includes/js/swfupload/handlers.js", array('jquery'), '2201-20100523'); |
| 97 | |
| 98 | wp_enqueue_script('jquery-browserplus-min', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/browserplus-min.js', array('jquery')); |
| 99 | wp_enqueue_script('full-plupload', WP_ALL_IMPORT_ROOT_URL . '/static/js/plupload/plupload.full.js', array('jquery-browserplus-min')); |
| 100 | wp_enqueue_script('jquery-plupload', WP_ALL_IMPORT_ROOT_URL . '/static/js/plupload/wplupload.js', array('full-plupload', 'jquery')); |
| 101 | |
| 102 | wp_enqueue_script('pmxi-admin-script', WP_ALL_IMPORT_ROOT_URL . '/static/js/admin.js', array('jquery', 'jquery-ui-dialog', 'jquery-ui-datepicker', 'jquery-ui-draggable', 'jquery-ui-droppable')); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @see Controller::render() |
| 108 | */ |
| 109 | protected function render($viewPath = NULL) |
| 110 | { |
| 111 | // assume template file name depending on calling function |
| 112 | if (is_null($viewPath)) { |
| 113 | $trace = debug_backtrace(); |
| 114 | $viewPath = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower($trace[1]['class']))) . '/' . $trace[1]['function']; |
| 115 | } |
| 116 | |
| 117 | // render contextual help automatically |
| 118 | $viewHelpPath = $viewPath; |
| 119 | // append file extension if not specified |
| 120 | if ( ! preg_match('%\.php$%', $viewHelpPath)) { |
| 121 | $viewHelpPath .= '.php'; |
| 122 | } |
| 123 | $viewHelpPath = preg_replace('%\.php$%', '-help.php', $viewHelpPath); |
| 124 | $fileHelpPath = PMXI_Plugin::ROOT_DIR . '/views/' . $viewHelpPath; |
| 125 | |
| 126 | if (is_file($fileHelpPath)) { // there is help file defined |
| 127 | ob_start(); |
| 128 | include $fileHelpPath; |
| 129 | add_contextual_help(PMXI_Plugin::getInstance()->getAdminCurrentScreen()->id, ob_get_clean()); |
| 130 | } |
| 131 | |
| 132 | parent::render($viewPath); |
| 133 | } |
| 134 | |
| 135 | } |