add_gutenberg_compatible_body_class()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 10
nc 10
nop 1
dl 0
loc 18
rs 7.3166
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' );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string[] expected by parameter $deps of wp_enqueue_style(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
	wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/assets/css/admin/gutenberg-admin.css', /** @scrutinizer ignore-type */ false, '1.0.0' );
Loading history...
18
}
19
add_action( 'admin_enqueue_scripts', 'load_gutenberg_admin_style' );
20
21
// Gutenberg Compatibility.
22
require get_template_directory() . '/lib/theme-support.php';
23
24
/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$classes" missing
Loading history...
25
 * Add custom class for Gutenberg Compatible template
26
 */
27
function add_gutenberg_compatible_body_class( $classes ) {
28
	// if ( ! is_home() && ! is_front_page() ).
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
	if ( is_page() || is_page_template() || is_single() )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
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
		if ( 'core/cover' === $blocks[0]['blockName'] ) {
41
			$classes[] = 'has-block-cover';
42
		}
43
	}
44
	return $classes;
45
}
46
47
add_filter( 'body_class', __NAMESPACE__ . '\add_gutenberg_compatible_body_class' );
48
49
// Add custom class for templates that are using the Gutenberg editor.
50
add_action('body_class', function( $classes ) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
51
	if ( function_exists( 'has_blocks' ) && has_blocks( get_the_ID() ) && ( ( is_singular( 'post' ) || is_page() ) ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
52
		$classes[] = 'using-gutenberg';
53
	return $classes;
54
});
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
55
56
/**
57
 * Removes the default LSX banner if there is a page that is using blocks. (Only works if LSX banners is not turned on)
58
 *
59
 * @return void
60
 */
61
function remove_lsx_page_banner_when_using_blocks() {
62
	if ( function_exists( 'has_blocks' ) && ( ! class_exists( 'LSX_Banners' ) ) ) {
63
		add_filter( 'lsx_page_banner_disable', '__return_true' );
64
	}
65
}
66
add_filter( 'init', 'remove_lsx_page_banner_when_using_blocks' );
67