-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
83 lines (69 loc) · 2.11 KB
/
index.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
<?php
/**
* Plugin Name: GM Lazy Video native
* Description: Add lazy loading on native video from gutenberg
* Version: 0.0.1
* Author: Faramaz Pat <[email protected]>
* License: MIT License
* Domain Path: /languages
* Text Domain: gm-lazy-video
**/
namespace Goodmotion\Lazy\Video;
require_once(dirname(__FILE__) . '/helpers/simplehtmldom/simple_html_dom.php');
defined('ABSPATH') || exit;
/**
* test override embedded block
*/
function core_video_render($attributes, $content)
{
$html = new \simple_html_dom();
$html->load($content);
// find video tag
$container = $html->find('video');
if (array_key_exists(0, $container)) {
$src = $container[0]->getAttribute('src');
$container[0]->setAttribute('data-src', $src);
$container[0]->removeAttribute('src');
// add class
$class = $container[0]->getAttribute('class');
$container[0]->setAttribute('class', $class . ' b-lazy');
// search source tag if exist
$source = $html->find('source');
// pass src attribute to data-src
foreach ($source as $key => $value) {
$src = $value->getAttribute('src');
$value->setAttribute('data-src', $src);
$value->removeAttribute('src');
}
}
return $html->save();
}
/**
* override block core/video
*/
function replace_core_video_render()
{
register_block_type('core/video', array(
'render_callback' => __NAMESPACE__ . '\core_video_render',
));
}
/**
* add script to front
*/
function frontend_scripts()
{
if (has_block('core/video')) {
wp_enqueue_script(
'gm-lazy-video-blazy',
plugins_url('js/blazy.min.js', __FILE__),
filemtime(plugin_dir_path(__FILE__) . 'js/blazy.min.js')
);
wp_enqueue_script(
'gm-lazy-video-front',
plugins_url('js/index.js', __FILE__),
filemtime(plugin_dir_path(__FILE__) . 'js/index.js')
);
}
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\frontend_scripts');
add_action('init', __NAMESPACE__ . '\replace_core_video_render');