Esempio n. 1
0
 /** @noinspection PhpMissingDocCommentInspection */
 public static function ReplaceFont($srcFontId, $dstFontname)
 {
     /* @var $wpdb wpdb */
     global $wpdb;
     $sql = "SELECT wp_post_id FROM {$wpdb->prefix}gf_font_post WHERE gf_fontlist_id = %d";
     $sqlPrepared = $wpdb->prepare($sql, $srcFontId);
     $ids = $wpdb->get_col($sqlPrepared);
     $sql = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}gf_fontlist WHERE name = %s", $dstFontname);
     $dstFontId = $wpdb->get_var($sql);
     if ($dstFontId === null) {
         $dstFontId = GFontsDB::InstallFontUsedIn($dstFontname, 0, 0);
     }
     $srcName = $wpdb->get_var($wpdb->prepare("SELECT name FROM {$wpdb->prefix}gf_fontlist WHERE id = %d", $srcFontId));
     $query = array('posts_per_page' => 0, 'offset' => 0, 'category' => '', 'orderby' => 'post_date', 'order' => 'DESC', '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, 'include' => implode(',', $ids));
     $posts = get_posts($query);
     $postCount = 0;
     $trashCount = 0;
     foreach ($posts as $post) {
         $content = $post->post_content;
         $q = preg_replace('/font-family\\s?:\\s+\'?(' . $srcName . ')\'?([,|;|\'|"])/i', 'font-family: ' . $dstFontname . '$2', $content);
         $npost = array('ID' => $post->ID, 'post_content' => $q);
         wp_update_post($npost);
         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}gf_font_post WHERE wp_post_id = %d AND gf_fontlist_id = %d", $post->ID, $srcFontId));
         GFontsDB::FontPostRelation($post->ID, $dstFontId);
         if ($post->post_status != 'trash') {
             $postCount++;
         } else {
             $trashCount++;
         }
     }
     $sql = "UPDATE {$wpdb->prefix}gf_fontlist SET used_in_posts = 0, total_used = 0, in_trash = 0 WHERE id = %d";
     $sqlPrepared = $wpdb->prepare($sql, $srcFontId);
     $wpdb->query($sqlPrepared);
     $sql = "UPDATE {$wpdb->prefix}gf_fontlist SET used_in_posts = used_in_posts + %d, total_used = total_used + %d WHERE name = %s";
     $sqlPrepared = $wpdb->prepare($sql, $postCount, $postCount, $dstFontname);
     $wpdb->query($sqlPrepared);
     $sql = "UPDATE {$wpdb->prefix}gf_fontlist SET in_trash = in_trash + %d, total_used = total_used + %d WHERE name = %s";
     $sqlPrepared = $wpdb->prepare($sql, $trashCount, $trashCount, $dstFontname);
     $wpdb->query($sqlPrepared);
 }