Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
22 / 28
80.00% covered (warning)
80.00%
4 / 5
CRAP
n/a
0 / 0
jetpack_verification_extract_code
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
jetpack_verification_validate_code
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
jetpack_verification_validate_codes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
jetpack_verification_validate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
jetpack_verification_get_code
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Helper functions that are called from API even when module is inactive should be added here.
4 * This file will be included in module-extras.php.
5 *
6 * @package jetpack
7 */
8
9if ( ! function_exists( 'jetpack_verification_extract_code' ) ) {
10    /**
11     * Extract a site verification code from a meta tag.
12     *
13     * @since 16.3
14     *
15     * @param string $code Verification meta tag.
16     * @return string|false Extracted code, or false when none is found.
17     */
18    function jetpack_verification_extract_code( $code ) {
19        $pattern = '/content=["\']?([^"\' ]*)["\' ]/is';
20        preg_match( $pattern, $code, $match );
21
22        return $match ? rawurldecode( $match[1] ) : false;
23    }
24}
25
26if ( ! function_exists( 'jetpack_verification_validate_code' ) ) {
27    /**
28     * Validate and normalize a site verification code.
29     *
30     * @since 16.3
31     *
32     * @param mixed $code Verification code or meta tag.
33     * @return string|false Normalized code, or false when invalid.
34     */
35    function jetpack_verification_validate_code( $code ) {
36        if ( ! is_scalar( $code ) ) {
37            return false;
38        }
39
40        // Allow printable ASCII except characters that can delimit HTML attributes or tags.
41        $code_pattern = '/^(?!.*[<>"\'])[!-~]+$/';
42        $code         = trim( (string) $code );
43
44        if ( '' === $code ) {
45            return '';
46        }
47
48        // Parse html meta tag if it does not look like a valid code.
49        if ( ! preg_match( $code_pattern, $code ) ) {
50            $code = jetpack_verification_extract_code( $code );
51        }
52
53        $code = trim( (string) $code );
54        if ( '' === $code || ! preg_match( $code_pattern, $code ) ) {
55            return false;
56        }
57
58        return substr( $code, 0, 100 );
59    }
60}
61
62if ( ! function_exists( 'jetpack_verification_validate_codes' ) ) {
63    /**
64     * Validate jetpack verification codes.
65     *
66     * @since 16.3
67     *
68     * @param array $verification_services_codes - array of verification codes.
69     * @return array Validated verification codes.
70     */
71    function jetpack_verification_validate_codes( $verification_services_codes ) {
72        foreach ( $verification_services_codes as $key => $code ) {
73            $code = jetpack_verification_validate_code( $code );
74            $code = false === $code ? '' : $code;
75
76            /**
77             * Fire after each Verification code was validated.
78             *
79             * @module verification-tools
80             *
81             * @since 3.0.0
82             *
83             * @param string $key Verification service name.
84             * @param string $code Verification service code provided in field in the Tools menu.
85             */
86            do_action( 'jetpack_site_verification_validate', $key, $code );
87
88            $verification_services_codes[ $key ] = $code;
89        }
90        return $verification_services_codes;
91    }
92}
93
94if ( ! function_exists( 'jetpack_verification_validate' ) ) {
95    /**
96     * Validate jetpack verification codes.
97     *
98     * @param array $verification_services_codes - array of verification codes.
99     * @return array Validated verification codes.
100     */
101    function jetpack_verification_validate( $verification_services_codes ) {
102        return jetpack_verification_validate_codes( $verification_services_codes );
103    }
104}
105
106if ( ! function_exists( 'jetpack_verification_get_code' ) ) {
107    /**
108     * Return the code we're trying to verify after decoding.
109     *
110     * @param string $code - the code we need to parse.
111     * @return string|false Extracted code, or false when none is found.
112     */
113    function jetpack_verification_get_code( $code ) {
114        return jetpack_verification_extract_code( $code );
115    }
116}