source/php/App.php
public function replaceMedia($postId)
    {
        if (empty($_POST['media-replacer-replace-with']) || !is_numeric($_POST['media-replacer-replace-with'])) {
            return;
        }

        $uploadDir = wp_upload_dir();

        $filename = $uploadDir['basedir'] . '/' . get_post_meta($postId, '_wp_attached_file', true);
        $file = pathinfo($filename);
        $replacementFile = $uploadDir['basedir'] . '/' . get_post_meta($_POST['media-replacer-replace-with'], '_wp_attached_file', true);

        $backup = $this->createBackup($filename);
        $replaced = $this->replaceFile($filename, $replacementFile);

        if ($replaced) {
            unlink($replacementFile);
        }

        // Update attachment metadata
        $meta = wp_generate_attachment_metadata($postId, $filename);
        wp_update_attachment_metadata($postId, $meta);

        // Update revision meta
        $revisions = get_post_meta($postId, self::$revisionMetaKey, true);
        if (!$revisions) {
            $revisions = array();
        }

        $revisions[time()] = $backup;
        update_post_meta($postId, self::$revisionMetaKey, $revisions);

        // Remove replacement attachment post
        $this->removeAttachment($_POST['media-replacer-replace-with']);
    }
public function replaceMedia($postId)
    {
        if (empty($_POST['media-replacer-replace-with']) || !is_numeric($_POST['media-replacer-replace-with'])) {
            return;
        }

        $uploadDir = wp_upload_dir();

        $filename = $uploadDir['basedir'] . '/' . get_post_meta($postId, '_wp_attached_file', true);
        $file = pathinfo($filename);
        $replacementFile = $uploadDir['basedir'] . '/' . get_post_meta($_POST['media-replacer-replace-with'], '_wp_attached_file', true);

        $backup = $this->createBackup($filename);
        $replaced = $this->replaceFile($filename, $replacementFile);

        if ($replaced) {
            unlink($replacementFile);
        }

        // Update attachment metadata
        $meta = wp_generate_attachment_metadata($postId, $filename);
        wp_update_attachment_metadata($postId, $meta);

        // Update revision meta
        $revisions = get_post_meta($postId, self::$revisionMetaKey, true);
        if (!$revisions) {
            $revisions = array();
        }

        $revisions[time()] = $backup;
        update_post_meta($postId, self::$revisionMetaKey, $revisions);

        // Remove replacement attachment post
        $this->removeAttachment($_POST['media-replacer-replace-with']);
    }
public function replaceMedia ( $ postId ) { if ( empty ( $ _POST [ 'media-replacer-replace-with' ] ) || ! is_numeric ( $ _POST [ 'media-replacer-replace-with' ] ) ) { return ; } $ uploadDir = wp_upload_dir ( ) ; $ filename = $ uploadDir [ 'basedir' ] . '/' . get_post_meta ( $ postId , '_wp_attached_file' , true ) ; $ file = pathinfo ( $ filename ) ; $ replacementFile = $ uploadDir [ 'basedir' ] . '/' . get_post_meta ( $ _POST [ 'media-replacer-replace-with' ] , '_wp_attached_file' , true ) ; $ backup = $ this -> createBackup ( $ filename ) ; $ replaced = $ this -> replaceFile ( $ filename , $ replacementFile ) ; if ( $ replaced ) { unlink ( $ replacementFile ) ; } // Update attachment metadata $ meta = wp_generate_attachment_metadata ( $ postId , $ filename ) ; wp_update_attachment_metadata ( $ postId , $ meta ) ; // Update revision meta $ revisions = get_post_meta ( $ postId , self :: $ revisionMetaKey , true ) ; if ( ! $ revisions ) { $ revisions = array ( ) ; } $ revisions [ time ( ) ] = $ backup ; update_post_meta ( $ postId , self :: $ revisionMetaKey , $ revisions ) ; // Remove replacement attachment post $ this -> removeAttachment ( $ _POST [ 'media-replacer-replace-with' ] ) ; }
Replaces the media file
@return void
Replaces the media file