/**
  * Convert markdown image to html image tag
  *
  * @return string
  */
 public function convert()
 {
     $time = get_the_time('Y/m', $this->post_id);
     $wp_upload_dir = wp_upload_dir($time, $this->post_id);
     $this->upload_url = untrailingslashit($wp_upload_dir['url']);
     return preg_replace_callback('/\\!\\[(.*?)\\]\\((.+?)\\)/sm', function ($matches) {
         $pathinfo = pathinfo($matches[2]);
         $Unicode_Normalization = new Markdown_Importer_Unicode_Normalization($matches[2]);
         $filename = $Unicode_Normalization->convert();
         $filename = sha1($filename) . '.' . $pathinfo['extension'];
         $attachment_url = $this->upload_url . '/' . $filename;
         $attachment_id = attachment_url_to_postid($attachment_url);
         $full = wp_get_attachment_image_url($attachment_id, 'full');
         $large = wp_get_attachment_image_url($attachment_id, 'large');
         if (!$full || !$large) {
             return;
         }
         return sprintf('<a class="markdown-importer-image-link" href="%1$s"><img class="size-large wp-image-%2$d markdown-importer-image" src="%3$s" alt="%4$s" /></a>', esc_url($full), esc_attr($attachment_id), esc_url($large), esc_attr($matches[1]));
     }, $this->content);
 }
Exemple #2
0
 /**
  * Import the image file
  *
  * @param string $file
  * @param int $post_id
  * @return bool
  */
 protected function _import_the_image($file, $post_id)
 {
     require_once ABSPATH . 'wp-admin' . '/includes/media.php';
     require_once ABSPATH . 'wp-admin' . '/includes/image.php';
     $time = get_the_time('Y/m', $post_id);
     $wp_upload_dir = wp_upload_dir($time, $post_id);
     $upload_dir = untrailingslashit($wp_upload_dir['path']);
     $pathinfo = pathinfo($file);
     $Unicode_Normalization = new Markdown_Importer_Unicode_Normalization(basename($file));
     $filename = $Unicode_Normalization->convert();
     $filename = sha1($filename) . '.' . $pathinfo['extension'];
     $new_filepath = $upload_dir . '/' . $filename;
     if (file_exists($new_filepath)) {
         return false;
     }
     rename($file, $new_filepath);
     $wp_check_filetype = wp_check_filetype($new_filepath);
     $attachment = array('post_mime_type' => $wp_check_filetype['type'], 'post_title' => basename($file), 'post_status' => 'inherit', 'post_content' => __('Uploaded from the Markdown Importer', 'markdown-importer'));
     $attach_id = wp_insert_attachment($attachment, $new_filepath, $post_id);
     if (!$attach_id) {
         return false;
     }
     $attach_data = wp_generate_attachment_metadata($attach_id, $new_filepath);
     wp_update_attachment_metadata($attach_id, $attach_data);
     return true;
 }