Completed
Pull Request — develop (#1588)
by Zack
37:04 queued 17:05
created

Block   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A register() 0 12 2
A get_block_attributes() 0 16 3
1
<?php
2
3
namespace GV\Gutenberg\Blocks\Block;
4
5
// Exit if accessed directly
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * Block class extended by individual blocks
12
 *
13
 * @since 2.10.2
14
 */
15
abstract class Block {
16
17
	const BLOCK_NAME = 'block';
18
19
	/**
20
	 * Render block shortcode
21
	 *
22
	 * @since 2.10.2
23
	 *
24
	 * @param array $attributes
25
	 *
26
	 * @return string
27
	 */
28
	public static function render( $attributes = array() ) {
29
		return '';
30
	}
31
32
	/**
33
	 * Register block
34
	 *
35
	 * @since 2.10.2
36
	 *
37
	 * @return void
38
	 */
39
	public static function register() {
40
		$block_class = get_called_class();
41
42
		if ( ! function_exists( 'register_block_type' ) ) {
43
			return;
44
		}
45
46
		register_block_type( 'gv-blocks/' . $block_class::BLOCK_NAME, array(
47
			'render_callback' => array( $block_class, 'render' ),
48
			'attributes'      => $block_class::get_block_attributes(),
49
		) );
50
	}
51
52
	/**
53
	 * Get block attributes
54
	 *
55
	 * @see   https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/
56
	 *
57
	 * @since 2.10.2
58
	 *
59
	 * @return array
60
	 */
61
	public static function get_block_attributes() {
62
		$reflector       = new \ReflectionClass( get_called_class() );
63
		$attributes_file = dirname( $reflector->getFileName() ) . '/config.json';
64
65
		if ( ! file_exists( $attributes_file ) ) {
66
			return array();
67
		}
68
69
		try {
70
			$attributes = json_decode( file_get_contents( $attributes_file ), true );
71
		} catch ( \Exception $e ) {
72
			$attributes = array();
73
		}
74
75
		return $attributes;
76
	}
77
}
78