<?php

namespace Yoast\YoastCom\Theme;

$plugins = query_plugins(array('posts_per_page' => 2, 'orderby' => 'rand', 'tax_query' => array(array('taxonomy' => 'yoast_plugin_category', 'field' => 'term_id', 'terms' => array(303, 407)))));
$i = 0;
while ($plugins->have_posts()) {
    $plugins->the_post();
    ?>
	<?php 
    $icon = get_product_icon();
    ?>
	<a href="<?php 
    the_permalink();
    ?>
" class="more color-academy">
		<?php 
    if ($icon && 0 === $i % 2) {
        ?>
			<img src="<?php 
        echo esc_url($icon);
        ?>
" class="more__plug show-on-desktop" width="40" height="40" />
		<?php 
    }
    ?>
		<div class="more__holder arrowed-small">
			<div class="more__title"><?php 
    the_title();
    ?>
</div>
">
	<div class="row">
		<h2><?php 
_e('Yoast Premium Add-On Modules', 'yoastcom');
?>
</h2>

		<?php 
$i = 0;
?>
		<?php 
while ($plugins->have_posts()) {
    $plugins->the_post();
    ?>
			<?php 
    $icon = get_product_icon(get_the_ID(), 'diapositive');
    ?>
			<a href="<?php 
    the_permalink();
    ?>
" class="more">
				<?php 
    if ($icon && 0 === $i % 2) {
        ?>
					<img src="<?php 
        echo esc_url($icon);
        ?>
" class="more__plug show-on-desktop" width="40" height="40" />
				<?php 
    }
    ?>
<li
	class="grid <?php 
echo esc_attr($template_args['class']);
?>
 edd_cart_item"
	id="<?php 
echo esc_attr($id);
?>
"
	data-download-id="<?php 
echo esc_attr($item['id']);
?>
">
	<div class="three-seventh small-full checkout__title edd_cart_item_name">
		<?php 
$icon = get_product_icon($item['id']);
?>
		<?php 
if ($icon) {
    ?>
			<img class="more__plug more__plug--small show-on-desktop" src="<?php 
    echo esc_url($icon);
    ?>
" width="40" height="40" />
		<?php 
}
?>

		<?php 
the_checkout_item_title($item['id']);
?>
/**
 * Returns the url of an icon for a certain product (This can be free/premium/download), will try to find the icon
 * through references download_ids
 *
 * @param int  $product_id  The product to find the icon for.
 * @param bool $diapositive Whether to retrieve the diapositive version.
 *
 * @return string
 */
function get_product_icon($product_id = null, $diapositive = false)
{
    $icon = '';
    if (null === $product_id) {
        $product_id = get_the_ID();
    }
    $icon_key = 'icon';
    if ($diapositive) {
        $icon_key = 'icon_diapositive';
    }
    // If we find the icon just return it.
    if (get_post_meta($product_id, $icon_key, true)) {
        $icon = get_post_meta($product_id, $icon_key, true);
    } elseif ($download_id = get_post_meta($product_id, 'download_id', true)) {
        $icon = get_product_icon($download_id, $diapositive);
    } elseif ($premium_id = get_post_meta($product_id, 'connected_premium_plugin', true)) {
        $icon = get_product_icon($premium_id, $diapositive);
    }
    return $icon;
}