-
Notifications
You must be signed in to change notification settings - Fork 2
/
post-gen.php
190 lines (162 loc) · 4.76 KB
/
post-gen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
* Plugin Name: Post Gen
* Plugin URI: trepmal.com
* Description: Post generator.
* Version:
* Author: Kailey Lampert
* Author URI: kaileylampert.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* TextDomain: post-gen
* DomainPath:
* Network:
*/
/**
* Generate one post
*
* @return void
*/
function post_gen_create_post( $args = array() ) {
$defaults = array(
'post_type' => 'post',
'post_status' => 'publish',
'paragraphs' => '4,7',
'noimage' => 0,
'days-offset' => array( 1, 300, 5 ),
'hours-offset' => array( 1, 24 ),
'img-height' => 800,
'img-width' => 1000,
'img-lowgrey' => 150,
'img-highgrey' => '',
);
$args = wp_parse_args( $args, $defaults );
$post_type = sanitize_key( $args['post_type'] );
if ( ! post_type_exists( $post_type ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered post type.", $post_type ) );
}
$img_args = array();
foreach ( $args as $k => $v ) {
if ( strpos( $k, 'img-' ) === 0 ) {
$new_key = str_replace( 'img-', '', $k );
$img_args[ $new_key ] = $v;
unset( $args[ $k ] );
}
}
$title = post_gen_get_random_title();
$day = post_gen_convert_to_value( $args['days-offset'] );
$hour = post_gen_convert_to_value( $args['hours-offset'] );
$post_status = $args['post_status'];
if ( 'future' === $post_status ) {
$date = date( 'Y-m-d H:i:s', strtotime( "+$day days +$hour hours" ) );
} else {
$date = date( 'Y-m-d H:i:s', strtotime( "-$day days -$hour hours" ) );
}
$post_args = array(
'post_type' => $post_type,
'post_status' => $post_status,
'post_title' => $title,
'post_content' => post_gen_get_random_content( $args['paragraphs'] ),
'post_author' => 1,
'post_date' => $date,
'post_date_gmt' => $date,
);
$post_args = apply_filters( 'post_gen_args', $post_args );
$postid = wp_insert_post( $post_args );
if ( ! is_wp_error( $postid ) ) {
if ( function_exists('image_gen__create_image' ) && ! $args['noimage'] ) {
// generate matching image
// split long title into reasonable lines
$image_title = _split_str_by_whitespace( $title, 30 );
$image_title = array_map( 'trim', $image_title );
// todo, only run this over integer vals
$img_args = array_map( 'post_gen_convert_to_value', $img_args );
// if no highgrey, make the same as low. this will make the image solid and faster to generate
$img_args['highgrey'] = empty( $img_args['highgrey'] ) ? $img_args['lowgrey'] : $img_args['highgrey'];
$img_args['text'] = empty( $img_args['text'] ) ? $image_title : $img_args['text'];
$imageid = image_gen__create_image( $title, $img_args );
// make featured
set_post_thumbnail ( $postid, $imageid );
// attach to post
wp_update_post( array(
'ID' => $imageid,
'post_parent' => $postid,
'post_content' => print_r( $img_args, true ),
) );
}
}
}
/**
* Generate multiple posts
*
* @param int $count Number posts to generate
* @return void
*/
function post_gen_create_posts( $count = 1 ) {
$counter = 1;
do {
post_gen_create_post();
++$counter;
} while ( $counter <= $count );
}
/**
* Get random title from title list
*
* @return string HTML
*/
function post_gen_get_random_title() {
$titles = file_get_contents( plugin_dir_path(__FILE__) .'/lorem-titles.txt' );
$titles = explode( "\n", $titles );
shuffle( $titles );
return array_pop( $titles );
}
/**
* Get random content from paragraph list
*
* @param int|array $number_paragraphs One number, or array of 2
* @return string HTML
*/
function post_gen_get_random_content( $number_paragraphs = array( 3, 6 ) ) {
$paras = file_get_contents( plugin_dir_path(__FILE__) .'/lorem-paragraphs.txt' );
$paras = explode( "\n", $paras );
shuffle( $paras );
$num = post_gen_convert_to_value( $number_paragraphs );
$paras = array_slice( $paras, 0, $num );
return implode( "\n\n", $paras );
}
/**
* Helper function.
*
* Convert given input to single value
* Input could be single value, comma-separated string, array
*
* @param int|string|array $input
* @return int|string
*/
function post_gen_convert_to_value( $input ) {
// if not array, explode
if ( ! is_array( $input ) ) {
$input = explode( ',', $input );
}
$input = array_map( 'intval', $input );
// if a third param, assume last is incremental value
// get range, randomize, return
if ( isset( $input[2] ) ) {
$incr = array_pop( $input );
sort( $input );
$range = range( $input[0], $input[1], $incr );
shuffle( $range );
return array_shift( $range );
}
// otherwise, just get random in range
if ( isset( $input[1] ) ) {
sort( $input );
$input = rand( $input[0], $input[1] );
} else {
$input = $input[0];
}
return $input;
}
if ( defined('WP_CLI') && WP_CLI ) {
include plugin_dir_path( __FILE__ ) . '/post-gen-cli.php';
}