/**
  * Outputs the content for each column.
  *
  * @param array $item A singular item (one full row's worth of data)
  * @param array $column_name The name/slug of the column to be processed
  * @return string Text or HTML to be placed inside the column <td>
  * @since 1.0
  */
 public function column_default($item, $column_name)
 {
     global $woocommerce;
     $current_gmt_time = gmdate('U');
     switch ($column_name) {
         case 'status':
             $actions = array();
             $action_url = add_query_arg(array('page' => $_REQUEST['page'], 'user' => $item['user_id'], 'subscription' => $item['subscription_key'], '_wpnonce' => wp_create_nonce($item['subscription_key'])));
             if (isset($_REQUEST['status'])) {
                 $action_url = add_query_arg(array('status' => $_REQUEST['status']), $action_url);
             }
             $order = new WC_Order($item['order_id']);
             $all_statuses = array('active' => __('Reactivate', WC_Subscriptions::$text_domain), 'on-hold' => __('Suspend', WC_Subscriptions::$text_domain), 'cancelled' => __('Cancel', WC_Subscriptions::$text_domain), 'trash' => __('Trash', WC_Subscriptions::$text_domain), 'deleted' => __('Delete Permanently', WC_Subscriptions::$text_domain));
             foreach ($all_statuses as $status => $label) {
                 if (WC_Subscriptions_Manager::can_subscription_be_changed_to($status, $item['subscription_key'], $item['user_id'])) {
                     $action = 'deleted' == $status ? 'delete' : $status;
                     // For built in CSS
                     $actions[$action] = sprintf('<a href="%s">%s</a>', add_query_arg('new_status', $status, $action_url), $label);
                 }
             }
             if ($item['status'] == 'pending') {
                 unset($actions['active']);
                 unset($actions['trash']);
             } elseif (!in_array($item['status'], array('cancelled', 'expired', 'suspended'))) {
                 unset($actions['trash']);
             }
             $actions = apply_filters('woocommerce_subscriptions_list_table_actions', $actions, $item);
             $column_content = sprintf('<mark class="%s">%s</mark> %s', sanitize_title($item[$column_name]), WC_Subscriptions_Manager::get_status_to_display($item[$column_name], $item['subscription_key'], $item['user_id']), $this->row_actions($actions));
             $column_content = apply_filters('woocommerce_subscriptions_list_table_column_status_content', $column_content, $item, $actions, $this);
             break;
         case 'title':
             //Return the title contents
             $column_content = sprintf('<a href="%s">%s</a>', get_edit_post_link($item['product_id']), WC_Subscriptions_Order::get_item_name($item['order_id'], $item['product_id']));
             $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', 'product_id', $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item['product_id']);
             $order = new WC_Order($item['order_id']);
             $order_item = WC_Subscriptions_Order::get_item_by_product_id($order, $item['product_id']);
             $product = $order->get_product_from_item($order_item);
             if (isset($product->variation_data)) {
                 $column_content .= '<br />' . woocommerce_get_formatted_variation($product->variation_data, true);
             }
             break;
         case 'order_id':
             $order = new WC_Order($item[$column_name]);
             $column_content = sprintf('<a href="%1$s">%2$s</a>', get_edit_post_link($item[$column_name]), sprintf(__('Order %s', WC_Subscriptions::$text_domain), $order->get_order_number()));
             $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', $column_name, $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item[$column_name]);
             break;
         case 'user':
             $user = get_user_by('id', $item['user_id']);
             $column_content = sprintf('<a href="%s">%s</a>', admin_url('user-edit.php?user_id=' . $user->ID), ucfirst($user->display_name));
             $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', 'user_id', $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item['user_id']);
             break;
         case 'start_date':
         case 'expiry_date':
         case 'end_date':
             if ($column_name == 'expiry_date' && $item[$column_name] == 0) {
                 $column_content = __('Never', WC_Subscriptions::$text_domain);
             } else {
                 if ($column_name == 'end_date' && $item[$column_name] == 0) {
                     $column_content = __('Not yet ended', WC_Subscriptions::$text_domain);
                 } else {
                     $gmt_timestamp = strtotime($item[$column_name]);
                     $user_timestamp = $gmt_timestamp + get_option('gmt_offset') * 3600;
                     $column_content = sprintf('<time title="%s">%s</time>', esc_attr($gmt_timestamp), date_i18n(woocommerce_date_format(), $user_timestamp));
                 }
             }
             break;
         case 'trial_expiry_date':
             $trial_expiration = WC_Subscriptions_Manager::get_trial_expiration_date($item['subscription_key'], $item['user_id'], 'timestamp');
             if (empty($trial_expiration)) {
                 $column_content = '-';
             } else {
                 $column_content = sprintf('<time title="%s">%s</time>', esc_attr($trial_expiration), date_i18n(woocommerce_date_format(), $trial_expiration + get_option('gmt_offset') * 3600));
             }
             break;
         case 'last_payment_date':
             $last_payment_timestamp = strtotime($item['last_payment_date']);
             $time_diff = $current_gmt_time - $last_payment_timestamp;
             if ($time_diff > 0 && $time_diff < 7 * 24 * 60 * 60) {
                 $last_payment = sprintf(__('%s ago', WC_Subscriptions::$text_domain), human_time_diff($last_payment_timestamp, $current_gmt_time));
             } else {
                 $last_payment = date_i18n(woocommerce_date_format(), $last_payment_timestamp + get_option('gmt_offset') * 3600);
             }
             $column_content = sprintf('<time title="%s">%s</time>', esc_attr($last_payment_timestamp), $last_payment);
             break;
         case 'next_payment_date':
             $next_payment_timestamp = WC_Subscriptions_Manager::get_next_payment_date($item['subscription_key'], $item['user_id'], 'timestamp');
             if ($next_payment_timestamp == 0) {
                 $column_content = '-';
             } else {
                 // Convert to site time
                 $time_diff = $next_payment_timestamp - $current_gmt_time;
                 if ($time_diff > 0 && $time_diff < 7 * 24 * 60 * 60) {
                     $next_payment = sprintf(__('In %s', WC_Subscriptions::$text_domain), human_time_diff($current_gmt_time, $next_payment_timestamp));
                 } else {
                     $next_payment = date_i18n(woocommerce_date_format(), $next_payment_timestamp + get_option('gmt_offset') * 3600);
                 }
                 $column_content = sprintf('<time class="next-payment-date" title="%s">%s</time>', esc_attr($next_payment_timestamp), $next_payment);
                 if (WC_Subscriptions_Manager::can_subscription_be_changed_to('new-payment-date', $item['subscription_key'], $item['user_id'])) {
                     $column_content .= '<div class="edit-date-div row-actions hide-if-no-js">';
                     $column_content .= '<img class="date-picker-icon" src="' . admin_url('images/date-button.gif') . '" title="Date Picker Icon"/>';
                     $column_content .= '<a href="#edit_timestamp" class="edit-timestamp" tabindex="4">' . __('Change', WC_Subscriptions::$text_domain) . '</a>';
                     $column_content .= '<div class="date-picker-div hide-if-js">';
                     $column_content .= WC_Subscriptions_Manager::touch_time(array('date' => date('Y-m-d', $next_payment_timestamp), 'echo' => false, 'multiple' => true, 'include_time' => false));
                     $column_content .= '</div>';
                     $column_content .= '</form>';
                 }
             }
             break;
         case 'renewal_order_count':
             $count = WC_Subscriptions_Renewal_Order::get_renewal_order_count($item['order_id']);
             $column_content = sprintf('<a href="%1$s">%2$d</a>', admin_url('edit.php?post_status=all&post_type=shop_order&_renewal_order_parent_id=' . absint($item['order_id'])), $count);
             break;
     }
     return $column_content;
 }
 /**
  * If no sort order set, default to title. If no sort order, default to descending.
  *
  * @since 1.0
  */
 function sort_subscriptions($a, $b)
 {
     $order_by = !empty($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'start_date';
     $order = !empty($_REQUEST['order']) ? $_REQUEST['order'] : 'desc';
     switch ($order_by) {
         case 'product_name':
             $product_name_a = get_the_title($a['product_id']);
             $product_name_b = get_the_title($b['product_id']);
             $result = strcasecmp($product_name_a, $product_name_b);
             break;
         case 'user':
             $user_a = get_user_by('id', $a['user_id']);
             $user_b = get_user_by('id', $b['user_id']);
             $result = strcasecmp($user_a->display_name, $user_b->display_name);
             break;
         case 'expiry_date':
             if ($order == 'asc') {
                 $result = self::sort_with_zero_at_end($a[$order_by], $b[$order_by]);
             } else {
                 $result = self::sort_with_zero_at_beginning($a[$order_by], $b[$order_by]);
             }
             break;
         case 'end_date':
             $result = self::sort_with_zero_at_end($a[$order_by], $b[$order_by]);
             // Display subscriptions that have not ended at the end of the list
             break;
         case 'next_payment_date':
             $next_payment_a = WC_Subscriptions_Manager::get_next_payment_date($a['subscription_key'], $a['user_id'], 'mysql');
             $next_payment_b = WC_Subscriptions_Manager::get_next_payment_date($b['subscription_key'], $b['user_id'], 'mysql');
             $result = self::sort_with_zero_at_end($next_payment_a, $next_payment_b);
             // Display subscriptions with no future payments at the end
             break;
         case 'last_payment_date':
             $last_payment_a = empty($a['completed_payments']) ? 0 : strtotime(array_pop($a['completed_payments']));
             $last_payment_b = empty($b['completed_payments']) ? 0 : strtotime(array_pop($b['completed_payments']));
             $result = self::sort_with_zero_at_end($last_payment_a, $last_payment_b);
             // Display subscriptions with no compelted payments at the end
             break;
         case 'trial_expiry_date':
             $trial_expiration_a = WC_Subscriptions_Manager::get_trial_expiration_date($a['subscription_key'], $a['user_id'], 'mysql');
             $trial_expiration_b = WC_Subscriptions_Manager::get_trial_expiration_date($b['subscription_key'], $b['user_id'], 'mysql');
             $result = self::sort_with_zero_at_end($trial_expiration_a, $trial_expiration_b);
             break;
         case 'renewal_order_count':
             $result = strcmp(WC_Subscriptions_Renewal_Order::get_renewal_order_count($a['order_id']), WC_Subscriptions_Renewal_Order::get_renewal_order_count($b['order_id']));
             break;
         case 'order_id':
             $result = strnatcmp($a[$order_by], $b[$order_by]);
             break;
         default:
             $result = strcmp($a[$order_by], $b[$order_by]);
             break;
     }
     return $order == 'asc' ? $result : -$result;
     // Send final sort direction to usort
 }
function woo_ce_get_subscription_renewals( $order_id = 0 ) {

	$renewals = 0;
	// Check that get_renewal_order_count() exists within the WC_Subscriptions_Renewal_Order class
	if( method_exists( 'WC_Subscriptions_Renewal_Order', 'get_renewal_order_count' ) )
		$renewals = WC_Subscriptions_Renewal_Order::get_renewal_order_count( $order_id );
	return $renewals;

}