コード例 #1
0
ファイル: Repository.php プロジェクト: vutran/wpmvc-core
 /**
  * Creates a new post
  *
  * @access public
  * @param array $postData           An array of post data that complies with wp_insert_post()
  * @param string $fileKey           (default: false) The ID of the $_FILES key to set as the featured image
  * @return int                      The ID of the post if the post is successfully added to the database. On failure, it returns 0 if $wp_error is set to false, or a WP_Error object if $wp_error is set to true.
  */
 public function add($postData, $fileKey = false)
 {
     // Set the post type
     $postData['post_type'] = $this->postType;
     // Insert the post and return the response
     $newPostId = WP::wpInsertPost($postData);
     // If the new post was added successfully
     if ($newPostId && !WP::isWpError($newPostId)) {
         // If the file is uploaded
         if (is_uploaded_file($_FILES[$fileKey]['tmp_name'])) {
             require_once ABSPATH . 'wp-admin/includes/image.php';
             require_once ABSPATH . 'wp-admin/includes/file.php';
             require_once ABSPATH . 'wp-admin/includes/media.php';
             // Upload the file
             $attachmentId = WP::mediaHandleUpload($fileKey, $newPostId);
             // If the attachment is valid
             if ($attachmentId && !WP::isWpError($attachmentId)) {
                 // Set the post thumbnail
                 WP::setPostThumbnail($newPostId, $attachmentId);
             }
         }
     }
     return $newPostId;
 }