public static function updateImageName_callback() { global $wpdb; $postID = (int) $_POST['post_id']; $name = trim($_POST['meta_value']); $iname = preg_replace('/(\\s{1,})/', '-', $name); $img_meta = get_post_meta($postID, '_wp_attachment_metadata', TRUE); $linkold = $img_meta['file']; $response = new stdClass(); $response->updated = FALSE; $response->msg = __('There is a problem when update image name', 'wp-meta-seo'); $upload_dirs = wp_upload_dir(); $upload_dir = $upload_dirs['basedir']; $oldpart = ImageHelper::IGetPart($linkold); $old_name = $oldpart->name; if ($name !== "") { if (file_exists($upload_dir . "/" . $linkold)) { $newFileName = $oldpart->base_path . $iname . $oldpart->ext; #if ((!file_exists($upload_dir . "/" . $newFileName)) && $check==0) { if (!file_exists($upload_dir . "/" . $newFileName)) { if (rename($upload_dir . "/" . $linkold, $upload_dir . "/" . $newFileName)) { $post_title = get_the_title($postID); $data_post = array('ID' => $postID, 'post_name' => $name); //if (wp_update_post($data_post)) { $where = array('ID' => $postID); $guid = $upload_dirs['baseurl'] . "/" . $newFileName; if (!$post_title) { $id = $wpdb->update($wpdb->posts, array('guid' => $guid, 'post_title' => $name, 'post_name' => strtolower($iname)), $where); } else { $id = $wpdb->update($wpdb->posts, array('guid' => $guid), $where); } if ($id) { $attached_metadata = get_post_meta($postID, "_wp_attachment_metadata", true); $attached_metadata["file"] = $newFileName; $images_to_rename = array($oldpart->name . $oldpart->ext => $iname . $oldpart->ext); $old_path = $upload_dir . "/" . $linkold; foreach ($attached_metadata['sizes'] as &$clone) { $clone_file_new = ImageHelper::IReplace($iname, $clone['file']); $clone_path = $upload_dir . '/' . $oldpart->base_path . $clone['file']; $clone_path_new = $upload_dir . '/' . $oldpart->base_path . $clone_file_new; if (@rename($clone_path, $clone_path_new)) { $images_to_rename[$clone['file']] = $clone_file_new; $clone['file'] = $clone_file_new; } } /** Update source of this image or its clones in post contains them **/ $query = "SELECT `ID`,`post_title`,`post_content`,`post_type`,`post_date`\n\t\t\t\t\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\t\t\t\t\tWHERE (`post_type` = 'post' or `post_type` = 'page')\n\t\t\t\t\t\t\t\t\tAND `post_content` <> ''\n\t\t\t\t\t\t\t\t\tAND `post_content` LIKE '%<img%>%' \n\t\t\t\t\t\t\t\t\tORDER BY ID"; $posts = $wpdb->get_results($query); $imgs = array($old_name . $oldpart->ext => $postID); $posts_contain_img = array(); foreach ($posts as $post) { $ifound = ImageHelper::IScan($imgs, $post->post_content); if (count($ifound) > 0) { $posts_contain_img[] = $post->ID; } } foreach ($posts_contain_img as $id) { if ($post = get_post($id)) { foreach ($images_to_rename as $src_before => $src_after) { $src_before = '/' . $src_before; $src_after = '/' . $src_after; $post->post_content = str_replace($src_before, $src_after, $post->post_content); } wp_update_post(array('ID' => $post->ID, 'post_content' => $post->post_content)); unset($post, $posts_contain_img); //--------------------------------- } } /*****************************************************/ /** Update Image registered to Attachment sizes on Add media page**/ $sizeOptional = get_post_meta($postID, '_metaseo_sizes_optional', true); $newOptional = array(); if (!empty($sizeOptional) && is_array($sizeOptional)) { foreach ($sizeOptional as $key => $detail) { $pattern = '/^' . strtolower($old_name) . '(-metaseo\\d+)$/'; $key = preg_replace($pattern, strtolower($iname) . '${1}', $key); $detail['url'] = ImageHelper::IReplace($iname, $detail['url']); $newOptional[$key] = $detail; } update_post_meta($postID, '_metaseo_sizes_optional', $newOptional); unset($sizeOptional, $newOptional); } /****************************************************/ //Need to update optimization info of this image ImageHelper::IScanPosts(array($iname . $oldpart->ext => $postID), true); update_post_meta($postID, '_wp_attached_file', $newFileName); update_post_meta($postID, '_wp_attachment_metadata', $attached_metadata); $response->updated = true; $response->msg = __('Image name was changed', 'wp-meta-seo'); } else { $response->iname = $old_name; $response->msg = __('There is a problem when update image name', 'wp-meta-seo'); } } } else { $response->msg = __('Name is existing', 'wp-meta-seo'); $response->iname = $old_name; } } else { $response->iname = $old_name; $response->msg = __('File is not existed', 'wp-meta-seo'); } } else { $response->iname = $old_name; $response->msg = __('Should not be empty', 'wp-meta-seo'); } echo json_encode($response); wp_die(); }
/** * */ public static function IPrepare($imgs, $posts, $meta_checkout = array(), $forceScan = false) { $iNotGood = array(); $iNotGoodTotal = array(); $metaNotGood = array(); $metaNotGoodTotal = array(); $upload_dir = wp_upload_dir(); foreach ($posts as $post) { if (empty($post->post_content)) { continue; } $ifound = self::IScan($imgs, $post->post_content); if (count($ifound) < 1) { continue; } foreach ($ifound as $order => $img) { $iID = $img['id']; //Get image that its size is not good if (!(list($width_origin, $height_origin) = @getimagesize($img['src']))) { continue; } $ratio_origin = $width_origin / $height_origin; $width = $img['width']; $height = $img['height']; //Check if img tag is missing with/height attribute value or not if (!$width && !$height) { $width = $width_origin; $height = $height_origin; } elseif ($width && !$height) { $height = $width * (1 / $ratio_origin); } elseif ($height && !$width) { $width = $height * $ratio_origin; } if ($width_origin > $width || $height_origin > $height) { $img_before = str_replace(array($upload_dir['baseurl']), '', $img['src']); $ibpart = ImageHelper::IGetPart($img_before); $img_after = $ibpart->base_path . (!empty($ibpart->name_prefix) ? $ibpart->name_prefix : $ibpart->name) . '-' . $width . 'x' . $height . $ibpart->name_suffix . $ibpart->ext; $destination = $upload_dir['basedir'] . '/' . $img_after; $srcs = array('img_before_dir' => $upload_dir['basedir'] . $img_before, 'img_before_url' => $upload_dir['baseurl'] . $img_before, 'img_after_dir' => $upload_dir['basedir'] . $img_after, 'img_after_url' => $upload_dir['baseurl'] . $img_after); $size = filesize($srcs['img_before_dir']) / 1024; if ($size > 1024) { $size = $size / 1024; $sizes = 'MB'; } else { $sizes = 'KB'; } $size = @round($size, 1); $iNotGood[$iID][$post->ID]['ID'] = $post->ID; $iNotGood[$iID][$post->ID]['title'] = $post->post_title; $iNotGood[$iID][$post->ID]['post_type'] = $post->post_type; $iNotGood[$iID][$post->ID]['img_before_optm'][$order] = array('size' => $size, 'sizes' => $sizes, 'src' => $srcs['img_before_url'], 'width' => $width, 'height' => $height, 'dimension' => ImageHelper::IGetDimension($img_before)); $iNotGood[$iID][$post->ID]['img_after_optm'][$order] = array('size' => 0, 'path' => $img_after, 'src' => $srcs['img_after_url'], 'src_origin' => $srcs['img_before_url'], 'width' => $width, 'height' => $height, 'dimension' => ImageHelper::IGetDimension($img_after)); //Get the number of images which their size are not good if (!isset($iNotGoodTotal[$iID])) { $iNotGoodTotal[$iID] = 0; } $iNotGoodTotal[$iID]++; } else { if (!isset($iNotGood[$iID])) { $iNotGood[$iID] = array(); } if (!isset($iNotGoodTotal[$iID])) { $iNotGoodTotal[$iID] = 0; } } //Get image that its meta/metas is/are not good if (count($meta_checkout) > 0) { foreach ($meta_checkout as $key => $meta) { $meta_value = $img[$meta]; $imNotGood[$iID][$post->ID]['ID'] = $post->ID; $imNotGood[$iID][$post->ID]['title'] = $post->post_title; $imNotGood[$iID][$post->ID]['post_type'] = $post->post_type; $imNotGood[$iID][$post->ID]['meta'][$order]['img_src'] = $img['src']; $imNotGood[$iID][$post->ID]['meta'][$order]['type'][$meta] = $meta_value; #if($forceScan !== TRUE && $meta_value != ''){ #unset($_posts['meta_not_good'][$img_post_id][$post->ID]['meta'][$order]['type'][$meta]); #} if ($meta_value == '') { if (!isset($imNotGoodTotal[$iID][$meta])) { $imNotGoodTotal[$iID][$meta] = 0; } $imNotGoodTotal[$iID][$meta]++; } } } } } foreach ($imgs as $name => $iID) { if (!isset($iNotGoodTotal[$iID])) { $iNotGoodTotal[$iID] = -1; } if (!isset($iNotGood[$iID])) { $iNotGood[$iID] = array(); } if (!isset($imNotGood[$iID])) { $imNotGood[$iID] = array(); } if (!isset($imNotGoodTotal[$iID])) { foreach ($meta_checkout as $mkey) { $imNotGoodTotal[$iID][$mkey] = 0; } } } foreach ($imNotGoodTotal as &$mStatis) { foreach ($meta_checkout as $mkey) { if (!isset($mStatis[$mkey])) { $mStatis[$mkey] = 0; } } } unset($posts, $imgs); $ret = array('iNotGood' => $iNotGood, 'iNotGoodTotal' => $iNotGoodTotal, 'imNotGood' => $imNotGood, 'imNotGoodTotal' => $imNotGoodTotal); return $ret; }