Automattic / pym-shortcode

A WordPress solution to embed iframes that are responsive horizontally and vertically using the NPR Visuals Team's https://blog.apps.npr.org/pym.js/
https://wordpress.org/plugins/pym-shortcode/
GNU General Public License v2.0
14 stars 7 forks source link

Add Gutenberg support to the plugin #34

Closed benlk closed 6 years ago

benlk commented 6 years ago

Changes

To do list

benlk commented 6 years ago

With the following post_content:

<!-- wp:shortcode -->
[pym src="https://f.benlk.com/graphics/heartbeat-abortion-bills/child.html"]
<!-- /wp:shortcode -->

<!-- wp:paragraph -->
<p>the preceding is a shortcode block.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The following is the Pym Embed:</p>
<!-- /wp:paragraph -->

<!-- wp:pym-shortcode/pym {"src":"http://blog.apps.npr.org/pym.js/examples/table/child.html","align":"full"} /-->

The following output:

<div id="pym_1" class="pym  "></div>
<script>var pym_1 = new pym.Parent('pym_1', 'https://f.benlk.com/graphics/heartbeat-abortion-bills/child.html', {})</script>

<p>the preceding is a shortcode block.</p>
<p>The following is the Pym Embed:</p>
<div id="pym_0" class="pym alignfull "></div>
<script src="http://pym-shortcode.test/wp-content/plugins/pym-shortcode/js/pym.v1.min.js"></script><script>var pym_0 = new pym.Parent('pym_0', 'http://blog.apps.npr.org/pym.js/examples/table/child.html', {})</script>

Why does the first one have a later ID? Does the block renderer run before the shortcode renderer? How can it be assured that the script source is output before any and all tags?

benlk commented 6 years ago
Long long debug output ```html wp> global $wp_filter; var_dump( $wp_filter['the_content'], true ); object(WP_Hook)#1139 (5) { ["callbacks"]=> array(5) { [8]=> array(3) { ["00000000430b60a000000000382d9011run_shortcode"]=> array(2) { ["function"]=> array(2) { [0]=> object(WP_Embed)#1440 (7) { ["handlers"]=> array(2) { [10]=> array(1) { ["youtube_embed_url"]=> array(2) { ["regex"]=> string(51) "#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i" ["callback"]=> string(24) "wp_embed_handler_youtube" } } [9999]=> array(2) { ["audio"]=> array(2) { ["regex"]=> string(41) "#^https?://.+?\.(mp3|ogg|flac|m4a|wav)$#i" ["callback"]=> string(22) "wp_embed_handler_audio" } ["video"]=> array(2) { ["regex"]=> string(41) "#^https?://.+?\.(mp4|m4v|webm|ogv|flv)$#i" ["callback"]=> string(22) "wp_embed_handler_video" } } } ["post_ID"]=> NULL ["usecache"]=> bool(true) ["linkifunknown"]=> bool(true) ["last_attr"]=> array(0) { } ["last_url"]=> string(0) "" ["return_false_on_fail"]=> bool(false) } [1]=> string(13) "run_shortcode" } ["accepted_args"]=> int(1) } ["00000000430b60a000000000382d9011autoembed"]=> array(2) { ["function"]=> array(2) { [0]=> object(WP_Embed)#1440 (7) { ["handlers"]=> array(2) { [10]=> array(1) { ["youtube_embed_url"]=> array(2) { ["regex"]=> string(51) "#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i" ["callback"]=> string(24) "wp_embed_handler_youtube" } } [9999]=> array(2) { ["audio"]=> array(2) { ["regex"]=> string(41) "#^https?://.+?\.(mp3|ogg|flac|m4a|wav)$#i" ["callback"]=> string(22) "wp_embed_handler_audio" } ["video"]=> array(2) { ["regex"]=> string(41) "#^https?://.+?\.(mp4|m4v|webm|ogv|flv)$#i" ["callback"]=> string(22) "wp_embed_handler_video" } } } ["post_ID"]=> NULL ["usecache"]=> bool(true) ["linkifunknown"]=> bool(true) ["last_attr"]=> array(0) { } ["last_url"]=> string(0) "" ["return_false_on_fail"]=> bool(false) } [1]=> string(9) "autoembed" } ["accepted_args"]=> int(1) } ["gutenberg_wpautop"]=> array(2) { ["function"]=> string(17) "gutenberg_wpautop" ["accepted_args"]=> int(1) } } [9]=> array(1) { ["do_blocks"]=> array(2) { ["function"]=> string(9) "do_blocks" ["accepted_args"]=> int(1) } } [10]=> array(4) { ["wptexturize"]=> array(2) { ["function"]=> string(11) "wptexturize" ["accepted_args"]=> int(1) } ["shortcode_unautop"]=> array(2) { ["function"]=> string(17) "shortcode_unautop" ["accepted_args"]=> int(1) } ["prepend_attachment"]=> array(2) { ["function"]=> string(18) "prepend_attachment" ["accepted_args"]=> int(1) } ["wp_make_content_images_responsive"]=> array(2) { ["function"]=> string(33) "wp_make_content_images_responsive" ["accepted_args"]=> int(1) } } [11]=> array(2) { ["capital_P_dangit"]=> array(2) { ["function"]=> string(16) "capital_P_dangit" ["accepted_args"]=> int(1) } ["do_shortcode"]=> array(2) { ["function"]=> string(12) "do_shortcode" ["accepted_args"]=> int(1) } } [20]=> array(1) { ["convert_smilies"]=> array(2) { ["function"]=> string(15) "convert_smilies" ["accepted_args"]=> int(1) } } } ["iterations":"WP_Hook":private]=> array(0) { } ["current_priority":"WP_Hook":private]=> array(0) { } ["nesting_level":"WP_Hook":private]=> int(0) ["doing_action":"WP_Hook":private]=> bool(false) } ```

Looks like yes, the block renderer does run before the shortcode renderer.

benlk commented 6 years ago

Hrm, how might this work:

  1. Add a filter to the_content that checks for the presence of the string 'pym', and if it is there, sets a global "maybe" variable to true
  2. In the pym renderer, if it is called, if the tag doesn't have its own pymsrc, set a global definitely variable to true. If pymsrc is set, then instead output the pymsrc's tag
  3. In a filter running on the_content that runs at a very late priority, like 100, if $maybe and $definitely, prepend the_content with a script tag that registers Pym.

Alternately:

  1. Everytime we would output a <script>var pym_1 = new pym.Parent('pym_1', 'https://f.benlk.com/graphics/heartbeat-abortion-bills/child.html', {})</script> , instead of outputting it then, add it to the footer with a generated callback function that has the correct variables inserted into its call!
benlk commented 6 years ago

Gonna open a separate issue for this problem, because that makes it easier to discuss separately from "should we implement blocks".

benlk commented 6 years ago

Can the Gutenberg anchor item be used to set the parent ID?

In theory, yes.

In practice:

So:

benlk commented 6 years ago

Things that I'm going to spin off into separate issues:

Left for this PR:

benlk commented 6 years ago

On the subject of the browser console errors, hard to tell.

It's disconcerting to deactivate the plugin and see nothing where a block used to be, just an empty "Classic" editor.

benlk commented 6 years ago
benlk commented 6 years ago

Internationalization is blocked by insufficient Gutenberg docs; see https://github.com/INN/pym-shortcode/issues/40.

benlk commented 6 years ago

Okay, state of this PR:

Changes

New issues created

to be addressed before the next release

deferred

benlk commented 6 years ago

Changes to docs: