Completed
Push — master ( 14813c...0a2c27 )
by Virginia
08:28
created

gutenberg.php ➔ add_gutenberg_compatible_body_class()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 6
nop 1
dl 0
loc 16
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * LSX functions to make theme Gutenberg compatible
4
 *
5
 * @package    lsx
6
 * @subpackage Gutenberg
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * Enqueue Admin styles on admin area
15
 */
16
function load_gutenberg_admin_style() {
17
	wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/assets/css/admin/gutenberg-admin.css', false, '1.0.0' );
18
}
19
add_action( 'admin_enqueue_scripts', 'load_gutenberg_admin_style' );
20
21
// Gutenberg Compatibility.
22
require_once( get_template_directory() . '/lib/theme-support.php' );
23
24
/**
25
 * Add custom class for Gutenberg Compatible template
26
 */
27
function add_gutenberg_compatible_body_class( $classes ) {
28
	// if ( ! is_home() && ! is_front_page() ).
29
	if ( is_page() || is_page_template() || is_single() )
30
		$classes[] = 'gutenberg-compatible-template';
31
32
	// Add a class if the page is using the Content and Media block.
33
	$post = get_post();
34
	if ( function_exists( 'has_blocks' ) && isset( $post->post_content ) && has_blocks( $post->post_content ) && ( ! is_search() ) && ( ! is_archive() ) ) {
35
		$blocks = parse_blocks( $post->post_content );
36
37
		if ( 'core/media-text' === $blocks[0]['blockName'] ) {
38
			$classes[] = 'has-block-media-text';
39
		}
40
	}
41
	return $classes;
42
}
43
44
add_filter( 'body_class', __NAMESPACE__ . '\add_gutenberg_compatible_body_class' );
45
46
// Add custom class for templates that are using the Gutenberg editor.
47
add_action('body_class', function( $classes ) {
48
	if ( function_exists( 'has_blocks' ) && has_blocks( get_the_ID() ) && ( ! is_search() ) && ( ! is_archive() ) )
49
		$classes[] = 'using-gutenberg';
50
	return $classes;
51
});
52