閲覧数まとめ
閃きついでに総合計と週合計をフラグで制御する方式に変更してみた。これまではベタに関数をまるまるコピーして合計する時のSQL文に修正を加えていたのだが、せっかく前回閃いてフラグ的な使い方で制御できるようになったので、全体をフラグで制御することも出来るのではないかと思い試してみたところ、出来たのでこの方法でいこうと思う。まずはオリジナルのコードから。使用しているプラグインは「Post Views Counter」のままである。
function pvc_get_post_views( $post_id = 0 ) { if ( empty( $post_id ) ) $post_id = get_the_ID(); if ( is_array( $post_id ) ) $post_id = implode( ',', array_map( 'intval', $post_id ) ); else $post_id = (int) $post_id; global $wpdb; $query = "SELECT SUM(count) AS views FROM " . $wpdb->prefix . "post_views WHERE id IN (" . $post_id . ") AND type = 4";
これを以下のようにフラグで制御するように変更する。引数として’$week_flag’を用意して、デフォルト(false)時は総合計を、フラグオン(true)時は週合計を取得するようにした。
function pvc_get_post_views( $post_id = 0, $week_flag = 0 ) { if ( empty( $post_id ) ) $post_id = get_the_ID(); if ( is_array( $post_id ) ) $post_id = implode( ',', array_map( 'intval', $post_id ) ); else $post_id = (int) $post_id; global $wpdb; $datew = date( 'oW' ); if ( $week_flag ) { $query = "SELECT SUM(count) AS views FROM " . $wpdb->prefix . "post_views WHERE (id, period) IN (SELECT " . $post_id . ',' . $datew . ") AND type = 1"; } else { $query = "SELECT SUM(count) AS views FROM " . $wpdb->prefix . "post_views WHERE id IN (" . $post_id . ") AND type = 4"; }
この関数が呼ばれる場面でフラグをオンにすれば週合計を取得してくれる。その部分のコードは以下の通り。一つの関数で総合計と週合計を取得できるようになったので、表示用の変数である、$viewsと$views2のいずれも同じ関数、’pvc_get_post_views()’で取得している。
function pvc_post_views( $post_id = 0, $echo = true ) { // get all data $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id ); $options = Post_Views_Counter()->options['display']; $views = pvc_get_post_views( $post_id ); $views2 = pvc_get_post_views( $post_id, $week_flag = 1 );
ついでに今回は出力部分のHTML生成に関しても手を入れていて、表示区切りの「/」をこの中で処理できるように変更した。
$html = '<span class="comments-link">'; $html .= apply_filters( 'pvc_post_views_html' , '<span class="post-views post-' . $post_id . ' ">' . ( $options['display_style']['icon'] && $icon_class !== '' ? $icon : '' ) . ( $options['display_style']['text'] && $label !== '' ? $label : '') . number_format_i18n( $views ) . '(' . number_format_i18n( $views2 ) . ')</span>' , $post_id , $views , $views2 , $label , $icon ); $html .= '</span>';
サイトの右下に表示している「今週のトップ5」用の改修は以下の通り。
まずは’pvc_most_viewed_posts()’から。ここでもフラグ’$week_flag’を使って制御する。
function pvc_most_viewed_posts( $args = array(), $display = true, $week_flag = 0 ) { --- 中略 --- $html .= ' <a class="post-title" href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a>' . ( $args['show_post_author'] ? ' <span class="author">(' . get_the_author_meta( 'display_name', $post->post_author ) . ')</span> ' : '' ) . ( $args['show_post_views'] ? ' <span class="count">(' . number_format_i18n( pvc_get_post_views( $post->ID, $week_flag = 1 ) ) . ')</span>' : '' );
呼び出し側の’widgets.php’の記述を’pvc_most_viewed_posts()’関数に合わせて変更する。
週合計を取得して欲しいので’$week_flag’は’true’を指定する。
public function widget( $args, $instance ) { $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); $html = $args['before_widget'] . ( ! empty( $instance['title'] ) ? $args['before_title'] . $instance['title'] . $args['after_title'] : ''); $html .= pvc_most_viewed_posts( $instance, false, true ); $html .= $args['after_widget']; echo $html; }
閃きを活用して、不要な変更を削除し、スッキリしたコードに出来た。今後、元プラグインの更新時にも最小限の変更で対応できるのではないかと期待している。