WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Платформа для конвертации и монетизации трафика
функция не описана

Automattic\WooCommerce\Admin\Features

Marketing::get_knowledge_base_posts() public WC 1.0

Load knowledge base posts from WooCommerce.com

{} Это метод класса: Marketing{}

Хуков нет.

Возвращает

Массив.

Использование

$Marketing = new Marketing();
$Marketing->get_knowledge_base_posts( $category );
$category(строка) (обязательный)
Category of posts to retrieve.

Код Marketing::get_knowledge_base_posts() WC 4.7.1

<?php
public function get_knowledge_base_posts( $category ) {

	$kb_transient = self::KNOWLEDGE_BASE_TRANSIENT;

	$categories = array(
		'marketing' => 1744,
		'coupons'   => 25202,
	);

	// Default to marketing category (if no category set on the kb component).
	if ( ! empty( $category ) && array_key_exists( $category, $categories ) ) {
		$category_id  = $categories[ $category ];
		$kb_transient = $kb_transient . '_' . strtolower( $category );
	} else {
		$category_id = $categories['marketing'];
	}

	$posts = get_transient( $kb_transient );

	if ( false === $posts ) {
		$request_url = add_query_arg(
			array(
				'categories' => $category_id,
				'page'       => 1,
				'per_page'   => 8,
				'_embed'     => 1,
			),
			'https://woocommerce.com/wp-json/wp/v2/posts'
		);

		$request = wp_remote_get( $request_url );
		$posts   = [];

		if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {
			$raw_posts = json_decode( $request['body'], true );

			foreach ( $raw_posts as $raw_post ) {
				$post = [
					'title'         => html_entity_decode( $raw_post['title']['rendered'] ),
					'date'          => $raw_post['date_gmt'],
					'link'          => $raw_post['link'],
					'author_name'   => isset( $raw_post['author_name'] ) ? html_entity_decode( $raw_post['author_name'] ) : '',
					'author_avatar' => isset( $raw_post['author_avatar_url'] ) ? $raw_post['author_avatar_url'] : '',
				];

				$featured_media = $raw_post['_embedded']['wp:featuredmedia'];

				if ( count( $featured_media ) > 0 ) {
					$image         = current( $featured_media );
					$post['image'] = add_query_arg(
						array(
							'resize' => '650,340',
							'crop'   => 1,
						),
						$image['source_url']
					);
				}

				$posts[] = $post;
			}
		}

		set_transient(
			$kb_transient,
			$posts,
			// Expire transient in 15 minutes if remote get failed.
			empty( $posts ) ? 900 : DAY_IN_SECONDS
		);
	}

	return $posts;
}