forked from denchev/Wordpress-to-placehold.it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer.php
129 lines (92 loc) · 3.17 KB
/
replacer.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
<?php
/**
* Replace any image occurances in Wordpress exported xml file with a link to placehold.it url for the same size.
*/
// A little download helper
if( isset( $_GET['download'] ) ) {
$file = $_GET['download'];
if(!file_exists($file)) {
die('You shall not pass!');
}
header('Content-Type: text/xml');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
die;
}
if( isset( $_POST['submit'] ) ) {
// Big files might take a while
set_time_limit(0);
$file = $_FILES['xml'];
$content = file_get_contents( $file['tmp_name'] );
// Find blog url
$matches = array();
preg_match('/<wp:base_blog_url>(.*?)<\/wp:base_blog_url>/', $content, $matches );
$link = $matches[1];
$file = $file['name'];
$file_parts = explode('.', $file);
$file_ext = array_pop($file_parts);
$file_name = implode('.', $file_parts);
// Extract all image files
$matches = array();
preg_match_all('/http(s)*:\/\/(.*?)\.(jpg|png|gif)/', $content, $matches);
$urls = $matches[0];
$urls = array_unique($urls);
// Prepare a folder where to download Placehold.it images later to be imported.
$dir = 'wp-content/uploads/placehold.it';
if( ! file_exists($dir) ) {
if(!is_writable($dir) || false === mkdir($dir)) {
die('Unable to create folder: <strong>' . $dir . '</strong>. Check file permissions.');
}
}
$counter = 0;
echo '<table width="100%" cellspacing="2" cellpadding="2">';
$urls_count = count($urls);
foreach ($urls as $url) {
$counter++;
$image_info = @getimagesize($url);
// It is not a valid image
if($image_info === false) {
echo '<tr><td>' . $counter . ' / ' . $urls_count . '</td><td colspan="2"><span style="color: red">Image <strong>' . $url . '</strong> skipped. Probably not found.</span></td></tr>';
continue;
}
$width = $image_info[0];
$height = $image_info[1];
$placeholdit_new_file = $dir . '/placehold.it-' . $width . 'x' . $height . '.gif';
if( ! file_exists( $placeholdit_new_file ) ) {
$placeholdit = 'http://placehold.it/' . $width . 'x' . $height;
$image_content = file_get_contents($placeholdit);
file_put_contents($placeholdit_new_file, $image_content);
}
// Show some stats
echo '<tr><td>' . $counter . ' / ' . $urls_count . '</td><td>' . $url . '</td><td>' . ($link . '/' . $placeholdit_new_file) . '</td></tr>';
$path = str_replace($link, '', $url);
$search = array($url, $path);
$replace = array($link . '/' . $placeholdit_new_file, '/' . $placeholdit_new_file);
$content = str_replace($search, $replace, $content);
flush();
ob_flush();
}
echo '</table>';
$handle = fopen($file_name . '-replaced.xml', 'w+');
$write = fwrite($handle, $content, strlen($content));
fclose($handle);
if( $write ) {
echo '<a href="replacer.php?download=' . $file_name . '-replaced.xml' . '">Download new file!</a>';
} else {
echo 'Unabled to save new file. Please check permissions.';
}
}
?>
<!doctype html>
<html>
<head>
<title>Simple placehold.it replacer for Wordpress</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="xml">
<input type="submit" name="submit">
</form>
</body>
</html>