Passed
Push — master ( b096e4...dcfecd )
by Chris
05:13
created

add_gutenberg_compatible_body_class()   B

Complexity

Conditions 11
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 10
nc 10
nop 1
dl 0
loc 18
rs 7.3166
c 2
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
/**
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
		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 ) {
51
	if ( function_exists( 'has_blocks' ) && has_blocks( get_the_ID() ) && ( ( is_singular( 'post' ) || is_page() ) ) )
52
		$classes[] = 'using-gutenberg';
53
	return $classes;
54
});
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