ID ) ) { return; } $type = get_post_type_object( $p->post_type ); $title = '' !== $p->post_title ? $p->post_title : sprintf( '#%d', $p->ID ); $out[] = array( 'label' => sprintf( '%s: %s (%s)', $type ? $type->labels->singular_name : $p->post_type, wp_strip_all_tags( $title ), self::kind_label( $kind ) ), 'url' => current_user_can( 'edit_post', $p->ID ) ? get_edit_post_link( $p->ID, 'raw' ) : get_permalink( $p ), 'kind' => $kind, ); }; // Featured image. // phpcs:ignore WordPress.DB.DirectDatabaseQuery $featured = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_thumbnail_id' AND meta_value = %s LIMIT %d", (string) $id, $limit ) ); foreach ( $featured as $pid ) { $push( (int) $pid, 'featured' ); } // Content: block/class markers and the file name. $file = get_attached_file( $id ); $base = $file ? pathinfo( $file, PATHINFO_FILENAME ) : ''; $base = preg_replace( '/-scaled$/', '', $base ); $like1 = '%' . $wpdb->esc_like( 'wp-image-' . $id . '"' ) . '%'; $like2 = '%' . $wpdb->esc_like( '"id":' . $id . ',' ) . '%'; $like3 = $base ? '%' . $wpdb->esc_like( '/' . $base ) . '%' : $like1; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_content FROM {$wpdb->posts} WHERE post_type NOT IN ('revision','attachment') AND post_status NOT IN ('trash','auto-draft') AND (post_content LIKE %s OR post_content LIKE %s OR post_content LIKE %s) LIMIT %d", $like1, $like2, $like3, $limit * 2 ) ); // LIKE is only a pre-filter ("/photo-1" also matches "/photo-1024x683.jpg"); confirm precisely. $name_re = $base ? '#/' . preg_quote( $base, '#' ) . '(?:-\d+x\d+)?(?:-scaled)?\.(?:jpe?g|png|webp|gif|avif|heic)\b#i' : ''; foreach ( $rows as $row ) { $c = (string) $row->post_content; if ( false !== strpos( $c, 'wp-image-' . $id . '"' ) || false !== strpos( $c, 'wp-image-' . $id . ' ' ) || false !== strpos( $c, '"id":' . $id . ',' ) || ( $name_re && preg_match( $name_re, $c ) ) ) { $push( (int) $row->ID, 'content' ); } } if ( (int) get_theme_mod( 'custom_logo' ) === $id ) { $out[] = array( 'label' => __( 'Site logo', 'pictureeditor' ), 'url' => admin_url( 'customize.php' ), 'kind' => 'logo', ); } if ( (int) get_option( 'site_icon' ) === $id ) { $out[] = array( 'label' => __( 'Site icon', 'pictureeditor' ), 'url' => admin_url( 'options-general.php' ), 'kind' => 'icon', ); } /** * Filter the uses found for an attachment. Integrations (WooCommerce, page builders) add theirs. * * @param array[] $out Uses. * @param int $id Attachment ID. * @param int $limit Max results. */ $out = (array) apply_filters( 'pictureeditor_attachment_usage', $out, $id, $limit ); return array_slice( $out, 0, $limit ); } /** * Label for a kind. * * @param string $kind Kind. * @return string */ private static function kind_label( $kind ) { switch ( $kind ) { case 'featured': return __( 'featured image', 'pictureeditor' ); case 'content': return __( 'in content', 'pictureeditor' ); case 'product': return __( 'product image', 'pictureeditor' ); case 'gallery': return __( 'product gallery', 'pictureeditor' ); case 'variation': return __( 'variation image', 'pictureeditor' ); default: return $kind; } } /** * Public wrapper for integrations. * * @param string $kind Kind. * @return string */ public static function label_for( $kind ) { return self::kind_label( $kind ); } }