public static function show_results($_type = 'recent', $_id = 'p0', $_column = 'id', $_args = array())
 {
     // Initialize default values, if not specified
     $_args = array_merge(array('custom_where' => '', 'more_columns' => '', 'join_tables' => '', 'having_clause' => '', 'order_by' => '', 'total_for_percentage' => 0, 'as_column' => '', 'filter_op' => 'equals', 'use_date_filters' => true), $_args);
     $column = !empty($_args['as_column']) ? $_column : wp_slimstat_db::get_table_identifier($_column) . $_column;
     // Get ALL the results
     $temp_starting = wp_slimstat_db::$filters_normalized['misc']['start_from'];
     $temp_limit_results = wp_slimstat_db::$filters_normalized['misc']['limit_results'];
     wp_slimstat_db::$filters_normalized['misc']['start_from'] = 0;
     wp_slimstat_db::$filters_normalized['misc']['limit_results'] = 9999;
     //$count_all_results = wp_slimstat_db::count_records();
     switch ($_type) {
         case 'recent':
             $all_results = wp_slimstat_db::get_recent($column, $_args['custom_where'], $_args['join_tables'], $_args['having_clause'], $_args['order_by'], $_args['use_date_filters']);
             break;
         case 'popular':
             $all_results = wp_slimstat_db::get_popular($column, $_args['custom_where'], $_args['more_columns'], $_args['having_clause'], $_args['as_column']);
             break;
             //case 'popular_complete':
             //	$all_results = wp_slimstat_db::get_popular_complete($column, $_args['custom_where'], $_args['join_tables'], $_args['having_clause']);
             //	break;
         //case 'popular_complete':
         //	$all_results = wp_slimstat_db::get_popular_complete($column, $_args['custom_where'], $_args['join_tables'], $_args['having_clause']);
         //	break;
         case 'popular_outbound':
             $all_results = wp_slimstat_db::get_popular_outbound();
             break;
         default:
     }
     // Restore the filter
     wp_slimstat_db::$filters_normalized['misc']['start_from'] = $temp_starting;
     wp_slimstat_db::$filters_normalized['misc']['limit_results'] = $temp_limit_results;
     // Slice the array
     $results = array_slice($all_results, wp_slimstat_db::$filters_normalized['misc']['start_from'], wp_slimstat_db::$filters_normalized['misc']['limit_results']);
     $count_page_results = count($results);
     if ($count_page_results == 0) {
         echo '<p class="nodata">' . __('No data to display', 'wp-slimstat') . '</p>';
         return true;
     }
     // Sometimes we use aliases for columns
     if (!empty($_args['as_column'])) {
         $_column = trim(str_replace('AS', '', $_args['as_column']));
     }
     self::report_pagination($_id, $count_page_results, count($all_results));
     $is_expanded = wp_slimstat::$options['expand_details'] == 'yes' ? ' expanded' : '';
     // Traffic sources: display pageviews with no referrer
     if ($_column == 'referer') {
         $count_all = wp_slimstat_db::count_records();
         $count_no_referer = wp_slimstat_db::count_records('(t1.referer IS NULL OR t1.referer = "")');
         $percentage = number_format(sprintf("%01.2f", 100 * $count_no_referer / $count_all), 2, wp_slimstat_db::$formats['decimal'], wp_slimstat_db::$formats['thousand']);
         echo "<p>Direct Access <span>{$percentage}%</span> <b class='slimstat-row-details{$is_expanded}'>Hits: {$count_no_referer}</b></p>";
     }
     for ($i = 0; $i < $count_page_results; $i++) {
         $row_details = $percentage = '';
         $element_pre_value = '';
         $element_value = $results[$i][$_column];
         // Convert the IP address
         if (!empty($results[$i]['ip'])) {
             $results[$i]['ip'] = long2ip($results[$i]['ip']);
         }
         // Some columns require a special pre-treatment
         switch ($_column) {
             case 'browser':
                 if (!empty($results[$i]['user_agent']) && wp_slimstat::$options['show_complete_user_agent_tooltip'] == 'yes') {
                     $element_pre_value = self::inline_help($results[$i]['user_agent'], false);
                 }
                 $element_value = $results[$i]['browser'] . (isset($results[$i]['version']) && intval($results[$i]['version']) != 0 ? ' ' . $results[$i]['version'] : '');
                 break;
             case 'category':
                 $row_details .= '<br>' . __('Category ID', 'wp-slimstat') . ": {$results[$i]['category']}";
                 $cat_ids = explode(',', $results[$i]['category']);
                 if (!empty($cat_ids)) {
                     $element_value = '';
                     foreach ($cat_ids as $a_cat_id) {
                         if (empty($a_cat_id)) {
                             continue;
                         }
                         $cat_name = get_cat_name($a_cat_id);
                         if (empty($cat_name)) {
                             $tag = get_term($a_cat_id, 'post_tag');
                             if (!empty($tag->name)) {
                                 $cat_name = $tag->name;
                             }
                         }
                         $element_value .= ', ' . (!empty($cat_name) ? $cat_name : $a_cat_id);
                     }
                     $element_value = substr($element_value, 2);
                 }
                 break;
             case 'country':
                 $row_details .= '<br>' . __('Country Code', 'wp-slimstat') . ": {$results[$i]['country']}";
                 $element_value = __('c-' . $results[$i]['country'], 'wp-slimstat');
                 break;
             case 'ip':
                 if (wp_slimstat::$options['convert_ip_addresses'] == 'yes') {
                     $element_value = gethostbyaddr($results[$i]['ip']);
                 } else {
                     $element_value = $results[$i]['ip'];
                 }
                 break;
             case 'language':
                 $row_details = '<br>' . __('Language Code', 'wp-slimstat') . ": {$results[$i]['language']}";
                 $element_value = __('l-' . $results[$i]['language'], 'wp-slimstat');
                 break;
             case 'platform':
                 $row_details = '<br>' . __('OS Code', 'wp-slimstat') . ": {$results[$i]['platform']}";
                 $element_value = __($results[$i]['platform'], 'wp-slimstat');
                 break;
             case 'resource':
                 $post_id = url_to_postid(strtok($results[$i]['resource'], '?'));
                 if ($post_id > 0) {
                     $row_details = '<br>' . htmlentities($results[$i]['resource'], ENT_QUOTES, 'UTF-8');
                 }
                 $element_value = self::get_resource_title($results[$i]['resource']);
                 break;
             case 'searchterms':
                 if ($_type == 'recent') {
                     $row_details = '<br>' . __('Referrer', 'wp-slimstat') . ": {$results[$i]['domain']}";
                     $element_value = self::get_search_terms_info($results[$i]['searchterms'], $results[$i]['domain'], $results[$i]['referer'], true);
                 } else {
                     $element_value = htmlentities($results[$i]['searchterms'], ENT_QUOTES, 'UTF-8');
                 }
                 break;
             case 'user':
                 $element_value = $results[$i]['user'];
                 if (wp_slimstat::$options['show_display_name'] == 'yes') {
                     $element_custom_value = get_user_by('login', $results[$i]['user']);
                     if (is_object($element_custom_value)) {
                         $element_value = $element_custom_value->display_name;
                     }
                 }
                 break;
             default:
         }
         $element_value = "<a class='slimstat-filter-link' href='" . self::fs_url($_column . ' ' . $_args['filter_op'] . ' ' . $results[$i][$_column]) . "'>{$element_value}</a>";
         if ($_type == 'recent') {
             $row_details = date_i18n(wp_slimstat_db::$formats['date_time_format'], $results[$i]['dt'], true) . $row_details;
         } else {
             $percentage = '<span>' . ($_args['total_for_percentage'] > 0 ? number_format(sprintf("%01.2f", 100 * $results[$i]['counthits'] / $_args['total_for_percentage']), 2, wp_slimstat_db::$formats['decimal'], wp_slimstat_db::$formats['thousand']) : 0) . '%</span>';
             $row_details = __('Hits', 'wp-slimstat') . ': ' . number_format($results[$i]['counthits'], 0, wp_slimstat_db::$formats['decimal'], wp_slimstat_db::$formats['thousand']) . $row_details;
         }
         // Some columns require a special post-treatment
         if ($_column == 'resource' && strpos($_args['custom_where'], '404') === false) {
             $base_url = '';
             if (isset($results[$i]['blog_id'])) {
                 $base_url = parse_url(get_site_url($results[$i]['blog_id']));
                 $base_url = $base_url['scheme'] . '://' . $base_url['host'];
             }
             $element_value = '<a target="_blank" class="slimstat-font-logout" title="' . __('Open this URL in a new window', 'wp-slimstat') . '" href="' . $base_url . htmlentities($results[$i]['resource'], ENT_QUOTES, 'UTF-8') . '"></a> ' . $base_url . $element_value;
         }
         if ($_column == 'referer') {
             $element_url = htmlentities($results[$i]['referer'], ENT_QUOTES, 'UTF-8');
             $element_value = '<a target="_blank" class="slimstat-font-logout" title="' . __('Open this URL in a new window', 'wp-slimstat') . '" href="' . $element_url . '"></a> ' . $element_value;
         }
         if (!empty($results[$i]['ip']) && $_column != 'ip' && wp_slimstat::$options['convert_ip_addresses'] != 'yes') {
             $row_details .= '<br> IP: <a class="slimstat-filter-link" href="' . self::fs_url('ip equals ' . $results[$i]['ip']) . '">' . $results[$i]['ip'] . '</a>' . (!empty($results[$i]['other_ip']) ? ' / ' . long2ip($results[$i]['other_ip']) : '') . '<a title="WHOIS: ' . $results[$i]['ip'] . '" class="slimstat-font-location-1 whois" href="' . wp_slimstat::$options['ip_lookup_service'] . $results[$i]['ip'] . '"></a>';
         }
         if (!empty($row_details)) {
             $row_details = "<b class='slimstat-row-details{$is_expanded}'>{$row_details}</b>";
         }
         echo "<p>{$element_pre_value}{$element_value}{$percentage} {$row_details}</p>";
     }
 }
Exemple #2
0
 /**
  * Retrieves the information from the database
  */
 protected static function _get_results($_attr = array())
 {
     // Optional fields and other variables are defined to avoid PHP warnings
     $join_tables = '';
     $table_identifier = wp_slimstat_db::get_table_identifier($_attr['w']);
     if ($table_identifier != 't1.') {
         $join_tables = $table_identifier . '*,';
     }
     if (!isset($_attr['lf'])) {
         $_attr['lf'] = '';
     }
     if (!isset($_attr['lc'])) {
         $_attr['lc'] = array($_attr['w']);
     } elseif ($_attr['lc'] != '*') {
         $_attr['lc'] = explode(',', $_attr['lc']);
         foreach ($_attr['lc'] as $a_column) {
             $table_identifier = wp_slimstat_db::get_table_identifier($a_column);
             if ($table_identifier != 't1.' && strpos($join_tables, $table_identifier . '*') === false) {
                 $join_tables .= $table_identifier . '*,';
             }
         }
     }
     $join_tables = substr($join_tables, 0, -1);
     if (!isset($_attr['s'])) {
         $_attr['s'] = ', ';
     }
     // Load locales
     load_plugin_textdomain('countries-languages', WP_PLUGIN_DIR . '/wp-slimstat/admin/lang', '/wp-slimstat/lang');
     // If a local translation for countries and languages does not exist, use English
     if (!isset($l10n['countries-languages'])) {
         load_textdomain('countries-languages', WP_PLUGIN_DIR . '/wp-slimstat/admin/lang/countries-languages-en_US.mo');
     }
     $content = '';
     switch ($_attr['f']) {
         // Custom SQL: use the lf param to retrieve the data; no syntax check is done!
         case 'custom':
             if (!empty($_attr['lf'])) {
                 return $GLOBALS['wpdb']->query($_attr['lf']);
             }
             break;
         case 'recent':
         case 'popular':
         case 'count':
         case 'count-all':
             // Avoid PHP warnings in strict mode
             $custom_where = '';
             // Ampersands are encoded as HTML
             $_attr['lf'] = str_replace('&#038;&#038;&#038;', '&&&', $_attr['lf']);
             if (strpos($_attr['lf'], 'WHERE:') !== false) {
                 $custom_where = html_entity_decode(substr($_attr['lf'], 6), ENT_QUOTES, 'UTF-8');
                 $custom_where = str_replace('NOW()', date_i18n('U'), $custom_where);
                 wp_slimstat_db::init();
             } else {
                 wp_slimstat_db::init($_attr['lf']);
             }
             if ($_attr['f'] == 'count') {
                 return wp_slimstat_db::count_records($custom_where, $_attr['w'], true, true, $join_tables);
             }
             if ($_attr['f'] == 'count-all') {
                 return wp_slimstat_db::count_records($custom_where, $_attr['w'], true, false, $join_tables);
             }
             $_attr['f'] = 'get_' . $_attr['f'];
             $results = wp_slimstat_db::$_attr['f'](wp_slimstat_db::get_table_identifier($_attr['w']) . $_attr['w'], $custom_where, $join_tables);
             // Format results
             if (empty($results)) {
                 return $content;
             }
             // What columns to include?
             if ($_attr['lc'] == '*') {
                 $_attr['lc'] = array_keys($results[0]);
             }
             $home_url = get_home_url();
             foreach ($results as $a_result) {
                 $content .= '<li>';
                 foreach ($_attr['lc'] as $id_column => $a_column) {
                     $content .= "<span class='col-{$id_column}'>";
                     switch ($a_column) {
                         case 'post_link':
                             $post_id = url_to_postid(strtok($a_result['resource'], '?'));
                             if ($post_id > 0) {
                                 $content .= "<a href='{$a_result['resource']}'>" . get_the_title($post_id) . '</a>';
                             } else {
                                 $content .= strtok($a_result['resource'], '?');
                             }
                             break;
                         case 'dt':
                             $content .= date_i18n(wp_slimstat_db::$date_time_format, $a_result['dt']);
                             break;
                         case 'hostname':
                             $content .= gethostbyaddr($a_result['ip']);
                             break;
                         case 'ip':
                             $content .= long2ip($a_result['ip']);
                             break;
                         case 'count':
                             $content .= $a_result['counthits'];
                             break;
                         default:
                             $content .= $a_result[$a_column];
                             break;
                     }
                     $content .= $_attr['s'];
                 }
                 $content = substr($content, 0, strrpos($content, $_attr['s'])) . '</li>';
             }
             return "<ul class='slimstat-shortcode {$_attr['f']}-{$_attr['w']}'>{$content}</ul>";
             break;
         default:
     }
 }