Esempio n. 1
0
function wp_statistics_get_top_pages()
{
    global $wpdb;
    // Get every unique URI from the pages database.
    $result = $wpdb->get_results("SELECT DISTINCT uri FROM {$wpdb->prefix}statistics_pages", ARRAY_N);
    $total = 0;
    $uris = array();
    // Now get the total page visit count for each unique URI.
    foreach ($result as $out) {
        // Increment the total number of results.
        $total++;
        // Retreive the post ID for the URI.
        $id = wp_statistics_uri_to_id($out[0]);
        // Lookup the post title.
        $post = get_post($id);
        if (is_object($post)) {
            $title = $post->post_title;
        } else {
            if ($out[0] == '/') {
                $title = get_bloginfo();
            } else {
                $title = '';
            }
        }
        // Add the current post to the array.
        $uris[] = array($out[0], wp_statistics_pages('total', $out[0]), $id, $title);
    }
    // If we have more than one result, let's sort them using usort.
    if (count($uris) > 1) {
        // Sort the URI's based on their hit count.
        usort($uris, 'wp_stats_compare_uri_hits');
    }
    return array($total, $uris);
}
	function wp_statistics_purge_data( $purge_days ) {
		GLOBAL $wpdb, $WP_Statistics;
		
		// If it's less than 30 days, don't do anything.
		if($purge_days > 30) {
			// Purge the visit data.
			$table_name = $wpdb->prefix . 'statistics_visit';
			$date_string = $WP_Statistics->current_date( 'Y-m-d', '-' . $purge_days); 
	 
			$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE `last_counter` < %s", $date_string ) );
			
			if($result) {
				// Update the historical count with what we purged.
				$historical_result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}statistics_historical SET value = value + %d WHERE `category` = 'visits'", $result ) );

				if( $historical_result == 0 ) {
					$wpdb->insert( $wpdb->prefix . "statistics_historical", array( 'value' => $result, 'category' => 'visits', 'page_id' => -2, 'uri' => '-2' ) );
				}
			
				$result_string = sprintf(__('%s data older than %s days purged successfully.', 'wp_statistics'), '<code>' . $table_name . '</code>', '<code>' . $purge_days . '</code>');
			} else {
				$result_string = sprintf(__('No records found to purge from %s!', 'wp_statistics'), '<code>' . $table_name . '</code>' ); 
			}

			// Purge the visitors data.
			$table_name = $wpdb->prefix . 'statistics_visitor';

			$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE `last_counter` < %s", $date_string ) );
			
			if($result) {
				// Update the historical count with what we purged.
				$historical_result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}statistics_historical SET value = value + %d WHERE `category` = 'visitors'", $result ) );
				
				if( $historical_result == 0 ) {
					$wpdb->insert( $wpdb->prefix . "statistics_historical", array( 'value' => $result, 'category' => 'visitors', 'page_id' => -1, 'uri' => '-1' ) );
				}
			
				$result_string .= '<br>' . sprintf(__('%s data older than %s days purged successfully.', 'wp_statistics'), '<code>' . $table_name . '</code>', '<code>' . $purge_days . '</code>');
			} else {
				$result_string .= '<br>' . sprintf(__('No records found to purge from %s!', 'wp_statistics'), '<code>' . $table_name . '</code>' ); 
			}

			// Purge the exclusions data.
			$table_name = $wpdb->prefix . 'statistics_exclusions';

			$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE `date` < %s", $date_string ) );
			
			if($result) {
				$result_string .= '<br>' . sprintf(__('%s data older than %s days purged successfully.', 'wp_statistics'), '<code>' . $table_name . '</code>', '<code>' . $purge_days . '</code>');
			} else {
				$result_string .= '<br>' . sprintf(__('No records found to purge from %s!', 'wp_statistics'), '<code>' . $table_name . '</code>' ); 
			}

			// Purge the search data.
			$table_name = $wpdb->prefix . 'statistics_search';

			$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE `last_counter` < %s", $date_string ) );
			
			if($result) {
				$result_string .= '<br>' . sprintf(__('%s data older than %s days purged successfully.', 'wp_statistics'), '<code>' . $table_name . '</code>', '<code>' . $purge_days . '</code>');
			} else {
				$result_string .= '<br>' . sprintf(__('No records found to purge from %s!', 'wp_statistics'), '<code>' . $table_name . '</code>' ); 
			}

			// Purge the pages data, this is more complex as we want to save the historical data per page.
			$table_name = $wpdb->prefix . 'statistics_pages';
			$historical = 0;
			
			// The first thing we need to do is update the historical data by finding all the unique pages.
			$result = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT uri FROM {$table_name} WHERE `date` < %s", $date_string ) );

			// If we have a result, let's store the historical data.		
			if( $result ) {
				// Loop through all the unique rows that were returned.
				foreach( $result as $row ) {
					// Use the unique rows to get a total count from the database of all the data from the given URIs/Pageids that we're going to delete later.
					$historical = $wpdb->get_var( $wpdb->prepare( "SELECT sum(count) FROM {$table_name} WHERE `uri` = %s AND `date` < %s", $row->uri, $date_string));
		
					// Do an update of the historical data.
					$uresult = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}statistics_historical SET `value` = value + %d WHERE `uri` = %s AND `category` = 'uri'", $historical, $row->uri, $date_string ) );

					// If we failed it's because this is the first time we've seen this URI/pageid so let's create a historical row for it.
					if( $uresult == 0 ) {
						$wpdb->insert( $wpdb->prefix . "statistics_historical", array( 'value' => $historical, 'category' => 'uri', 'uri' => $row->uri, 'page_id' => wp_statistics_uri_to_id($row->uri) ) );
					}
				}
			}
			
			// Now that we've done all of the required historical data storage, we can actually delete the data from the database.
			$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE `date` < %s", $date_string ) );
			
			if($result) {
				$result_string .= '<br>' . sprintf(__('%s data older than %s days purged successfully.', 'wp_statistics'), '<code>' . $table_name . '</code>', '<code>' . $purge_days . '</code>');
			} else {
				$result_string .= '<br>' . sprintf(__('No records found to purge from %s!', 'wp_statistics'), '<code>' . $table_name . '</code>' ); 
			}
			
			if( $WP_Statistics->get_option('prune_report') == true ) {
				$blogname = get_bloginfo('name');
				$blogemail = get_bloginfo('admin_email');
				
				$headers[] = "From: $blogname <$blogemail>";
				$headers[] = "MIME-Version: 1.0";
				$headers[] = "Content-type: text/html; charset=utf-8";

				if( $WP_Statistics->get_option('email_list') == '' ) { $WP_Statistics->update_option( 'email_list', $blogemail ); }
				
				wp_mail( $WP_Statistics->get_option('email_list'), __('Database pruned on', 'wp_statistics') . ' ' . $blogname, $result_string, $headers );
			}

			return $result_string;
		} else {
			return __('Please select a value over 30 days.', 'wp_statistics');
		}
	}
Esempio n. 3
0
		postboxes.add_postbox_toggles(pagenow);
	});
</script>
<?php 
if (array_key_exists('page-uri', $_GET)) {
    $pageuri = $_GET['page-uri'];
} else {
    $pageuri = null;
}
if (array_key_exists('page-id', $_GET)) {
    $pageid = (int) $_GET['page-id'];
} else {
    $pageid = null;
}
if ($pageuri && !$pageid) {
    $pageid = wp_statistics_uri_to_id($pageuri);
}
$post = get_post($pageid);
if (is_object($post)) {
    $title = $post->post_title;
} else {
    $title = "";
}
$urlfields = "&page-id={$pageid}";
if ($pageuri) {
    $urlfields .= "&page-uri={$pageuri}";
}
$daysToDisplay = 20;
if (array_key_exists('hitdays', $_GET)) {
    $daysToDisplay = intval($_GET['hitdays']);
}
Esempio n. 4
0
function wp_statistics_generate_page_postbox_content($pageuri, $pageid, $days = 20, $chart_title = null, $rangestart = '', $rangeend = '')
{
    global $WP_Statistics;
    if (!$WP_Statistics->get_option('pages')) {
        return;
    }
    if ($chart_title == null) {
        $chart_title = __('Page Trending Stats', 'wp_statistics');
    }
    if ($pageuri && !$pageid) {
        $pageid = wp_statistics_uri_to_id($pageuri);
    }
    $post = get_post($pageid);
    if (is_object($post)) {
        $title = $post->post_title;
    } else {
        $title = "";
    }
    $urlfields = "&page-id={$pageid}";
    if ($pageuri) {
        $urlfields .= "&page-uri={$pageuri}";
    }
    list($daysToDisplay, $rangestart_utime, $rangeend_utime) = wp_statistics_date_range_calculator($days, $rangestart, $rangeend);
    ?>
						<script type="text/javascript">
						var pages_chart;
						jQuery(document).ready(function() {
<?php 
    echo "var page_data_line = [";
    for ($i = $daysToDisplay; $i >= 0; $i--) {
        $stat = wp_statistics_pages('-' . $i, $pageuri, $pageid);
        echo "['" . $WP_Statistics->Real_Current_Date('Y-m-d', '-' . $i, $rangeend_utime) . "'," . $stat . "], ";
    }
    echo "];\n";
    $tickInterval = $daysToDisplay / 20;
    if ($tickInterval < 1) {
        $tickInterval = 1;
    }
    ?>
							pages_jqchart = jQuery.jqplot('page-stats', [page_data_line], {
								title: {
									text: '<b>' + <?php 
    echo json_encode(__($chart_title, 'wp_statistics'));
    ?>
 + '</b>',
									fontSize: '12px',
									fontFamily: 'Tahoma',
									textColor: '#000000',
									},
								axes: {
									xaxis: {
											min: '<?php 
    echo $WP_Statistics->Real_Current_Date('Y-m-d', '-' . $daysToDisplay, $rangeend_utime);
    ?>
',
											max: '<?php 
    echo $WP_Statistics->Real_Current_Date('Y-m-d', '-0', $rangeend_utime);
    ?>
',
											tickInterval:  '<?php 
    echo $tickInterval;
    ?>
 day',
											renderer:jQuery.jqplot.DateAxisRenderer,
											tickRenderer: jQuery.jqplot.CanvasAxisTickRenderer,
											tickOptions: { 
												angle: -45,
												formatString:'%b %#d',
												showGridline: false, 
												},
										},										
									yaxis: {
											min: 0,
											padMin: 1.0,
											label: <?php 
    echo json_encode(__('Number of Hits', 'wp_statistics'));
    ?>
,
											labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer,
											labelOptions: {
												angle: -90,
												fontSize: '12px',
												fontFamily: 'Tahoma',
												fontWeight: 'bold',
											},
										}
									},
								legend: {
									show: true,
									location: 's',
									placement: 'outsideGrid',
									labels: [ '<?php 
    echo $pageid . ' - ' . $title;
    ?>
' ],
									renderer: jQuery.jqplot.EnhancedLegendRenderer,
									rendererOptions:
										{
											numberColumns: 5, 
											disableIEFading: false,
											border: 'none',
										},
									},
								highlighter: {
									show: true,
									bringSeriesToFront: true,
									tooltipAxes: 'xy',
									formatString: '%s:&nbsp;<b>%i</b>&nbsp;',
									tooltipContentEditor: tooltipContentEditor,
								},
								grid: {
								 drawGridlines: true,
								 borderColor: 'transparent',
								 shadow: false,
								 drawBorder: false,
								 shadowColor: 'transparent'
								},
							} );

							function tooltipContentEditor(str, seriesIndex, pointIndex, plot) {
								// display series_label, x-axis_tick, y-axis value
								return plot.legend.labels[seriesIndex] + ", " + str;;
							}
							
							jQuery(window).resize(function() {
								JQPlotPagesChartLengendClickRedraw()
							});

							function JQPlotPagesChartLengendClickRedraw() {
								pages_jqchart.replot( {resetAxes: ['yaxis'] } );
								jQuery('div[id="page-stats"] .jqplot-table-legend').click(function() {
									JQPlotPagesChartLengendClickRedraw();
								});
							}
							
							jQuery('div[id="page-stats"] .jqplot-table-legend').click(function() {
								JQPlotPagesChartLengendClickRedraw()
							});
						});
						</script>
						
						<div id="page-stats" style="height:500px;"></div>

<?php 
}