Skip to content

Hook attach#152

Open
drzraf wants to merge 2 commits into
humanmade:masterfrom
drzraf:hook-attach
Open

Hook attach#152
drzraf wants to merge 2 commits into
humanmade:masterfrom
drzraf:hook-attach

Conversation

@drzraf

@drzraf drzraf commented Apr 3, 2018

Copy link
Copy Markdown

Provide a hook to allow developer omitting file download for whatever reason (like reusing already existing local directory).
Sample implementation of this hook:

<?php


add_filter( 'wxr_importer.process_attachment.uploaded', function( $uploaded, $remote_url, $post, $meta ) {
    $filepath = wp_upload_dir()['basedir'] . '/' . $post['upload_date'] . '/' . basename( $remote_url );
    if ( file_exists( $filepath ) && ($filesize = filesize( $filepath ))) {
        $wp_url = wp_upload_dir()['baseurl'] . '/' . $post['upload_date'] . '/' . basename( $remote_url );
        $id = attachment_url_to_postid( $wp_url );
        if ( $id ) {
            $metadata = wp_get_attachment_metadata( $id );
            $metadata['url'] = $wp_url;
			printf(
				__( 'unmanaged media "%s" already registered (id: %d, %d bytes).', 'wordpress-importer' ) . PHP_EOL,
				$post['post_title'], $id, $filesize
			);
            return $metadata;
        } else {
			printf(
				__( 'unmanaged media "%s" already exists (%d bytes).', 'wordpress-importer' ) . PHP_EOL,
				$post['post_title'], $filesize
			);

            return [ 'file' => $filepath, 'url' => $wp_url ];
        }
    }
    return $uploaded;
}, 10, 4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant