* @param WP_Term|object $term Term object. */ public function __construct( $term ) { foreach ( get_object_vars( $term ) as $key => $value ) { $this->$key = $value; } } /** * Sanitizes term fields, according to the filter type provided. * * @since 4.4.0 * * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'rss', or 'raw'. */ public function filter( $filter ) { sanitize_term( $this, $this->taxonomy, $filter ); } /** * Converts an object to array. * * @since 4.4.0 * * @return array Object as array. */ public function to_array() { return get_object_vars( $this ); } /** * Getter. * * @since 4.4.0 * * @param string $key Property to get. * @return mixed Property value. */ public function __get( $key ) { switch ( $key ) { case 'data': $data = new stdClass(); $columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' ); foreach ( $columns as $column ) { $data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null; } return sanitize_term( $data, $data->taxonomy, 'raw' ); } } } * @param string $url_to_sanitize The URL to sanitize. * @param array $url_pieces The url pieces. * * @return string The sanitized url. */ private function sanitize_slash( $url_to_sanitize, array $url_pieces = [] ) { $url = $url_to_sanitize; if ( $url !== '/' && ! isset( $url_pieces['scheme'] ) ) { return trim( $url_to_sanitize, '/' ); } return $url; } /** * Strip the protocol from the URL. * * @param string $scheme The scheme to strip. * @param string $url The URL to remove the scheme from. * * @return string The url without the scheme. */ private function strip_scheme_from_url( $scheme, $url ) { return str_replace( $scheme . '://', '', $url ); } /** * Remove the home URL from the redirect to ensure that relative URLs are created. * * @param string $url The URL to sanitize. * * @return string The sanitized url. */ private function sanitize_origin_url( $url ) { $home_url = static::$home_url->get(); $home_url_pieces = static::$home_url->get_parsed(); $url_pieces = $this->parse_url( $url ); if ( $this->match_home_url( $home_url_pieces, $url_pieces ) ) { $url = substr( $this->strip_scheme_from_url( $url_pieces['scheme'], $url ), strlen( $this->strip_scheme_from_url( $home_url_pieces['scheme'], $home_url ) ) ); $url_pieces['scheme'] = null; } return $this->sanitize_slash( $url, $url_pieces ); } /** * Sanitizes the target url. * * @param string $url The url to sanitize. * * @return string The sanitized url. */ private function sanitize_target_url( $url ) { $home_url_pieces = static::$home_url->get_parsed(); $url_pieces = $this->parse_url( $url ); if ( $this->match_home_url( $home_url_pieces, $url_pieces ) ) { $url = substr( $this->strip_scheme_from_url( $url_pieces['scheme'], $url ), strlen( $home_url_pieces['host'] ) ); $url_pieces['scheme'] = null; } return $this->sanitize_slash( $url, $url_pieces ); } /** * Checks if the URL matches the home URL. * * @param array $home_url_pieces The pieces (wp_parse_url) from the home_url. * @param array $url_pieces The pieces (wp_parse_url) from the url to match. * * @return bool True when the URL matches the home URL. */ private function match_home_url( $home_url_pieces, $url_pieces ) { if ( ! isset( $url_pieces['scheme'] ) ) { return false; } if ( ! isset( $url_pieces['host'] ) || ! $this->match_home_url_host( $home_url_pieces['host'], $url_pieces['host'] ) ) { return false; } if ( ! isset( $home_url_pieces['path'] ) ) { return true; } return isset( $url_pieces['path'] ) && $this->match_home_url_path( $home_url_pieces['path'], $url_pieces['path'] ); } /** * Checks if the URL matches the home URL by comparing their host. * * @param string $home_url_host The home URL host. * @param string $url_host The URL host. * * @return bool True when both hosts are equal. */ private function match_home_url_host( $home_url_host, $url_host ) { return $url_host === $home_url_host; } /** * Checks if the URL matches the home URL by comparing their path. * * @param string $home_url_path The home URL path. * @param string $url_path The URL path. * * @return bool True when the home URL path is empty or when the URL path begins with the home URL path. */ private function match_home_url_path( $home_url_path, $url_path ) { $home_url_path = trim( $home_url_path, '/' ); if ( empty( $home_url_path ) ) { return true; } return strpos( trim( $url_path, '/' ), $home_url_path ) === 0; } /** * Parses the URL into separate pieces. * * @param string $url The URL string. * * @return array Array of URL pieces. */ private function parse_url( $url ) { $parsed_url = wp_parse_url( $url ); if ( is_array( $parsed_url ) ) { return $parsed_url; } return []; } }