10
10
11
11
add_action ( 'init ' , __NAMESPACE__ . '\init ' );
12
12
13
+ /**
14
+ * Registers the block using the metadata loaded from the `block.json` file.
15
+ * Behind the scenes, it registers also all assets so they can be enqueued
16
+ * through the block editor in the corresponding context.
17
+ *
18
+ * @see https://developer.wordpress.org/reference/functions/register_block_type/
19
+ */
20
+ function init () {
21
+ register_block_type (
22
+ dirname ( dirname ( __DIR__ ) ) . '/build/site-meta-list ' ,
23
+ array (
24
+ 'render_callback ' => __NAMESPACE__ . '\render ' ,
25
+ )
26
+ );
27
+ }
28
+
13
29
/**
14
30
* Render the block content.
15
31
*
@@ -29,114 +45,74 @@ function render( $attributes, $content, $block ) {
29
45
$ meta_fields = array (
30
46
array (
31
47
'label ' => __ ( 'Author ' , 'wporg ' ),
48
+ 'type ' => 'meta ' ,
32
49
'key ' => 'author ' ,
33
50
),
34
51
array (
35
52
'label ' => __ ( 'Country ' , 'wporg ' ),
53
+ 'type ' => 'meta ' ,
36
54
'key ' => 'country ' ,
37
55
),
38
56
array (
39
- 'label ' => __ ( 'Theme ' , 'wporg ' ),
40
- 'key ' => 'theme ' ,
57
+ 'label ' => __ ( 'Category ' , 'wporg ' ),
58
+ 'type ' => 'taxonomy ' ,
59
+ 'key ' => 'category ' ,
60
+ ),
61
+ array (
62
+ 'label ' => __ ( 'Flavor ' , 'wporg ' ),
63
+ 'type ' => 'taxonomy ' ,
64
+ 'key ' => 'flavor ' ,
41
65
),
42
66
array (
43
- 'label ' => __ ( 'Launched on ' , 'wporg ' ),
44
- 'key ' => 'date_launched ' ,
67
+ 'label ' => __ ( 'Published ' , 'wporg ' ),
68
+ 'type ' => 'other ' ,
69
+ 'key ' => 'published ' ,
70
+ ),
71
+ array (
72
+ 'label ' => __ ( 'Site tags ' , 'wporg ' ),
73
+ 'type ' => 'taxonomy ' ,
74
+ 'key ' => 'post_tag ' ,
45
75
),
46
76
);
47
77
48
78
foreach ( $ meta_fields as $ field ) {
49
- $ value = get_custom_field ( $ field ['key ' ], $ block ->context ['postId ' ] );
79
+ $ value = get_value ( $ field [ ' type ' ], $ field ['key ' ], $ block ->context ['postId ' ] );
50
80
51
81
if ( ! empty ( $ value ) ) {
52
- $ list_items [] = '<dt> ' . $ field ['label ' ] . '</dt><dd > ' . esc_html ( $ value ) . '</dd > ' ;
82
+ $ list_items [] = '<li><strong> ' . $ field ['label ' ] . '</strong> <span > ' . wp_kses_post ( $ value ) . '</span></li > ' ;
53
83
}
54
84
}
55
85
56
- $ terms = get_associated_terms ( $ block ->context ['postId ' ] );
57
-
58
- if ( ! empty ( $ terms ) ) {
59
- $ list_items [] = '<dt> ' . __ ( 'Archived In ' , 'wporg ' ) . "</dt><dd> $ terms</dd> " ;
60
- }
61
-
62
- // Separate items into 2 different containers
63
- $ half = ceil ( count ( $ list_items ) / 2 );
64
- $ left = array_slice ( $ list_items , 0 , $ half );
65
- $ right = array_slice ( $ list_items , $ half );
66
-
67
86
$ wrapper_attributes = get_block_wrapper_attributes ();
68
87
return sprintf (
69
- '<div %s><dl><div> %s</div><div>%s</div></dl ></div> ' ,
88
+ '<div %s><ul> %s</ul ></div> ' ,
70
89
$ wrapper_attributes ,
71
- join ( '' , $ left ),
72
- join ( '' , $ right )
90
+ join ( '' , $ list_items )
73
91
);
74
92
}
75
93
76
94
/**
77
- * Retrieves a custom site field
95
+ * Retrieves a value from a given post.
78
96
*
79
- * @param string $key Name of meta field.
80
- * @return string
81
- */
82
- function get_custom_field ( $ key , $ post_id ) {
83
- $ values = get_post_custom_values ( $ key , $ post_id );
84
-
85
- if ( empty ( $ values ) ) {
86
- return '' ;
87
- }
88
-
89
- return $ values [0 ];
90
- }
91
-
92
- /**
93
- * Get's tags and categories associated with site.
97
+ * @param string $type Type of data source, one of meta, taxonomy, other.
98
+ * @param string $key Name of meta field or taxonomy.
99
+ * @param string $post_id ID of the post to look up.
94
100
*
95
- * @param integer $post_id
96
101
* @return string
97
102
*/
98
- function get_associated_terms ( $ post_id ) {
99
- $ terms = array ();
100
- $ tags = get_the_terms ( $ post_id , 'post_tag ' );
101
- $ categories = get_the_terms ( $ post_id , 'category ' );
102
-
103
- if ( $ tags ) {
104
- $ terms = array_merge ( $ terms , $ tags );
105
- }
106
-
107
- if ( $ categories ) {
108
- $ terms = array_merge ( $ terms , $ categories );
103
+ function get_value ( $ type , $ key , $ post_id ) {
104
+ if ( 'taxonomy ' === $ type ) {
105
+ $ value = get_the_term_list ( $ post_id , $ key , '' , ', ' );
106
+ } else if ( 'meta ' === $ type ) {
107
+ $ value = get_post_meta ( $ post_id , $ key , true );
108
+ } else if ( 'published ' === $ key ) {
109
+ // Publish date is a special case.
110
+ $ value = get_the_date ( 'F Y ' , $ post_id );
109
111
}
110
112
111
- if ( empty ( $ terms ) ) {
113
+ if ( is_wp_error ( $ value ) ) {
112
114
return '' ;
113
115
}
114
116
115
- $ links = array ();
116
-
117
- foreach ( $ terms as $ value ) {
118
- $ link = get_term_link ( $ value ->term_id , $ value ->taxonomy );
119
- if ( is_wp_error ( $ link ) ) {
120
- return $ link ;
121
- }
122
- $ links [] = "<a href=' " . esc_url ( $ link ) . "'> " . $ value ->name . '</a> ' ;
123
- }
124
-
125
- return join ( ', ' , $ links );
126
- }
127
-
128
- /**
129
- * Registers the block using the metadata loaded from the `block.json` file.
130
- * Behind the scenes, it registers also all assets so they can be enqueued
131
- * through the block editor in the corresponding context.
132
- *
133
- * @see https://developer.wordpress.org/reference/functions/register_block_type/
134
- */
135
- function init () {
136
- register_block_type (
137
- dirname ( dirname ( __DIR__ ) ) . '/build/site-meta-list ' ,
138
- array (
139
- 'render_callback ' => __NAMESPACE__ . '\render ' ,
140
- )
141
- );
117
+ return $ value ;
142
118
}
0 commit comments