Completed
Push — master ( 55c935...d6f625 )
by Sébastien
01:59
created

WCSATT_STT_Admin::admin_style()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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