-
Notifications
You must be signed in to change notification settings - Fork 20
/
class-splittwocolumns.php
176 lines (153 loc) · 5.85 KB
/
class-splittwocolumns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Split Two Columns block class
*
* @package P4GBKS
* @since 0.1
*/
namespace P4GBKS\Blocks;
/**
* Class SplitTwoColumns
*
* @package P4BKS
* @since 0.1
*/
class SplitTwoColumns extends Base_Block {
/**
* Block name.
*
* @const string BLOCK_NAME.
*/
public const BLOCK_NAME = 'split-two-columns';
/**
* Block version, update when changing attributes
*
* @var int VERSION.
*/
private const VERSION = 3;
private const CONFIG_PATH = P4GBKS_PLUGIN_DIR . '/assets/src/blocks/Splittwocolumns';
/**
* @var array Block attributes.
*/
private const ATTRIBUTES = [
'version' => [ 'type' => 'integer' ],
'select_issue' => [ 'type' => 'integer' ],
'title' => [ 'type' => 'string' ],
'issue_description' => [ 'type' => 'string' ],
'issue_link_text' => [ 'type' => 'string' ],
'issue_link_path' => [ 'type' => 'string' ],
'issue_image_id' => [ 'type' => 'integer' ],
'issue_image_src' => [ 'type' => 'string' ],
'issue_image_srcset' => [ 'type' => 'string' ],
'issue_image_title' => [ 'type' => 'string' ],
'focus_issue_image' => [ 'type' => 'string' ],
'select_tag' => [ 'type' => 'integer' ],
'tag_name' => [ 'type' => 'string' ],
'tag_description' => [ 'type' => 'string' ],
'tag_link' => [ 'type' => 'string' ],
'button_text' => [ 'type' => 'string' ],
'button_link' => [ 'type' => 'string' ],
'tag_image_id' => [ 'type' => 'integer' ],
'tag_image_src' => [ 'type' => 'string' ],
'tag_image_srcset' => [ 'type' => 'string' ],
'tag_image_title' => [ 'type' => 'string' ],
'focus_tag_image' => [ 'type' => 'string' ],
'edited' => [ 'type' => 'object' ],
];
/**
* SplitTwoColumns constructor.
*/
public function __construct() {
// Registering meta field to make it appear in REST API.
\register_meta(
'term',
'tag_attachment_id',
[
'show_in_rest' => true,
'type' => 'integer',
'single' => true,
]
);
\register_block_type(
self::CONFIG_PATH,
[
'editor_script' => 'planet4-blocks',
]
);
}
/**
* Required by the `Base_Block` class.
*
* @param array $fields Unused, required by the abstract function.
*/
public function prepare_data( $fields ): array {
return [];
}
/**
* Migrate data from one version to the last.
*
* @param array $fields This is the array of fields of this block.
*
* @return array The data updated to the current version.
*/
public static function update_data( array $fields ): array {
$version = empty( $fields['version'] ) ? 0 : (int) $fields['version'];
if ( version_compare( $version, self::VERSION, '>=' ) ) {
return $fields;
}
$issue_id = (int) ( $fields['select_issue'] ?? null );
$tag_id = (int) ( $fields['select_tag'] ?? null );
$issue_image_id = (int) ( $fields['issue_image'] ?? $fields['issue_image_id'] ?? get_post_thumbnail_id( $issue_id ) ?? 0 );
$tag_image_id = (int) ( $fields['tag_image'] ?? $fields['tag_image_id'] ?? get_term_meta( $tag_id, 'tag_attachment_id', true ) ?? 0 );
// Registering fields edition status.
$edited = [
'title' => ! empty( $fields['title'] ),
'issue_description' => ! empty( $fields['issue_description'] ),
'issue_link_text' => ! empty( $fields['issue_link_text'] ),
'tag_description' => ! empty( $fields['tag_description'] ),
'button_text' => ! empty( $fields['button_text'] ),
'issue_image_id' => null !== $issue_image_id,
'tag_image_id' => null !== $tag_image_id,
];
$fields = array_filter( $fields );
$fields['issue_image_src'] = $issue_image_id ? wp_get_attachment_url( $issue_image_id ) : '';
$fields['issue_image_title'] = $issue_image_id ? get_post_meta( $issue_image_id, '_wp_attachment_image_alt', true ) : '';
$fields['issue_image_srcset'] = $issue_image_id ? wp_get_attachment_image_srcset( $issue_image_id, 'large' ) : '';
$fields['tag_image_src'] = $tag_image_id ? wp_get_attachment_url( $tag_image_id ) : '';
$fields['tag_img_srcset'] = $tag_image_id ? wp_get_attachment_image_srcset( $tag_image_id, 'large' ) : '';
$fields['tag_image_title'] = $tag_image_id ? get_post_meta( $tag_image_id, '_wp_attachment_image_alt', true ) : '';
$fields['tag_image_srcset'] = $tag_image_id ? wp_get_attachment_image_srcset( $tag_image_id, 'large' ) : '';
if ( $issue_id ) {
$issue_meta_data = get_post_meta( $issue_id );
$fields['title'] = $fields['title'] ?? $issue_meta_data['p4_title'][0] ?? get_the_title( $issue_id );
$fields['issue_description'] = wp_trim_words( $fields['issue_description'] ?? $issue_meta_data['p4_description'][0] ?? '', 25 );
$fields['issue_link_path'] = $fields['issue_link_path'] ?? get_permalink( $issue_id );
$fields['issue_link_text'] = $fields['issue_link_text'] ?? __( 'Learn more about this issue', 'planet4-blocks' );
}
if ( $tag_id ) {
$tag = get_term( $tag_id );
if ( $tag instanceof \WP_Term ) {
$fields['tag_name'] = $fields['tag_name'] ?? $tag->name ?? '';
$fields['tag_link'] = get_tag_link( $tag );
$fields['tag_description'] = wp_trim_words( $fields['tag_description'] ?? $tag->description ?? '', 25 );
}
}
$fields['button_text'] = $fields['button_text'] ?? __( 'Get involved', 'planet4-blocks' );
$fields['button_link'] = $fields['button_link'] ?? $fields['tag_link'] ?? '';
// Filter allowed properties.
$updated = array_filter(
$fields,
static function ( $key ) {
return isset( self::ATTRIBUTES[ $key ] ) || 'className' === $key;
},
\ARRAY_FILTER_USE_KEY
);
$updated['version'] = self::VERSION;
$updated['select_issue'] = $issue_id;
$updated['select_tag'] = $tag_id;
$updated['issue_image_id'] = $issue_image_id;
$updated['tag_image_id'] = $tag_image_id;
$updated['edited'] = $edited;
return $updated;
}
}