if ($rangestartdate != null || $rangeenddate != null) { if ($rangeenddate == null) { $result = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(agent) FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE `agent` = %s AND `last_counter` = %s", $agent, $rangestartdate) ); } else { $result = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(agent) 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(agent) 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') { // Get a complete list of search engines $searchengine_list = WP_STATISTICS\SearchEngine::getList(); // For all of them? Ok, look through the search engine list and create a SQL query string to get them all from the database. foreach ($searchengine_list as $key => $se) { $search_query .= $wpdb->prepare("`engine` = %s OR ", $key); } // Trim off the last ' OR ' for the loop above. $search_query = substr($search_query, 0, strlen($search_query) - 4); } else { // Are we getting results for all search engines or a specific one? $search_query .= $wpdb->prepare("`engine` = %s", $search_engine); } return $search_query; } /** * Get Search engine Statistics * * @param string $search_engine * @param string $time * @param string $search_by [query / name] * @return mixed */ function wp_statistics_get_search_engine_query($search_engine = 'all', $time = 'total', $search_by = 'query') { global $wpdb; //Prepare Table Name $table_name = \WP_STATISTICS\DB::table('search'); //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(*) FROM {$table_name} WHERE ({$search_query})"; // Check Sanitize Datetime if (TimeZone::isValidDate($time)) { $mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time, array('is_day' => true)); } else { $mysql_time_sql = WP_STATISTICS\Helper::mysql_time_conditions($date_column, $time, array('current_date' => true)); } //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 * @return mixed */ function wp_statistics_searchengine($search_engine = 'all', $time = 'total') { return wp_statistics_get_search_engine_query($search_engine, $time, $search_by = 'query'); } /** * Return Refer List * * @param null $time * @return int */ function wp_statistics_referrer($time = null) { global $wpdb; $timezone = array( 'today' => 0, 'yesterday' => -1, 'week' => -7, 'month' => -30, 'year' => -365, 'total' => 'ALL', ); $sql = "SELECT `referred` FROM `" . \WP_STATISTICS\DB::table('visitor') . "` WHERE referred <> ''"; if (array_key_exists($time, $timezone)) { if ($time != "total") { $sql .= " AND (`last_counter` = '" . TimeZone::getCurrentDate('Y-m-d', $timezone[$time]) . "')"; } } else { //Set Default $sql .= " AND (`last_counter` = '" . TimeZone::getCurrentDate('Y-m-d', $time) . "')"; } $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']) || stristr(get_bloginfo('url'), $url['host'])) { continue; } $urls[] = $url['scheme'] . '://' . $url['host']; } $get_urls = array_count_values($urls); return count($get_urls); }