/** * Displays the widget on the front end * * @since 1.0.0 * * @param array $args * @param array $instance */ function widget($atts, $instance) { global $post; if (!empty($post->ID)) { $current_post_id = $post->ID; } else { $current_post_id = 0; } $issuem_settings = get_issuem_settings(); $out = ''; extract($atts); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); $args = array('posts_per_page' => empty($instance['posts_per_page']) ? -1 : $instance['posts_per_page'], 'post_type' => 'article', 'orderby' => empty($instance['orderby']) ? 'menu_order' : $instance['orderby'], 'order' => empty($instance['order']) ? 'DESC' : $instance['order']); $issuem_issue = array('taxonomy' => 'issuem_issue', 'field' => 'slug', 'terms' => get_active_issuem_issue()); if (!empty($instance['article_category']) && 'all' != $instance['article_category']) { if (!empty($issuem_settings['use_wp_taxonomies'])) { $cat_type = 'category'; } else { $cat_type = 'issuem_issue_categories'; } $category = array('taxonomy' => $cat_type, 'field' => 'slug', 'terms' => (array) $instance['article_category']); $args['tax_query'] = array('relation' => 'AND', $issuem_issue, $category); } else { $args['tax_query'] = array($issuem_issue); } $articles = new WP_Query($args); if ($articles->have_posts()) { while ($articles->have_posts()) { $articles->the_post(); $out .= '<div class="article_list">'; $out .= "\n\n"; if ($current_post_id == $post->ID) { $out .= '<div id="current_article">'; } $out .= issuem_replacements_args($instance['article_format'], $post); if ($current_post_id == $post->ID) { $out .= '</div>'; } $out .= '</div>'; } } if (!empty($out)) { echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } echo '<div class="issuem_article_list_widget">'; echo $out; echo '</div>'; echo $after_widget; } wp_reset_query(); }
/** * Outputs Article HTML from shortcode call * * @since 1.0.0 * * @param array $atts Arguments passed through shortcode * @return string HTML output of IssueM Articles */ function do_issuem_articles($atts, $article_format = NULL) { global $post; $issuem_settings = get_issuem_settings(); $results = ''; $articles = array(); $post__in = array(); $defaults = array('posts_per_page' => -1, 'offset' => 0, 'orderby' => 'menu_order', 'order' => 'DESC', 'article_format' => empty($article_format) ? $issuem_settings['article_format'] : $article_format, 'show_featured' => 1, 'issue' => get_active_issuem_issue(), 'article_category' => 'all', 'use_category_order' => 'false'); // Merge defaults with passed atts // Extract (make each array element its own PHP var extract(shortcode_atts($defaults, $atts)); $args = array('posts_per_page' => $posts_per_page, 'offset' => $offset, 'post_type' => 'article', 'orderby' => $orderby, 'order' => $order); if (!$show_featured) { $args['meta_query'] = array('relation' => 'AND', array('key' => '_featured_rotator', 'compare' => 'NOT EXISTS'), array('key' => '_featured_thumb', 'compare' => 'NOT EXISTS')); } $issuem_issue = array('taxonomy' => 'issuem_issue', 'field' => 'slug', 'terms' => $issue); $args['tax_query'] = array($issuem_issue); if (!empty($issuem_settings['use_wp_taxonomies'])) { $cat_type = 'category'; } else { $cat_type = 'issuem_issue_categories'; } if ('true' === $use_category_order && 'issuem_issue_categories' === $cat_type) { $count = 0; if ('all' === $article_category) { $all_terms = get_terms('issuem_issue_categories'); foreach ($all_terms as $term) { $issue_cat_meta = get_option('issuem_issue_categories_' . $term->term_id . '_meta'); if (!empty($issue_cat_meta['category_order'])) { $terms[$issue_cat_meta['category_order']] = $term->slug; } else { $terms['-' . ++$count] = $term->slug; } } } else { foreach (split(',', $article_category) as $term_slug) { $term = get_term_by('slug', $term_slug, 'issuem_issue_categories'); $issue_cat_meta = get_option('issuem_issue_categories_' . $term->term_id . '_meta'); if (!empty($issue_cat_meta['category_order'])) { $terms[$issue_cat_meta['category_order']] = $term->slug; } else { $terms['-' . ++$count] = $term->slug; } } } krsort($terms); $articles = array(); foreach ($terms as $term) { $category = array('taxonomy' => $cat_type, 'field' => 'slug', 'terms' => $term); $args['tax_query'] = array('relation' => 'AND', $issuem_issue, $category); $articles = array_merge($articles, get_posts($args)); } //And we want all articles not in a category $category = array('taxonomy' => $cat_type, 'field' => 'slug', 'terms' => $terms, 'operator' => 'NOT IN'); $args['tax_query'] = array('relation' => 'AND', $issuem_issue, $category); $articles = array_merge($articles, get_posts($args)); //Now we need to get rid of duplicates (assuming an article is in more than one category if (!empty($articles)) { foreach ($articles as $article) { $post__in[] = $article->ID; } $args['post__in'] = array_unique($post__in); $args['orderby'] = 'post__in'; unset($args['tax_query']); $articles = get_posts($args); } } else { if (!empty($article_category) && 'all' !== $article_category) { $category = array('taxonomy' => $cat_type, 'field' => 'slug', 'terms' => split(',', $article_category)); $args['tax_query'] = array('relation' => 'AND', $issuem_issue, $category); } $articles = get_posts($args); } $results .= '<div class="issuem_articles_shortcode">'; if ($articles) { $old_post = $post; foreach ($articles as $article) { $post = $article; setup_postdata($article); $results .= '<div class="issuem_article article-' . $article->ID . '">'; $results .= "\n" . issuem_replacements_args($article_format, $post) . "\n"; $results .= '</div>'; } if (get_option('issuem_api_error_received')) { $results .= '<div class="api_error"><p><a href="http://issuem.com/" target="_blank">' . __('Issue Management by ', 'issuem') . 'IssueM</a></div>'; } $post = $old_post; } else { $results .= apply_filters('issuem_no_articles_found_shortcode_message', '<h1 class="issuem-entry-title no-articles-found">' . __('No articles Found', 'issuem') . '</h1>'); } $results .= '</div>'; wp_reset_postdata(); return $results; }