[ Index ] |
MailPress 7.2 |
[ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] [ Statistics ] |
[Summary view] [Print] [Text view]
1 <?php 2 class MP_Dashboard_users_per_post extends MP_WP_Dashboard_widget_ 3 { 4 var $id = 'mp_users_per_post'; 5 6 function widget() 7 { 8 global $wpdb, $wp_locale; 9 $empty = -1; 10 11 // Posts Activity 12 13 $_Item_ids = $_Items = array(); 14 15 $dend = date( 'Y-m-d' ); $y = substr( $dend,0,4 ); $m = substr( $dend,5,2 ); $d = substr( $dend,8,2 ); 16 $dbeg = date( 'Y-m-d',mktime( 0, 0, 0, $m, $d-66, $y ) ); $y = substr( $dbeg,0,4 ); $m = substr( $dbeg,5,2 ); $d = substr( $dbeg,8,2 ); 17 18 // get all in one query 19 $query = " SELECT sdate, '1', slib, sum( scount ) as scount FROM $wpdb->mp_stats WHERE stype = 'c' AND sdate BETWEEN '%s' AND '%s' GROUP BY 1,2,3 20 UNION 21 SELECT '%s', '0', slib, sum( scount ) FROM $wpdb->mp_stats WHERE stype = 'c' AND sdate < '%s' GROUP BY 1,2,3 22 ORDER BY 1,2,3;"; 23 $items = $wpdb->get_results( $wpdb->prepare( $query, $dbeg, $dend, $dbeg, $dbeg ) ); 24 25 foreach ( $items as $item ) 26 { 27 if ( isset( $_Items[$item->sdate][$item->slib] ) ) $_Items[$item->sdate][$item->slib] += $item->scount; 28 else $_Items[$item->sdate][$item->slib] = $item->scount; 29 $_Item_ids[$item->slib] = $_Items[$item->sdate][$item->slib]; 30 } 31 32 // clean up // 33 foreach( $_Item_ids as $item_id => $x ) 34 { 35 $vide = true; 36 foreach ( $_Items as $date => $item ) 37 { 38 foreach ( $item as $post_id => $scount ) 39 { 40 if ( $item_id == $post_id && $scount > 0 ) 41 { 42 $vide = false; 43 break 2; 44 } 45 } 46 } 47 if ( $vide ) unset( $_Item_ids[$item_id] ); 48 } 49 50 // keep last 5 most subscribed 51 arsort( $_Item_ids ); 52 $_Item_ids = array_slice( $_Item_ids, 0, 5, 1 ); 53 54 // clean up // 55 foreach ( $_Items as $date => $item ) 56 { 57 foreach ( $item as $post_id => $scount ) 58 if ( !isset( $_Item_ids[$post_id] ) ) unset( $_Items[$date][$post_id] ); 59 if ( empty( $_Items[$date] ) ) unset( $_Items[$date] ); 60 } 61 62 $chxl_y = 0; 63 $chxl_day = $chxl_month = $chxl_year = $chds = $prev_chds = $colors = $item_ids = array(); 64 65 $time = $dbeg; $wy = $y; $wm = $m; $wd = $d; 66 67 do 68 { 69 // data 70 foreach( $_Item_ids as $item_id => $v ) 71 { 72 if ( isset( $_Items[$time][$item_id] ) ) 73 { 74 $total = ( isset( $prev_chds[$item_id] ) && is_numeric( $prev_chds[$item_id] ) && ( $prev_chds[$item_id] > -1 ) ) ? $prev_chds[$item_id] + $_Items[$time][$item_id] : $_Items[$time][$item_id]; 75 if ( $total < 0 ) $total = $empty; 76 $chds[$item_id][] = $prev_chds[$item_id] = $total; 77 } 78 elseif ( isset( $prev_chds[$item_id] ) && ( $prev_chds[$item_id] != '' ) ) 79 { 80 $chds[$item_id][] = $prev_chds[$item_id]; 81 } 82 else $chds[$item_id][] = $prev_chds[$item_id] = $empty; 83 84 // y 85 $chxl_y = max( $prev_chds[$item_id], $chxl_y ); 86 } 87 88 // axis 89 // x 90 if ( empty( $chxl_year ) ) $chxl_year[] = $wy; 91 elseif ( '0101' == substr( $time,5,2 ) . substr( $time,8,2 ) ) $chxl_year[] = substr( $time,0,4 ); 92 else $chxl_year[] = ''; 93 94 $chxl_month[] = ( '15' == substr( $time,8,2 ) ) ? $wp_locale->get_month_abbrev( $wp_locale->get_month( substr( $time,5,2 ) ) ) : ''; 95 96 if ( '01' == substr( $time,8,2 ) ) $chxl_day[] = '01'; 97 elseif ( '15' == substr( $time,8,2 ) ) $chxl_day[] = '15'; 98 else $chxl_day[] = ''; 99 100 $time = date( 'Y-m-d',mktime( 0, 0, 0, $m, ++$d, $y ) ); 101 } while ( $time <= $dend ); 102 103 foreach( $_Item_ids as $item_id => $v ) 104 { 105 $item_ids[] = $item_id; 106 $values[] = join( ',', $chds[$item_id] ); 107 $colors[] = $this->post_color( $item_id ); 108 } 109 110 if ( empty( $values ) ) return; 111 112 $args = array(); 113 $args['cht'] = 'lc'; 114 $args['chs'] = $this->widget_size( '570x330' ); 115 $args['chxt'] = 'x,y,x,x'; 116 $args['chxtc']= '0,3'; 117 $args['chxl'] = '0:|' . join( '|', $chxl_day ) . '|1:||' . $chxl_y . '|2:|' . join( '|', $chxl_month ) . '|3:|' . join( '|', $chxl_year ); 118 $args['chds'] = "0,{$chxl_y}"; 119 $args['chdlp']= 'b'; 120 $args['chdl'] = join( '|', $item_ids ); 121 $args['chco'] = join( ',', $colors ); 122 $args['chd'] = 't:' . join( '|', $values ); 123 $url = esc_url( add_query_arg( $args, $this->url ) ); 124 125 echo '<div style="text-align:center;"><img style="width:100%;" src="' . $url . '" alt="' . __( 'Comments subscribers per post', 'MailPress' ) . '" /></div>' . "\r\n"; 126 } 127 128 function post_color( $p ) 129 { 130 $x = pow( 3,$p ); 131 $y = intval( $p/3 ) * 10; 132 $p = ( 355/113 )* pow( $p,2 ); 133 $c = ''; 134 $c = sprintf( "%02X", $this->my_bcmod( $p, '255' ) ) . sprintf( "%02X", $this->my_bcmod( $y, '255' ) ) . sprintf( "%02X", $this->my_bcmod( $x, '255' ) ); 135 return $c; 136 } 137 138 function my_bcmod( $x, $y ) 139 { 140 $y = (int) $y; 141 142 // how many numbers to take at once? carefull not to exceed ( int ) 143 $take = 5; 144 $mod = ''; 145 do 146 { 147 $a = $mod.substr( $x, 0, $take ); 148 $a = (int) $a; 149 $x = substr( $x, $take ); 150 $mod = $a % $y; 151 } 152 while ( strlen( $x ) ); 153 return ( int )$mod; 154 } 155 } 156 new MP_Dashboard_users_per_post( __( 'MailPress - Comments subscribers per post', 'MailPress' ) );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue May 19 15:55:14 2020 | Cross-referenced by PHPXref 0.7.1 |