forked from noeltock/hammy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammy.php
252 lines (162 loc) · 6 KB
/
hammy.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
<?php
/*
Plugin Name: Hammy
Plugin URI: http://wordpress.org/extend/plugins/hammy/
Description: Creates responsive images for your content area with breakpoints that you set.
Author: Noel Tock
Version: 1.0.1
Author URI: http://www.noeltock.com
*/
/**
* Defines
*/
define ( 'HAMMY_VERSION', '1.0' );
define ( 'HAMMY_PATH', WP_PLUGIN_URL . '/' . end( explode( DIRECTORY_SEPARATOR, dirname( __FILE__ ) ) ) );
/**
* Register Default Settings
*/
if (get_option('hammy_options')== '') {
register_activation_hook(__FILE__, 'hammy_defaults');
}
function hammy_defaults() {
$arr = array('hammy_breakpoints' => '320,480,768', 'hammy_ignores' => 'nextgen, thumbnail', 'hammy_parent' => '.entry-content', 'hammy_lazy' => '');
update_option('hammy_options', $arr);
}
/**
* Check for WPThumb, include.
*
*/
if ( !function_exists('wpthumb') ) {
include_once('WPThumb/wpthumb.php');
}
/**
* Plugin Options
*/
include_once('includes/options.php');
add_action('admin_init', 'hammy_options_init' );
add_action('admin_menu', 'hammy_add_page');
/**
* Enqueue & Localize JavaScript & CSS
*/
function load_hammy_js() {
$options = get_option('hammy_options');
if ($options['hammy_lazy']) {
wp_enqueue_script( 'jquery-picture', HAMMY_PATH . '/js/jquery-picture-lazy.js', array('jquery'),null,true );
wp_enqueue_script('lazyload', HAMMY_PATH . '/js/jquery.lazyload.min.js', array('jquery'),null, true );
wp_enqueue_script( 'hammy', HAMMY_PATH . '/js/hammy-lazy.js', array('jquery'),null,true );
} else {
wp_enqueue_script( 'jquery-picture', HAMMY_PATH . '/js/jquery-picture.js', array('jquery'),null,true );
wp_enqueue_script( 'hammy', HAMMY_PATH . '/js/hammy.js', array('jquery'),null,true );
}
wp_localize_script( 'hammy', 'imageParent', $options['hammy_parent'] );
}
add_action('wp_print_scripts', 'load_hammy_js');
function load_hammy_css() {
wp_enqueue_style('hammy-css', HAMMY_PATH . '/css/hammy-admin.css');
}
add_action('admin_print_styles', 'load_hammy_css');
/**
* Hammy Time
*
* @param $content
* @return DOM element with fallback (<picture> -> <img>)
*/
function hammy_replace_images( $content ) {
global $post;
$options = get_option('hammy_options');
preg_match_all('/<img (.*?)\/>/', $content, $images);
if( !is_null( $images ) ) {
foreach( $images[0] as $index => $value) {
$doc = new DOMDocument();
$doc->loadHTML($value);
$items = $doc->getElementsByTagName('img');
foreach($items as $item) {
// Get Attributes
$original = $item->getAttribute('src');
$width = $item->getAttribute('width');
$class = $item->getAttribute('class');
$alt = $item->getAttribute('alt');
// Check if it's part of an ignored class
$ignoreClasses = explode(",", $options['hammy_ignores']);
$ignorelist = '/' . implode("|", $ignoreClasses) . '/';
if ( !preg_match( $ignorelist, $class) ) {
// Get Sizes
$sizes = explode(",", $options['hammy_breakpoints']);
// Render Sizes
$i = 0;
$breakpoint = null;
// Output & Replace Strings
$newimage = '<picture class="hammy-responsive ' . $class . '" alt="' . $alt . '">';
foreach ($sizes as $size) {
if ( $i == 0 ) {
$media = null;
} else {
$media = ' media="(min-width:' . $breakpoint . 'px)"';
}
if ( $size <= $width ) {
$resized_image = wpthumb( $original, 'width=' . $size . '&crop=0' );
$newimage .= '<source src="' . $resized_image . '"' . $media . '>';
}
$i++;
$breakpoint = $size;
}
$newimage .= '<noscript><img src="' . $original . '" alt="' . $alt . '"></noscript></picture>';
$content = str_replace($images[0][$index], $newimage, $content);
}
}
}
}
return $content;
}
add_filter('the_content', 'hammy_replace_images', 999);
/**
* Hammy, please give me some adaptive images even if I'm not using the_content();
* Modified by Jacques Letesson (@jacquesletesson)
*
* @params
* - $id (int) - ID of the image
* - $class (string) - Class(es) of the image
* - $size (string) - The thumbnail size to use
* - $caption (boolean) - Display the caption of the image, or not
*
* @return DOM element with fallback (<picture> -> <img>)
*/
function hammy_please_replace_images( $id, $class = "", $size = "large", $caption = false ) {
$options = get_option('hammy_options');
// Get Sizes
$sizes = explode(",", $options['hammy_breakpoints']);
// Render Sizes
$i = 0;
$breakpoint = null;
// Get the image and its metadata
$original = wp_get_attachment_image_src( $id, $size );
$metadata = wp_get_attachment_metadata($id);
$data = get_post($id);
// Get the caption and the alt text
$caption = $data->post_excerpt;
$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
list($width, $height, $type, $attr)= getimagesize($original[0]);
$newimage = '<picture class="hammy-responsive ' . $class . '" alt="' . $alt . '">';
foreach ($sizes as $size) {
if ( $i == 0 ) {
$media = null;
} else {
$media = ' media="(min-width:' . $breakpoint . 'px)"';
}
if ( $size <= $width ) {
$resized_image = wpthumb( $original[0], 'width=' . $size . '&crop=0' );
$newimage .= '<source src="' . $resized_image . '"' . $media . '>';
}
$i++;
$breakpoint = $size;
}
$newimage .= '<noscript><img src="' . $original[0]. '" alt="' . $alt . '"></noscript>';
// If has a caption display it inside a figcaption
if ($caption) {
$newimage .= '<figcaption>' . $caption . '</figcaption></picture>';
} else {
$newimage .= '</picture>';
}
echo $newimage;
}
?>