$value['url'], 'title' => isset( $value['title'] ) ? (string) $value['title'] : '', 'target' => isset( $value['target'] ) && '_blank' === $value['target'] ? '_blank' : '', ); case 'json': return $value; } throw new Tool_Error( esc_html( "$label can't be changed through ADI." ) ); } /** * A link a page may carry: empty, a site path, an anchor, a query, or an absolute URL whose * scheme WordPress allows in content (https, mailto, tel, …) — never javascript:/data:. Not a * reachability check: staging and intranet hosts are valid link targets. */ public static function is_link( $value ) { if ( '' === $value ) { return true; } if ( preg_match( '/[\s<>"]/', $value ) ) { return false; } if ( false !== strpos( $value, '\\' ) ) { return false; // browsers read "\" as "/": "/\evil.test" is another site dressed as a path } if ( in_array( $value[0], array( '/', '#', '?' ), true ) ) { // A site path, anchor or query (/about/, /#contact, /?page_id=5); `//host/x` must name a host. return 0 !== strpos( $value, '//' ) || (bool) wp_parse_url( 'https:' . $value, PHP_URL_HOST ); } if ( ! preg_match( '/^([a-z][a-z0-9+.-]*):/i', $value, $m ) || ! in_array( strtolower( $m[1] ), wp_allowed_protocols(), true ) ) { return false; } return in_array( strtolower( $m[1] ), array( 'http', 'https' ), true ) ? (bool) wp_parse_url( $value, PHP_URL_HOST ) : true; } /** * Does the field still hold what the caller read (wp_update_field's `expected`)? An empty field * reads as "" from one editor and null from another, so either matches an empty expectation. */ public static function still( $now, $expected ) { if ( ( null === $expected || '' === $expected ) && ( null === $now || '' === $now ) ) { return true; } return self::same( $now, $expected ); } /** * Do two stored values mean the same thing? Line endings, trailing space, entity spelling * (`&` vs `&`, 🎉 vs `🎉`) and key order at any depth don't count; any change in text * or markup does. */ public static function same( $a, $b ) { if ( is_string( $a ) && is_string( $b ) && Html::equivalent( $a, $b ) ) { return true; } $sorted = static function ( $v ) use ( &$sorted ) { if ( ! is_array( $v ) ) { return $v; } ksort( $v ); // lists keep their order: their keys are already 0, 1, 2… return array_map( $sorted, $v ); }; $norm = static function ( $v ) use ( $sorted ) { if ( is_string( $v ) ) { return rtrim( str_replace( "\r\n", "\n", $v ) ); } if ( is_array( $v ) ) { return wp_json_encode( $sorted( $v ) ); } return is_bool( $v ) || null === $v ? $v : (string) $v; }; return $norm( $a ) === $norm( $b ); } }