Completed
Push — feature/reorg ( a598b4...783db3 )
by
unknown
134:01 queued 124:04
created

dialogue.php ➔ get_participant_slug()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Dialogue Block.
4
 *
5
 * @since 9.3.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Dialogue;
11
12
use Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'dialogue';
16
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
17
18
/**
19
 * Registers the block for use in Gutenberg
20
 * This is done via an action so that we can disable
21
 * registration if we need to.
22
 */
23 View Code Duplication
function register_block() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
	$deprecated = function_exists( 'gutenberg_get_post_from_context' );
25
	$uses       = $deprecated ? 'context' : 'uses_context';
26
	Blocks::jetpack_register_block(
27
		BLOCK_NAME,
28
		array(
29
			'render_callback' => __NAMESPACE__ . '\render_block',
30
			$uses             => array(
31
				'jetpack/conversation-participants',
32
				'jetpack/conversation-showTimestamps',
33
			),
34
		)
35
	);
36
}
37
add_action( 'init', __NAMESPACE__ . '\register_block' );
38
39
/**
40
 * Helper function to filter dialogue content,
41
 * in order to provide a safe markup.
42
 *
43
 * @param string $content Dialogue content.
44
 * @return string Safe dialgue content markup.
45
 */
46
function filter_content( $content ) {
47
	if ( empty( $content ) ) {
48
		return '';
49
	}
50
51
	return wp_kses(
52
		$content,
53
		array(
54
			'small'  => true,
55
			'strong' => true,
56
			'b'      => true,
57
			'em'     => true,
58
		)
59
	);
60
}
61
62
/**
63
 * Helper to check dialogue block attributes.
64
 *
65
 * @param array  $attrs Dialogue block attributes.
66
 * @param object $block Block object data.
67
 * @return array Checked block attribues.
68
 */
69
function check_dialogue_attrs( $attrs, $block ) {
70
	return array(
71
		'slug'           => isset( $attrs['participantSlug'] ) ? $attrs['participantSlug'] : null,
72
		'label'          => isset( $attrs['participant'] ) ? $attrs['participant'] : null,
73
		'timestamp'      => isset( $attrs['timestamp'] ) ? esc_attr( $attrs['timestamp'] ) : '00:00',
74
		'show_timestamp' => isset( $block->context['jetpack/conversation-showTimestamps'] ),
75
		'content'        => ! empty( $attrs['content'] ) ? filter_content( $attrs['content'] ) : '',
76
	);
77
}
78
/**
79
 * Return participant list.
80
 * It will try to pick them up from the block context.
81
 * Otherwise, it will return the default participants list.
82
 *
83
 * @param object $block Block object data.
84
 * @param array  $default Default conversation data.
85
 * @return array dialogue participants list.
86
 */
87
function get_participantes_list( $block, $default ) {
88
	return ! empty( $block->context['jetpack/conversation-participants'] )
89
		? $block->context['jetpack/conversation-participants']
90
		: $default['list'];
91
}
92
93
/**
94
 * Return participan slug,
95
 * dependng on the slug and label fo the dialogue block,
96
 * and default slug defined in the conversation data.
97
 *
98
 * @param array  $attrs Checked dialogue attributes array.
99
 * @param object $block Block object data.
100
 * @param array  $default Default conversation data.
101
 * @return array Dialoge slug if it's defined. Otherwise, default conversation slug.
102
 */
103
function get_participant_slug( $attrs, $block, $default ) {
104
	return ! $attrs['slug'] && ! $attrs['label']
105
		? $default['slug']
106
		: $attrs['slug'];
107
}
108
109
/**
110
 * Helper function to pick the dialogie participant object.
111
 *
112
 * @param array  $participants Dialogue participants.
113
 * @param string $slug participant slug.
114
 * @return array Dialogue participant when exists. Otherwise, False.
115
 */
116
function get_current_participant( $participants, $slug ) {
117
	// Participant names map.
118
	$participant_names_map = array();
119
	foreach ( $participants as $participant ) {
120
		$participant_names_map[ $participant['participantSlug'] ] = $participant;
121
	}
122
123
	return isset( $participant_names_map[ $slug ] )
124
		? $participant_names_map[ $slug ]
125
		: false;
126
}
127
128
/**
129
 * Helper function to get the participant name.
130
 *
131
 * @param array  $participants Dialogue participants.
132
 * @param string $slug participant slug.
133
 * @param array  $attrs checked dialogue block atteributes.
134
 * @return string Participant name.
135
 */
136
function get_participant_name( $participants, $slug, $attrs ) {
137
	// Try to pick up participant data from context.
138
	$participant = get_current_participant( $participants, $slug );
139
140
	return isset( $participant['participant'] )
141
		? esc_attr( $participant['participant'] )
142
		: $attrs['label'];
143
}
144
145
/**
146
 * Helper function to build CSS class,
147
 * for the given participant.
148
 *
149
 * @param array  $participants Dialogue participants.
150
 * @param string $slug participant slug.
151
 * @param array  $attrs checked dialogue block atteributes.
152
 * @param string $css_class Base dialogue block CSS classname.
153
 * @return string Participant CSS classnames.
154
 */
155
function build_participant_css_classes( $participants, $slug, $attrs, $css_class ) {
156
	$is_custom_speaker   = $attrs['label'] && ! $attrs['slug'];
157
	$current_participant = get_current_participant( $participants, $slug );
158
159
	$participant_has_bold_style = $is_custom_speaker && isset( $attrs['hasBoldStyle'] )
160
		? $attrs['hasBoldStyle']
161
		: (
162
			isset( $current_participant['hasBoldStyle'] )
163
				? $current_participant['hasBoldStyle']
164
				: false
165
		);
166
167
	$participant_has_italic_style = $is_custom_speaker && isset( $attrs['hasItalicStyle'] )
168
		? $attrs['hasItalicStyle']
169
		: (
170
			isset( $current_participant['hasItalicStyle'] )
171
				? $current_participant['hasItalicStyle']
172
				: false
173
		);
174
175
	$participant_has_uppercase_style = $is_custom_speaker && isset( $attrs['hasUppercaseStyle'] )
176
		? $attrs['hasUppercaseStyle']
177
		: (
178
			isset( $current_participant['hasUppercaseStyle'] )
179
				? $current_participant['hasUppercaseStyle']
180
				: false
181
		);
182
183
	$participant_css_classes = array( $css_class . '__participant' );
184
	if ( $participant_has_bold_style ) {
185
		array_push( $participant_css_classes, 'has-bold-style' );
186
	}
187
188
	if ( $participant_has_italic_style ) {
189
		array_push( $participant_css_classes, 'has-italic-style' );
190
	}
191
192
	if ( $participant_has_uppercase_style ) {
193
		array_push( $participant_css_classes, 'has-uppercase-style' );
194
	}
195
196
	return implode( ' ', $participant_css_classes );
197
}
198
199
/**
200
 * Dialogue block registration/dependency declaration.
201
 *
202
 * @param array  $dialogue_attrs Array containing the Dialogue block attributes.
203
 * @param string $block_content String containing the Dialogue block content.
204
 * @param object $block Block object data.
205
 *
206
 * @return string
207
 */
208
function render_block( $dialogue_attrs, $block_content, $block ) {
209
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
210
211
	// Pick up conversation data from context.
212
	$default_participants = json_decode(
213
		// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
214
		file_get_contents( JETPACK__PLUGIN_DIR . 'extensions/blocks/conversation/participants.json' ),
215
		true
216
	);
217
218
	// Dialogue Attributes.
219
	$attrs = check_dialogue_attrs( $dialogue_attrs, $block );
220
221
	// Conversation/Dialogue data.
222
	$participants     = get_participantes_list( $block, $default_participants );
223
	$participant_slug = get_participant_slug( $attrs, $block, $default_participants );
224
	$participant_name = get_participant_name( $participants, $participant_slug, $attrs );
225
226
	// CSS classes and inline styles.
227
	$css_classname           = Blocks::classes( FEATURE_NAME, $dialogue_attrs );
228
	$participant_css_classes = build_participant_css_classes( $participants, $participant_slug, $attrs, $css_classname );
229
230
	// Markup.
231
	return '<div class="' . $css_classname . '" >' .
232
		'<div class="' . $css_classname . '__meta">' .
233
			'<div class="' . $participant_css_classes . '">' .
234
				$participant_name .
235
			'</div>' .
236
			( $attrs['show_timestamp']
237
				? '<div class="' . $css_classname . '__timestamp">' .
238
					$attrs['timestamp'] .
239
				'</div>'
240
				: ''
241
			) .
242
		'</div>' .
243
		'<div>' . $attrs['content'] . '</div>' .
244
	'</div>';
245
}
246