Class: WordPress::Base
- Inherits:
-
Object
- Object
- WordPress::Base
- Defined in:
- lib/wordpress/base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #get_post_meta(id, meta_key) ⇒ Object
- #get_the_terms(id, taxonomy) ⇒ Object
-
#initialize(connection, wp_tables) ⇒ Base
constructor
A new instance of Base.
- #insert(table, content) ⇒ Object
- #set_post_meta(id, meta_key, meta_value) ⇒ Object
- #set_post_terms(post_id, terms, taxonomy, append = false) ⇒ Object
- #update(table, where, content) ⇒ Object
- #update_or_insert(table, where, content) ⇒ Object
Constructor Details
#initialize(connection, wp_tables) ⇒ Base
Returns a new instance of Base.
6 7 8 9 |
# File 'lib/wordpress/base.rb', line 6 def initialize(connection, wp_tables) @tbl = wp_tables @conn = connection end |
Instance Method Details
#get_post_meta(id, meta_key) ⇒ Object
52 53 54 |
# File 'lib/wordpress/base.rb', line 52 def (id, ) (@conn.query("SELECT `meta_value` FROM `#{@tbl[:postmeta]}` WHERE `post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape .to_s}'").first || {})[:meta_value] end |
#get_the_terms(id, taxonomy) ⇒ Object
64 65 66 67 |
# File 'lib/wordpress/base.rb', line 64 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
11 12 13 14 15 16 17 18 19 |
# File 'lib/wordpress/base.rb', line 11 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 |
#set_post_meta(id, meta_key, meta_value) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/wordpress/base.rb', line 56 def (id, , ) update_or_insert $tbl[:postmeta], "`post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape .to_s}'", { post_id: id, meta_key: .to_s, meta_value: .to_s } end |
#set_post_terms(post_id, terms, taxonomy, append = false) ⇒ Object
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 |
# File 'lib/wordpress/base.rb', line 69 def set_post_terms(post_id, terms, taxonomy, append=false) terms.map! { |e| UnicodeUtils.nfc(e) } terms_esc = terms.map { |e| "'#{@conn.escape e.to_s}'" } terms_slugs = terms.map { |e| "'#{@conn.escape(CGI::escape e.to_s)}'"} raise ArgumentError, 'Terms must be an array with more than zero elements' unless terms_esc.count > 0 # 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wordpress/base.rb', line 21 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
44 45 46 47 48 49 50 |
# File 'lib/wordpress/base.rb', line 44 def update_or_insert(table, where, content) return nil if content.keys.length == 0 unless update table, where, content insert table, content end end |