1: <?php
2: /**
3: * User Meta Sanitization
4: *
5: * Input side hardening for the author profile fields. These values are rendered
6: * in the author box on the front end, so they are sanitized at the meta layer
7: * where every write path passes through, admin or not.
8: *
9: * @package EPL
10: * @subpackage Includes/UserMeta
11: * @copyright Copyright (c) 2020, Merv Barrett
12: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
13: * @since 3.5.25
14: */
15:
16: // Exit if accessed directly.
17: if ( ! defined( 'ABSPATH' ) ) {
18: exit;
19: }
20:
21: /**
22: * User meta keys that hold a profile URL or a social handle.
23: *
24: * @return array
25: * @since 3.5.25
26: */
27: function epl_get_user_link_meta_keys() {
28: return apply_filters(
29: 'epl_user_link_meta_keys',
30: array( 'facebook', 'twitter', 'linkedin', 'google', 'instagram', 'pinterest', 'youtube', 'skype', 'video' )
31: );
32: }
33:
34: /**
35: * User meta keys that hold a plain text value.
36: *
37: * @return array
38: * @since 3.5.25
39: */
40: function epl_get_user_text_meta_keys() {
41: return apply_filters(
42: 'epl_user_text_meta_keys',
43: array( 'mobile', 'office_phone', 'position', 'slogan', 'contact-form' )
44: );
45: }
46:
47: /**
48: * Sanitize a profile URL or social handle before it is stored.
49: *
50: * These values end up inside href attributes in the author box. Output is
51: * escaped there as well, this is the matching input side guard so a hostile
52: * value never reaches the database in the first place. Handles entered without
53: * a scheme (for example a bare Twitter username) are preserved, since the
54: * author class turns those into absolute URLs at render time.
55: *
56: * @param mixed $value Meta value being saved.
57: *
58: * @return mixed
59: * @since 3.5.25
60: */
61: function epl_sanitize_user_link_meta( $value ) {
62:
63: if ( ! is_string( $value ) || '' === $value ) {
64: return $value;
65: }
66:
67: $value = sanitize_text_field( $value );
68:
69: if ( preg_match( '#^[a-z][a-z0-9+.\-]*:#i', $value ) ) {
70: // Looks like an absolute URL, so drop anything with a disallowed scheme.
71: return esc_url_raw( $value, epl_get_profile_link_protocols() );
72: }
73:
74: // A bare handle: strip the characters that could break out of an attribute.
75: return str_replace( array( '"', "'", '<', '>', '`', '\\' ), '', $value );
76: }
77:
78: /**
79: * Sanitize a plain text profile value before it is stored.
80: *
81: * @param mixed $value Meta value being saved.
82: *
83: * @return mixed
84: * @since 3.5.25
85: */
86: function epl_sanitize_user_text_meta( $value ) {
87: return is_string( $value ) ? sanitize_text_field( $value ) : $value;
88: }
89:
90: /**
91: * Protocols permitted in an author profile link.
92: *
93: * Skype is added to the WordPress list because the author box renders
94: * skype: URIs for the Skype contact method.
95: *
96: * @return array
97: * @since 3.5.25
98: */
99: function epl_get_profile_link_protocols() {
100: return apply_filters(
101: 'epl_profile_link_protocols',
102: array_values( array_unique( array_merge( wp_allowed_protocols(), array( 'skype' ) ) ) )
103: );
104: }
105:
106: /**
107: * Register the sanitize_user_meta_{$key} callbacks.
108: *
109: * Hooking the meta layer rather than the profile form covers every write path,
110: * including the REST API, wp_update_user() and direct update_user_meta() calls.
111: *
112: * @since 3.5.25
113: */
114: function epl_register_user_meta_sanitizers() {
115:
116: foreach ( epl_get_user_link_meta_keys() as $meta_key ) {
117: add_filter( "sanitize_user_meta_{$meta_key}", 'epl_sanitize_user_link_meta', 10, 1 );
118: }
119:
120: foreach ( epl_get_user_text_meta_keys() as $meta_key ) {
121: add_filter( "sanitize_user_meta_{$meta_key}", 'epl_sanitize_user_text_meta', 10, 1 );
122: }
123: }
124: // Registered immediately so the filters are in place for any early meta write,
125: // and again on init so late loading plugins can still extend the key lists.
126: epl_register_user_meta_sanitizers();
127: add_action( 'init', 'epl_register_user_meta_sanitizers', 1 );
128: