Class: WordPress::Options

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

Instance Method Summary collapse

Methods inherited from Base

#get_post_meta, #get_the_terms, #initialize, #insert, #inspect, #set_post_meta, #set_post_terms, #update, #update_or_insert

Constructor Details

This class inherits a constructor from WordPress::Base

Instance Method Details

#[](key) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wordpress/options.rb', line 6

def [](key)
  v = nil
  @conn.query("SELECT `option_value` FROM `#{@tbl[:options]}` WHERE `option_name`='#{@conn.escape key}' LIMIT 1").each do |row|
    v = row[:option_value]
  end
  # Apply out-filters
  if v
    if v[0, 1] == 'a' and v[-1, 1] == '}'
      # PHP-serialized array
      v = PHP.unserialize v
    end
  end
  v
end

#[]=(key, value) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wordpress/options.rb', line 21

def []=(key, value)
  # Apply in-filters

  if value.kind_of?(Hash) or value.kind_of?(Array)
    value = PHP.serialize value
  end

  old_value = self[key]

  if !value.nil? and !old_value.nil? and value != old_value
    # Update operation.
    @conn.query("UPDATE `#{@tbl[:options]}` SET `option_value`='#{@conn.escape value}' WHERE `option_name`='#{@conn.escape key}'")
  elsif value.nil? and !old_value.nil?
    # New value nil, old value not. Delete operation.
    @conn.query("DELETE FROM `#{@tbl[:options]}` WHERE `option_name`='#{@conn.escape key}'")
  elsif !value.nil? and old_value.nil?
    # New value non-nil, old value nil. Insert operation.
    @conn.query("INSERT INTO `#{@tbl[:options]}` (`option_name`, `option_value`, `autoload`) VALUES ('#{@conn.escape key}', '#{@conn.escape value.to_s}', 'no')")
  end
  value
end