Skip to content

Commit

Permalink
Add POST_THUMBNAIL(featured image) option
Browse files Browse the repository at this point in the history
  • Loading branch information
misohena committed Mar 20, 2019
1 parent 38eef7a commit f3e9385
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions org2blog.el
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ closer to doing more blogging!
(insert (concat "#+BLOG: " org2blog/wp-blog-name "\n"))
(insert (concat "#+POSTID: " post-id "\n"))))
(org2blog/wp-save-details post post-id publish subtree-p)

(org2blog/update-post-thumbnail)

(message (if publish
"Published your post: “%s”. Its ID is “%s”."
"Saved your post as a draft: “%s”. Its ID is “%s”.")
Expand Down Expand Up @@ -1561,6 +1564,67 @@ and munge it a little to make it suitable to use with the




;;; update post thumbnail (eyecatch image)

(defun org2blog/wp-get-post-thumbnail (post-id)
(cdr
(assoc
"post_thumbnail"
(xml-rpc-method-call
org2blog/wp-server-xmlrpc-url
"wp.getPost"
org2blog/wp-server-blogid
org2blog/wp-server-userid
org2blog/wp-server-pass
post-id
'("post_thumbnail")))))

(defun org2blog/wp-get-post-thumbnail-file (post-id)
(cdr (assoc "file" (cdr (assoc "metadata" (org2blog/wp-get-post-thumbnail post-id))))))

(defun org2blog/nondir (file)
" a/b/c => c "
(if (stringp file) (car (last (split-string file "/")))))

(defun org2blog/wp-edit-post-thumbnail (post-id post-thumbnail-attachment-id)
(xml-rpc-method-call
org2blog/wp-server-xmlrpc-url
"wp.editPost"
org2blog/wp-server-blogid
org2blog/wp-server-userid
org2blog/wp-server-pass
post-id
`(("post_thumbnail" . ,post-thumbnail-attachment-id))))

(defun org2blog/update-post-thumbnail ()
(let* ((post-id (org2blog/wp-get-option "POSTID"))
(curr-info (org2blog/wp-get-post-thumbnail post-id))
;;(curr-file (or (cdr (assoc "file" (cdr (assoc "metadata" curr-info)))) ""))
(curr-title (or (cdr (assoc "title" curr-info)) ""))
(new-file (org2blog/wp-get-option "POST_THUMBNAIL"))) ;;string or nil

(when (and (stringp new-file) ;; new-file==nil(POST_THUMBNAIL option is not specified) means keep curr-info
(not (string= (org2blog/nondir curr-title) (org2blog/nondir new-file))))
;; delete curr-file @todo
;; upload new-file
(let ((attachment-id (if (> (length new-file) 0)
(cdr (assoc "id"
(metaweblog-upload-file
org2blog/wp-server-xmlrpc-url
org2blog/wp-server-userid
org2blog/wp-server-pass
org2blog/wp-server-blogid
(get-file-properties new-file))))
""))) ;;"" means remove post thumbnail
;; set new-file to post-thumbnail
(if (not (null attachment-id))
(org2blog/wp-edit-post-thumbnail post-id attachment-id))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;; Mode

;;;###autoload
Expand Down

0 comments on commit f3e9385

Please sign in to comment.