font
8 years ago
img
9 years ago
README.md
8 years ago
contributors.txt
8 years ago
notice.css
8 years ago
notice.js
8 years ago
plugin-ui.php
8 years ago
wdev-ui.css
8 years ago
wdev-ui.js
8 years ago
plugin-ui.php
315 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Provides PHP support for simple use of the WPMUDEV plugin UI. |
| 4 | * |
| 5 | * @package WPMUDEV_UI |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'WDEV_Plugin_Ui' ) ) { |
| 9 | |
| 10 | /** |
| 11 | * UI class that encapsulates all module functions. |
| 12 | */ |
| 13 | class WDEV_Plugin_Ui { |
| 14 | |
| 15 | /** |
| 16 | * Current module version. |
| 17 | */ |
| 18 | const VERSION = '1.0'; |
| 19 | |
| 20 | /** |
| 21 | * Internal translation container. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | static protected $i10n = array(); |
| 26 | |
| 27 | /** |
| 28 | * Internal storage that holds additional classes for body tag. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | static protected $body_class = ''; |
| 33 | |
| 34 | /** |
| 35 | * URL to this module (directory). Used to enqueue the css/js files. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | static protected $module_url = ''; |
| 40 | |
| 41 | /** |
| 42 | * Initializes all UI components. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @internal |
| 46 | */ |
| 47 | static public function reset() { |
| 48 | self::$i10n = array( |
| 49 | 'empty_search' => __( 'Nothing found', 'wpmudev' ), |
| 50 | 'default_msg_ok' => __( 'Okay, we saved your changes!', 'wpmudev' ), |
| 51 | 'default_msg_err' => __( 'Oops, we could not do this...', 'wpmudev' ), |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Enqueues the CSS and JS files needed for plugin UI |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | * @api Call this function before/in `admin_head`. |
| 60 | * @param string $module_url URL to this module (directory). |
| 61 | * @param string $body_class List of additional classes for the body tag. |
| 62 | */ |
| 63 | static public function load( $module_url, $body_class = '' ) { |
| 64 | self::$module_url = trailingslashit( $module_url ); |
| 65 | self::$body_class = trim( $body_class ); |
| 66 | add_filter( |
| 67 | 'admin_body_class', |
| 68 | array( __CLASS__, 'admin_body_class' ) |
| 69 | ); |
| 70 | |
| 71 | if ( ! did_action( 'admin_enqueue_scripts' ) ) { |
| 72 | add_action( |
| 73 | 'admin_enqueue_scripts', |
| 74 | array( __CLASS__, 'enqueue_early' ) |
| 75 | ); |
| 76 | add_action( |
| 77 | 'admin_enqueue_scripts', |
| 78 | array( __CLASS__, 'enqueue_late' ), |
| 79 | 999 |
| 80 | ); |
| 81 | } else { |
| 82 | self::enqueue(); |
| 83 | self::enqueue_late(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Enqueues the CSS and JS files. |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * @internal Do not call this method manually. It's called by `load()`! |
| 92 | */ |
| 93 | static public function enqueue() { |
| 94 | /* |
| 95 | * Beta-testers will not have cached scripts! |
| 96 | * Just in case we have to update the plugin prior to launch. |
| 97 | */ |
| 98 | if ( defined( 'WPMUDEV_BETATEST' ) && WPMUDEV_BETATEST ) { |
| 99 | $script_version = time(); |
| 100 | } else { |
| 101 | $script_version = self::VERSION; |
| 102 | } |
| 103 | |
| 104 | wp_enqueue_style( |
| 105 | 'wdev-plugin-google_fonts', |
| 106 | 'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Roboto:400,500,300,300italic', |
| 107 | false, |
| 108 | $script_version |
| 109 | ); |
| 110 | |
| 111 | wp_enqueue_style( |
| 112 | 'wdev-plugin-notice', |
| 113 | self::$module_url . 'notice.css', |
| 114 | array( 'wdev-plugin-google_fonts' ), |
| 115 | $script_version |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Enqueues the CSS and JS files after all other files. |
| 121 | * |
| 122 | * @since 1.0.0 |
| 123 | * @internal Do not call this method manually. It's called by `load()`! |
| 124 | */ |
| 125 | static public function enqueue_late() { |
| 126 | /* |
| 127 | * Beta-testers will not have cached scripts! |
| 128 | * Just in case we have to update the plugin prior to launch. |
| 129 | */ |
| 130 | if ( defined( 'WPMUDEV_BETATEST' ) && WPMUDEV_BETATEST ) { |
| 131 | $script_version = time(); |
| 132 | } else { |
| 133 | $script_version = self::VERSION; |
| 134 | } |
| 135 | |
| 136 | wp_enqueue_style( |
| 137 | 'wdev-plugin-ui', |
| 138 | self::$module_url . 'wdev-ui.css', |
| 139 | array( 'wdev-plugin-google_fonts' ), |
| 140 | $script_version |
| 141 | ); |
| 142 | |
| 143 | wp_enqueue_script( |
| 144 | 'wdev-plugin-ui', |
| 145 | self::$module_url . 'wdev-ui.js', |
| 146 | array( 'jquery' ), |
| 147 | $script_version |
| 148 | ); |
| 149 | |
| 150 | /** |
| 151 | * Allow other plugins to enqueue css/js after shared-ui to |
| 152 | * overwrite certain definitions. |
| 153 | */ |
| 154 | do_action( 'wpmudev_plugin_ui_enqueued' ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Adds the page-specific class to the admin page body tag. |
| 159 | * |
| 160 | * @since 1.0.0 |
| 161 | * @internal Action hook |
| 162 | * @param string $classes List of CSS classes of the body tag. |
| 163 | * @return string Updated list of CSS classes. |
| 164 | */ |
| 165 | static public function admin_body_class( $classes ) { |
| 166 | $classes .= ' wpmud'; |
| 167 | if ( self::$body_class ) { |
| 168 | $classes .= ' ' . self::$body_class; |
| 169 | } |
| 170 | $classes .= ' '; |
| 171 | |
| 172 | return $classes; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Sets a translation from javascript. |
| 177 | * |
| 178 | * @since 1.0.0 |
| 179 | * @api Use this before calling `output_header()`. |
| 180 | * @param string $key The translation key (used in javascript). |
| 181 | * @param string $value Human readable text. |
| 182 | */ |
| 183 | static public function translate( $key, $value ) { |
| 184 | self::$i10n[ $key ] = (string) $value; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Outputs code in the page header. |
| 189 | * |
| 190 | * This function must be called by the plugin! |
| 191 | * It's not important if it's in the header or in the footer of the page, |
| 192 | * but in top/header is recommended. |
| 193 | * |
| 194 | * @since 1.0.0 |
| 195 | * @api Call this function somewhere after output started. |
| 196 | * @param array $commands Optinal list of additional JS commands that |
| 197 | * are executed when page loaded. |
| 198 | */ |
| 199 | static public function output( $commands = array() ) { |
| 200 | $data = array(); |
| 201 | $data[] = 'window.WDP = window.WDP || {}'; |
| 202 | $data[] = 'WDP.data = WDP.data || {}'; |
| 203 | $data[] = 'WDP.data.site_url = ' . json_encode( get_site_url() ); |
| 204 | $data[] = 'WDP.lang = ' . json_encode( self::$i10n ); |
| 205 | |
| 206 | // Add custom JS commands to the init-code. |
| 207 | if ( is_array( $commands ) ) { |
| 208 | $data = array_merge( $data, $commands ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Display a custom success message on the WPMU Dashboard pages. |
| 213 | * |
| 214 | * @var string|array The message to display. |
| 215 | * Array options: |
| 216 | * 'type' => [ok|err] (default: 'ok') |
| 217 | * 'delay' => 3000 (default: 3000ms) |
| 218 | * 'message' => '...' (required!) |
| 219 | */ |
| 220 | $notice = apply_filters( 'wpmudev-admin-notice', false ); |
| 221 | if ( $notice ) { |
| 222 | $command = 'WDP'; |
| 223 | if ( is_array( $notice ) && ! empty( $notice['type'] ) ) { |
| 224 | $command .= sprintf( '.showMessage("type", "%s")', esc_attr( $notice['type'] ) ); |
| 225 | } |
| 226 | if ( is_array( $notice ) && ! empty( $notice['delay'] ) ) { |
| 227 | $command .= sprintf( '.showMessage("delay", %s)', intval( $notice['delay'] ) ); |
| 228 | } |
| 229 | if ( is_array( $notice ) && ! empty( $notice['message'] ) ) { |
| 230 | $command .= sprintf( '.showMessage("message", "%s")', esc_html( $notice['message'] ) ); |
| 231 | } elseif ( is_string( $notice ) ) { |
| 232 | $command .= sprintf( '.showMessage("message", "%s")', esc_html( $notice ) ); |
| 233 | } |
| 234 | $command .= '.showMessage("show")'; |
| 235 | $data[] = $command; |
| 236 | } |
| 237 | |
| 238 | foreach ( $data as $item ) { |
| 239 | printf( |
| 240 | "<script>;jQuery(function(){%s;});</script>\n", |
| 241 | // @codingStandardsIgnoreStart: This is javascript code, no escaping! |
| 242 | $item |
| 243 | // @codingStandardsIgnoreEnd |
| 244 | ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Output the HTML code to display the notification. |
| 250 | * |
| 251 | * @since 1.0.0 |
| 252 | * @param string $module_url URL to this module (directory). |
| 253 | * @param array $msg The message details. |
| 254 | * id .. Required, can be any valid class-name. |
| 255 | * content .. Required, can contain HTML. |
| 256 | * dismissed .. Optional. If true then no message is output. |
| 257 | * can_dismiss .. Optional. If true a Dismiss button is added. |
| 258 | * cta .. Optional. Can be HTML code of a button/link. |
| 259 | */ |
| 260 | static public function render_dev_notification( $module_url, $msg ) { |
| 261 | if ( ! is_array( $msg ) ) { return; } |
| 262 | if ( ! isset( $msg['id'] ) ) { return; } |
| 263 | if ( empty( $msg['content'] ) ) { return; } |
| 264 | if ( $msg['dismissed'] ) { return; } |
| 265 | |
| 266 | $css_url = $module_url . 'notice.css'; |
| 267 | $js_url = $module_url . 'notice.js'; |
| 268 | |
| 269 | if ( empty( $msg['id'] ) ) { |
| 270 | $msg_dismiss = ''; |
| 271 | } else { |
| 272 | $msg_dismiss = __( 'Saving', 'wpmudev' ); |
| 273 | } |
| 274 | |
| 275 | $show_actions = $msg['can_dismiss'] || $msg['cta']; |
| 276 | |
| 277 | $allowed = array( |
| 278 | 'a' => array( 'href' => array(), 'title' => array(), 'target' => array(), 'class' => array() ), |
| 279 | 'br' => array(), |
| 280 | 'hr' => array(), |
| 281 | 'em' => array(), |
| 282 | 'i' => array(), |
| 283 | 'strong' => array(), |
| 284 | 'b' => array(), |
| 285 | ); |
| 286 | |
| 287 | ?> |
| 288 | <link rel="stylesheet" type="text/css" href="<?php echo esc_url( $css_url ); ?>" /> |
| 289 | <div class="notice frash-notice" style="display:none"> |
| 290 | <input type="hidden" name="msg_id" value="<?php echo esc_attr( $msg['id'] ); ?>" /> |
| 291 | |
| 292 | <div class="frash-notice-logo"><span></span></div> |
| 293 | <div class="frash-notice-message"> |
| 294 | <?php echo wp_kses( $msg['content'], $allowed ); ?> |
| 295 | </div> |
| 296 | <?php if ( $show_actions ) : ?> |
| 297 | <div class="frash-notice-cta"> |
| 298 | <?php echo $msg['cta']; ?> |
| 299 | <?php if ( $msg['can_dismiss'] ) : ?> |
| 300 | <button class="frash-notice-dismiss" data-msg="<?php echo esc_attr( $msg_dismiss ); ?>"> |
| 301 | <?php esc_html_e( 'Dismiss', 'wpmudev' ); ?> |
| 302 | </button> |
| 303 | <?php endif; ?> |
| 304 | </div> |
| 305 | <?php endif; ?> |
| 306 | </div> |
| 307 | <script src="<?php echo esc_url( $js_url ); ?>"></script> |
| 308 | <?php |
| 309 | } |
| 310 | }; |
| 311 | |
| 312 | // Initialize the UI. |
| 313 | WDEV_Plugin_Ui::reset(); |
| 314 | } |
| 315 |