This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpi.easy_jail.php
245 lines (201 loc) · 6.48 KB
/
pi.easy_jail.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
<?php
/*
=====================================================
Easy JAIL - by Easy Designs, LLC
-----------------------------------------------------
http://easy-designs.net/
=====================================================
This extension was created by Aaron Gustafson
(aaron@easy-designs.net) with contributions from
Jens Korff
This work is licensed under the MIT License.
=====================================================
File: pi.easy_jail.php
-----------------------------------------------------
Purpose: Automates the implementation of Sebastiano
Armeli-Battana’s jQuery Asynchronous Image Loader
Plugin
=====================================================
*/
$plugin_info = array(
'pi_name' => 'Easy JAIL',
'pi_version' => '1.0',
'pi_author' => 'Aaron Gustafson',
'pi_author_url' => 'http://easy-designs.net/',
'pi_description' => 'Automates the implementation of Sebastiano Armeli-Battana’s jQuery Asynchronous Image Loader Plugin',
'pi_usage' => Easy_jail::usage()
);
class Easy_jail {
var $return_data;
var $template = '<img class="{class_name}" src="{blank_img}" data-src="{real_img}" {attributes}/><noscript><img src="{real_img}" {attributes}/></noscript>';
var $xhtml = TRUE;
var $class_name = 'jail';
var $blank_img = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
var $alt = '';
var $config = '';
/**
* Easy_jail constructor
* sets any overrides and triggers the processing
*
* @param str $str - the content to be parsed
*/
function __construct()
{
$this->return_data = ee()->TMPL->tagdata;
} # end Easy_jail constructor
/**
* Easy_jail::prep()
* processes the supplied content based on the configuration
*
* @param str $str - the content to be parsed
*/
function prep( $str='', $xhtml='', $class_name='', $blank_img='' )
{
# get any tag overrides
if ( empty( $xhtml ) )
{
$xhtml = ee()->TMPL->fetch_param( 'xhtml', $this->xhtml );
}
if ( empty( $class_name ) )
{
$class_name = ee()->TMPL->fetch_param( 'class_name', $this->class_name );
}
if ( empty( $blank_img ) )
{
$blank_img = ee()->TMPL->fetch_param( 'blank_img', ( empty( $blank_img ) ? $this->blank_img : $blank_img ) );
}
# Fetch string
if ( empty( $str ) )
{
$str = ee()->TMPL->tagdata;
}
# trim
$str = trim( $str );
$lookup = '/(<img([^>]*)\/?>)/';
if ( preg_match_all( $lookup, $str, $found, PREG_SET_ORDER ) )
{
# loop the matches
foreach ( $found as $instance )
{
$o_img = $instance[1];
$src = '';
# get the attributes
$attributes = array();
# remove the /
if ( substr( $instance[2], -1, 1 ) == '/' )
{
$instance[2] = substr( $instance[2], 0, -1 );
}
# Get all attributes
# Reference: http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php#answer-2937682
$doc = new DOMDocument();
@$doc->loadHTML($o_img);
$tags = $doc->getElementsByTagName('img');
foreach ( $tags as $tag )
{
foreach ( $tag->attributes as $attribute )
{
$name = $attribute->name;
$value = $attribute->value;
if ( $name == 'src' )
{
$src = $value;
}
elseif ( $name == 'class' )
{
$class_name .= " {$value}";
}
else
{
$attributes[$name] = $name . '="' . $value . '"';
}
}
}
# enforce an alt attribute
if ( ! isset( $attributes['alt'] ) )
{
$attributes['alt'] = $this->alt;
}
# build the new image
$swap = array(
'attributes' => implode( ' ', $attributes ),
'class_name' => $class_name,
'blank_img' => $blank_img,
'real_img' => $src
);
$n_img = ee()->functions->var_swap( $this->template, $swap );
# XHTML?
if ( ! $xhtml )
{
$n_img = str_replace( '/>', '>', $n_img );
}
$str = str_replace( $o_img, $n_img, $str );
} # end foreach instance
} # end if match
$this->return_data = $str;
return $this->return_data;
} # end Easy_jail::prep()
/**
* Easy_jail::js()
* Describes how the plugin is used
*/
function js( $class_name='', $config='' )
{
$js = '';
# get tag params
if ( empty( $class_name ) )
{
$class_name = ee()->TMPL->fetch_param( 'class_name', $this->class_name );
}
if ( empty( $config ) )
{
$config = ee()->TMPL->fetch_param( 'config', $this->config );
}
# get JAIL
$js .= file_get_contents( PATH_THIRD . '/easy_jail/vendors/jail/dist/jail.min.js' ) . "\n\n";
# build the trigger
$template = '(function(window,$){$("img.{class_name}").jail({jail_config});$(window).on("load",function(){$(this).resize();});}(this,jQuery));';
$swap = array(
'class_name' => $class_name,
'jail_config' => $config
);
$js .= ee()->functions->var_swap( $template, $swap );
$this->return_data = '<script>' . $js . '</script>';
return $this->return_data;
} # end Easy_jail::js()
/**
* Easy_jail::usage()
* Describes how the plugin is used
*/
function usage()
{
ob_start(); ?>
First off, this plugin requires jQuery. Using it requires 2 steps:
1) Wrap the markup you want to JAIL in {exp:easy_jail:prep}
{exp:easy_jail:prep}
{body}
{/exp:easy_jail:prep}
This will cause the plugin to convert
<img src="foo.png" alt=""/>
into
<img class="jail" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="foo.png" alt=""/>
<noscript><img src="foo.png" alt=""/></noscript>
Providing it with additional params allows you to customize certain bits:
* xhtml="n" - HTML output
* blank_img="my_blank.gif" - Your custom blank image
* class_name="custom_class" - Your custom class choice
2) Include {exp:easy_jail:js} at the end of your body element, after you included jQuery.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/j/jquery.js"><\/script>')</script>
{exp:easy_jail:js}
By default, this will include the JAIL script and a baseline configuration. To configure the output of the script, you can use the following parameters:
* class_name="custom_class" - Your custom class choice
* config="{offset:300}" - Your custom configuration (see http://sebarmeli.github.io/JAIL/ for a run-down of options)
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
} # end easy_jail::usage()
} # end Easy_jail
/* End of file pi.easy_jail.php */
/* Location: ./system/expressionengine/third_party/easy_jail/pi.easy_jail.php */