Completed
Push — upgrade/eslint-5 ( 071910...9f2520 )
by
unknown
07:52
created

Jetpack_Contact_Info_Block::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class Jetpack_Contact_Info_Block
4
 *
5
 * @package Jetpack
6
 */
7
8
/**
9
 * Helper class that lets us add schema attributes dynamically because they are not something that is store with the content.
10
 * Due to the limitations of wp_kses.
11
 *
12
 * @since 7.1.0
13
 */
14
class Jetpack_Contact_Info_Block {
15
16
	/**
17
	 * Adds contact info schema attributes.
18
	 *
19
	 * @param array  $attr    Array containing the contact info block attributes.
20
	 * @param string $content String containing the contact info block content.
21
	 *
22
	 * @return string
23
	 */
24
	public static function render( $attr, $content ) {
25
		Jetpack_Gutenberg::load_assets_as_required( 'contact-info' );
26
		return str_replace(
27
			'class="wp-block-jetpack-contact-info', // Closing " intentionally ommited to that the user can also add the className as expected.
28
			'itemprop="location" itemscope itemtype="http://schema.org/Organization" class="wp-block-jetpack-contact-info',
29
			$content
30
		);
31
	}
32
33
	/**
34
	 * Adds address schema attributes.
35
	 *
36
	 * @param array  $attr    Array containing the address block attributes.
37
	 * @param string $content String containing the address block content.
38
	 *
39
	 * @return string
40
	 */
41
	public static function render_address( $attr, $content ) {
42
		// Returns empty content if the only attribute set is linkToGoogleMaps.
43
		if ( ! self::has_attributes( $attr, array( 'linkToGoogleMaps', 'className' ) ) ) {
44
			return '';
45
		}
46
		$find    = array(
47
			'class="wp-block-jetpack-address"',
48
			'class="jetpack-address__address',
49
			// Closing " left out on purpose - there are multiple address fields and they all need to be updated with the same itemprop.
50
			'class="jetpack-address__region"',
51
			'class="jetpack-address__city"',
52
			'class="jetpack-address__postal"',
53
			'class="jetpack-address__country"',
54
		);
55
		$replace = array(
56
			'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="wp-block-jetpack-address" ',
57
			'itemprop="streetAddress" class="jetpack-address__address', // Closing " left out on purpose.
58
			'itemprop="addressRegion" class="jetpack-address__region"',
59
			'itemprop="addressLocality" class="jetpack-address__city"',
60
			'itemprop="postalCode" class="jetpack-address__postal"',
61
			'itemprop="addressCountry" class="jetpack-address__country"',
62
		);
63
64
		return str_replace( $find, $replace, $content );
65
	}
66
67
	/**
68
	 * Helper function that lets us determine if a block has any valid attributes.
69
	 *
70
	 * @param array $attr Array containing the block attributes.
71
	 * @param array $omit Array containing the block attributes that we ignore.
72
	 *
73
	 * @return string
74
	 */
75
	public static function has_attributes( $attr, $omit = array() ) {
76
		foreach ( $attr as $attribute => $value ) {
77
			if ( ! in_array( $attribute, $omit, true ) && ! empty( $value ) ) {
78
				return true;
79
			}
80
		}
81
82
		return false;
83
	}
84
85
	/**
86
	 * Adds email schema attributes.
87
	 *
88
	 * @param array  $attr    Array containing the email block attributes.
89
	 * @param string $content String containing the email block content.
90
	 *
91
	 * @return string
92
	 */
93
	public static function render_email( $attr, $content ) {
94
		$content = self::has_attributes( $attr, array( 'className' ) ) ?
95
			str_replace( 'href="mailto:', 'itemprop="email" href="mailto:', $content ) :
96
			'';
97
		return $content;
98
	}
99
100
	/**
101
	 * Adds phone schema attributes.
102
	 *
103
	 * @param array  $attr    Array containing the phone block attributes.
104
	 * @param string $content String containing the phone block content.
105
	 *
106
	 * @return string
107
	 */
108
	public static function render_phone( $attr, $content ) {
109
		$content = self::has_attributes( $attr, array( 'className' ) ) ?
110
			str_replace( 'href="tel:', 'itemprop="telephone" href="tel:', $content ) :
111
			'';
112
		return $content;
113
	}
114
}
115