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 }
Class Method Summary collapse
Instance Method Summary collapse
-
#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
239 240 241 242 243 244 245 246 247 |
# File 'lib/wordpress/post.rb', line 239 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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/wordpress/post.rb', line 249 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
#attach_featured_image(image) ⇒ Object
Attachments
118 119 120 121 |
# File 'lib/wordpress/post.rb', line 118 def attach_featured_image image img_id = attach_image(image).post_id ['_thumbnail_id'] = img_id end |
#attach_image(image) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 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 |
# File 'lib/wordpress/post.rb', line 123 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? : 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
230 231 232 233 234 235 |
# File 'lib/wordpress/post.rb', line 230 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
225 226 227 228 |
# File 'lib/wordpress/post.rb', line 225 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
103 104 105 106 |
# File 'lib/wordpress/post.rb', line 103 def get_the_terms taxonomy raise 'Post must be saved before manipulating taxonomies' if @post_id == -1 super @post_id, taxonomy end |
#inspect ⇒ Object
47 48 49 |
# File 'lib/wordpress/post.rb', line 47 def inspect to_h.to_s end |
#persisted? ⇒ Boolean
51 52 53 |
# File 'lib/wordpress/post.rb', line 51 def persisted? @post_id != -1 and !unsaved_changes? end |
#post_meta ⇒ Object
Post Meta
96 97 98 99 |
# File 'lib/wordpress/post.rb', line 96 def raise 'Post must be saved before manipulating metadata' if @post_id == -1 ||= WordPress::Post::Meta.new @wp, self end |
#save ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/wordpress/post.rb', line 75 def save # We don't need to save because nothing has changed return self if persisted? new_id = update_or_insert @tbl[:posts], "`#{@tbl[:posts]}`.`ID`='#{ post_id.to_i }'", Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }].reject { |k, v| READONLY_ATTRIBUTES.include? k } # 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
90 91 92 |
# File 'lib/wordpress/post.rb', line 90 def save! save || raise(WordPress::Error.new('Save failed.')) end |
#set_post_terms(terms, taxonomy, append = false) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/wordpress/post.rb', line 108 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=false end |
#to_h ⇒ Object
43 44 45 |
# File 'lib/wordpress/post.rb', line 43 def to_h Hash[ WORDPRESS_ATTRIBUTES.keys.map { |e| [e, instance_variable_get(:"@#{e}")] }] end |
#unsaved_changes ⇒ Object
55 56 57 58 59 60 |
# File 'lib/wordpress/post.rb', line 55 def unsaved_changes WORDPRESS_ATTRIBUTES.keys.select do |k| false true if instance_variable_get(:"@#{k}") != @in_database[k] end end |
#unsaved_changes? ⇒ Boolean
62 63 64 65 |
# File 'lib/wordpress/post.rb', line 62 def unsaved_changes? # Not an empty array of changes means there are unsaved changes. unsaved_changes != [] end |