Class: WordPress::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wordpress/base.rb

Direct Known Subclasses

Options, Post, Post::Meta, Schema

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Base

Returns a new instance of Base.


6
7
8
9
10
11
# File 'lib/wordpress/base.rb', line 6

def initialize root
  @wp = root

  @conn = @wp.conn
  @tbl = @wp.tbl
end

Instance Method Details

#get_post_meta(id, meta_key) ⇒ Object


58
59
60
# File 'lib/wordpress/base.rb', line 58

def (id, meta_key)
  (@conn.query("SELECT `meta_value` FROM `#{@tbl[:postmeta]}` WHERE `post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape meta_key.to_s}'").first || {})[:meta_value]
end

#get_the_terms(id, taxonomy) ⇒ Object


70
71
72
73
# File 'lib/wordpress/base.rb', line 70

def get_the_terms(id, taxonomy)
  terms = @conn.query("SELECT `#{@tbl[:termtax]}`.`taxonomy`, `#{@tbl[:terms]}`.`name` FROM `#{@tbl[:posts]}`, `#{@tbl[:termtax]}`, `#{@tbl[:termrel]}`, `#{@tbl[:terms]}` WHERE `#{@tbl[:posts]}`.`ID` = '#{id.to_i}' AND `#{@tbl[:termrel]}`.`object_id` = `#{@tbl[:posts]}`.`ID` AND `#{@tbl[:termrel]}`.`term_taxonomy_id` = `#{@tbl[:termtax]}`.`term_taxonomy_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}' AND `#{@tbl[:terms]}`.`term_id` = `#{@tbl[:termtax]}`.`term_id`")
  terms.map { |e| e[:name] }
end

#insert(table, content) ⇒ Object


17
18
19
20
21
22
23
24
25
# File 'lib/wordpress/base.rb', line 17

def insert(table, content)
  return nil if content.keys.length == 0

  fields = content.keys.map { |e| "`#{@conn.escape e.to_s}`" }
  values = content.keys.map { |e| "'#{@conn.escape content[e].to_s}'" }

  @conn.query("INSERT INTO `#{@conn.escape table}` (#{fields.join ', '}) VALUES (#{values.join ', '})")
  @conn.last_id
end

#inspectObject


13
14
15
# File 'lib/wordpress/base.rb', line 13

def inspect
  nil
end

#set_post_meta(id, meta_key, meta_value) ⇒ Object


62
63
64
65
66
67
68
# File 'lib/wordpress/base.rb', line 62

def (id, meta_key, meta_value)
  update_or_insert $tbl[:postmeta], "`post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape meta_key.to_s}'", {
    :post_id => id,
    :meta_key => meta_key.to_s,
    :meta_value => meta_value.to_s
  }
end

#set_post_terms(post_id, terms, taxonomy, append = false) ⇒ Object


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
# File 'lib/wordpress/base.rb', line 75

def set_post_terms(post_id, terms, taxonomy, append=false)
  terms_esc = terms.map { |e| "'#{@conn.escape e.to_s}'" }
  terms_slugs = terms.map { |e| "'#{@conn.escape(CGI::escape e.to_s)}'"}

  if terms_esc.count == 0 and !append
    # Overwrite with nothing.
    @conn.query("DELETE `#{@tbl[:termrel]}` FROM `#{@tbl[:termrel]}` JOIN `#{@tbl[:termtax]}` ON `#{@tbl[:termrel]}`.`term_taxonomy_id`=`#{@tbl[:termtax]}`.`term_taxonomy_id` WHERE `#{@tbl[:termtax]}`.`taxonomy`='#{@conn.escape taxonomy}' AND `#{@tbl[:termrel]}`.`object_id` = '#{post_id.to_i}'")

    return
  elsif terms_esc.count == 0 and append
    # Because we want to append nothing, there's no harm done with just returning here.
    return
  end

  # Cache post terms and term IDs
  termtax_rel = Hash[@conn.query("SELECT `#{@tbl[:termtax]}`.`term_taxonomy_id`, `#{@tbl[:terms]}`.`name` FROM `#{@tbl[:terms]}`, `#{@tbl[:termtax]}` WHERE (`#{@tbl[:terms]}`.`name` IN (#{ terms_esc.join ', ' }) OR `#{@tbl[:terms]}`.`slug` IN (#{ terms_slugs.join ', ' })) AND `#{@tbl[:terms]}`.`term_id` = `#{@tbl[:termtax]}`.`term_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}' GROUP BY `#{@tbl[:terms]}`.`name`").map { |e| [e[:name], e[:term_taxonomy_id]] }]

  (terms - termtax_rel.keys).each do |x|
    # These are terms that do not exist yet
    term_id = (@conn.query("SELECT `term_id` FROM `#{@tbl[:terms]}` WHERE `name`='#{@conn.escape x}' AND `slug`='#{@conn.escape(CGI::escape x)}' LIMIT 1").first || {})[:term_id]
    unless term_id
      @conn.query("INSERT INTO `#{@tbl[:terms]}` (`name`, `slug`, `term_group`) VALUES ('#{@conn.escape x}', '#{@conn.escape(CGI::escape x)}', '0')")
      term_id = @conn.last_id
    end
    termtax_id = (@conn.query("SELECT `term_taxonomy_id` FROM `#{@tbl[:termtax]}` WHERE `term_id`='#{term_id.to_i}' AND `taxonomy`='#{@conn.escape taxonomy}' LIMIT 1").first || {})[:term_taxonomy_id]
    unless termtax_id
      @conn.query("INSERT INTO `#{@tbl[:termtax]}` (`term_id`, `taxonomy`, `parent`, `count`) VALUES ('#{term_id.to_i}', '#{@conn.escape taxonomy}', '0', '0')")
      termtax_id = @conn.last_id
    end
    termtax_rel[x] = termtax_id
  end

  termtax_to_add = terms

  if !append
    # Delete all associations first
    @conn.query("DELETE `#{@tbl[:termrel]}` FROM `#{@tbl[:termrel]}` JOIN `#{@tbl[:termtax]}` ON `#{@tbl[:termrel]}`.`term_taxonomy_id`=`#{@tbl[:termtax]}`.`term_taxonomy_id` WHERE `#{@tbl[:termtax]}`.`taxonomy`='#{@conn.escape taxonomy}' AND `#{@tbl[:termrel]}`.`object_id` = '#{post_id.to_i}'")
  else
    currently_associated = @conn.query("SELECT `#{@tbl[:termrel]}`.`term_taxonomy_id` FROM `#{@tbl[:termrel]}`, `#{@tbl[:termtax]}` WHERE `#{@tbl[:termrel]}`.`object_id` = '#{post_id.to_i}' AND `#{@tbl[:termrel]}`.`term_taxonomy_id` = `#{@tbl[:termtax]}`.`term_taxonomy_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}'").map { |e| e[:term_taxonomy_id] }
    termtax_to_add -= currently_associated
  end

  termtax_to_add.each do |term|
    @conn.query("INSERT INTO `#{@tbl[:termrel]}` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES ('#{post_id.to_i}', '#{termtax_rel[term].to_i}', '0')")
  end
end

#update(table, where, content) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wordpress/base.rb', line 27

def update(table, where, content)
  return nil if content.keys.length == 0

  fields = content.keys.map { |e| "`#{@conn.escape e.to_s}`" }
  result = @conn.query("SELECT #{fields.join ', '} FROM `#{@conn.escape table}` WHERE #{where}")
  return false if result.count == 0

  if content.respond_to?(:diff)
    row = result.to_a[0]
    diff = content.diff row

    # Already up-to-date
    return true if diff.count == 0
  else
    diff = content
  end

  # Let's update the difference
  statements = diff.keys.map { |e| "`#{@conn.escape e.to_s}`='#{@conn.escape content[e].to_s}'" }
  @conn.query("UPDATE `#{@conn.escape table}` SET #{statements.join ', ' } WHERE #{where}")
  true
end

#update_or_insert(table, where, content) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/wordpress/base.rb', line 50

def update_or_insert(table, where, content)
  return nil if content.keys.length == 0

  unless update table, where, content
    insert table, content
  end
end