Plugin Directory

Changeset 1232717


Ignore:
Timestamp:
08/28/2015 12:55:55 AM (11 years ago)
Author:
s-hiroshi
Message:

Improve object cache.

Location:
category-monthly-archives/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • category-monthly-archives/trunk/category-monthly-archives.php

    r1231700 r1232717  
    22/*
    33Plugin Name: Category Monthly Archives
    4 Version: 0.0.1
     4Version: 0.0.2
    55Description: This widget display monthly archive links on category archives page.
    66Author: Hiroshi Sawai
  • category-monthly-archives/trunk/class-category-monthly-archives.php

    r1231698 r1232717  
    11<?php
     2/**
     3 * Category Monthly Archives Widget class file
     4 */
    25
    36/**
    47 * Category Monthly Archives Widget
    5  *
    68 * This Widget displays category monthly archives links on category page.
    79 * Only displays links on category page.
    8  *
    910 */
    1011class CategoryMonthlyArchives extends WP_Widget
     
    1314     * Setting widget
    1415     */
    15     function __construct()
    16     {
     16    function __construct() {
    1717        $widget_ops = array(
    1818            'description' => __(
    1919                'Displays monthly archives link on category page.',
    2020                'cmarchives'
    21             )
     21            ),
    2222        );
    2323        parent::__construct(
     
    3030    /**
    3131     * Display setting form on admin page.
    32      *
    3332     * @param object $instance received from WordPress.
    3433     */
    35     function form( $instance )
    36     {
     34    function form( $instance ) {
    3735        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0 ) );
    3836        $title    = sanitize_text_field( $instance['title'] );
     
    4139        <p>
    4240            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
    43                 <?php _e( 'Title:', 'cmarchives' ); ?>
     41                <?php esc_html_e( 'Title:', 'cmarchives' ); ?>
    4442            </label>
    4543            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
     
    4846        </p>
    4947        <p>
    50             <input class="checkbox" type="checkbox" <?php echo $count; ?>
     48            <input class="checkbox" type="checkbox" <?php echo esc_attr( $count ); ?>
    5149                   id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"
    5250                   name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>"/>
    5351            <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>">
    54                 <?php _e(
    55                     'Show post counts',
    56                     'cmarchives'
    57                 ); ?>
     52                <?php esc_html_e( 'Show post counts', 'cmarchives' ); ?>
    5853            </label>
    5954        </p>
     
    6358    /**
    6459     * Save widget form setting.
    65      *
    6660     * @param object $new_instance received from WordPress.
    6761     * @param object $old_instance received from WordPress.
    6862     */
    69     function update( $new_instance, $old_instance )
    70     {
     63    function update( $new_instance, $old_instance ) {
    7164        $instance          = $old_instance;
    7265        $new_instance      = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0 ) );
     
    8275     * @param object $instance received from WordPress.
    8376     */
    84     function widget( $args, $instance )
    85     {
     77    function widget( $args, $instance ) {
    8678        global $wp_query;
    8779
     
    9688            $this->id_base
    9789        );
    98         // get category id
    99         $cat_id = $wp_query->query_vars["cat"];
    100 
    101         // display category monthly archives link
     90        // Get category id.
     91        $cat_id = $wp_query->query_vars['cat'];
     92
     93        // Display category monthly archives link.
    10294        echo $args['before_widget'];
    10395        if ( ! empty( $title ) ) {
     
    120112        </ul>
    121113        <?php
    122         echo $args["after_widget"];
    123     }
    124 
    125     /**
    126      * Get archives markup
    127      *
    128      * customize wp-includes/general-template.php -> wp_get_archives
    129      *
    130      * @param integer $cat_id Category ID.
     114        echo $args['after_widget'];
     115    }
     116
     117    /**
     118     * Get archives markup.
     119     *
     120     * Customize wp-includes/general-template.php -> wp_get_archives
     121     *
    131122     * @param string|array $args Optional. Override defaults.
    132123     * @return string|null String when retrieving, null when displaying.
    133124     */
    134     private function get_archives( $args = '' )
    135     {
     125    private function get_archives( $args = '' ) {
    136126        global $wpdb;
    137127
    138         // get category id
     128        // Get category id
    139129        $cat_id = (int) $args['cat_id'];
    140130
     
    147137            'show_post_count' => false,
    148138            'echo'            => 1,
    149             'order'           => 'DESC'
     139            'order'           => 'DESC',
    150140        );
    151141
     
    161151
    162152        $order = strtoupper( $r['order'] );
    163         if ( $order !== 'ASC' ) {
     153        if ( 'ASC' !== $order ) {
    164154            $order = 'DESC';
    165155        }
     
    170160            $r
    171161        );
    172         $custom_sql = "LEFT JOIN $wpdb->term_relationships AS r ON $wpdb->posts.ID = r.object_ID"
    173             . " LEFT JOIN $wpdb->term_taxonomy AS t ON r.term_taxonomy_id = t.term_taxonomy_id"
    174             . " LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id";
     162        $custom_sql = "LEFT JOIN $wpdb->term_relationships AS r ON $wpdb->posts.ID = r.object_ID LEFT JOIN $wpdb->term_taxonomy AS t ON r.term_taxonomy_id = t.term_taxonomy_id LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id";
    175163        $join       = apply_filters( 'getarchives_join', $custom_sql, $r );
    176164
    177165        $output = '';
    178166
     167        $last_changed = wp_cache_get( 'last_changed', 'posts' );
     168        if ( ! $last_changed ) {
     169            $last_changed = microtime();
     170            wp_cache_set( 'last_changed', $last_changed, 'posts' );
     171        }
     172
    179173        if ( 'monthly' === $type ) {
    180             $query   = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) AS posts "
    181                 . "FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) "
    182                 . "ORDER BY post_date $order $limit";
    183             $results = $wpdb->get_results( $query );
     174            $query   = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) AS posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
     175            $key   = md5( $query );
     176            $key   = "wp_get_archives:$key:$last_changed";
     177            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     178                $results = $wpdb->get_results( $query );
     179                wp_cache_set( $key, $results, 'posts' );
     180            }
    184181            if ( ! empty( $results ) ) {
    185182                $afterafter = $r['after'];
     
    205202     * Get link.
    206203     *
    207      * refer wp-includes/link-template.php->get_month_link
     204     * Refer wp-includes/link-template.php->get_month_link
    208205     *
    209206     * @param bool|int $year False for current year. Integer of year.
     
    215212     * @since 1.0.0
    216213     */
    217     private function get_link( $year, $month, $cat_id )
    218     {
     214    private function get_link( $year, $month, $cat_id ) {
    219215        global $wp_rewrite;
    220216
     
    225221            $month = gmdate( 'm', current_time( 'timestamp' ) );
    226222        }
    227         // get monthly link
     223        // Get monthly link.
    228224        $month_link = $wp_rewrite->get_month_permastruct();
    229225        if ( ! empty( $month_link ) ) {
    230226            $month_link = str_replace( '%year%', $year, $month_link );
    231227            $month_link = str_replace( '%monthnum%', zeroise( intval( $month ), 2 ), $month_link );
    232             // add category id to query parameter
     228            // Add category id to query parameter.
    233229            return apply_filters(
    234230                'month_link',
  • category-monthly-archives/trunk/readme.txt

    r1231698 r1232717  
    55Requires at least: 4.3
    66Tested up to: 4.3
    7 Stable tag: 0.0.1
     7Stable tag: 0.0.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.