get_results($sql, OBJECT); if ($results) { $output.=''; } return $output; } function owa_category_popular($category="all",$max_owa_popular=10) { // CheckCache $output=wp_cache_get('owa_category_popular'); if ($output === false) { global $wpdb; $sql="SELECT doc.page_title as title, doc.url as url"; $sql.=" FROM ".$wpdb->prefix."terms as term"; $sql.=" INNER JOIN ".$wpdb->prefix."term_taxonomy as tax ON tax.term_id = term.term_id"; $sql.=" INNER JOIN ".$wpdb->prefix."term_relationships as rel ON tax.term_taxonomy_id =rel.term_taxonomy_id"; $sql.=" INNER JOIN ".$wpdb->prefix."posts as post ON rel.object_id = post.ID"; $sql.=" INNER JOIN owa_document as doc ON doc.url = post.guid"; $sql.=" INNER JOIN owa_request as req ON doc.id = req.document_id"; if (is_numeric($category)) { $sql.=" WHERE term.term_id = ".$category; } $sql.=" GROUP BY doc.id"; $sql.=" ORDER BY count(doc.id) DESC"; $sql.=" LIMIT 0,".$max_owa_popular; $results = $wpdb->get_results($sql, OBJECT); if ($results) { $output.=''; wp_cache_set('owa_category_popular', $output, 'owa_popular', '60'); } } return $output; } function owa_weighted_popular($max_owa_popular=10) { // CheckCache $output=wp_cache_get('owa_weighted_popular'); if ($output === false) { global $wpdb; // The Magic $sql.="SELECT count(doc.id) as count, post.post_date_gmt as postdate, post.post_title as name,"; $sql.=" DATEDIFF(CURDATE(),post.post_date_gmt) as ddate,"; $sql.=" count(doc.id)/(DATEDIFF(CURDATE(),post.post_date_gmt)+1) as factor, doc.url as url"; $sql.=" FROM owa_request as req"; $sql.=" INNER JOIN owa_document as doc ON req.document_id = doc.id"; $sql.=" INNER JOIN ".$wpdb->prefix."posts as post ON doc.url = post.guid"; $sql.=" GROUP BY doc.id"; $sql.=" ORDER BY factor DESC, count(doc.id) DESC"; $sql.=" LIMIT 0,".$max_owa_popular; // Fetch $results = $wpdb->get_results($sql, OBJECT); if ($results) { $output.=''; wp_cache_set('owa_weighted_popular', $output, 'owa_popular', '60'); } } return $output; } function owa_api_popular($max_owa_popular=10,$date_range_owa_popular="last_seven_days",$post_only_owa_popular=TRUE) { $output=wp_cache_get('owa_api_popular'); if ($output == false) { global $wpdb; global $owa_wp; // OWA must be an active plugin for this to work $owa_params = array(); $owa_params['site_id'] = $owa_wp->config['site_id']; // the current site id $owa_params['period'] = $date_range_owa_popular; $owa_params['limit'] = $max_owa_popular; // the number of top pages you want if ($post_only_owa_popular) { $owa_params['constraints']['page_type'] = 'Post'; } //$top_pages_data = $owa_wp->api->getMetric('base.topPages', $owa_params); $api = &owa_coreAPI::singleton($owa_params); $top_pages_data = $api->getMetric('base.topPages', $owa_params); if (count($top_pages_data) > 0) { $output.=''; } wp_cache_set('owa_api_popular', $output, 'owa_popular', 60); } return $output; } function widget_owa_popular_init() { if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') ) return; // This saves options and prints the widget's config form. function widget_owa_popular_control() { $options = $newoptions = get_option('widget_owa_popular'); if ( $_POST['owapopular-submit'] ) { $newoptions['limit'] = strip_tags(stripslashes($_POST['owapopular-limit'])); $newoptions['postonly'] = strip_tags(stripslashes($_POST['owapopular-postonly'])); $newoptions['style'] = strip_tags(stripslashes($_POST['owapopular-style'])); $newoptions['range'] = strip_tags(stripslashes($_POST['owapopular-range'])); $newoptions['category'] = strip_tags(stripslashes($_POST['owapopular-category'])); } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_owa_popular', $options); } if ($options['limit'] == "") { $options['limit']=10; } if ($options['range'] == "") { $options['range']='last_seven_days'; } switch ($options['postonly']) { case "FALSE": $options['postonly']=""; break; default: $options['postonly']="CHECKED"; break; } ?>
10, 'postonly' => 'TRUE', 'style' => 'api', 'range' => 'last_seven_days', 'category' => ''); $options = (array) get_option('widget_owa_popular'); foreach ( $defaults as $key => $value ) if ( !isset($options[$key]) ) $options[$key] = $defaults[$key]; $max_owa_popular=$options['limit']; $style_owa_popular=$options['style']; switch ($style_owa_popular) { case "weighted": $widgettitle=_('Most Popular Posts'); $output=owa_weighted_popular($max_owa_popular); break; case "live": $widgettitle=_('Live Traffic Feed'); $output=owa_live_traffic_feed($max_owa_popular); break; case "category": $widgettitle=_('Most Popular Posts'); $owa_category=$options['category']; $output=owa_category_popular($owa_category,$max_owa_popular); break; default: $widgettitle=_('Most Popular Posts'); $date_range_owa_popular=$options['range']; $post_only_owa_popular=$options['postonly']; $output=owa_api_popular($max_owa_popular,$date_range_owa_popular,$post_only_owa_popular); break; } print $before_widget.$before_title.$widgettitle.$after_title.$output.$after_widget; } register_widget_control('OWA Most Popular', 'widget_owa_popular_control', 315, 420); register_sidebar_widget('OWA Most Popular', 'widget_owa_popular'); register_sidebar_widget('OWA Live Feed', 'widget_owa_live_feed'); } add_action('init', 'widget_owa_popular_init'); ?>