/**
  * A flag to indicate whether the current site has roughly more than 3000 subscriptions. Used to disable
  * features on the Manage Subscriptions list table that do not scale well (yet).
  *
  * @since 1.4.4
  */
 public static function is_large_site()
 {
     if (false === self::$is_large_site) {
         self::$is_large_site = filter_var(get_option('wcs_is_large_site'), FILTER_VALIDATE_BOOLEAN);
         if (false === self::$is_large_site && self::get_total_subscription_count() > 2500) {
             add_option('wcs_is_large_site', 'true', '', false);
             self::$is_large_site = true;
         }
     }
     return apply_filters('woocommerce_subscriptions_is_large_site', self::$is_large_site);
 }
 /**
  * Make the table sortable by all columns and set the default sort field to be start_date.
  *
  * @return array An associative array containing all the columns that should be sortable: 'slugs' => array( 'data_values', bool )
  * @since 1.0
  */
 public function get_sortable_columns()
 {
     $sortable_columns = array('status' => array('status', false), 'order_id' => array('order_id', false), 'title' => array('_order_item_name', false), 'user' => array('user_display_name', false), 'start_date' => array('start_date', true), 'expiry_date' => array('expiry_date', false), 'trial_expiry_date' => array('trial_expiry_date', false), 'end_date' => array('end_date', false), 'last_payment_date' => array('last_payment_date', false), 'renewal_order_count' => array('renewal_order_count', false));
     if (false === WC_Subscriptions::is_large_site()) {
         $sortable_columns['next_payment_date'] = array('next_payment_date', false);
     }
     return $sortable_columns;
 }
	function woo_ce_return_count( $export_type = '', $args = array() ) {

		global $wpdb;

		$count_sql = null;
		$woocommerce_version = woo_get_woo_version();

		switch( $export_type ) {

			case 'product':
				$post_type = array( 'product', 'product_variation' );
				$args = array(
					'post_type' => $post_type,
					'posts_per_page' => 1,
					'fields' => 'ids',
					'suppress_filters' => 1
				);
				$count_query = new WP_Query( $args );
				$count = $count_query->found_posts;
				break;

			case 'category':
				$term_taxonomy = 'product_cat';
				if( taxonomy_exists( $term_taxonomy ) )
					$count = wp_count_terms( $term_taxonomy );
				break;

			case 'tag':
				$term_taxonomy = 'product_tag';
				if( taxonomy_exists( $term_taxonomy ) )
					$count = wp_count_terms( $term_taxonomy );
				break;

			case 'brand':
				$term_taxonomy = apply_filters( 'woo_ce_brand_term_taxonomy', 'product_brand' );
				if( taxonomy_exists( $term_taxonomy ) )
					$count = wp_count_terms( $term_taxonomy );
				break;

			case 'order':
				$post_type = 'shop_order';
				// Check if this is a WooCommerce 2.2+ instance (new Post Status)
				if( version_compare( $woocommerce_version, '2.2' ) >= 0 )
					$post_status = ( function_exists( 'wc_get_order_statuses' ) ? apply_filters( 'woo_ce_order_post_status', array_keys( wc_get_order_statuses() ) ) : 'any' );
				else
					$post_status = apply_filters( 'woo_ce_order_post_status', woo_ce_post_statuses() );
				$args = array(
					'post_type' => $post_type,
					'posts_per_page' => 1,
					'post_status' => $post_status,
					'fields' => 'ids'
				);
				$count_query = new WP_Query( $args );
				$count = $count_query->found_posts;
				break;

			case 'customer':
				if( $users = woo_ce_return_count( 'user' ) > 1000 ) {
					$count = sprintf( '~%s+', 1000 );
				} else {
					$post_type = 'shop_order';
					$args = array(
						'post_type' => $post_type,
						'posts_per_page' => -1,
						'fields' => 'ids'
					);
					// Check if this is a WooCommerce 2.2+ instance (new Post Status)
					if( version_compare( $woocommerce_version, '2.2' ) >= 0 ) {
						$args['post_status'] = apply_filters( 'woo_ce_customer_post_status', array( 'wc-pending', 'wc-on-hold', 'wc-processing', 'wc-completed' ) );
					} else {
						$args['post_status'] = apply_filters( 'woo_ce_customer_post_status', woo_ce_post_statuses() );
						$args['tax_query'] = array(
							array(
								'taxonomy' => 'shop_order_status',
								'field' => 'slug',
								'terms' => array( 'pending', 'on-hold', 'processing', 'completed' )
							),
						);
					}
					$order_ids = new WP_Query( $args );
					$count = $order_ids->found_posts;
					if( $count > 100 ) {
						$count = sprintf( '~%s', $count );
					} else {
						$customers = array();
						if( $order_ids->posts ) {
							foreach( $order_ids->posts as $order_id ) {
								$email = get_post_meta( $order_id, '_billing_email', true );
								if( !in_array( $email, $customers ) )
									$customers[$order_id] = $email;
								unset( $email );
							}
							$count = count( $customers );
						}
					}
				}
/*
				if( false ) {
					$orders = get_posts( $args );
					if( $orders ) {
						$customers = array();
						foreach( $orders as $order ) {
							$order->email = get_post_meta( $order->ID, '_billing_email', true );
							if( empty( $order->email ) ) {
								if( $order->user_id = get_post_meta( $order->ID, '_customer_user', true ) ) {
									$user = get_userdata( $order->user_id );
									if( $user )
										$order->email = $user->user_email;
									unset( $user );
								} else {
									$order->email = '-';
								}
							}
							if( !in_array( $order->email, $customers ) ) {
								$customers[$order->ID] = $order->email;
								$count++;
							}
						}
						unset( $orders, $order );
					}
				}
*/
				break;

			case 'user':
				if( $users = count_users() )
					$count = $users['total_users'];
				break;

			case 'coupon':
				$post_type = 'shop_coupon';
				if( post_type_exists( $post_type ) )
					$count = wp_count_posts( $post_type );
				break;

			case 'subscription':
				$count = 0;
				// Check that WooCommerce Subscriptions exists
				if( class_exists( 'WC_Subscriptions' ) ) {
					if( method_exists( 'WC_Subscriptions', 'is_large_site' ) ) {
						// Does this store have roughly more than 3000 Subscriptions
						if( false === WC_Subscriptions::is_large_site() ) {
							if( class_exists( 'WC_Subscriptions_Manager' ) ) {
								// Check that the get_all_users_subscriptions() function exists
								if( method_exists( 'WC_Subscriptions_Manager', 'get_all_users_subscriptions' ) ) {
									if( $subscriptions = WC_Subscriptions_Manager::get_all_users_subscriptions() ) {
										foreach( $subscriptions as $key => $user_subscription ) {
											if( !empty( $user_subscription ) ) {
												foreach( $user_subscription as $subscription )
													$count++;
											}
										}
										unset( $subscriptions, $subscription, $user_subscription );
									}
								}
							}
						} else {
							if( method_exists( 'WC_Subscriptions', 'get_total_subscription_count' ) )
								$count = WC_Subscriptions::get_total_subscription_count();
							else
								$count = "~2500";
						}
					}
				}
				break;

			case 'product_vendor':
				$term_taxonomy = 'shop_vendor';
				if( taxonomy_exists( $term_taxonomy ) )
					$count = wp_count_terms( $term_taxonomy );
				break;

			case 'commission':
				$post_type = 'shop_commission';
				if( post_type_exists( $post_type ) )
					$count = wp_count_posts( $post_type );
				break;

			case 'shipping_class':
				$term_taxonomy = 'product_shipping_class';
				if( taxonomy_exists( $term_taxonomy ) )
					$count = wp_count_terms( $term_taxonomy );
				break;

			case 'attribute':
				$attributes = ( function_exists( 'wc_get_attribute_taxonomies' ) ? wc_get_attribute_taxonomies() : array() );
				$count = count( $attributes );
				break;

		}
		if( isset( $count ) || $count_sql ) {
			if( isset( $count ) ) {
				if( is_object( $count ) ) {
					$count = (array)$count;
					$count = (int)array_sum( $count );
				}
				return $count;
			} else {
				if( $count_sql )
					$count = $wpdb->get_var( $count_sql );
				else
					$count = 0;
			}
			return $count;
		} else {
			return 0;
		}

	}
    /**
     * Outputs the Subscription Management admin page with a sortable @see WC_Subscriptions_List_Table used to
     * display all the subscriptions that have been purchased.
     *
     * @uses WC_Subscriptions_List_Table
     * @since 1.0
     */
    public static function subscriptions_management_page()
    {
        $subscriptions_table = self::get_subscriptions_list_table();
        $subscriptions_table->prepare_items();
        ?>
<div class="wrap">
	<div id="icon-woocommerce" class="icon32-woocommerce-users icon32"><br/></div>
	<h2><?php 
        _e('Manage Subscriptions', 'woocommerce-subscriptions');
        ?>
</h2>
	<?php 
        $subscriptions_table->messages();
        ?>
	<?php 
        $subscriptions_table->views();
        ?>
	<?php 
        if (false === WC_Subscriptions::is_large_site()) {
            ?>
	<form id="subscriptions-search" action="" method="get"><?php 
            // Don't send all the subscription meta across
            ?>
		<?php 
            $subscriptions_table->search_box(__('Search Subscriptions', 'woocommerce-subscriptions'), 'subscription');
            ?>
		<input type="hidden" name="page" value="subscriptions" />
		<?php 
            if (isset($_REQUEST['status'])) {
                ?>
			<input type="hidden" name="status" value="<?php 
                echo esc_attr($_REQUEST['status']);
                ?>
" />
		<?php 
            }
            ?>
	</form>
	<?php 
        }
        ?>
	<form id="subscriptions-filter" action="" method="get">
		<?php 
        $subscriptions_table->display();
        ?>
	</form>
</div>
		<?php 
    }