-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
216 lines (153 loc) · 5.16 KB
/
functions.php
File metadata and controls
216 lines (153 loc) · 5.16 KB
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
<?php
/*
If you want to use it i WordPress, copy + paste
this is for your WordPress Theme functions.php
*/
function srcbox_add_image_sizes () {
add_image_size( 'mobile', 480 );
add_image_size( 'retina@2', 640 );
add_image_size( 'tablet', 900 );
add_image_size( 'desktop', 1170 );
add_image_size( 'retina@3', 2048 );
}
add_action( 'init', 'srcbox_add_image_sizes' );
function srcbox_add_admin_menu ( ) {
add_options_page( 'srcBox', 'srcBox', 'manage_options', 'srcBox', 'srcbox_options_page' );
}
function srcbox_settings_init ( ) {
register_setting( 'srcPage', 'srcbox_settings' );
add_settings_section(
'srcbox_srcPage_section',
__( 'srcBox breakpoints', 'wordpress' ),
'srcbox_settings_section_callback',
'srcPage'
);
add_settings_field (
'srcbox_checkbox_field_0',
__( 'Breakpoints', 'wordpress' ),
'srcbox_checkbox_field_0_render',
'srcPage',
'srcbox_srcPage_section'
);
}
function srcbox_checkbox_field_0_render ( ) {
$options = get_option( 'srcbox_settings' );
if ( !$options ) $options = array();
$html = '';
$_breakpoints = array(
array(
'breakpoint_id' => 'breakpoint_480',
'name' => 'breakpoint mobile'
),
array(
'breakpoint_id' => 'breakpoint_640',
'name' => 'breakpoint mobile retina @2'
),
array(
'breakpoint_id' => 'breakpoint_900',
'name' => 'breakpoint tablet'
),
array(
'breakpoint_id' => 'breakpoint_1170',
'name' => 'breakpoint desktop'
),
array(
'breakpoint_id' => 'breakpoint_2048',
'name' => 'breakpoint tablet retina @2 / mobile retina @3'
),
);
foreach ($_breakpoints as $breakpoint) {
$checked = in_array($breakpoint['breakpoint_id'], $options) ? 'checked="checked"' : '';
$disabled = !empty($checked) && $breakpoint['breakpoint_id'] !== 'breakpoint_2048' ? 'disabled' : '';
$html .= sprintf( '<input type="checkbox" id="%1$s[%2$s]" name="%1$s[]" value="%2$s" %3$s %4$s />', 'srcbox_settings', $breakpoint['breakpoint_id'], $checked, $disabled );
$html .= sprintf( '<label for="%1$s[%3$s]"> %2$s</label><br>', 'srcbox_settings', $breakpoint['name'], $breakpoint['breakpoint_id'] );
}
echo $html;
}
function srcbox_settings_section_callback( ) {
echo __( 'Bij voorkeur alle checkboxes aanvinken, voor elk apparaat wordt dan een aparte afbeelding ingeladen.', 'wordpress' );
}
function srcbox_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>srcBox</h2>
<?php
settings_fields( 'srcPage' );
do_settings_sections( 'srcPage' );
submit_button();
?>
</form>
<?php
}
add_action( 'admin_menu', 'srcbox_add_admin_menu' );
add_action( 'admin_init', 'srcbox_settings_init' );
function srcbox_handle_upload_prefilter ( $file ) {
$options = get_option( 'srcbox_settings' );
$minimum_width = (int) str_replace('breakpoint_', '', end($options));
$img = getimagesize($file['tmp_name']);
$width = $img[0];
if ( $width < $minimum_width ) {
$e_settings = __('Settings');
return array(
"error" => "Image dimensions are too small.
Minimum width is {$minimum_width}px.
Uploaded image width is {$width} px.
Remove the largest breakpoints, like 2048 and perhaps 1170,
from \"{$e_settings} >srcBox\" to prevent this message."
);
} else {
return $file;
}
}
add_filter( 'wp_handle_upload_prefilter', 'srcbox_handle_upload_prefilter' );
function srcbox_rename_intermediates ( $image ) {
// Split the $image path into directory/extension/name
$info = pathinfo( $image );
$dir = $info['dirname'] . '/';
$ext = '.' . $info['extension'];
$name = wp_basename( $image, "$ext" );
// Build our new image name
$name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
$size_extension = substr( $name, strrpos( $name, '-' ) + 1, 3 );
$new_name = $dir . $name_prefix . '-' . $size_extension . $ext;
// Rename the intermediate size
$did_it = rename( $image, $new_name );
// Renaming successful, return new name
if( $did_it ) return $new_name;
return $image;
}
// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'srcbox_rename_intermediates' );
function srcbox_post_thumbnail_html ( $html ) {
if ( empty($html) ) return $html;
preg_match(
'/< *img[^>]*src *="[^"]*(uploads\/)(.+\\/.+\\-[0-9]+(.jpg|.jpeg|.png|.gif))/i',
preg_replace(
'/(https?:\\/\\/)(.+\\/)(wp-content\/)(.+\\/)(.+\\-)([0-9]+)(.jpg|.jpeg|.png|.gif)/',
'$4${5}640$7',
$html
),
$is_src_boxed
);
$upload_dir = wp_upload_dir();
if ( file_exists( $upload_dir['basedir'] . '/' . $is_src_boxed[2] ) ) {
$html = preg_replace(
'/src="(https?:\\/\\/.+\\/)(.+\\-)([0-9]+)(.jpg|.jpeg|.png|.gif)"/',
'src="" data-breakpoint="$1" data-img="$2{folder}$4"',
preg_replace(
'/( width| height)=["\']\d*["\']\s?/',
'',
preg_replace(
'/<img ?([^>]*)class="([^"]*)"?/',
'<img $1 class="$2 srcbox thumbnail"',
$html
)
)
);
return $html;
}
return $html;
}
add_filter( 'post_thumbnail_html', 'srcbox_post_thumbnail_html', 99, 1 );
add_filter( 'the_content', 'srcbox_post_thumbnail_html', 10 );
add_filter( 'get_avatar', 'srcbox_post_thumbnail_html', 10 );