use WP_STATISTICS\Country;
use WP_STATISTICS\IP;
use WP_STATISTICS\Pages;
use WP_Statistics\Service\Analytics\DeviceDetection\DeviceHelper;
use WP_Statistics\Service\Analytics\DeviceDetection\UserAgent;
use WP_Statistics\Service\Geolocation\GeolocationFactory;
use WP_Statistics\Service\Admin\PrivacyAudit\Faqs\RequireConsent;
use WP_STATISTICS\TimeZone;
use WP_STATISTICS\User;
/**
* Get Current User IP
*/
function wp_statistics_get_user_ip()
{
return IP::getIP();
}
/**
* Get Current User Data
*
* @throws Exception
*/
function wp_statistics_get_current_user_data()
{
// Get Current User country and City
$data = wp_statistics_get_user_location();
// Get Current User IP
$data['ip'] = wp_statistics_get_user_ip();
// Get User Agent contain Browser and Platform
$data['agent'] = UserAgent::getUserAgent();
// Get User info if Registered in WordPress
if (User::is_login()) {
$data['user'] = User::get();
}
// Return
return $data;
}
/**
* Get User Statistics Data By IP
*
* @param bool $ip
* @return array
* @throws Exception
*/
function wp_statistics_get_user_location($ip = false)
{
$ip = ($ip === false ? wp_statistics_get_user_ip() : $ip);
$data = array(
'country' => '',
'city' => '',
);
// Get the location
$location = GeolocationFactory::getLocation($ip);
$country = $location['country'];
$data['country'] = array(
'code' => $location,
'name' => Country::getName($country),
'flag' => Country::flag($country)
);
// Get User City
$data['city'] = $location['city'];
return $data;
}
/**
* Get Current Users online
*
* @param array $options
* @return mixed
*/
function wp_statistics_useronline($options = array())
{
global $wpdb;
//Check Parameter
$defaults = array(
/**
* Type Of Page in WordPress
* @See Frontend\get_page_type
*
* -- Acceptable values --
*
* post -> WordPress Post single page From All of public post Type
* page -> WordPress page single page
* product -> WooCommerce product single page
* home -> Home Page website
* category -> WordPress Category Page
* post_tag -> WordPress Post Tags Page
* tax -> WordPress Term Page for all Taxonomies
* author -> WordPress Users page
* 404 -> 404 Not Found Page
* archive -> WordPress Archive Page
* all -> All Site Page
*
*/
'type' => 'all',
/**
* WordPress Query object ID
* @example array('type' => 'product', 'ID' => 5)
*/
'ID' => 0,
/**
* Get number of logged users or all users
*
* -- Acceptable values --
* false -> Get Number of all users
* true -> Get Number of all logged users in wordpress
*/
'logged_users' => false,
/**
* Get number User From Custom Country
*
* -- Acceptable values --
* ISO Country Code -> For Get List @See \wp-statistics\includes\functions\country-code.php
*
*/
'location' => 'all',
/**
* Search Filter by User agent name
* e.g : Firefox , Chrome , Safari , Unknown ..
* @see wp_statistics_get_browser_list()
*
*/
'agent' => 'all',
/**
* Search filter by User Operating System name
* e.g : Windows, iPad, Macintosh, Unknown, ..
*
*/
'platform' => 'all',
/**
* Return Of Data
*
* -- Acceptable values --
* count -> Get number of user online
* all -> Get List of User Online data
*/
'return' => 'count'
);
// Parse incoming $args into an array and merge it with $defaults
$arg = wp_parse_args($options, $defaults);
//Basic SQL
$type_request = ($arg['return'] == "all" ? '*' : 'COUNT(*)');
$sql = "SELECT {$type_request} FROM " . WP_STATISTICS\DB::table('useronline') . " as useronline JOIN " . WP_STATISTICS\DB::table('visitor') . " as visitor ON useronline.visitor_id = visitor.ID";
//Check Where Condition
$where = [];
//Check Type of Page
if ($arg['type'] != "all") {
$where[] = "`visitor`.`last_page` = " . $arg['ID'];
}
//Check Custom user
if ($arg['logged_users'] === true) {
$where[] = "`user_id` > 0";
}
//Check Location
if ($arg['location'] != "all") {
$ISOCountryCode = Country::getList();
if (array_key_exists($arg['location'], $ISOCountryCode)) {
$where[] = "`location` = '" . $arg['location'] . "'";
}
}
//Check User Agent
if ($arg['agent'] != "all") {
$where[] = "`agent` = '" . $arg['agent'] . "'";
}
//Check User Platform
if ($arg['platform'] != "all") {
$where[] = "`platform` = '" . $arg['platform'] . "'";
}
//Push Conditions to SQL
if (!empty($where)) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
//Return Number od user Online
return ($arg['return'] == "count" ? $wpdb->get_var($sql) : $wpdb->get_results($sql)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* This function get the visit statistics for a given time frame
*
* @param $time
* @param null $daily
* @return int
*/
function wp_statistics_visit($time, $daily = null)
{
global $wpdb;
//Date Column Name in visits table
$table_name = WP_STATISTICS\DB::table('visit');
$date_column = 'last_counter';
//Prepare Selector Sql
$selector = 'SUM(visit)';
if ($daily == true) {
$selector = '*';
}
//Generate Base Sql
$sql = "SELECT " . $selector . " FROM `" . $table_name . "` ";
//Create Sum Views variable
$sum = 0;
//Check if daily Report
if ($daily === true) {
// Check Sanitize Datetime
if (TimeZone::isValidDate($time)) {
$d = $time;
} else {
$d = TimeZone::getCurrentDate('Y-m-d', $time);
}
$result = $wpdb->get_row($sql . " WHERE `$date_column` = '" . $d . "'"); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if (null !== $result) {
$sum = $result->visit;
}
} else {
//Generate MySql Time Conditions
if (is_array($time) && array_key_exists('start', $time) && array_key_exists('end', $time)) {
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, '', $time);
if (!empty($mysql_time_sql)) {
$sql = $sql . ' WHERE ' . $mysql_time_sql;
}
} else {
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time);
if (!empty($mysql_time_sql)) {
$sql = $sql . ' WHERE ' . $mysql_time_sql;
}
}
//Request To database
$result = $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
//Custom Action
if ($time == "total") {
$result += WP_STATISTICS\Historical::get('visits');
}
$sum = $result;
}
return !is_numeric($sum) ? 0 : $sum;
}
/**
* This function gets the visitor statistics for a given time frame.
*
* @param $time
* @param null $daily
* @param bool $count_only
* @param array $options
* @return int|null|string
*/
function wp_statistics_visitor($time, $daily = null, $count_only = false, $options = array())
{
global $wpdb;
//Check Parameter
$defaults = array(
/**
* Type Of Page in WordPress
* @See Frontend\get_page_type
*
* -- Acceptable values --
*
* post -> WordPress Post single page From All of public post Type
* page -> WordPress page single page
* product -> WooCommerce product single page
* home -> Home Page website
* category -> WordPress Category Page
* post_tag -> WordPress Post Tags Page
* tax -> WordPress Term Page for all Taxonomies
* author -> WordPress Users page
* 404 -> 404 Not Found Page
* archive -> WordPress Archive Page
* all -> All Site Page
*
*/
'type' => 'all',
/**
* WordPress Query object ID
* @example array('type' => 'product', 'ID' => 5)
*/
'ID' => 0,
/**
* Get number User From Custom Country
*
* -- Acceptable values --
* ISO Country Code -> For Get List @See \wp-statistics\includes\functions\country-code.php
*
*/
'location' => 'all',
/**
* Search Filter by User agent name
* e.g : Firefox , Chrome , Safari , Unknown ..
* @see wp_statistics_get_browser_list()
*
*/
'agent' => 'all',
/**
* Search filter by User Operating System name
* e.g : Windows, iPad, Macintosh, Unknown, ..
*
*/
'platform' => 'all'
);
// Parse incoming $args into an array and merge it with $defaults
$arg = wp_parse_args($options, $defaults);
//Create History Visitors variable
$history = 0;
//Prepare Selector Sql
$date_column = 'last_counter';
$selector = '*';
if ($count_only == true) {
$selector = 'count(last_counter)';
}
//Generate Base Sql
if ($arg['type'] != "all" and WP_STATISTICS\Option::get('visitors_log') == true) {
$sql = "SELECT {$selector} FROM `" . WP_STATISTICS\DB::table('visitor') . "` INNER JOIN `" . WP_STATISTICS\DB::table("visitor_relationships") . "` ON `" . WP_STATISTICS\DB::table("visitor_relationships") . "`.`visitor_id` = `" . WP_STATISTICS\DB::table('visitor') . "`.`ID` INNER JOIN `" . WP_STATISTICS\DB::table('pages') . "` ON `" . WP_STATISTICS\DB::table('pages') . "`.`page_id` = `" . WP_STATISTICS\DB::table("visitor_relationships") . "` . `page_id`";
} else {
$sql = "SELECT {$selector} FROM `" . WP_STATISTICS\DB::table('visitor') . "`";
}
//Check Where Condition
$where = [];
//Check Type of Page
if ($arg['type'] != "all" and WP_STATISTICS\Option::get('visitors_log') == true) {
$where[] = "`" . WP_STATISTICS\DB::table('pages') . "`.`type`='" . $arg['type'] . "' AND `" . WP_STATISTICS\DB::table('pages') . "`.`page_id` = " . $arg['ID'];
}
//Check Location
if ($arg['location'] != "all") {
$ISOCountryCode = Country::getList();
if (array_key_exists($arg['location'], $ISOCountryCode)) {
$where[] = "`" . WP_STATISTICS\DB::table('visitor') . "`.`location` = '" . $arg['location'] . "'";
}
}
//Check User Agent
if ($arg['agent'] != "all") {
$where[] = "`" . WP_STATISTICS\DB::table('visitor') . "`.`agent` = '" . $arg['agent'] . "'";
}
//Check User Platform
if ($arg['platform'] != "all") {
$where[] = "`" . WP_STATISTICS\DB::table('visitor') . "`.`platform` = '" . $arg['platform'] . "'";
}
//Check Date Time report
if ($daily == true) {
// Check Sanitize Datetime
if (TimeZone::isValidDate($time)) {
$d = $time;
} else {
$d = TimeZone::getCurrentDate('Y-m-d', $time);
}
//Get Only Current Day Visitors
$where[] = "`" . WP_STATISTICS\DB::table('visitor') . "`.`last_counter` = '" . $d . "'";
} else {
//Generate MySql Time Conditions
if (is_array($time) && array_key_exists('start', $time) && array_key_exists('end', $time)) {
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, '', $time);
if (!empty($mysql_time_sql)) {
$sql = $sql . ' WHERE ' . $mysql_time_sql;
}
} else {
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time);
if (!empty($mysql_time_sql)) {
$where[] = $mysql_time_sql;
}
}
}
//Push Conditions to SQL
if (!empty($where)) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
//Custom Action
if ($time == "total" and $arg['type'] == "all") {
$history = WP_STATISTICS\Historical::get('visitors');
}
// Execute the SQL call, if we're only counting we can use get_var(), otherwise we use query().
if ($count_only == true) {
$sum = $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$sum += $history;
} else {
$sum = $wpdb->query($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
return $sum;
}
/**
* This function returns the statistics for a given page.
*
* @param $time
* @param string $page_uri
* @param int $id
* @param null $rangestartdate
* @param null $rangeenddate
* @param bool $type
* @return int|null|string
*
* @todo Replace all instances of this function with `ViewsModel->countViews()`.
*/
function wp_statistics_pages($time, $page_uri = '', $id = -1, $rangestartdate = null, $rangeenddate = null, $type = false)
{
global $wpdb;
//Date Column Name in visits table
$table_name = WP_STATISTICS\DB::table('pages');
$date_column = 'date';
// History Vars
$history = 0;
$history_key = null;
$history_id = null;
//Check Where Condition
$where = [];
//Check Query By Page ID or Page Url
if ($type) {
$query = $wpdb->prepare("`type` = %s", $type);
if ($id != -1) {
$query .= $wpdb->prepare(" AND `id` = %d", $id);
$history_key = 'page';
$history_id = absint($id);
}
if ($page_uri != '') {
$page_uri_sql = esc_sql($page_uri);
$query .= $wpdb->prepare(" AND `URI` = %s", $page_uri_sql);
$history_key = 'uri';
$history_id = $page_uri;
}
$where[] = apply_filters('wp_statistics_pages_where_type_query', $query, $id, $type);
} else {
// If no page URI has been passed in, get the current page URI.
if ($page_uri == '') {
$page_uri = Pages::get_page_uri();
}
$page_uri_sql = esc_sql($page_uri);
// If a page/post ID has been passed, use it to select the rows, otherwise use the URI.
if ($id != -1) {
$where[] = "`id`= " . absint($id);
$history_key = 'page';
$history_id = absint($id);
} else {
$where[] = "`URI` = '{$page_uri_sql}'";
$history_key = 'uri';
$history_id = $page_uri;
}
}
//Custom Action
if ($time == "total") {
if ($history_key && $history_id) {
$history = WP_STATISTICS\Historical::get($history_key, $history_id);
}
}
//Prepare Time
$time_array = array();
if (is_numeric($time) || TimeZone::isValidDate($time)) {
$time_array['is_day'] = true;
}
if (!is_null($rangestartdate) and !is_null($rangeenddate)) {
$time_array = array('start' => $rangestartdate, 'end' => $rangeenddate);
}
//Check MySql Time Conditions
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time, $time_array);
if (!empty($mysql_time_sql)) {
$where[] = $mysql_time_sql;
}
//Generate Base Sql
$sql = "SELECT SUM(count) FROM {$table_name}";
//Push Conditions to SQL
if (!empty($where)) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
//Request Get data
$sum = $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$sum += $history;
//Return Number Statistic
return ($sum == '' ? 0 : $sum);
}
/**
* Get top Pages between Time
*
* @param null $rangestartdate
* @param null $rangeenddate
* @param null $limit
* @param null $post_type
* @return array
*/
function wp_statistics_get_top_pages($rangestartdate = null, $rangeenddate = null, $limit = null, $post_type = null)
{
global $wpdb;
$spliceLimit = ($limit != null ? $limit : 5);
$limit = null;
// Get every unique URI from the pages database.
if ($rangestartdate != null && $rangeenddate != null) {
$whereType = ($post_type != null ? $wpdb->prepare(" AND `type`=%s", $post_type) : '');
$result = $wpdb->get_results(
$wpdb->prepare("SELECT `uri`,`id`,`type` FROM " . \WP_STATISTICS\DB::table('pages') . " WHERE `date` BETWEEN %s AND %s {$whereType} GROUP BY `id`" . ($limit != null ? ' LIMIT ' . $limit : ''), $rangestartdate, $rangeenddate), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_N);
} else {
$limitQuery = '';
if ($limit) {
$limitQuery = $wpdb->prepare(" LIMIT %d", $limit);
}
$whereType = ($post_type != null ? $wpdb->prepare(" WHERE `type`=%s", $post_type) : '');
$result = $wpdb->get_results("SELECT `uri`, `id`, `type` FROM " . \WP_STATISTICS\DB::table('pages') . " {$whereType} GROUP BY `id` {$limitQuery}", ARRAY_N); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
$total = 0;
$uris = array();
// Now get the total page visit count for each unique URI.
foreach ($result as $out) {
//Prepare item
list($url, $page_id, $page_type) = $out;
// Check if item is of specific post type (string or part of an array) or if post type is set to null
if (is_null($post_type) || $page_type == $post_type || (is_array($post_type) && in_array($page_type, $post_type))) {
// Increment the total number of results.
$total++;
//Get Page Title
$page_info = Pages::get_page_info($page_id, $page_type);
$title = mb_substr($page_info['title'], 0, 200, "utf-8");
$page_url = $page_info['link'];
// Check age Title if page id or type not exist
if ($page_info['link'] == "") {
$page_url = path_join(get_site_url(), $url);
$id = WP_STATISTICS\Pages::uri_to_id($out[0]);
$post = get_post($id);
if (is_object($post)) {
$title = esc_html($post->post_title);
} else {
if ($out[0] == '/') {
$title = get_bloginfo();
} else {
$title = '';
}
}
}
//Check Title is empty
if (empty($title)) {
$title = '-';
}
// Add the current post to the array.
if ($rangestartdate != null && $rangeenddate != null) {
$uris[] = array(
urldecode_deep($out[0]),
wp_statistics_pages('range', $out[0], -1, $rangestartdate, $rangeenddate, $post_type),
$page_id,
$title,
$page_url,
);
} else {
$uris[] = array(
urldecode_deep($out[0]),
wp_statistics_pages('total', $out[0], -1, $rangestartdate, $rangeenddate, $post_type),
$page_id,
$title,
$page_url
);
}
}
}
// If we have more than one result, let's sort them using usort.
if (count($uris) > 1) {
usort($uris, array('\WP_STATISTICS\Helper', 'compare_uri_hits_int'));
}
array_splice($uris, $spliceLimit);
return array($spliceLimit, $uris);
// return array($total, $uris);
}
/**
* Returns all unique user agents in the database.
*
* @param null $rangestartdate
* @param null $rangeenddate
* @return array
*/
function wp_statistics_ua_list($rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null && $rangeenddate != null) {
if ($rangeenddate == 'CURDATE()') {
$result = $wpdb->get_results(
$wpdb->prepare("SELECT DISTINCT agent FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `last_counter` BETWEEN %s AND CURDATE()", $rangestartdate),
ARRAY_N);
} else {
$result = $wpdb->get_results(
$wpdb->prepare("SELECT DISTINCT agent FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `last_counter` BETWEEN %s AND %s", $rangestartdate, $rangeenddate),
ARRAY_N);
}
} else {
$result = $wpdb->get_results(
"SELECT DISTINCT agent FROM `" . \WP_STATISTICS\DB::table('visitor') . "` ",
ARRAY_N);
}
$Browsers = array();
$default_browser = DeviceHelper::getBrowserList();
foreach ($result as $out) {
//Check Browser is defined in wp-statistics
if (array_key_exists(strtolower($out[0]), $default_browser)) {
$Browsers[] = esc_html($out[0]);
}
}
return $Browsers;
}
/**
* Count User By User Agent
*
* @param $agent
* @param null $rangestartdate
* @param null $rangeenddate
* @return mixed
*/
function wp_statistics_useragent($agent, $rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null || $rangeenddate != null) {
if ($rangeenddate == null) {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(*) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `agent` = %s AND `last_counter` = %s", $agent, $rangestartdate)
);
} else {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(*) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `agent` = %s AND `last_counter` BETWEEN %s AND %s", $agent, $rangestartdate, $rangeenddate)
);
}
} else {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(*) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `agent` = %s", $agent)
);
}
return $result;
}
/**
* Returns all unique platform types from the database.
*
* @param null $rangestartdate
* @param null $rangeenddate
* @return array
*/
function wp_statistics_platform_list($rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null && $rangeenddate != null) {
$result = $wpdb->get_results(
$wpdb->prepare("SELECT DISTINCT platform FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `last_counter` BETWEEN %s AND %s", $rangestartdate, $rangeenddate),
ARRAY_N);
} else {
$result = $wpdb->get_results(
"SELECT DISTINCT platform FROM `" . \WP_STATISTICS\DB::table('visitor') . "` ",
ARRAY_N);
}
$Platforms = array();
foreach ($result as $out) {
$Platforms[] = esc_html($out[0]);
}
return $Platforms;
}
/**
* Returns the count of a given platform in the database.
*
* @param $platform
* @param null $rangestartdate
* @param null $rangeenddate
* @return mixed
*/
function wp_statistics_platform($platform, $rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null && $rangeenddate != null) {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(platform) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `platform` = %s AND `last_counter` BETWEEN %s AND %s", $platform, $rangestartdate, $rangeenddate)
);
} else {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(platform) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `platform` = %s", $platform)
);
}
return $result;
}
/**
* Returns all unique versions for a given agent from the database.
*
* @param $agent
* @param null $rangestartdate
* @param null $rangeenddate
* @return array
*/
function wp_statistics_agent_version_list($agent, $rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null && $rangeenddate != null) {
$result = $wpdb->get_results(
$wpdb->prepare("SELECT DISTINCT `version` FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE agent = %s AND `last_counter` BETWEEN %s AND %s", $agent, $rangestartdate, $rangeenddate),
ARRAY_N);
} else {
$result = $wpdb->get_results(
$wpdb->prepare("SELECT DISTINCT `version` FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE agent = %s", $agent),
ARRAY_N);
}
$Versions = array();
foreach ($result as $out) {
$Versions[] = $out[0];
}
return $Versions;
}
/**
* Returns the statistics for a given agent/version pair from the database.
*
* @param $agent
* @param $version
* @param null $rangestartdate
* @param null $rangeenddate
* @return mixed
*/
function wp_statistics_agent_version($agent, $version, $rangestartdate = null, $rangeenddate = null)
{
global $wpdb;
if ($rangestartdate != null && $rangeenddate != null) {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(version) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE agent = %s AND version = %s AND `last_counter` BETWEEN %s AND %s", $agent, $version, $rangestartdate, $rangeenddate)
);
} else {
$result = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(version) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE agent = %s AND version = %s", $agent, $version)
);
}
return $result;
}
/**
* Return the SQL WHERE clause for getting the search engine.
*
* @param string $search_engine
* @return bool|string
*/
function wp_statistics_searchengine_query($search_engine = 'all')
{
global $wpdb;
$search_query = '';
// Are we getting results for all search engines or a specific one?
if (strtolower($search_engine) == 'all') {
$search_query .= "`source_channel` in ('search')";
} else {
// Are we getting results for all search engines or a specific one?
$search_query .= $wpdb->prepare("`source_name` = %s", $search_engine);
}
return $search_query;
}
/**
* Get Search engine Statistics
*
* @param string $search_engine
* @param string $time
* @param string $search_by [query / name]
* @param array $range
* @return mixed
*/
function wp_statistics_get_search_engine_query($search_engine = 'all', $time = 'total', $search_by = 'query', $range = [])
{
global $wpdb;
//Prepare Table Name
$table_name = \WP_STATISTICS\DB::table('visitor');
//Date Column table
$date_column = 'last_counter';
// Get a complete list of search engines
if ($search_by == "query") {
$search_query = wp_statistics_searchengine_query($search_engine);
}
//Generate Base Sql
$sql = "SELECT COUNT(ID) FROM {$table_name} WHERE ({$search_query})";
// Check Sanitize Datetime
if (TimeZone::isValidDate($time)) {
if (empty($range)) $range = ['is_day' => true];
} else {
if (empty($range)) $range = ['current_date' => true];
}
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time, $range);
//Generate MySql Time Conditions
if (!empty($mysql_time_sql)) {
$sql = $sql . ' AND (' . $mysql_time_sql . ')';
}
//Request Data
return $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* This function will return the statistics for a given search engine.
*
* @param string $search_engine
* @param string $time
* @param array $range
* @return mixed
*/
function wp_statistics_searchengine($search_engine = 'all', $time = 'total', $range = [])
{
return wp_statistics_get_search_engine_query($search_engine, $time, $search_by = 'query', $range);
}
/**
* Return Refer List
*
* @param null $time
* @param array $range
* @return int
*/
function wp_statistics_referrer($time = null, $range = [])
{
global $wpdb;
$sql = "SELECT `referred` FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE referred <> ''";
// Check Sanitize Datetime
if (TimeZone::isValidDate($time)) {
if (empty($range)) $range = ['is_day' => true];
} else {
if (empty($range)) $range = ['current_date' => true];
}
$mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions('last_counter', $time, $range);
//Generate MySql Time Conditions
if (!empty($mysql_time_sql)) {
$sql = $sql . ' AND (' . $mysql_time_sql . ')';
}
$result = $wpdb->get_results($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$urls = array();
foreach ($result as $item) {
$url = wp_parse_url($item->referred);
if (empty($url['host']) || stripos(get_bloginfo('url'), $url['host']) !== false) {
continue;
}
$urls[] = $url['scheme'] . '://' . $url['host'];
}
$get_urls = array_count_values($urls);
return count($get_urls);
}
/**
* Checks if consent is required for collecting user statistics.
*
* This function evaluates several conditions that determine whether consent
* is needed to collect and store user data for statistics purposes. If any
* of the conditions are not met, it indicates that consent is required.
*
* @return bool Returns true if consent is required, false otherwise.
* @since 14.10.1
*/
function wp_statistics_needs_consent()
{
// Get the current status of the consent requirement
$status = RequireConsent::getStatus();
// Check if consent is required
if ($status == 'warning') {
return true; // Consent is required
}
// Return false if consent is not required
return false;
}
在加拿大的热水炉是租赁好还是购买或买断好? ⋆ 晒鱼
Skip to content
自 1950 年代以来,租赁公司为房屋建筑商提供补贴热水器,他们将预先安装到新房屋中,而租赁公司将每月从房主那里收取租金作为交换。最初的目的是刺激天然气的使用,但随着时间的推移,它变成了一种激进、高利润、收取租金的商业模式。
购房者经常惊讶地发现他们正在签订合同。在大量法律协议的最终审查和签署过程中,通常不清楚协议的条款和条件是什么。唯一表明存在协议的迹象是“出租物品“部分下列出的“热水器“字样。
新建房屋的买家也感到惊讶,因为他们认为这是房屋购买价格的一部分。当他们试图让建筑商让他们选择退出租赁时,他们发现许多建筑商与租赁公司签订了协议,并且在家中包括一个出租单元是不可协商的。
那些知道的人在卖方市场的购买过程中没有能力处理它(类似于房屋检查如何被搁置一旁)。
一旦合同到位,买断合同的成本就变得高昂且过程具有挑战性——以至于租赁公司及其前身在过去 20 年中被多次调查、罚款并做出承诺+ 年由竞争局。
更糟糕的是,加拿大人一生平均要搬家 4.5 到 5.5 次,或者大约每 8 到 10 年搬一次家,许多人计划或不确定比这更早搬家,所以买下它听起来不像很划算。
热水炉即热水箱、热水罐,也叫热水器,在安大略省,许多房子每个月是在租用热水器的,热水器租赁、分期购买和一次性买断到底哪种更省钱?在挑选热水器的时候应该注意什么?和租赁公司签订合同有哪些注意事项?今天就给大家盘点一下。
租一个热水器要多少钱?
安大略省居民可以从 Reliance Home Comfort 或 Enercare(以前的 Direct Energy)以每月 20 至 35 加币的价格租用热水箱,具体取决于水箱的大小。
新建的房子现在比较常见的是无水箱热水器,租赁每月费用为 35 至 50 加币。
此外,协议规定这些费用每年增加 3.5% 和安大略省 CPI(Reliance)和 CPI + 2%(Enercare)中的较大者。
相比之下,购买一个新的热水箱需要 600 到 1000 加币,专业安装费需要 400 到 600 加币,总共需要 1000 到 1600 加币。
购买一个热水器要多少钱?
Tank:
传统的 40 到 60 加仑电动水箱的价格在 500 到 1,000 加币之间,而气罐的价格在 700 到 1,400 加币之间。
高效版本的价格为 1,500 至 2,000 加币。
Tankless:
即热式电动热水器价格为 600 至 900 加币,而天然气版本为 1,700 至 2,500 加币。
安装费用
以下是40 或 50 加仑油箱的安装成本价格参考:
安装公司
安装费用
Home Depot $513 to $594
Lowes $450
Local Plumber $500
值得注意的是:Home Depot的价格取决于单位:Rheem Performance Series(6 年)vs Rheem Performance Platinum 系列(12 年)。

Reliance热水器租赁费用
在 2017 年购买我们的第一套房子时,Reliance 每季度向我们收取 64.65 加币的费用,40 加仑天然气罐每月 21.55 加币或每年 258.60 加币,全新价格约为 1000 加币。

租用vs购买热水箱
根据竞争局的说法,租用热水器通常对消费者来说价值不高,而购买自己的热水器可以随着时间的推移节省大量费用。
但是,在某些情况下,可以租用而不是购买 – 特别是你无法负担新设备的前期成本,或者如果你的水非常硬并且你没有软水器导致水箱损坏经常失败。此外,如果房屋是出租物业,则具有经济意义。
举个例子:
例如拿一个 50 加仑,GSW – G850T45N-PDV-ES2 30 油箱,可在 Rona 以 940 加币购买。假设安装费为 500 加币,含税总额为 1627.20 加币。
Enercare 以每月 40 加币(每年 480 加币)的价格向客户出租,平均为 16 年,范围为 10 至 20 年。因此,在协议期间支付的总付款额将达到 4800 至 9600 加币,是水箱成本的 4.5 至 9 倍,租金超过了大约 3 年零 5 个月的购买成本:

租用与购买热水箱
如果在你的情况下购买比租房更有意义,但你已经受困于现有协议:
举个例子:
继续上面的例子,在协议签订大约 2 年后,现在使用和折旧单位的买断价格为 1,950 加币,是全新水炉价格的两倍多。
假设一个新水箱的价格为 1,600 加币,包括安装费用,并且每年的租金为 480 加币,那么购买该装置比在 4 年后租用要好。
因此,如果你认为你可以离开该单位超过 4 年,并打算在该物业中至少停留那么长时间,那么进行买断是显而易见的选择。
现在有些人可能会说:“但是通过租用而不是购买来获得的服务和‘安心‘呢?”

需要维修的可能性有多大?
热水器水箱的保修期通常为 6 至 12 年,无水箱的保修期为 1 至 5 年。它们通常可以预期使用 15 到 20 年和 10 到 15 年,除了偶尔排水外,几乎不需要维护。
也就是说,设备越复杂,就越有可能需要维修。直接排放比动力排放更简单,动力排放比无罐更简单。根据 Enercare 的年报,平均租赁年限为 16 年。

租赁协议
租赁协议涵盖哪些内容?
租赁公司将有效地运行一个热水箱,并且几乎不对其进行维护,直到它出现故障,他们将更换它并续签协议。在你家中发生可预防的事件之前,你必须与他们协商更换合同和新油箱。
值得注意的是:租赁公司对因水箱泄漏造成的任何损坏概不负责。
如何解除热水器租赁合同?
如果你最近签署了 HWT 租赁合同,请知道你可以在收到书面协议副本后的 10 天内以任何理由取消合同。
如果你已经被困在其中,有 3 种主要方法可以取消你的租赁合同,具体取决于公司和协议签订年份:
-
买断和保留产品
-
支付取消和退货费用
-
一旦单位出现故障(已达到使用寿命)终止协议
这些选项隐藏在租赁协议的细则中,因此许多消费者不知道他们的选择,并感到被困或感觉自己被骗了。
要找出最佳可行的行动方案,请按照以下步骤操作并仔细记录所有内容,以便你有记录在以后需要时参考。

打电话索要合同副本 (Reliance公司中文服务电话 中文热热线 1-855-512-5122)
了解你的权利和责任。你应该查看已签署的合同原件以及与你的设备和地址相关的文书工作,直至所安装设备的序列号。
打电话给租赁公司,在提供了我的姓名、电话号码和地址,他们告诉你合同情况,或者是没有合同只是按月收费。
在这种情况下,你没有义务继续支付,因为没有法律承诺。我建议不要向他们提供任何其他个人信息或允许他们来你家,这样他们就不可能“拿出“任何文件。
如果他们打硬仗,根据 Redditor TemporaryBoyfriend 的说法,这里有一些选择:
询问设备何时安装
以Reliance公司为例,他们给了我3个选择:(Reliance公司中文服务电话 中文热热线 1-855-512-5122)
-
预约时间,将排干和断开连接的水箱免费放下,所需要的只是出示地址的 Reliance 账单。
-
预约 Reliance 以 65 加币+HST 的价格来取货。
-
为 Reliance 安排预约,以 125 加币+HST 的价格进行排水、断开连接并带走。

获取电话参考号码
和客服联系之后,客服人员会给了你一个参考号码,以防需要回电话或者做后续的沟通。
做完以上的这一切后,以下是你所需要做的:
买一个新的热水箱
找一家当地的管道工或暖通空调公司,他们将断开并拆除旧的,将其交付给租赁公司并安装新的。
你也可以从 Home Depot 或 Lowes 购买,他们可能不会使用旧设备。
买断现有热水箱
根据你所在的公司以及设备的安装时间,你可能别无选择,只能买断合同的其余部分。价格取决于设备的品牌和型号以及使用了多少年。
提示:即使你不决定将其买断,当你打电话告诉他们你想买断并获取买断详细信息时,一些租赁公司也会为你提供 3 到 6 个月的免费租赁保留优惠。
如何取消 Reliance Home Comfort 热水器合同?
对于 2014 年 11 月 5 日之后至 2024 年 11 月 5 日开始的协议,适用于 Reliance Home Comfort 的 2014 年竞争法庭命令,其中规定:
终止:
Reliance 应允许客户或其代理人随时通过电话、传真、电子邮件或网络表格提交终止通知来终止租赁协议。
拆除:
Reliance 应允许客户或其代理人:
排空、断开、移除和/或将作为租赁协议主题的热水器归还给 Reliance;
要么要求 Reliance 在安装地址排放、断开、移除和/或取走作为租赁协议的热水器
费用:
除了任何未付的租金和利息费用外,Reliance 不得收取除下列费用外的任何退出费用:
-
最高 200 加币的终止费(单元不足 1 年)和最高 40 加币(单元超过 1 年且不足 10 年)。
-
天然气的最高取费(不包括排水或断开连接)为 65 加币,电动车的最高取费为 125 加币。
-
最高取费(包括排水和断开连接)为 125 加币。
-
仅当满足以下两个条件时,才能修复 Reliance 单位损坏的任何费用:
-
损坏的单元不足七 (7) 年;
-
损害是由 Reliance 以外的人造成的,任何此类损害由 Reliance 以外的人造成的证据属于 Reliance。
-
客户自愿同意以直接购买该单位的购买价格。
如何取消 Enercare 热水器合同?
如果你的设备是在 2010 年 9 月 15 日之前安装的,你可以随时终止协议,无需买断。
你必须将其退还给他们(免费),否则他们会以 75 加币的价格为你断开连接并移除它。
自 2010 年以来,Enercare 一直使用“仅买断使用寿命“(BOULC) 合同。
在热水器的“使用寿命“束之前,BOULC 的唯一终止选择是在合同签订时按照 Enercare 确定的价格表购买设备。

如何让你的热水器更耐用
1.更换阳极棒
阳极棒是一根金属棒,悬挂在由镁、铝或锌制成的水箱顶部,它比制造水箱的金属更容易腐蚀——”牺牲“自身以保护水箱免受腐蚀。它们的价格为 15 到 30 加币,具体取决于长度、厚度(这决定了它们的使用寿命)和材料类型(镁比铝更有效且成本更高,但因此可能不会持续很长时间)。
2. 排空水箱,直到流清为止
一年一次,或者每次你在杂物间里想到它时,打开水箱底部围嘴处的排水管,让水排出,直到它流清,理想情况下完全排空。你想排出并冲洗水箱底部的沉积物,并锻炼你的软管围兜,因为它们被卡住了。
随着时间的推移,水中的矿物质和沉积物会聚集在水箱底部并形成一层。这使得热量更难从燃烧器通过水箱底部传递到水中,从而导致燃烧器运行时间更长、水箱底部温度更高以及水箱破裂的风险增加。它还可能影响其能源效率,导致整个房子的固定装置堵塞,以及:
-
可用热水较少
-
水没那么热
-
较低的能源效率
-
储罐腐蚀并最终泄漏或失效