Class: WordPress

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

Defined Under Namespace

Modules: Version Classes: Base, Error, Options, Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WordPress

Returns a new instance of WordPress.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wordpress.rb', line 14

def initialize(options)
  # Symbolize keys
  options = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

  options[:wordpress_prefix] ||= 'wp_'
  @tbl = {
    terms: options[:wordpress_prefix] + 'terms',
    termtax: options[:wordpress_prefix] + 'term_taxonomy',
    termrel: options[:wordpress_prefix] + 'term_relationships',
    posts: options[:wordpress_prefix] + 'posts',
    postmeta: options[:wordpress_prefix] + 'postmeta',
    options: options[:wordpress_prefix] + 'options',
    prefix: options[:wordpress_prefix]
  }

  @conn = Mysql2::Client.new options
  @conn.query_options.merge!(symbolize_keys: true)

  @configuration = options

  # The WordPress options table
  @options = WordPress::Options.new self
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



43
44
45
# File 'lib/wordpress.rb', line 43

def configuration
  @configuration
end

#connObject (readonly)

Returns the value of attribute conn.



41
42
43
# File 'lib/wordpress.rb', line 41

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/wordpress.rb', line 38

def options
  @options
end

#tblObject (readonly)

Returns the value of attribute tbl.



40
41
42
# File 'lib/wordpress.rb', line 40

def tbl
  @tbl
end

Instance Method Details

#new_post(args) ⇒ Object



132
133
134
135
136
# File 'lib/wordpress.rb', line 132

def new_post args
  # 'args' is a hash of attributes that WordPress::Post responds to
  # See wordpress/post.rb
  WordPress::Post.build self, args
end

#query(args) ⇒ Object

Raises:

  • (ArgumentError)


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

def query args
  # Main query function.
  # The goal is to support as much as the original WP_Query API as possible.
  raise ArgumentError.new('Hash arguments are supported.') unless args.kind_of?(Hash)

  # Defaults
  args = {
    post_type: 'post',
    post_status: 'publish'
  }.merge(args)

  wheres_and = []
  inner_joins = []

  # Page finders

  if args[:page_id]
    args[:p] = args[:page_id]
    args[:post_type] = 'page'
  end

  if args[:pagename]
    args[:name] = args[:pagename]
    args[:post_type] = 'page'
  end

  # Post finders

  if args[:post_type]
    wheres_and << "`#{@tbl[:posts]}`.`post_type`='#{ @conn.escape args[:post_type] }'"
  end

  if args[:p]
    wheres_and << "`#{@tbl[:posts]}`.`ID`='#{ args[:p].to_i }'"
  end

  if args[:post_parent]
    wheres_and << "`#{@tbl[:posts]}`.`post_parent`='#{ args[:post_parent].to_i }'"
  end

  if args[:name]
    wheres_and << "`#{@tbl[:posts]}`.`post_name`='#{ @conn.escape args[:name] }'"
  end

  if args[:post_status]
    wheres_and << "`#{@tbl[:posts]}`.`post_status`='#{ @conn.escape args[:post_status] }'"
  end

  if args[:post__in]
    raise ArgumentError.new(':post__in should be an array.') unless args[:post__in].kind_of?(Array)
    wheres_and << "`#{@tbl[:posts]}`.`ID` IN (#{ args[:post__in].map { |e| "'#{ e.to_i }'" }.join ', ' })"
  end

  if args[:post__not_in]
    raise ArgumentError.new(':post__not_in should be an array.') unless args[:post__not_in].kind_of?(Array)
    wheres_and << "`#{@tbl[:posts]}`.`ID` NOT IN (#{ args[:post__not_in].map { |e| "'#{ e.to_i }'" }.join ', ' })"
  end

  # Meta finders

  if (mqs = args[:meta_query]) and mqs.kind_of?(Array)
    inner_joins << "`#{@tbl[:postmeta]}` ON `#{@tbl[:posts]}`.`ID`=`#{@tbl[:postmeta]}`.`post_id`"
    mqs.each do |mq|
      mq_params = {
        compare: '=',
        type: 'CHAR' # Ignored for now
      }.merge(mq)

      # Allowed compares
      mq_params[:compare] = '=' unless ['=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'].include?(mq_params[:compare])

      wheres_and << "(`#{@tbl[:postmeta]}`.`meta_key`='#{@conn.escape mq_params[:key].to_s}' AND `#{@tbl[:postmeta]}`.`meta_value`='#{@conn.escape mq_params[:value].to_s}')"
    end
  end

  query = "SELECT `#{@tbl[:posts]}`.* FROM `#{@tbl[:posts]}` "
  if inner_joins.length > 0
    query += "INNER JOIN #{inner_joins.join ', '} "
  end

  query += "WHERE #{ wheres_and.join ' AND ' }"

  @conn.query(query).map do |row|
    WordPress::Post.build self, row
  end
end

#update_taxonomy_counts(*taxes) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wordpress.rb', line 138

def update_taxonomy_counts *taxes
  taxes.each do |taxonomy|
    @conn.query("SELECT `term_taxonomy_id`, `count` FROM `#{@tbl[:termtax]}` WHERE `taxonomy`='#{@conn.escape taxonomy.to_s}'").each do |term_tax|
      termtax_id = term_tax[:term_taxonomy_id]
      count = 0
      @conn.query("SELECT COUNT(*) as `c` FROM `#{@tbl[:posts]}`, `#{@tbl[:termrel]}` WHERE `#{@tbl[:posts]}`.`ID`=`#{@tbl[:termrel]}`.`object_id` AND `#{@tbl[:termrel]}`.`term_taxonomy_id`='#{ termtax_id.to_i }'").each do |x|
        count = x[:c]
      end
      if count != term_tax[:count]
        @conn.query("UPDATE `#{@tbl[:termtax]}` SET `count`='#{count.to_i}' WHERE `term_taxonomy_id`='#{ termtax_id.to_i }'")
      end
    end
  end
end