/**
  * Filter the "Orders" list to show only orders associated with a specific subscription.
  *
  * @param string $where
  * @param string $request
  * @return string
  * @since 2.0
  */
 public static function filter_orders($where)
 {
     global $typenow, $wpdb;
     if (is_admin() && 'shop_order' == $typenow) {
         $related_orders = array();
         if (isset($_GET['_subscription_related_orders']) && $_GET['_subscription_related_orders'] > 0) {
             $subscription_id = absint($_GET['_subscription_related_orders']);
             $subscription = wcs_get_subscription($subscription_id);
             if (!wcs_is_subscription($subscription)) {
                 // translators: placeholder is a number
                 wcs_add_admin_notice(sprintf(__('We can\'t find a subscription with ID #%d. Perhaps it was deleted?', 'woocommerce-subscriptions'), $subscription_id), 'error');
                 $where .= " AND {$wpdb->posts}.ID = 0";
             } else {
                 self::$found_related_orders = true;
                 $where .= sprintf(" AND {$wpdb->posts}.ID IN (%s)", implode(',', array_map('absint', array_unique($subscription->get_related_orders('ids')))));
             }
         }
     }
     return $where;
 }
 /**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     if ('shop_subscription' == $post->post_type && !empty($_POST['woocommerce_meta_nonce']) && wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) {
         if (isset($_POST['_billing_interval'])) {
             update_post_meta($post_id, '_billing_interval', $_POST['_billing_interval']);
         }
         if (!empty($_POST['_billing_period'])) {
             update_post_meta($post_id, '_billing_period', $_POST['_billing_period']);
         }
         $subscription = wcs_get_subscription($post_id);
         $dates = array();
         foreach (wcs_get_subscription_date_types() as $date_key => $date_label) {
             if ('last_payment' == $date_key) {
                 continue;
             }
             $utc_timestamp_key = $date_key . '_timestamp_utc';
             // A subscription needs a start date, even if it wasn't set
             if (isset($_POST[$utc_timestamp_key])) {
                 $datetime = $_POST[$utc_timestamp_key];
             } elseif ('start' === $date_key) {
                 $datetime = current_time('timestamp', true);
             } else {
                 // No date to set
                 continue;
             }
             $dates[$date_key] = date('Y-m-d H:i:s', $datetime);
         }
         try {
             $subscription->update_dates($dates, 'gmt');
             wp_cache_delete($post_id, 'posts');
         } catch (Exception $e) {
             wcs_add_admin_notice($e->getMessage(), 'error');
         }
     }
 }
 /**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     global $wpdb;
     if ('shop_subscription' != $post->post_type || empty($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) {
         return;
     }
     self::init_address_fields();
     // Update meta
     update_post_meta($post_id, '_customer_user', absint($_POST['customer_user']));
     if (self::$billing_fields) {
         foreach (self::$billing_fields as $key => $field) {
             update_post_meta($post_id, '_billing_' . $key, wc_clean($_POST['_billing_' . $key]));
         }
     }
     if (self::$shipping_fields) {
         foreach (self::$shipping_fields as $key => $field) {
             update_post_meta($post_id, '_shipping_' . $key, wc_clean($_POST['_shipping_' . $key]));
         }
     }
     $subscription = wcs_get_subscription($post_id);
     try {
         WCS_Change_Payment_Method_Admin::save_meta($subscription);
         if ('cancelled' == $_POST['order_status']) {
             $subscription->cancel_order();
         } else {
             $subscription->update_status($_POST['order_status'], '', true);
         }
     } catch (Exception $e) {
         // translators: placeholder is error message from by payment gateway
         wcs_add_admin_notice(sprintf(__('Unable to change payment method: %s', 'woocommerce-subscriptions'), $e->getMessage()), 'error');
     }
     do_action('woocommerce_process_shop_subscription_meta', $post_id, $post);
 }