PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
← All changes | includes/Admin/Settings.php +234 -19 1.4.02.0.0 View file →
@@ -7,14 +7,23 @@
7 7 *
8 8 * @package SpringDevs\Subscription\Admin
9 9 */
10 10 class Settings {
11 + /**
12 + * Settings fields.
13 + *
14 + * @var array
15 + */
16 + public $settings_fields = [];
11 17
12 18 /**
13 19 * Initialize the class.
14 20 */
15 21 public function __construct() {
16 - add_action( 'admin_menu', array( $this, 'admin_menu' ), 30 );
22 + // Load the settings helper.
23 + SettingsHelper::get_instance();
24 +
25 + add_action( 'admin_menu', array( $this, 'admin_menu' ) );
17 26 add_action( 'admin_init', array( $this, 'register_settings' ) );
18 27 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wc_admin_styles' ) );
19 28 }
20 29
@@ -23,31 +32,188 @@
23 32 *
24 33 * @return void
25 34 */
26 35 public function admin_menu() {
27 - $post_type_link = 'edit.php?post_type=subscrpt_order';
36 + // Initialize & process settings fields.
37 + $this->initiate_settings_fields();
28 38
29 - add_submenu_page(
30 - $post_type_link,
31 - __( 'WP Subscription Settings', 'wp_subscription' ),
32 - __( 'Settings', 'wp_subscription' ),
33 - 'manage_options',
34 - 'edit.php?post_type=wp_subscription_settings',
35 - array( $this, 'settings_content' ),
36 - 40
39 + // Add submenu page.
40 + $parent_slug = 'wp-subscription';
41 + add_submenu_page(
42 + $parent_slug,
43 + __( 'Settings', 'subscription' ),
44 + __( 'Settings', 'subscription' ),
45 + 'manage_options',
46 + 'wp-subscription-settings',
47 + [ $this, 'settings_content' ]
37 48 );
38 49 }
39 50
40 51 /**
52 + * Initialize settings fields.
53 + */
54 + public function initiate_settings_fields() {
55 + global $wp_roles;
56 + $roles = [];
57 + foreach ( ( $wp_roles->roles ?? [] ) as $role_key => $role ) {
58 + $roles[ $role_key ] = $role['name'];
59 + }
60 +
61 + // Setting fields.
62 + $settings_fields = [
63 + [
64 + 'type' => 'heading',
65 + 'group' => 'renewals',
66 + 'priority' => 0,
67 + 'field_data' => [
68 + 'title' => __( 'Renewals', 'subscription' ),
69 + ],
70 + ],
71 + [
72 + 'type' => 'select',
73 + 'group' => 'renewals',
74 + 'priority' => 1,
75 + 'field_data' => [
76 + 'id' => 'wp_subscription_renewal_process',
77 + 'title' => __( 'Renewal Process', 'subscription' ),
78 + 'description' => __( 'How renewal process will be done after Subscription Expired.', 'subscription' ),
79 + 'options' => [
80 + 'auto' => __( 'Automatic', 'subscription' ),
81 + 'manual' => __( 'Manual', 'subscription' ),
82 + ],
83 + 'selected' => esc_attr( subscrpt_get_renewal_process() ),
84 + ],
85 + ],
86 + [
87 + 'type' => 'input',
88 + 'group' => 'renewals',
89 + 'priority' => 2,
90 + 'field_data' => [
91 + 'id' => 'wp_subscription_manual_renew_cart_notice',
92 + 'title' => __( 'Renewal Cart Notice', 'subscription' ),
93 + 'description' => __( 'Display Notice when Renewal Subscription product add to cart. Only available for Manual Renewal Process.', 'subscription' ),
94 + 'value' => esc_attr( subscrpt_get_manual_renew_cart_notice() ),
95 + ],
96 + ],
97 + [
98 + 'type' => 'toggle',
99 + 'group' => 'renewals',
100 + 'priority' => 3,
101 + 'field_data' => [
102 + 'id' => 'wp_subscription_stripe_auto_renew',
103 + 'title' => __( 'Stripe Auto Renewal', 'subscription' ),
104 + 'label' => __( 'Accept Stripe Auto Renewals', 'subscription' ),
105 + 'description' => sprintf(
106 + /* translators: HTML tags */
107 + __( '%1$s WooCommerce Stripe Payment Gateway %2$s plugin is required!', 'subscription' ),
108 + '<a href="https://wordpress.org/plugins/woocommerce-gateway-stripe/" target="_blank">',
109 + '</a>'
110 + ),
111 + 'value' => '1',
112 + 'checked' => '1' === get_option( 'wp_subscription_stripe_auto_renew', '1' ),
113 + ],
114 + ],
115 + [
116 + 'type' => 'toggle',
117 + 'group' => 'renewals',
118 + 'priority' => 4,
119 + 'field_data' => [
120 + 'id' => 'wp_subscription_auto_renewal_toggle',
121 + 'title' => __( 'Auto Renewal Toggle', 'subscription' ),
122 + 'label' => __( 'Display the auto renewal toggle', 'subscription' ),
123 + 'description' => __( 'Allow customers to turn on and off automatic renewals from their Subscription details page', 'subscription' ),
124 + 'value' => '1',
125 + 'checked' => '1' === get_option( 'wp_subscription_auto_renewal_toggle', '1' ),
126 + ],
127 + ],
128 + [
129 + 'type' => 'select',
130 + 'group' => 'role_based_settings',
131 + 'priority' => 2,
132 + 'field_data' => [
133 + 'id' => 'wp_subscription_active_role',
134 + 'title' => __( 'Subscriber Default Role', 'subscription' ),
135 + 'description' => __( 'When a subscription is activated, either manually or after a successful purchase, new users will be assigned this role.', 'subscription' ),
136 + 'options' => $roles,
137 + 'selected' => esc_attr( get_option( 'wp_subscription_active_role', 'subscriber' ) ),
138 + ],
139 + ],
140 + [
141 + 'type' => 'select',
142 + 'group' => 'role_based_settings',
143 + 'priority' => 3,
144 + 'field_data' => [
145 + 'id' => 'wp_subscription_unactive_role',
146 + 'title' => __( 'Subscriber Inactive Role', 'subscription' ),
147 + 'description' => __( "If a subscriber's subscription is manually cancelled or expires, they will be assigned this role.", 'subscription' ),
148 + 'options' => $roles,
149 + 'selected' => esc_attr( get_option( 'wp_subscription_unactive_role', 'customer' ) ),
150 + ],
151 + ],
152 + ];
153 +
154 + // Allow other modules to add/modify settings fields.
155 + $settings_fields = apply_filters( 'subscrpt_settings_fields', $settings_fields );
156 +
157 + // Process settings fields (group & sort).
158 + $settings_fields = apply_filters( 'process_subscrpt_settings_fields', $settings_fields );
159 +
160 + // Set the settings fields.
161 + $this->settings_fields = $settings_fields;
162 + }
163 +
164 + /**
41 165 * Register settings options.
42 166 **/
43 167 public function register_settings() {
44 - register_setting( 'wp_subscription_settings', 'wp_subscription_renewal_process' );
45 - register_setting( 'wp_subscription_settings', 'wp_subscription_manual_renew_cart_notice' );
46 - register_setting( 'wp_subscription_settings', 'wp_subscription_active_role' );
47 - register_setting( 'wp_subscription_settings', 'wp_subscription_unactive_role' );
48 - register_setting( 'wp_subscription_settings', 'wp_subscription_stripe_auto_renew' );
49 - register_setting( 'wp_subscription_settings', 'wp_subscription_auto_renewal_toggle' );
168 + register_setting(
169 + 'wp_subscription_settings',
170 + 'wp_subscription_renewal_process',
171 + array(
172 + 'type' => 'string',
173 + 'sanitize_callback' => 'sanitize_text_field',
174 + )
175 + );
176 + register_setting(
177 + 'wp_subscription_settings',
178 + 'wp_subscription_manual_renew_cart_notice',
179 + array(
180 + 'type' => 'string',
181 + 'sanitize_callback' => 'sanitize_textarea_field',
182 + )
183 + );
184 + register_setting(
185 + 'wp_subscription_settings',
186 + 'wp_subscription_active_role',
187 + array(
188 + 'type' => 'string',
189 + 'sanitize_callback' => 'sanitize_text_field',
190 + )
191 + );
192 + register_setting(
193 + 'wp_subscription_settings',
194 + 'wp_subscription_unactive_role',
195 + array(
196 + 'type' => 'string',
197 + 'sanitize_callback' => 'sanitize_text_field',
198 + )
199 + );
200 + register_setting(
201 + 'wp_subscription_settings',
202 + 'wp_subscription_stripe_auto_renew',
203 + array(
204 + 'type' => 'string',
205 + 'sanitize_callback' => 'sanitize_text_field',
206 + )
207 + );
208 + register_setting(
209 + 'wp_subscription_settings',
210 + 'wp_subscription_auto_renewal_toggle',
211 + array(
212 + 'type' => 'string',
213 + 'sanitize_callback' => 'sanitize_text_field',
214 + )
215 + );
50 216
51 217 do_action( 'subscrpt_register_settings', 'subscrpt_settings' );
52 218 }
53 219
@@ -54,21 +220,70 @@
54 220 /**
55 221 * Settings HTML.
56 222 */
57 223 public function settings_content() {
224 + $settings_fields = $this->settings_fields;
225 + $category_groups = SettingsHelper::category_groups( $settings_fields );
226 + $active_cat = $this->get_active_category( $category_groups );
227 +
228 + // Header.
229 + $menu = new Menu();
230 + $menu->render_admin_header( __( 'Settings', 'subscription' ) );
231 +
58 232 include 'views/settings.php';
233 +
234 + // Footer.
235 + $menu->render_admin_footer();
59 236 }
60 237
61 238 /**
239 + * Which settings section the page opens on.
240 + *
241 + * Read from `?cat=`, and it has to be a query argument rather than a
242 + * fragment: the form posts to `options.php`, which redirects back to
243 + * `_wp_http_referer`, and a fragment never reaches the server. Keeping the
244 + * section in the query string is what returns you to the panel you saved
245 + * from.
246 + *
247 + * Validated against the sections that actually hold groups, so an unknown
248 + * or hostile value — or one naming a section only Pro fills — opens the
249 + * first real section rather than an empty panel.
250 + *
251 + * @param array<string,string[]> $category_groups Section key => group keys.
252 + * @return string Section key, or an empty string when there are no groups.
253 + */
254 + private function get_active_category( array $category_groups ) {
255 + $cats = array_keys( $category_groups );
256 +
257 + if ( empty( $cats ) ) {
258 + return '';
259 + }
260 +
261 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only section selection, validated against the list above.
262 + $requested = isset( $_GET['cat'] ) ? sanitize_key( wp_unslash( $_GET['cat'] ) ) : '';
263 +
264 + // `all` stacks every section on one page; a known section opens just that
265 + // one. With nothing requested (or something invalid), open the first
266 + // section rather than the stacked "All Settings" view.
267 + if ( 'all' === $requested || in_array( $requested, $cats, true ) ) {
268 + return $requested;
269 + }
270 +
271 + return $cats[0];
272 + }
273 +
274 + /**
62 275 * Enqueue WooCommerce admin styles for settings page.
276 + *
277 + * @param string $hook The current admin page hook.
63 278 */
64 279 public function enqueue_wc_admin_styles( $hook ) {
65 280 // Only load on our settings page
66 - if ( isset( $_GET['post_type'] ) && strpos( $_GET['post_type'], 'subscrpt_order' ) !== false ) {
281 + if ( isset( $_GET['post_type'] ) && strpos( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), 'subscrpt_order' ) !== false ) {
67 282 // WooCommerce admin styles
68 - wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), WC_VERSION );
283 + wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), SUBSCRPT_VERSION );
69 284 // Optional: WooCommerce enhanced select2
70 - wp_enqueue_style( 'woocommerce_admin_select2', WC()->plugin_url() . '/assets/css/select2.css', array(), WC_VERSION );
285 + wp_enqueue_style( 'woocommerce_admin_select2', WC()->plugin_url() . '/assets/css/select2.css', array(), SUBSCRPT_VERSION );
71 286 wp_enqueue_script( 'select2' );
72 287 }
73 288 }
74 289 }