From 2e052a7a3dd0ec7ae801cf15042c074d8a5e6d6a Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 4 Aug 2015 17:01:02 -0400 Subject: [PATCH] Update WIP northernlakes --- controllers/Import.php | 131 ++++++++++++++++++++++++++++++++++------- 1 file changed, 109 insertions(+), 22 deletions(-) diff --git a/controllers/Import.php b/controllers/Import.php index e64490e..81a4053 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -295,6 +295,26 @@ return "\n".''.$fileName.''; } + private function _addMedia($file, $postId) + { + $fileType = wp_check_filetype(basename($file), null); + $wpUploadDir = wp_upload_dir(); + $attachment = array( + 'guid' => $wpUploadDir . '/' . basename($file), + 'post_mime_type' => $fileType['type'], + 'post_title' => basename($file), + 'post_content' => '', + 'post_status' => 'inherit' + ); + $id = wp_insert_attachment($attachment, $file, $postId); + if (!is_wp_error($id)) { + $data = wp_generate_attachment_metadata($id, $file); + wp_update_attachment_metadata($id, $data); + } + return ($id) ? $this->_getAttachmentById($id) : false; + } + + /** * _handleMediaFile * @@ -828,9 +848,11 @@ FROM {$wpdb->prefix}posts WHERE post_content LIKE '%get_results($sql, OBJECT); - echo '

'.count($results).'

'; + echo '

Number of Pages'.count($results).'

'; if (count($results) > 0) { $pattern = ';(]>);si'; foreach ($results as $post) { @@ -840,30 +862,63 @@ $images = $dom->getElementsByTagName('img'); foreach ($images as $image) { $width = $height = null; + echo '
'; //echo '
' . print_r($image, true)  . '
'; if ($image->hasAttribute('class')) { - //echo '
' . print_r($image->getAttribute('class'), true)  . '
'; + echo '
' . print_r($image->getAttribute('class'), true)  . '
'; + if (preg_match('%wp-image-\d+%', $image->getAttribute('class'))) { + //echo 'Continue'; + //echo '
'; + //continue; + } } if ($image->hasAttribute('style')) { $style = $image->getAttribute('style'); + var_dump($style); echo '
' . print_r($style, true)  . '
'; - if (preg_match(';[^-]width[: ]+(\d+)px;', $style, $wMatch)) { + if (preg_match('%[^-]width[: ]+(\d+)%', $style, $wMatch)) { $width = $wMatch[1]; + echo '
wMatch: ' . print_r($wMatch, true)  . '
'; } - if (preg_match(';[^-]height[: ]+(\d+)px;', $style, $hMatch)) { + if (preg_match('%[^-]height[: ]+(\d+)%', $style, $hMatch)) { $height = $hMatch[1]; } - echo '
' . print_r($width, true)  . '
'; - echo '
' . print_r($height, true)  . '
'; + echo '
width: ' . print_r($width, true)  . '
'; + echo '
height: ' . print_r($height, true)  . '
'; + } + if ($image->hasAttribute('width')) { + $width = $image->getAttribute('width'); + } + if ($image->hasAttribute('height')) { + $height = $image->getAttribute('height'); } if ($image->hasAttribute('src')) { //echo '
' . print_r($image->getAttribute('src'), true)  . '
'; $imgName = basename($image->getAttribute('src')); echo '
' . print_r($imgName, true)  . '
'; $imgPathInfo = pathinfo($imgName); + echo '
' . print_r($imgPathInfo, true) . '
'; $wpImage = $this->_getAttachmentByName($imgPathInfo['filename']); + //echo '
' . print_r($wpImage, true) . '
'; + //exit; + if (!$wpImage) { + echo '

Image not found as attachment

'; + echo '
' . print_r($imgPathInfo, true) . '
'; + if (preg_match(';(is\d{2}-\d{10}-\d{5})(\d{1}|[-]\d+x\d+);', $imgPathInfo['filename'], $matches)) { + echo '

Duplicate Image found

'; + echo '
' . print_r($matches, true) . '
'; + $wpImage = $this->_getAttachmentByName($matches[1]); + if ($wpImage) { + echo '

Found Image

'; + } else { + echo '

Image still not found

'; + continue; + } + } + } if (!$wpImage) { - echo '
' . print_r($image->getAttribute('src'), true) . '
'; + echo '

Image not found as attachment 2

'; + //echo '
' . print_r($image->getAttribute('src'), true) . '
'; $parsed = parse_url($image->getAttribute('src')); $filename = basename($parsed['path']); if (preg_match(';is\d{2,}-\d{10,}-\d{5,};', $filename)) { @@ -871,21 +926,31 @@ echo '
' . print_r($parsed, true) . '
'; echo '
' . print_r($filename, true) . '
'; echo '
' . print_r($rootUrl, true) . '
'; - $wpImage = $this->_handleMediaFile( - basename($parsed['path']), - '', - $post->ID, - $rootUrl . '/' - ); + //if (preg_match('/(\d{4})\/(\d{2})$/', $rootUrl, $matches)) { + //echo '
' . print_r($matches, true) . '
'; + //$uploadDir = wp_upload_dir(); + //echo '
' . print_r($uploadDir, true) . '
'; + //$absPath = $uploadDir['basedir'] . '/' . $matches[1] . '/' . $matches[2]; + //echo '
' . print_r($absPath, true) . '
'; + ////$wpImage = $this->_addMedia($absPath . '/' . $filename, $post->ID); + //echo '
' . print_r($imageId, true) . '
'; + //} + //$fileExists = file_exists($upload_dir['baseurl'] . '/' . $filename); + //$wpImage = $this->_handleMediaFile( + //basename($parsed['path']), + //'', + //$post->ID, + //$rootUrl . '/' + //); } } if ($wpImage) { //$strToReplace = $dom->saveHTML($image); //echo '
'.htmlspecialchars($strToReplace).'
'; + echo '

Image found: id '.$wpImage->ID.'

'; if ($width && $height) { - echo '

Image found: id '.$wpImage->ID.'

'; - //echo '

width: ' . $width . '

'; - //echo '

height: ' . $height . '

'; + echo '

width: ' . $width . '

'; + echo '

height: ' . $height . '

'; if ($width > 300 || $height > 300) { echo '

Found Large

'; $size = 'large'; @@ -911,15 +976,25 @@ $image->setAttribute('src', $newImage[0]); } } - $image->removeAttribute('style'); - $image->setAttribute('width', $width); - $image->setAttribute('height', $height); - //echo htmlspecialchars($dom->saveHTML($image)); + } else { + $size = 'large'; + $newImage = wp_get_attachment_image_src($wpImage->ID, $size); + $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID); + $width = $newImage[1]; + $height = $newImage[2]; + if($newImage) { + $image->setAttribute('src', $newImage[0]); + } } + $image->removeAttribute('style'); + $image->setAttribute('width', $width); + $image->setAttribute('height', $height); + //echo htmlspecialchars($dom->saveHTML($image)); } else { echo '

no media found

'; } } + echo ''; } // remove the extra stuff Dom put in $content = preg_replace( @@ -948,7 +1023,19 @@ SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' - AND post_title = '{$name}'"; + AND post_name = '{$name}'"; + $results = $wpdb->get_results($sql, OBJECT); + return (!empty($results)) ? $results[0] : false; + } + + private function _getAttachmentById($id) + { + global $wpdb; + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_type = 'attachment' + AND ID = {$id}"; $results = $wpdb->get_results($sql, OBJECT); return (!empty($results)) ? $results[0] : false; } -- 2.17.1