From 9ea466e72c920415bf4bb98680974d5aaeaf7a50 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 15 Jul 2015 12:24:53 -0400 Subject: [PATCH] Finish the url replace and keyword replace --- controllers/Import.php | 136 +++++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 45 deletions(-) diff --git a/controllers/Import.php b/controllers/Import.php index 4ba6360..1cc15bb 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -504,50 +504,6 @@ echo '
' . print_r($this->_options, true) . '
'; } - /** - * _findAndReplace - * - * Find and Replace all the 'keyword' - * - * @param mixed $haystack String for replacement - * - * @access private - * @return void - */ - private function _findAndReplace($haystack) - { - if (strstr($haystack, '{') !== false) { - $pattern = '/\{([A-Z0-9\&\-\,\'\" ]*)\}/i'; - if (preg_match_all($pattern, $haystack, $needle) != 0) { - $total = count($needle[0]); - for ($iter = 0; $iter < $total; $iter++) { - // replace this with the real post - $page = $this->_pageGateway->findByKeyword($needle[1][$iter]); - if ($page == false) { - //return $haystack; - } else { - // need to get the page url for this from wp - $seoUrl = Toolkit_Template_Page::getSeoUrl( - $this->_pageGateway, - $page['id'] - ); - $anchor = ''.$page['navigation_name'].''; - $haystack = str_replace($needle[0][$iter], $anchor, $haystack); - } - } - return $haystack; - } else { - return $haystack; - } - - if (strstr($haystack, '{') !== false) { - return $this->_findAndReplace($haystack); - } - } - - return $haystack; - } - /** * _import * @@ -606,8 +562,9 @@ } } - private function _replaceUrls() + private function _replaceKeywords() { + global $wpdb; $this->_connect(); echo '

Replace keywords

'; $sql = " @@ -618,8 +575,92 @@ $stmt = $this->_dbh->query($sql); while ($page = $stmt->fetch()) { echo '
' . print_r($page, true) .  '
'; + // find out which page (wp) this needs to be + $wpPost = get_page_by_title($page['navigation_name']); + //echo '
' . print_r($wpPost, true) .  '
'; + echo '
' . print_r(get_permalink($wpPost), true) .  '
'; + $replaceUrl = '' . $page['navigation_name'] . ''; + + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%\{{$page['keyword']}\}%' + AND post_type = 'page'"; + //echo '
' . print_r($sql, true) . '
'; + $results = $wpdb->get_results($sql, OBJECT); + $total = count($results); + if ($total > 0) { + foreach ($results as $post) { + //echo '
' . print_r($post, true) . '
'; + $content = $post->post_content; + $content = preg_replace("/\{{$page['keyword']}\}/", $replaceUrl, $content); + wp_update_post(array( + 'ID' => $post->ID, + 'post_content' => $content + )); + } + } + echo '

Count results: ' . $total . '

'; + //echo '
' . print_r($results, true) . '
'; } + } + + private function _getPageTitleById($id) + { + $this->_connect(); + $sql = " + SELECT navigation_name + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} + WHERE id = :id"; + $stmt = $this->_dbh->prepare($sql); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchColumn(); + } + + private function _replaceUrls() + { + global $wpdb; + $this->_connect(); echo '

Replace page url\'s

'; + $searchUrl = $this->_options['site_url']; + $parsedUrl = parse_url($searchUrl); + // find all pages with links to site pages + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%{$searchUrl}%' + AND post_type = 'page'"; + $results = $wpdb->get_results($sql, OBJECT); + if (count($results) > 0) { + $pattern = ';http://' . $parsedUrl['host'] . '/([^"]+);si'; + echo '
' . print_r($pattern, true) . '
'; + //echo '
' . print_r($results, true) . '
'; + foreach ($results as $post) { + $content = $post->post_content; + preg_match_all($pattern, $post->post_content, $matches); + echo '
' . print_r($matches, true) . '
'; + foreach ($matches[0] as $match) { + // find the page id for matched url + if (preg_match(';-([0-9]+)/$;', $match, $found)) { + if ($found) { + echo '
' . print_r($found, true) . '
'; + $toolboxPageName = $this->_getPageTitleById($found[1]); + echo '
' . print_r($toolboxPageName, true) . '
'; + $wpPost = get_page_by_title($toolboxPageName); + $replaceUrl = get_permalink($wpPost->ID); + echo '
' . print_r($replaceUrl, true) . '
'; + $content = str_replace($match, $replaceUrl, $content); + } + } + } + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + wp_update_post($updatePost); + } + } } /** @@ -647,8 +688,13 @@ break; case 1: $this->_import(); + echo '

Part 2

'; break; case 2: + $this->_replaceKeywords(); + echo '

Part 3

'; + break; + case 3: $this->_replaceUrls(); break; } -- 2.17.1