class WordPress::Post
Constants
- DB_MAP
Left: DB, right: our symbol
- READONLY_ATTRIBUTES
- SAVE_FILTERS
- WORDPRESS_ATTRIBUTES
Public Class Methods
build(root, values)
click to toggle source
# File lib/wordpress/post.rb, line 262 def self.build root, values post = new root # Use the map values = Hash[values.map { |k, v| [DB_MAP[k] || k, v] }] from_db = false # Because the post ID is greater than zero, let's assume that this is a persisted record. from_db = true if values[:post_id] and values[:post_id] > 0 values.select { |key, value| WORDPRESS_ATTRIBUTES.keys.include? key }.each do |key, value| post.instance_variable_set(:"@#{key}", value) post.instance_variable_get(:@in_database)[key] = value if from_db end post end
new(root)
click to toggle source
Initializators
Calls superclass method
WordPress::Base::new
# File lib/wordpress/post.rb, line 252 def initialize root super WORDPRESS_ATTRIBUTES.each do |att, default| instance_variable_set :"@#{att}", default end @in_database = {} end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/wordpress/post.rb, line 285 def <=> other post_id <=> other.post_id end
==(other)
click to toggle source
Equality
# File lib/wordpress/post.rb, line 281 def == other other.post_id == post_id ? true : false end
attach_featured_image(image)
click to toggle source
Attachments
# File lib/wordpress/post.rb, line 131 def attach_featured_image image img_id = attach_image(image).post_id post_meta['_thumbnail_id'] = img_id end
attach_image(image)
click to toggle source
# File lib/wordpress/post.rb, line 136 def attach_image image raise 'Post must be saved before manipulating attached images' if @post_id == -1 # Make a new post with the "attachment" format if image.respond_to? :open handle = image.open else handle = image end title = (0...10).map{(65+rand(26)).chr}.join if image.respond_to? :path path = Pathname.new image.path title = path.each_filename.to_a[-1] end mimetype = nil ext = '' if image.respond_to? :meta mimetype = (image.meta['content-type'] || '').split(';')[0] type = MIME::Types[mimetype].first if type ext = '.' + type.extensions.first end else if type = MIME::Types.type_for(title).first mimetype = types.content_type # ext = types.extensions.first end end # Build the pathname where this will go. file_basename = File.join(@wp.configuration[:wordpress_wp_content], '/uploads') uri_basename = @wp.configuration[:wordpress_wp_content_url] || (@wp.options['siteurl'] + '/wp-content/uploads') today = Date.today relative_filepath = "#{'%02d' % today.year}/#{'%02d' % today.month}/#{title}" # Copy the file local_filepath = Pathname.new(File.join(file_basename, relative_filepath + ext)) FileUtils.mkdir_p local_filepath.dirname.to_s buffer = handle.read.force_encoding('BINARY') out = File.open(local_filepath.to_s, 'wb') out.write buffer out.close attachment = self.class.build @wp, { :post_title => title, :post_name => CGI::escape(title.downcase), :post_status => 'inherit', :post_parent => @post_id, :post_type => 'attachment', :post_mime_type => mimetype, :guid => uri_basename + '/' + relative_filepath + ext } attachment.save attachment.post_meta['_wp_attached_file'] = relative_filepath + ext # Get image metadata begin img = Magick::Image::read(local_filepath.to_s).first size_hash = {} thumbnail_filename = title + '-150x150' + ext thumb_img = img.resize_to_fill(150, 150) thumb_img.write File.join(file_basename, "#{'%02d' % today.year}/#{'%02d' % today.month}/#{thumbnail_filename}") size_hash[:thumbnail] = { :file => thumbnail_filename, :width => 150, :height => 150 } size_hash[:medium] = { :file => title + ext, :height => img.rows, :width => img.columns } size_hash[:large] = { :file => title + ext, :height => img.rows, :width => img.columns } attachment.post_meta['_wp_attachment_metadata'] = { :file => title + ext, :height => img.rows, :width => img.columns, :sizes => size_hash } rescue Exception => e # raise e puts "Warn: Ignoring exception #{e.to_s}" end attachment end
attached_files(*args)
click to toggle source
# File lib/wordpress/post.rb, line 243 def attached_files *args attach_args = { :post_type => 'attachment', :post_parent => @post_id, :post_status => 'inherit' }.merge(args[0] || {}) @wp.query attach_args end
featured_image()
click to toggle source
# File lib/wordpress/post.rb, line 238 def featured_image thumb_id = post_meta['_thumbnail_id'] @wp.query(:post_type => 'attachment', :post_parent => @post_id, :post_status => 'inherit', :p => thumb_id).first if thumb_id end
get_the_terms(taxonomy)
click to toggle source
Taxonomies
Calls superclass method
WordPress::Base#get_the_terms
# File lib/wordpress/post.rb, line 116 def get_the_terms taxonomy raise 'Post must be saved before manipulating taxonomies' if @post_id == -1 super @post_id, taxonomy end
inspect()
click to toggle source
# File lib/wordpress/post.rb, line 54 def inspect to_h.to_s end
persisted?()
click to toggle source
# File lib/wordpress/post.rb, line 58 def persisted? @post_id != -1 and !unsaved_changes? end
post_meta()
click to toggle source
save()
click to toggle source
# File lib/wordpress/post.rb, line 82 def save # We don't need to save because nothing has changed return self if persisted? save_hash = Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }].reject { |k, v| READONLY_ATTRIBUTES.include? k } save_hash = Hash[ save_hash.map { |k, v| [k, SAVE_FILTERS[k] ? SAVE_FILTERS[k].call(v) : v] }] new_id = update_or_insert @tbl[:posts], "`#{@tbl[:posts]}`.`ID`='#{ post_id.to_i }'", save_hash # We'll assume everything went OK since no errors were thrown. @in_database = Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }] if new_id and new_id > 0 @in_database[:post_id] = @post_id = new_id end self end
save!()
click to toggle source
# File lib/wordpress/post.rb, line 103 def save! save || raise(WordPress::Error.new('Save failed.')) end
set_post_terms(terms, taxonomy, append=false)
click to toggle source
Calls superclass method
WordPress::Base#set_post_terms
# File lib/wordpress/post.rb, line 121 def set_post_terms terms, taxonomy, append=false raise 'Post must be saved before manipulating taxonomies' if @post_id == -1 terms = [terms] unless terms.kind_of?(Array) current_terms = get_the_terms(taxonomy) return current_terms if current_terms.sort == terms.sort && append == false super @post_id, terms, taxonomy, append end
to_h()
click to toggle source
# File lib/wordpress/post.rb, line 50 def to_h Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }] end
unsaved_changes()
click to toggle source
# File lib/wordpress/post.rb, line 62 def unsaved_changes WORDPRESS_ATTRIBUTES.keys.select do |k| false true if instance_variable_get(:"@#{k}") != @in_database[k] end end
unsaved_changes?()
click to toggle source
# File lib/wordpress/post.rb, line 69 def unsaved_changes? # Not an empty array of changes means there are unsaved changes. unsaved_changes != [] end