= 24 ) { break; } $line = self::truncate_unicode( $line, 160 ); $line = str_replace( array( '{', '}', ';', '<' ), '', $line ); $lower = strtolower( trim( $line ) ); if ( '' === $line || in_array( $lower, array( '*', 'html', 'body', ':root' ), true ) || false !== strpos( $lower, ':has(' ) || false !== strpos( $lower, '.akava-btt' ) || false !== strpos( $lower, '#akava-btt' ) || false !== strpos( $lower, '::before' ) || false !== strpos( $lower, '::after' ) ) { continue; } $clean[] = $line; } $clean = array_values( array_unique( $clean ) ); return implode( "\n", $clean ); } /** * Truncate UTF-8 text safely even when the optional mbstring extension is absent. * * @param string $value Text. * @param int $maximum Maximum characters. * @return string */ private static function truncate_unicode( $value, $maximum ) { if ( function_exists( 'mb_substr' ) ) { return mb_substr( $value, 0, $maximum ); } if ( preg_match( '//u', $value ) ) { $characters = preg_split( '//u', $value, -1, PREG_SPLIT_NO_EMPTY ); if ( is_array( $characters ) ) { return implode( '', array_slice( $characters, 0, $maximum ) ); } } return substr( $value, 0, $maximum ); } /** * Accept image attachments already admitted by the WordPress media system. * * The plugin never enables a MIME type itself and never inlines uploaded SVG. * When another trusted component enables sanitized SVG uploads, the selected * file is rendered through an ordinary img element instead of being rejected. * * @param mixed $attachment_id Raw attachment ID. * @return int */ public static function image_attachment_id( $attachment_id ) { $attachment_id = absint( $attachment_id ); if ( ! $attachment_id || 'attachment' !== get_post_type( $attachment_id ) ) { return 0; } $mime = (string) get_post_mime_type( $attachment_id ); $url = wp_get_attachment_url( $attachment_id ); if ( 0 !== strpos( $mime, 'image/' ) || ! $url ) { return 0; } /** * Filter whether an existing WordPress image attachment can be used. * * @param bool $allowed Current decision. * @param int $attachment_id Attachment ID. * @param string $mime Attachment MIME type. */ $allowed = (bool) apply_filters( 'akava_btt_allow_image_attachment', true, $attachment_id, $mime ); return $allowed ? $attachment_id : 0; } }