PluginProbe
Document Gallery / 4.1
Document Gallery v4.1
trunk 0.8 0.8.5 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 2.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 94 releases
document-gallery / admin / class-admin.php

class-admin.php in Document Gallery 4.1, at admin/class-admin.php

469 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'WPINC' ) OR exit;
3
4 class DG_Admin {
5 /**
6 * @var string The hook for the Document Gallery settings page.
7 */
8 private static $hook;
9
10 /**
11 * @var string The current tab being rendered.
12 */
13 private static $current;
14
15 /**
16 * NOTE: This should only ever be accessed through getTabs().
17 *
18 * @var string[] Associative array containing all tab names, keyed by tab slug.
19 */
20 private static $tabs;
21
22 /**
23 * Returns reference to tabs array, initializing if needed.
24 *
25 * NOTE: This cannot be done in a static constructor due to timing with i18n.
26 */
27 public static function &getTabs() {
28 if ( ! isset( self::$tabs ) ) {
29 self::$tabs = array(
30 'general-tab' => __( 'General', 'document-gallery' ),
31 'thumber-co-tab' => __( 'Thumber.co', 'document-gallery' ),
32 'thumbnail-management-tab' => __( 'Thumbnail Management', 'document-gallery' ),
33 'logging-tab' => __( 'Logging', 'document-gallery' ),
34 'advanced-tab' => __( 'Advanced', 'document-gallery' )
35 );
36 }
37
38 return self::$tabs;
39 }
40
41 /**
42 * Renders Document Gallery options page.
43 */
44 public static function renderOptions() { ?>
45 <div class="wrap">
46 <h2><?php echo __( 'Document Gallery Settings', 'document-gallery' ); ?></h2>
47
48 <h2 class="nav-tab-wrapper">
49 <?php foreach ( self::getTabs() as $tab => $name ) {
50 $class = ( $tab === self::$current ) ? ' nav-tab-active' : '';
51 echo '<a id="' . $tab . '-header" class="nav-tab' . $class . '" href="?page=' . DG_OPTION_NAME . '&tab=' . $tab . '">' . $name . '</a>';
52 } ?>
53 </h2>
54
55 <form method="post" action="options.php?tab=<?php echo self::$current; ?>" id="<?php echo self::$current ?>">
56 <?php
57 settings_fields( DG_OPTION_NAME );
58 do_settings_sections( DG_OPTION_NAME );
59 if ( self::$current !== 'thumbnail-management-tab' && self::$current != 'logging-tab' ) {
60 submit_button();
61 }
62 ?>
63 </form>
64 </div>
65 <?php }
66
67 /**
68 * Adds settings link to main plugin view.
69 * @param $links string[] The links being prepended.
70 * @return string[] The given array with settings link prepended.
71 */
72 public static function addSettingsLink( $links ) {
73 $settings = '<a href="options-general.php?page=' . DG_OPTION_NAME . '">' .
74 __( 'Settings', 'document-gallery' ) . '</a>';
75 array_unshift( $links, $settings );
76
77 return $links;
78 }
79
80 /**
81 * Adds donate link to main plugin view.
82 * @param $links string[] The links.
83 * @param $file string The file.
84 * @return string[] The given array with donate link appended.
85 */
86 public static function addDonateLink( $links, $file ) {
87 if ( $file === DG_BASENAME ) {
88 global $dg_options;
89
90 $donate = '<strong><a href="' . $dg_options['meta']['donate_link'] . '">' .
91 '<span class="dashicons dashicons-heart"></span> ' .
92 __( 'Donate', 'document-gallery' ) . '</a></strong>';
93 $links[] = $donate;
94 }
95
96 return $links;
97 }
98
99 /**
100 * Adds Document Gallery settings page to admin navigation.
101 */
102 public static function addAdminPage() {
103 DG_Admin::$hook = add_options_page(
104 __( 'Document Gallery Settings', 'document-gallery' ),
105 __( 'Document Gallery', 'document-gallery' ),
106 'manage_options', DG_OPTION_NAME, array( __CLASS__, 'renderOptions' ) );
107 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueueScriptsAndStyles' ) );
108 }
109
110 /**
111 * Enqueues styles and scripts for the admin settings page.
112 * @param $hook string The hook.
113 */
114 public static function enqueueScriptsAndStyles( $hook ) {
115 include_once DG_PATH . 'admin/class-feature-pointers.php';
116 DG_FeaturePointers::enqueueScripts();
117
118 if ( in_array( $hook, array( DG_Admin::$hook, 'post.php', 'post-new.php' ), true ) ) {
119 // Settings Page
120 DG_Util::enqueueAsset( 'document-gallery-admin', 'assets/css/admin.css' );
121
122 DG_Util::enqueueAsset( 'document-gallery-admin', 'assets/js/admin.js', array( 'jquery' ) );
123 wp_localize_script( 'document-gallery-admin', 'dg_admin_vars', array( 'upload_limit' => wp_max_upload_size() ) );
124 if ( $hook !== self::$hook ) { //if $hook is 'post.php' or 'post-new.php'
125 global $dg_options;
126
127 // Media Manager integration
128 add_action( 'admin_print_footer_scripts', array(
129 'DG_Admin',
130 'loadCustomTemplates'
131 ) ); //wp_print_scripts || wp_footer
132
133 DG_Util::enqueueAsset( 'dg-media-manager', 'assets/js/media_manager.js', array( 'media-views' ) );
134 wp_localize_script( 'dg-media-manager', 'DGl10n', array(
135 'dgMenuTitle' => __( 'Create Document Gallery', 'document-gallery' ),
136 'dgButton' => __( 'Create a new Document Gallery', 'document-gallery' ),
137 'canceldgTitle' => '&#8592; ' . __( 'Cancel Document Gallery', 'document-gallery' ),
138 'updatedg' => __( 'Update Document Gallery', 'document-gallery' ),
139 'insertdg' => __( 'Insert Document Gallery', 'document-gallery' ),
140 'addTodg' => __( 'Add to Document Gallery', 'document-gallery' ),
141 'addTodgTitle' => __( 'Add to Document Gallery', 'document-gallery' ),
142 'editdgTitle' => __( 'Edit Document Gallery', 'document-gallery' ),
143 'unfitSCalert' => __( 'This DG shortcode is an advanced one. '.
144 'Sorry there is no way to use standard edit dialog for it. '.
145 'You should switch to text mode to edit shortcode itself.', 'document-gallery' ),
146 ) );
147 wp_localize_script( 'dg-media-manager', 'dgDefaults', $dg_options['gallery'] );
148
149 // TinyMCE visual editor
150 add_filter( 'mce_external_plugins', array( __CLASS__, 'mce_external_plugins' ) );
151 add_filter( 'mce_css', array( __CLASS__, 'dg_plugin_mce_css' ) );
152 }
153 }
154 }
155
156 /**
157 * Adds assets/js/gallery.js as registered TinyMCE plugin
158 *
159 * @param string[] $plugins An array of default TinyMCE plugins.
160 *
161 * @return string[] Default TinyMCE plugins plus custom DG plugin.
162 */
163 public static function mce_external_plugins( $plugins ) {
164 $plugins['dg'] = DG_Util::getAssetPath( 'assets/js/gallery.js' );
165
166 return $plugins;
167 }
168
169 /**
170 * Adds assets/css/style.css as registered TinyMCE CSS
171 *
172 * @param string $stylesheets Comma-delimited list of stylesheets.
173 *
174 * @return string Comma-delimited list of stylesheets.
175 */
176 public static function dg_plugin_mce_css( $stylesheets ) {
177 if ( ! empty( $stylesheets ) ) {
178 $stylesheets .= ',';
179 }
180 $stylesheets .= str_replace( ',', '%2C', DG_Util::getAssetPath( 'assets/css/style.css' ) );
181
182 return $stylesheets;
183 }
184
185 /**
186 * Load Document Gallery Custom templates.
187 */
188 public static function loadCustomTemplates() {
189 include_once DG_PATH . 'admin/media-manager-template.php';
190 }
191
192 /**
193 * Registers settings for the Document Gallery options page.
194 */
195 public static function registerSettings() {
196 self::initCurrentTab();
197
198 register_setting( DG_OPTION_NAME, DG_OPTION_NAME, array( __CLASS__, 'validateSettings' ) );
199
200 include_once DG_PATH . 'admin/tabs/' . self::$current . '.php';
201 dg_register_settings();
202 }
203
204 /**
205 * Validates submitted options, sanitizing any invalid options.
206 *
207 * @param mixed[] $values User-submitted new options.
208 * @return mixed[] Sanitized new options.
209 */
210 public static function validateSettings( $values ) {
211 // NOTE: WP double-calls this function -- below logic prevents potential
212 // side effects by processing a maximum of one call to validate
213 // per page load, re-returning the previous result on any
214 // subsequent calls.
215 static $ret = null;
216 if ( is_null( $ret ) ) {
217 self::initCurrentTab();
218
219 if ( isset( $values['ajax'] ) ) {
220 unset( $values['ajax'] );
221 define( 'DOING_AJAX', true );
222 }
223
224 DG_Logger::writeLog( DG_LogLevel::Detail, 'Validating ' . self::$current . ' tab.' );
225 include_once DG_PATH . 'admin/tabs/' . self::$current . '.php';
226 $ret = dg_validate_settings( $values );
227 }
228
229 return $ret;
230 }
231
232 /**
233 * Validates uploaded file as a semi for potential thumbnail.
234 *
235 * @param string $var File field name.
236 *
237 * @return bool|string False on failure, path to temp file on success.
238 */
239 public static function validateUploadedFile( $var = 'file' ) {
240 // checking if any file was delivered
241 if ( ! isset( $_FILES[ $var ] ) ) {
242 return false;
243 }
244 // we gonna process only first one
245 if ( ! is_array( $_FILES[ $var ]['error'] ) ) {
246 $upload_err = $_FILES[ $var ]['error'];
247 $upload_path = $_FILES[ $var ]['tmp_name'];
248 $upload_size = $_FILES[ $var ]['size'];
249 $upload_type = $_FILES[ $var ]['type'];
250 $upload_name = $_FILES[ $var ]['name'];
251 } else {
252 $upload_err = $_FILES[ $var ]['error'][0];
253 $upload_path = $_FILES[ $var ]['tmp_name'][0];
254 $upload_size = $_FILES[ $var ]['size'][0];
255 $upload_type = $_FILES[ $var ]['type'][0];
256 $upload_name = $_FILES[ $var ]['name'][0];
257 }
258 $info = getimagesize( $upload_path );
259 if ( $info ) {
260 if ( $info['mime'] != $upload_type ) {// in DG_Thumber::getExt() we'll define and set appropriate extension
261 DG_Logger::writeLog(
262 DG_LogLevel::Warning,
263 __( 'File extension doesn\'t match the MIME type of the image: ', 'document-gallery' ) .
264 $upload_name . ' - ' . $info['mime'] );
265 }
266 if ( $upload_size > wp_max_upload_size() ) {
267 DG_Logger::writeLog(
268 DG_LogLevel::Warning,
269 __( 'Uploaded file size exceeds the allowable limit: ', 'document-gallery' ) .
270 $upload_name . ' - ' . $upload_size . 'b' );
271
272 return false;
273 }
274 } else {
275 DG_Logger::writeLog(
276 DG_LogLevel::Warning,
277 __( 'Uploaded file is not an image: ', 'document-gallery' ) .
278 $upload_name );
279
280 return false;
281 }
282 if ( $upload_err == UPLOAD_ERR_OK && $upload_size > 0 ) {
283 $temp_file = $upload_path;
284 } else {
285 DG_Logger::writeLog(
286 DG_LogLevel::Error,
287 __( 'Failed to get uploaded file: ', 'document-gallery' ) .
288 $upload_err );
289
290 return false;
291 }
292
293 return $temp_file;
294 }
295
296 /**
297 * @return bool Whether to register settings.
298 */
299 public static function doRegisterSettings() {
300 if ( ! is_multisite() ) {
301 $script = ! empty( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : null;
302 } else {
303 $script = parse_url( $_SERVER['REQUEST_URI'] );
304 $script = basename( $script['path'] );
305 }
306
307 return ! empty( $script ) && ( 'options-general.php' === $script || 'options.php' === $script );
308 }
309
310 /**
311 * Adds meta box to the attchements' edit pages.
312 */
313 public static function addMetaBox() {
314 $screens = array( 'attachment' );
315 foreach ( $screens as $screen ) {
316 add_meta_box(
317 DG_OPTION_NAME . '_gen_box',
318 __( '<b>Thumbnail</b> for <i><b>Document Gallery</b></i>', 'document-gallery' ),
319 array( __CLASS__, 'renderMetaBox' ),
320 $screen,
321 'normal'
322 );
323 }
324 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueueScriptsAndStyles' ) );
325 }
326
327 /**
328 * Render a Meta Box.
329 * @param $post WP_Post The post.
330 */
331 public static function renderMetaBox( $post ) {
332 wp_nonce_field( DG_OPTION_NAME . '_meta_box', DG_OPTION_NAME . '_meta_box_nonce' );
333 $ID = $post->ID;
334 $options = DG_Thumber::getOptions();
335 $thumb = DG_Thumb::getThumb( $ID, $options['width'] . 'x' . $options['height'] );
336 $icon = ! is_null( $thumb ) && $thumb->isSuccess()
337 ? $thumb->getUrl()
338 : DG_DefaultThumber::getInstance()->getThumbnail( $ID );
339
340 echo '<table id="ThumbsTable" class="wp-list-table widefat fixed media" cellpadding="0" cellspacing="0">' .
341 '<tbody><tr data-entry="' . $ID . '"><td class="column-icon media-icon"><img src="' .
342 $icon . '" />' . '</td><td class="column-thumbupload">' .
343 '<span class="manual-download">' .
344 '<span class="dashicons dashicons-upload"></span>' .
345 '<span class="html5dndmarker">Drop file here<span> or </span></span>' .
346 '<span class="buttons-area">' .
347 '<input id="upload-button' . $ID . '" type="file" />' .
348 '<input id="trigger-button' . $ID . '" type="button" value="Select File" class="button" />' .
349 '</span>' .
350 '</span>' .
351 '</td></tr></tbody></table>' .
352 ( is_null( $thumb ) ? '<span class="dashicons dashicons-info"></span><span class="">Please note this attachment hasn&#39;t been used in any Document Gallery instance and so there is no autogenerated thumbnail, in the meantime default one is used instead.</span>' : '' ) . PHP_EOL;
353 }
354
355 /**
356 * Save a Meta Box.
357 */
358 public static function saveMetaBox( $post_id ) {
359 // Check if our nonce is set.
360 // Verify that the nonce is valid.
361 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
362 if ( ! isset( $_POST[ DG_OPTION_NAME . '_meta_box_nonce' ] ) || ! wp_verify_nonce( $_POST[ DG_OPTION_NAME . '_meta_box_nonce' ], DG_OPTION_NAME . '_meta_box' ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
363 return;
364 }
365
366 $responseArr = array( 'result' => false );
367 if ( isset( $_POST[ DG_OPTION_NAME ]['entry'] ) ) {
368 $ID = intval( $_POST[ DG_OPTION_NAME ]['entry'] );
369 } else {
370 $ID = - 1;
371 }
372
373 $thumbs = DG_Thumb::getThumbs();
374 if ( isset( $_POST[DG_OPTION_NAME]['upload'] ) && isset( $_FILES['file'] ) && isset( $thumbs[$ID] ) ) {
375 $uploaded_filename = self::validateUploadedFile();
376 if ( $uploaded_filename && ( $thumb = DG_Thumber::setThumbnail( $ID, $uploaded_filename ) ) ) {
377 $responseArr['result'] = true;
378 $responseArr['url'] = $thumb->getUrl();
379 }
380 }
381
382 if ( isset( $_POST[ DG_OPTION_NAME ]['ajax'] ) ) {
383 wp_send_json( $responseArr );
384 }
385 }
386
387 /**
388 * Render a checkbox field.
389 *
390 * @param mixed[] $args
391 */
392 public static function renderCheckboxField( $args ) {
393 $args['disabled'] = isset( $args['disabled'] ) ? $args['disabled'] : false;
394 printf( '<label><input type="checkbox" value="1" name="%1$s[%2$s]" id="%3$s" %4$s %5$s/> %6$s</label>',
395 $args['option_name'],
396 $args['name'],
397 $args['label_for'],
398 checked( $args['value'], 1, false ),
399 disabled( $args['disabled'], true, false ),
400 $args['description'] );
401 }
402
403 /**
404 * Render a text field.
405 *
406 * @param mixed[] $args
407 */
408 public static function renderTextField( $args ) {
409 printf( '<input type="%1$s" value="%2$s" name="%3$s[%4$s]" id="%5$s" /> %6$s',
410 isset( $args['type'] ) ? $args['type'] : 'text',
411 $args['value'],
412 $args['option_name'],
413 $args['name'],
414 $args['label_for'],
415 $args['description'] );
416 }
417
418 /**
419 * Accepts a two-dimensional array where each inner array consists of valid arguments for renderTextField.
420 *
421 * @param mixed[] $args
422 */
423 public static function renderMultiTextField( $args ) {
424 foreach ( $args as $arg ) {
425 self::renderTextField( $arg );
426 }
427 }
428
429 /**
430 * Render a select field.
431 *
432 * @param mixed[] $args
433 */
434 public static function renderSelectField( $args ) {
435 printf( '<select name="%1$s[%2$s]" id="%3$s">',
436 $args['option_name'],
437 $args['name'],
438 $args['label_for'] );
439
440 foreach ( $args['options'] as $val ) {
441 printf( '<option value="%1$s" %2$s>%3$s</option>',
442 $val,
443 selected( $val, $args['value'], false ),
444 $val,
445 $args['description'] );
446 }
447
448 print '</select> ' . $args['description'];
449 }
450
451 /**
452 * Initializes the current tab value.
453 */
454 private static function initCurrentTab() {
455 if ( empty( $_GET['tab'] ) || ! array_key_exists( $_GET['tab'], self::getTabs() ) ) {
456 reset( self::getTabs() );
457 self::$current = key( self::getTabs() );
458 } else {
459 self::$current = $_GET['tab'];
460 }
461 }
462
463 /**
464 * Blocks instantiation. All functions are static.
465 */
466 private function __construct() {
467
468 }
469 }