BlogContent.php
48 lines
| 1 | <?php |
| 2 | namespace Kubio\Ai; |
| 3 | |
| 4 | class BlogContent { |
| 5 | public static function generate_ipsum( $paragraphs = 6 ) { |
| 6 | $faker = \Faker\Factory::create(); |
| 7 | // faker lorem |
| 8 | return $faker->paragraphs( $paragraphs, true ); |
| 9 | } |
| 10 | |
| 11 | public static function save_articles_by_category( $articles = array(), $category_id = 0 ) { |
| 12 | $posts = array(); |
| 13 | if ( ! empty( $articles ) ) { |
| 14 | foreach ( $articles as $article ) { |
| 15 | $content = self::generate_ipsum( |
| 16 | wp_rand( 5, 8 ) |
| 17 | ); |
| 18 | |
| 19 | $post_id = wp_insert_post( |
| 20 | array( |
| 21 | 'post_title' => $article['title'], |
| 22 | 'post_status' => 'publish', |
| 23 | 'post_type' => 'post', |
| 24 | 'post_content' => $content, |
| 25 | 'meta_input' => array( |
| 26 | '_kubio_created_post' => 1, |
| 27 | '_kubio_post_image_keywords' => $article['imageKeywords'], |
| 28 | ), |
| 29 | ) |
| 30 | ); |
| 31 | |
| 32 | if ( ! is_wp_error( $post_id ) ) { |
| 33 | wp_set_post_categories( $post_id, $category_id, false ); |
| 34 | $attach_id = PostImage::get_featured_image_from_keywords( |
| 35 | $article['imageKeywords'] |
| 36 | ); |
| 37 | if ( is_int( $attach_id ) ) { |
| 38 | set_post_thumbnail( $post_id, $attach_id ); |
| 39 | } |
| 40 | |
| 41 | $posts[] = $post_id; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return $posts; |
| 46 | } |
| 47 | } |
| 48 |