BP_Media_Extractor::extract_images_from_galleries( string $richtext, string $plaintext, array $extra_args = array() )
Extract images in [galleries] shortcodes from text.
Description
Parameters
- $richtext
-
(Required) Content to parse.
- $plaintext
-
(Required) Sanitized version of the content.
- $extra_args
-
(Optional) Bespoke data for a particular extractor (optional).
Default value: array()
Return
(array)
Source
File: bp-core/classes/class-bp-media-extractor.php
protected function extract_images_from_galleries( $richtext, $plaintext, $extra_args = array() ) {
if ( ! isset( $extra_args['post'] ) || ! is_a( $extra_args['post'], 'WP_Post' ) ) {
$post = new WP_Post( (object) array( 'post_content' => $richtext ) );
} else {
$post = $extra_args['post'];
}
// We're not using get_post_galleries_images() because it returns thumbnails; we want the original image.
$galleries = get_post_galleries( $post, false );
$galleries_data = array();
if ( ! empty( $galleries ) ) {
// Validate the size of the images requested.
if ( isset( $extra_args['width'] ) ) {
// A width was specified but not a height, so calculate it assuming a 4:3 ratio.
if ( ! isset( $extra_args['height'] ) && ctype_digit( $extra_args['width'] ) ) {
$extra_args['height'] = round( ( $extra_args['width'] / 4 ) * 3 );
}
if ( ctype_digit( $extra_args['width'] ) ) {
$image_size = array( $extra_args['width'], $extra_args['height'] );
} else {
$image_size = $extra_args['width']; // E.g. "thumb", "medium".
}
} else {
$image_size = 'full';
}
/**
* There are two variants of gallery shortcode.
*
* One kind specifies the image (post) IDs via an `ids` parameter.
* The other gets the image IDs from post_type=attachment and post_parent=get_the_ID().
*/
foreach ( $galleries as $gallery_id => $gallery ) {
$data = array();
$images = array();
// Gallery ids= variant.
if ( isset( $gallery['ids'] ) ) {
$images = wp_parse_id_list( $gallery['ids'] );
// Gallery post_parent variant.
} elseif ( isset( $extra_args['post'] ) ) {
$images = wp_parse_id_list(
get_children( array(
'fields' => 'ids',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'post_mime_type' => 'image',
'post_parent' => $extra_args['post']->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
) )
);
}
// Extract the data we need from each image in this gallery.
foreach ( $images as $image_id ) {
$image = wp_get_attachment_image_src( $image_id, $image_size );
$data[] = array(
'url' => $image[0],
'width' => $image[1],
'height' => $image[2],
'gallery_id' => 1 + $gallery_id,
);
}
$galleries_data[] = $data;
}
}
/**
* Filters image galleries extracted from text.
*
* @since BuddyPress 2.3.0
*
* @param array $galleries_data Galleries. See {@link BP_Media_Extractor::extract_images_from_galleries()}.
* @param string $richtext Content to parse.
* @param string $plaintext Copy of $richtext without any markup.
* @param array $extra_args Bespoke data for a particular extractor.
*/
return apply_filters( 'bp_media_extractor_galleries', $galleries_data, $richtext, $plaintext, $extra_args );
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 2.3.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.