-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmime_type_link_images.php
2282 lines (1884 loc) · 79.4 KB
/
mime_type_link_images.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package MimeTypeLinkImages
* @version 3.2.20
*/
/*
Plugin Name: MimeTypes Link Icons
Plugin URI: http://blog.eagerterrier.co.uk/2010/10/holy-cow-ive-gone-and-made-a-mime-type-wordpress-plugin/
Description: This will add file type icons next to links automatically. Change options in the <a href="options-general.php?page=mimetypes-link-icons">settings page</a>
Version: 3.2.20
Author: Toby Cox, Juliette Reinders Folmer
Author URI: https://github.com/eagerterrier/MimeTypes-Link-Icons
Author: Toby Cox
Author URI: http://eagerterrier.co.uk/
Author: Juliette Reinders Folmer
Author URI: http://adviesenzo.nl/
Contributor: Keith Parker
Contributor URI: http://infas.net/
Contributor: Birgir Erlendsson
Contributor URI: http://wordpress.stackexchange.com/users/26350/birgire
Contributor: x06designs
Contributor URI: https://github.com/x06designs
Text Domain: mimetypes-link-icons
Domain Path: /languages
*/
/*
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @todo: test with safe_mode on and allow_url_fopen off ?
*
* POTENTIAL ROAD MAP:
* @todo look into issue: http://wordpress.org/support/topic/async-replacement-causing-jquery-problems
* @todo look into issue: http://wordpress.org/support/topic/problem-with-images-13
*
* @todo may be implement Google Drive file types ? http://wordpress.org/support/topic/ms-publisher
* @todo may be implement some way to allow more jquery selector types for hidden, i.e. #selector, TAG etc ? http://wordpress.org/support/topic/problem-with-images-13?replies=5
* @todo may be add a 'reset all settings' button ? this should delete the plugin option so all settings will revert back to default
* @todo may be incorporate a directory crawler in the upgrade routine to inventarise the available file extensions and save the found extensions to a WP option. Only needs to be done on upgrade as that is the only time new extensions will be added. Problem with this is that the style.php file also uses the list and does not have access to the WP functions to retrieve the option.
* @todo try and figure something out to only load the front-end stylesheet and js file when needed
* @todo figure out a way how to rename this file to mimetypes-link-icons to be in line with the rest of the plugin. Problem: plugin will deactivate when people upgrade if the name of the main file has changed.... Has to do with option holding file names of activated plugins
*/
if ( ! class_exists( 'Mime_Types_Link_Icons' ) ) {
/**
* @package WordPress\Plugins\MimeTypes Link Icons
* @version 3.2.20
* @link http://wordpress.org/plugins/mimetypes-link-icons/ MimeTypes Link Icons WordPress plugin
* @link https://github.com/eagerterrier/MimeTypes-Link-Icons GitHub development of MimeTypes Link Icons WordPress plugin
*
* @copyright 2010 - 2013 Toby Cox, Juliette Reinders Folmer
* @license http://creativecommons.org/licenses/GPL/2.0/ GNU General Public License, version 2
*/
class Mime_Types_Link_Icons {
/* *** DEFINE CLASS CONSTANTS *** */
/**
* @const string Plugin version number
* @usedby upgrade_options(), __construct()
*/
const VERSION = '3.2.20';
/**
* @const string Version in which the front-end styles where last changed
* @usedby wp_enqueue_scripts()
*/
const STYLES_VERSION = '3.0';
/**
* @const string Version in which the front-end scripts where last changed
* @usedby wp_enqueue_scripts()
*/
const SCRIPTS_VERSION = '3.1.0';
/**
* @const string Version in which the admin styles where last changed
* @usedby admin_enqueue_scripts()
*/
const ADMIN_STYLES_VERSION = '3.0';
/**
* @const string Version in which the admin scripts where last changed
* @usedby admin_enqueue_scripts()
*/
const ADMIN_SCRIPTS_VERSION = '3.0';
/**
* @const string Plugin version in which the DB options structure was last changed
* @usedby upgrade_options()
*/
const DB_LASTCHANGE = '3.2';
/**
* @const string Minimum required capability to change the plugin options
*/
const REQUIRED_CAP = 'manage_options';
/**
* @const string Page underneath which the settings page will be hooked
*/
const PARENT_PAGE = 'options-general.php';
/**
* @const string Name of options variable containing the plugin proprietary settings
*/
const SETTINGS_OPTION = 'mimetype_link_icon_options';
/**
* @const string Name of options variable containing the filesize cached values
*/
const CACHE_OPTION = 'mimetype_link_icons_filesize_cache';
/**
* @const int Number of columns to put the image settings in on the options page
*/
const NR_OF_COLUMNS = 2;
/* *** DEFINE STATIC CLASS PROPERTIES *** */
/**
* These static properties will be initialized - *before* class instantiation -
* by the static init() function
*/
/**
* @staticvar string $basename Plugin Basename = 'dir/file.php'
*/
public static $basename;
/**
* @staticvar string $name Plugin name = dirname of the plugin
* Also used as text domain for translation
*/
public static $name;
/**
* @staticvar string $path Full server path to the plugin directory, has trailing slash
*/
public static $path;
/**
* @staticvar string $suffix Suffix to use if scripts/styles are in debug mode
*/
public static $suffix;
/* *** DEFINE CLASS PROPERTIES *** */
/* *** Semi Static Properties *** */
/**
* @var array Available file sizes
* @todo IMPORTANT: for now on each change, also copy this array to style.php
*/
public $sizes = array(
16,
24,
48,
64,
128,
);
/**
* @var array Available images types
* @todo IMPORTANT: for now on each change, also copy this array to style.php
*/
public $image_types = array(
'gif',
'png',
);
/**
* @var array Available image alignments: key = setting, value = field label
* Will be set by set_properties() as the field labels need translating
* @todo IMPORTANT: for now on each change, also copy this array to style.php
*/
public $alignments;
/**
* @var array array of mimetypes
* @todo IMPORTANT: for now on each change, also copy this array to style.php
* and of course to the readme ;-)
*/
public $mime_types = array(
'3g2', '3gp',
'ai', 'air', 'asf', 'avi',
'bib',
'capx', 'cls', 'csv',
'deb', 'djvu', 'dmg', 'doc', 'docx', 'dwf', 'dwg',
'eps', 'epub', 'exe',
'f', 'f77', 'f90', 'flac', 'flv',
'gif', 'gz',
'ico', 'indd', 'iso',
'jpg', 'jpeg',
'key',
'log',
'm4a', 'm4v', 'midi', 'mkv', 'mov', 'mp3', 'mp4', 'mpeg', 'mpg', 'msi', 'msix',
'odp', 'ods', 'odt', 'oga', 'ogg', 'ogv',
'pages', 'pdf', 'png', 'pps', 'ppsx', 'ppt', 'pptm', 'pptx', 'psd', 'pub', 'py',
'qt',
'ra', 'ram', 'rar', 'rm', 'rpm', 'rtf', 'rv',
'skp', 'spx', 'sql', 'sty',
'tar', 'tex', 'tgz', 'tiff', 'ttf', 'txt',
'vob',
'wav', 'wmv',
'xls', 'xlsx', 'xml', 'xpi',
'zip',
);
/**
* @var array array of mimetypes which default to true / 'on' status
*/
public $default_is_true = array(
'pdf',
);
/**
* @var array Default option values - this array will be enriched by the enrich_default_settings() method
* @todo IMPORTANT: For now, on change in default size, type or alignment, also copy
* the new defaults to style.php
*/
public $defaults = array(
'internal_domains' => array(),
'image_size' => 16,
'image_type' => 'png',
'leftorright' => 'left',
'show_file_size' => false,
'precision' => 2,
'use_cache' => true,
'cache_time' => 604800, // seconds: 1 hour = 3600, 1 day = 86400, 1 week = 604800
'enable_async' => false,
'enable_async_debug' => false,
'enable_hidden_class' => true,
'hidden_classname' => array( 'wp-caption', ),
'version' => null,
'show_file_size_over' => 0,
//'upgrading' => false, // will never change, not saved to db, only used to distinguish a call from the upgrade method
);
/**
* @var array array of option form sections: key = setting area, value = section label
* Will be set by set_properties() as the section labels need translating
* @usedby display_options_page()
*/
public $form_sections = array();
/**
* @var array array of byte suffixes for creating a human readable file size
* Will be set by set_properties() as the labels need translating
* @usedby human_readable_filesize()
*/
public $byte_suffixes = array();
/* *** Properties Holding Various Parts of the Class' State *** */
/**
* @var string settings page registration hook suffix
*/
public $hook;
/**
* @var array Variable holding current settings for this plugin
*/
public $settings = array();
/**
* @var array Efficiency property - array of the mimetype for which the plugin should be active
*/
public $active_mimetypes = array();
/**
* @var array Array holding cached filesize values
* key = sanitized file path
* values = array( 'size' => file size, 'time' => time of last filesize retrieval in seconds )
*/
public $cache = array();
/**
* @var array Array holding the rel / filesize CSS styles to be added to the page
*/
public $filesize_styles = array();
/**
* @var resource Holds the curl resource if one exists
*/
public $curl;
/**
* @var bool Debug setting to enable extra debugging for the plugin
*/
public $debug = false;
/* *** PLUGIN INITIALIZATION METHODS *** */
/**
* Object constructor for plugin
*/
public function __construct() {
/* Initialize our settings option */
$this->options_init();
/* Check if we have any activation or upgrade actions to do */
if ( ! isset( $this->settings['version'] ) || version_compare( self::DB_LASTCHANGE, $this->settings['version'], '>' ) ) {
add_action( 'init', array( $this, 'upgrade_options' ), 8 );
}
// Make sure that the upgrade actions are run on (re-)activation as well.
add_action( 'mimetype_link_icons_plugin_activate', array( $this, 'upgrade_options' ) );
// Register the plugin initialization actions
add_action( 'init', array( $this, 'pre_init' ), 5 );
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
/**
* Set the static path and directory variables for this class
* Is called from the global space *before* instantiating the class to make
* sure the correct values are available to the object
*
* @return void
*/
public static function init_statics() {
self::$basename = plugin_basename( __FILE__ );
self::$name = dirname( self::$basename );
self::$path = plugin_dir_path( __FILE__ );
self::$suffix = ( ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min' );
}
/** ******************* OPTION MANAGEMENT ******************* **/
/**
* Initialize our settings option and add all relevant actions and filters
*
* @since 3.2
*/
public function options_init() {
/* Enrich the defaults */
$this->enrich_default_settings();
/* Add filter which get applied to get_options() results */
$this->add_default_filter();
add_filter( 'option_' . self::SETTINGS_OPTION, array( $this, 'filter_option' ) );
/* The option validation routines remove the default filters to prevent failing to insert
an option if it's new. Let's add them back afterwards */
add_action( 'add_option', array( $this, 'add_default_filter' ) );
if ( version_compare( $GLOBALS['wp_version'], '3.7', '!=' ) ) {
add_action( 'update_option', array( $this, 'add_default_filter' ) );
}
else {
// Abuse a filter for WP 3.7 where the update_option filter is placed in the wrong location
add_filter( 'pre_update_option_' . self::SETTINGS_OPTION, array( $this, 'wp37_add_default_filters' ) );
}
/* Make sure the option will always get validated, independently of register_setting()
(which is only available on back-end) */
add_filter( 'sanitize_option_' . self::SETTINGS_OPTION, array( $this, 'validate_options' ) );
/* Register our option for the admin pages */
add_action( 'admin_init', array( $this, 'register_setting' ) );
/* Refresh the $settings property on option update */
add_action( 'add_option_' . self::SETTINGS_OPTION, array( $this, 'on_add_option' ), 10, 2 );
add_action( 'update_option_' . self::SETTINGS_OPTION, array( $this, 'on_update_option' ), 10, 2 );
/* Initialize the $settings property */
$this->refresh_current();
/* Refresh the $cache property on cache option update */
add_action( 'add_option_' . self::CACHE_OPTION, array( $this, 'on_add_cache_option' ), 10, 2 );
add_action( 'update_option_' . self::CACHE_OPTION, array( $this, 'on_update_cache_option' ), 10, 2 );
}
/**
* Enrich the default settings array
*/
private function enrich_default_settings() {
foreach ( $this->mime_types as $type ) {
$this->defaults[ 'enable_' . $type ] = ( false === in_array( $type, $this->default_is_true ) ? false : true );
}
}
/**
* Register our option
*
* @since 3.2 (moved from admin_init to separate method)
*/
public function register_setting() {
register_setting( self::SETTINGS_OPTION . '-group', self::SETTINGS_OPTION );
}
/**
* Add filtering of the option default values
*
* @since 3.2
*/
public function add_default_filter() {
if ( has_filter( 'default_option_' . self::SETTINGS_OPTION, array( $this, 'filter_option_defaults' ) ) === false ) {
add_filter( 'default_option_' . self::SETTINGS_OPTION, array( $this, 'filter_option_defaults' ) );
};
}
/**
* Abusing a filter to re-add our default filters
* WP 3.7 specific as update_option action hook was in the wrong place temporarily
* @see http://core.trac.wordpress.org/ticket/25705
*
* @param mixed $new_value
*
* @return mixed unchanged value
*/
public function wp37_add_default_filters( $new_value ) {
$this->add_default_filter();
return $new_value;
}
/**
* Remove filtering of the option default values
* Called from the validate_options() method to prevent failure to add new options
*
* @since 3.2
*/
public function remove_default_filter() {
remove_filter( 'default_option_' . self::SETTINGS_OPTION, array( $this, 'filter_option_defaults' ) );
}
/**
* Filter option defaults
*
* This in effect means that get_option() will not return false if the option is not found,
* but will instead return our defaults. This way we always have all of our option values available.
*
* @since 3.2
*/
public function filter_option_defaults() {
$this->refresh_current( $this->defaults );
return $this->defaults;
}
/**
* Filter option
*
* This in effect means that get_option() will not just return our option from the database,
* but will instead return that option merged with our defaults.
* This way we always have all of our option values available. Even when we add new option
* values (to the defaults array) when the plugin is upgraded.
*
* @since 3.2
*/
public function filter_option( $options ) {
$options = $this->array_filter_merge( $this->defaults, $options );
$this->refresh_current( $options );
return $options;
}
/**
* Set the $settings property to the value of our option
* @since 3.2
*/
private function refresh_current( $value = null ) {
if ( ! isset( $value ) ) {
$value = get_option( self::SETTINGS_OPTION );
}
$this->settings = $value;
/* Update the active_mimetypes array */
$this->active_mimetypes = array();
foreach ( $this->mime_types as $mime_type ) {
if ( true === $this->settings[ 'enable_' . $mime_type ] ) {
$this->active_mimetypes[] = $mime_type;
}
}
unset( $mime_type );
}
/**
* Refresh the $settings property when our property is added to wp
* @since 3.2
*/
public function on_add_option( $option_name, $value ) {
$this->refresh_current( $value );
}
/**
* Refresh the $settings property when our property is updated
* @since 3.2
*/
public function on_update_option( $old_value, $value ) {
$this->refresh_current( $value );
}
/**
* Set the $cache property to the value of our option
*
* @since 3.2
*/
private function refresh_cache( $value = null ) {
if ( ! isset( $value ) ) {
$value = get_option( self::CACHE_OPTION );
}
if ( $value === false ) {
/* Set the default
- don't hook into WP as no validation is used and it would break when adding the option as new */
$value = array();
}
$this->cache = $value;
}
/**
* Refresh the $cache property when our property is added to wp
*
* @since 3.2
*
* @param $option_name Name of the option added
* @param $value Option value
* @return void
*/
public function on_add_cache_option( $option_name, $value ) {
$this->refresh_cache( $value );
}
/**
* Refresh the $cache property when our property is updated
*
* @since 3.2
*
* @param $old_value Original option value
* @param $value New option value
* @return void
*/
public function on_update_cache_option( $old_value, $value ) {
$this->refresh_cache( $value );
}
/**
* Update cached filesizes
*
* @since 3.2 - replaces get_set_...() method
*
* @param array|null $update New cache to save to db - make sure the new array
* is validated first!
* @param string|null $key file key to update the cache for
* @return bool|void if an update took place: whether it worked
*/
private function update_cache( $update, $key = null ) {
$updated = null;
// Is this a complete or a one field update ?
if ( ! is_null( $key ) ) {
$new_cache = $this->cache;
$new_cache[ $key ] = array(
'size' => $update, // file size or false if size could not be determined
'time' => time(),
);
$update = $new_cache;
unset( $new_cache );
}
if ( $update !== $this->cache ) {
$updated = update_option( self::CACHE_OPTION, $update );
}
else {
$updated = true; // no update necessary
}
return $updated;
}
/** ******************* ADMINISTRATIVE METHODS ******************* **/
/**
* Make sure our text strings and properties are available
* @since 3.2
*/
public function pre_init() {
/* Allow filtering of our plugin name */
self::filter_statics();
/* Load plugin text strings */
load_plugin_textdomain( 'mimetypes-link-icons', false, self::$name . '/languages' );
/* Translate a number of strings */
$this->set_properties();
}
/**
* Allow filtering of the plugin name
* Mainly useful for non-standard directory setups
*
* @since 3.2
*
* @return void
*/
public static function filter_statics() {
/* Allow filtering of the plugin name, Mainly useful for non-standard directory setups
@api string $plugin_name plugin name */
self::$name = apply_filters( 'mimetype_link_icons_plugin_name', self::$name );
}
/**
* Fill some property arrays with translated strings
* @since 3.0
*/
private function set_properties() {
$this->alignments = array(
'left' => __( 'Left', 'mimetypes-link-icons' ),
'right' => __( 'Right', 'mimetypes-link-icons' ),
);
$this->form_sections = array(
'general' => __( 'General Settings', 'mimetypes-link-icons' ),
'images' => __( 'Image Settings', 'mimetypes-link-icons' ),
'advanced' => __( 'Advanced Settings', 'mimetypes-link-icons' ),
);
$this->byte_suffixes = array(
_x( 'b', 'Abbreviation of "byte"', 'mimetypes-link-icons' ),
__( 'kB', 'mimetypes-link-icons' ),
__( 'MB', 'mimetypes-link-icons' ),
__( 'GB', 'mimetypes-link-icons' ),
__( 'TB', 'mimetypes-link-icons' ),
__( 'PB', 'mimetypes-link-icons' ),
__( 'EB', 'mimetypes-link-icons' ),
__( 'ZB', 'mimetypes-link-icons' ),
__( 'YB', 'mimetypes-link-icons' ),
);
}
/**
* Add the actions for the front end functionality
*/
public function init() {
/**
* Filter hook for active mime types list
* @api array Allows a developer to filter (add/remove) mimetypes from the array of mimetypes
* for which the plugin should be active as selected by the admin on the settings
* page
*/
$this->active_mimetypes = apply_filters( 'mtli_active_mimetypes', $this->active_mimetypes );
/* Validate/sanitize the active mime types array */
$this->active_mimetypes = array_filter( $this->active_mimetypes, 'is_string' );
$this->active_mimetypes = array_map( 'strtolower', $this->active_mimetypes );
$this->active_mimetypes = preg_grep( '`^[a-z0-9]{2,8}$`', $this->active_mimetypes );
// Don't do anything if no active_mimetypes or if we're not on the frontend
if ( false === is_admin() && !wp_is_json_request() && array() !== $this->active_mimetypes ) {
/* Register the_content filter */
if ( false === $this->settings['enable_async'] || true === $this->settings['show_file_size'] ) {
add_filter( 'the_content', array( $this, 'mimetype_to_icon' ), 15 );
add_filter( 'acf_the_content', array( $this, 'mimetype_to_icon' ), 15 );
}
/* Add js and css files */
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
}
}
/**
* Add the actions for the back-end functionality
*/
public function admin_init() {
/* Don't do anything if user does not have the required capability */
if ( false === is_admin() || false === current_user_can( self::REQUIRED_CAP ) ) {
return;
}
/* Register the settings sections and their callbacks */
foreach ( $this->form_sections as $section => $title ) {
add_settings_section(
'mtli-' . $section . '-settings', // id
$title, // title
array( $this, 'do_settings_section_' . $section ), // callback for this section
self::$name // page menu_slug
);
}
/* Add settings link on plugin page */
add_filter( 'plugin_action_links_' . self::$basename , array( $this, 'add_settings_link' ), 10, 2 );
/* Add js and css files */
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
/* Add help tabs for our settings page */
add_action( 'load-' . $this->hook, array( $this, 'add_help_tab' ) );
}
/**
* Register the options page for all users that have the required capability
*/
public function add_options_page() {
$this->hook = add_options_page(
__( 'MimeType Link Icons', 'mimetypes-link-icons' ), /* page title */
__( 'MimeType Icons', 'mimetypes-link-icons' ), /* menu title */
self::REQUIRED_CAP, /* capability */
self::$name, /* menu slug */
array( $this, 'display_options_page' ) /* function for subpanel */
);
}
/**
* Add settings link to plugin row
*
* @since 3.0
*
* @param array $links Current links for the current plugin
* @param string $file The file for the current plugin
* @return array
*/
public function add_settings_link( $links, $file ) {
if ( self::$basename === $file && current_user_can( self::REQUIRED_CAP ) ) {
$links[] = '<a href="' . esc_url( $this->plugin_options_url() ) . '" alt="' . esc_attr__( 'MimeType Link Icons Settings', 'mimetypes-link-icons' ) . '">' . esc_html__( 'Settings', 'mimetypes-link-icons' ) . '</a>';
}
return $links;
}
/**
* Return absolute URL of options page
*
* @since 3.0
*
* @return string
*/
private function plugin_options_url() {
return add_query_arg( 'page', self::$name, admin_url( self::PARENT_PAGE ) );
}
/**
* Conditionally enqueue scripts and styles for front-end pages
* @todo: Probably quite difficult in this case: see if we can load our scripts and styles conditionally, i.e. only on the pages where used
* @todo: For now: may be add the active mimetypes as an encoded setting to the url so as only to generate the css rules for the active mimetypes
* @todo: May be generate a .css file on a settings save to avoid having to generate the .css file on each page load
* @todo: Also generate a .min.css file
*/
public function wp_enqueue_scripts() {
wp_register_style(
self::$name, // id
add_query_arg(
'cssvars',
base64_encode( 'mtli_height=' . $this->settings['image_size'] . '&mtli_image_type=' . $this->settings['image_type'] . '&mtli_leftorright=' . $this->settings['leftorright'] . '&active_types=' . implode('|', $this->active_mimetypes) ),
plugins_url( 'css/style.php', __FILE__ )
), // url
array(), // not used
self::STYLES_VERSION, // version
'all'
);
wp_enqueue_style( self::$name );
if ( ( true === $this->settings['enable_hidden_class'] && ( is_array( $this->settings['hidden_classname'] ) && array() !== $this->settings['hidden_classname'] ) ) || ( true === $this->settings['enable_async'] && ( is_array( $this->active_mimetypes ) && array() !== $this->active_mimetypes ) ) ) {
wp_enqueue_script(
self::$name, // id
plugins_url( 'js/mtli-str-replace' . self::$suffix . '.js', __FILE__ ), // url
array( 'jquery' ), // dependants
self::SCRIPTS_VERSION, // version
true // load in footer
);
wp_localize_script( self::$name, 'i18n_mtli', $this->get_javascript_i18n() );
}
}
/**
* Retrieve the strings for use in the javascript file
*
* @since 3.0
* @usedby wp_enqueue_scripts()
*
* @return array
*/
private function get_javascript_i18n() {
$strings = array(
'hidethings' => ( ( true === $this->settings['enable_hidden_class'] && ( is_array( $this->settings['hidden_classname'] ) && array() !== $this->settings['hidden_classname'] ) ) ? true : false ),
'enable_async' => ( ( true === $this->settings['enable_async'] && ( is_array( $this->active_mimetypes ) && array() !== $this->active_mimetypes ) ) ? true : false ),
'enable_async_debug' => ( ( true === $this->settings['enable_async_debug'] && ( is_array( $this->active_mimetypes ) && array() !== $this->active_mimetypes ) ) ? true : false ),
);
/* Add jQuery class selector string if hidden classes are used */
if ( true === $this->settings['enable_hidden_class'] && ( is_array( $this->settings['hidden_classname'] ) && array() !== $this->settings['hidden_classname'] ) ) {
$strings['avoid_selector'] = '';
foreach ( $this->settings['hidden_classname'] as $classname ) {
$strings['avoid_selector'] .= '.' . $classname . ',';
}
$strings['avoid_selector'] = substr( $strings['avoid_selector'], 0, -1 );
}
/* Add array of active mimetypes if in async mode*/
if ( true === $this->settings['enable_async'] && ( is_array( $this->active_mimetypes ) && array() !== $this->active_mimetypes ) ) {
$strings['mime_array'] = $this->active_mimetypes;
}
return $strings;
}
/**
* Adds necessary javascript and css files for the back-end on the appropriate screen
*/
public function admin_enqueue_scripts() {
$screen = get_current_screen();
if ( property_exists( $screen, 'base' ) && $screen->base === $this->hook ) {
wp_enqueue_script(
self::$name, // id
plugins_url( 'js/mtli-admin' . self::$suffix . '.js', __FILE__ ), // url
array( 'jquery' ), // dependants
self::ADMIN_SCRIPTS_VERSION, // version
true // load in footer
);
wp_enqueue_style(
self::$name, // id
plugins_url( 'css/admin-style' . self::$suffix . '.css', __FILE__ ), // url
array(), // not used
self::ADMIN_STYLES_VERSION, // version
'all'
);
wp_localize_script( self::$name, 'i18n_mtli', $this->get_admin_javascript_i18n() );
}
}
/**
* Retrieve the strings for use in the javascript file
*
* @since 3.0
* @usedby admin_enqueue_scripts()
*
* @return array
*/
private function get_admin_javascript_i18n() {
$strings = array(
'togglebox' => '<div class="check-images"><span class="check-all">' . esc_html__( 'Check All', 'mimetypes-link-icons' ) . '</span>|<span class="uncheck-all">' . esc_html__( 'Uncheck All', 'mimetypes-link-icons' ) . '</span></div>',
);
return $strings;
}
/**
* Adds contextual help tab to the plugin page
*
* @since 3.0
*/
public function add_help_tab() {
$screen = get_current_screen();
if ( property_exists( $screen, 'base' ) && $screen->base === $this->hook ) {
$screen->add_help_tab(
array(
'id' => self::$name . '-main', // This should be unique for the screen.
'title' => __( 'MimeType Link Icons', 'mimetypes-link-icons' ),
'callback' => array( $this, 'get_helptext' ),
)
);
$screen->add_help_tab(
array(
'id' => self::$name . '-advanced', // This should be unique for the screen.
'title' => __( 'Advanced Settings', 'mimetypes-link-icons' ),
'callback' => array( $this, 'get_helptext' ),
)
);
$screen->add_help_tab(
array(
'id' => self::$name . '-extras', // This should be unique for the screen.
'title' => __( 'Extras', 'mimetypes-link-icons' ),
'callback' => array( $this, 'get_helptext' ),
)
);
$screen->set_help_sidebar( $this->get_help_sidebar() );
}
}
/**
* Function containing the helptext string
*
* @since 3.0
*
* @param object $screen
* @param $tab
* @return string help text
*/
public function get_helptext( $screen, $tab ) {
$helptext[ self::$name . '-main' ] = '
<p>' .
/* Translators: %s = link target. */
sprintf( __( 'The <em><a href="%s">MimeTypes Link Icons</a></em> plugin will automatically add an icon next to links of the activated file types. If you like, you can also let the plugin add the file size of the linked file to the page.', 'mimetypes-link-icons' ), 'http://wordpress.org/plugins/mimetypes-link-icons/" target="_blank" class="ext-link' ) . '</p>
<p>' . esc_html__( 'On this settings page you can specify the icon size, icon type (white matte gif or transparent png), icon alignment. You can also select the file types for which this plugin will be enabled.', 'mimetypes-link-icons' ) . '</p>';
$helptext[ self::$name . '-advanced' ] = '
<p>' . __( 'In the advanced settings, you can enable <em>"exclusion classnames"</em>, enable the display of the <em>file size</em> of a linked file and/or choose to use <em>asynchronous replacement</em>.', 'mimetypes-link-icons' ) . '</p>
<p>' . __( '<strong>"Exclusion classnames"</strong> works as follows:', 'mimetypes-link-icons' ) . '<br />
' . esc_html__( 'The plugin will look for the classname in your document and will remove the Mimetypes link icons (and file sizes) from all links wrapped within that class. You can add several classnames, just separate them with a comma.', 'mimetypes-link-icons' ) . '</p>';
$helptext[ self::$name . '-extras' ] = '
<p>' . __( 'There is even some more advanced functionality available: for instance an <em>output filter</em> for the file size output and a way to add the plugin\'s functionality to widgets or other areas of your blog outside of the main content area.', 'mimetypes-link-icons' ) . '</p>
<p>' .
/* Translators: %1$s = <a> tag, %2$s is closing</a> tag. */
sprintf( esc_html__( 'For more information on these tasty extras, have a look at the %1$sFAQ%2$s', 'mimetypes-link-icons' ), '<a href="http://wordpress.org/plugins/mimetypes-link-icons/faq/" target="_blank" class="ext-link">', '</a>' ) . '</p>';
echo $helptext[ $tab['id'] ];
}
/**
* Generate the links for the help sidebar
*
* @return string
*/
private function get_help_sidebar() {
return '
<p><strong>' . esc_html__( 'For more information:', 'mimetypes-link-icons' ) . '</strong></p>
<p>
<a href="http://wordpress.org/plugins/mimetypes-link-icons/" target="_blank">' . esc_html__( 'Official plugin page', 'mimetypes-link-icons' ) . '</a> |
<a href="http://wordpress.org/plugins/mimetypes-link-icons/faq/" target="_blank">' . esc_html__( 'FAQ', 'mimetypes-link-icons' ) . '</a> |
<a href="http://wordpress.org/plugins/mimetypes-link-icons/changelog/" target="_blank">' . esc_html__( 'Changelog', 'mimetypes-link-icons' ) . '</a> |
<a href="http://wordpress.org/support/plugin/mimetypes-link-icons" target="_blank">' . esc_html__( 'Support Forum', 'mimetypes-link-icons' ) . '</a>
</p>
<p><a href="https://github.com/eagerterrier/MimeTypes-Link-Icons" target="_blank">' . esc_html__( 'Github repository', 'mimetypes-link-icons' ) . '</a></p>
<p><a href="http://blog.eagerterrier.co.uk/2010/10/holy-cow-ive-gone-and-made-a-mime-type-wordpress-plugin/" target="_blank">' . esc_html__( 'Blog post about this plugin', 'mimetypes-link-icons' ) . '</a></p>
';
}