Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3773 add has block functions #8340

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
6602399
Add gutenberg_content_has_block and gutenberg_post_has_block function…
andreilupu Jul 31, 2018
aa1e30b
fix phpcs warnings
andreilupu Aug 1, 2018
3c44958
Merge remote-tracking branch 'upstream/master' into 3773-add-has-bloc…
andreilupu Aug 3, 2018
e493909
adds export getBlockTypes to be used in react native (#8457)
mzorz Aug 4, 2018
426f484
Fixing the inner block link (#8476)
Ipstenu Aug 4, 2018
3450273
Use @wordpress/compose instead of @wordpress/components (#8537)
Shelob9 Aug 5, 2018
3a52e52
Fix Custom HTML error when block is empty (#8556)
Rahmon Aug 6, 2018
b941b5e
Fix link to server-side-render component (#8546)
ajitbohra Aug 6, 2018
00884ce
Export the description for server-registered blocks. (#8555)
bradyvercher Aug 6, 2018
3fdaf3d
Docs: Update links to resources moved to packages folder (#8572)
gziolo Aug 6, 2018
b83b827
Polish focus and active styles, and do cleanups (#8385)
jasmussen Aug 6, 2018
81bc778
Data Module: Adding The queried data handling into the entities abstr…
youknowriad Aug 6, 2018
fb03f0b
DomReady: Fix race condition (#8578)
youknowriad Aug 6, 2018
4fc46e7
Translatable demo content (#8551)
swissspidy Aug 6, 2018
efea640
Fix awkward capitalisation in demo post content; fixes #8590. (#8591)
sarahmonster Aug 6, 2018
576872f
API: Replace all withAPIData usage and deprecate the HoC (#8584)
youknowriad Aug 6, 2018
c59ca24
Inserter: Try making the inline tokens less important (#8587)
youknowriad Aug 6, 2018
ce6643a
Docs: Add @mirucon to CONTRIBUTORS.md
aduth Aug 6, 2018
22ba56f
Reset previous changes, add CSS to do the job instead (#7751)
Aug 6, 2018
4fd5c20
Avoid using elt.remove because it's unavailable in ie11 (#8598)
youknowriad Aug 6, 2018
4c28bd0
TinyMCE: Remove container assignment hack (#8465)
aduth Aug 6, 2018
f6fd8e5
Add a note about handling blocks with `save` (#8154)
designsimply Aug 6, 2018
c1ddfb2
Updated dependencies: `jest`, `npm-package-json-lint` and `read-pkg-u…
gziolo Aug 7, 2018
41ff1f2
DOM: Restore horizontal edge traversal implementation (#8461)
aduth Aug 7, 2018
d54396c
swaps out the CSS2 :before with the CSS3 ::before, see #8618 (#8630)
tomjn Aug 7, 2018
98274f0
Fix unrecognised `forwardedRef` prop (#8570)
noisysocks Aug 7, 2018
38378c8
Fix issue with link dialog. (#8646)
jasmussen Aug 7, 2018
1949e3a
Data: Add redux-routine package for synchronous generator flow (#8096)
aduth Aug 7, 2018
8fdf226
Allow plugins to extend the block settings menu (#7895)
nosolosw Aug 7, 2018
d5eb120
Let Wordpress make images in gallery block responsive / add srcset at…
fabianpimminger Aug 7, 2018
42b870c
RichText: Removing/merging richtext should only trigger if the select…
youknowriad Aug 7, 2018
7d8e27e
Move embed API call out of block and into data (#6678)
notnownikki Aug 7, 2018
d9b3e6f
Redux Routine: Remove fake timers from tests (#8661)
aduth Aug 7, 2018
248fc29
Fix thead colspan bug (#7899)
earnjam Aug 7, 2018
17852d6
Initialize the Autosaves controller, even if revisions are disabled (…
Aug 7, 2018
9c75e47
Move CI back to latest WordPress version and bump minimum version to …
notnownikki Aug 7, 2018
d74a4fc
Remove REST API shims for code introduced in WP 4.9.8 (#8695)
danielbachhuber Aug 7, 2018
47a69e1
chore: Add test for block mover (#8011) (#8392)
tofumatt Aug 7, 2018
e6fb5f6
Add `gutenberg_can_edit_post` filter (#8323)
wpscholar Aug 8, 2018
dd81e6b
Update `package-lock.json` (#8700)
ntwb Aug 8, 2018
d5c9b4b
Set babel dependencies to fixed version and added core-js2 support (#…
xyfi Aug 8, 2018
ea64a0f
Packages: Redux Routine: Add Babel runtime dependency (#8660)
aduth Aug 8, 2018
8cbc988
Add stylelint for SCSS linting (#8647)
noisysocks Aug 8, 2018
5b81cbc
Add components examples in README.md files - Part 2 (#8338)
mmtr Aug 8, 2018
ae2d929
Remove unused terms, taxonomies and categories code (#8250)
nosolosw Aug 8, 2018
e95026c
Framework: Remove deprecations slated for 3.5 removal (#8687)
aduth Aug 8, 2018
bec6a3c
DOM: Abort selection range set on unset range target (#8691)
aduth Aug 8, 2018
ac2fa9a
Add a warning when Cloudflare blocks REST API requests (#8640)
pento Aug 8, 2018
a529cc3
Add nosolosw as a Contributor (#8705)
nosolosw Aug 8, 2018
0023eea
I18n: Consolidate similar strings (#8708)
dimadin Aug 8, 2018
1b9af37
Try to rewrite e2e tests using jest-puppeter preset (#8301)
gziolo Aug 8, 2018
b1196b8
Add "components" docs (#8337)
wpscholar Aug 8, 2018
09dc71c
NUX: Prevent duplicate DotTips from appearing (#8642)
noisysocks Aug 8, 2018
ee6060f
Polish the checkbox, update switch documentation (#8588)
jasmussen Aug 8, 2018
fb15328
Blocks: Render Button RichText as div (#8451)
aduth Aug 8, 2018
8a8ba2a
onWriting Flow: Remove the provisional block behavior (#8706)
youknowriad Aug 8, 2018
8380680
Bug fixes for full/wide image alignments. (#8464)
Aug 8, 2018
8deaf5e
Expand date filter select box width (#8335)
ianbelanger79 Aug 8, 2018
315ce7e
Improve pre-publish panel styling (#7879)
sarahmonster Aug 8, 2018
fcd0e87
Packages: update api-fetch package documentation (#8730)
vindl Aug 8, 2018
4a78cae
Prevent Travis from running when changes are only made to .md files (…
earnjam Aug 9, 2018
abbd908
Additional keyboaard shortcuts for navigateRegions. (#8005)
afercia Aug 9, 2018
a0d9405
Manually print inline metabox initialization JavaScript when the `wp-…
desrosj Aug 9, 2018
0298cff
Sort strikethrough after link (#8755)
jasmussen Aug 9, 2018
93b6ac7
Fix 'Prevent duplicate DotTips from appearing' feedback (#8741)
noisysocks Aug 9, 2018
2c0510e
Expose redux-routine to React Native (#8761)
koke Aug 9, 2018
9bacc93
Testing: Trigger E2E test failure on console logging (#8721)
aduth Aug 9, 2018
16d7a9d
Update Lerna to 3.0.0-rc.0 (#8759)
ntwb Aug 9, 2018
f02064f
chore(release): publish
ntwb Aug 9, 2018
4f0a643
Publishes unknown-type handler methods for mobile. (#8688)
diegoreymendez Aug 9, 2018
80d2d91
Capital P, dangit (#8772)
dimadin Aug 9, 2018
f5617ac
Element: Improve validation for attribute names in serializer (#8775)
gziolo Aug 9, 2018
45e3391
RichText: Fix empty value delete behaviors (#8735)
aduth Aug 9, 2018
ed547f3
i18n: Add context to occurrences of 'More' string (#8766)
mcsf Aug 9, 2018
b66cc66
Update plugin version to 3.5 (#8784)
aduth Aug 9, 2018
2b39ea9
Localization & Spelling fixes (#8795)
Aug 10, 2018
2b4bc5b
Add new keyboard shortcuts for block settings menu (#8279)
talldan Aug 10, 2018
0a21147
Add new keyboard shortcuts help modal (#8316)
talldan Aug 10, 2018
6e62e8a
Pad menus (#8758)
jasmussen Aug 10, 2018
dd78fd2
Port heading block to mobile (#8774)
koke Aug 10, 2018
6627001
Audio Block: Avoid scrollbars when shown full-width (#8820)
youknowriad Aug 10, 2018
a88f379
Make active pill button look pressed. (#8819)
jasmussen Aug 10, 2018
29ccdcb
Allow access to the WordPress installation if DOCKER_ENV=localwpdev (…
notnownikki Aug 10, 2018
ad889ba
Extract Core Blocks to the npm packages folder (#8287)
youknowriad Aug 10, 2018
2d1e3e0
Updates docs for templatelock's `insert` option (#8843)
chrisvanpatten Aug 10, 2018
e95528d
Remove TinyMCE shim what was shimmed in WP 4.9.7 (#8832)
danielbachhuber Aug 10, 2018
97dd134
Framework: Remove client-side document title updates (#8831)
aduth Aug 10, 2018
b5490a6
Fix deletion of custom block classes (#8232)
johngodley Aug 11, 2018
2b83470
Ensure meta box functions are available in Gutenberg context (#8850)
danielbachhuber Aug 11, 2018
dab3fc1
Update Dashicons component. (#8811)
jasmussen Aug 11, 2018
fa3e58a
Add a package license checker (#8808)
pento Aug 13, 2018
eeda623
Fix unhandled error dialog styling (#8834)
youknowriad Aug 13, 2018
6ce1a6b
Add styles for Spinner component (#8801)
mmtr Aug 13, 2018
fd6aa36
URLInput: Pass the the search result object to props.onChange (#8662)
roborourke Aug 13, 2018
216f279
IE11: Fix default appender placeholder (#8837)
youknowriad Aug 13, 2018
e1811b2
chore(release): publish
gziolo Aug 13, 2018
4c441d2
Data Module: Correctly handle per_page=-1 in the queried data state (…
youknowriad Aug 13, 2018
35b2fb2
Test: Ensure the full content integration test is run (#8923)
youknowriad Aug 13, 2018
0595c3e
Adds support for matching blocks with diacritics using lodash.deburr …
dsawardekar Aug 13, 2018
28e00cd
PrePublishPanel: suggest tags and postformat (#8235)
nosolosw Aug 13, 2018
eeb298e
i18n: Use argument swapping instead of named arguments for string pla…
dimadin Aug 13, 2018
070c26f
[Fix] Make sure Latest Posts alignment class behaviour (#8847)
Aug 13, 2018
a5b6126
Remove the workaround for intermittent multiple-tab preview test fail…
notnownikki Aug 13, 2018
751a2fc
Fix paragraph splits on RN (#8915)
SergioEstevao Aug 13, 2018
acef68f
Add remove block keyboard shortcut (#8805)
talldan Aug 14, 2018
2de40d8
Add the React Native entrypoint to all runtime packages (#8933)
hypest Aug 14, 2018
8085f2e
Update & flesh out design handbook (#8924)
sarahmonster Aug 14, 2018
ca39dc5
Components: Deprecate `withContext` HOC and remove its usage (#8189)
gziolo Aug 14, 2018
8dd93db
Inserter: take into account nested blocks to render the list of inser…
nosolosw Aug 14, 2018
fb27a2e
Fix the shortcode block UI in IE11 (#8922)
youknowriad Aug 14, 2018
8762d3e
Tags: Fix the tags input in IE11 (#8918)
youknowriad Aug 14, 2018
710cd9f
[Enhancement] Change color of links in gallery block caption (#8004)
Aug 14, 2018
3767009
Fix our custom element serializer in IE11 (#8967)
youknowriad Aug 14, 2018
1759c92
Apply new checkbox CSS to radio buttons and fix border radius (#8835)
chrisvanpatten Aug 14, 2018
a6ae71e
Add new Insert Before/After menu items and shortcuts (#8909)
talldan Aug 15, 2018
b895f2f
Remove unnecessary comma ( , ) (#8910)
prajapatisagar Aug 15, 2018
a7ab5d8
Fix code block style regression. (#9011)
jasmussen Aug 15, 2018
dfada03
Refactor template select field to use SelectControl (#8987)
chrisvanpatten Aug 15, 2018
a0e0847
Preview: Open new window if prior preview window closed (#8446)
aduth Aug 15, 2018
ad5e8d3
Default Block Appender: Remove redundant event handlers (#8714)
aduth Aug 15, 2018
859a8d2
Element: Create dummy context components for type switch (#8998)
aduth Aug 15, 2018
7461f5d
Fix canvas jumb when navigation regions (#9014)
youknowriad Aug 15, 2018
d9ef0ed
Bring the preview tab to the front when clicking the preview button (…
youknowriad Aug 15, 2018
c72e8b7
toAsyncIterable: remove Promise.resolve call that's already done by t…
jsnajdr Aug 15, 2018
aefe0dd
Show drop zone background when file is dragged (#8752)
stoyan0v Aug 15, 2018
a234c34
Fix small typo (#9020)
Aug 15, 2018
5f84999
Edit Post: Reset active sidebar tab on initial load (#8995)
aduth Aug 15, 2018
8d9b4b3
Add generic has_blocks() function (#8631)
obenland Aug 16, 2018
c647b05
Fix crashed block height and inserted order (#8926)
johngodley Aug 16, 2018
cbb44b6
Publish button: Avoid changing the label of the "publish" button if a…
youknowriad Aug 16, 2018
0451142
Polish permalink UI and make it responsive (#9010)
jasmussen Aug 16, 2018
966175f
Release font-size for code. (#8914)
jasmussen Aug 16, 2018
c3211fa
Let the menu grow (#8756)
jasmussen Aug 16, 2018
837d908
Add styles for Notice component (#8856)
mmtr Aug 16, 2018
3b9d23f
Data: Add RegistryConsumer export (#9042)
coderkevin Aug 16, 2018
2399173
feat: Hide keyboard shortcuts on mobile screens (#9051)
tofumatt Aug 16, 2018
cbf3041
IE11: Fix The metaboxes area position (overlapping the content) (#8964)
youknowriad Aug 16, 2018
babc9e5
Fix the block styles in IE11 (#8962)
youknowriad Aug 16, 2018
f11b1eb
Fix the heading level buttons in IE11 (#8840)
youknowriad Aug 16, 2018
3b33874
feat: Show sidebar in HTML edit mode for blocks (#8969)
tofumatt Aug 16, 2018
63f0cf2
Framework: Remove deprecations slated for 3.6 removal (#8993)
aduth Aug 16, 2018
f9e82d0
Custom Block Icons (#8916)
mtias Aug 16, 2018
72da0cd
Introduce strip_dynamic_blocks() for excerpts (#8984)
obenland Aug 16, 2018
5d1df74
Add gutenberg_content_has_block and gutenberg_post_has_block function…
andreilupu Jul 31, 2018
ebb478f
fix phpcs warnings
andreilupu Aug 1, 2018
17ae880
Remove prefix and make it consistent with has_blocks
andreilupu Aug 16, 2018
2388042
merge
andreilupu Aug 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,55 @@ function do_blocks( $content ) {
return $rendered_content;
}
add_filter( 'the_content', 'do_blocks', 9 ); // BEFORE do_shortcode().

/**
* Determine whether a post has a specific block. This test optimizes for
* performance rather than strict accuracy, detecting the pattern of a block type
* but not validating its structure. For strict accuracy, you should use the
* block parser on post content.
*
* @see gutenberg_parse_blocks()
*
* @since 3.4
*
* @param string $block_name Full Block name to look for.
* @param object $post Post.
* @return bool Whether the content contain the specified block.
*/
function gutenberg_post_has_block( $block_name, $post = null ) {

if ( null === $post ) {
global $post;
} else {
$post = get_post( $post );
}

return $post && gutenberg_content_has_block( $block_name, $post->post_content );
}

/**
* Determine whether a content string contains a specific block. This test
* optimizes for performance rather than strict accuracy, detecting the
* block type but not validating its structure. For strict accuracy,
* you should use the block parser on post content.
*
* @since 3.4
* @see gutenberg_parse_blocks()
*
* @param string $block_name Full Block name to look for.
* @param string $content Content to test. Fallback on the $post global's content in case it's missing.
* @return bool Whether the content contain the specified block.
*/
function gutenberg_content_has_block( $block_name, $content = null ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this magic behavior of $content = null. If I passed a $content variable that was set to null, then I'd unexpectedly get the global $post->post_content.

Is there prior art in core that you're basing this on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to be consistent with gutenberg_post_has_block but now I'm having second thoughts.
gutenberg_post_has_block is related to $post and prior in core, I can think of has_excerpt.

But in opposition, I think that gutenberg_content_has_block should be specific with params and no magic effects.

I'll make it required. Do you think that gutenberg_post_has_block should keep the global $post default?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that gutenberg_post_has_block should keep the global $post default?

Isn't gutenberg_post_has_block a new function? I think it should be passed an explicit string instead of any dependency on the $post object.

Taking a step back though, I think we should wait until further progress on #8352 until we spend more time on this. It may be that the parser itself exposes an equivalent method.

global $post;

if ( null === $content && isset( $post->post_content ) ) {
$content = $post->post_content;
}

if ( ! gutenberg_content_has_blocks( $content ) ) {
return false;
}

return false !== strpos( $content, '<!-- wp:' . $block_name . ' ' );
}
118 changes: 118 additions & 0 deletions phpunit/class-block-type-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@
* Tests for WP_Block_Type
*/
class Block_Type_Test extends WP_UnitTestCase {
function setUp() {
parent::setUp();
}

/**
* Editor user ID.
*
* @var int
*/
protected static $editor_user_id;

/**
* ID for a post containing blocks.
*
* @var int
*/
protected static $post_with_blocks;

/**
* ID for a post without blocks.
*
* @var int
*/
protected static $post_without_blocks;

/**
* Set up before class.
*/
public static function wpSetUpBeforeClass() {
self::$editor_user_id = self::factory()->user->create( array(
'role' => 'editor',
) );

self::$post_with_blocks = self::factory()->post->create( array(
'post_title' => 'Example',
'post_content' => "<!-- wp:core/text {\"dropCap\":true} -->\n<p class=\"has-drop-cap\">Tester</p>\n<!-- /wp:core/text -->",
) );

self::$post_without_blocks = self::factory()->post->create( array(
'post_title' => 'Example',
'post_content' => 'Tester',
) );
}

function test_set_props() {
$name = 'core/dummy';
$args = array(
Expand Down Expand Up @@ -112,6 +156,80 @@ function test_prepare_attributes() {
), $prepared_attributes );
}

function test_has_block_with_mixed_content() {
$mixed_post_content = 'before' .
'<!-- wp:core/dummy --><!-- /wp:core/dummy -->' .
'<!-- wp:core/dummy_atts {"value":"b1"} --><!-- /wp:core/dummy_atts -->' .
'<!-- wp:core/dummy-child -->
<p>testing the test</p>
<!-- /wp:core/dummy-child -->' .
'between' .
'<!-- wp:core/self-close-dummy /-->' .
'<!-- wp:custom/dummy {"value":"b2"} /-->' .
'after';

$this->assertTrue( gutenberg_content_has_block( 'core/dummy', $mixed_post_content ) );

$this->assertTrue( gutenberg_content_has_block( 'core/dummy_atts', $mixed_post_content ) );

$this->assertTrue( gutenberg_content_has_block( 'core/dummy-child', $mixed_post_content ) );

$this->assertTrue( gutenberg_content_has_block( 'core/self-close-dummy', $mixed_post_content ) );

$this->assertTrue( gutenberg_content_has_block( 'custom/dummy', $mixed_post_content ) );

// checking for a partial block name should fail.
$this->assertFalse( gutenberg_content_has_block( 'core/dumm', $mixed_post_content ) );

// checking for a wrong namespace should fail.
$this->assertFalse( gutenberg_content_has_block( 'custom/dummy_atts', $mixed_post_content ) );

// checking for namespace only should not work. Or maybe ... ?
$this->assertFalse( gutenberg_content_has_block( 'core', $mixed_post_content ) );
}

function test_has_block_with_invalid_content() {
// some content with invalid HMTL comments and a single valid block.
$invalid_content = 'before' .
'<!- - wp:core/weird-space --><!-- /wp:core/weird-space -->' .
'<!--wp:core/untrimmed-left --><!-- /wp:core/untrimmed -->' .
'<!-- wp:core/dummy --><!-- /wp:core/dummy -->' .
'<!-- wp:core/untrimmed-right--><!-- /wp:core/untrimmed2 -->' .
'after';

$this->assertFalse( gutenberg_post_has_block( 'core/text', self::$post_without_blocks ) );

$this->assertFalse( gutenberg_content_has_block( 'core/weird-space', $invalid_content ) );

$this->assertFalse( gutenberg_content_has_block( 'core/untrimmed-left', $invalid_content ) );

$this->assertFalse( gutenberg_content_has_block( 'core/untrimmed-right', $invalid_content ) );

$this->assertTrue( gutenberg_content_has_block( 'core/dummy', $invalid_content ) );
}

function test_post_has_block() {
$static_content = "<!-- wp:core/text {\"dropCap\":true} -->\n<p class=\"has-drop-cap\">Tester</p>\n<!-- /wp:core/text -->";

$this->assertTrue( gutenberg_post_has_block( 'core/text', self::$post_with_blocks ) );
// should fail for a non-existent block `custom/dummy`.
$this->assertFalse( gutenberg_post_has_block( 'custom/dummy', self::$post_with_blocks ) );

$this->assertTrue( gutenberg_content_has_block( 'core/text', $static_content ) );
$this->assertFalse( gutenberg_content_has_block( 'custom/dummy', $static_content ) );

// this functions should not work without the second param until the $post global is set.
$this->assertFalse( gutenberg_content_has_block( 'core/text' ) );
$this->assertFalse( gutenberg_post_has_block( 'core/text' ) );

global $post;
$post = get_post( self::$post_with_blocks );

// check if the function correctly detects content from the $post global.
$this->assertTrue( gutenberg_content_has_block( 'core/text' ) );
$this->assertTrue( gutenberg_post_has_block( 'core/text' ) );
}

function render_dummy_block( $attributes ) {
return json_encode( $attributes );
}
Expand Down