Skip to content

Commit

Permalink
Carousel optimizations (#7943)
Browse files Browse the repository at this point in the history
* Stop calling consecutive `get_post()`

and instead call one big `get_posts()` call for fewer db queries.

Props @westi for the suggestion.

* Let's instead do one big str_replace with arrays instead of a bunch of repeated ones.

* I can haz typo

* We don't need `numberposts`.

get_posts already sets that to the number of array items passed into include.

* Add function to prime all the attachment meta in one go.

* Correction, this is being done already.

https://github.com/WordPress/WordPress/blob/df0958697a5d897a15ea78d37a001e2f3752875f/wp-includes/class-wp-query.php#L694
  • Loading branch information
George Stephanis authored and dereksmart committed Oct 25, 2017
1 parent f0616e6 commit a11d9f4
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions modules/carousel/jetpack-carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,26 @@ function add_data_img_tags_and_enqueue_assets( $content ) {
}
}

foreach ( $selected_images as $attachment_id => $image_html ) {
$attachment = get_post( $attachment_id );
$find = array();
$replace = array();
$attachments = get_posts( array(
'include' => array_keys( $selected_images ),
) );

if ( ! $attachment ) {
continue;
}
foreach ( $attachments as $attachment ) {
$image_html = $selected_images[ $attachment->ID ];

$attributes = $this->add_data_to_images( array(), $attachment );
$attributes_html = '';
foreach( $attributes as $k => $v ) {
$attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
}
$image_html_with_data = str_replace( '<img ', "<img $attributes_html", $image_html );
$content = str_replace( $image_html, $image_html_with_data, $content );

$find[] = $image_html;
$replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
}

$content = str_replace( $find, $replace, $content );
$this->enqueue_assets();
return $content;
}
Expand Down

0 comments on commit a11d9f4

Please sign in to comment.