forked from envato/wp-image-size-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-image-size-limit.php
135 lines (105 loc) · 2.94 KB
/
wp-image-size-limit.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
<?php
/*
Plugin Name: WP Image Size Limit
Plugin URI: http://wordpress.org/extend/plugins/wp-image-size-limit
Description: Allows setting a maximum file size for image uploads.
Author: Sean Butze
Author URI: http://www.seanbutze.com
Version: 1.0.4
*/
define('WPISL_DEBUG', false);
require_once ('wpisl-options.php');
class WP_Image_Size_Limit {
public function __construct() {
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'add_plugin_links') );
add_filter('wp_handle_upload_prefilter', array($this, 'error_message'));
}
public function add_plugin_links( $links ) {
return array_merge(
array(
'settings' => '<a href="' . get_bloginfo( 'wpurl' ) . '/wp-admin/options-media.php?settings-updated=true#wpisl-limit">Settings</a>'
),
$links
);
}
public function get_limit() {
$option = get_option('wpisl_options');
if ( isset($option['img_upload_limit']) ){
$limit = $option['img_upload_limit'];
} else {
$limit = $this->wp_limit();
}
return $limit;
}
public function output_limit() {
$limit = $this->get_limit();
$limit_output = $limit;
$mblimit = $limit / 1000;
if ( $limit >= 1000 ) {
$limit_output = $mblimit;
}
return $limit_output;
}
public function wp_limit() {
$output = wp_max_upload_size();
$output = round($output);
$output = $output / 1000000; //convert to megabytes
$output = round($output);
$output = $output * 1000; // convert to kilobytes
return $output;
}
public function limit_unit() {
$limit = $this->get_limit();
if ( $limit < 1000 ) {
return 'KB';
}
else {
return 'MB';
}
}
public function is_limited_type($type) {
$options = get_option('wpisl_options');
$pattern = $options['img_upload_limit_types_regex'];
return preg_match($pattern, $type);
}
public function error_message($file) {
$size = $file['size'];
$size = $size / 1024;
$type = $file['type'];
$limit = $this->get_limit();
$limit_output = $this->output_limit();
$unit = $this->limit_unit();
if ( ( $size > $limit ) && ($this->is_limited_type($type)) ) {
$file['error'] = 'Image files must be smaller than '.$limit_output.$unit;
if (WPISL_DEBUG) {
$file['error'] .= ' [ filesize = '.$size.', limit ='.$limit.' ]';
}
}
return $file;
}
public function load_styles() {
$limit = $this->get_limit();
$limit_output = $this->output_limit();
$mblimit = $limit / 1000;
$wplimit = $this->wp_limit();
$unit = $this->limit_unit();
?>
<!-- .Custom Max Upload Size -->
<style type="text/css">
.after-file-upload {
display: none;
}
<?php if ( $limit < $wplimit ) : ?>
.upload-flash-bypass:after {
content: 'Maximum image size: <?php echo $limit_output . $unit; ?>.';
display: block;
margin: 15px 0;
}
<?php endif; ?>
</style>
<!-- END Custom Max Upload Size -->
<?php
}
}
$WP_Image_Size_Limit = new WP_Image_Size_Limit;
add_action('admin_head', array($WP_Image_Size_Limit, 'load_styles'));