Finish the url replace and keyword replace
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 15 Jul 2015 16:24:53 +0000 (12:24 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 15 Jul 2015 16:24:53 +0000 (12:24 -0400)
controllers/Import.php

index 4ba6360..1cc15bb 100644 (file)
             echo '<pre>' . print_r($this->_options, true) . '</pre>';
         }
 
-        /**
-         * _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 = '<a href="'.$seoUrl.'">'.$page['navigation_name'].'</a>';
-                            $haystack = str_replace($needle[0][$iter], $anchor, $haystack);
-                        }
-                    }
-                    return $haystack;
-                } else {
-                    return $haystack;
-                }
-
-                if (strstr($haystack, '{') !== false) {
-                    return $this->_findAndReplace($haystack);
-                }
-            }
-
-            return $haystack;
-        }
-
         /**
          * _import
          *
             }
         }
 
-        private function _replaceUrls()
+        private function _replaceKeywords()
         {
+            global $wpdb;
             $this->_connect();
             echo '<p>Replace keywords</p>';
             $sql = "
             $stmt = $this->_dbh->query($sql);
             while ($page = $stmt->fetch()) {
                 echo '<pre>' . print_r($page, true) .  '</pre>';
+                // find out which page (wp) this needs to be
+                $wpPost = get_page_by_title($page['navigation_name']);
+                //echo '<pre>' . print_r($wpPost, true) .  '</pre>';
+                echo '<pre>' . print_r(get_permalink($wpPost), true) .  '</pre>';
+                $replaceUrl = '<a href="'.get_permalink($wpPost->ID).'">' . $page['navigation_name'] . '</a>';
+
+                $sql = "
+                SELECT *
+                  FROM {$wpdb->prefix}posts
+                 WHERE post_content LIKE '%\{{$page['keyword']}\}%'
+                   AND post_type = 'page'";
+                //echo '<pre>' . print_r($sql, true) . '</pre>';
+                $results = $wpdb->get_results($sql, OBJECT);
+                $total = count($results);
+                if ($total > 0) {
+                    foreach ($results as $post) {
+                        //echo '<pre>' . print_r($post, true) . '</pre>';
+                        $content = $post->post_content;
+                        $content = preg_replace("/\{{$page['keyword']}\}/", $replaceUrl, $content);
+                        wp_update_post(array(
+                            'ID'           => $post->ID,
+                            'post_content' => $content
+                        ));
+                    }
+                }
+                echo '<p>Count results: ' . $total . '</p>';
+                //echo '<pre>' . print_r($results, true) . '</pre>';
             }
+        }
+
+        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 '<p>Replace page url\'s</p>';
+            $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 '<pre>' . print_r($pattern, true) . '</pre>';
+                //echo '<pre>' . print_r($results, true) . '</pre>';
+                foreach ($results as $post) {
+                    $content = $post->post_content;
+                    preg_match_all($pattern, $post->post_content, $matches);
+                    echo '<pre>' . print_r($matches, true) . '</pre>';
+                    foreach ($matches[0] as $match) {
+                        // find the page id for matched url
+                        if (preg_match(';-([0-9]+)/$;', $match, $found)) {
+                            if ($found) {
+                                echo '<pre>' . print_r($found, true) . '</pre>';
+                                $toolboxPageName = $this->_getPageTitleById($found[1]);
+                                echo '<pre>' . print_r($toolboxPageName, true) . '</pre>';
+                                $wpPost = get_page_by_title($toolboxPageName);
+                                $replaceUrl = get_permalink($wpPost->ID);
+                                echo '<pre>' . print_r($replaceUrl, true) . '</pre>';
+                                $content = str_replace($match, $replaceUrl, $content);
+                            }
+                        }
+                     }
+                    $updatePost = array(
+                        'ID'           => $post->ID,
+                        'post_content' => $content
+                    );
+                    wp_update_post($updatePost);
+                }
+            }
         }
 
         /**
                 break;
             case 1:
                 $this->_import();
+                echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
                 break;
             case 2:
+                $this->_replaceKeywords();
+                echo '<p><a href="admin.php?import=toolbox&amp;step=3">Part 3</a></p>';
+                break;
+            case 3:
                 $this->_replaceUrls();
                 break;
             }