예제 #1
0
 public static function RecalculateStats()
 {
     /* @var $wpdb wpdb */
     global $wpdb;
     $wpdb->query("UPDATE {$wpdb->prefix}gf_fontlist SET used_in_posts = 0, in_trash = 0, total_used = 0");
     $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}gf_font_post");
     $serializedItems = get_option(GFontsEngine::PLUGIN_OPTION_FONT_DATABASE);
     $gfonts = array();
     $items = unserialize($serializedItems);
     foreach ($items as $itm) {
         $gfonts[] = $itm['name'];
     }
     $finish = false;
     $offset = 0;
     $usedInPosts = array();
     $usedInPostsInTrash = array();
     while (!$finish) {
         $args = array('posts_per_page' => 200, 'offset' => $offset * 200, 'category' => '', 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'trash'), 'suppress_filters' => true);
         $offset++;
         $posts = get_posts($args);
         if (count($posts) != 200) {
             $finish = true;
         }
         $regex = '/font-family:\\s?\'?(.+?)\'?[;|\'|"]/i';
         $stats = array();
         foreach ($posts as $post) {
             $content = $post->post_content;
             if (preg_match_all($regex, $content, $matches)) {
                 update_post_meta($post->ID, GFontsEngine::PLUGIN_META_NO_FONT, 0);
                 $fonts = $matches[1];
                 $usedFonts = array();
                 foreach ($fonts as $font) {
                     $font = str_replace("'", "", $font);
                     $font = str_replace('"', "", $font);
                     $fArray = explode(",", $font);
                     $fname = ucwords(trim($fArray[0]));
                     if (!in_array($fname, $usedFonts)) {
                         $usedFonts[] = $fname;
                         if ($post->post_status != 'trash') {
                             if (isset($usedInPosts[$fname])) {
                                 $usedInPosts[$fname]++;
                             } else {
                                 $usedInPosts[$fname] = 1;
                             }
                         } else {
                             if (isset($usedInPostsInTrash[$fname])) {
                                 $usedInPostsInTrash[$fname]++;
                             } else {
                                 $usedInPostsInTrash[$fname] = 1;
                             }
                         }
                     }
                 }
                 $stats[$post->ID] = $usedFonts;
             } else {
                 update_post_meta($post->ID, GFontsEngine::PLUGIN_META_NO_FONT, 1);
             }
         }
         $allFonts = GFontsDB::GetAllFonts();
         $fontIdByName = array();
         foreach ($allFonts as $font) {
             $fontIdByName[$font->name] = $font->id;
         }
         foreach ($usedInPosts as $name => $value) {
             if (isset($fontIdByName[$name])) {
                 GFontsDB::UpdateFontUsedIn($fontIdByName[$name], $value, in_array($name, $gfonts));
             } else {
                 $id = GFontsDB::InstallFontUsedIn($name, $value, in_array($name, $gfonts));
                 $fontIdByName[$name] = $id;
             }
         }
         foreach ($usedInPostsInTrash as $name => $value) {
             if (isset($fontIdByName[$name])) {
                 GFontsDB::UpdateFontUsedInTrash($fontIdByName[$name], $value, in_array($name, $gfonts));
             } else {
                 GFontsDB::InstallFontUsedInTrash($name, $value, in_array($name, $gfonts));
             }
         }
         $allFonts = GFontsDB::GetAllFonts();
         $fontIdByName = array();
         foreach ($allFonts as $font) {
             $fontIdByName[$font->name] = $font->id;
         }
         foreach ($stats as $idpost => $fonts) {
             foreach ($fonts as $fntName) {
                 $fntId = isset($fontIdByName[$fntName]) ? $fontIdByName[$fntName] : false;
                 if ($fntId !== false) {
                     GFontsDB::FontPostRelation($idpost, $fntId);
                 }
             }
         }
         GFontsDB::CalculateTotalUsed();
     }
 }