'attachment', 'post_status' => 'inherit', 'posts_per_page' => 1, 'fields' => 'ids', 'meta_key' => AIEO_Content::META_MEDIA_KEY, 'meta_value' => (string) $relative, 'no_found_rows' => true, ) ); return $ids ? (int) $ids[0] : 0; } public static function source( $relative, array $opts ) { $relative = (string) $relative; if ( ! AIEO_Content_Spec::media_path_ok( $relative ) || str_contains( $relative, "\0" ) ) { return ''; } if ( 0 === strpos( $relative, 'media/' ) ) { $dir = rtrim( (string) ( $opts['media_dir'] ?? '' ), '/\\' ); $file = $dir . '/' . $relative; } elseif ( '' !== trim( (string) ( $opts['wp_content_dir'] ?? '' ) ) ) { $dir = rtrim( (string) $opts['wp_content_dir'], '/\\' ); $file = $dir . substr( $relative, strlen( '/wp-content' ) ); } elseif ( 0 === strpos( $relative, '/wp-content/uploads/' ) ) { $dir = untrailingslashit( (string) wp_get_upload_dir()['basedir'] ); $file = $dir . substr( $relative, strlen( '/wp-content/uploads' ) ); } else { $dir = untrailingslashit( (string) get_theme_root() ); $file = $dir . substr( $relative, strlen( '/wp-content/themes' ) ); } return '' !== $dir && is_file( $file ) && is_readable( $file ) ? $file : ''; } private static function from_cli() { return null !== self::$cli ? (bool) self::$cli : ( defined( 'WP_CLI' ) && WP_CLI ); } private static function filesystem() { global $wp_filesystem; if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! $wp_filesystem instanceof WP_Filesystem_Base && ! WP_Filesystem() ) { return null; } return $wp_filesystem instanceof WP_Filesystem_Base ? $wp_filesystem : null; } public static function attach( $relative, $alt, array $opts ) { $relative = (string) $relative; $known = self::find( $relative ); if ( $known ) { if ( '' !== (string) $alt && (string) get_post_meta( $known, '_wp_attachment_image_alt', true ) !== (string) $alt ) { update_post_meta( $known, '_wp_attachment_image_alt', (string) $alt ); } return $known; } $source = self::source( $relative, $opts ); if ( '' === $source ) { return new WP_Error( 'aieo_content_media_missing', sprintf( 'The media file %s is not in the library and not under the media folders given (--media-dir, --wp-content-dir).', $relative ) ); } $svg = 'svg' === strtolower( pathinfo( $source, PATHINFO_EXTENSION ) ); if ( $svg && 0 !== strpos( $relative, 'media/' ) ) { return new WP_Error( 'aieo_content_media_svg_source', sprintf( 'An SVG is sideloaded only from the content repository (a media/… path under --media-dir), never from the old site\'s wp-content: %s.', $relative ) ); } if ( $svg && ! self::from_cli() ) { return new WP_Error( 'aieo_content_media_svg_cli', sprintf( 'An SVG is sideloaded only by WP-CLI (wp aieo page apply): %s.', $relative ) ); } if ( ! function_exists( 'wp_tempnam' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'media_handle_sideload' ) ) { require_once ABSPATH . 'wp-admin/includes/media.php'; } if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; } $fs = self::filesystem(); $tmp = $fs ? wp_tempnam( basename( $source ) ) : ''; if ( ! $tmp ) { return new WP_Error( 'aieo_content_media_copy', sprintf( 'Cannot copy %s for the upload.', $relative ) ); } if ( $svg ) { $clean = self::sanitise_svg( (string) $fs->get_contents( $source ) ); if ( is_wp_error( $clean ) ) { wp_delete_file( $tmp ); return new WP_Error( 'aieo_content_media_svg', sprintf( '%s: %s', $relative, $clean->get_error_message() ) ); } if ( $clean['unstyled'] ) { wp_delete_file( $tmp ); return new WP_Error( 'aieo_content_media_svg_style', sprintf( '%s: the SVG sanitiser would drop %s, and the picture would be drawn without its stylesheet (every shape black). Write its CSS as plain rules: no at-rule, no escape, no url() but a fragment\'s.', $relative, implode( ', ', $clean['unstyled'] ) ) ); } if ( ! $fs->put_contents( $tmp, $clean['svg'], FS_CHMOD_FILE ) ) { wp_delete_file( $tmp ); return new WP_Error( 'aieo_content_media_copy', sprintf( 'Cannot copy %s for the upload.', $relative ) ); } if ( $clean['dropped'] ) { self::$warnings[] = sprintf( '%s: the SVG sanitiser dropped %s.', $relative, implode( ', ', $clean['dropped'] ) ); } } elseif ( ! $fs->copy( $source, $tmp, true, FS_CHMOD_FILE ) ) { wp_delete_file( $tmp ); return new WP_Error( 'aieo_content_media_copy', sprintf( 'Cannot copy %s for the upload.', $relative ) ); } $mimes = static function ( $types ) { $types['svg'] = 'image/svg+xml'; return $types; }; $check = static function ( $data, $file, $filename ) { return 'svg' === strtolower( pathinfo( (string) $filename, PATHINFO_EXTENSION ) ) ? array( 'ext' => 'svg', 'type' => 'image/svg+xml', 'proper_filename' => false ) : $data; }; if ( $svg ) { add_filter( 'upload_mimes', $mimes ); add_filter( 'wp_check_filetype_and_ext', $check, 10, 3 ); } $id = media_handle_sideload( array( 'name' => basename( $source ), 'tmp_name' => $tmp ), 0, (string) $alt, array( 'post_title' => '' !== (string) $alt ? (string) $alt : sanitize_title( basename( $source ) ) ) ); if ( $svg ) { remove_filter( 'upload_mimes', $mimes ); remove_filter( 'wp_check_filetype_and_ext', $check, 10 ); } if ( is_wp_error( $id ) ) { wp_delete_file( $tmp ); return $id; } update_post_meta( (int) $id, AIEO_Content::META_MEDIA_KEY, $relative ); if ( '' !== (string) $alt ) { update_post_meta( (int) $id, '_wp_attachment_image_alt', (string) $alt ); } return (int) $id; } public static function sanitise_svg( $xml ) { $xml = (string) $xml; $dropped = array(); $unstyled = array(); if ( preg_match( '/\[]*(\[[^\]]*\])?[^>]*>/is', '', $xml ); $dropped[] = 'the DOCTYPE'; } $previous = libxml_use_internal_errors( true ); $dom = new DOMDocument( '1.0', 'UTF-8' ); $loaded = '' !== trim( $xml ) && $dom->loadXML( $xml, LIBXML_NONET ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( ! $loaded || ! $dom->documentElement || 'svg' !== $dom->documentElement->localName ) { return new WP_Error( 'aieo_content_media_svg', 'not an SVG document.' ); } $xpath = new DOMXPath( $dom ); foreach ( iterator_to_array( $xpath->query( '//processing-instruction()' ) ) as $pi ) { if ( $pi->parentNode ) { $pi->parentNode->removeChild( $pi ); $dropped[] = 'a processing instruction'; } } foreach ( iterator_to_array( $xpath->query( '//*' ) ) as $el ) { if ( ! $el instanceof DOMElement || ! $el->parentNode ) { continue; } $name = strtolower( (string) $el->localName ); if ( 'style' === $name && ( 0 !== $el->childElementCount || ! self::static_css( $el->textContent ) ) ) { $el->parentNode->removeChild( $el ); $dropped[] = '