-
Notifications
You must be signed in to change notification settings - Fork 6
/
blogger-importer-blogitem.php
168 lines (142 loc) · 5.72 KB
/
blogger-importer-blogitem.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Based on WP_SimplePieAtomPub_Item
* Expect this to become part of core wordpress at some point.
* See http://core.trac.wordpress.org/ticket/7652
*
* http://codex.wordpress.org/Geodata
*
*/
define('SIMPLEPIE_NAMESPACE_ATOMPUB', 'http://purl.org/atom/app#');
define('SIMPLEPIE_NAMESPACE_GEOTAG', 'http://www.georss.org/georss');
define('SIMPLEPIE_NAMESPACE_THREAD','http://purl.org/syndication/thread/1.0');
/**
* SimplePie Helper for AtomPub
*
* @package WordPress
* @subpackage Publishing
* @since 3.1
*/
if (!class_exists('WP_SimplePie_Blog_Item'))
{
class WP_SimplePie_Blog_Item extends SimplePie_Item
{
/**
* Constructor
*/
function __construct($feed, $data)
{
parent::__construct($feed, $data);
}
/**
* Get the status of the entry
*
* @return bool True if the item is a draft, false otherwise
*/
function get_draft_status()
{
$draft = false;
if (($control = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOMPUB, 'control')) && !empty($control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']))
{
$draft = ('yes' == $control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']);
}
return $draft;
}
//Tried using date functions from http://core.trac.wordpress.org/attachment/ticket/7652/7652-separate.diff
//but ended up with 1970s dates so returned to Otto's version which is much simplified
function get_updated()
{
$temparray = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated');
if (isset($temparray[0]['data']))
return $this->convert_date($temparray[0]['data']);
else
return null;
}
function get_published()
{
$temparray = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'published');
if (isset($temparray[0]['data']))
return $this->convert_date($temparray[0]['data']);
else
return null;
}
function get_geotags()
{//Return an array of geo tags see http://codex.wordpress.org/Geodata
//example source
// <georss:featurename>Rådhuspladsen 3, 1550 Copenhagen, Denmark</georss:featurename>
// <georss:point>55.6760968 12.5683371</georss:point>
$latlong = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEOTAG, 'point');
if (isset($latlong[0]['data'])) {
preg_match('/([0-9.-]+).+?([0-9.-]+)/', $latlong[0]['data'], $matches);
$lat=(float)$matches[1];
$long=(float)$matches[2];
}
if (!isset($lat) |!isset($long)) {
return null; //Without lat long we can't have a valid location
}
$address = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEOTAG, 'featurename');
if (isset($address[0]['data']))
$geo_address = $address[0]['data'];
else
$geo_address = null;
$geo = array('geo_latitude' => $lat, 'geo_longitude' => $long, 'geo_address' => $geo_address );
return $geo;
}
function convert_date($date)
{
preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?(Z|[\+|\-][0-9]{2,4}){0,1}#', $date, $date_bits);
$offset = iso8601_timezone_to_offset($date_bits[7]);
$timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
$timestamp -= $offset; // Convert from Blogger local time to GMT
$timestamp += get_option('gmt_offset') * 3600; // Convert from GMT to WP local time
return gmdate('Y-m-d H:i:s', $timestamp);
}
//Don't Sanitize the ID, the default get_id was cleaning our IDs and that meant that nested comments did not work
function get_id($hash = false, $fn = 'md5')
{
if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'id'))
{
return $return[0]['data'];
}
}
//Prefiltered links
function get_links($rel = 'alternate') {
$mylinks = array();
foreach ($rel as $type)
{
$links =parent::get_links($type);
if (!is_null($links)) {
foreach ($links as $link) {
$mylinks[] = array('rel' => $type, 'href' => $link);
}
}
}
return $mylinks;
}
//Preprocessed categories
function get_categories() {
$cats = parent::get_categories();
$mycats = array();
if (!is_null($cats)) {
foreach ($cats as $cat) {
$mycats[] = $cat->term;
}
}
return $mycats;
}
//What is the source of this item e.g. a comment linked to a post
//10/3/2014 Added error handling for where the comment links to a post that no longer exists on blogger.
function get_source() {
$temp = $this->get_item_tags(SIMPLEPIE_NAMESPACE_THREAD, 'in-reply-to');
if (!is_null($temp)){
foreach ($temp as $t) {
if (isset($t['attribs']['']['source'])) {
$source = $t['attribs']['']['source'];
}
}
return $source;
}
}
}
}
?>