Skip to content
53 changes: 52 additions & 1 deletion packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,58 @@
* @since 9.0.0
*/
class Blocks {
/**
* Get CSS classes for a block.
*
* @since 9.0.0
*
* @param string $slug Block slug.
* @param array $attr Block attributes.
* @param array $extra Potential extra classes you may want to provide.
*
* @return string $classes List of CSS classes for a block.
*/
public static function classes( $slug, $attr, $extra = array() ) {
if ( empty( $slug ) ) {
return '';
}

}
// Basic block name class.
$classes = array(
'wp-block-jetpack-' . $slug,
);

// Add alignment if provided.
if (
! empty( $attr['align'] )
&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true )
) {
$classes[] = 'align' . $attr['align'];
}

// Add custom classes if provided in the block editor.
if ( ! empty( $attr['className'] ) ) {
$classes[] = $attr['className'];
}

// Add any extra classes.
if ( is_array( $extra ) && ! empty( $extra ) ) {
$classes = array_merge( $classes, array_filter( $extra ) );
}

return implode( ' ', $classes );
}

/**
* Does the page return AMP content.
*
* @return bool $is_amp_request Are we on an AMP view.
*/
public static function is_amp_request() {
$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );

/** This filter is documented in 3rd-party/class.jetpack-amp-support.php */
return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
Comment thread
jeherve marked this conversation as resolved.
Resolved
}
}

16 changes: 16 additions & 0 deletions packages/blocks/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* PHPUnit bootstrap file.
*
* @package automattic/jetpack-blocks
*/

/**
* Load the composer autoloader.
*/
require_once __DIR__ . '/../../vendor/autoload.php';

/**
* Load WorDBless
*/
\WorDBless\Load::load();
87 changes: 87 additions & 0 deletions packages/blocks/tests/php/test-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Test methods from Automattic\Jetpack\Blocks
*
* @since 9.0.0
*
* @package automattic/jetpack-blocks
*/

namespace Automattic\Jetpack;

use Automattic\Jetpack\Blocks;
use PHPUnit\Framework\TestCase;

/**
* Class Test_Blocks
*/
class Test_Blocks extends TestCase {
/**
* Test the different inputs and matching output for Classes.
*
* @since 9.0.0
*
* @covers Automattic\Jetpack\Blocks::classes
*/
public function test_block_classes() {
$block_name = 'foo';
$attr = array(
'bar' => 'baz',
'align' => 'wide',
'className' => 'editorclass',
);
$extra = array( 'extraclass' );

$block_classes = Blocks::classes( $block_name, $attr, $extra );

$this->assertContains( 'wp-block-jetpack-foo', $block_classes ); // a general class is created from the block name.
$this->assertNotContains( 'bar', $block_classes ); // The extra 'bar' attribute should be dropped.
$this->assertNotContains( 'baz', $block_classes ); // The extra 'baz' attribute should be dropped.
$this->assertNotContains( 'align ', $block_classes ); // The align attribute should only be used to create a new attribute.
$this->assertNotContains( 'className', $block_classes ); // The className attribute should be dropped, only the editorclass value should remain.
$this->assertContains( 'alignwide', $block_classes ); // an alignment class is created.
$this->assertContains( 'editorclass', $block_classes ); // className classes are passed.
$this->assertContains( 'extraclass', $block_classes ); // Extra class remains.
}
Comment thread
jeherve marked this conversation as resolved.
Resolved

/**
* Test for invalid alignment values.
*
* @since 9.0.0
*
* @covers Automattic\Jetpack\Blocks::classes
*/
public function test_block_classes_invalid_align() {
$attr = array( 'align' => 'test' );
$block_classes = Blocks::classes( 'test', $attr );

$this->assertNotContains( 'aligntest', $block_classes );
}

/**
* Test whether we can detect an AMP view.
*
* @since 9.0.0
*
* @covers Automattic\Jetpack\Blocks::is_amp_request
*/
public function test_is_amp_request() {
add_filter( 'jetpack_is_amp_request', '__return_true' );
try {
$this->assertTrue( Blocks::is_amp_request() );
} finally {
remove_filter( 'jetpack_is_amp_request', '__return_true' );
}
}

/**
* Test whether we can detect an AMP view.
*
* @since 9.0.0
*
* @covers Automattic\Jetpack\Blocks::is_amp_request
*/
public function test_is_not_amp_request() {
$this->assertFalse( Blocks::is_amp_request() );
}
}