diff --git a/packages/blocks/src/class-blocks.php b/packages/blocks/src/class-blocks.php index 208707af45008..69b0d07b11172 100644 --- a/packages/blocks/src/class-blocks.php +++ b/packages/blocks/src/class-blocks.php @@ -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 ); + } +} diff --git a/packages/blocks/tests/php/bootstrap.php b/packages/blocks/tests/php/bootstrap.php new file mode 100644 index 0000000000000..9a50b9a7a6730 --- /dev/null +++ b/packages/blocks/tests/php/bootstrap.php @@ -0,0 +1,16 @@ + '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. + } + + /** + * 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() ); + } +}