false,
'iframes' => false,
'backgrounds' => false,
'skip_classes' => '',
);
$this->options = wp_parse_args(WP_Optimize()->get_options()->get_option('lazyload'), $default_options);
$skip_classes = array_map('trim', explode(',', $this->options['skip_classes']));
$skip_classes[] = 'no-lazy';
$skip_classes[] = 'skip-lazy';
$this->options['skip_classes'] = apply_filters('wp_optimize_lazy_load_skip_classes', $skip_classes);
$hook_these = apply_filters('wp_optimize_lazy_load_hook_these', array('get_avatar', 'the_content', 'widget_text', 'get_image_tag', 'post_thumbnail_html', 'woocommerce_product_get_image', 'woocommerce_single_product_image_thumbnail_html'));
$hook_priority = apply_filters('wp_optimize_lazy_load_hook_priority', PHP_INT_MAX);
foreach ($hook_these as $hook) {
add_filter($hook, array($this, 'process_content'), $hook_priority);
}
}
/**
* Add lazy-load metabox to admin.
*/
public function add_lazyload_control_metabox() {
add_meta_box('wpo-lazyload-metabox', ''.__('Lazy-load', 'wp-optimize').'', array($this, 'render_lazyload_control_metabox'), get_post_types(array('public' => true)), 'side');
}
/**
* Render lazy-load metabox.
*/
public function render_lazyload_control_metabox($post) {
$post_id = $post->ID;
$meta_key = '_wpo_disable_lazyload';
$disable_lazyload = get_post_meta($post_id, $meta_key, true);
$post_type_obj = get_post_type_object(get_post_type($post_id));
$extract = array(
'disable_lazyload' => $disable_lazyload,
'post_id' => $post_id,
'post_type' => strtolower($post_type_obj->labels->singular_name),
);
WP_Optimize()->include_template('images/admin-metabox-lazyload-control.php', false, $extract);
}
/**
* Returns true if Lazy loading enabled.
*
* @return bool
*/
public function is_enabled() {
$post_id = get_queried_object_id();
// if disabled on the post editr page then return false.
if ($post_id && get_post_meta($post_id, '_wpo_disable_lazyload', true)) {
return false;
}
return $this->options['images'] || $this->options['iframes'] || $this->options['backgrounds'];
}
/**
* Filter the content and replace corresponding tags for lazy loading.
*
* @param string $content
* @return string
*/
public function process_content($content) {
if (!$this->is_enabled()) return $content;
if ($this->options['images']) {
$content = preg_replace_callback('/\/Uis', array($this, 'update_picture_callback'), $content);
$content = preg_replace_callback('/\
/Uis', array($this, 'update_images_callback'), $content);
}
if ($this->options['backgrounds']) {
$regexp = '/\<([a-z]+)\s+([^\>]*style=[\'|\"][^\>]*(background-image|background):[^\>;]*url\(([^\>]+)\)[^\>;]*[^\>\'\"]*[\'|\"][^\>]*)\>/i';
$content = preg_replace_callback($regexp, array($this, 'update_background_callback'), $content);
}
if ($this->options['iframes']) {
$content = preg_replace_callback('/\