コード例 #1
0
 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();
 }