@@ -201,11 +201,14 @@ public function renderChart( $atts ) {
201
201
global $ wp_version ;
202
202
$ atts = shortcode_atts (
203
203
array (
204
- 'id ' => false , // chart id
205
- 'class ' => false , // chart class
206
- 'series ' => false , // series filter hook
207
- 'data ' => false , // data filter hook
208
- 'settings ' => false , // data filter hook
204
+ // chart id
205
+ 'id ' => false ,
206
+ // chart class
207
+ 'class ' => false ,
208
+ // for lazy load
209
+ // set 'yes' to use the default intersection limit (300px)
210
+ // OR set a number (e.g. 700) to use 700px as the intersection limit
211
+ 'lazy ' => apply_filters ( 'visualizer_lazy_by_default ' , false , $ atts ['id ' ] ),
209
212
),
210
213
$ atts
211
214
);
@@ -215,6 +218,7 @@ public function renderChart( $atts ) {
215
218
return '' ;
216
219
}
217
220
221
+ // do not show the chart?
218
222
if ( ! apply_filters ( 'visualizer_pro_show_chart ' , true , $ atts ['id ' ] ) ) {
219
223
return '' ;
220
224
}
@@ -228,8 +232,18 @@ public function renderChart( $atts ) {
228
232
$ id = 'visualizer- ' . $ atts ['id ' ];
229
233
$ defaultClass = 'visualizer-front ' ;
230
234
$ class = apply_filters ( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS , $ atts ['class ' ], $ atts ['id ' ] );
231
- $ class = $ defaultClass . ' ' . $ class . ' ' . 'visualizer-front- ' . $ atts ['id ' ];
232
- $ class = ! empty ( $ class ) ? ' class=" ' . trim ( $ class ) . '" ' : '' ;
235
+
236
+ $ lazyClass = $ atts ['lazy ' ] === 'yes ' || ctype_digit ( $ atts ['lazy ' ] ) ? 'visualizer-lazy ' : '' ;
237
+
238
+ $ class = sprintf ( '%s %s %s %s ' , $ defaultClass , $ class , 'visualizer-front- ' . $ atts ['id ' ], $ lazyClass );
239
+ $ attributes = array ();
240
+ if ( ! empty ( $ class ) ) {
241
+ $ attributes ['class ' ] = trim ( $ class );
242
+ }
243
+
244
+ if ( ctype_digit ( $ atts ['lazy ' ] ) ) {
245
+ $ attributes ['data-lazy-limit ' ] = $ atts ['lazy ' ];
246
+ }
233
247
234
248
$ type = get_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_CHART_TYPE , true );
235
249
@@ -243,20 +257,12 @@ public function renderChart( $atts ) {
243
257
244
258
// handle series filter hooks
245
259
$ series = apply_filters ( Visualizer_Plugin::FILTER_GET_CHART_SERIES , get_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_SERIES , true ), $ chart ->ID , $ type );
246
- if ( ! empty ( $ atts ['series ' ] ) ) {
247
- $ series = apply_filters ( $ atts ['series ' ], $ series , $ chart ->ID , $ type );
248
- }
260
+
249
261
// handle settings filter hooks
250
262
$ settings = apply_filters ( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS , $ settings , $ chart ->ID , $ type );
251
- if ( ! empty ( $ atts ['settings ' ] ) ) {
252
- $ settings = apply_filters ( $ atts ['settings ' ], $ settings , $ chart ->ID , $ type );
253
- }
254
263
255
264
// handle data filter hooks
256
265
$ data = self ::get_chart_data ( $ chart , $ type );
257
- if ( ! empty ( $ atts ['data ' ] ) ) {
258
- $ data = apply_filters ( $ atts ['data ' ], $ data , $ chart ->ID , $ type );
259
- }
260
266
261
267
$ css = '' ;
262
268
$ arguments = $ this ->get_inline_custom_css ( $ id , $ settings );
@@ -271,7 +277,7 @@ public function renderChart( $atts ) {
271
277
272
278
$ amp = Visualizer_Plugin::instance ()->getModule ( Visualizer_Module_AMP::NAME );
273
279
if ( $ amp && $ amp ->is_amp () ) {
274
- return '<div id=" ' . $ id . '" ' . $ class . '> ' . $ amp ->get_chart ( $ chart , $ data , $ series , $ settings ) . '</div> ' ;
280
+ return '<div id=" ' . $ id . '" ' . $ this -> getHtmlAttributes ( $ attributes ) . '> ' . $ amp ->get_chart ( $ chart , $ data , $ series , $ settings ) . '</div> ' ;
275
281
}
276
282
277
283
// add chart to the array
@@ -314,8 +320,8 @@ public function renderChart( $atts ) {
314
320
315
321
$ actions_div .= $ css ;
316
322
317
- foreach ( $ this ->_charts as $ id => $ attributes ) {
318
- $ library = $ attributes ['library ' ];
323
+ foreach ( $ this ->_charts as $ id => $ array ) {
324
+ $ library = $ array ['library ' ];
319
325
wp_register_script (
320
326
"visualizer-render- $ library " ,
321
327
VISUALIZER_ABSURL . 'js/render-facade.js ' ,
@@ -344,7 +350,26 @@ public function renderChart( $atts ) {
344
350
}
345
351
346
352
// return placeholder div
347
- return $ actions_div . '<div id=" ' . $ id . '" ' . $ class . '></div> ' . $ this ->addSchema ( $ chart ->ID );
353
+ return $ actions_div . '<div id=" ' . $ id . '" ' . $ this ->getHtmlAttributes ( $ attributes ) . '></div> ' . $ this ->addSchema ( $ chart ->ID );
354
+ }
355
+
356
+ /**
357
+ * Converts the array of attributes to a string of HTML attributes.
358
+ *
359
+ * @since ?
360
+ *
361
+ * @access private
362
+ */
363
+ private function getHtmlAttributes ( $ attributes ) {
364
+ $ string = '' ;
365
+ if ( ! $ attributes ) {
366
+ return $ string ;
367
+ }
368
+
369
+ foreach ( $ attributes as $ name => $ value ) {
370
+ $ string .= sprintf ( '%s="%s" ' , esc_attr ( $ name ), esc_attr ( $ value ) );
371
+ }
372
+ return $ string ;
348
373
}
349
374
350
375
/**
0 commit comments