options = $options;
$this->query = $query;
// add_shortcode( 'docs', array( $this, 'docs' ) );
}
/**
* Get shortcode attributes.
*
* @param array $args The carousel attribute.
* @return array Mixed array of shortcode attributes.
*/
public function get_attributes(array $instance)
{
/**
* Define data by given attributes.
*/
$instance = \ItalyStrap\Core\shortcode_atts_multidimensional_array(require(ITALYSTRAP_PLUGIN_PATH . 'config/grouped-posts.php'), $instance, 'query_posts');
$instance = apply_filters('italystrap_query_posts_args', $instance);
$this->args = $instance;
return $instance;
}
/**
* Function description
*
* @param string $value [description]
* @return string [description]
*/
public function get_posts($category_term_id)
{
// $query_posts = Posts::init();
// $query_posts->get_widget_args( $instance );
// return $query_posts->output();
// echo "
";
// print_r($this->query->query);
// print_r($query_posts);
// echo "";
//
$output = '';
$args = [
'no_found_rows' => true,
'posts_per_page' => 5,
// 'cat' => $category->term_id,
'category__in' => $category_term_id,
];
$query = new WP_Query($args);
// $query = $this->query->get_attributes( $args );
if ($query->have_posts()) :
$output .= '';
while ($query->have_posts()) :
$query->the_post();
$output .= '- ';
// $output .= get_the_post_thumbnail( $query->post->ID );
$output .= get_the_post_thumbnail($query->post->ID, 'thumbnail');
$output .= '' . get_the_title() . '';
$output .= '
';
endwhile;
$output .= '
';
// $output .= ' View all';
endif;
wp_reset_postdata();
return $output;
}
/**
* Get tax query args
*
* @see https://developer.wordpress.org/reference/functions/get_terms/
*
* @param string $value [description]
* @return string [description]
*/
public function get_tax_query_args(array $tax_query_args = [], $tax = 'category')
{
$defaults = [
'taxonomy' => $tax,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
// 'include' => 0, // (array|string) Array or comma/space-separated string of term ids to include.
// 'exclude' => 0, // (array|string) Array or comma/space-separated string of term ids to exclude. If $include is non-empty, $exclude is ignored.
// 'exclude_tree' => 0, // (array|string) Array or comma/space-separated string of term ids to exclude along with all of their descendant terms. If $include is non-empty, $exclude_tree is ignored.
// 'number' => 0, // (int|string) Maximum number of terms to return. Accepts ''|0 (all) or any positive number. Default ''|0 (all).
// 'offset' => 0, // (int) The number by which to offset the terms query.
// 'fields' => 0, // (string) Term fields to query for. Accepts 'all' (returns an array of complete term objects), 'ids' (returns an array of ids), 'id=>parent' (returns an associative array with ids as keys, parent term IDs as values), 'names' (returns an array of term names), 'count' (returns the number of matching terms), 'id=>name' (returns an associative array with ids as keys, term names as values), or 'id=>slug' (returns an associative array with ids as keys, term slugs as values). Default 'all'.
// 'name' => 0, // (string|array) Optional. Name or array of names to return term(s) for.
// 'slug' => 0, // (string|array) Optional. Slug or array of slugs to return term(s) for.
// 'hierarchical' => 0, // (bool) Whether to include terms that have non-empty descendants (even if $hide_empty is set to true). Default true.
// 'search' => 0, // (string) Search criteria to match terms. Will be SQL-formatted with wildcards before and after.
// 'name__like' => 0, // (string) Retrieve terms with criteria by which a term is LIKE $name__like.
// 'description__like' => 0, // (string) Retrieve terms where the description is LIKE $description__like.
// 'pad_counts' => 0, // (bool) Whether to pad the quantity of a term's children in the quantity of each term's "count" object variable. Default false.
// 'get' => 0, // (string) Whether to return terms regardless of ancestry or whether the terms are empty. Accepts 'all' or empty (disabled).
// 'child_of' => 0, // (int) Term ID to retrieve child terms of. If multiple taxonomies are passed, $child_of is ignored. Default 0.
'parent' => 0,
];
// if ( ! empty( $this->args['include'] ) ) {
// $defaults['include'] = $this->args['include'];
// }
// if ( ! empty( $this->args['exclude'] ) ) {
// $defaults['exclude'] = $this->args['exclude'];
// }
$this->set_tax_query_args($defaults);
return wp_parse_args($tax_query_args, $defaults);
}
/**
* Continue after first call
*
* @param string $value [description]
* @return string [description]
*/
public function set_tax_query_args(&$defaults)
{
static $count = 0;
if (1 <= $count) {
return;
}
if (! empty($this->args['include'])) {
$defaults['include'] = $this->args['include'];
}
if (! empty($this->args['exclude'])) {
$defaults['exclude'] = $this->args['exclude'];
}
$count++;
}
/**
* Retrieve top 10 most-commented posts and cache the results.
*
* @param bool $force_refresh Optional. Whether to force the cache to be refreshed.
* Default false.
* @return array|WP_Error Array of WP_Post objects with the highest comment counts,
* WP_Error object otherwise.
*/
function get_category_array($force_refresh = false, $tax = 'category', array $args = [], $id = null)
{
if (! isset($id)) {
$id = $tax;
}
// Check for the category_array key in the 'category_posts' group.
$category_array = wp_cache_get("italystrap_{$id}_array", 'category_posts');
// If nothing is found, build the object.
if (true === $force_refresh || false === $category_array) {
// Grab the top 10 most commented posts.
$category_array = get_categories($this->get_tax_query_args($args, $tax));
if (! is_wp_error($category_array)) {
// In this case we don't need a timed cache expiration.
wp_cache_set("italystrap_{$id}_array", $category_array, 'category_posts');
}
}
// echo "";
// print_r($category_array);
// echo "";
return $category_array;
}
/**
* Function description
*
* @param string $value [description]
* @return string [description]
*/
public function get_categories($parent_id)
{
$args = ['parent' => $parent_id];
$categories = $this->get_category_array(false, 'category', $args, $parent_id);
if (empty($categories)) {
return;
}
$output = '';
$output .= '';
foreach ((array) $categories as $category) {
if (0 === $category->count) {
continue;
}
$output .= sprintf(
'- %s (%s)
',
esc_url(get_term_link($category)),
$category->name,
sprintf(
_n('%1$s Article', '%1$s Articles', $category->count, 'italystrap'),
number_format_i18n($category->count)
)
);
}
$output .= '
';
return $output;
}
/**
* Gets all of the posts grouped by terms for the specified
* post type and taxonomy.
*
* Results are grouped by terms and ordered by the term and post IDs.
* https://github.com/hellofromtonya/CollapsibleContent/blob/master/src/faq/template/helpers.php
* @since 1.0.0
*
* @param string $post_type_name Post type to limit query to
* @param string $taxonomy_name Taxonomy to limit query to
*
* @return array|false
*/
public function get_posts_grouped_by_term($post_type_name, $taxonomy_name)
{
$records = $this->get_posts_grouped_by_term_from_db($post_type_name, $taxonomy_name);
// d( $records );
$groupings = [];
foreach ($records as $record) {
$term_id = (int) $record->term_id;
$post_id = (int) $record->post_id;
if (! array_key_exists($term_id, $groupings)) {
$groupings[ $term_id ] = ['term_id' => $term_id, 'term_name' => $record->term_name, 'term_slug' => $record->term_slug, 'term_description' => $record->term_description, 'term_parent' => $record->term_parent, 'posts' => []];
}
$groupings[ $term_id ]['posts'][ $post_id ] = ['post_id' => $post_id, 'post_title' => $record->post_title, 'post_content' => $record->post_content, 'post_parent' => $record->post_parent, 'menu_order' => $record->menu_order, 'guid' => $record->guid];
}
return $groupings;
}
/**
* Gets all of the posts grouped by terms for the specified
* post type and taxonomy.
*
* Results are grouped by terms and ordered by the term and post IDs.
* https://github.com/hellofromtonya/CollapsibleContent/blob/master/src/faq/template/helpers.php
* @since 1.0.0
*
* @param string $post_type_name Post type to limit query to
* @param string $taxonomy_name Taxonomy to limit query to
*
* @return array|false
*/
protected function get_posts_grouped_by_term_from_db($post_type_name, $taxonomy_name)
{
global $wpdb;
$sql_query =
"SELECT t.term_id, t.name AS term_name, t.slug AS term_slug, tt.description AS term_description, tt.parent AS term_parent, p.ID AS post_id, p.post_title, p.post_content, p.post_parent, p.menu_order, p.guid
FROM {$wpdb->term_taxonomy} AS tt
INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
INNER JOIN {$wpdb->term_relationships} AS tr ON (tt.term_taxonomy_id = tr.term_taxonomy_id)
INNER JOIN {$wpdb->posts} AS p ON (tr.object_id = p.ID)
WHERE p.post_status = 'publish' AND p.post_type = %s AND tt.taxonomy = %s
GROUP BY t.term_id, p.ID
ORDER BY t.term_id, p.menu_order ASC;";
$sql_query = $wpdb->prepare($sql_query, $post_type_name, $taxonomy_name);
$results = $wpdb->get_results($sql_query);
if (! $results || ! is_array($results)) {
return false;
}
return $results;
}
/**
* Function description
*
* @param string $value [description]
* @return string [description]
*/
public function output($atts = null)
{
$output = '';
$categories = $this->get_posts_grouped_by_term('post', 'category');
d($categories);
$term = (object) [];
foreach ((array) $categories as $category) {
$term->taxonomy = 'category';
$term->term_id = $category['term_id'];
$term->slug = $category['term_slug'];
$count = is_countable($category['posts']) ? count($category['posts']) : 0;
$output .= sprintf(
'',
! empty($this->args['tax_class']) ? esc_attr($this->args['tax_class']) : '',
home_url('/') . $category['term_slug'], // esc_url( get_term_link( $category['term_id'] ) ),
// esc_url( \get_permalink_by_slug( $category['term_slug'] ) ),
// esc_url( get_term_link( $category['term_id'] ), 'category' ),
// esc_url( get_term_link( $category['term_id'] ) ),
// esc_url( get_term_link( $term ) ),
esc_html($category['term_name']),
sprintf(
_n('%1$s Article', '%1$s Articles', $count, 'italystrap'),
number_format_i18n($count)
),
$category['term_description'],
$this->get_categories($category['term_id']),
// '',
// $this->get_posts( $category['term_id'] ),
$this->get_the_grouped_posts($category['posts']),
'' //$query_posts->output( $query_args )
);
}
return $output;
}
/**
* Function description
*
* @param string $value [description]
* @return string [description]
*/
public function get_the_grouped_posts(array $posts = [], $limit = 5)
{
$output = '';
$i = 0;
$output .= '';
foreach ($posts as $post_key => $post) {
if ($i >= $limit) {
continue;
}
$this->get_the_permalink($post);
$output .= '- ';
$output .= ' ';
// $output .= get_the_post_thumbnail( $post['post_id'], 'thumbnail' );
// $output .= '' . $post['post_title'] . '';
$output .= '' . $post['post_title'] . '';
$output .= '
';
$i++;
}
$output .= '
';
return $output;
}
/**
* Get the post permalink
*
* @param array $post The post array.
*
* @return string Return the permalink of the post.
*/
public function get_the_permalink(array $post)
{
static $permalink = null;
if (! isset($permalink)) {
$permalink = get_option('permalink_structure');
}
$link = '';
return $link;
// if they're not using the fancy permalink option
if ('' !== $permalink) {
return home_url('?p=' . $post['post_id']);
}
$unixtime = strtotime($post->post_date);
$category = '';
if (strpos($permalink, '%category%') !== false) {
$cats = get_the_category($post->ID);
if ($cats) {
$cats = wp_list_sort($cats, ['term_id' => 'ASC']);
/**
* Filters the category that gets used in the %category% permalink token.
*
* @since 3.5.0
*
* @param WP_Term $cat The category to use in the permalink.
* @param array $cats Array of all categories (WP_Term objects) associated with the post.
* @param WP_Post $post The post in question.
*/
$category_object = apply_filters('post_link_category', $cats[0], $cats, $post);
$category_object = get_term($category_object, 'category');
$category = $category_object->slug;
if ($parent = $category_object->parent) {
$category = get_category_parents($parent, false, '/', true) . $category;
}
}
// show default category in permalinks, without
// having to assign it explicitly
if (empty($category)) {
$default_category = get_term(get_option('default_category'), 'category');
if ($default_category && ! is_wp_error($default_category)) {
$category = $default_category->slug;
}
}
}
$author = '';
if (strpos($permalink, '%author%') !== false) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ", date('Y m d H i s', $unixtime));
$rewritereplace =
[$date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->post_name, $post->ID, $category, $author, $post->post_name];
$permalink = home_url(str_replace($rewritecode, $rewritereplace, $permalink));
$permalink = user_trailingslashit($permalink, 'single');
}
/**
* Aggiungo lo shortcode per mostrare il loop dei plus
* @param array $atts Attributi dello shortcode.
*/
public function outputs($atts = null)
{
$query_posts = Posts::init();
$query_posts->get_widget_args($this->args);
$output = '';
/**
* Array delle categorie
* @todo Mettere a posto la cache per le varie query, leggere 10up su github
* @var array
*/
$categories = $this->get_category_array();
$output .= sprintf(
'',
! empty($this->args['tax_container_class']) ? esc_attr($this->args['tax_container_class']) : ''
);
foreach ((array) $categories as $category) :
if (0 === $category->count) {
continue;
}
/**
* @see http://wordpress.stackexchange.com/questions/169469/get-posts-from-child-categories-with-parent-category-id
*/
$query_args = [
'category__in' => $category->term_id,
// 'tax_query' => array(
// array(
// 'taxonomy' => 'category',
// 'field' => 'term_id',
// 'terms' => $category->term_id,
// ),
// ),
'update_post_term_cache' => true,
];
$output .= sprintf(
'
',
! empty($this->args['tax_class']) ? esc_attr($this->args['tax_class']) : '',
esc_url(get_term_link($category)),
$category->name,
sprintf(
_n('%1$s Article', '%1$s Articles', $category->count, 'italystrap'),
number_format_i18n($category->count)
),
$category->description,
$this->get_categories($category->term_id),
// ''
// $this->get_posts( $category->term_id )
$query_posts->output()
);
endforeach;
$output .= '
';
return $output;
}
}