Skip to content

Commit

Permalink
Testing Fix phpcs job
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 4, 2017
1 parent 378ee1b commit f0cd860
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 64 deletions.
93 changes: 47 additions & 46 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function gutenberg_menu() {
/**
* Registers a block.
*
* @param string $slug Block slug including namespace.
* @param array $settings Block settings
* @param string $slug Block slug including namespace.
* @param array $settings Block settings.
* @return array The block, if it has been successfully registered.
* @return array The block, if it has been successfully registered.
*/
function register_block( $slug, $settings ) {
global $registered_blocks;
Expand All @@ -58,7 +58,7 @@ function register_block( $slug, $settings ) {
return;
}

$settings[ 'slug' ] = $slug;
$settings['slug'] = $slug;
$registered_blocks[ $slug ] = $settings;

return $settings;
Expand All @@ -67,13 +67,14 @@ function register_block( $slug, $settings ) {
/**
* Unregisters a block.
*
* @param string slug Block slug
* @return array The previous block value, if it has been
* successfully unregistered; otherwise `null`.
* @param string $slug Block slug.
* @return array The previous block value, if it has been
* successfully unregistered; otherwise `null`.
*/
function unregister_block( $slug ) {
global $registered_blocks;
if ( ! isset( $registered_blocks[ $slug ] ) ) {
/* translators: 1: block slug */
$message = sprintf( __( 'Block "%s" is not registered.' ), $slug );
_doing_it_wrong( __FUNCTION__, $message, '0.1.0' );
return;
Expand All @@ -86,16 +87,16 @@ function unregister_block( $slug ) {
*
* @since 0.1.0
*
* @param string $attr_string Attributes string
* @param string $attr_string Attributes string.
* @return array
*/
function parse_block_attributes( $attr_string ) {
$attributes_matcher = '/([^\s]+):([^\s]+)\s*/';
preg_match_all( $attributes_matcher, $attr_string, $matches );
$attributes = array();
foreach ( $matches[ 1 ] as $index => $attribute_match ) {
$attributes[ $attribute_match ] = $matches[ 2 ][ $index ];
foreach ( $matches[1] as $index => $attribute_match ) {
$attributes[ $attribute_match ] = $matches[2][ $index ];
}

return $attributes;
Expand All @@ -106,40 +107,40 @@ function parse_block_attributes( $attr_string ) {
*
* @since 0.1.0
*
* @param string $content Post content
* @return string Updated post content
* @param string $content Post content.
*
* @return string Updated post content.
*/
function do_blocks( $content ) {
global $registered_blocks;

// Extract the blocks from the post content
// Extract the blocks from the post content.
$open_matcher = '/<!--\s*wp:([a-z](?:[a-z0-9\/]+)*)\s+((?:(?!-->).)*)-->.*?<!--\s*\/wp:\g1\s+-->/';
preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE );

$new_content = $content;
foreach ( $matches[ 0 ] as $index => $block_match ) {
$block_name = $matches[ 1 ][ $index ][ 0 ];
// do nothing if the block is not registered
foreach ( $matches[0] as $index => $block_match ) {
$block_name = $matches[1][ $index ][0];
// do nothing if the block is not registered.
if ( ! isset( $registered_blocks[ $block_name ] ) ) {
continue;
}

$block_markup = $block_match[ 0 ];
$block_position = $block_match[ 1 ];
$block_attributes_string = $matches[ 2 ][ $index ][ 0 ];
$block_markup = $block_match[0];
$block_position = $block_match[1];
$block_attributes_string = $matches[2][ $index ][0];
$block_attributes = parse_block_attributes( $block_attributes_string );

// Call the block's render function to generate the dynamic output
$output = call_user_func( $registered_blocks[ $block_name ][ 'render' ], $block_attributes );
// Call the block's render function to generate the dynamic output.
$output = call_user_func( $registered_blocks[ $block_name ]['render'], $block_attributes );

// Replace the matched block with the dynamic output
// Replace the matched block with the dynamic output.
$new_content = str_replace( $block_markup, $output, $new_content );
}

return $new_content;
}
add_filter( 'the_content', 'do_blocks', 10 ); // BEFORE do_shortcode()
add_filter( 'the_content', 'do_blocks', 10 ); // BEFORE do_shortcode().

/**
* Registers common scripts to be used as dependencies of the editor and plugins.
Expand Down Expand Up @@ -173,9 +174,9 @@ function gutenberg_register_scripts() {
* @since 0.1.0
*/
function gutenberg_add_edit_links_filters() {
// For hierarchical post types
// For hierarchical post types.
add_filter( 'page_row_actions', 'gutenberg_add_edit_links', 10, 2 );
// For non-hierarchical post types
// For non-hierarchical post types.
add_filter( 'post_row_actions', 'gutenberg_add_edit_links', 10, 2 );
}
add_action( 'admin_init', 'gutenberg_add_edit_links_filters' );
Expand All @@ -185,14 +186,18 @@ function gutenberg_add_edit_links_filters() {
* the Gutenberg editor.
*
* @since 0.1.0
*
* @param array $actions Post actions.
* @param array $post Edited post.
*
* @return array Updated post actions.
*/
function gutenberg_add_edit_links( $actions, $post ) {
$can_edit_post = current_user_can( 'edit_post', $post->ID );
$title = _draft_or_post_title( $post->ID );

if ( $can_edit_post && 'trash' !== $post->post_status ) {
// Build the Gutenberg edit action. See also:
// WP_Posts_List_Table::handle_row_actions()
// Build the Gutenberg edit action. See also: WP_Posts_List_Table::handle_row_actions().
$gutenberg_url = menu_page_url( 'gutenberg', false );
$gutenberg_action = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
Expand All @@ -208,7 +213,9 @@ function gutenberg_add_edit_links( $actions, $post ) {
$edit_offset = array_search( 'edit', array_keys( $actions ), true );
$actions = array_merge(
array_slice( $actions, 0, $edit_offset + 1 ),
array( 'gutenberg hide-if-no-js' => $gutenberg_action ),
array(
'gutenberg hide-if-no-js' => $gutenberg_action,
),
array_slice( $actions, $edit_offset + 1 )
);
}
Expand All @@ -221,6 +228,8 @@ function gutenberg_add_edit_links( $actions, $post ) {
*
* @since 0.1.0
*
* @param string $domain Translation domain.
*
* @return array
*/
function gutenberg_get_jed_locale_data( $domain ) {
Expand All @@ -232,10 +241,10 @@ function gutenberg_get_jed_locale_data( $domain ) {
$domain => array(
'' => array(
'domain' => $domain,
'lang' => is_admin() ? get_user_locale() : get_locale()
)
)
)
'lang' => is_admin() ? get_user_locale() : get_locale(),
),
),
),
);

if ( ! empty( $translations->headers['Plural-Forms'] ) ) {
Expand Down Expand Up @@ -268,16 +277,16 @@ function gutenberg_scripts_and_styles( $hook ) {
* Scripts
*/

// The editor code itself
// The editor code itself.
wp_enqueue_script(
'wp-editor',
plugins_url( 'editor/build/index.js', __FILE__ ),
array( 'wp-i18n', 'wp-blocks', 'wp-element' ),
filemtime( plugin_dir_path( __FILE__ ) . 'editor/build/index.js' ),
true // $in_footer
true // enqueue in the footer.
);

// Load an actual post if an ID is specified
// Load an actual post if an ID is specified.
$post_to_edit = null;
if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) {
$request = new WP_REST_Request(
Expand All @@ -304,15 +313,15 @@ function gutenberg_scripts_and_styles( $hook ) {
);
}

// Prepare Jed locale data
// Prepare Jed locale data.
$locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
wp_add_inline_script(
'wp-editor',
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );',
'before'
);

// Initialize the editor
// Initialize the editor.
wp_add_inline_script( 'wp-editor', 'wp.editor.createEditorInstance( \'editor\', _wpGutenbergPost );' );

/**
Expand Down Expand Up @@ -361,11 +370,3 @@ function the_gutenberg_project() {
</div>
<?php
}

function render_html_block( $attributes ) {
return 'THIS IS AWESOME';
}

register_block( 'core/html', array(
'render' => 'render_html_block'
) );
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./phpunit/</directory>
<directory suffix="-test.php">./phpunit/</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
<?php
/**
* Dynamic blocks rendering Test
*
* @package Gutenberg
*/

class DynamicBlocksRenderTest extends WP_UnitTestCase {
/**
* Test do_blocks
*/
class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
/**
* @expectedIncorrectUsage register_block
* Dummy block rendering function.
*
* @param array $attributes Block attributes.
*
* @return string Block output.
*/
function test_invalid_non_string_slugs() {
register_block( 1, array() );
function render_dummy_block( $attributes ) {
return $attributes['value'];
}

/**
* @expectedIncorrectUsage register_block
* Successfull dynamic block rendering
*/
function test_invalid_slugs_without_namespace() {
register_block( 'text', array() );
}

function render_dummy_block( $attributes ) {
return $attributes[ 'value' ];
}

function test_register_block() {
$settings = array( 'render' => [ $this, 'render_dummy_block' ] );
$settings = array(
'render' => array(
$this,
'render_dummy_block',
),
);
register_block( 'core/dummy', $settings );
$post_content =
'before' .
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
<?php
/**
* Blocks registration Tests
*
* @package Gutenberg
*/

class RegistrationTest extends WP_UnitTestCase {
/**
* Test register_block
*/
class Registration_Test extends WP_UnitTestCase {
/**
* The block slug should be a string
*
* @expectedIncorrectUsage register_block
*/
function test_invalid_non_string_slugs() {
register_block( 1, array() );
}

/**
* The block slug should have a namespace
*
* @expectedIncorrectUsage register_block
*/
function test_invalid_slugs_without_namespace() {
register_block( 'text', array() );
}

/**
* Successfull block registration
*/
function test_register_block() {
$settings = array( 'icon' => 'text' );
$settings = array(
'icon' => 'text',
);
$updated_settings = register_block( 'core/text', $settings );
$this->assertEquals( $updated_settings, array(
'icon' => 'text',
'slug' => 'core/text'
'slug' => 'core/text',
) );
unregister_block( 'core/text' );
}
Expand Down

0 comments on commit f0cd860

Please sign in to comment.