diff --git a/generate-posts.php b/generate-posts.php index d205a7d..452a802 100644 --- a/generate-posts.php +++ b/generate-posts.php @@ -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_args ); + } + + // Mix it up. + shuffle( $content_bits ); + shuffle( $content_bits ); + shuffle( $content_bits ); + + return implode( ' ', $content_bits ); } /** @@ -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, @@ -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' );