LLMS_Abstract_Generator_Posts::sideload_images( LLMS_Post_Model $post, array $raw )

Sideload images found in a given post


Description Description

This attempts to sideload the src attribute of every element found in the post_content of the supplied post.


Top ↑

Parameters Parameters

$post

(LLMS_Post_Model) (Required) Post object.

$raw

(array) (Required) Array of raw data.


Top ↑

Return Return

(null|boolean) Returns true on success, false if there were no images to update, or null if sideloading is disabled.


Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	protected function sideload_images( $post, $raw ) {

		// Sideloading is disabled.
		if ( ! $this->is_image_sideloading_enabled() ) {
			return null;
		}

		// No images to sideload.
		if ( empty( $raw['_extras']['images'] ) ) {
			return false;
		}

		/**
		 * List of hostnames from which sideloading is explicitly disabled
		 *
		 * If the source url of an image is from a host in this list, the image will not be sideloaded
		 * during generation.
		 *
		 * By default the current site is included in the blocklist ensuring that images aren't
		 * sideloaded into the same site.
		 *
		 * @since 4.7.0
		 *
		 * @param string[] $blocked_hosts Array of hostnames.
		 */
		$blocked_hosts = apply_filters(
			'llms_generator_sideload_hosts_blocklist',
			array(
				parse_url( get_site_url(), PHP_URL_HOST ),
			)
		);

		$post_id = $post->get( 'id' );
		$find    = array();
		$replace = array();
		foreach ( $raw['_extras']['images'] as $src ) {

			// Don't sideload images from blocked hosts.
			if ( in_array( parse_url( $src, PHP_URL_HOST ), $blocked_hosts, true ) ) {
				continue;
			}

			$new_src = $this->sideload_image( $post_id, $src );
			if ( ! is_wp_error( $new_src ) ) {
				$find[]    = $src;
				$replace[] = $new_src;
			}
		}

		if ( $find && $replace ) {
			$content = str_replace( $find, $replace, $post->get( 'content', true ) );
			return $post->set( 'content', $content );
		}

		return false;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.