Completed
Push — revert-5405-update/module-plac... ( 39c88e...712e0c )
by
unknown
21:19 queued 12:30
created

blog-verification-tools.php ➔ jetpack_verification_services()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
// Edit here to add new services
4
function jetpack_verification_services() {
5
	return array(
6
			'google' => array(
7
			'name'   =>'Google Search Console',
8
			'key'    =>'google-site-verification',
9
			'format' =>'dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8',
10
			'url'    => 'https://www.google.com/webmasters/tools/',
11
		),
12
		'bing' => array(
13
			'name'   =>'Bing Webmaster Center',
14
			'key'    =>'msvalidate.01',
15
			'format' =>'12C1203B5086AECE94EB3A3D9830B2E',
16
			'url'    => 'http://www.bing.com/webmaster/',
17
		 ),
18
		'pinterest' => array(
19
			'name'   => 'Pinterest Site Verification',
20
			'key'    => 'p:domain_verify',
21
			'format' => 'f100679e6048d45e4a0b0b92dce1efce',
22
			'url'    => 'https://pinterest.com/website/verify/',
23
		),
24
		'yandex'     => array(
25
			'name'   => 'Yandex.Webmaster',
26
			'key'    => 'yandex-verification',
27
			'format' => '44d68e1216009f40',
28
			'url'    => 'https://webmaster.yandex.com/sites/',
29
		),
30
	);
31
}
32
33
34
function jetpack_verification_options_init() {
35
	register_setting( 'verification_services_codes_fields', 'verification_services_codes', 'jetpack_verification_validate' );
36
}
37
add_action( 'admin_init', 'jetpack_verification_options_init' );
38
39
function jetpack_verification_print_meta() {
40
	$verification_services_codes =  Jetpack_Options::get_option_and_ensure_autoload( 'verification_services_codes', '0' );
41
	if ( is_array( $verification_services_codes ) ) {
42
		$ver_output = "<!-- Jetpack Site Verification Tags -->\n";
43
		foreach ( jetpack_verification_services() as $name => $service ) {
44
			if ( is_array( $service ) && !empty( $verification_services_codes["$name"] ) ) {
45
				$ver_tag = sprintf( '<meta name="%s" content="%s" />', esc_attr( $service["key"] ), esc_attr( $verification_services_codes["$name"] ) );
46
				/**
47
				 * Filter the meta tag template used for all verification tools.
48
				 *
49
				 * @module verification-tools
50
				 *
51
				 * @since 3.0.0
52
				 *
53
				 * @param string $ver_tag Verification Tool meta tag.
54
				 */
55
				$ver_output .= apply_filters( 'jetpack_site_verification_output', $ver_tag );
56
				$ver_output .= "\n";
57
			}
58
		}
59
	echo $ver_output;
60
	}
61
}
62
add_action( 'wp_head', 'jetpack_verification_print_meta', 1 );
63
64
function jetpack_verification_get_code( $code ){
65
	$pattern = '/content=["\']?([^"\' ]*)["\' ]/is';
66
	preg_match( $pattern, $code, $match );
67
	if ( $match ){
0 ignored issues
show
Bug Best Practice introduced by
The expression $match of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
		return urldecode( $match[1] );
69
	} else {
70
		return false;
71
	}
72
}
73
74
function jetpack_verification_validate( $verification_services_codes ) {
75
	foreach ( $verification_services_codes as $key => &$code ) {
76
		// Parse html meta tags if present
77
		if ( stripos( $code, 'meta' ) )
78
			$code = jetpack_verification_get_code( $code );
79
80
		$code = esc_attr( trim( $code ) );
81
82
		// limit length to 100 chars.
83
		$code = substr( $code, 0, 100 );
84
85
		/**
86
		 * Fire after each Verification code was validated.
87
		 *
88
		 * @module verification-tools
89
		 *
90
		 * @since 3.0.0
91
		 *
92
		 * @param string $key Verification service name.
93
		 * @param string $code Verification service code provided in field in the Tools menu.
94
		 */
95
		do_action( 'jetpack_site_verification_validate', $key, $code );
96
	}
97
	return $verification_services_codes;
98
}
99
100
function jetpack_verification_options_form() {
101
	$verification_services_codes = get_option( 'verification_services_codes' );
102
	?>
103
<form method="post" action="options.php">
104
	<?php settings_fields( 'verification_services_codes_fields' ); ?>
105
	<div class="tools-container">
106
	<?php
107
	foreach ( jetpack_verification_services() as $key => $service ) {
108
		echo "<div class='jp-verification-service'>
109
				<h4>" . esc_html( $service['name'] ) . "</h4>
110
					<input value='" . esc_attr( isset( $verification_services_codes[ $key ] ) ? $verification_services_codes[ $key ] : '' ) . "' name='verification_services_codes[" . esc_attr( $key ) . "]' type='text' />
111
				<small>
112
					<label for='verification_services_codes[" . esc_attr( $key ) . "]'>" . esc_html( __( 'Example:' , 'jetpack' ) ) . " <span>&lt;meta name='" . esc_attr( $service['key'] ) . "' content='<strong>" . esc_attr( $service['format'] ) . "</strong>'&gt;</span></label>
113
				</small>
114
			</div>";
115
	}
116
	?>
117
	</div>
118
	<p class="submit">
119
		<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' , 'jetpack' ); ?>" />
120
	</p>
121
</form>
122
123
<style>
124
/*  Jetpack styles aren't loaded in the tools section of the admin, let's save on some http requests and just do an inline block */
125
126
	.jp-verification-tools h3 a {
127
		text-decoration: none;
128
	}
129
130
	.jp-verification-service {
131
		border-bottom: 1px #f1f1f1 solid;
132
		padding-bottom: 20px;
133
	}
134
135
	.jp-verification-service input[type="text"] {
136
		width: 100%;
137
		margin-bottom: 10px;
138
	}
139
140
	.jp-verification-service label {
141
		font-size: 13px;
142
	}
143
144
	/* mimic 'code' tag style, but this allows for better visuals + line breaks on mobile devices */
145
	.jp-verification-service span {
146
		display: block;
147
		margin-top: 5px;
148
		font-size: 14px;
149
		padding: 10px;
150
		background: #f1f1f1;
151
		font-family: monospace;
152
		word-wrap: break-word;
153
	}
154
155
	.jp-verification-service strong {
156
		font-weight: bold;
157
	}
158
</style>
159
160
<?php
161
}
162
163
function jetpack_verification_tool_box() {
164
	global $current_user;
165
166
	/**
167
	 * Decide whether Site Verification tools be added to the Tools menu.
168
	 *
169
	 * @module verification-tools
170
	 *
171
	 * @since 3.0.0
172
	 *
173
	 * @param bool true Should the Site Verification tools be added to the Tools menu.
174
	 */
175
	if ( ! apply_filters( 'jetpack_enable_site_verification', true ) )
176
		return;
177
178
	$list = array();
179
	foreach ( jetpack_verification_services() as $key => $service ) {
180
		$list[] = '<a href="' . esc_url( $service['url'] ) . '">' . esc_html( $service['name'] ) . '</a>';
181
	}
182
	$last = array_pop( $list );
183
184
	if ( current_user_can( 'manage_options' ) ) {
185
		echo '<div class="jp-verification-tools card"><h3 class="title">' . __( 'Website Verification Services' , 'jetpack' ) . ' <a href="http://support.wordpress.com/webmaster-tools/" target="_blank">(?)</a></h3>';
186
		echo '<p>' . sprintf( esc_html( __( 'Enter your meta key "content" value to verify your blog with %s' , 'jetpack' ) ), implode( ', ', $list ) ) . ' ' . __( 'and' , 'jetpack' ) . ' ' . $last . '.</p>';
187
		jetpack_verification_options_form();
188
		echo '</div>';
189
	}
190
}
191
add_action( 'tool_box', 'jetpack_verification_tool_box', 25 );
192