return $id;
}
+ private function _getAttachmentByName($name)
+ {
+ global $wpdb;
+ $sql = "
+ SELECT *
+ FROM {$wpdb->prefix}posts
+ WHERE post_type = 'attachment'
+ AND (post_name like '{$name}%' OR post_title like '{$name}%')";
+ $results = $wpdb->get_results($sql, OBJECT);
+ return (!empty($results)) ? $results[0]->ID : false;
+ //return (!empty($results)) ? $results[0] : false;
+ }
+
private function _fetchRemoteImage($file, $path, $baseUrl = null)
{
- $filename = wp_unique_filename($path, $file);
+ $saveFileName = preg_replace( '/[?].*$/', '', $file );
+ $filename = wp_unique_filename($path, $saveFileName);
$fp = fopen($path . '/' . $filename, 'w+');
$fileUrl = ($baseUrl) ? $baseUrl . '/' . $file : $this->_options['toolbox_image_url'] . $file;
return $filename;
}
+ private function _updateMediaSrc()
+ {
+ global $wpdb;
+
+ $sql = "
+ SELECT *
+ FROM {$wpdb->prefix}posts
+ WHERE post_content LIKE '%<img%'
+ AND post_type = 'post'";
+ //$sql .= " LIMIT 1 OFFSET 0";
+
+ $results = $wpdb->get_results($sql, OBJECT);
+
+ echo count( $results );
+
+ if (count($results) > 0) {
+ foreach ($results as $post) {
+ $content = $post->post_content;
+ echo '<pre>Updating Media Src\'s: ' . print_r($post->post_title, true) . '</pre>';
+ echo '<div style="border: 1px solid green; padding: 9px; margin: 10px;">';
+ if (preg_match_all('%(:?\[caption [^\]]+\])?<img ([^>]+)>(:?([^\[]*)\[/caption\])?%', $content, $cMatch)) {
+ $captionCount = count($cMatch[0]);
+ //echo '<p>Matches in Caption ' . $captionCount . '</p>';
+ //echo '<pre>' . var_export($cMatch, true) . '</pre>';
+ for ($index = 0; $index < $captionCount; ++$index) {
+ //echo '<pre>cMatch: ' . print_r($cMatch[0][$index], true) . '</pre>';
+ echo '<pre>'.htmlspecialchars($cMatch[0][$index]).'</pre>';
+ //echo '<pre>Caption Match: ' . print_r($cMatch[1][$index], true) . '</pre>';
+ //echo '<pre>Caption Text: ' . print_r($cMatch[2][$index], true) . '/End Caption Text</pre>';
+ if (preg_match('%src="([^"]+)"%', $cMatch[0][$index], $srcMatch)) {
+ //echo '<pre>' . print_r($srcMatch, true) . '</pre>';
+ echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true) . '</pre>';
+ $fileInfo = pathinfo($srcMatch[1]);
+ $fileName = basename($srcMatch[1]);
+ $fileBaseName = $fileInfo['filename'];
+ echo '<pre>File Info: ' . print_r($fileInfo, true) . '</pre>';
+ echo '<pre>File Name: ' . print_r($fileName, true) . '</pre>';
+ $path = str_replace($fileName, '', $srcMatch[1]);
+ $path = $fileInfo['dirname'];
+ //echo '<pre>'.var_dump($path).'</pre>';
+
+ $imagesKey = $this->_getAttachmentByName($fileBaseName);
+ var_dump($fileBaseName);
+ var_dump($imagesKey);
+ if (!$imagesKey) {
+ $imagesKey = $this->_handleMediaFile(
+ $fileName,
+ $fileBaseName,
+ '',
+ $post->ID,
+ $path
+ );
+ }
+
+ if ($imagesKey) {
+ var_dump($imagesKey);
+ $newImage = wp_get_attachment_image_src($imagesKey, 'full');
+ // need to replace the src tags in content with the new image tags
+ $caption = str_replace(
+ 'src="' . $srcMatch[1] . '"',
+ 'src="' . $newImage[0] . '"',
+ $cMatch[0][$index]);
+ $caption = preg_replace('/wp-image-([0-9]*)/', 'wp-image-' . $imagesKey , $caption);
+ $caption = preg_replace('/attachment_([0-9]*)/', 'attachment_' . $imagesKey, $caption);
+ $content = str_replace($cMatch[0][$index], $caption, $content);
+ }
+ }
+ }
+ }
+ //echo '<pre>'.htmlspecialchars($caption).'</pre>';
+ //exit;
+ echo '<pre>'.htmlspecialchars($content).'</pre>';
+ echo '</div>';
+ $updatePost = array(
+ 'ID' => $post->ID,
+ 'post_content' => $content
+ );
+ wp_update_post($updatePost);
+ }
+ }
+ }
+
private function replaceBlogUrls()
{
if (!$this->_options['blog_url']) {
return false;
}
global $wpdb;
+ $images = array();
$this->_connect();
echo '<p>Replace Blog url\'s</p>';
$searchUrl = $this->_options['blog_url'];
$content = $post->post_content;
preg_match_all($pattern, $post->post_content, $matches);
$matches = array_unique($matches[0]);
- echo '<pre>' . print_r($post->ID, true) . '</pre>';
+ //echo '<pre>' . print_r($post->ID, true) . '</pre>';
echo '<pre>' . print_r($matches, true) . '</pre>';
foreach ($matches as $match) {
// process the file as media library
$parsed = parse_url($match);
$filename = basename($parsed['path']);
$rootUrl = str_replace('/' . $filename, '', $match);
- echo '<pre>' . print_r($parsed, true) . '</pre>';
+ $images[] = $filename;
echo '<pre>' . print_r($filename, true) . '</pre>';
- echo '<pre>' . print_r($rootUrl, true) . '</pre>';
- $img_id = $this->_handleMediaFile(
- $filename,
- '',
- '',
- $post->ID,
- $rootUrl . '/'
- );
- echo '<pre>' . print_r($img_id, true) . '</pre>';
- $replaceUrl = wp_get_attachment_url($img_id);
- echo '<pre>' . print_r($image, true) . '</pre>';
- $content = str_replace($match, $replaceUrl, $content);
+ $fileInfo = pathinfo($match);
+ $fileBaseName = $fileInfo['filename'];
+ $img_id = $this->_getAttachmentByName($fileBaseName);
+ if ( $img_id ) {
+ echo '<pre>' . print_r($img_id, true) . '</pre>';
+ $replaceUrl = wp_get_attachment_url($img_id);
+ echo '<pre>' . print_r($image, true) . '</pre>';
+ $content = str_replace($match, $replaceUrl, $content);
+ echo '<b>Replaced ' . $filename . '</b>';
+ }
}
+ //echo '<pre>'.htmlspecialchars($content).'</pre>';
$updatePost = array(
'ID' => $post->ID,
'post_content' => $content
);
wp_update_post($updatePost);
}
+ echo '<pre>' . print_r($images, true) . '</pre>';
+ }
+ }
+
+ public function findLauryImages()
+ {
+ global $wpdb;
+
+ $author_id = 4; // Laury
+
+ $the_query = new WP_Query(
+ array(
+ 'posts_per_page' => -1,
+ 'post_type' => 'attachment',
+ 'post_status' => 'inherit',
+ 'author' => $author_id
+ )
+ );
+ if ( $the_query->have_posts() ) :
+ echo '<p>found ' . $the_query->found_posts . '</p>';
+ while ( $the_query->have_posts() ) : $the_query->the_post();
+ echo $the_query->post->ID . '<br>';
+ echo get_the_title() . '<br>';
+ echo wp_get_attachment_url( ) . '<br>';
+ wp_delete_attachment( $the_query->post->ID, true );
+ endwhile;
+ endif;
+
+ }
+
+ function checkForBlogImages()
+ {
+ global $wpdb;
+
+ $sql = "
+ SELECT *
+ FROM {$wpdb->prefix}posts
+ WHERE post_content LIKE '%<img%'
+ AND post_type = 'post'";
+
+ $results = $wpdb->get_results($sql, OBJECT);
+
+ echo count( $results );
+
+ if (count($results) > 0) {
+ foreach ($results as $post) {
+ $content = $post->post_content;
+ echo '<pre>Updating Media Src\'s: ' . print_r($post->post_title, true) . '</pre>';
+ echo '<div style="border: 1px solid green; padding: 9px; margin: 10px;">';
+ if (preg_match_all('%(:?\[caption [^\]]+\])?<img ([^>]+)>(:?([^\[]*)\[/caption\])?%', $content, $cMatch)) {
+ $captionCount = count($cMatch[0]);
+ //echo '<p>Matches in Caption ' . $captionCount . '</p>';
+ //echo '<pre>' . var_export($cMatch, true) . '</pre>';
+ for ($index = 0; $index < $captionCount; ++$index) {
+ //echo '<pre>cMatch: ' . print_r($cMatch[0][$index], true) . '</pre>';
+ echo '<pre>'.htmlspecialchars($cMatch[0][$index]).'</pre>';
+ //echo '<pre>Caption Match: ' . print_r($cMatch[1][$index], true) . '</pre>';
+ //echo '<pre>Caption Text: ' . print_r($cMatch[2][$index], true) . '/End Caption Text</pre>';
+ if (preg_match('%src="([^"]+)"%', $cMatch[0][$index], $srcMatch)) {
+ //echo '<pre>' . print_r($srcMatch, true) . '</pre>';
+ echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true) . '</pre>';
+ $fileInfo = pathinfo($srcMatch[1]);
+ $fileName = basename($srcMatch[1]);
+ $fileBaseName = $fileInfo['filename'];
+ echo '<pre>File Info: ' . print_r($fileInfo, true) . '</pre>';
+ echo '<pre>File Name: ' . print_r($fileName, true) . '</pre>';
+ $path = str_replace($fileName, '', $srcMatch[1]);
+ $path = $fileInfo['dirname'];
+ //echo '<pre>'.var_dump($path).'</pre>';
+
+ }
+ }
+ }
+
+ echo '<pre>'.htmlspecialchars($content).'</pre>';
+ echo '</div>';
+ }
}
}
switch($step) {
case 0:
if ($this->_greet()) {
- echo '<p><a href="admin.php?import=blogimages&step=1">Import Images</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=1">Update Blog Images</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=2">Update Blog URL\'s</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=3">Remove Laury\'s Images</a></p>';
}
+ $this->checkForBlogImages();
break;
case 1:
+ echo '<p><a href="admin.php?import=blogimages&step=2">Update Blog URL\'s</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=3">Remove Laury\'s Images</a></p>';
echo '<p>Updating Blog Images</p>';
+ $this->_updateMediaSrc();
+ break;
+ case 2:
+ echo '<p><a href="admin.php?import=blogimages&step=1">Update Blog Images</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=3">Remove Laury\'s Images</a></p>';
$this->replaceBlogUrls();
break;
+ case 3:
+ echo '<p><a href="admin.php?import=blogimages&step=1">Update Blog Images</a></p>';
+ echo '<p><a href="admin.php?import=blogimages&step=2">Update Blog URL\'s</a></p>';
+ $this->findLauryImages();
+ break;
}
$this->_footer();