Giter VIP home page Giter VIP logo

embed-command's Introduction

wp-cli/embed-command

Inspects oEmbed providers, clears embed cache, and more.

Testing

Quick links: Using | Installing | Contributing | Support

Using

This package implements the following commands:

wp embed

Inspects oEmbed providers, clears embed cache, and more.

wp embed

EXAMPLES

# Get embed HTML for a given URL.
$ wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ
<iframe width="525" height="295" src="https://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed" ...

# Find cache post ID for a given URL.
$ wp embed cache find https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500
123

# List format,endpoint fields of available providers.
$ wp embed provider list
+------------------------------+-----------------------------------------+
| format                       | endpoint                                |
+------------------------------+-----------------------------------------+
| #https?://youtu\.be/.*#i     | https://www.youtube.com/oembed          |
| #https?://flic\.kr/.*#i      | https://www.flickr.com/services/oembed/ |
| #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/            |

# List id,regex,priority fields of available handlers.
$ wp embed handler list --fields=priority,id
+----------+-------------------+
| priority | id                |
+----------+-------------------+
| 10       | youtube_embed_url |
| 9999     | audio             |
| 9999     | video             |
+----------+-------------------+

wp embed fetch

Attempts to convert a URL into embed HTML.

wp embed fetch <url> [--width=<width>] [--height=<height>] [--post-id=<id>] [--discover] [--skip-cache] [--skip-sanitization] [--do-shortcode] [--limit-response-size=<size>] [--raw] [--raw-format=<json|xml>]

In non-raw mode, starts by checking the URL against the regex of the registered embed handlers. If none of the regex matches and it's enabled, then the URL will be given to the WP_oEmbed class.

In raw mode, checks the providers directly and returns the data.

OPTIONS

<url>
	URL to retrieve oEmbed data for.

[--width=<width>]
	Width of the embed in pixels.

[--height=<height>]
	Height of the embed in pixels.

[--post-id=<id>]
	Cache oEmbed response for a given post.

[--discover]
	Enable oEmbed discovery. Defaults to true.

[--skip-cache]
	Ignore already cached oEmbed responses. Has no effect if using the 'raw' option, which doesn't use the cache.

[--skip-sanitization]
	Remove the filter that WordPress from 4.4 onwards uses to sanitize oEmbed responses. Has no effect if using the 'raw' option, which by-passes sanitization.

[--do-shortcode]
	If the URL is handled by a registered embed handler and returns a shortcode, do shortcode and return result. Has no effect if using the 'raw' option, which by-passes handlers.

[--limit-response-size=<size>]
	Limit the size of the resulting HTML when using discovery. Default 150 KB (the standard WordPress limit). Not compatible with 'no-discover'.

[--raw]
	Return the raw oEmbed response instead of the resulting HTML. Ignores the cache and does not sanitize responses or use registered embed handlers.

[--raw-format=<json|xml>]
	Render raw oEmbed data in a particular format. Defaults to json. Can only be specified in conjunction with the 'raw' option.
	---
	options:
	  - json
	  - xml
	---

EXAMPLES

# Get embed HTML for a given URL.
$ wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ
<iframe width="525" height="295" src="https://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed" ...

# Get raw oEmbed data for a given URL.
$ wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ --raw
{"author_url":"https:\/\/www.youtube.com\/user\/RickAstleyVEVO","width":525,"version":"1.0", ...

wp embed provider

Retrieves oEmbed providers.

wp embed provider

EXAMPLES

# List format,endpoint fields of available providers.
$ wp embed provider list
+------------------------------+-----------------------------------------+
| format                       | endpoint                                |
+------------------------------+-----------------------------------------+
| #https?://youtu\.be/.*#i     | https://www.youtube.com/oembed          |
| #https?://flic\.kr/.*#i      | https://www.flickr.com/services/oembed/ |
| #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/            |

# Get the matching provider for the URL.
$ wp embed provider match https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/oembed

wp embed provider list

Lists all available oEmbed providers.

wp embed provider list [--field=<field>] [--fields=<fields>] [--format=<format>] [--force-regex]

OPTIONS

[--field=<field>]
	Display the value of a single field

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

[--format=<format>]
	Render output in a particular format.
	---
	default: table
	options:
	  - table
	  - csv
	  - json
	---

[--force-regex]
	Turn the asterisk-type provider URLs into regexes.

AVAILABLE FIELDS

These fields will be displayed by default for each provider:

  • format
  • endpoint

This field is optionally available:

  • regex

EXAMPLES

# List format,endpoint fields of available providers.
$ wp embed provider list --fields=format,endpoint
+------------------------------+-----------------------------------------+
| format                       | endpoint                                |
+------------------------------+-----------------------------------------+
| #https?://youtu\.be/.*#i     | https://www.youtube.com/oembed          |
| #https?://flic\.kr/.*#i      | https://www.flickr.com/services/oembed/ |
| #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/            |

wp embed provider match

Gets the matching provider for a given URL.

wp embed provider match <url> [--discover] [--limit-response-size=<size>] [--link-type=<json|xml>]

OPTIONS

<url>
	URL to retrieve provider for.

[--discover]
	Whether to use oEmbed discovery or not. Defaults to true.

[--limit-response-size=<size>]
	Limit the size of the resulting HTML when using discovery. Default 150 KB (the standard WordPress limit). Not compatible with 'no-discover'.

[--link-type=<json|xml>]
	Whether to accept only a certain link type when using discovery. Defaults to any (json or xml), preferring json. Not compatible with 'no-discover'.
	---
	options:
	  - json
	  - xml
	---

EXAMPLES

# Get the matching provider for the URL.
$ wp embed provider match https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/oembed

wp embed handler

Retrieves embed handlers.

wp embed handler

EXAMPLES

# List id,regex,priority fields of available handlers.
$ wp embed handler list --fields=priority,id
+----------+-------------------+
| priority | id                |
+----------+-------------------+
| 10       | youtube_embed_url |
| 9999     | audio             |
| 9999     | video             |

wp embed handler list

Lists all available embed handlers.

wp embed handler list [--field=<field>] [--fields=<fields>] [--format=<format>]

OPTIONS

[--field=<field>]
	Display the value of a single field

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

[--format=<format>]
	Render output in a particular format.
	---
	default: table
	options:
	  - table
	  - csv
	  - json
	---

AVAILABLE FIELDS

These fields will be displayed by default for each handler:

  • id
  • regex

These fields are optionally available:

  • callback
  • priority

EXAMPLES

# List id,regex,priority fields of available handlers.
$ wp embed handler list --fields=priority,id
+----------+-------------------+
| priority | id                |
+----------+-------------------+
| 10       | youtube_embed_url |
| 9999     | audio             |
| 9999     | video             |

wp embed cache

Finds, triggers, and deletes oEmbed caches.

wp embed cache

EXAMPLES

# Find cache post ID for a given URL.
$ wp embed cache find https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500
123

# Clear cache for a post.
$ wp embed cache clear 123
Success: Cleared oEmbed cache.

# Triggers cache for a post.
$ wp embed cache trigger 456
Success: Caching triggered!

wp embed cache clear

Deletes all oEmbed caches for a given post.

wp embed cache clear <post_id>

oEmbed caches for a post are stored in the post's metadata.

OPTIONS

<post_id>
	ID of the post to clear the cache for.

EXAMPLES

# Clear cache for a post
$ wp embed cache clear 123
Success: Cleared oEmbed cache.

wp embed cache find

Finds an oEmbed cache post ID for a given URL.

wp embed cache find <url> [--width=<width>] [--height=<height>] [--discover]

Starting with WordPress 4.9, embeds that aren't associated with a specific post will be cached in a new oembed_cache post type. There can be more than one such entry for a url depending on attributes and context.

Not to be confused with oEmbed caches for a given post which are stored in the post's metadata.

OPTIONS

<url>
	URL to retrieve oEmbed data for.

[--width=<width>]
	Width of the embed in pixels. Part of cache key so must match. Defaults to `content_width` if set else 500px, so is theme and context dependent.

[--height=<height>]
	Height of the embed in pixels. Part of cache key so must match. Defaults to 1.5 * default width (`content_width` or 500px), to a maximum of 1000px.

[--discover]
	Whether to search with the discover attribute set or not. Part of cache key so must match. If not given, will search with attribute: unset, '1', '0', returning first.

EXAMPLES

# Find cache post ID for a given URL.
$ wp embed cache find https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500
123

wp embed cache trigger

Triggers the caching of all oEmbed results for a given post.

wp embed cache trigger <post_id>

oEmbed caches for a post are stored in the post's metadata.

OPTIONS

<post_id>
	ID of the post to do the caching for.

EXAMPLES

# Triggers cache for a post
$ wp embed cache trigger 456
Success: Caching triggered!

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/embed-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.

embed-command's People

Contributors

danielbachhuber avatar ernilambar avatar gitlost avatar janw-me avatar jrfnl avatar pattonwebz avatar pdaalder avatar schlessera avatar swissspidy avatar wojsmol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

embed-command's Issues

Include oEmbed endpoints introduced by plugins to the `wp embed provider list`

Feature Request

Describe your use case and the problem you are facing

Hello,
I noticed the wp embed provider list only contains default WP providers. It would be great if it listed providers added to WP via plugins through wp_oembed_add_provider on the init hook. I tested a few plugins beyond my own and none when added seem to list their providers. Some I tested;
https://en-ca.wordpress.org/plugins/sendtonews-oembed/
https://en-ca.wordpress.org/plugins/jotform-oembed/
https://en-ca.wordpress.org/plugins/github-embed/

In addition to that the wp embed fetch <url> won't work for any oembed URLs of these custom providers.

Describe the solution you'd like

I would love the wp embed functions to cover custom oembed providers installed via plugins to the users WordPress install.

Thank you

Test broken for `--force-regex` flag

The tests are currently failing for the List oEmbed providers scenario when it tries to test the effects of the --force-regex flag.

It took me a bit of research to understand what's happening. Here's my current understanding:

  • oEmbed provider formats can be provided as proper regexes, or in a simplified format that uses an asterisk as a wildcard.
  • The --force-regex flag turns the simplified format in a proper regex.
  • The test assumed that Core comes with some simplified formats by default. Therefore, to test whether the --force-regex had an effect, it simplifies the logic by just testing whether the result with the flag is different from the result without the flag.
  • Now, Core currently only ships with proper regex formats by default, no simplified ones.
  • This makes the test fail, as there's nothing to convert, and hence the result with or without the flag is exactly the same.

This means that the test needs to be changed to verify this in a different way. I think the easiest is to filter the list of providers before retrieving them, injecting a simplified format in there and comparing before & after application of the --force-regex flag. If we inject one ourselves, we can even test against the exact regex to be generated.

Support --all for `wp embed cache clear`

Feature Request

Describe your use case and the problem you are facing

There's currently no way of completely clearing the oembed cache. This is for example useful after one has added a oembed_dataparse filter to modify the HTML.

Describe the solution you'd like

I'd propose adding an cache clear-all command.

The quick and dirty version is to run DELETE FROM wp_posts WHERE post_type = 'oembed_cache' and DELETE FROM wp_postmeta WHERE meta_key LIKE '_oembed_%'.

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:

Add command to list embed handlers

Custom embed handlers can be added using wp_embed_register_handler().

It would be nice if there was a way to list all of them. By default WordPress adds 3 custom handlers already, see wp_maybe_load_embeds().

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

Error when no cache is found

The current behavior of wp embed cache clear is to output a line and return when no cache is found:

if ( empty( $post_metas ) ) {
	WP_CLI::line( sprintf( 'No metadata available for post with ID %d!', $post_id ) );
	return;
}

However, this is inconsistent with the rest of WP-CLI:

local ➜  wordpress-develop git:(master) wp option delete foo
Error: Could not delete 'foo' option. Does it exist?
local ➜  wordpress-develop git:(master) wp cache delete foo
Error: The object was not deleted.

The correct behavior should be to error when no cache is found.

Add cache command

It would be nice if we could:

  • Delete the oembed_cache post for a given URL (+ width / height)
    (\WP_Embed::find_oembed_post_id())
  • Delete the oEmbed cache for a given post ID (\WP_Embed::delete_oembed_caches())
  • Trigger oEmbed cache for a given post ID (\WP_Embed::cache_oembed())

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
    

Set WP version requirement for find command

wp embed cache find requires WordPress 4.9 because \WP_Embed::find_oembed_post_id() was introduced then.

However, wp embed cache currently requires WordPress 2.9:

WP_CLI::add_command( 'embed cache', '\WP_CLI\Embeds\Cache_Command', array(
'before_invoke' => function () {
if ( \WP_CLI\Utils\wp_version_compare( '2.9', '<' ) ) {
WP_CLI::error( 'Requires WordPress 2.9 or greater.' );
}
},
) );

Note: wp embed cache clear works with WordPress 2.9 as \WP_Embed::delete_oembed_caches() was already present.

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.