Skip to content

Images in content #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions generate-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,34 @@ private function get_post_content() {
return '';
}

return wp_remote_retrieve_body( $request );
// Get the content and explode it.
$content = wp_remote_retrieve_body( $request );
$content_bits = explode( ' ', $content );

// Get up to 3 images to insert to the post.
$image_count = mt_rand( 1, 3 );
$images = array();

for ( $i = $image_count; $i--; ) {
// Randomize image sizes.
$image_sizes = array( mt_rand( 200, 800 ), mt_rand( 200, 800 ) );
$alignment = ( mt_rand( 1, 10 ) % 3 ) ? 'alignleft' : 'alignright';
$img_args = array(
$this->get_image_url( $image_sizes ),
$image_sizes[0],
$image_sizes[1],
$alignment,
);

$content_bits[] = vsprintf( '<img src="%1$s" width="%2$d" height="%3$d" class="%4$s" />', $img_args );
}

// Mix it up.
shuffle( $content_bits );
shuffle( $content_bits );
shuffle( $content_bits );

return implode( ' ', $content_bits );
}

/**
Expand Down Expand Up @@ -464,20 +491,10 @@ private function get_term() {
* @return int|null The new attachment ID
*/
private function download_image( $sizes, $post_id = 0 ) {
$sizes = implode( '/', array_filter( $sizes ) );

$img_type = isset( $this->assoc_args['img-type'] ) ? $this->assoc_args['img-type'] : '';

$url = 'http://lorempixel.com/' . $sizes;
if ( ! empty( $img_type ) ) {
$url .= '/' . $img_type;
}
WP_CLI::line( sprintf( 'Downloading an image with the size of %s, please wait...', str_replace( '/', 'x', $sizes ) ) );

require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';

$tmp = download_url( $url );
$tmp = download_url( $this->get_image_url( $sizes ) );
$type = image_type_to_extension( exif_imagetype( $tmp ) );
$file_array = array(
'name' => 'placeholderImage_' . mt_rand( 30948, 40982 ) . '_' . str_replace( '/', 'x', $sizes ) . $type,
Expand Down Expand Up @@ -574,6 +591,30 @@ private function transform_img_types_to_table() {
return $out;
}

/**
* Get an image URL based on provided size.
*
* @since NEXT
*
* @author Zach Owen
*
* @param array $sizes An array of image dimensions.
* @return string
*/
private function get_image_url( $sizes ) {
$sizes = implode( '/', array_filter( $sizes ) );

$img_type = isset( $this->assoc_args['img-type'] ) ? $this->assoc_args['img-type'] : '';

$url = 'http://lorempixel.com/' . $sizes;
if ( ! empty( $img_type ) ) {
$url .= '/' . $img_type;
}
WP_CLI::line( sprintf( 'Downloading an image with the size of %s, please wait...', str_replace( '/', 'x', $sizes ) ) );

return $url;
}

}

WP_CLI::add_command( 'jw-random', 'JW_Random_Posts' );
Expand Down