MarketingSpecs.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Marketing Specs Handler |
| 4 | * |
| 5 | * Fetches the specifications for the marketing feature from WooCommerce.com API. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\Marketing; |
| 9 | |
| 10 | /** |
| 11 | * Marketing Specifications Class. |
| 12 | * |
| 13 | * @internal |
| 14 | * @since x.x.x |
| 15 | */ |
| 16 | class MarketingSpecs { |
| 17 | /** |
| 18 | * Name of knowledge base post transient. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const KNOWLEDGE_BASE_TRANSIENT = 'wc_marketing_knowledge_base'; |
| 23 | |
| 24 | /** |
| 25 | * Load knowledge base posts from WooCommerce.com |
| 26 | * |
| 27 | * @param string|null $topic The topic of marketing knowledgebase to retrieve. |
| 28 | * @return array |
| 29 | */ |
| 30 | public function get_knowledge_base_posts( ?string $topic ): array { |
| 31 | // Default to the marketing topic (if no topic is set on the kb component). |
| 32 | if ( empty( $topic ) ) { |
| 33 | $topic = 'marketing'; |
| 34 | } |
| 35 | |
| 36 | $kb_transient = self::KNOWLEDGE_BASE_TRANSIENT . '_' . strtolower( $topic ); |
| 37 | |
| 38 | $posts = get_transient( $kb_transient ); |
| 39 | |
| 40 | if ( false === $posts ) { |
| 41 | $request_url = add_query_arg( |
| 42 | array( |
| 43 | 'page' => 1, |
| 44 | 'per_page' => 8, |
| 45 | '_embed' => 1, |
| 46 | ), |
| 47 | 'https://woocommerce.com/wp-json/wccom/marketing-knowledgebase/v1/posts/' . $topic |
| 48 | ); |
| 49 | |
| 50 | $request = wp_remote_get( |
| 51 | $request_url, |
| 52 | array( |
| 53 | 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), |
| 54 | ) |
| 55 | ); |
| 56 | $posts = array(); |
| 57 | |
| 58 | if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) { |
| 59 | $raw_posts = json_decode( $request['body'], true ); |
| 60 | |
| 61 | foreach ( $raw_posts as $raw_post ) { |
| 62 | $post = array( |
| 63 | 'title' => html_entity_decode( $raw_post['title']['rendered'] ), |
| 64 | 'date' => $raw_post['date_gmt'], |
| 65 | 'link' => $raw_post['link'], |
| 66 | 'author_name' => isset( $raw_post['author_name'] ) ? html_entity_decode( $raw_post['author_name'] ) : '', |
| 67 | 'author_avatar' => isset( $raw_post['author_avatar_url'] ) ? $raw_post['author_avatar_url'] : '', |
| 68 | ); |
| 69 | |
| 70 | $featured_media = isset( $raw_post['_embedded']['wp:featuredmedia'] ) && is_array( $raw_post['_embedded']['wp:featuredmedia'] ) ? $raw_post['_embedded']['wp:featuredmedia'] : array(); |
| 71 | if ( count( $featured_media ) > 0 ) { |
| 72 | $image = current( $featured_media ); |
| 73 | $post['image'] = add_query_arg( |
| 74 | array( |
| 75 | 'resize' => '650,340', |
| 76 | 'crop' => 1, |
| 77 | ), |
| 78 | $image['source_url'] |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | $posts[] = $post; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | set_transient( |
| 87 | $kb_transient, |
| 88 | $posts, |
| 89 | // Expire transient in 15 minutes if remote get failed. |
| 90 | empty( $posts ) ? 900 : DAY_IN_SECONDS |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | return $posts; |
| 95 | } |
| 96 | } |
| 97 |