Class: Wordpress::WXR::Item

Inherits:
Element
  • Object
show all
Defined in:
lib/wordpress/wxr/item.rb

Direct Known Subclasses

Attachment, DirectoryListing, Page, Post

Instance Method Summary collapse

Methods inherited from Element

#initialize

Constructor Details

This class inherits a constructor from Wordpress::WXR::Element

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



121
122
123
124
# File 'lib/wordpress/wxr/item.rb', line 121

def ==(other)
  self.class == other.class &&
    id == other.id
end

#attachmentsObject



84
85
86
# File 'lib/wordpress/wxr/item.rb', line 84

def attachments
  wxr.attachments.select { |attachment| attachment.parent_id == id }
end

#categoriesObject



78
79
80
81
82
# File 'lib/wordpress/wxr/item.rb', line 78

def categories
  node.xpath("category[@domain='category']").map do |cat|
    wxr.categories.find_by(nicename: cat['nicename'])
  end
end

#comment_statusObject



48
49
50
51
# File 'lib/wordpress/wxr/item.rb', line 48

def comment_status
  # TODO: `opened` or `closed`
  node.xpath('wp:comment_status').text
end

#contentObject



28
29
30
# File 'lib/wordpress/wxr/item.rb', line 28

def content
  node.xpath('content:encoded').text
end

#creatorObject



20
21
22
# File 'lib/wordpress/wxr/item.rb', line 20

def creator
  wxr.authors.find_by(login: node.xpath('dc:creator').text)
end

#draft?Boolean

TODO: status: publish, draft, pending, private, trash, inherit

Returns:

  • (Boolean)


109
110
111
# File 'lib/wordpress/wxr/item.rb', line 109

def draft?
  status != 'publish'
end

#excerptObject



32
33
34
# File 'lib/wordpress/wxr/item.rb', line 32

def excerpt
  node.xpath('excerpt:encoded').text
end

#guidObject



24
25
26
# File 'lib/wordpress/wxr/item.rb', line 24

def guid
  node.xpath('guid').text
end

#idObject



36
37
38
# File 'lib/wordpress/wxr/item.rb', line 36

def id
  Integer(node.xpath('wp:post_id').text)
end


16
17
18
# File 'lib/wordpress/wxr/item.rb', line 16

def link
  URI(node.xpath('link').text)
end

#metaObject



96
97
98
99
100
# File 'lib/wordpress/wxr/item.rb', line 96

def meta
  node.xpath('wp:postmeta').each_with_object({}) { |meta, hash|
    hash[meta.xpath('wp:meta_key').text] = meta.xpath('wp:meta_value').text
  }
end

#nameObject



57
58
59
# File 'lib/wordpress/wxr/item.rb', line 57

def name
  node.xpath('wp:post_name').text
end

#pagesObject



88
89
90
# File 'lib/wordpress/wxr/item.rb', line 88

def pages
  wxr.pages.select { |page| page.parent_id == id }
end

#parent_idObject



92
93
94
# File 'lib/wordpress/wxr/item.rb', line 92

def parent_id
  Integer(node.xpath('wp:post_parent').text)
end

#ping_statusObject



53
54
55
# File 'lib/wordpress/wxr/item.rb', line 53

def ping_status
  node.xpath('wp:ping_status').text
end

#posted_atObject



40
41
42
43
44
45
46
# File 'lib/wordpress/wxr/item.rb', line 40

def posted_at
  # Attempt to use the GMT post date since it includes a timezone.
  # Failing that, use the post_date which will use the current timezone.
  DateTime.parse(node.xpath('wp:post_date_gmt').text)
rescue ArgumentError
  DateTime.parse node.xpath('wp:post_date').text
end

#published?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/wordpress/wxr/item.rb', line 113

def published?
  !draft?
end

#statusObject



61
62
63
# File 'lib/wordpress/wxr/item.rb', line 61

def status
  node.xpath('wp:status').text
end

#sticky?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/wordpress/wxr/item.rb', line 117

def sticky?
  !node.xpath('wp:is_sticky').text.to_i.zero?
end

#tagsObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wordpress/wxr/item.rb', line 65

def tags
  # xml dump has "post_tag" for wordpress 3.1 and "tag" for 3.0
  path = if node.xpath("category[@domain='post_tag']").count > 0
           "category[@domain='post_tag']"
         else
           "category[@domain='tag']"
         end

  node.xpath(path).map do |tag_node|
    wxr.tags.find_by(slug: tag_node['nicename'])
  end
end

#titleObject



12
13
14
# File 'lib/wordpress/wxr/item.rb', line 12

def title
  node.xpath('title').text
end