-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFpThumbnailEvent.php
76 lines (65 loc) · 2.56 KB
/
FpThumbnailEvent.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
<?php
class FpThumbnailEvent {
private function __construct() {}
public static function get_the_post_thumbnail_src($img, $size) {
$img = (preg_match('~\bsrc="(https?:\/\/farm[0-9]+\.staticflickr\.com\/[^"]+\.(?:jpg|JPG|jpeg|jpeg|gif|GIF|png|PNG))"~', $img, $matches)) ? $matches[1] : '';
if (!$img) {
return '';
}
$suffix = '' === $size ? '' : "_$size";
if ( preg_match('/_\w{1,2}\.\w+$/', $img) ) {
$img = preg_replace('/^(.+)_(\w+)(\.\w+)$/', '${1}' . $suffix. '${3}', $img);
} else {
$img = preg_replace('/^(.+)(\.\w+)$/', '${1}' . $suffix . '${2}', $img);
}
return $img;
}
public static function filterPostThumbnailHtml($html, $post_id, $post_thumbnail_id, $size, $attr) {
global $post;
if ( !FlickrPress::isExtractThumbnailByPostID($post_id) || $html ) {
return $html;
}
$thumbnailSize = FlickrPress::getThumbnailSizeSuffix($size);
if ( null === $thumbnailSize || !FlickrPress::isThumbnailSize($size) ) {
$thumbnailSize = FlickrPress::getThumbnailSizeSuffix();
}
$src = self::get_the_post_thumbnail_src($post->post_content, $thumbnailSize);
if ( $src ) {
return '<img src="' . $src . '" class="attachment-post-thumbnail wp-post-image"/>';
} else {
return '';
}
}
public static function filterGetPostMetadata($metadata, $object_id, $meta_key, $single) {
$meta_cache = wp_cache_get($object_id, 'post_meta');
if ( FlickrPress::isExtractThumbnailByMetadata($meta_cache) && '_thumbnail_id' === $meta_key ) {
return PHP_INT_MAX;
} else {
return null;
}
}
public static function actionAddMetaBoxesPost($post) {
add_meta_box(
'wpfp_post_thumbnail',
__('Flickr Thumbnail', FlickrPress::TEXT_DOMAIN),
array(__CLASS__, 'getMetaBoxHtml'),
'post',
'advanced',
'default',
array($post)
);
}
public static function getMetaBoxHtml($post) {
$use = FlickrPress::isExtractThumbnailByPostID($post->ID);
?>
<p>Use Post Thumbnail: <label class="selectit">Yes <input value="1" type="radio" name="wpfp_use_post_thumbnail" <?php echo $use ? 'checked="checked"' : ''; ?>></label>
<label class="selectit">No <input value="0" type="radio" name="wpfp_use_post_thumbnail" <?php echo !$use ? 'checked="checked"' : ''; ?>></label></p>
<?php
}
public static function filterWpInsertPostData($data, $postarr) {
if ( array_key_exists( 'wpfp_use_post_thumbnail', $postarr ) ) {
update_post_meta($postarr['ID'], 'wpfp_use_post_thumbnail', $postarr['wpfp_use_post_thumbnail']);
}
return $data;
}
}