Completed
Push — master ( 31d6dc...971dad )
by Sébastien
29s
created

WCSATT_STT_Admin::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Filters the product meta data in the admin for subscription schemes.
4
 *
5
 * @class WCSATT_STT_Admin
6
 * @since 1.0.0
7
 */
8
9
class WCSATT_STT_Admin extends WCS_ATT_Admin {
10
11
	/**
12
	 * Initialize the product meta.
13
	 *
14
	 * @access public
15
	 * @static
16
	 */
17
	public static function init() {
18
		// Adds to the default values for subscriptions schemes content.
19
		add_filter( 'wcsatt_default_subscription_scheme', __CLASS__ . '::subscription_schemes_content', 10, 1 );
20
21
		// Subscription scheme options displayed on the 'wcsatt_subscription_scheme_product_content' action.
22
		add_action( 'wcsatt_subscription_scheme_product_content', __CLASS__ . '::wcsatt_stt_fields', 15, 3 );
23
24
		// Filter the subscription scheme data to process the sign up and trial options on the ''wcsatt_subscription_scheme_process_scheme_data' filter.
25
		add_filter( 'wcsatt_subscription_scheme_process_scheme_data', __CLASS__ . '::wcsatt_stt_process_scheme_data', 10, 2 );
26
	}
27
28
	/**
29
	 * Adds the default values for subscriptions schemes content.
30
	 *
31
	 * @access public
32
	 * @static
33
	 * @param  array $defaults
34
	 * @return void
35
	 */
36
	public static function subscription_schemes_content( $defaults ) {
37
		$new_defaults = array(
38
			'subscription_sign_up_fee'  => '',
39
			'subscription_trial_length' => '',
40
			'subscription_trial_period' => ''
41
		);
42
43
		return array_merge( $new_defaults, $defaults );
44
	} // END subscription_schemes_content()
45
46
	/**
47
	 * Adds the trial and sign up fields under the subscription section.
48
	 *
49
	 * @access public
50
	 * @static
51
	 * @param  int   $index
52
	 * @param  array $scheme_data
53
	 * @param  int   $post_id
54
	 * @return void
55
	 */
56
	public static function wcsatt_stt_fields( $index, $scheme_data, $post_id ) {
57
		if ( ! empty( $scheme_data ) ) {
58
			$subscription_sign_up_fee = ! empty( $scheme_data[ 'subscription_sign_up_fee' ] ) ? $scheme_data[ 'subscription_sign_up_fee' ] : '';
59
			$subscription_trial_length = isset( $scheme_data[ 'subscription_trial_length' ] ) ? $scheme_data[ 'subscription_trial_length' ] : 0;
60
			$subscription_trial_period = isset( $scheme_data[ 'subscription_trial_period' ] ) ? $scheme_data[ 'subscription_trial_period' ] : '';
61
		} else {
62
			$subscription_sign_up_fee = '';
63
			$subscription_trial_length = 0;
64
			$subscription_trial_period = '';
65
		}
66
67
		// Sign-up Fee
68
		woocommerce_wp_text_input( array(
69
			'id'          => '_subscription_sign_up_fee',
70
			'class'       => 'wc_input_subscription_intial_price',
71
			// translators: %s is a currency symbol / code
72
			'label'       => sprintf( __( 'Sign-up Fee (%s)', WCSATT_STT::TEXT_DOMAIN ), get_woocommerce_currency_symbol() ),
73
			'placeholder' => _x( 'e.g. 9.90', 'example price', WCSATT_STT::TEXT_DOMAIN ),
74
			'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.', WCSATT_STT::TEXT_DOMAIN ),
75
			'desc_tip'    => true,
76
			'type'        => 'text',
77
			'custom_attributes' => array(
78
				'step' => 'any',
79
				'min'  => '0',
80
			),
81
			'name'        => 'wcsatt_schemes[' . $index . '][subscription_sign_up_fee]',
82
			'value'       => $subscription_sign_up_fee
83
		) );
84
85
		// Trial Length
86
		woocommerce_wp_text_input( array(
87
			'id'          => '_subscription_trial_length',
88
			'class'       => 'wc_input_subscription_trial_length',
89
			'label'       => __( 'Free Trial', WCSATT_STT::TEXT_DOMAIN ),
90
			'name'        => 'wcsatt_schemes[' . $index . '][subscription_trial_length]',
91
			'value'       => $subscription_trial_length
92
		) );
93
94
		// Trial Period
95
		woocommerce_wp_select( array(
96
			'id'          => '_subscription_trial_period',
97
			'class'       => 'wc_input_subscription_trial_period',
98
			'label'       => __( 'Subscription Trial Period', WCSATT_STT::TEXT_DOMAIN ),
99
			'options'     => wcs_get_available_time_periods(),
100
			// translators: placeholder is trial period validation message if passed an invalid value (e.g. "Trial period can not exceed 4 weeks")
101
			'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', WCSATT_STT::TEXT_DOMAIN ), WC_Subscriptions_Admin::get_trial_period_validation_message() ),
102
			'desc_tip'    => true,
103
			'value'       => WC_Subscriptions_Product::get_trial_period( $post_id ), // Explicitly set value in to ensure backward compatibility
104
			'name'        => 'wcsatt_schemes[' . $index . '][subscription_trial_period]',
105
			'value'       => $subscription_trial_period
106
	) );
107
	} // END wcsatt_stt_fields()
108
109
	/**
110
	 * Filters the subscription scheme data to pass the 
111
	 * sign up and trial options when saving.
112
	 *
113
	 * @access public
114
	 * @static
115
	 * @param  ini    $posted_scheme
116
	 * @param  string $product_type
117
	 * @return void
118
	 */
119
	public static function wcsatt_stt_process_scheme_data( $posted_scheme, $product_type ) {
120
		// Copy variable type fields.
121
		if ( 'variable' == $product_type ) {
122
			if ( isset( $posted_scheme[ 'subscription_sign_up_fee_variable' ] ) ) {
123
				$posted_scheme[ 'subscription_sign_up_fee' ] = $posted_scheme[ 'subscription_sign_up_fee_variable' ];
124
			}
125
			if ( isset( $posted_scheme[ 'subscription_trial_length_variable' ] ) ) {
126
				$posted_scheme[ 'subscription_trial_length' ] = $posted_scheme[ 'subscription_trial_length_variable' ];
127
			}
128
			if ( isset( $posted_scheme[ 'subscription_trial_period_variable' ] ) ) {
129
				$posted_scheme[ 'subscription_trial_period' ] = $posted_scheme[ 'subscription_trial_period_variable'];
130
			}
131
		}
132
133
		// Format subscription sign up fee.
134
		if ( isset( $posted_scheme[ 'subscription_sign_up_fee' ] ) ) {
135
			$posted_scheme[ 'subscription_sign_up_fee' ] = ( $posted_scheme[ 'subscription_sign_up_fee' ] === '' ) ? '' : wc_format_decimal( $posted_scheme[ 'subscription_sign_up_fee' ] );
136
		}
137
138
		// Make sure trial period is within allowable range.
139
		$subscription_ranges = wcs_get_subscription_ranges();
140
		$max_trial_length = count( $subscription_ranges[ $posted_scheme[ 'subscription_trial_period' ] ] ) - 1;
141
142
		// Format subscription trial length.
143
		if ( isset( $posted_scheme[ 'subscription_trial_length' ] ) && $posted_scheme[ 'subscription_trial_length' ] > $max_trial_length ) {
144
			$posted_scheme[ 'subscription_trial_length' ] = ( $posted_scheme[ 'subscription_trial_length' ] === '' ) ? '' : absint( $posted_scheme[ 'subscription_trial_length' ] );
145
		}
146
147
		// Format subscription trial period.
148
		$trial_periods = apply_filters( 'wcsatt_stt_trial_periods', array( 'day', 'week', 'month', 'year' ) );
149
		if ( isset( $posted_scheme[ 'subscription_trial_period' ] ) && in_array( $posted_scheme[ 'subscription_trial_period' ], $trial_periods ) ) {
150
			$posted_scheme[ 'subscription_trial_period' ] = trim( $posted_scheme[ 'subscription_trial_period' ] );
151
		}
152
153
		return $posted_scheme;
154
	} // END wcsatt_stt_process_scheme_data()
155
156
}
157
158
WCSATT_STT_Admin::init();
159