Passed
Push — master ( 6f0a6d...90a9ca )
by
unknown
09:41
created

MonsterInsights_Blocks   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 137
rs 10
c 1
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register_blocks() 0 27 1
A add_default_values() 0 8 1
A set() 0 2 1
A popular_posts_inline_output() 0 5 1
A popular_posts_widget_output() 0 6 1
A __construct() 0 7 2
1
<?php
2
/**
3
 * Gutenberg Blocks registration class.
4
 *
5
 * @since 7.13.9
6
 *
7
 * @package MonsterInsights
8
 */
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Gutenberg Blocks registration class.
17
 *
18
 * @since 7.13.0
19
 */
20
class MonsterInsights_Blocks {
21
22
	/**
23
	 * Holds the class object.
24
	 *
25
	 * @since 7.13.0
26
	 *
27
	 * @var object
28
	 */
29
	public static $instance;
30
31
	/**
32
	 * Path to the file.
33
	 *
34
	 * @since 7.13.0
35
	 *
36
	 * @var string
37
	 */
38
	public $file = __FILE__;
39
40
	/**
41
	 * Holds the base class object.
42
	 *
43
	 * @since 7.13.0
44
	 *
45
	 * @var object
46
	 */
47
	public $base;
48
49
	/**
50
	 * Primary class constructor.
51
	 *
52
	 * @since 7.13.0
53
	 */
54
	public function __construct() {
55
56
		if ( function_exists( 'register_block_type' ) ) {
57
58
			// Set our object.
59
			$this->set();
60
			$this->register_blocks();
61
		}
62
63
	}
64
65
	/**
66
	 * Sets our object instance and base class instance.
67
	 *
68
	 * @since 7.13.0
69
	 */
70
	public function set() {
71
		self::$instance = $this;
72
	}
73
74
	/**
75
	 * Register MonsterInsights Gutenberg blocks on the backend.
76
	 *
77
	 * @since 7.13.0
78
	 */
79
	public function register_blocks() {
80
		register_block_type(
81
			'monsterinsights/popular-posts-inline',
82
			array(
83
				'attributes'      => array(
84
					'slug'        => array(
85
						'type' => 'string',
86
					),
87
					'followrules' => array(
88
						'type' => 'boolean',
89
					),
90
				),
91
				'render_callback' => array( $this, 'popular_posts_inline_output' ),
92
			)
93
		);
94
		register_block_type(
95
			'monsterinsights/popular-posts-widget',
96
			array(
97
				'attributes'      => array(
98
					'slug'        => array(
99
						'type' => 'string',
100
					),
101
					'followrules' => array(
102
						'type' => 'boolean',
103
					),
104
				),
105
				'render_callback' => array( $this, 'popular_posts_widget_output' ),
106
			)
107
		);
108
	}
109
110
	/**
111
	 * Get form HTML to display in a MonsterInsights Gutenberg block.
112
	 *
113
	 * @param array $atts Attributes passed by MonsterInsights Gutenberg block.
114
	 *
115
	 * @return string
116
	 * @since 7.13.0
117
	 *
118
	 */
119
	public function popular_posts_inline_output( $atts ) {
120
121
		$output = MonsterInsights_Popular_Posts_Inline()->shortcode_output( $atts );
122
123
		return $output;
124
	}
125
126
	/**
127
	 * Get form HTML to display in a MonsterInsights Gutenberg block.
128
	 *
129
	 * @param array $atts Attributes passed by MonsterInsights Gutenberg block.
130
	 *
131
	 * @return string
132
	 * @since 7.13.0
133
	 */
134
	public function popular_posts_widget_output( $atts ) {
135
136
		$atts   = $this->add_default_values( $atts );
137
		$output = MonsterInsights_Popular_Posts_Widget()->shortcode_output( $atts );
138
139
		return $output;
140
	}
141
142
	/**
143
	 * This ensures that what is displayed as default in the Gutenberg block is reflected in the output.
144
	 *
145
	 * @param array $atts The attributes from Gutenberg.
146
	 *
147
	 * @return array
148
	 */
149
	private function add_default_values( $atts ) {
150
151
		$default_values = array(
152
			'columns'      => 1,
153
			'widget_title' => false,
154
		);
155
156
		return wp_parse_args( $atts, $default_values );
157
158
	}
159
160
}
161
162
new MonsterInsights_Blocks();
163