functions.php 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
namespace guaraci;

/**
 * Função para resolver o problema exposto em https://mekshq.com/passing-variables-via-get_template_part-wordpress/
 * @param string $template_name nome do template localizado dentro pasta template-parts do tema
 * @param array $params
 */
function template_part($template_name, $params = []){
10
    global $wp_query;
11 12
    $template_filename = locate_template("template-parts/{$template_name}.php");

13 14 15 16
    if(empty($template_filename)){
        return;
    }

17 18 19
    extract($params);

    include $template_filename;;
20 21
}

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * Pagination
 */
function pagination_links() {
    global $wp_query;
    if ( $wp_query->max_num_pages > 1 ) {

        $big = 999999999; // need an unlikely integer

        echo paginate_links( array(
            'base' => str_replace( $big , '%#%' , esc_url( get_pagenum_link( $big ) ) ) ,
            'format' => '?paged=%#%' ,
            'current' => max( 1 , get_query_var( 'paged' ) ) ,
            'total' => $wp_query->max_num_pages ,
            'prev_text' => __( '<' , 'green' ) ,
            'next_text' => __( '>' , 'green' )
        ) );
    }
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

/**
 * Retorna uma lista de termos ordenados pela data de utilização.
 *
 * @param string $taxonomy
 * @param integer $limit
 * @param integer $paged
 * @return \WP_Term[]
 */
function get_terms_order_by_posts_publish_date(string $taxonomy, int $limit = 10, int $paged = 1){
    global $wpdb;

    if(false) $wpdb = new \wpdb(1,2,3,4);

    $offset = $limit * ($paged - 1);

    $sql = "
        SELECT 
            t.term_id,
            t.name,
            t.slug,
            tx.term_taxonomy_id,
            tx.taxonomy,
            tx.description,
            tx.parent,
            tx.count
        FROM 
            $wpdb->terms t
            LEFT JOIN $wpdb->term_taxonomy tx ON tx.term_id = t.term_id
            LEFT JOIN $wpdb->term_relationships tr ON tr.term_taxonomy_id = tx.term_taxonomy_id
            LEFT JOIN $wpdb->posts p ON p.ID = tr.object_id
        
        WHERE 
            tx.taxonomy = '$taxonomy' AND
            p.post_type = 'coluna' AND

            tr.object_id IN (
                SELECT 
                    MAX(object_id) 
                FROM 
                    $wpdb->term_relationships 
                WHERE 
                    term_taxonomy_id = tr.term_taxonomy_id
            )
            
            GROUP BY tx.term_taxonomy_id
            ORDER BY p.post_date DESC
        
        LIMIT $limit
        OFFSET $offset
        ";

    $results = $wpdb->get_results($sql);

    $terms = array_map(function($term){ return new \WP_Term($term); }, $results);

    return $terms;
Isaque Melo's avatar
Isaque Melo committed
99 100 101 102 103 104 105 106 107 108
}

function get_posts_ranking() {
    if(!class_exists('\\AjaxPageviews')){
        return;
    }

    $cache_key = 'most_viewed_within_hour';

    // use wp cache for optimization
Isaque Melo's avatar
Isaque Melo committed
109
    $ids = wp_cache_get( $cache_key );
Isaque Melo's avatar
Isaque Melo committed
110 111

    if ( $ids === false ) {
112
        $most_viewed = \AjaxPageviews::get_top_viewed(500, ['post_type' => 'post', 'from' => date('Y-m-d H:00', strtotime('-24 hour'))]);
Isaque Melo's avatar
Isaque Melo committed
113 114 115 116 117 118
        $ids = array();

        foreach ( $most_viewed as $post => $value ) {
            array_push( $ids, $value->post_id );
        }

119
        // stores in cache for 1 hour
120
        wp_cache_add( $cache_key, $ids, HOUR_IN_SECONDS );
Isaque Melo's avatar
Isaque Melo committed
121 122
    }

123
    //var_dump($ids);
Isaque Melo's avatar
Isaque Melo committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    return $ids;
}

function get_post_ranking_position( $post_id ) {
    $posts_ranking = get_posts_ranking(); // the function already returns a cached value

    // linear search
    foreach ( $posts_ranking as $key => $id ) {
        if ($post_id == $id) {
            //print('The post(' . $id .  ') rank position is ' . ($key + 1));
            return $key + 1;
        }
    }

    return false;
140
}