public static function IScanPosts($imgs, $meta_checking = false, $forceScan = false)
 {
     global $wpdb;
     $msg = array();
     $meta_keys = array('alt', 'title');
     $_imgs = array_flip($imgs);
     $post_types = MetaSeo_Content_List_Table::get_post_types();
     $query = "SELECT `ID`, `post_title`, `post_content`, `post_type`, `post_date`\n\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\tWHERE `post_type` IN ({$post_types})\n\t\t\t\t\tAND `post_content` <> ''\n\t\t\t\t\tAND `post_content` LIKE '%<img%>%' \n\t\t\t\t\tORDER BY ID";
     $posts = $wpdb->get_results($query);
     $results = self::IPrepare($imgs, $posts, $meta_keys, $forceScan);
     //Update some value into fields in wp_postmeta
     if (count($results['iNotGood']) > 0) {
         foreach ($results['iNotGood'] as $iID => $post_group) {
             if ($results['iNotGoodTotal'][$iID] > 0) {
                 //This has a litle bit value
                 if ($results['iNotGoodTotal'][$iID] > 1) {
                     $im = ' images ';
                 } else {
                     $im = ' image ';
                 }
                 $msg[$iID]['iNotGood']['msg'] = __($results['iNotGoodTotal'][$iID] . $im . 'with wrong size', 'wp-meta-seo');
                 $msg[$iID]['iNotGood']['warning'] = true;
                 $msg[$iID]['iNotGood']['button'] = '<a href="javascript:void(0);" class=" img-resize button button-primary" data-img-name="' . $_imgs[$iID] . '" data-post-id="' . $iID . '" data-opt-key="resize_image" onclick="showPostsList(this)">' . __('Resize image', 'wp-meta-seo') . '<span class="spinner-light"></span></a>';
                 update_post_meta($iID, '_metaseo_resize_image_counter', $results['iNotGoodTotal'][$iID]);
                 update_post_meta($iID, '_metaseo_resize_image', $post_group);
             } else {
                 $msg[$iID]['iNotGood']['msg'] = __('Image sizes are good!', 'wp-meta-seo');
                 $msg[$iID]['iNotGood']['warning'] = false;
                 delete_post_meta($iID, '_metaseo_resize_image_counter');
                 delete_post_meta($iID, '_metaseo_resize_image');
             }
         }
     }
     if (count($results['imNotGood']) > 0) {
         foreach ($results['imNotGood'] as $iID => $post_group) {
             //This has a litle bit value
             foreach ($meta_keys as $mkey) {
                 $text = '';
                 if ($mkey == 'alt') {
                     $text = ' text';
                 }
                 if ($results['imNotGoodTotal'][$iID][$mkey] > 1) {
                     $msg[$iID]['imNotGood']['msg'][$mkey] = __($results['imNotGoodTotal'][$iID][$mkey] . ' ' . $mkey . $text . 's are missing', 'wp-meta-seo');
                 } elseif ($results['imNotGoodTotal'][$iID][$mkey] == 1) {
                     $msg[$iID]['imNotGood']['msg'][$mkey] = __($results['imNotGoodTotal'][$iID][$mkey] . ' ' . $mkey . $text . ' is missing', 'wp-meta-seo');
                 }
             }
             $msg[$iID]['imNotGood']['warning'] = true;
             $msg[$iID]['imNotGood']['button'] = '<a href="javascript:void(0);" class=" fix-metas button button-primary" data-img-name="' . $_imgs[$iID] . '" data-post-id="' . $iID . '" data-opt-key="fix_metas" onclick="showPostsList(this)">' . __('Fix meta', 'wp-meta-seo') . '<span class="spinner-light"></span></a>';
             update_post_meta($iID, '_metaseo_fix_metas_counter', count($post_group));
             update_post_meta($iID, '_metaseo_fix_metas', $post_group);
             if ($results['imNotGoodTotal'][$iID]['alt'] == 0 && $results['imNotGoodTotal'][$iID]['title'] == 0) {
                 if ($results['iNotGoodTotal'][$iID] != -1) {
                     $msg[$iID]['imNotGood']['button'] = '<a href="javascript:void(0);" class=" fix-metas button" data-img-name="' . $_imgs[$iID] . '" data-post-id="' . $iID . '" data-opt-key="fix_metas" onclick="showPostsList(this)">' . __('Edit meta', 'wp-meta-seo') . '<span class="spinner-light"></span></a>';
                 } else {
                     $msg[$iID]['imNotGood']['button'] = '';
                 }
                 $msg[$iID]['imNotGood']['warning'] = false;
                 $msg[$iID]['imNotGood']['msg'] = '';
             }
         }
     }
     unset($results, $imgs);
     return $msg;
 }
 public static function optimizationChecking()
 {
     global $wpdb;
     $imgs = 0;
     #$imgs_metas = 0;
     $imgs_metas = array('alt' => 0, 'title' => 0);
     $imgs_are_good = 0;
     $imgs_metas_are_good = array();
     $meta_keys = array('alt', 'title');
     $response = array('imgs_statis' => array(0, 0), 'imgs_metas_statis' => array(0, 0), 'meta_title_statis' => array(0, 0), 'meta_desc_statis' => array(0, 0), 'metacontent' => array(0, 0));
     foreach ($meta_keys as $meta_key) {
         $imgs_metas_are_good[$meta_key] = 0;
         $imgs_metas_are_not_good[$meta_key] = 0;
     }
     $post_types = MetaSeo_Content_List_Table::get_post_types();
     $query = "SELECT `ID`, `post_title`, `post_content`, `post_type`, `post_date`\n\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\tWHERE `post_type` IN ({$post_types})\n\t\t\t\t\tAND `post_content` <> ''\n\t\t\t\t\tAND `post_content` LIKE '%<img%>%' \n\t\t\t\t\tORDER BY ID";
     $posts = $wpdb->get_results($query);
     if (count($posts) > 0) {
         $doc = new DOMDocument();
         $upload_dir = wp_upload_dir();
         foreach ($posts as $post) {
             $dom = $doc->loadHTML($post->post_content);
             $tags = $doc->getElementsByTagName('img');
             foreach ($tags as $tag) {
                 $img_src = $tag->getAttribute('src');
                 if (!preg_match('/\\.(jpg|png|gif)$/i', $img_src, $matches)) {
                     continue;
                 }
                 $img_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $img_src);
                 if (!file_exists($img_path)) {
                     continue;
                 }
                 $width = $tag->getAttribute('width');
                 $height = $tag->getAttribute('height');
                 if (list($real_width, $real_height) = @getimagesize($img_path)) {
                     $ratio_origin = $real_width / $real_height;
                     //Check if img tag is missing with/height attribute value or not
                     if (!$width && !$height) {
                         $width = $real_width;
                         $height = $real_height;
                     } elseif ($width && !$height) {
                         $height = $width * (1 / $ratio_origin);
                     } elseif ($height && !$width) {
                         $width = $height * $ratio_origin;
                     }
                     if ($real_width <= $width && $real_height <= $height) {
                         $imgs_are_good++;
                     }
                     foreach ($meta_keys as $meta_key) {
                         if (trim($tag->getAttribute($meta_key))) {
                             $imgs_metas_are_good[$meta_key]++;
                         }
                     }
                 }
                 $imgs++;
             }
         }
         //Report analytic of images optimization
         $response['imgs_statis'][0] = $imgs_are_good;
         $response['imgs_statis'][1] = $imgs;
         $response['imgs_metas_statis'][0] = ceil(($imgs_metas_are_good['alt'] + $imgs_metas_are_good['title']) / 2);
         $response['imgs_metas_statis'][1] = $imgs;
     }
     //Get number of post/page and number of images inserted into them
     $posts_counter = wp_count_posts('post');
     $pages_counter = wp_count_posts('page');
     $posts_pages_total = $posts_counter->publish + $pages_counter->publish;
     $response['meta_title_statis'][1] = $posts_pages_total;
     $response['meta_desc_statis'][1] = $posts_pages_total;
     $query = "SELECT `meta_key`, count( `meta_value` ) as total\n                                      FROM {$wpdb->postmeta}\n                                      WHERE `meta_key` = '_metaseo_metatitle' AND `meta_value` <> ''\n                                      UNION (\n                                      SELECT `meta_key`, count( `meta_value` ) as total\n                                      FROM {$wpdb->postmeta}\n                                      WHERE `meta_key` = '_metaseo_metadesc' AND `meta_value` <> ''\n                                      )";
     $alias_names = array('_metaseo_metatitle' => 'meta_title_statis', '_metaseo_metadesc' => 'meta_desc_statis');
     $results = $wpdb->get_results($query);
     if (count($results) > 0) {
         $count_tt_desc = 0;
         foreach ($results as $result) {
             if ($result->meta_key === NULL) {
                 continue;
             }
             //Report analytic of content meta
             $count_tt_desc += (int) $result->total;
             $response[$alias_names[$result->meta_key]][0] = (int) $result->total;
             $response[$alias_names[$result->meta_key]][1] = $posts_pages_total;
         }
         $response['metacontent'][0] = ceil($count_tt_desc / 2);
         $response['metacontent'][1] = $posts_pages_total;
     }
     echo json_encode($response);
     wp_die();
 }