Giter Site home page Giter Site logo

wp-cli / media-command Goto Github PK

View Code? Open in Web Editor NEW
43.0 11.0 38.0 8.58 MB

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.

License: MIT License

Gherkin 64.86% PHP 34.27% Shell 0.87%
wp-cli wp-cli-package wordpress cli media thumbnail regeneration upload hacktoberfest

media-command's Introduction

wp-cli/media-command

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.

Testing

Quick links: Using | Installing | Contributing | Support

Using

This package implements the following commands:

wp media

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.

wp media

EXAMPLES

# Re-generate all thumbnails, without confirmation.
$ wp media regenerate --yes
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Sydney Harbor Bridge" (ID 760).
2/3 Regenerated thumbnails for "Boardwalk" (ID 757).
3/3 Regenerated thumbnails for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

# Import a local image and set it to be the featured image for a post.
$ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image
Success: Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.

# List all registered image sizes
$ wp media image-size
+---------------------------+-------+--------+-------+
| name                      | width | height | crop  |
+---------------------------+-------+--------+-------+
| full                      |       |        | N/A   |
| twentyfourteen-full-width | 1038  | 576    | hard  |
| large                     | 1024  | 1024   | soft  |
| medium_large              | 768   | 0      | soft  |
| medium                    | 300   | 300    | soft  |
| thumbnail                 | 150   | 150    | hard  |
+---------------------------+-------+--------+-------+

wp media import

Creates attachments from local files or URLs.

wp media import <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>] [--skip-copy] [--preserve-filetime] [--featured_image] [--porcelain]

OPTIONS

<file>...
	Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
	    If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
	    downloaded to a temp file before being sideloaded.

[--post_id=<post_id>]
	ID of the post to attach the imported files to.

[--title=<title>]
	Attachment title (post title field).

[--caption=<caption>]
	Caption for attachment (post excerpt field).

[--alt=<alt_text>]
	Alt text for image (saved as post meta).

[--desc=<description>]
	"Description" field (post content) of attachment post.

[--skip-copy]
	If set, media files (local only) are imported to the library but not moved on disk.
	File names will not be run through wp_unique_filename() with this set.

[--preserve-filetime]
	Use the file modified time as the post published & modified dates.
	Remote files will always use the current time.

[--featured_image]
	If set, set the imported image as the Featured Image of the post its attached to.

[--porcelain]
	Output just the new attachment ID.

EXAMPLES

# Import all jpgs in the current user's "Pictures" directory, not attached to any post.
$ wp media import ~/Pictures/**\/*.jpg
Imported file '/home/person/Pictures/landscape-photo.jpg' as attachment ID 1751.
Imported file '/home/person/Pictures/fashion-icon.jpg' as attachment ID 1752.
Success: Imported 2 of 2 items.

# Import a local image and set it to be the post thumbnail for a post.
$ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image
Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
Success: Imported 1 of 1 images.

# Import a local image, but set it as the featured image for all posts.
# 1. Import the image and get its attachment ID.
# 2. Assign the attachment ID as the featured image for all posts.
$ ATTACHMENT_ID="$(wp media import ~/Downloads/image.png --porcelain)"
$ wp post list --post_type=post --format=ids | xargs -d ' ' -I % wp post meta add % _thumbnail_id $ATTACHMENT_ID
Success: Added custom field.
Success: Added custom field.

# Import an image from the web.
$ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing"
Imported file 'http://s.wordpress.org/style/images/wp-header-logo.png' as attachment ID 1755.
Success: Imported 1 of 1 images.

# Get the URL for an attachment after import.
$ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --porcelain | xargs -I {} wp post list --post__in={} --field=url --post_type=attachment
http://wordpress-develop.dev/wp-header-logo/

wp media regenerate

Regenerates thumbnails for one or more attachments.

wp media regenerate [<attachment-id>...] [--image_size=<image_size>] [--skip-delete] [--only-missing] [--yes]

OPTIONS

[<attachment-id>...]
	One or more IDs of the attachments to regenerate.

[--image_size=<image_size>]
	Name of the image size to regenerate. Only thumbnails of this image size will be regenerated, thumbnails of other image sizes will not.

[--skip-delete]
	Skip deletion of the original thumbnails. If your thumbnails are linked from sources outside your control, it's likely best to leave them around. Defaults to false.

[--only-missing]
	Only generate thumbnails for images missing image sizes.

[--yes]
	Answer yes to the confirmation message. Confirmation only shows when no IDs passed as arguments.

EXAMPLES

# Regenerate thumbnails for given attachment IDs.
$ wp media regenerate 123 124 125
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Vertical Image" (ID 123).
2/3 Regenerated thumbnails for "Horizontal Image" (ID 124).
3/3 Regenerated thumbnails for "Beautiful Picture" (ID 125).
Success: Regenerated 3 of 3 images.

# Regenerate all thumbnails, without confirmation.
$ wp media regenerate --yes
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Sydney Harbor Bridge" (ID 760).
2/3 Regenerated thumbnails for "Boardwalk" (ID 757).
3/3 Regenerated thumbnails for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

# Re-generate all thumbnails that have IDs between 1000 and 2000.
$ seq 1000 2000 | xargs wp media regenerate
Found 4 images to regenerate.
1/4 Regenerated thumbnails for "Vertical Featured Image" (ID 1027).
2/4 Regenerated thumbnails for "Horizontal Featured Image" (ID 1022).
3/4 Regenerated thumbnails for "Unicorn Wallpaper" (ID 1045).
4/4 Regenerated thumbnails for "I Am Worth Loving Wallpaper" (ID 1023).
Success: Regenerated 4 of 4 images.

# Re-generate only the thumbnails of "large" image size for all images.
$ wp media regenerate --image_size=large
Do you really want to regenerate the "large" image size for all images? [y/n] y
Found 3 images to regenerate.
1/3 Regenerated "large" thumbnail for "Sydney Harbor Bridge" (ID 760).
2/3 No "large" thumbnail regeneration needed for "Boardwalk" (ID 757).
3/3 Regenerated "large" thumbnail for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

wp media image-size

Lists image sizes registered with WordPress.

wp media image-size [--fields=<fields>] [--format=<format>]

OPTIONS

[--fields=<fields>]
	Limit the output to specific fields. Defaults to all fields.

[--format=<format>]
	Render output in a specific format
	---
	default: table
	options:
	  - table
	  - json
	  - csv
	  - yaml
	  - count
	---

AVAILABLE FIELDS

These fields will be displayed by default for each image size:

  • name
  • width
  • height
  • crop
  • ratio

EXAMPLES

# List all registered image sizes
$ wp media image-size
+---------------------------+-------+--------+-------+-------+
| name                      | width | height | crop  | ratio |
+---------------------------+-------+--------+-------+-------+
| full                      |       |        | N/A   | N/A   |
| twentyfourteen-full-width | 1038  | 576    | hard  | 173:96|
| large                     | 1024  | 1024   | soft  | N/A   |
| medium_large              | 768   | 0      | soft  | N/A   |
| medium                    | 300   | 300    | soft  | N/A   |
| thumbnail                 | 150   | 150    | hard  | 1:1   |
+---------------------------+-------+--------+-------+-------+

Installing

This package is included with WP-CLI itself, no additional installation necessary.

To install the latest version of this package over what's included in WP-CLI, run:

wp package install [email protected]:wp-cli/media-command.git

Contributing

We appreciate you taking the initiative to contribute to this project.

Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

For a more thorough introduction, check out WP-CLI's guide to contributing. This package follows those policy and guidelines.

Reporting a bug

Think you’ve found a bug? We’d love for you to help us get it fixed.

Before you create a new issue, you should search existing issues to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.

Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please create a new issue. Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, review our bug report documentation.

Creating a pull request

Want to contribute a new feature? Please first open a new issue to discuss whether the feature is a good fit for the project.

Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience. See "Setting up" for details specific to working on this package locally.

Support

GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support

This README.md is generated dynamically from the project's codebase using wp scaffold package-readme (doc). To suggest changes, please submit a pull request against the corresponding part of the codebase.

media-command's People

Contributors

balbuf avatar clemens-tolboom avatar danielbachhuber avatar desrosj avatar diggy avatar ernilambar avatar francescolaffi avatar gitlost avatar goldenapples avatar jmslbam avatar johnbillion avatar lkwdwrd avatar miya0001 avatar morganestes avatar mpeshev avatar mwilliamson avatar natewr avatar ntwb avatar nyordanov avatar ramoonus avatar rodrigoprimo avatar runofthemill avatar schlessera avatar scribu avatar swissspidy avatar szepeviktor avatar terriann avatar thrijith avatar wesm87 avatar wojsmol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

media-command's Issues

Adopt and enforce new `WP_CLI_CS` standard

We have a new PHPCS standard for WP-CLI called WPCliCS (props @jrfnl). It is part of the wp-cli/wp-cli-tests package starting with version v2.1.0.

To adopt & enforce this new standard, the following actions need to be taken for this repository:

  • Create a PR that adds a custom ruleset phpcs.xml.dist to the repository

    • Add phpcs.xml.dist file
    • Adapt .distignore to ignore phpcs.xml.dist & phpunit.xml.dist
    • Adapt .gitignore to ignore phpunit.xml, phpcs.xml & .phpcs.xml
    • Require version ^2.1 of the wp-cli/wp-cli-tests as a dev dependency
  • Make any required changes to the code that fail the checks from the above ruleset in separate PRs

  • Merge thre ruleset once all required changes have been processed and merged

A sample PR for a simple repository can be seen here: https://github.com/wp-cli/maintenance-mode-command/pull/3/files

Related wp-cli/wp-cli#5179

Allow import without moving files on disk

A bit backwards - PR submitted earlier: #21

Currently media import always moves files to the media library folder. This impliments a flag to allow importing to the library without moving on disk. Seems like a common use to me - either in custom workflows when storing some media outside the main directory for some reason (my use case), or in coordination with something like the prospective diff command #6 to import files not in the library but already in the uploads dir.

Feature request: Option to generate by file-type

It would be epic if you could only generate media for specific file-types. For example, I've got a client who's site contains a large number of PDFs. I only need to regenerate files with the filetypes "image/jpeg" and "image/png". It would make use in deployments much faster if filetypes not needing thumbnail generation were never checked.

ERROR: No "medium_large" thumbnail regeneration needed for...

When I run wp media regenerate 10927 --image_size=medium_large I receive this:

Found 1 image to regenerate.
1/1 No "medium_large" thumbnail regeneration needed for "Bay" (ID 10927).

However, I DO need that image regenerated. Why is it saying I don't, and how do I override?

Lack of type test results into PHP warnings

Current code uses wp_get_attachment_metadata function from Wordpress to retrieve attachment's metadata (1, 2, 3). It silently assumes that returned value is array and makes no checks for it, but function description in Wordpress sources clearly states that returned value is mixed.

In practice for SVG images returned metadata is empty string and it results into 6 warning messages per image.

It is necessary to add checks for returned information and, maybe, exclude SVG images from regeneration process.

regenerate command runs on non-image files

When running this:

$ wp media regenerate --yes

The command runs on ALL attachments, regardless of file type.

This results in lots of awkward errors when run on audio, video, PDFs, etc. Here's an example:

...
Warning: 23/23 Couldn't regenerate thumbnails for "2012 Guest gallery proposal guidelines" (ID 20).
Error: Only regenerated 16 of 23 images.

In the above example, the media library contains 16 images and 7 non-image files.

It would seem wp regenerate media is naturally intended only for images and so it would make sense to only run it on a whitelist of filetypes. This would also avoid issues like this one.

Automatically rerun failed scenarios

The following changes need to be made:

  1. In the .travis.yml file, the - composer behat line in the script: section needs to be changed into the following:
- composer behat || composer behat-rerun
  1. In the composer-json file, the requirement on wp-cli/wp-cli-tests needs to be adapted to require at least v2.0.7:
"wp-cli/wp-cli-tests": "^2.0.7"
  1. In the composer-json file, the "scripts" section needs to be extended. Immediately after the line "behat": "run-behat-tests",, the following line needs to be inserted:
"behat-rerun": "rerun-behat-tests",

Here's an example of how this should look like:

Adding media diff to media command

Following the contrib rules by opening a ticket before issuing a a PR ;)

We've put together a few functions here: https://github.com/DigitalTactics/wp-cli-mediadiff for listing what are likely to be orphan files (files on the file system in the uploads folder which don't (for whatever reason) have corresponding attachment entries - yes we know some plugins manage files here too which don't result in attachments hence the likely); we then have options to move these files to a trash folder for inspection/retention and/or then delete them permanently if appropriate.

We think it makes sense to have our diff command trail off of media command so wp media diff makes sense to us... but what are your thoughts? Is this something you want to include

We may have some more work to on this generally but what's currently there should be of some use already.

Skipped thumbnail regeneration

Hello,

I use wp media regenerate want to re-thumbnail my featured image.

But it always skipped thumbnail regeneration.

Command-line:

$ wp media regenerate 330575
Found 1 image to regenerate.
1/1 Skipped thumbnail regeneration for "image_201812211157F8" (ID 330575).
Success: Regenerated 0 of 1 images (1 skipped)

I use each ID will be skipped, and why?

How to fix this problem.

Thanks,
mike

Update Examples in Documentation

Some of the examples in the documentation for the wp media command could be improved by removing cultural references and a couple unnecessary gendered examples. This speaks to some of the changes we’ve seen in WordPress this year, as well as being a consideration for future translators and creating a sense of inclusivity with use of intentional language, even in our documentation.

I’d like to propose reusing other examples in the documentation where possible. I'll submit a pull request to get some feedback from you all.

Import media from local machin to remote server

I try to post some media file from my local pc to my server.
I configured wp-cli to use ssh to connect.
I tried to import file but I got error about the file does not exist. So It tries to find it on the server machine I guess.

wp media import "/local/path/to/image.jpg" --post_id=1216 --title="Some title" --featured_image --porcelain

Then, I change a bit my command to add -http but it does not change anything.

wp media import "/local/path/to/image.jpg" --post_id=1216 --title="Some title" --featured_image --porcelain --http=http://blog.domain.org/

I keep getting "File does not exit error"

display /local/path/to/image.jpg

Show the picture.

Is there a way to send off file to remote server ?

(my pc and my server are both in my house, I host my blog).

QUESTION: Expanding import --skip-copy possible?

In specific cases when the files are inside the wp-content folder but not in uploads, is it possible to set their URL according to their real position within wp-content instead to statically set the URL to wp-content/uploads?

My exact issue is, that i have PDF-s for WP Download Manager Pro in wp-content/uploads/download-manager-files but to allow them to be indexed by SearchWP i need to import them into Medialibrary, which in turn needs to either copy them, or by using skip-copy i have to relocate the WPDMPro base directory to wp-content/uploads what is not desired.

Move command over to new v2 structure

The following changes need to be made to move the command over to the v2 structure:

  • Make sure the correct framework is required:
    composer require wp-cli/wp-cli:^2
    
  • Require the testing framework as a dev dependency:
    composer require --dev wp-cli/wp-cli-tests:^0
    
  • Use the .travis.yml file from wp-cli/wp-cli:
    wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/.travis.yml
    
  • Add the default script configuration to Composer file:
      "scripts": {
          "lint": "run-linter-tests",
          "phpcs": "run-phpcs-tests",
          "phpunit": "run-php-unit-tests",
          "behat": "run-behat-tests",
          "prepare-tests": "install-package-tests",
          "test": [
              "@lint",
              "@phpcs",
              "@phpunit",
              "@behat"
          ]
      },
    
  • Remove scaffolded binary files:
    git rm bin/install-package-tests.sh
    git rm bin/test.sh
    
  • Remove scaffolded Behat setup:
    git rm features/bootstrap/*
    git rm features/extra/*
    git rm features/steps/*
    
  • Remove scaffolded Behat tags util script:
    git rm utils/behat-tags.php
    
  • Add command packages that are needed for Behat tests as --dev dependencies.
    The following commands are already available, anything else needs to be explicitly required:
    • cli *
    • config *
    • core *
    • eval
    • eval-file
    • help
  • Update all dependencies:
    composer update
    
  • Optional - Add PHPCS rule set to enable CS & compatibility sniffing:
    wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/phpcs.xml.dist
    
  • Run and adapt tests to make sure they all pass:
    composer test
    

Media regenerating is slow

Should media regenerating be so slow? I was only able to do about 800 (out of 13000) images last night. The process is only using 1-3% CPU. Does anyone have any tips to speed this up?

wp media regenerate behavior is different than plugin Regenerate Thumbnails

Or maybe it's the other way around, in which case please mark this appropriately.

Anyway, when I do single (or batch) wp-cli media regenerate like so

debian@8cylinder:/var/www/html/wp$ wp media regenerate 12530

it borks my featured image thumbnails on my index page (excerpts) back to the square-ish WP default.

But when I use the WP Gui plugin Regenerate Thumbnails, Version 3.0.2 | By Alex Mills (Viper007Bond), it performs what I consider expected behavior... making the index excerpt thumbs my preferred 720x320 aspect ratio

<?php the_post_thumbnail( 'wide-n-short-featured-720' ); ?>

as I've described in wp-config.php

add_image_size( 'wide-n-short-featured-720', 720, 320, true );

Is it me?

problems regenerating thumbnails in the other languages

Hi, We just updated and have problems regenerating thumbnails in the other languages.

Command: wp media regenerate --only-missing --yes

Old version :
WP-CLI: 1.5.0
WORDPRESS: WordPress 4.9.5
WPML: Version: 3.9.4

New version :
WP-CLI: 1.5.0
WORDPRESS: WordPress 4.9.8
WPML: Version: 4.0.5

Now, the CLI Command only regenerate thumbnails in the MAIN language.

Any clue? Thanks!

wp-cli phar doesn't contain the latest media command

Currently the 1.1.0 phar (released Feb 1) doesn't contain the command with the --image_size parameter available.

Even though the docs contain the command the cli reports the parameter as unknown.
Since the media command was last updated on April 19, is it possible that the versions currently bundled are not the lastest?

wp package list provides no info into this as it is not considered a package.
wp package install|update wp-cli/media-command doesn't do anything, probably because it is considered to be core?

Also the docs are out of sync:

Add reason for skipping regeneration

We get a lot of bug reports and support reports related to the thumbnail regeneration, where it often is just not clear why the regeneration is being skipped.

The command should be more verbose and add the specific reason for why it decided to skip a given thumbnail regeneration.

Also, it should come with additional --debug output in case one wants to verify said reasons.

Regenerate Success but no update in DB

Hi, I have problem with Regenrate thumbnails. I have message:
Found 1 image to regenerate.
1/1 Regenerated thumbnails for "miss-finallll-jpg" (ID 1316609).
Success: Regenerated 1 of 1 images

But when I check db for image sizes there are no sizes set in db, so request for image size gives me back all the time only the default uploaded size. When I try to regenerate in wp admin, then is it working well. I have 64k images, so I am not able to do it in wp-admin. If I check files, the sizes are there generated correct (miss-finallll-768x404.jpg)

Perhaps update process_regeneration() to reflect changes in WP 5.3

Just an FYI that from WP 5.3 there are couple of functions to find and generate missing image sub-sizes, and the image meta is saved after each sub-size is generated.

For back-compat wp_generate_attachment_metadata() still returns the whole image meta array after all sub-sizes have been created (if the server doesn't run out of resources), but is not the best way to generate only missing image sub-sizes. See https://core.trac.wordpress.org/ticket/40439 and https://core.trac.wordpress.org/changeset/45538 (testing and suggestions welcome).

Pull from remote

Hi!
Great plugin!

How about being able to pull all media files from a production site? That would be awesome.

Regenerate a specific image size for one or more images

On an existing production site, it can be a little bit scary to regenerate all image sizes for all images because of the fear of the unknown.

If I've only changed the width / height values for one image size, it would be nice to be able to only regenerate that specific image size.

For instance, if I've changed thumbnail from 183x183 to 185x185, I could run wp media regenerate --image_size=thumbnail to solely regenerate thumbnails.

Preserve file time when importing files

When calling wp media import, it would be useful if an option existed to use the file time instead of the current time when importing files on the current server.

Current use case: I have a non-WordPress site that I am sun setting. I am creating a WordPress site to serve as an archive of all files from the old site. Preserving the file time is important so that admins can search for files by date.

Change visibility of private methods

Right now the method process_regeneration() and all other internal methods are declared private. This makes it impossible make a small change to the inner workings of a command.

For example, I want to reduce the log output of the regenerate command by changing some WP_CLI::log() calls in the process_regeneration() method, but this is impossible without overriding the whole regenerate() method (which doesn't even have visibility).

I would like to propose to:

  1. Add visibility to all methods which have no visibility right now
  2. Change the visibility of internal methods to protected

media regenerate removes/doesn't recreate custom site-icon sizes

Summary

When regenerating all media, the special-case site-icon loses its custom sizes

Steps to Reproduce

  1. Through Customizer -> Site Identity, add a Site Icon
  2. Observe that in addition to the originally uploaded image present in the media gallery, a 2nd "cropped" variant is created with unique image sizes
  3. Run wp media regenerate --yes
  4. Observe that the cropped variant's sizes are replaced with the standard active sizes

Environment

  1. PHP 7.1.16 (cli) (built: Mar 31 2018 02:28:54) ( NTS )
  2. $wp_version = '4.9.6';
  3. WP-CLI 1.5.1
  4. twentyseventeen theme, no plugins

Severity - Low

Expected Results: The special case 'site-icon' should be handled as it is in core to generate the correct media sizes

Actual Results: The site-icon image attachment is handled the same as all other uploads, and loses its special sizes, which may cause issues with devices/services looking for/expecting to find images of a particular size. The meta tags output in the site header do link to the newly generated image sizes, but attributes on the meta tags still show the original sizes that were expected to exist.

Core is only expecting this image to ever be set via the customizer, so the case for it is handled in ajax-actions.php which calls on the WP_Site_Icon class from class-wp-site-icon.php.

File and metadata before/after regeneration: https://gist.github.com/dotsam/f31dfed2ee1b64b149883520ee901b43

skip existing and delete unregistred in WP cli ?

Hi is there a way to set up below commands with WP cli as well or is it set by default?
I couldn't find it anywhere.

-Skip regenerating existing correctly sized thumbnails (faster).
-Delete thumbnail files for old unregistered sizes in order to free up server space. This may result in broken images in your posts and pages.

Many thanks

Fix Travis CI caching

Via @gitlost in #7:

"(Just on the current ".travis.yml" I think the cache syntax https://github.com/wp-cli/media-command/blob/master/.travis.yml#L16 for caching "$HOME/.composer/.cache" needs to be in a "directories" list like https://github.com/gitlost/media-command/blob/imagick/.travis.yml#L16)."

@gitlost I don't think this is correct for either repo:
https://travis-ci.org/wp-cli/media-command/jobs/222084435
https://travis-ci.org/gitlost/media-command/jobs/222084435

Neither of the above jobs have Setting up build cache or store build cache sections

You can see both these sections in:
https://travis-ci.org/paulgibbs/behat-wordpress-extension/jobs/224696788

Do each of the respective repos have Travis CI cache info here to view or do you see an error?
https://travis-ci.org/wp-cli/media-command/caches
https://travis-ci.org/gitlost/media-command/caches

Allow import of files from 'unsafe' URLs

A bit backwards - PR submitted earlier: #22

Currently media import uses wp_safe_remote_get which importantly denies import via local domain/IPs. This impliments a flag to allow use of wp_remote_get so that you can import from those locations. Writtent to cope with managing some private domain/LAN sites.

Feature request: media replace subcommand

Hi,

I would like to suggest a new feature (subcommand) that would allow to seamlessly replace media file of existing attachment: media replace <file> <attachment_id>.

For the time being, I use Enable Media Replace whenever I need to replace media file, but it would be great to have a command line equivalent.

Import subcommand wording is specific to images

wp media import https://archive.org/download/testmp3testfile/mpthreetest.mp3
Imported file 'https://archive.org/download/testmp3testfile/mpthreetest.mp3' as attachment ID 37.
Success: Imported 1 of 1 images.

I would expect this wording to be more generic to something like "items" as it is possible that the site does not even allow image mime types to be uploaded.

Find a way to install PHP imagick which doesn't seg fault in PHP 7.0 and 7.1.

See #5 (comment).

In order to run media tests involving PDF previews, the PHP extension imagick needs to be installed.

Unfortunately using pecl install of the latest 3.4.3 version under Ubuntu Precise 12.02 can cause it to randomly seg fault when built against PHP 7.0 (or PHP 7.1).

One would hope that there might be some way or combination of systems/versions etc to make it work.

Hook into this command

Hi, i'm interested in doing some sanitation on filenames when doing wp media import, preferably using a hook. Is that possible?

Add force option to media regenerate.

In media regenerate a --force option to always regenerate would be useful eg when you've changed your image editor and/or settings or added a plugin enhancement etc.

Easy to implement, just by-pass Media_Command::needs_regeneration().

wp media import filename sanitization does not address spaces

Summary
We recently bulk-imported ~4,000 media library items using wp media import. A while into development after the import, we noticed a problem with the links to these files when inserted into content as they were not using the Amazon S3 URL but the local URL. We're using the pro version of Delicious Brains' Offload WP plugin for that piece. WP sanitizes filenames by replacing spaces with hyphens, among other transformations. Offload WP relies on this functionality and assumes that neither spaces nor + signs will be present in a media library item URL. Here is how the plugin validates a URL: preg_match( '@^(?:https?:)?//[a-zA-Z0-9\-]+@', $string ).

If the same file is uploaded manually via the Media Library UI, the filename is properly sanitized with the space being replaced with a hyphen.

Steps to repeat

  1. Import a file containing a space in its filename with:
    wp media import "/path/to/file/file name.docx" --skip-copy --porcelain
  2. Insert link to file into a post using the Add Media button on post edit form
  3. Save post and view. Check URL to file and see a space, or %20, in filename.

Environment
WP is running inside a docker-compose setup using the official wordpress image. WP-CLI is installed inside the wordpress container instead of using the wordpress-cli variant of that image. It's running PHP 7.2.18.

Output from wp cli info:

OS:	Linux 4.15.0-50-generic #54~16.04.1-Ubuntu SMP Wed May 8 15:55:19 UTC 2019 x86_64
Shell:	
PHP binary:	/usr/local/bin/php
PHP version:	7.2.18
php.ini used:	
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/var/www/html
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.2.0

Severity — High
If we don't find a way to adjust several hundred URLs in the DB in a way that updates them on S3, we may have to redo hours of additional work that came after this import but before we discovered the issue. wp media import should utilize the same sanitization methods as importing via the UI.

Expected Behavior
Upon import, filenames with spaces in them will have the spaces replaced with a hyphen.

Actual Behavior
Upon import, filename spaces are left untouched.

Additional Info
This class was used to trigger the wp media import after using the RecursiveIteratorIterator and RecursiveDirectoryIterator PHP classes to recursively loop over the directories and files to import: https://gist.github.com/livewyer/4019d0b2a10a5e38b1cade765ee1fad3

I also have been talking with Offload WP support. Here's what they said about how their plugin interacts with filenames:

In most cases WP handles the sanitizing and renaming of the file during upload to the Media Library, WP Offload Media then just offloads that file. The primary way WP handles the filename clean up is through \wp_unique_filename (from wp-includes/functions.php).

When a file is uploaded to the Media Library and "Remove Files From Server" is turned on in WP Offload Media, then WP Offload Media hooks into wp_unique_filename and does a quick check to make sure that a previously offloaded and removed file doesn't have a local file path that would clash with the new file if it were to be downloaded. If there's a clash then WP Offload Media uses the same technique as WP to append a correct "version" numeric (such as "-1") to the file's base name. But WP Offload Media doesn't change the filename otherwise, WP has already sanitized and substituted hyphens for spaces etc.

wp media import fatal error

I have newest Wordpress (4.8.1) and Cherry Framework (3.1.5) based theme. When I run import media command:

wp media import wp-content/uploads/2017/08/**\/*.jpg

I get this error:

Fatal error: require_once(): Failed opening required '//includes/getLanguageDict.php' (include_path='phar:///home/totumfacky/bin/wp/vendor/phpunit/php-token-stream:phar:///home/totumfacky/bin/wp/vendor/phpunit/phpunit-mock-objects:phar:///home/totumfacky/bin/wp/vendor/phpunit/php-code-coverage:phar:///home/totumfacky/bin/wp/vendor/phpunit/phpunit:phar:///home/totumfacky/bin/wp/vendor/symfony/yaml:.:/usr/local/share/pear56') in /home/totumfacky/webapps/wp_koga/wp-content/plugins/motopress-content-editor/includes/ce/templates/landing.php on line 3

And this garbage on CLI output:

<div class="alert"><span class="object">id: mp_row, name: Row, icon: , closeType: enclosed, resize: none, position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_row_inner, name: Inner Row, icon: , closeType: enclosed, resize: none, position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_span, name: Column, icon: , closeType: enclosed, resize: none, position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_span_inner, name: Inner Column, icon: , closeType: enclosed, resize: none, position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_text, name: Paragraph, icon: , closeType: enclosed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/text.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_heading, name: Title, icon: , closeType: enclosed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/heading.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_code, name: WordPress Text, icon: , closeType: enclosed, resize: horizontal, position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_quote, name: Quote, icon: , closeType: enclosed, resize: horizontal, position: 40, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/quotes.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_members_content, name: Members Content, icon: , closeType: enclosed, resize: horizontal, position: 50, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/members.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_image, name: Image, icon: , closeType: self-closed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/image.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_image_slider, name: Slider, icon: , closeType: self-closed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/image-slider.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_grid_gallery, name: Grid Gallery, icon: , closeType: self-closed, resize: horizontal, position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/grid-gallery.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_button, name: Button, icon: , closeType: self-closed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/button.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_social_buttons, name: Social Share Buttons, icon: , closeType: enclosed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/social-buttons.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_social_profile, name: Social Buttons, icon: , closeType: self-closed, resize: horizontal, position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/social-profile.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_video, name: Video, icon: , closeType: self-closed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/video.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_audio, name: Audio, icon: , closeType: enclosed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/player.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_gmap, name: Google Maps, icon: , closeType: self-closed, resize: all, position: 60, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/map.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_space, name: Space, icon: , closeType: self-closed, resize: all, position: 50, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/space.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_embed, name: Embed, icon: , closeType: self-closed, resize: horizontal, position: 40, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/code.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_google_chart, name: Chart, icon: , closeType: enclosed, resize: all, position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/chart.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_tabs, name: Tabs, icon: , closeType: enclosed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/tabs.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_tab, name: Tab, icon: , closeType: enclosed, resize: none, position: 0, show: </span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_accordion, name: Accordion, icon: , closeType: enclosed, resize: horizontal, position: 11, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/accordion.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_accordion_item, name: Accordion Section, icon: , closeType: enclosed, resize: none, position: 0, show: </span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/no-object.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_table, name: Table, icon: , closeType: enclosed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/table.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_posts_grid, name: Posts Grid, icon: , closeType: self-closed, resize: horizontal, position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/posts-grid.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_archives, name: Archives, icon: , closeType: self-closed, resize: horizontal, position: 45, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_calendar, name: Calendar, icon: , closeType: self-closed, resize: horizontal, position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_categories, name: Categories, icon: , closeType: self-closed, resize: horizontal, position: 40, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_navmenu, name: Custom Menu, icon: , closeType: self-closed, resize: horizontal, position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_meta, name: Meta, icon: , closeType: self-closed, resize: horizontal, position: 55, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_pages, name: Pages, icon: , closeType: self-closed, resize: horizontal, position: 15, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_posts, name: Recent Posts, icon: , closeType: self-closed, resize: horizontal, position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_comments, name: Recent Comments, icon: , closeType: self-closed, resize: horizontal, position: 25, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_rss, name: RSS, icon: , closeType: self-closed, resize: horizontal, position: 50, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_search, name: Search, icon: , closeType: self-closed, resize: horizontal, position: 35, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_tagcloud, name: Tag Cloud, icon: , closeType: self-closed, resize: horizontal, position: 60, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wp_widgets_area, name: Widgets Area, icon: , closeType: self-closed, resize: horizontal, position: 5, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/object/sidebar.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_grid, name: Grid, icon: , position: 0, show: </span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_text, name: Text, icon: , position: 0, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/text.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_image, name: Image, icon: , position: 10, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/image.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_button, name: Button, icon: , position: 20, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/button.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_media, name: Media, icon: , position: 30, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/media.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_other, name: Other, icon: , position: 40, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/other.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>
<div class="alert"><span class="object">id: mp_wordpress, name: WordPress, icon: , position: 50, show: 1</span>
    <ul class="property">
        <li>icon:
            <ul class="errors">
                <li>File //images/ce/group/no-group.png does not exist</li>
                <li>File //images/ce/group/wordpress.png does not exist</li>
            </ul>
        </li>
    </ul>
</div>

Regenerate Image Order

I was working on a large site, 150+k images. and the images need regenerated.
I ran the script for 4-5 days, and it failed at around 80%.

I think it currently process the media entry one by one using ASC order (by IDs) ?

It would be nice to be able to switch the order.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.