/
shortlist-manifest-generator.php
90 lines (78 loc) · 2.57 KB
/
shortlist-manifest-generator.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
<?php
class Shortlist_Manifest_Builder {
public function __construct($post_object) {
$this->post = new TimberPost($post_object);
$this->base_url = get_bloginfo('url');
$this->permalink = get_permalink($post_object);
$cachebust = time();
$this->raw_html = file_get_contents($this->permalink);
}
public function build_manifest() {
ob_start();
$this->cache_header();
$this->featured_image();
$this->cache_list();
$this->inline_images();
$this->theme_manifest();
return ob_get_clean();
}
public function featured_image() {
$featured_image_id = get_post_thumbnail_id($this->post->ID);
if (isset($featured_image_id) && !empty($featured_image_id)):
$this->line("");
$this->comment("Featured image: ");
$url = wp_get_attachment_url($featured_image_id);
$this->line($this->absolute_to_relative_url($url));
endif;
}
public function cache_header() {
$this->line("CACHE MANIFEST");
$this->line("");
$this->comment("Post: ".$this->post->title);
$this->comment("Generated at: ".date('Y-m-d\TH:i:sP', time()));
$this->comment("Generated by ShortList Manifest Generator");
}
public function theme_manifest() {
$this->line("");
$this->comment("Theme Manifest");
$theme_folder = get_theme_root()."/".get_stylesheet();
$asset_folder = "/public";
$uri_path = $theme_folder.$asset_folder;
$files_array = glob_recursive($uri_path."/*.*", GLOB_ERR);
$files_array = array_map(array($this, 'root_to_relative_url'), $files_array);
foreach($files_array as $file):
$this->line($file);
endforeach;
}
public function root_to_relative_url($root_path) {
return str_replace(dirname(ABSPATH), "", $root_path);
}
public function cache_list() {
$this->line("");
$this->line("CACHE:");
$this->line("");
$this->comment("Post Attachments (".count($this->image_urls()). " found)");
}
public function inline_images() {
$this->comment("Inline Images:");
foreach($this->image_urls() as $url):
$this->line($url);
endforeach;
}
public function image_urls() {
$pattern = '~(http.*\.)(jpe?g|png|[tg]iff?|svg)~i';
$m = preg_match_all($pattern,$this->raw_html,$matches);
$image_array = $matches[0];
$image_array = array_map(array($this, 'absolute_to_relative_url'), $image_array);
return $image_array;
}
public function absolute_to_relative_url($url) {
return str_replace($this->base_url, "", $url);
}
public function line($line) {
print("$line\n");
}
public function comment($line) {
print("# $line\n");
}
}