Completed
Push — update/publicize-settings-ui ( 6f3aeb...ae05fe )
by
unknown
07:30
created

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

Complexity

Conditions 3
Paths 2

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 62
rs 8.829
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
				if ( preg_match( '#^<meta name="([a-z0-9_\-.:]+)?" content="([a-z0-9_-]+)?" />$#i', $verification_services_codes["$name"], $matches ) ) {
46
					$verification_code = $matches[2];
47
				} else {
48
					$verification_code = $verification_services_codes["$name"];
49
				}
50
				$ver_tag = sprintf( '<meta name="%s" content="%s" />', esc_attr( $service["key"] ), esc_attr( $verification_code ) );
51
				/**
52
				 * Filter the meta tag template used for all verification tools.
53
				 *
54
				 * @module verification-tools
55
				 *
56
				 * @since 3.0.0
57
				 *
58
				 * @param string $ver_tag Verification Tool meta tag.
59
				 */
60
				$ver_output .= apply_filters( 'jetpack_site_verification_output', $ver_tag );
61
				$ver_output .= "\n";
62
			}
63
		}
64
	echo $ver_output;
65
	}
66
}
67
add_action( 'wp_head', 'jetpack_verification_print_meta', 1 );
68
69
function jetpack_verification_options_form() {
70
	$verification_services_codes = get_option( 'verification_services_codes' );
71
	?>
72
<form method="post" action="options.php">
73
	<?php settings_fields( 'verification_services_codes_fields' ); ?>
74
	<div class="tools-container">
75
	<?php
76
	foreach ( jetpack_verification_services() as $key => $service ) {
77
		echo "<div class='jp-verification-service'>
78
				<h4>" . esc_html( $service['name'] ) . "</h4>
79
					<input value='" . esc_attr( isset( $verification_services_codes[ $key ] ) ? $verification_services_codes[ $key ] : '' ) . "' name='verification_services_codes[" . esc_attr( $key ) . "]' type='text' />
80
				<small>
81
					<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>
82
				</small>
83
			</div>";
84
	}
85
	?>
86
	</div>
87
	<p class="submit">
88
		<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' , 'jetpack' ); ?>" />
89
	</p>
90
</form>
91
92
<style>
93
/*  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 */
94
95
	.jp-verification-tools h3 a {
96
		text-decoration: none;
97
	}
98
99
	.jp-verification-service {
100
		border-bottom: 1px #f1f1f1 solid;
101
		padding-bottom: 20px;
102
	}
103
104
	.jp-verification-service input[type="text"] {
105
		width: 100%;
106
		margin-bottom: 10px;
107
	}
108
109
	.jp-verification-service label {
110
		font-size: 13px;
111
	}
112
113
	/* mimic 'code' tag style, but this allows for better visuals + line breaks on mobile devices */
114
	.jp-verification-service span {
115
		display: block;
116
		margin-top: 5px;
117
		font-size: 14px;
118
		padding: 10px;
119
		background: #f1f1f1;
120
		font-family: monospace;
121
		word-wrap: break-word;
122
	}
123
124
	.jp-verification-service strong {
125
		font-weight: bold;
126
	}
127
</style>
128
129
<?php
130
}
131
132
function jetpack_verification_tool_box() {
133
	global $current_user;
134
135
	/**
136
	 * Decide whether Site Verification tools be added to the Tools menu.
137
	 *
138
	 * @module verification-tools
139
	 *
140
	 * @since 3.0.0
141
	 *
142
	 * @param bool true Should the Site Verification tools be added to the Tools menu.
143
	 */
144
	if ( ! apply_filters( 'jetpack_enable_site_verification', true ) )
145
		return;
146
147
	$list = array();
148
	foreach ( jetpack_verification_services() as $key => $service ) {
149
		$list[] = '<a href="' . esc_url( $service['url'] ) . '">' . esc_html( $service['name'] ) . '</a>';
150
	}
151
	$last = array_pop( $list );
152
153
	if ( current_user_can( 'manage_options' ) ) {
154
		echo '<div class="jp-verification-tools card"><h3 class="title">' . __( 'Website Verification Services' , 'jetpack' ) . ' <a href="http://support.wordpress.com/webmaster-tools/" rel="noopener noreferrer" target="_blank">(?)</a></h3>';
155
		echo '<p>' . sprintf( esc_html( __( 'Enter your meta key "content" value to verify your blog with %s' , 'jetpack' ) ), implode( ', ', $list ) ) . ' ' . __( 'and' , 'jetpack' ) . ' ' . $last . '.</p>';
156
		jetpack_verification_options_form();
157
		echo '</div>';
158
	}
159
}
160
add_action( 'tool_box', 'jetpack_verification_tool_box', 25 );
161