Last active
December 20, 2021 13:24
-
-
Save angelorocha/a2dbcd0790ec8653e572cf43356fe193 to your computer and use it in GitHub Desktop.
Generate WordPress dummy posts with content and post thumbnail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class WPSSDummy | |
* | |
* Generate dummy posts, see parameters of loripsum api: | |
* | |
* (integer) - The number of paragraphs to generate. | |
* short, medium, long, verylong - The average length of a paragraph. | |
* decorate - Add bold, italic and marked text. | |
* link - Add links. | |
* ul - Add unordered lists. | |
* ol - Add numbered lists. | |
* dl - Add description lists. | |
* bq - Add blockquotes. | |
* code - Add code samples. | |
* headers - Add headers. | |
* allcaps - Use ALL CAPS. | |
* prude - Prude version. | |
* plaintext - Return plain text, no HTML. | |
*/ | |
class WPSSDummy { | |
/** | |
* Posts limit to generate | |
* | |
* @var int | |
*/ | |
public static int $posts_quantity = 10; | |
/** | |
* Define post type to dummy posts | |
* | |
* @var string | |
*/ | |
public static string $post_type = 'post'; | |
/** | |
* Define default post author | |
* | |
* @var int|null | |
*/ | |
public static ?int $post_author = null; | |
/** | |
* Define days interval for each post | |
* | |
* @var int | |
*/ | |
public static int $days_interval = 0; | |
/** | |
* Define hours interval for each post | |
* | |
* @var int | |
*/ | |
public static int $hour_interval = 1; | |
/** | |
* Define default post status | |
* | |
* @var string | |
*/ | |
public static string $post_status = 'publish'; | |
/** | |
* Define content api | |
* | |
* @var string | |
*/ | |
public static string $content_api = 'http://loripsum.net/api/'; | |
/** | |
* Default content length | |
* | |
* @var int | |
*/ | |
public static int $content_length = 10; | |
/** | |
* Set thumbnail image api | |
* | |
* @var string | |
*/ | |
public static string $thumbnail_api = 'http://picsum.photos/'; | |
/** | |
* Define image width | |
* | |
* @var int | |
*/ | |
public static int $thumb_width = 880; | |
/** | |
* Define image height | |
* | |
* @var int | |
*/ | |
public static int $thumb_height = 660; | |
public function __construct() { | |
self::$content_api = self::$content_api . self::$content_length . '/medium/decorate/link'; | |
self::$thumbnail_api = self::$thumbnail_api . self::$thumb_width . '/' . self::$thumb_height; | |
if( is_null(self::$post_author) ): | |
self::$post_author = self::wpss_assign_post_to_user(); | |
endif; | |
} | |
/** | |
* Make post types | |
*/ | |
public function wpss_make_posts(): void { | |
$output = __('Starting post create...<br>', 'wpss'); | |
for( $posts = 0; $posts < self::$posts_quantity; $posts++ ): | |
if( self::$days_interval > 0 ): | |
self::$days_interval++; | |
endif; | |
if( self::$hour_interval > 0 ): | |
self::$hour_interval++; | |
endif; | |
$output .= self::wpss_insert_posts(); | |
endfor; | |
$output .= __('Dummy posts created successfully!', 'wpss'); | |
echo $output; | |
} | |
/** | |
* Insert singular post | |
* | |
* @return string | |
*/ | |
private function wpss_insert_posts(): string { | |
$post_id = wp_insert_post(self::wpss_post_args()); | |
self::wpss_set_thumbnail($post_id, basename(get_permalink($post_id))); | |
return sprintf(__('The post <strong>%s</strong> was created successfully<br>'), get_the_title($post_id)); | |
} | |
/** | |
* Default post args | |
* | |
* @return array | |
*/ | |
private function wpss_post_args(): array { | |
return [ | |
'post_author' => self::$post_author, | |
'post_date' => self::wpss_posts_date_interval(), | |
'post_date_gmt' => self::wpss_posts_date_interval(true), | |
'post_title' => self::wpss_generate_title(), | |
'post_content' => self::wpss_generate_content(), | |
'post_excerpt' => self::wpss_generate_excerpt(), | |
'post_status' => self::$post_status, | |
'post_type' => self::$post_type, | |
]; | |
} | |
/** | |
* Generate post title | |
* | |
* @return string | |
*/ | |
private function wpss_generate_title(): string { | |
$title = wp_remote_retrieve_body(self::wpss_remote_get(self::$content_api)); | |
$title = str_replace('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '', $title); | |
$title = explode(' ', sanitize_text_field($title)); | |
$title = array_slice($title, 0, rand(6, 12)); | |
return implode(' ', (array)$title); | |
} | |
/** | |
* Generate post excerpt | |
* | |
* @param int $limit limit of words for excerpt | |
* | |
* @return string | |
*/ | |
private function wpss_generate_excerpt(int $limit = 100): string { | |
$content = sanitize_text_field(self::wpss_generate_content()); | |
$content = explode(' ', $content); | |
$content = array_slice($content, 0, $limit); | |
return implode(' ', (array)$content); | |
} | |
/** | |
* Generate post content | |
* | |
* @return string | |
*/ | |
private function wpss_generate_content(): string { | |
$content = self::wpss_remote_get(self::$content_api); | |
return wp_remote_retrieve_body($content); | |
} | |
/** | |
* Define post date | |
* | |
* @param bool $gmt define if is gmt date | |
* | |
* @return string | |
*/ | |
private function wpss_posts_date_interval(bool $gmt = false): string { | |
$days = self::$days_interval; | |
$hour = self::$hour_interval; | |
$generate_date = strtotime(current_time('Y-m-d H:i:s', $gmt) . "-$days days -$hour hour"); | |
return date('Y-m-d H:i:s', $generate_date); | |
} | |
/** | |
* Define rand author post | |
* | |
* @return int | |
*/ | |
private function wpss_assign_post_to_user(): int { | |
$args = [ 'role' => 'administrator' ]; | |
$users = new WP_User_Query($args); | |
$author = []; | |
foreach( $users->get_results() as $user ): | |
$author[$user->ID] = $user->display_name; | |
endforeach; | |
return array_rand($author); | |
} | |
/** | |
* Set automatic post thumbnail | |
* | |
* @param int $post_id | |
* @param string $image_name | |
*/ | |
private function wpss_set_thumbnail(int $post_id, string $image_name): void { | |
$get_thumbnail = self::wpss_remote_get(self::$thumbnail_api); | |
if( !empty($get_thumbnail) ) : | |
$the_thumbnail = wp_upload_bits("{$image_name}.jpg", null, wp_remote_retrieve_body($get_thumbnail)); | |
$mime = wp_remote_retrieve_header($get_thumbnail, 'content-type'); | |
$set_thumbnail = [ | |
'post_title' => $image_name, | |
'guid' => $the_thumbnail['url'], | |
'post_status' => 'inherit', | |
'post_mime_type' => $mime, | |
]; | |
$thumbnail_id = wp_insert_attachment($set_thumbnail, $the_thumbnail['file'], $post_id); | |
include_once ABSPATH . 'wp-admin/includes/image.php'; | |
$thumbnail_data = wp_generate_attachment_metadata($thumbnail_id, $the_thumbnail['file']); | |
wp_update_attachment_metadata($thumbnail_id, $thumbnail_data); | |
set_post_thumbnail($post_id, $thumbnail_id); | |
endif; | |
} | |
/** | |
* @param string $url | |
* | |
* @return array | |
*/ | |
private function wpss_remote_get(string $url): ?array { | |
$response = wp_remote_get($url, [ 'timeout' => 120, 'sslverify' => false ]); | |
if( wp_remote_retrieve_response_code($response) !== 200 ): | |
return []; | |
endif; | |
return $response; | |
} | |
} | |
/** | |
* Usage sample | |
*/ | |
//$posts = new WPSSDummy(); | |
//$posts->wpss_make_posts(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment