public static function _get_post_list($iID, $opt_type)
 {
     global $wpdb;
     //Get image info from wp_postmeta with key equals to _metaseo_img_meta_not_good
     $meta_key = '_metaseo_' . strtolower(trim($opt_type));
     $posts = get_post_meta($iID, $meta_key, true);
     if (is_array($posts) && !empty($posts)) {
         $posts = metaseo_utf8($posts, 'decode');
         return $posts;
     }
     return array();
 }
 public static function updateImgMeta_call_back($_post)
 {
     global $wpdb;
     $response = new stdClass();
     $response->updated = false;
     foreach ($_post as $k => $v) {
         if (!$v && !in_array($k, array('meta_value', 'meta_order'))) {
             $response->msg = __('There is a problem when update image meta!', 'wp-meta-seo');
             echo json_encode($response);
             wp_die();
         }
     }
     $meta_key = strtolower(trim($_post['meta_key']));
     $meta_type = strtolower(trim($_post['meta_type']));
     $meta_value = htmlspecialchars(trim($_post['meta_value']));
     $meta_order = intval($_post['meta_order']);
     $img_post_id = intval($_post['img_post_id']);
     $post_id = intval($_post['post_id']);
     $meta = get_post_meta($img_post_id, $meta_key, true);
     //Update new value for meta info of this image in wp_postmeta
     $meta[$post_id]['meta'][$meta_order]['type'][$meta_type] = metaseo_utf8($meta_value);
     update_post_meta($img_post_id, $meta_key, $meta);
     $meta = get_post_meta($img_post_id, $meta_key, true);
     //Then we must update this meta info in the appropriate post content
     if (!($post = get_post($post_id))) {
         $response->msg = __('The post has been deleted before, please check again!', 'wp-meta-seo');
     } else {
         if ($post->post_content !== '') {
             //Split content part that do not contain img tag
             $post_content_split = preg_split('/<img [^<>]+ \\/>/i', $post->post_content);
             //Get all img tag from the content
             preg_match_all('/<img [^<>]+ \\/>/i', $post->post_content, $matches);
             $img_tags = $matches[0];
             if (isset($img_tags[$meta_order])) {
                 //&& strpos($img_tags[$meta_order], $img_src)){
                 $pattern = '/' . $meta_type . '\\s*?\\=?\\"[^\\"]*\\"/i';
                 $replacement = $meta_type . '="' . $meta_value . '"';
                 if (!preg_match($pattern, $img_tags[$meta_order], $match)) {
                     $pattern = '/\\/>/i';
                     $replacement = $meta_type . '="' . $meta_value . '" />';
                 }
                 $img_tags[$meta_order] = preg_replace($pattern, $replacement, $img_tags[$meta_order]);
                 $post_content = '';
                 foreach ($post_content_split as $key => $split) {
                     if (isset($img_tags[$key])) {
                         $img_tag = $img_tags[$key];
                     } else {
                         $img_tag = '';
                     }
                     $post_content .= $split . $img_tag;
                 }
                 //Update content of this post.
                 if (!wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content))) {
                     $response->msg = __('The post haven\'t been updated, please check again!', 'wp-meta-seo');
                 } else {
                     $response->updated = true;
                     $response->msg = __(ucfirst($meta_type) . ' was saved', 'wp-meta-seo');
                 }
             } else {
                 $response->msg = __('This image has been removed from the post, please check again!', 'wp-meta-seo');
             }
         } else {
             $response->msg = __('Content of the post is empty, please check again', 'wp-meta-seo!');
         }
     }
     echo json_encode($response);
     wp_die();
 }
/**
 * Encode or decode all values in string format of an array
 */
function metaseo_utf8($obj, $action = 'encode')
{
    $action = strtolower(trim($action));
    $fn = "utf8_{$action}";
    if (is_array($obj)) {
        foreach ($obj as &$el) {
            if (is_array($el)) {
                if (is_callable($fn)) {
                    $el = metaseo_utf8($el, $action);
                }
            } elseif (is_string($el)) {
                //var_dump(mb_detect_encoding($el));
                $isASCII = mb_detect_encoding($el, 'ASCII');
                if ($action === 'encode' && !$isASCII) {
                    $el = mb_convert_encoding($el, "UTF-8", "auto");
                }
                $el = $fn($el);
            }
        }
    } elseif (is_object($obj)) {
        $vars = array_keys(get_object_vars($obj));
        foreach ($vars as $var) {
            metaseo_utf8($obj->{$var}, $action);
        }
    }
    return $obj;
}