Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommonSiteTypeService
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
240
0.00% covered (danger)
0.00%
0 / 1
 publish_article
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
56
 set_featured_image_from_url
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 import_image_from_url
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 create_blog_category
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Common site type service.
4 *
5 * @package NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes
6 */
7
8namespace NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes;
9
10/**
11 * Common site type service.
12 */
13class CommonSiteTypeService {
14
15    /**
16     * Publishes a blog post.
17     *
18     * @param array $post The post.
19     * @return int|WP_Error The post ID.
20     */
21    public static function publish_article( string $title, string $excerpt, string $content, string $image = '', array $categories = array()) {
22        // Remove hooks that can slow down the operation.
23        remove_all_actions('wp_insert_post');
24        remove_all_actions('save_post');
25
26        $post_data = array(
27            'post_title'   => $title,
28            'post_content' => $content,
29            'post_excerpt' => $excerpt,
30            'post_status'  => 'publish',
31            'post_type'    => 'post',
32            'post_author'  => get_current_user_id() ?: 1,
33        );
34        // Insert post.
35        $post_id = wp_insert_post( $post_data );
36        // Validate post was created successfully.
37        if ( is_wp_error( $post_id ) || !$post_id ) {
38            return new \WP_Error( 'error_publishing_blog_post', 'Failed to create post' );
39        }
40        // Post categories.
41        if ( ! empty( $categories ) ) {
42            $category_ids = array();
43            foreach ( $categories as $category ) {
44                $category_ids[] = self::create_blog_category( $category );
45            }
46            wp_set_post_terms( $post_id, $category_ids, 'category' );
47        }
48        // Featured image.
49        if ( ! empty( $image ) ) {
50            self::set_featured_image_from_url( $image, $post_id );
51        }
52
53        return $post_id;
54    }
55
56    /**
57     * Sets the featured image for a blog post.
58     *
59     * @param string $image_url The URL of the image.
60     * @param int $post_id The ID of the post.
61     * @return void
62     */
63    private static function set_featured_image_from_url( string $image_url, int $post_id ): void {
64        $image_id = self::import_image_from_url( $image_url, $post_id );
65        if ( $image_id ) {
66            update_post_meta( $post_id, '_thumbnail_id', $image_id );
67        }
68    }
69
70    /**
71     * Imports an image from a URL.
72     *
73     * @param string $image_url The URL of the image.
74     * @param int $post_id The ID of the post.
75     * @return int The ID of the attachment.
76     */
77    private static function import_image_from_url( string $image_url, int $post_id ): int {
78        if ( ! function_exists( 'media_handle_sideload' ) ) {
79            require_once( ABSPATH . 'wp-admin/includes/media.php' );
80            require_once( ABSPATH . 'wp-admin/includes/file.php' );
81            require_once( ABSPATH . 'wp-admin/includes/image.php' );
82        }
83
84        // Add an arbitrary extension to the image URL to trick media_sideload_image to download the image.
85        $image_url     = $image_url . '?ext=.jpeg';
86        $attachment_id = media_sideload_image( $image_url, $post_id, null, 'id' );
87        if ( is_wp_error( $attachment_id ) ) {
88            return 0;
89        }
90
91        return $attachment_id;
92    }
93
94    /**
95     * Creates or gets a blog category.
96     *
97     * @param string $name The name of the category.
98     * @return int The ID of the category.
99     */
100    public static function create_blog_category( string $name ): int {
101        $category_slug = sanitize_title( $name );
102        $category      = get_term_by( 'slug', $category_slug, 'category' );
103        if ( $category ) {
104            return $category->term_id;
105        }
106        $category = wp_insert_term( $name, 'category' );
107        if ( is_wp_error( $category ) ) {
108            return 0;
109        }
110        return $category['term_id'];
111    }
112}