Completed
Push — develop ( 799090...17cc41 )
by
unknown
15:43
created

GravityView_Theme_Hooks_GeneratePress   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A detect_views_in_block_content() 0 17 5
1
<?php
2
/**
3
 * Add support for GeneratePress theme
4
 *
5
 * @file      class-gravityview-theme-hooks-generatepress.php
6
 * @since     2.10.3
7
 * @license   GPL2+
8
 * @author    GravityView <[email protected]>
9
 * @link      http://gravityview.co
10
 * @copyright Copyright 2021, Katz Web Services, Inc.
11
 *
12
 * @package   GravityView
13
 */
14
15
/**
16
 * @inheritDoc
17
 * @since 2.10.3
18
 */
19
class GravityView_Theme_Hooks_GeneratePress extends GravityView_Plugin_and_Theme_Hooks {
20
	/**
21
	 * @inheritDoc
22
	 * @since 2.10.3
23
	 */
24
	public function __construct() {
25
		if ( 'generatepress' !== wp_get_theme()->__get( 'template' ) ) {
26
			return;
27
		}
28
29
		parent::__construct();
30
31
		add_filter( 'render_block', array( $this, 'detect_views_in_block_content' ) );
32
	}
33
34
	/**
35
	 * Detect GV Views in parsed Gutenberg block content
36
	 *
37
	 * @since 2.10.3
38
	 *
39
	 * @see   \WP_Block::render()
40
	 *
41
	 * @param string $block_content Gutenberg block content
42
	 *
43
	 * @return false|string
44
	 */
45
	public function detect_views_in_block_content( $block_content ) {
46
		if ( ! class_exists( 'GV\View_Collection' ) || ! class_exists( 'GV\View' ) ) {
47
			return $block_content;
48
		}
49
50
		$gv_view_data = GravityView_View_Data::getInstance();
51
52
		$views = \GV\View_Collection::from_content( $block_content );
53
54
		foreach ( $views->all() as $view ) {
55
			if ( ! $gv_view_data->views->contains( $view->ID ) ) {
0 ignored issues
show
Bug introduced by
The method contains cannot be called on $gv_view_data->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
56
				$gv_view_data->views->add( $view );
0 ignored issues
show
Bug introduced by
The method add cannot be called on $gv_view_data->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
			}
58
		}
59
60
		return $block_content;
61
	}
62
}
63
64
new GravityView_Theme_Hooks_GeneratePress;