Updates for NLEA
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Aug 2015 18:35:32 +0000 (14:35 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Aug 2015 18:35:32 +0000 (14:35 -0400)
controllers/Import.php

index 7d6476e..45a7286 100644 (file)
             }
         }
 
+        function handle_accents($content) {
+        // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513
+        if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) {
+            mb_detect_order( "ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15" );
+            if ( empty( $encod ) )
+                $encod = mb_detect_encoding( $content );
+                $headpos = mb_strpos( $content,'<head>' );
+            if ( FALSE === $headpos )
+                $headpos= mb_strpos( $content,'<HEAD>' );
+            if ( FALSE !== $headpos ) {
+                $headpos+=6;
+                $content = mb_substr( $content,0,$headpos ) . '<meta http-equiv="Content-Type" content="text/html; charset='.$encod.'">' .mb_substr( $content,$headpos );
+            }
+            $content = mb_convert_encoding( $content, 'HTML-ENTITIES', $encod );
+        }
+        return $content;
+    }
+
         private function _replaceSrc()
         {
             global $wpdb;
               FROM {$wpdb->prefix}posts
              WHERE post_content LIKE '%<img%'
                AND post_type = 'page'";
-            //$sql .= " AND post_title = 'NLEA Chamber Council'";
+            //$sql .= " AND post_title = 'Home'";
             //$sql .= " AND ID = 2101";
 
             $results = $wpdb->get_results($sql, OBJECT);
             if (count($results) > 0) {
                 $pattern = ';(<img[^>]>);si';
                 foreach ($results as $post) {
-                    $dom = new DOMDocument();
-                    $test = $dom->loadHTML($post->post_content);
+                    $dom = new DOMDocument('1.0', 'UTF-8');
+                    $content = $this->handle_accents($post->post_content);
+                    $dom->encoding = 'UTF-8';
+                    $test = $dom->loadHTML($content);
                     //var_dump($test);
                     $images = $dom->getElementsByTagName('img');
                     foreach ($images as $image) {
                         'ID'           => $post->ID,
                         'post_content' => $content
                     );
-                    //wp_update_post($updatePost);
+                    wp_update_post($updatePost);
                 }
                 //echo '<pre>Files: ' . print_r($files, true) . '</pre>';
                 echo '<pre>Files Found: ' . print_r($filesFound, true) . '</pre>';