function wp_statistics_pages($time, $page_uri = '', $id = -1) { // We need database and the global $WP_Statistics object access. global $wpdb, $WP_Statistics; $history = 0; $sqlstatement = ''; // If no page URI has been passed in, get the current page URI. if ($page_uri == '') { $page_uri = wp_statistics_get_uri(); } // If a page/post ID has been passed, use it to select the rows, otherwise use the URI. // Note that a single page/post ID can have multiple URI's associated with it. if ($id != -1) { $page_sql = '`id` = ' . $id; $history_key = 'page'; $history_id = $id; } else { $page_sql = "`URI` = '{$page_uri}'"; $history_key = 'uri'; $history_id = $page_uri; } // This function accepts several options for time parameter, each one has a unique SQL query string. // They're pretty self explanatory. switch ($time) { case 'today': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` = '{$WP_Statistics->Current_Date('Y-m-d')}' AND {$page_sql}"; break; case 'yesterday': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` = '{$WP_Statistics->Current_Date('Y-m-d', -1)}' AND {$page_sql}"; break; case 'week': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` BETWEEN '{$WP_Statistics->Current_Date('Y-m-d', -7)}' AND '{$WP_Statistics->Current_Date('Y-m-d')}' AND {$page_sql}"; break; case 'month': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` BETWEEN '{$WP_Statistics->Current_Date('Y-m-d', -30)}' AND '{$WP_Statistics->Current_Date('Y-m-d')}' AND {$page_sql}"; break; case 'year': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` BETWEEN '{$WP_Statistics->Current_Date('Y-m-d', -365)}' AND '{$WP_Statistics->Current_Date('Y-m-d')}' AND {$page_sql}"; break; case 'total': $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE {$page_sql}"; $history = $WP_Statistics->Get_Historical_Data($history_key, $history_id); break; default: $sqlstatement = "SELECT SUM(count) FROM {$wpdb->prefix}statistics_pages WHERE `date` = '{$WP_Statistics->Current_Date('Y-m-d', $time)}' AND {$page_sql}"; break; } // Since this function only every returns a count, just use get_var(). $result = $wpdb->get_var($sqlstatement); $result += $history; // If we have an empty result, return 0 instead of a blank. if ($result == '') { $result = 0; } return $result; }
public function Pages() { global $wp_query; // If we're a webcrawler or referral from ourselves or an excluded address don't record the page hit. if (!$this->exclusion_match) { // Don't track anything but actual pages and posts, unless we've been told to. if ($this->get_option('track_all_pages') || is_page() || is_single() || is_front_page()) { // Get the pages or posts ID if it exists and we haven't set it in the visitors code. if (!$this->current_page_id && is_object($wp_query)) { $this->current_page_id = $wp_query->get_queried_object_id(); } // Get the current page URI. $page_uri = wp_statistics_get_uri(); if ($this->get_option('strip_uri_parameters')) { $temp = explode('?', $page_uri); if ($temp !== false) { $page_uri = $temp[0]; } } // Limit the URI length to 255 characters, otherwise we may overrun the SQL field size. $page_uri = substr($page_uri, 0, 255); // If we have already been to this page today (a likely scenario), just update the count on the record. $sql = $this->db->prepare("UPDATE {$this->tb_prefix}statistics_pages SET `count` = `count` + 1 WHERE `date` = '{$this->Current_Date('Y-m-d')}' AND `uri` = %s", $page_uri); $this->result = $this->db->query($sql); // If the update failed (aka the record doesn't exist), insert a new one. Note this may drop a page hit if a race condition // exists where two people load the same page a the roughly the same time. In that case two inserts would be attempted but // there is a unique index requirement on the database and one of them would fail. if (!$this->result) { $this->db->insert($this->tb_prefix . "statistics_pages", array('uri' => $page_uri, 'date' => $this->Current_date('Y-m-d'), 'count' => 1, 'id' => $this->current_page_id)); } } } }