Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions bin/create-php-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const parser = pegjs.generate(
phpegjs: {
parserNamespace: null,
parserGlobalNamePrefix: 'Gutenberg_PEG_',
mbstringAllowed: false,
},
}
);
Expand Down
5 changes: 2 additions & 3 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ This also <a href="https://github.com/WordPress/gutenberg/issues/1516">gives us
In JS:

```js
wp.blocks.parse( postContent );
var blocks = wp.blocks.parse( postContent );
```

In PHP:

```php
$parser = new Gutenberg_PEG_Parser;
$blocks = $parser->parse( $post_content );
$blocks = gutenberg_parse_blocks( $post_content );
```
## Why should I start using this once released?
Blocks are likely to become the main way users interact with content. Users are going to be discovering functionality in the new universal inserter tool, with a richer block interface that provides more layout opportunities.
Expand Down
1 change: 1 addition & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Load API functions, register scripts and actions, etc.
require_once dirname( __FILE__ ) . '/lib/blocks.php';
require_once dirname( __FILE__ ) . '/lib/client-assets.php';
require_once dirname( __FILE__ ) . '/lib/compat.php';
require_once dirname( __FILE__ ) . '/lib/i18n.php';
require_once dirname( __FILE__ ) . '/lib/parser.php';
require_once dirname( __FILE__ ) . '/lib/register.php';
Expand Down
25 changes: 15 additions & 10 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,30 @@ function unregister_block_type( $name ) {
}

/**
* Renders the dynamic blocks into the post content
* Parses blocks out of a content string.
*
* @since 0.1.0
* @since 0.5.0
*
* @param string $content Post content.
* @return array Array of parsedblock objects.
*/
function gutenberg_parse_blocks( $content ) {
$parser = new Gutenberg_PEG_Parser;
return $parser->parse( _gutenberg_utf8_split( $content ) );
}

/**
* Parses dynamic blocks out of `post_content` and re-renders them.
*
* @since 0.1.0
*
* @param string $content Post content.
* @return string Updated post content.
*/
function do_blocks( $content ) {
global $wp_registered_blocks;

if ( ! extension_loaded( 'mbstring' ) ) {
// Skip server-side rendering of blocks until WordPress/gutenberg#1611
// is properly fixed.
return $content;
}

$parser = new Gutenberg_PEG_Parser;
$blocks = $parser->parse( $content );
$blocks = gutenberg_parse_blocks( $content );

$content_after_blocks = '';

Expand Down
69 changes: 69 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* PHP configuration compatibility functions for the Gutenberg editor plugin.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

/**
* Splits a UTF-8 string into an array of UTF-8-encoded codepoints.
*
* @since 0.5.0
*
* Based on WordPress' _mb_substr() compat function.
*
* @param string $str The string to split.
* @return array
*/
function _gutenberg_utf8_split( $str ) {
if ( _wp_can_use_pcre_u() ) {
// Use the regex unicode support to separate the UTF-8 characters into
// an array.
preg_match_all( '/./us', $str, $match );
return $match[0];
}

$regex = '/(
[\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xE1-\xEC][\x80-\xBF]{2}
| \xED[\x80-\x9F][\x80-\xBF]
| [\xEE-\xEF][\x80-\xBF]{2}
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
| [\xF1-\xF3][\x80-\xBF]{3}
| \xF4[\x80-\x8F][\x80-\xBF]{2}
)/x';

// Start with 1 element instead of 0 since the first thing we do is pop.
$chars = array( '' );
do {
// We had some string left over from the last round, but we counted it
// in that last round.
array_pop( $chars );

// Split by UTF-8 character, limit to 1000 characters (last array
// element will contain the rest of the string).
$pieces = preg_split(
$regex,
$str,
1000,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

$chars = array_merge( $chars, $pieces );

// If there's anything left over, repeat the loop.
if ( count( $pieces ) > 1 ) {
$str = array_pop( $pieces );
} else {
break;
}
} while ( $str );

return $chars;
}
Loading