admin.php
143 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 | /** |
| 4 | * Introduce special type for controllers which render pages inside admin area |
| 5 | * |
| 6 | * @author Maksym Tsypliakov <maksym.tsypliakov@gmail.com> |
| 7 | */ |
| 8 | abstract class PMXI_Controller_Admin extends PMXI_Controller { |
| 9 | /** |
| 10 | * Admin page base url (request url without all get parameters but `page`) |
| 11 | * @var string |
| 12 | */ |
| 13 | public $baseUrl; |
| 14 | /** |
| 15 | * Parameters which is left when baseUrl is detected |
| 16 | * @var array |
| 17 | */ |
| 18 | public $baseUrlParamNames = array('page', 'pagenum', 'order', 'order_by', 'type', 's', 'f'); |
| 19 | /** |
| 20 | * Whether controller is rendered inside wordpress page |
| 21 | * @var bool |
| 22 | */ |
| 23 | public $isInline = false; |
| 24 | /** |
| 25 | * Constructor |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 29 | $remove = array_diff(array_keys($_GET), $this->baseUrlParamNames); |
| 30 | |
| 31 | $p_url = wp_parse_url( site_url() ); |
| 32 | |
| 33 | $url = $p_url['scheme'] . '://' . $p_url['host']; |
| 34 | |
| 35 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 36 | if (!empty($_POST['is_settings_submitted'])) { // save settings form |
| 37 | $input = new PMXI_Input(); |
| 38 | $post = $input->post(array( |
| 39 | 'port' => '' |
| 40 | )); |
| 41 | PMXI_Plugin::getInstance()->updateOption($post); |
| 42 | } |
| 43 | |
| 44 | $port = PMXI_Plugin::getInstance()->getOption('port'); |
| 45 | |
| 46 | if ( ! empty($port) and is_numeric($port) ){ |
| 47 | $url .= ':' . $port; |
| 48 | } |
| 49 | else{ |
| 50 | $url = ( ! empty($p_url['port']) && ! in_array( $p_url['port'], array( 80, 443 ) ) ) ? $url . ':' . $p_url['port'] : $url; |
| 51 | } |
| 52 | |
| 53 | if ($remove) { |
| 54 | $this->baseUrl = $url . remove_query_arg($remove); |
| 55 | } else { |
| 56 | $this->baseUrl = $url . esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'] ?? '')); |
| 57 | } |
| 58 | |
| 59 | parent::__construct(); |
| 60 | |
| 61 | // enqueue required sripts and styles |
| 62 | global $wp_styles; |
| 63 | if ( ! is_a($wp_styles, 'WP_Styles')) |
| 64 | $wp_styles = new WP_Styles(); |
| 65 | |
| 66 | wp_enqueue_style('jquery-ui', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/redmond/jquery-ui.css', array(), PMXI_VERSION); |
| 67 | wp_enqueue_style('jquery-tipsy', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/smoothness/jquery.tipsy.css', array(), PMXI_VERSION); |
| 68 | wp_enqueue_style('pmxi-admin-style', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin.css', array(), PMXI_VERSION); |
| 69 | wp_enqueue_style('pmxi-admin-style-ie', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-ie.css', array(), PMXI_VERSION); |
| 70 | wp_enqueue_style('jquery-select2', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/select2/select2.css', array(), PMXI_VERSION); |
| 71 | wp_enqueue_style('jquery-select2-ddslick-compat', WP_ALL_IMPORT_ROOT_URL . '/static/css/select2-v4-ddslick-compat.css', array('jquery-select2'), PMXI_VERSION); |
| 72 | wp_enqueue_style('jquery-chosen', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/chosen/chosen.css', array(), PMXI_VERSION); |
| 73 | add_editor_style( WP_ALL_IMPORT_ROOT_URL . '/static/css/custom-editor-style.css' ); |
| 74 | |
| 75 | wp_enqueue_style('jquery-timepicker', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/css/timepicker/jquery.timepicker.css', array(), PMXI_VERSION); |
| 76 | |
| 77 | wp_enqueue_style('wp-pointer'); |
| 78 | |
| 79 | if ( version_compare(get_bloginfo('version'), '3.8-RC1') >= 0 ){ |
| 80 | wp_enqueue_style('pmxi-admin-style-wp-3.8', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-wp-3.8.css', array(), PMXI_VERSION); |
| 81 | } |
| 82 | if ( version_compare(get_bloginfo('version'), '4.0-beta3') >= 0 ){ |
| 83 | wp_enqueue_style('pmxi-admin-style-wp-4.0', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-wp-4.0.css', array(), PMXI_VERSION); |
| 84 | } |
| 85 | if ( version_compare(get_bloginfo('version'), '4.4') >= 0 ){ |
| 86 | wp_enqueue_style('pmxi-admin-style-wp-4.4', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-wp-4.4.css', array(), PMXI_VERSION); |
| 87 | } |
| 88 | |
| 89 | $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'; |
| 90 | if (is_file(PMXI_Plugin::ROOT_DIR . '/static/css/admin-colors-' . $scheme_color . '.css')) { |
| 91 | wp_enqueue_style('pmxi-admin-style-color', WP_ALL_IMPORT_ROOT_URL . '/static/css/admin-colors-' . $scheme_color . '.css', array(), PMXI_VERSION); |
| 92 | } |
| 93 | |
| 94 | wp_enqueue_script('jquery-ui-datepicker', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/ui.datepicker.js', array('jquery-ui-core'), PMXI_VERSION, true); |
| 95 | //wp_enqueue_script('wp-all-import-autocomplete', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/ui.autocomplete.js', array('jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position')); |
| 96 | wp_enqueue_script('tipsy', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.tipsy.js', array('jquery'), PMXI_VERSION, true); |
| 97 | 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'), PMXI_VERSION, true); |
| 98 | wp_enqueue_script('jquery-select2', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/select2.min.js', array('jquery'), PMXI_VERSION, true); |
| 99 | wp_enqueue_script('jquery-chosen', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/chosen.jquery.min.js', array('jquery'), PMXI_VERSION, true); |
| 100 | // ddslick has been replaced with select2 |
| 101 | wp_enqueue_script('jquery-contextmenu', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.ui-contextmenu.min.js', array('jquery', 'jquery-ui-menu'), PMXI_VERSION, true); |
| 102 | |
| 103 | add_action("admin_enqueue_scripts", [$this, 'add_admin_scripts']); |
| 104 | |
| 105 | wp_enqueue_script('jquery-timepicker', WP_ALL_IMPORT_ROOT_URL . '/static/js/jquery/jquery.timepicker.js', array('jquery'), PMXI_VERSION, true); |
| 106 | |
| 107 | wp_enqueue_script('wp-pointer'); |
| 108 | |
| 109 | /* load plupload scripts */ |
| 110 | wp_deregister_script('swfupload-all'); |
| 111 | wp_deregister_script('swfupload-handlers'); |
| 112 | wp_enqueue_script('swfupload-handlers', site_url() . "/wp-includes/js/swfupload/handlers.js", array('jquery'), '2201-20100523', true); |
| 113 | |
| 114 | wp_enqueue_script('jquery-plupload', WP_ALL_IMPORT_ROOT_URL . '/static/js/plupload/wplupload.js', array('plupload', 'jquery'), PMXI_VERSION, true); |
| 115 | |
| 116 | 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', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position', 'jquery-ui-autocomplete', 'wp-theme-plugin-editor'), PMXI_VERSION, true); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | public function add_admin_scripts() { |
| 121 | $cm_settings['codeEditor'] = wp_enqueue_code_editor(['type' => 'php']); |
| 122 | |
| 123 | // Use our modified function if user has disabled the syntax editor. |
| 124 | if(false === $cm_settings['codeEditor']){ |
| 125 | $cm_settings['codeEditor'] = wpai_wp_enqueue_code_editor(['type' => 'php']); |
| 126 | } |
| 127 | wp_localize_script('pmxi-admin-script', 'wpai_cm_settings', $cm_settings); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @see Controller::render() |
| 132 | */ |
| 133 | protected function render($viewPath = NULL) { |
| 134 | // assume template file name depending on calling function |
| 135 | if (is_null($viewPath)) { |
| 136 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 137 | $trace = debug_backtrace(); |
| 138 | $viewPath = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower($trace[1]['class']))) . '/' . $trace[1]['function']; |
| 139 | } |
| 140 | parent::render($viewPath); |
| 141 | } |
| 142 | |
| 143 | } |