예제 #1
0
 /**
  * Executes the stub import by updating the Wordpress post and then adds its references to $html_post_lookup.
  *
  * @param WebPage       $webPage
  * @param WPMetaConfigs $meta
  * @param null          $html_post_lookup
  * @param null          $media_lookup
  *
  * @return mixed|void
  */
 protected function doImport(WebPage $webPage, WPMetaConfigs $meta, &$html_post_lookup = null, &$media_lookup = null)
 {
     $updateResult = $meta->updateWPPost();
     $html_post_lookup[$webPage->getFullPath()] = $meta->getPostId();
     if (is_wp_error($updateResult)) {
         echo '<li>***Unable to create content ' . $meta->getPostTitle() . ' from ' . $webPage->getFullPath() . '</li>';
     } else {
         $webPage->setWPID($updateResult);
         echo '<li>Stub post created from ' . $webPage->getFullPath() . ' into post #' . $updateResult . ' with title ' . $meta->getPostTitle() . '</li>';
     }
 }
예제 #2
0
 /**
  * Function that performs the action of updating the local links on the webpage.
  *
  * @param WebPage          $webPage
  * @param HTMLImportStages $stagesSettings
  * @param WPMetaConfigs    $meta
  * @param null             $html_post_lookup
  *
  * @return null
  */
 protected function performStage(WebPage $webPage, HTMLImportStages $stagesSettings, WPMetaConfigs &$meta, &$html_post_lookup = null)
 {
     $body = $meta->getPostContent();
     if (!is_null($html_post_lookup)) {
         $bodyXML = XMLHelper::getXMLObjectFromString($body);
         $filepath = dirname($meta->getSourcePath());
         $link_table = array();
         // get a list of all the links in the page and iterate through them
         $all_links = $bodyXML->xpath('//a[@href]');
         // TODO: encapsulate this in a function use XMLHelper::getAllHRefsFromHTML as a start
         if ($all_links) {
             foreach ($all_links as $link) {
                 // iterate the link's attributes to find the HREF value
                 foreach ($link->attributes() as $attribute => $value) {
                     $path = '' . $value;
                     if (0 == strcasecmp('href', $attribute)) {
                         // TODO: handle foo.html#rar
                         if (!preg_match('/^[a-zA-Z].*:.*/', $path)) {
                             // TODO: need to handle foo.html without handling http://...
                             // only handle files that end in .html or .htm
                             if (preg_match('/\\.([hH][tT][mM][lL]?)$/', $path)) {
                                 // if the file the path links to has been imported already, then it will exist in the lookup and we can update the link, otherwise leave the link alone
                                 $fullpath = $webPage->getFullPath($path);
                                 if ($fullpath) {
                                     if (array_key_exists($fullpath, $html_post_lookup)) {
                                         $link_table[$path] = $fullpath;
                                     }
                                 } else {
                                     echo '<span>***could not update link ' . $path . '</span><br>';
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // after building a list of all the links to update and what to update them to, we can do a change in the html file as a whole to catch all references
         foreach ($link_table as $link => $full_link) {
             $post_id = $html_post_lookup[$full_link];
             $post_link = get_permalink($post_id);
             echo 'Updating ' . $link . ' with ' . $post_link . '<br>';
             $search_str = '/(\\b[hH][rR][eE][fF]\\s*=\\s*")([\\b\\.\\/]*' . preg_quote($link, '/') . '\\b)(")/';
             $body = preg_replace($search_str, '$1' . preg_quote($post_link, '/') . '$3', $body);
         }
         $meta->setPostContent($body);
     }
 }
예제 #3
0
 /**
  * Processes through each stage and if permitted, executes them.
  *
  * @param WebPage       $webPage
  * @param WPMetaConfigs $meta
  * @param null          $html_post_lookup
  * @param null          $media_lookup
  *
  * @return mixed|void
  */
 protected function doImport(WebPage $webPage, WPMetaConfigs $meta, &$html_post_lookup = null, &$media_lookup = null)
 {
     $meta->setPostContent($meta->getPostContent());
     $nothing = null;
     $this->stageParse($webPage, $this->htmlImportStage, $meta, $nothing);
     $this->stageParse($webPage, $this->GDNHeaderFooterStage, $meta, $nothing);
     $this->stageParse($webPage, $this->updateLinksImportStage, $meta, $html_post_lookup);
     $this->stageParse($webPage, $this->mediaImportStage, $meta, $media_lookup);
     $meta->updateWPPost();
     // this happens automatically at the end, but needs to happen here to guarantee an ID for the template update
     $postContent = $meta->getPostContent();
     $this->stageParse($webPage, $this->setTemplateStage, $meta, $postContent, $nothing);
     $updateResult = $meta->updateWPPost();
     if (is_wp_error($updateResult)) {
         echo '<li>***Unable to fill content ' . $meta->getPostTitle() . ' from ' . $meta->getSourcePath() . ': ' . $updateResult->get_error_message() . '</li>';
     } else {
         echo '<li>Content filled from ' . $meta->getSourcePath() . ' into post #' . $updateResult . ' with title ' . $meta->getPostTitle() . '</li>';
     }
 }
예제 #4
0
 /**
  * Saves the post in wordpress.
  *
  * @param WPMetaConfigs $meta
  *
  * @return int|\WP_Error
  */
 protected function save(WPMetaConfigs $meta)
 {
     return $meta->updateWPPost();
 }
예제 #5
0
 /**
  * Executes the actual stage of importing the HTML contents.  <BODY> tags are converted to <DIV> tags.
  *
  * @param WebPage          $webPage
  * @param HTMLImportStages $stagesSettings
  * @param WPMetaConfigs    $meta
  * @param null             $other
  *
  * @return mixed|void
  */
 protected function performStage(WebPage $webPage, HTMLImportStages $stagesSettings, WPMetaConfigs &$meta, &$other = null)
 {
     $meta->setPostContent($this->replaceBodyWithDivs($meta->getPostContent()));
 }
 protected function performStage(WebPage $webPage, HTMLImportStages $stagesSettings, WPMetaConfigs &$meta, &$other = null)
 {
     $meta->setPostContent($this->getGridDirectorHeader($meta->getPostTitle()) . $meta->getPostContent() . $this->getGridDirectorFooter());
 }
예제 #7
0
 /**
  * Performs the stage action of uploading media files and updating the WebPage accordingly.
  *
  * @param WebPage          $webPage
  * @param HTMLImportStages $stagesSettings
  * @param WPMetaConfigs    $meta
  * @param null             $media_lookup
  *
  * @return null
  */
 protected function performStage(WebPage $webPage, HTMLImportStages $stagesSettings, WPMetaConfigs &$meta, &$media_lookup = null)
 {
     $post_id = $meta->getPostId();
     $body = $meta->getPostContent();
     if (is_null($body) || strcmp('', $body) == 0) {
         echo '** the body for post ' . $post_id . ' was empty, no media to import.';
         return;
     }
     $media_table = array();
     $file_as_xml_obj = XMLHelper::getXMLObjectFromString($body);
     // import img srcs
     $all_imgs = $file_as_xml_obj->xpath('//img[@src]');
     if ($all_imgs) {
         foreach ($all_imgs as $img) {
             foreach ($img->attributes() as $attribute => $value) {
                 $path = '' . $value;
                 if (0 == strcasecmp('src', $attribute)) {
                     // TODO: this is duplicated below, refactor it out
                     if (!preg_match('/^[a-zA-Z].*:.*/', $path)) {
                         // if it's local
                         if (!is_null($media_lookup) && !array_key_exists($path, $media_table)) {
                             $fullpath = $webPage->getFullPath($path);
                             if (array_key_exists($fullpath, $media_lookup)) {
                                 $attach_id = $media_lookup[$fullpath];
                                 require_once ABSPATH . 'wp-admin/includes/image.php';
                                 $attach_data = wp_get_attachment_metadata($attach_id);
                                 wp_update_attachment_metadata($attach_id, $attach_data);
                                 $media_table[$path] = $fullpath;
                             } else {
                                 $filename = basename($fullpath);
                                 $upload = wp_upload_bits($filename, null, $webPage->getLinkContents($path));
                                 if ($upload['error']) {
                                     echo '<li>***Unable to upload media file ' . $filename . '</li>';
                                 } else {
                                     echo '<li>' . $filename . ' media file uploaded.</li>';
                                     $wp_filetype = wp_check_filetype(basename($upload['file']), null);
                                     $attachment = array('guid' => $upload['file'], 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($upload['file'])), 'post_content' => '', 'post_status' => 'inherit');
                                     $attach_id = wp_insert_attachment($attachment, $upload['file'], $post_id);
                                     require_once ABSPATH . 'wp-admin/includes/image.php';
                                     $attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
                                     wp_update_attachment_metadata($attach_id, $attach_data);
                                     $media_lookup[$fullpath] = $attach_id;
                                     $media_table[$path] = $fullpath;
                                     echo '<li>' . $filename . ' attached to post ' . $post_id . '</li>';
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // linked media
     $all_links = $file_as_xml_obj->xpath('//a[@href]');
     // TODO: encapsulate this in a function
     if ($all_links) {
         foreach ($all_links as $link) {
             foreach ($link->attributes() as $attribute => $value) {
                 $path = '' . $value;
                 if (0 == strcasecmp('href', $attribute)) {
                     if (!preg_match('/^[a-zA-Z].*:.*/', $path)) {
                         if (preg_match('/\\.(png|bmp|jpg|jpeg|gif|pdf|doc|docx|mp3|ogg|wav)$/', strtolower($path))) {
                             // media png,bmp,jpg,jpeg,gif,pdf,doc,docx,mp3,ogg,wav
                             if (!is_null($media_lookup)) {
                                 /*if ( $path[0] != '/' ) {
                                 			$fullpath = realpath( dirname( $meta->getSourcePath() ) . '/' . $path );
                                 		} else {
                                 			$fullpath = $path;
                                 		}*/
                                 $fullpath = $webPage->getFullPath($path);
                                 if (array_key_exists($fullpath, $media_lookup)) {
                                     $attach_id = $media_lookup[$fullpath];
                                     require_once ABSPATH . 'wp-admin/includes/image.php';
                                     $attach_data = wp_get_attachment_metadata($attach_id);
                                     wp_update_attachment_metadata($attach_id, $attach_data);
                                     $media_table[$path] = $fullpath;
                                 } else {
                                     $filename = basename($fullpath);
                                     $upload = wp_upload_bits($filename, null, $webPage->getLinkContents($path));
                                     if ($upload['error']) {
                                         echo '<li>***Unable to upload media file ' . $filename . '</li>';
                                     } else {
                                         echo '<li>' . $filename . ' media file uploaded.</li>';
                                         $wp_filetype = wp_check_filetype(basename($upload['file']), null);
                                         $attachment = array('guid' => $upload['file'], 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($upload['file'])), 'post_content' => '', 'post_status' => 'inherit');
                                         $attach_id = wp_insert_attachment($attachment, $upload['file'], $post_id);
                                         require_once ABSPATH . 'wp-admin/includes/image.php';
                                         $attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
                                         wp_update_attachment_metadata($attach_id, $attach_data);
                                         $media_lookup[$fullpath] = $attach_id;
                                         $media_table[$path] = $fullpath;
                                         echo '<li>' . $filename . ' attached to post ' . $post_id . '</li>';
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     foreach ($media_table as $media_item => $full_media_path) {
         $media_id = $media_lookup[$full_media_path];
         $media_url = wp_get_attachment_url($media_id);
         $search_str = '/(\\b[iI][mM][gG]\\s*[^>]*\\s+[sS][rR][cC]\\s*=\\s*")([\\b\\/\\.]*' . preg_quote($media_item, '/') . '\\b)(")/';
         $body = preg_replace($search_str, '$1' . preg_quote($media_url, '/') . '$3', $body);
         // img src
         $body = preg_replace('/(\\b[hH][rR][eE][fF]\\s*=\\s*")(\\b' . preg_quote($media_item, '/') . '\\b)(")/', '$1' . preg_quote($media_url, '/') . '$3', $body);
         // a href
     }
     $meta->setPostContent($body);
     echo '<li>Post ' . $post_id . ' updated with correct image links.</li>';
 }
예제 #8
0
 /**
  * Performs the action of setting the appropriate template to the page.
  *
  * @param WebPage          $webPage
  * @param HTMLImportStages $stagesSettings
  * @param WPMetaConfigs    $meta
  * @param null             $other
  *
  * @return null
  */
 protected function performStage(WebPage $webPage, HTMLImportStages $stagesSettings, WPMetaConfigs &$meta, &$other = null)
 {
     update_post_meta($meta->getPostId(), '_wp_page_template', $meta->getPageTemplate());
 }