ID, '_subscription_period', true ) ) { $subscription_period = 'month'; } echo '
'; // Subscription Price woocommerce_wp_text_input( array( 'id' => '_subscription_price', 'class' => 'wc_input_subscription_price', // translators: placeholder is a currency symbol / code 'label' => sprintf( __( 'Subscription Price (%s)', 'woocommerce-subscriptions' ), get_woocommerce_currency_symbol() ), 'placeholder' => _x( 'e.g. 9.90', 'example price', 'woocommerce-subscriptions' ), 'type' => 'text', 'custom_attributes' => array( 'step' => 'any', 'min' => '0', ), ) ); // Subscription Period Interval woocommerce_wp_select( array( 'id' => '_subscription_period_interval', 'class' => 'wc_input_subscription_period_interval', 'label' => __( 'Subscription Periods', 'woocommerce-subscriptions' ), 'options' => wcs_get_subscription_period_interval_strings(), ) ); // Billing Period woocommerce_wp_select( array( 'id' => '_subscription_period', 'class' => 'wc_input_subscription_period', 'label' => __( 'Billing Period', 'woocommerce-subscriptions' ), 'value' => $subscription_period, 'description' => _x( 'for', 'for in "Every month _for_ 12 months"', 'woocommerce-subscriptions' ), 'options' => wcs_get_subscription_period_strings(), ) ); // Subscription Length woocommerce_wp_select( array( 'id' => '_subscription_length', 'class' => 'wc_input_subscription_length', 'label' => __( 'Subscription Length', 'woocommerce-subscriptions' ), 'options' => wcs_get_subscription_ranges( $subscription_period ), ) ); // Sign-up Fee woocommerce_wp_text_input( array( 'id' => '_subscription_sign_up_fee', 'class' => 'wc_input_subscription_intial_price', // translators: %s is a currency symbol / code 'label' => sprintf( __( 'Sign-up Fee (%s)', 'woocommerce-subscriptions' ), get_woocommerce_currency_symbol() ), 'placeholder' => _x( 'e.g. 9.90', 'example price', 'woocommerce-subscriptions' ), 'description' => __( 'Optionally include an amount to be charged at the outset of the subscription. The sign-up fee will be charged immediately, even if the product has a free trial or the payment dates are synced.', 'woocommerce-subscriptions' ), 'desc_tip' => true, 'type' => 'text', 'custom_attributes' => array( 'step' => 'any', 'min' => '0', ), ) ); // Trial Length woocommerce_wp_text_input( array( 'id' => '_subscription_trial_length', 'class' => 'wc_input_subscription_trial_length', 'label' => __( 'Free Trial', 'woocommerce-subscriptions' ), ) ); // Trial Period woocommerce_wp_select( array( 'id' => '_subscription_trial_period', 'class' => 'wc_input_subscription_trial_period', 'label' => __( 'Subscription Trial Period', 'woocommerce-subscriptions' ), 'options' => wcs_get_available_time_periods(), // translators: placeholder is trial period validation message if passed an invalid value (e.g. "Trial period can not exceed 4 weeks") 'description' => sprintf( _x( 'An optional period of time to wait before charging the first recurring payment. Any sign up fee will still be charged at the outset of the subscription. %s', 'Trial period dropdown\'s description in pricing fields', 'woocommerce-subscriptions' ), self::get_trial_period_validation_message() ), 'desc_tip' => true, 'value' => WC_Subscriptions_Product::get_trial_period( $post->ID ), // Explicitly set value in to ensure backward compatibility ) ); do_action( 'woocommerce_subscriptions_product_options_pricing' ); wp_nonce_field( 'wcs_subscription_meta', '_wcsnonce' ); echo '
'; echo '
'; } /** * Output subscription shipping options on the "Edit Product" admin screen * * @since 2.0 */ public static function subscription_shipping_fields() { global $post; echo ''; echo '
'; // Only one Subscription per customer woocommerce_wp_checkbox( array( 'id' => '_subscription_one_time_shipping', 'label' => __( 'One Time Shipping', 'woocommerce-subscriptions' ), 'description' => __( 'Shipping for subscription products is normally charged on the initial order and all renewal orders. Enable this to only charge shipping once on the initial order. Note: for this setting to be enabled the subscription must not have a free trial or a synced renewal date.', 'woocommerce-subscriptions' ), 'desc_tip' => true, ) ); do_action( 'woocommerce_subscriptions_product_options_shipping' ); } /** * Output advanced subscription options on the "Edit Product" admin screen * * @since 1.3.5 */ public static function subscription_advanced_fields() { global $post; echo '
'; echo '
'; // Only one Subscription per customer woocommerce_wp_select( array( 'id' => '_subscription_limit', 'label' => __( 'Limit Subscription', 'woocommerce-subscriptions' ), // translators: placeholders are opening and closing link tags 'description' => sprintf( __( 'Only allow a customer to have one subscription to this product. %sLearn more%s.', 'woocommerce-subscriptions' ), '', '' ), 'options' => array( 'no' => __( 'Do not limit', 'woocommerce-subscriptions' ), 'active' => __( 'Limit to one active subscription', 'woocommerce-subscriptions' ), 'any' => __( 'Limit to one of any status', 'woocommerce-subscriptions' ), ), ) ); do_action( 'woocommerce_subscriptions_product_options_advanced' ); } /** * Output the subscription specific pricing fields on the "Edit Product" admin page. * * @since 1.3 */ public static function variable_subscription_pricing_fields( $loop, $variation_data, $variation ) { global $thepostid; // Set month as the default billing period if ( ! $subscription_period = get_post_meta( $variation->ID, '_subscription_period', true ) ) { $subscription_period = 'month'; } // When called via Ajax if ( ! function_exists( 'woocommerce_wp_text_input' ) ) { require_once( WC()->plugin_path() . '/admin/post-types/writepanels/writepanels-init.php' ); } if ( ! isset( $thepostid ) ) { $thepostid = $variation->post_parent; } include( plugin_dir_path( WC_Subscriptions::$plugin_file ) . 'templates/admin/html-variation-price.php' ); wp_nonce_field( 'wcs_subscription_variations', '_wcsnonce_save_variations', false ); do_action( 'woocommerce_variable_subscription_pricing', $loop, $variation_data, $variation ); } /** * Output extra options in the Bulk Edit select box for editing Subscription terms. * * @since 1.3 */ public static function variable_subscription_bulk_edit_actions() { global $post; if ( WC_Subscriptions_Product::is_subscription( $post->ID ) ) : ?> $now ) ) ) ) { $price = $sale_price; } else { $price = $subscription_price; } update_post_meta( $post_id, '_price', stripslashes( $price ) ); // Make sure trial period is within allowable range $subscription_ranges = wcs_get_subscription_ranges(); $max_trial_length = count( $subscription_ranges[ $_POST['_subscription_trial_period'] ] ) - 1; $_POST['_subscription_trial_length'] = absint( $_POST['_subscription_trial_length'] ); if ( $_POST['_subscription_trial_length'] > $max_trial_length ) { $_POST['_subscription_trial_length'] = $max_trial_length; } update_post_meta( $post_id, '_subscription_trial_length', $_POST['_subscription_trial_length'] ); $_REQUEST['_subscription_sign_up_fee'] = wc_format_decimal( $_REQUEST['_subscription_sign_up_fee'] ); $_REQUEST['_subscription_one_time_shipping'] = isset( $_REQUEST['_subscription_one_time_shipping'] ) ? 'yes' : 'no'; $subscription_fields = array( '_subscription_sign_up_fee', '_subscription_period', '_subscription_period_interval', '_subscription_length', '_subscription_trial_period', '_subscription_limit', '_subscription_one_time_shipping', ); foreach ( $subscription_fields as $field_name ) { if ( isset( $_REQUEST[ $field_name ] ) ) { update_post_meta( $post_id, $field_name, stripslashes( $_REQUEST[ $field_name ] ) ); } } } /** * Save meta data for variable subscription product type when the "Edit Product" form is submitted. * * @param array Array of Product types & their labels, excluding the Subscription product type. * @return array Array of Product types & their labels, including the Subscription product type. * @since 2.0 */ public static function save_variable_subscription_meta( $post_id ) { if ( empty( $_POST['_wcsnonce'] ) || ! wp_verify_nonce( $_POST['_wcsnonce'], 'wcs_subscription_meta' ) || ! isset( $_POST['product-type'] ) || ! in_array( $_POST['product-type'], apply_filters( 'woocommerce_subscription_variable_product_types', array( 'variable-subscription' ) ) ) ) { return; } if ( isset( $_REQUEST['_subscription_limit'] ) ) { update_post_meta( $post_id, '_subscription_limit', stripslashes( $_REQUEST['_subscription_limit'] ) ); } update_post_meta( $post_id, '_subscription_one_time_shipping', stripslashes( isset( $_REQUEST['_subscription_one_time_shipping'] ) ? 'yes' : 'no' ) ); } /** * Calculate and set a simple subscription's prices when edited via the bulk edit * * @param object $product An instance of a WC_Product_* object. * @return null * @since 1.3.9 */ public static function bulk_edit_save_subscription_meta( $product ) { if ( ! $product->is_type( 'subscription' ) ) { return; } $price_changed = false; $old_regular_price = $product->regular_price; $old_sale_price = $product->sale_price; if ( ! empty( $_REQUEST['change_regular_price'] ) ) { $change_regular_price = absint( $_REQUEST['change_regular_price'] ); $regular_price = esc_attr( stripslashes( $_REQUEST['_regular_price'] ) ); switch ( $change_regular_price ) { case 1 : $new_price = $regular_price; break; case 2 : if ( strstr( $regular_price, '%' ) ) { $percent = str_replace( '%', '', $regular_price ) / 100; $new_price = $old_regular_price + ( $old_regular_price * $percent ); } else { $new_price = $old_regular_price + $regular_price; } break; case 3 : if ( strstr( $regular_price, '%' ) ) { $percent = str_replace( '%', '', $regular_price ) / 100; $new_price = $old_regular_price - ( $old_regular_price * $percent ); } else { $new_price = $old_regular_price - $regular_price; } break; } if ( isset( $new_price ) && $new_price != $old_regular_price ) { $price_changed = true; update_post_meta( $product->id, '_regular_price', $new_price ); update_post_meta( $product->id, '_subscription_price', $new_price ); $product->regular_price = $new_price; } } if ( ! empty( $_REQUEST['change_sale_price'] ) ) { $change_sale_price = absint( $_REQUEST['change_sale_price'] ); $sale_price = esc_attr( stripslashes( $_REQUEST['_sale_price'] ) ); switch ( $change_sale_price ) { case 1 : $new_price = $sale_price; break; case 2 : if ( strstr( $sale_price, '%' ) ) { $percent = str_replace( '%', '', $sale_price ) / 100; $new_price = $old_sale_price + ( $old_sale_price * $percent ); } else { $new_price = $old_sale_price + $sale_price; } break; case 3 : if ( strstr( $sale_price, '%' ) ) { $percent = str_replace( '%', '', $sale_price ) / 100; $new_price = $old_sale_price - ( $old_sale_price * $percent ); } else { $new_price = $old_sale_price - $sale_price; } break; case 4 : if ( strstr( $sale_price, '%' ) ) { $percent = str_replace( '%', '', $sale_price ) / 100; $new_price = $product->regular_price - ( $product->regular_price * $percent ); } else { $new_price = $product->regular_price - $sale_price; } break; } if ( isset( $new_price ) && $new_price != $old_sale_price ) { $price_changed = true; update_post_meta( $product->id, '_sale_price', $new_price ); $product->sale_price = $new_price; } } if ( $price_changed ) { update_post_meta( $product->id, '_sale_price_dates_from', '' ); update_post_meta( $product->id, '_sale_price_dates_to', '' ); if ( $product->regular_price < $product->sale_price ) { $product->sale_price = ''; update_post_meta( $product->id, '_sale_price', '' ); } if ( $product->sale_price ) { update_post_meta( $product->id, '_price', $product->sale_price ); } else { update_post_meta( $product->id, '_price', $product->regular_price ); } } } /** * Save a variable subscription's details when the edit product page is submitted for a variable * subscription product type (or the bulk edit product is saved). * * @param int $post_id ID of the parent WC_Product_Variable_Subscription * @return null * @since 1.3 */ public static function process_product_meta_variable_subscription( $post_id ) { if ( ! WC_Subscriptions_Product::is_subscription( $post_id ) || empty( $_POST['_wcsnonce_save_variations'] ) || ! wp_verify_nonce( $_POST['_wcsnonce_save_variations'], 'wcs_subscription_variations' ) ) { return; } // Make sure WooCommerce calculates correct prices $_POST['variable_regular_price'] = isset( $_POST['variable_subscription_price'] ) ? $_POST['variable_subscription_price'] : 0; // Run WooCommerce core saving routine for WC < 2.4 if ( ! is_ajax() ) { WC_Meta_Box_Product_Data::save_variations( $post_id, get_post( $post_id ) ); } if ( ! isset( $_REQUEST['variable_post_id'] ) ) { return; } $variable_post_ids = $_POST['variable_post_id']; $max_loop = max( array_keys( $variable_post_ids ) ); // Save each variations details for ( $i = 0; $i <= $max_loop; $i ++ ) { if ( ! isset( $variable_post_ids[ $i ] ) ) { continue; } $variation_id = absint( $variable_post_ids[ $i ] ); if ( isset( $_POST['variable_subscription_price'] ) && is_array( $_POST['variable_subscription_price'] ) ) { $subscription_price = wc_format_decimal( $_POST['variable_subscription_price'][ $i ] ); update_post_meta( $variation_id, '_subscription_price', $subscription_price ); update_post_meta( $variation_id, '_regular_price', $subscription_price ); } // Make sure trial period is within allowable range $subscription_ranges = wcs_get_subscription_ranges(); $max_trial_length = count( $subscription_ranges[ $_POST['variable_subscription_trial_period'][ $i ] ] ) - 1; $_POST['variable_subscription_trial_length'][ $i ] = absint( $_POST['variable_subscription_trial_length'][ $i ] ); if ( $_POST['variable_subscription_trial_length'][ $i ] > $max_trial_length ) { $_POST['variable_subscription_trial_length'][ $i ] = $max_trial_length; } // Work around a WPML bug which means 'variable_subscription_trial_period' is not set when using "Edit Product" as the product translation interface if ( $_POST['variable_subscription_trial_length'][ $i ] < 0 ) { $_POST['variable_subscription_trial_length'][ $i ] = 0; } $subscription_fields = array( '_subscription_sign_up_fee', '_subscription_period', '_subscription_period_interval', '_subscription_length', '_subscription_trial_period', '_subscription_trial_length', ); foreach ( $subscription_fields as $field_name ) { if ( isset( $_POST[ 'variable' . $field_name ][ $i ] ) ) { update_post_meta( $variation_id, $field_name, wc_clean( $_POST[ 'variable' . $field_name ][ $i ] ) ); } } } // Now that all the variation's meta is saved, sync the min variation price $variable_subscription = wc_get_product( $post_id ); $variable_subscription->variable_product_sync(); } /** * Make sure when saving a subscription via the admin to activate it, it has a valid customer set on it. * * When you click "Add New Subscription", the status is already going to be pending to begin with. This will prevent * changing the status to anything else besides pending if no customer is specified, or the customer specified is * not a valid WP_User. * * Hooked into `woocommerce_subscription_pre_update_status` * * @param string $old_status Previous status of the subscription in update_status * @param string $new_status New status of the subscription in update_status * @param WC_Subscription $subscription The subscription being saved * * @return null * @throws Exception in case there was no user found / there's no customer attached to it */ public static function check_customer_is_set( $old_status, $new_status, $subscription ) { global $post; if ( is_admin() && 'active' == $new_status && ! empty( $post ) && 'shop_subscription' === $post->post_type ) { $customer_user = ( isset( $_POST['customer_user'] ) ) ? sanitize_text_field( $_POST['customer_user'] ) : ''; // csrf in core wp save post function $user = new WP_User( $customer_user ); if ( 0 === $user->ID ) { throw new Exception( sprintf( __( 'Unable to change subscription status to "%s". Please assign a customer to the subscription to activate it.', 'woocommerce-subscriptions' ), $new_status ) ); } } } /** * Set default values for subscription dropdown fields when bulk adding variations to fix issue #1342 * * @param int $variation_id ID the post_id of the variation being added * @return null */ public static function set_variation_meta_defaults_on_bulk_add( $variation_id ) { if ( ! empty( $variation_id ) ) { update_post_meta( $variation_id, '_subscription_period', 'month' ); update_post_meta( $variation_id, '_subscription_period_interval', '1' ); update_post_meta( $variation_id, '_subscription_length', '0' ); update_post_meta( $variation_id, '_subscription_trial_period', 'month' ); } } /** * Adds all necessary admin styles. * * @param array Array of Product types & their labels, excluding the Subscription product type. * @return array Array of Product types & their labels, including the Subscription product type. * @since 1.0 */ public static function enqueue_styles_scripts() { global $post; // Get admin screen id $screen = get_current_screen(); $is_woocommerce_screen = ( in_array( $screen->id, array( 'product', 'edit-shop_order', 'shop_order', 'edit-shop_subscription', 'shop_subscription', 'users', 'woocommerce_page_wc-settings' ) ) ) ? true : false; $is_activation_screen = ( get_transient( WC_Subscriptions::$activation_transient ) == true ) ? true : false; if ( $is_woocommerce_screen ) { $dependencies = array( 'jquery' ); $woocommerce_admin_script_handle = 'wc-admin-meta-boxes'; if ( $screen->id == 'product' ) { $dependencies[] = $woocommerce_admin_script_handle; $dependencies[] = 'wc-admin-product-meta-boxes'; $dependencies[] = 'wc-admin-variation-meta-boxes'; $script_params = array( 'productType' => WC_Subscriptions::$name, 'trialPeriodSingular' => wcs_get_available_time_periods(), 'trialPeriodPlurals' => wcs_get_available_time_periods( 'plural' ), 'subscriptionLengths' => wcs_get_subscription_ranges(), 'trialTooLongMessages' => self::get_trial_period_validation_message( 'separate' ), 'bulkEditPeriodMessage' => __( 'Enter the new period, either day, week, month or year:', 'woocommerce-subscriptions' ), 'bulkEditLengthMessage' => __( 'Enter a new length (e.g. 5):', 'woocommerce-subscriptions' ), 'bulkEditIntervalhMessage' => __( 'Enter a new interval as a single number (e.g. to charge every 2nd month, enter 2):', 'woocommerce-subscriptions' ), 'oneTimeShippingCheckNonce' => wp_create_nonce( 'one_time_shipping' ), ); } else if ( 'edit-shop_order' == $screen->id ) { $script_params = array( 'bulkTrashWarning' => __( "You are about to trash one or more orders which contain a subscription.\n\nTrashing the orders will also trash the subscriptions purchased with these orders.", 'woocommerce-subscriptions' ), ); } else if ( 'shop_order' == $screen->id ) { $dependencies[] = $woocommerce_admin_script_handle; $dependencies[] = 'wc-admin-order-meta-boxes'; if ( WC_Subscriptions::is_woocommerce_pre( '2.6' ) ) { $dependencies[] = 'wc-admin-order-meta-boxes-modal'; } $script_params = array( 'bulkTrashWarning' => __( 'Trashing this order will also trash the subscription purchased with the order.', 'woocommerce-subscriptions' ), 'changeMetaWarning' => __( "WARNING: Bad things are about to happen!\n\nThe payment gateway used to purchase this subscription does not support modifying a subscription's details.\n\nChanges to the billing period, recurring discount, recurring tax or recurring total may not be reflected in the amount charged by the payment gateway.", 'woocommerce-subscriptions' ), 'removeItemWarning' => __( 'You are deleting a subscription item. You will also need to manually cancel and trash the subscription on the Manage Subscriptions screen.', 'woocommerce-subscriptions' ), 'roundAtSubtotal' => esc_attr( get_option( 'woocommerce_tax_round_at_subtotal' ) ), 'EditOrderNonce' => wp_create_nonce( 'woocommerce-subscriptions' ), 'postId' => $post->ID, ); } else if ( 'users' == $screen->id ) { $script_params = array( 'deleteUserWarning' => __( "Warning: Deleting a user will also delete the user's subscriptions. The user's orders will remain but be reassigned to the 'Guest' user.\n\nDo you want to continue to delete this user and any associated subscriptions?", 'woocommerce-subscriptions' ), ); } $script_params['ajaxLoaderImage'] = WC()->plugin_url() . '/assets/images/ajax-loader.gif'; $script_params['ajaxUrl'] = admin_url( 'admin-ajax.php' ); $script_params['isWCPre24'] = var_export( WC_Subscriptions::is_woocommerce_pre( '2.4' ), true ); wp_enqueue_script( 'woocommerce_subscriptions_admin', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/js/admin/admin.js', $dependencies, filemtime( plugin_dir_path( WC_Subscriptions::$plugin_file ) . 'assets/js/admin/admin.js' ) ); wp_localize_script( 'woocommerce_subscriptions_admin', 'WCSubscriptions', apply_filters( 'woocommerce_subscriptions_admin_script_parameters', $script_params ) ); // Maybe add the pointers for first timers if ( isset( $_GET['subscription_pointers'] ) && self::show_user_pointers() ) { $dependencies[] = 'wp-pointer'; $pointer_script_params = array( // translators: placeholders are for HTML tags. They are 1$: "

", 2$: "

", 3$: "

", 4$: "", 5$: "", 6$: "", 7$: "", 8$: "

" 'typePointerContent' => sprintf( _x( '%1$sChoose Subscription%2$s%3$sThe WooCommerce Subscriptions extension adds two new subscription product types - %4$sSimple subscription%5$s and %6$sVariable subscription%7$s.%8$s', 'used in admin pointer script params in javascript as type pointer content', 'woocommerce-subscriptions' ), '

', '

', '

', '', '', '', '', '

' ), // translators: placeholders are for HTML tags. They are 1$: "

", 2$: "

", 3$: "

", 4$: "

" 'pricePointerContent' => sprintf( _x( '%1$sSet a Price%2$s%3$sSubscription prices are a little different to other product prices. For a subscription, you can set a billing period, length, sign-up fee and free trial.%4$s', 'used in admin pointer script params in javascript as price pointer content', 'woocommerce-subscriptions' ), '

', '

', '

', '

' ), ); wp_enqueue_script( 'woocommerce_subscriptions_admin_pointers', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/js/admin/admin-pointers.js', $dependencies, WC_Subscriptions::$version ); wp_localize_script( 'woocommerce_subscriptions_admin_pointers', 'WCSPointers', apply_filters( 'woocommerce_subscriptions_admin_pointer_script_parameters', $pointer_script_params ) ); wp_enqueue_style( 'wp-pointer' ); } } // Maybe add the admin notice if ( $is_activation_screen ) { $woocommerce_plugin_dir_file = self::get_woocommerce_plugin_dir_file(); if ( ! empty( $woocommerce_plugin_dir_file ) ) { wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', self::get_woocommerce_plugin_dir_file() ), array(), WC_Subscriptions::$version ); if ( ! isset( $_GET['page'] ) || 'wcs-about' != $_GET['page'] ) { add_action( 'admin_notices', __CLASS__ . '::admin_installed_notice' ); } } delete_transient( WC_Subscriptions::$activation_transient ); } if ( $is_woocommerce_screen || $is_activation_screen ) { wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), WC_Subscriptions::$version ); wp_enqueue_style( 'woocommerce_subscriptions_admin', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/css/admin.css', array( 'woocommerce_admin_styles' ), WC_Subscriptions::$version ); } } /** * Add the "Active Subscriber?" column to the User's admin table */ public static function add_user_columns( $columns ) { if ( current_user_can( 'manage_woocommerce' ) ) { // Move Active Subscriber before Orders for aesthetics $last_column = array_slice( $columns, -1, 1, true ); array_pop( $columns ); $columns['woocommerce_active_subscriber'] = __( 'Active Subscriber?', 'woocommerce-subscriptions' ); $columns += $last_column; } return $columns; } /** * Hooked to the users table to display a check mark if a given user has an active subscription. * * @param string $value The string to output in the column specified with $column_name * @param string $column_name The string key for the current column in an admin table * @param int $user_id The ID of the user to which this row relates * @return string $value A check mark if the column is the active_subscriber column and the user has an active subscription. * @since 1.0 */ public static function user_column_values( $value, $column_name, $user_id ) { if ( 'woocommerce_active_subscriber' == $column_name ) { if ( wcs_user_has_subscription( $user_id, '', 'active' ) ) { $value = '
'; } else { $value = '
-
'; } } return $value; } /** * 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(); ?>

messages(); ?> views(); ?>
display(); ?>
__( 'Subscriptions', 'woocommerce-subscriptions' ), 'default' => 10, 'option' => self::$option_prefix . '_admin_per_page', ) ); } /** * Sets the correct value for screen options on the Subscription Management admin page. * * @since 1.3.1 */ public static function set_manage_subscriptions_screen_option( $status, $option, $value ) { if ( self::$option_prefix . '_admin_per_page' == $option ) { return $value; } return $status; } /** * Returns the columns for the Manage Subscriptions table, specifically used for adding the * show/hide column screen options. * * @since 1.3.1 */ public static function get_subscription_table_columns( $columns ) { $subscriptions_table = self::get_subscriptions_list_table(); return array_merge( $subscriptions_table->get_columns(), $columns ); } /** * Returns the columns for the Manage Subscriptions table, specifically used for adding the * show/hide column screen options. * * @since 1.3.1 */ public static function get_subscriptions_list_table() { if ( ! isset( self::$subscriptions_list_table ) ) { if ( ! class_exists( 'WC_Subscriptions_List_Table' ) ) { require_once( 'class-wc-subscriptions-list-table.php' ); } self::$subscriptions_list_table = new WC_Subscriptions_List_Table(); } return self::$subscriptions_list_table; } /** * Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function. * * @uses woocommerce_update_options() * @uses self::get_settings() * @since 1.0 */ public static function update_subscription_settings() { if ( empty( $_POST['_wcsnonce'] ) || ! wp_verify_nonce( $_POST['_wcsnonce'], 'wcs_subscription_settings' ) ) { return; } // Make sure automatic payments are on when manual renewals are switched off if ( ! isset( $_POST[ self::$option_prefix . '_accept_manual_renewals' ] ) && isset( $_POST[ self::$option_prefix . '_turn_off_automatic_payments' ] ) ) { unset( $_POST[ self::$option_prefix . '_turn_off_automatic_payments' ] ); } woocommerce_update_options( self::get_settings() ); } /** * Uses the WooCommerce admin fields API to output settings via the @see woocommerce_admin_fields() function. * * @uses woocommerce_admin_fields() * @uses self::get_settings() * @since 1.0 */ public static function subscription_settings_page() { woocommerce_admin_fields( self::get_settings() ); wp_nonce_field( 'wcs_subscription_settings', '_wcsnonce', false ); } /** * Add the Subscriptions settings tab to the WooCommerce settings tabs array. * * @param array $settings_tabs Array of WooCommerce setting tabs & their labels, excluding the Subscription tab. * @return array $settings_tabs Array of WooCommerce setting tabs & their labels, including the Subscription tab. * @since 1.0 */ public static function add_subscription_settings_tab( $settings_tabs ) { $settings_tabs[ self::$tab_name ] = __( 'Subscriptions', 'woocommerce-subscriptions' ); return $settings_tabs; } /** * Sets default values for all the WooCommerce Subscription options. Called on plugin activation. * * @see WC_Subscriptions::activate_woocommerce_subscriptions * @since 1.0 */ public static function add_default_settings() { foreach ( self::get_settings() as $setting ) { if ( isset( $setting['default'] ) ) { add_option( $setting['id'], $setting['default'] ); } } } /** * Get all the settings for the Subscriptions extension in the format required by the @see woocommerce_admin_fields() function. * * @return array Array of settings in the format required by the @see woocommerce_admin_fields() function. * @since 1.0 */ public static function get_settings() { if ( ! function_exists( 'get_editable_roles' ) ) { require_once( ABSPATH . 'wp-admin/includes/user.php' ); } $roles = get_editable_roles(); foreach ( $roles as $role => $details ) { $roles_options[ $role ] = translate_user_role( $details['name'] ); } $available_gateways = array(); foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) { if ( $gateway->supports( 'subscriptions' ) ) { $available_gateways[] = sprintf( '%s [%s]', $gateway->title, $gateway->id ); } } if ( count( $available_gateways ) == 0 ) { // translators: $1-2: opening and closing tags of a link that takes to PayPal settings, $3-4: opening and closing tags of a link that takes to Woo marketplace / Stripe product page $available_gateways_description = sprintf( __( 'No payment gateways capable of processing automatic subscription payments are enabled. Please enable the %1$sPayPal Standard%2$s gateway or get the %3$sfree Stripe extension%4$s if you want to process automatic payments.', 'woocommerce-subscriptions' ), '', '', '', '' ); } elseif ( count( $available_gateways ) == 1 ) { // translators: placeholder is name of a gateway $available_gateways_description = sprintf( __( 'The %s gateway can process automatic subscription payments.', 'woocommerce-subscriptions' ), '' . $available_gateways[0] . '' ); } elseif ( count( $available_gateways ) > 1 ) { // translators: %1$s - a comma separated list of gateway names (e.g. "stripe, paypal, worldpay"), %2$s - one name of gateway (e.g. "authorize.net") $available_gateways_description = sprintf( __( 'The %1$s & %2$s gateways can process automatic subscription payments.', 'woocommerce-subscriptions' ), '' . implode( ', ', array_slice( $available_gateways, 0, count( $available_gateways ) - 1 ) ) . '', '' . array_pop( $available_gateways ) . '' ); } return apply_filters( 'woocommerce_subscription_settings', array( array( 'name' => __( 'Button Text', 'woocommerce-subscriptions' ), 'type' => 'title', 'desc' => '', 'id' => self::$option_prefix . '_button_text', ), array( 'name' => __( 'Add to Cart Button Text', 'woocommerce-subscriptions' ), 'desc' => __( 'A product displays a button with the text "Add to Cart". By default, a subscription changes this to "Sign Up Now". You can customise the button text for subscriptions here.', 'woocommerce-subscriptions' ), 'tip' => '', 'id' => self::$option_prefix . '_add_to_cart_button_text', 'css' => 'min-width:150px;', 'default' => __( 'Sign Up Now', 'woocommerce-subscriptions' ), 'type' => 'text', 'desc_tip' => true, ), array( 'name' => __( 'Place Order Button Text', 'woocommerce-subscriptions' ), 'desc' => __( 'Use this field to customise the text displayed on the checkout button when an order contains a subscription. Normally the checkout submission button displays "Place Order". When the cart contains a subscription, this is changed to "Sign Up Now".', 'woocommerce-subscriptions' ), 'tip' => '', 'id' => self::$option_prefix . '_order_button_text', 'css' => 'min-width:150px;', 'default' => __( 'Sign Up Now', 'woocommerce-subscriptions' ), 'type' => 'text', 'desc_tip' => true, ), array( 'type' => 'sectionend', 'id' => self::$option_prefix . '_button_text' ), array( 'name' => __( 'Roles', 'woocommerce-subscriptions' ), 'type' => 'title', // translators: placeholders are tags 'desc' => sprintf( __( 'Choose the default roles to assign to active and inactive subscribers. For record keeping purposes, a user account must be created for subscribers. Users with the %sadministrator%s role, such as yourself, will never be allocated these roles to prevent locking out administrators.', 'woocommerce-subscriptions' ), '', '' ), 'id' => self::$option_prefix . '_role_options', ), array( 'name' => __( 'Subscriber Default Role', 'woocommerce-subscriptions' ), 'desc' => __( 'When a subscription is activated, either manually or after a successful purchase, new users will be assigned this role.', 'woocommerce-subscriptions' ), 'tip' => '', 'id' => self::$option_prefix . '_subscriber_role', 'css' => 'min-width:150px;', 'default' => 'subscriber', 'type' => 'select', 'options' => $roles_options, 'desc_tip' => true, ), array( 'name' => __( 'Inactive Subscriber Role', 'woocommerce-subscriptions' ), 'desc' => __( 'If a subscriber\'s subscription is manually cancelled or expires, she will be assigned this role.', 'woocommerce-subscriptions' ), 'tip' => '', 'id' => self::$option_prefix . '_cancelled_role', 'css' => 'min-width:150px;', 'default' => 'customer', 'type' => 'select', 'options' => $roles_options, 'desc_tip' => true, ), array( 'type' => 'sectionend', 'id' => self::$option_prefix . '_role_options' ), array( 'name' => _x( 'Renewals', 'option section heading', 'woocommerce-subscriptions' ), 'type' => 'title', 'desc' => '', 'id' => self::$option_prefix . '_renewal_options', ), array( 'name' => __( 'Manual Renewal Payments', 'woocommerce-subscriptions' ), 'desc' => __( 'Accept Manual Renewals', 'woocommerce-subscriptions' ), 'id' => self::$option_prefix . '_accept_manual_renewals', 'default' => 'no', 'type' => 'checkbox', // translators: placeholders are opening and closing link tags 'desc_tip' => sprintf( __( 'With manual renewals, a customer\'s subscription is put on-hold until they login and pay to renew it. %sLearn more%s.', 'woocommerce-subscriptions' ), '', '' ), 'checkboxgroup' => 'start', 'show_if_checked' => 'option', ), array( 'desc' => __( 'Turn off Automatic Payments', 'woocommerce-subscriptions' ), 'id' => self::$option_prefix . '_turn_off_automatic_payments', 'default' => 'no', 'type' => 'checkbox', // translators: placeholders are opening and closing link tags 'desc_tip' => sprintf( __( 'If you never want a customer to be automatically charged for a subscription renewal payment, you can turn off automatic payments completely. %sLearn more%s.', 'woocommerce-subscriptions' ), '', '' ), 'checkboxgroup' => 'end', 'show_if_checked' => 'yes', ), array( 'type' => 'sectionend', 'id' => self::$option_prefix . '_renewal_options' ), array( 'name' => _x( 'Miscellaneous', 'options section heading', 'woocommerce-subscriptions' ), 'type' => 'title', 'desc' => '', 'id' => self::$option_prefix . '_miscellaneous', ), array( 'name' => __( 'Customer Suspensions', 'woocommerce-subscriptions' ), 'desc' => _x( 'suspensions per billing period.', 'there\'s a number immediately in front of this text', 'woocommerce-subscriptions' ), 'id' => self::$option_prefix . '_max_customer_suspensions', 'css' => 'min-width:50px;', 'default' => 0, 'type' => 'select', 'options' => apply_filters( 'woocommerce_subscriptions_max_customer_suspension_range', array_merge( range( 0, 12 ), array( 'unlimited' => 'Unlimited' ) ) ), 'desc_tip' => __( 'Set a maximum number of times a customer can suspend their account for each billing period. For example, for a value of 3 and a subscription billed yearly, if the customer has suspended their account 3 times, they will not be presented with the option to suspend their account until the next year. Store managers will always be able able to suspend an active subscription. Set this to 0 to turn off the customer suspension feature completely.', 'woocommerce-subscriptions' ), ), array( 'name' => __( 'Mixed Checkout', 'woocommerce-subscriptions' ), 'desc' => __( 'Allow subscriptions and products to be purchased simultaneously.', 'woocommerce-subscriptions' ), 'id' => self::$option_prefix . '_multiple_purchase', 'default' => 'no', 'type' => 'checkbox', 'desc_tip' => __( 'Allow subscriptions and products to be purchased in a single transaction.', 'woocommerce-subscriptions' ), ), array( 'name' => __( 'Drip Downloadable Content', 'woocommerce-subscriptions' ), 'desc' => __( 'Enable dripping for downloadable content on subscription products.', 'woocommerce-subscriptions' ), 'id' => self::$option_prefix . '_drip_downloadable_content_on_renewal', 'default' => 'no', 'type' => 'checkbox', 'desc_tip' => sprintf( __( 'Enabling this grants access to new downloadable files added to a product only after the next renewal is processed.%sBy default, access to new downloadable files added to a product is granted immediately to any customer that has an active subscription with that product.', 'woocommerce-subscriptions' ), '
' ), ), array( 'type' => 'sectionend', 'id' => self::$option_prefix . '_miscellaneous' ), array( 'name' => __( 'Payment Gateways', 'woocommerce-subscriptions' ), 'desc' => $available_gateways_description, 'id' => self::$option_prefix . '_payment_gateways_available', 'type' => 'informational', ), array( // translators: placeholders are opening and closing link tags 'desc' => sprintf( __( 'Other payment gateways can be used to process %smanual subscription renewal payments%s only.', 'woocommerce-subscriptions' ), '', '' ), 'id' => self::$option_prefix . '_payment_gateways_additional', 'type' => 'informational', ), array( // translators: $1-$2: opening and closing tags. Link to documents->payment gateways, 3$-4$: opening and closing tags. Link to woothemes extensions shop page 'desc' => sprintf( __( 'Find new gateways that %1$ssupport automatic subscription payments%2$s in the official %3$sWooCommerce Marketplace%4$s.', 'woocommerce-subscriptions' ), '', '', '', '' ), 'id' => self::$option_prefix . '_payment_gateways_additional', 'type' => 'informational', ), ) ); } /** * Displays instructional information for a WooCommerce setting. * * @since 1.0 */ public static function add_informational_admin_field( $field_details ) { if ( isset( $field_details['name'] ) && $field_details['name'] ) { echo '

' . esc_html( $field_details['name'] ) . '

'; } if ( isset( $field_details['desc'] ) && $field_details['desc'] ) { echo wp_kses_post( wpautop( wptexturize( $field_details['desc'] ) ) ); } } /** * Outputs a welcome message. Called when the Subscriptions extension is activated. * * @since 1.0 */ public static function admin_installed_notice() { ?>

tags, $3-$4: opening and closing tags echo wp_kses( sprintf( __( '%1$sWooCommerce Subscriptions Installed%2$s – %3$sYou\'re ready to start selling subscriptions!%4$s', 'woocommerce-subscriptions' ), '', '', '', '' ), array( 'strong' => true, 'em' => true ) ); ?>

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; } /** * Display a notice indicating that the "Orders" list is filtered. * @see self::filter_orders() */ public static function display_renewal_filter_notice() { global $wp_version; $query_arg = '_subscription_related_orders'; if ( isset( $_GET[ $query_arg ] ) && $_GET[ $query_arg ] > 0 && true === self::$found_related_orders ) { $initial_order = new WC_Order( absint( $_GET[ $query_arg ] ) ); if ( version_compare( $wp_version, '4.2', '<' ) ) { echo '

'; printf( '×', esc_url( remove_query_arg( $query_arg ) ) ); // translators: placeholders are opening link tag, ID of sub, and closing link tag printf( esc_html__( 'Showing orders for %sSubscription %s%s', 'woocommerce-subscriptions' ), '', esc_html( $initial_order->get_order_number() ), '' ); echo '

'; } else { echo ''; } } /** * Returns either a string or array of strings describing the allowable trial period range * for a subscription. * * @since 1.0 */ public static function get_trial_period_validation_message( $form = 'combined' ) { $subscription_ranges = wcs_get_subscription_ranges(); if ( 'combined' == $form ) { // translators: number of 1$: days, 2$: weeks, 3$: months, 4$: years $error_message = sprintf( __( 'The trial period can not exceed: %1s, %2s, %3s or %4s.', 'woocommerce-subscriptions' ), array_pop( $subscription_ranges['day'] ), array_pop( $subscription_ranges['week'] ), array_pop( $subscription_ranges['month'] ), array_pop( $subscription_ranges['year'] ) ); } else { $error_message = array(); foreach ( wcs_get_available_time_periods() as $period => $string ) { // translators: placeholder is a time period (e.g. "4 weeks") $error_message[ $period ] = sprintf( __( 'The trial period can not exceed %s.', 'woocommerce-subscriptions' ), array_pop( $subscription_ranges[ $period ] ) ); } } return apply_filters( 'woocommerce_subscriptions_trial_period_validation_message', $error_message ); } /** * Callback for the [subscriptions] shortcode that displays subscription names for a particular user. * * @param array $attributes Shortcode attributes. * @return string */ public static function do_subscriptions_shortcode( $attributes ) { $attributes = wp_parse_args( $attributes, array( 'user_id' => 0, 'status' => 'active', ) ); $subscriptions = wcs_get_users_subscriptions( $attributes['user_id'] ); if ( empty( $subscriptions ) ) { return '
  • ' . esc_html_x( 'No subscriptions found.', 'in [subscriptions] shortcode', 'woocommerce-subscriptions' ) . '
'; } $list = '
    '; foreach ( $subscriptions as $subscription ) { if ( 'all' == $attributes['status'] || $subscription->has_status( $attributes['status'] ) ) { // translators: order number $shortcode_translate = sprintf( esc_html_x( 'Subscription %s', 'in [subscriptions] shortcode', 'woocommerce-subscriptions' ), $subscription->get_order_number() ); $list .= sprintf( '
  • %s
  • ', $subscription->get_view_order_url(), $shortcode_translate ); } } $list .= '
'; return $list; } /** * Adds Subscriptions specific details to the WooCommerce System Status report. * * @param array $attributes Shortcode attributes. * @return array */ public static function add_system_status_items( $debug_data ) { $is_wcs_debug = defined( 'WCS_DEBUG' ) ? WCS_DEBUG : false; $debug_data['wcs_debug'] = array( 'name' => _x( 'WCS_DEBUG', 'label that indicates whether debugging is turned on for the plugin', 'woocommerce-subscriptions' ), 'note' => ( $is_wcs_debug ) ? __( 'Yes', 'woocommerce-subscriptions' ) : __( 'No', 'woocommerce-subscriptions' ), 'success' => $is_wcs_debug ? 0 : 1, ); $debug_data['wcs_staging'] = array( 'name' => _x( 'Subscriptions Mode', 'Live or Staging, Label on WooCommerce -> System Status page', 'woocommerce-subscriptions' ), 'note' => '' . ( ( WC_Subscriptions::is_duplicate_site() ) ? _x( 'Staging', 'refers to staging site', 'woocommerce-subscriptions' ) : _x( 'Live', 'refers to live site', 'woocommerce-subscriptions' ) ) . '', 'success' => ( WC_Subscriptions::is_duplicate_site() ) ? 0 : 1, ); return $debug_data; } /** * A WooCommerce version aware function for getting the Subscriptions admin settings * tab URL. * * @since 1.4.5 * @return string */ public static function settings_tab_url() { $settings_tab_url = admin_url( 'admin.php?page=wc-settings&tab=subscriptions' ); return apply_filters( 'woocommerce_subscriptions_settings_tab_url', $settings_tab_url ); } /** * Add a column to the Payment Gateway table to show whether the gateway supports automated renewals. * * @since 1.5 * @return string */ public static function payment_gateways_rewewal_column( $header ) { $header_new = array_slice( $header, 0, count( $header ) - 1, true ) + array( 'renewals' => __( 'Automatic Recurring Payments', 'woocommerce-subscriptions' ) ) + // Ideally, we could add a link to the docs here, but the title is passed through esc_html() array_slice( $header, count( $header ) - 1, count( $header ) - ( count( $header ) - 1 ), true ); return $header_new; } /** * Check whether the payment gateway passed in supports automated renewals or not. * Automatically flag support for Paypal since it is included with subscriptions. * Display in the Payment Gateway column. * * @since 1.5 */ public static function payment_gateways_rewewal_support( $gateway ) { echo ''; if ( ( is_array( $gateway->supports ) && in_array( 'subscriptions', $gateway->supports ) ) || $gateway->id == 'paypal' ) { $status_html = '' . esc_html__( 'Yes', 'woocommerce-subscriptions' ) . ''; } else { $status_html = '-'; } $allowed_html = wp_kses_allowed_html( 'post' ); $allowed_html['span']['data-tip'] = true; /** * Automatic Renewal Payments Support Status HTML Filter. * * @since 2.0 * @param string $status_html * @param \WC_Payment_Gateway $gateway */ echo wp_kses( apply_filters( 'woocommerce_payment_gateways_renewal_support_status_html', $status_html, $gateway ), $allowed_html ); echo ''; } /** * Do not display formatted order total on the Edit Order administration screen * * @since 1.5.17 */ public static function maybe_remove_formatted_order_total_filter( $formatted_total, $order ) { // Check if we're on the Edit Order screen - get_current_screen() only exists on admin pages so order of operations matters here if ( is_admin() && function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( is_object( $screen ) && 'shop_order' == $screen->id ) { remove_filter( 'woocommerce_get_formatted_order_total', 'WC_Subscriptions_Order::get_formatted_order_total', 10, 2 ); } } return $formatted_total; } /** * Deprecated due to new meta boxes required for WC 2.2. * * @deprecated 1.5.10 */ public static function add_related_orders_meta_box() { _deprecated_function( __METHOD__, '1.5.10', __CLASS__ . '::add_meta_boxes()' ); self::add_meta_boxes(); } /** * Outputs the contents of the "Renewal Orders" meta box. * * @param object $post Current post data. */ public static function related_orders_meta_box( $post ) { _deprecated_function( __METHOD__, '2.0', 'WCS_Meta_Box_Related_Orders::output()' ); WCS_Meta_Box_Related_Orders::output(); } /** * Add users with subscriptions to the "Customers" report in WooCommerce -> Reports. * * @param WP_User_Query $user_query */ public static function add_subscribers_to_customers( $user_query ) { _deprecated_function( __METHOD__, '2.0' ); } /** * Set a translation safe screen ID for Subcsription * * @since 1.3.3 */ public static function set_admin_screen_id() { _deprecated_function( __METHOD__, '2.0' ); } /** * Once we have set a correct admin page screen ID, we can use it for adding the Manage Subscriptions table's columns. * * @since 1.3.3 */ public static function add_subscriptions_table_column_filter() { _deprecated_function( __METHOD__, '2.0' ); } /** * Removes anything that's not a digit or a dot from a string. Sadly it assumes that the decimal separator is a dot. * That however can be changed in WooCommerce settings, surfacing bugs such as 9,90 becoming 990, a hundred fold * increase. Use wc_format_decimal instead. * * Left in for backward compatibility reasons. * * @deprecated 1.5.24 */ private static function clean_number( $number ) { _deprecated_function( __METHOD__, '1.5.23', 'wc_format_decimal()' ); $number = preg_replace( '/[^0-9\.]/', '', $number ); return $number; } /** * Filter the "Orders" list to show only renewal orders associated with a specific parent order. * * @param array $request * @return array */ public static function filter_orders_by_renewal_parent( $request ) { _deprecated_function( __METHOD__, '2.0' ); return $request; } /** * Registers the "Renewal Orders" meta box for the "Edit Order" page. * @deprecated 2.0 */ public static function add_meta_boxes() { _deprecated_function( __METHOD__, '2.0', 'WCS_Admin_Meta_Boxes::add_meta_boxes()' ); } /** * Output the metabox */ public static function recurring_totals_meta_box( $post ) { _deprecated_function( __METHOD__, '2.0' ); } } WC_Subscriptions_Admin::init();