Class: WordPress::Post
Defined Under Namespace
Classes: Meta
Constant Summary collapse
- DB_MAP =
Left: DB, right: our symbol
{ :ID => :post_id }
- READONLY_ATTRIBUTES =
[ :post_id ]
- WORDPRESS_ATTRIBUTES =
{ :post_id => -1, :post_author => 1, :post_date => Time.new, :post_date_gmt => Time.new.utc, :post_content => '', :post_title => '', :post_excerpt => '', :post_status => 'draft', :comment_status => 'open', :ping_status => 'open', :post_password => '', :post_name => '', :to_ping => '', :pinged => '', :post_modified => Time.new, :post_modified_gmt => Time.new.utc, :post_content_filtered => '', :post_parent => 0, :guid => '0', :menu_order => 0, :post_type => 'post', :post_mime_type => '', :comment_count => 0 }
- SAVE_FILTERS =
{ post_date: ->( v ){ v.strftime "%Y-%m-%d %H:%M:%S" }, post_date_gmt: ->( v ){ v.strftime "%Y-%m-%d %H:%M:%S" }, post_modified: ->( v ){ v.strftime "%Y-%m-%d %H:%M:%S" }, post_modified_gmt: ->( v ){ v.strftime "%Y-%m-%d %H:%M:%S" } }
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Equality.
-
#attach_featured_image(image) ⇒ Object
Attachments.
- #attach_image(image) ⇒ Object
- #attached_files(*args) ⇒ Object
- #featured_image ⇒ Object
-
#get_the_terms(taxonomy) ⇒ Object
Taxonomies.
-
#initialize(root) ⇒ Post
constructor
Initializators.
- #inspect ⇒ Object
- #persisted? ⇒ Boolean
-
#post_meta ⇒ Object
Post Meta.
- #save ⇒ Object
- #save! ⇒ Object
- #set_post_terms(terms, taxonomy, append = false) ⇒ Object
- #to_h ⇒ Object
- #unsaved_changes ⇒ Object
- #unsaved_changes? ⇒ Boolean
Methods inherited from Base
#get_post_meta, #insert, #set_post_meta, #update, #update_or_insert
Constructor Details
#initialize(root) ⇒ Post
Initializators
252 253 254 255 256 257 258 259 260 |
# 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 |
Class Method Details
.build(root, values) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# 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 |
Instance Method Details
#<=>(other) ⇒ Object
285 286 287 |
# File 'lib/wordpress/post.rb', line 285 def <=> other post_id <=> other.post_id end |
#==(other) ⇒ Object
Equality
281 282 283 |
# File 'lib/wordpress/post.rb', line 281 def == other other.post_id == post_id ? true : false end |
#attach_featured_image(image) ⇒ Object
Attachments
131 132 133 134 |
# File 'lib/wordpress/post.rb', line 131 def attach_featured_image image img_id = attach_image(image).post_id ['_thumbnail_id'] = img_id end |
#attach_image(image) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# 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.['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.['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 = 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 } .save .['_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 } .['_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 end |
#attached_files(*args) ⇒ Object
243 244 245 246 247 248 |
# 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 ⇒ Object
238 239 240 241 |
# File 'lib/wordpress/post.rb', line 238 def featured_image thumb_id = ['_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) ⇒ Object
Taxonomies
116 117 118 119 |
# 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 ⇒ Object
54 55 56 |
# File 'lib/wordpress/post.rb', line 54 def inspect to_h.to_s end |
#persisted? ⇒ Boolean
58 59 60 |
# File 'lib/wordpress/post.rb', line 58 def persisted? @post_id != -1 and !unsaved_changes? end |
#post_meta ⇒ Object
Post Meta
109 110 111 112 |
# File 'lib/wordpress/post.rb', line 109 def raise 'Post must be saved before manipulating metadata' if @post_id == -1 @post_meta ||= WordPress::Post::Meta.new @wp, self end |
#save ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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! ⇒ Object
103 104 105 |
# File 'lib/wordpress/post.rb', line 103 def save! save || raise(WordPress::Error.new('Save failed.')) end |
#set_post_terms(terms, taxonomy, append = false) ⇒ Object
121 122 123 124 125 126 127 |
# 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 ⇒ Object
50 51 52 |
# File 'lib/wordpress/post.rb', line 50 def to_h Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }] end |
#unsaved_changes ⇒ Object
62 63 64 65 66 67 |
# 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? ⇒ Boolean
69 70 71 72 |
# File 'lib/wordpress/post.rb', line 69 def unsaved_changes? # Not an empty array of changes means there are unsaved changes. unsaved_changes != [] end |