Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* the class file mentioned on http://wordpress.stackexchange.com/questions/32088/wp-query-on-custom-taxonomy-works-fine-but-fails-if-run-through-wp-ajax */
- $jttvvs = new jttv_ViewStats();
- class jttv_ViewStats {
- function __construct()
- {
- add_action( 'admin_menu', array( $this, 'jttvvs_admin_menu') );
- add_action('wp_ajax_get_views', array( $this, 'get_views') );
- }
- /**
- * Add the admin page
- */
- function jttvvs_admin_menu()
- {
- $page = add_submenu_page('jimmyteenstv', __('View Stats'), __('View Stats'), 'edit_themes', 'jttv_viewstats', array( $this, 'jttvvs_settings_page') );
- add_action("admin_print_scripts-$page", array( $this, 'jttvvs_js') );
- add_action("admin_print_scripts-$page", array( $this, 'jttvvs_css') );
- }
- /**
- * load the js
- */
- function jttvvs_js()
- {
- wp_enqueue_script( "jttv_viewstats", WP_PLUGIN_URL . "/stats/js/jttv_viewstats.js", array( 'jquery' ) );
- }
- /**
- * load css
- */
- function jttvvs_css()
- {
- ?>
- <style type="text/css">span.loader { background: transparent url( WP_PLUGIN_URL . "/stats/assets/loader.gif") no-repeat 0 0; width:16px; height:11px; display: block; margin:3px 0 0 15px;}</style>
- <?php
- }
- /**
- * ajax return
- */
- function get_views()
- {
- $args = array(
- 'posts_per_page'=> -1,
- 'post_status' => 'publish'
- );
- if ( isset($_POST['tag_id']) ) :
- $args['tax_query'] =
- array(array(
- 'taxonomy' => 'region',
- 'field' => 'id',
- 'terms' => $_POST['tax_id'],
- ));
- endif;
- //echo '<pre>' . print_r($args, true) . '</pre>';
- $the_query = new WP_Query($args);
- while ( $the_query->have_posts() ) : $the_query->the_post();
- $reg = get_post_meta(get_the_ID(), 'reg_count');
- $unreg = get_post_meta(get_the_ID(), 'unreg_count');
- $reg_count = $reg_count + $reg[0];
- $unreg_count = $unreg_count + $unreg[0];
- endwhile;
- echo "total: " . number_format($reg_count + $unreg_count);
- die();
- }
- /**
- * non ajax return
- */
- function get_views_nojax($tax_id = false)
- {
- $args = array(
- 'numberposts' => -1,
- 'post_status' => 'publish'
- );
- if ( isset($tax_id) ) :
- $args['tax_query'] =
- array(
- array(
- 'taxonomy' => 'region',
- 'field' => 'id',
- 'terms' => $tax_id
- );
- endif;
- $posts = get_posts( $args );
- foreach ( $posts as $post ) :
- $reg = get_post_meta($post->ID, 'reg_count');
- $unreg = get_post_meta($post->ID, 'unreg_count');
- $reg_count = $reg_count + $reg[0];
- $unreg_count = $unreg_count + $unreg[0];
- endforeach;
- return "total " . number_format($reg_count + $unreg_count);
- }
- /**
- * the regions
- */
- function list_regions()
- {
- // regions
- $args = array(
- 'orderby' => 'name',
- 'order' => 'ASC',
- 'taxonomy' => 'region',
- 'child_of' => 0
- );
- $cats = get_categories($args);
- foreach ( $cats as $cat ) :
- echo '<tr>';
- echo '<td>' . $cat->cat_ID . " " . $cat->name . '</td>';
- echo '<td id="' . $cat->cat_ID . '">' . $this->get_views_nojax($cat->cat_ID) . '</td>';
- echo '<td id="' . $cat->cat_ID . '"><a class="button-secondary getviews">Count Views</a></td>';
- echo '</tr>';
- endforeach;
- }
- /**
- * Create the viewable page
- */
- function jttvvs_settings_page()
- {
- ?>
- <div class="wrap">
- <div id="icon-options-general" class="icon32"></div><h2>Film View Stats</h2>
- <table class="widefat">
- <tr>
- <td>Total Views:</td>
- <td><?php echo $this->get_views_nojax($cat->cat_ID); ?></td>
- <td width="50%"><a class="button-secondary getviews">Count Views</a></td>
- </tr>
- <tr>
- <td colspan="3"> </td>
- </tr>
- <?php $this->list_regions(); ?>
- </table>
- </div>
- <?php
- }
- }//
- ?>
- /* the jquery - in a separate file - loaded through function jttvvs_js() */
- jQuery(document).ready( function($) {
- $("a.getviews").click( function() {
- var td = $(this).parent();
- /* only fetch results once */
- $(this).unbind('click').bind('click', function(){return false;});
- // replace button with loader
- $(td).html('<span class="loader"></span>');
- $.post($(this), {
- action: "get_views",
- tax_id: td.attr("id")
- }, function(data) {
- td.html(data);
- }
- );
- return false;
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement