PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / trunk
Subscriptions for WooCommerce with Stripe Recurring Payments vtrunk
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 1.5.2 All 60 releases
subscription / includes / Admin / ProSettingsFields.php

ProSettingsFields.php in Subscriptions for WooCommerce with Stripe Recurring Payments trunk, at includes/Admin/ProSettingsFields.php

625 lines 20.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Pro Settings Fields — the single source of the Pro settings UI.
4 *
5 * Defines every Pro settings field (the UI only — no options are registered or
6 * saved here; the real settings are registered and processed by the Pro plugin).
7 * The fields are always added to the settings page via the
8 * `subscrpt_settings_fields` filter. When Pro is **not** active they render
9 * locked (disabled, with a "Pro" badge); when Pro is active they become
10 * interactive and the Pro plugin saves their values.
11 *
12 * The lock state is derived from `subscrpt_pro_activated()` and injected onto
13 * every field as `pro_locked`, which SettingsHelper uses for the badge and the
14 * non-interactive styling.
15 *
16 * @package SpringDevs\Subscription\Admin
17 */
18
19 namespace SpringDevs\Subscription\Admin;
20
21 // Exit if accessed directly.
22 defined( 'ABSPATH' ) || exit;
23
24 /**
25 * Class ProSettingsFields
26 *
27 * @package SpringDevs\Subscription\Admin
28 */
29 class ProSettingsFields {
30
31 /**
32 * Initialize the class.
33 */
34 public function __construct() {
35 add_filter( 'subscrpt_settings_fields', [ $this, 'add_pro_preview_fields' ] );
36 }
37
38 /**
39 * Merge the Pro settings fields into the settings fields list.
40 *
41 * Fields are locked (disabled + Pro badge) only while Pro is inactive.
42 *
43 * @param array $settings_fields Existing settings fields.
44 * @return array
45 */
46 public function add_pro_preview_fields( $settings_fields ) {
47 $pro_fields = array_merge(
48 $this->pro_core_fields(),
49 $this->grace_period_fields(),
50 $this->payment_failure_fields(),
51 $this->api_fields(),
52 $this->health_queue_fields(),
53 $this->order_fields(),
54 $this->switch_fields(),
55 $this->role_management_fields(),
56 $this->live_qr_fields(),
57 $this->payment_gateway_fields()
58 );
59
60 // Lock the fields only when Pro is not active.
61 $locked = ! subscrpt_pro_activated();
62 foreach ( $pro_fields as &$field ) {
63 $field['field_data']['pro_locked'] = $locked;
64 if ( $locked ) {
65 $field['field_data']['disabled'] = true;
66 }
67 }
68 unset( $field );
69
70 return array_merge( $settings_fields, $pro_fields );
71 }
72
73 /**
74 * Module: Pro Core (Admin/Settings.php) — general settings additions.
75 *
76 * @return array
77 */
78 private function pro_core_fields() {
79 return [
80 [
81 'type' => 'select',
82 'group' => 'main',
83 'priority' => 7,
84 'field_data' => [
85 'id' => 'subscrpt_renewal_price',
86 'title' => __( 'Renewal Price', 'subscription' ),
87 'description' => __( 'Choose a price that will be used for subscription renewal.', 'subscription' ),
88 'options' => [
89 'subscribed' => __( 'Subscribed Price', 'subscription' ),
90 'updated' => __( 'New/Updated Price', 'subscription' ),
91 ],
92 'selected' => esc_attr( get_option( 'subscrpt_renewal_price', 'subscribed' ) ),
93 ],
94 ],
95 [
96 'type' => 'toggle',
97 'group' => 'main',
98 'priority' => 8,
99 'field_data' => [
100 'id' => 'subscrpt_early_renew',
101 'title' => __( 'Early Renewal', 'subscription' ),
102 'label' => __( 'Accept Early Renewal Payments', 'subscription' ),
103 'description' => __( 'With early renewals enabled, customers can renew their subscriptions before the next payment date.', 'subscription' ),
104 'value' => '1',
105 'checked' => '1' === get_option( 'subscrpt_early_renew', '1' ),
106 ],
107 ],
108 [
109 'type' => 'select',
110 'group' => 'main',
111 'priority' => 9,
112 'field_data' => [
113 'id' => 'subscrpt_cancellation_delay',
114 'title' => __( 'Cancellation Timing', 'subscription' ),
115 'description' => __( 'When a subscription is cancelled, choose when it actually ends.', 'subscription' ),
116 'options' => [
117 '24h' => __( 'After 24 hours', 'subscription' ),
118 'instant' => __( 'Immediately', 'subscription' ),
119 'period' => __( 'At end of billing period (before next renewal)', 'subscription' ),
120 ],
121 'selected' => esc_attr( \SpringDevs\Subscription\Illuminate\Cancellation::get_settings( 'subscrpt_cancellation_delay' ) ),
122 ],
123 ],
124 [
125 'type' => 'editlist',
126 'group' => 'main',
127 'priority' => 9.6,
128 'field_data' => [
129 'id' => 'subscrpt_cancellation_reasons',
130 'title' => __( 'Cancellation Reasons', 'subscription' ),
131 'description' => __( 'Reasons offered in the cancellation survey form. Shown when Cancellation Survey is enabled.', 'subscription' ),
132 'value' => \SpringDevs\Subscription\Illuminate\Cancellation::get_reasons(),
133 'modal' => true,
134 'button_label' => __( 'Manage reasons', 'subscription' ),
135 'modal_title' => __( 'Cancellation Reasons', 'subscription' ),
136 'add_placeholder' => __( 'Add a reason…', 'subscription' ),
137 'add_label' => __( 'Add reason', 'subscription' ),
138 'empty_text' => __( 'No reasons yet. Add one below.', 'subscription' ),
139 ],
140 ],
141 ];
142 }
143
144 /**
145 * Module: Grace Period (Admin/Settings.php).
146 *
147 * @return array
148 */
149 private function grace_period_fields() {
150 return [
151 [
152 'type' => 'heading',
153 'group' => 'grace_period',
154 'priority' => 3,
155 'field_data' => [
156 'title' => __( 'Grace Period Settings', 'subscription' ),
157 ],
158 ],
159 [
160 'type' => 'input',
161 'group' => 'grace_period',
162 'priority' => 1,
163 'field_data' => [
164 'id' => 'subscrpt_default_payment_grace_period',
165 'title' => __( 'Grace Period (Days)', 'subscription' ),
166 'description' => __( 'Days to maintain access after subscriptions expires. (0 = No grace period. Max 30 days)', 'subscription' ),
167 'value' => esc_attr( get_option( 'subscrpt_default_payment_grace_period', '7' ) ),
168 'type' => 'number',
169 'attributes' => [
170 'min' => 0,
171 'max' => 30,
172 ],
173 ],
174 ],
175 [
176 'type' => 'toggle',
177 'group' => 'grace_period',
178 'priority' => 2,
179 'field_data' => [
180 'id' => 'subscrpt_enable_grace_period_notifications',
181 'title' => __( 'Grace Period Notifications', 'subscription' ),
182 'label' => __( 'Send notifications during grace period', 'subscription' ),
183 'description' => __( 'Customers will receive warnings before their access is suspended.', 'subscription' ),
184 'value' => '1',
185 'checked' => '1' === get_option( 'subscrpt_enable_grace_period_notifications', '1' ),
186 ],
187 ],
188 [
189 'type' => 'input',
190 'group' => 'grace_period',
191 'priority' => 3,
192 'field_data' => [
193 'id' => 'subscrpt_grace_period_warning_days',
194 'title' => __( 'Grace Period Warning (Days Before)', 'subscription' ),
195 'description' => __( 'Send warning emails this many days before grace period expires. (1-7 days)', 'subscription' ),
196 'value' => esc_attr( get_option( 'subscrpt_grace_period_warning_days', '2' ) ),
197 'type' => 'number',
198 'attributes' => [
199 'min' => 1,
200 'max' => 7,
201 ],
202 ],
203 ],
204 ];
205 }
206
207 /**
208 * Module: Payment Failure Handling (Admin/Settings.php).
209 *
210 * @return array
211 */
212 private function payment_failure_fields() {
213 return [
214 [
215 'type' => 'heading',
216 'group' => 'payment_failure',
217 'priority' => 4,
218 'field_data' => [
219 'title' => __( 'Payment Failure Handling', 'subscription' ),
220 ],
221 ],
222 [
223 'type' => 'input',
224 'group' => 'payment_failure',
225 'priority' => 1,
226 'field_data' => [
227 'id' => 'subscrpt_default_max_payment_retries',
228 'title' => __( 'Default Max Payment Retries', 'subscription' ),
229 'description' => __( 'Default number of automatic retry attempts for failed payments. (0 = No retries. Max 10 retries)', 'subscription' ),
230 'value' => esc_attr( get_option( 'subscrpt_default_max_payment_retries', '3' ) ),
231 'type' => 'number',
232 'attributes' => [
233 'min' => 0,
234 'max' => 10,
235 ],
236 ],
237 ],
238 [
239 'type' => 'toggle',
240 'group' => 'payment_failure',
241 'priority' => 2,
242 'field_data' => [
243 'id' => 'subscrpt_enable_payment_failure_emails',
244 'title' => __( 'Enable Payment Failure Emails', 'subscription' ),
245 'label' => __( 'Send email notifications when payments fail', 'subscription' ),
246 'description' => __( 'Customers will receive emails about failed payments and retry attempts.', 'subscription' ),
247 'value' => '1',
248 'checked' => '1' === get_option( 'subscrpt_enable_payment_failure_emails', '1' ),
249 ],
250 ],
251 [
252 'type' => 'input',
253 'group' => 'payment_failure',
254 'priority' => 3,
255 'field_data' => [
256 'id' => 'subscrpt_payment_failure_email_delay',
257 'title' => __( 'Payment Failure Email Delay (Hours)', 'subscription' ),
258 'description' => __( 'Delay before sending payment failure emails to avoid spam during temporary issues. (0-168 hours)', 'subscription' ),
259 'value' => esc_attr( get_option( 'subscrpt_payment_failure_email_delay', '24' ) ),
260 'type' => 'number',
261 'attributes' => [
262 'min' => 0,
263 'max' => 168,
264 ],
265 ],
266 ],
267 ];
268 }
269
270 /**
271 * Module: API Settings (Admin/Settings.php).
272 *
273 * @return array
274 */
275 private function api_fields() {
276 return [
277 [
278 'type' => 'heading',
279 'group' => 'api_settings',
280 'priority' => 99,
281 'field_data' => [
282 'title' => __( 'API Settings', 'subscription' ),
283 ],
284 ],
285 [
286 'type' => 'toggle',
287 'group' => 'api_settings',
288 'priority' => 1,
289 'field_data' => [
290 'id' => 'wpsubscription_api_enabled',
291 'title' => __( 'Enable API', 'subscription' ),
292 'description' => __( 'Enable REST API endpoints for subscription actions', 'subscription' ),
293 'value' => 'on',
294 'checked' => 'on' === get_option( 'wpsubscription_api_enabled', 'on' ),
295 ],
296 ],
297 [
298 'type' => 'join',
299 'group' => 'api_settings',
300 'priority' => 2,
301 'field_data' => [
302 'title' => __( 'API Key', 'subscription' ),
303 'description' => __( 'API key for external integrations. Keep this secure.', 'subscription' ),
304 'elements' => [
305 SettingsHelper::inp_element(
306 [
307 'id' => 'wpsubscription_api_key',
308 'value' => esc_attr( get_option( 'wpsubscription_api_key', '' ) ),
309 'type' => 'text',
310 'style' => 'min-width:366px;',
311 'attributes' => [
312 'readonly' => true,
313 ],
314 ],
315 true
316 ),
317 '<span class="wpsubs-tooltip" data-tip="' . esc_attr__( 'Regenerate API Key', 'subscription' ) . '">'
318 . '<button type="button" class="wpsubs-btn wpsubs-btn--outline wpsubs-btn--icon" id="regenerate_api_key" aria-label="' . esc_attr__( 'Regenerate API Key', 'subscription' ) . '">'
319 . '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/></svg>'
320 . '</button>'
321 . '</span>',
322 ],
323 ],
324 ],
325 [
326 'type' => 'input',
327 'group' => 'api_settings',
328 'priority' => 3,
329 'field_data' => [
330 'id' => 'subscrpt_api_endpoint_url',
331 'title' => __( 'REST API Endpoint URL', 'subscription' ),
332 'description' => __( 'Copy and use this endpoint for all API actions.', 'subscription' ),
333 'value' => esc_url( home_url( '/wp-json/wpsubscription/v1/action' ) ),
334 'type' => 'text',
335 'attributes' => [
336 'readonly' => true,
337 ],
338 ],
339 ],
340 ];
341 }
342
343 /**
344 * Module: Subscription Health (Admin/Settings.php).
345 *
346 * @return array
347 */
348 private function health_queue_fields() {
349 return [
350 [
351 'type' => 'heading',
352 'group' => 'health_queue',
353 'priority' => 8,
354 'field_data' => [
355 'title' => __( 'Subscription Health', 'subscription' ),
356 ],
357 ],
358 [
359 'type' => 'select',
360 'group' => 'health_queue',
361 'priority' => 2,
362 'field_data' => [
363 'id' => 'subscrpt_health_queue_email_frequency',
364 'title' => __( 'Health Queue Email Frequency', 'subscription' ),
365 'description' => __( 'How often to send the admin health queue reminder email. The email is only sent when subscriptions need attention.', 'subscription' ),
366 'options' => [
367 'daily' => __( 'Daily', 'subscription' ),
368 'subscrpt_weekly' => __( 'Weekly', 'subscription' ),
369 'subscrpt_monthly' => __( 'Monthly', 'subscription' ),
370 'never' => __( 'Never', 'subscription' ),
371 ],
372 'selected' => esc_attr( get_option( 'subscrpt_health_queue_email_frequency', 'subscrpt_weekly' ) ),
373 ],
374 ],
375 ];
376 }
377
378 /**
379 * Module: Order (Illuminate/Order.php) — auto complete orders.
380 *
381 * @return array
382 */
383 private function order_fields() {
384 return [
385 [
386 'type' => 'toggle',
387 'group' => 'payment_gateways',
388 'priority' => 1,
389 'field_data' => [
390 'id' => 'wp_subscription_auto_complete_order',
391 'title' => __( 'Auto Complete Orders', 'subscription' ),
392 'description' => __( 'Automatically change status of processing orders to completed.', 'subscription' ),
393 'value' => '1',
394 'checked' => '1' === get_option( 'wp_subscription_auto_complete_order', '1' ),
395 ],
396 ],
397 ];
398 }
399
400 /**
401 * Module: Switch Subscription (Illuminate/SubscriptionSwitch.php).
402 *
403 * @return array
404 */
405 private function switch_fields() {
406 return [
407 [
408 'type' => 'toggle',
409 'group' => 'general',
410 'priority' => 9,
411 'field_data' => [
412 'id' => 'subscrpt_switch_enabled',
413 'title' => __( 'Switch Subscription', 'subscription' ),
414 'label' => __( 'Allow users to upgrade/downgrade their subscriptions.', 'subscription' ),
415 'description' => '',
416 'value' => '1',
417 'checked' => '1' === get_option( 'subscrpt_switch_enabled', '0' ),
418 ],
419 ],
420 [
421 'type' => 'toggle',
422 'group' => 'general',
423 'priority' => 10,
424 'field_data' => [
425 'id' => 'subscrpt_downgrade_allowed',
426 'title' => __( 'Downgrade Subscription', 'subscription' ),
427 'label' => __( 'Allow users to downgrade their subscriptions.', 'subscription' ),
428 'description' => '',
429 'value' => '1',
430 'checked' => '1' === get_option( 'subscrpt_downgrade_allowed', '1' ),
431 ],
432 ],
433 [
434 'type' => 'join',
435 'group' => 'general',
436 'priority' => 11,
437 'field_data' => [
438 'title' => __( 'Switch Fee', 'subscription' ),
439 'description' => __( 'Optional fee charged when a customer switches plans. Use a flat amount or a percentage of the new plan price. (0 = no fee)', 'subscription' ),
440 'elements' => [
441 SettingsHelper::inp_element(
442 [
443 'id' => 'subscrpt_switch_fee_amount',
444 'value' => esc_attr( get_option( 'subscrpt_switch_fee_amount', '0' ) ),
445 'type' => 'number',
446 'style' => 'min-width:170px;width:170px;',
447 'attributes' => [
448 'min' => 0,
449 'step' => '0.01',
450 ],
451 ],
452 true
453 ),
454 SettingsHelper::select_element(
455 [
456 'id' => 'subscrpt_switch_fee_type',
457 'class' => 'subscrpt-switch-fee-type',
458 'options' => [
459 'flat' => sprintf(
460 /* translators: %s: store currency symbol. */
461 __( 'Flat (%s)', 'subscription' ),
462 get_woocommerce_currency_symbol()
463 ),
464 'percent' => __( 'Percentage (%)', 'subscription' ),
465 ],
466 'selected' => esc_attr( get_option( 'subscrpt_switch_fee_type', 'flat' ) ),
467 ],
468 true
469 ),
470 ],
471 ],
472 ],
473 [
474 'type' => 'select',
475 'group' => 'general',
476 'priority' => 12,
477 'field_data' => [
478 'id' => 'subscrpt_switch_fee_apply_to',
479 'title' => __( 'Apply Fee To', 'subscription' ),
480 'description' => __( 'Choose which switch directions the fee applies to.', 'subscription' ),
481 'options' => [
482 'both' => __( 'Both upgrade and downgrade', 'subscription' ),
483 'upgrade' => __( 'Upgrade only', 'subscription' ),
484 'downgrade' => __( 'Downgrade only', 'subscription' ),
485 ],
486 'selected' => esc_attr( get_option( 'subscrpt_switch_fee_apply_to', 'both' ) ),
487 ],
488 ],
489 ];
490 }
491
492 /**
493 * Module: Role Management (Illuminate/RoleManagement.php).
494 *
495 * @return array
496 */
497 private function role_management_fields() {
498 return [
499 [
500 'type' => 'heading',
501 'group' => 'role_based_settings',
502 'priority' => 6,
503 'field_data' => [
504 'title' => __( 'Role-Based Settings', 'subscription' ),
505 ],
506 ],
507 [
508 'type' => 'toggle',
509 'group' => 'role_based_settings',
510 'priority' => 1,
511 'field_data' => [
512 'id' => 'subscrpt_role_based_access',
513 'title' => __( 'Enable Role Based Access', 'subscription' ),
514 'description' => __( 'Show/Hide products based on user roles.', 'subscription' ),
515 'value' => '1',
516 'checked' => '1' === get_option( 'subscrpt_role_based_access', '1' ),
517 ],
518 ],
519 ];
520 }
521
522 /**
523 * Module: Live QR (Illuminate/LiveQR/LiveQR.php) — quick details QR.
524 *
525 * @return array
526 */
527 private function live_qr_fields() {
528 return [
529 [
530 'type' => 'heading',
531 'group' => 'live_qr_settings',
532 'priority' => 5,
533 'field_data' => [
534 'title' => __( 'Quick Details QR Settings', 'subscription' ),
535 ],
536 ],
537 [
538 'type' => 'toggle',
539 'group' => 'live_qr_settings',
540 'priority' => 1,
541 'field_data' => [
542 'id' => 'subscrpt_live_qr_active',
543 'title' => __( 'Enable Quick Details QR', 'subscription' ),
544 'label' => '',
545 'value' => '1',
546 'checked' => '1' === get_option( 'subscrpt_live_qr_active', '1' ),
547 ],
548 ],
549 [
550 'type' => 'toggle',
551 'group' => 'live_qr_settings',
552 'priority' => 2,
553 'field_data' => [
554 'id' => 'subscrpt_live_qr_show_product',
555 'title' => __( 'Product Details', 'subscription' ),
556 'label' => __( 'Show product details in the subscription quick view', 'subscription' ),
557 'value' => '1',
558 'checked' => '1' === get_option( 'subscrpt_live_qr_show_product', '1' ),
559 ],
560 ],
561 [
562 'type' => 'toggle',
563 'group' => 'live_qr_settings',
564 'priority' => 3,
565 'field_data' => [
566 'id' => 'subscrpt_live_qr_show_billing',
567 'title' => __( 'Billing Details', 'subscription' ),
568 'label' => __( 'Show billing details in the subscription quick view', 'subscription' ),
569 'value' => '1',
570 'checked' => '1' === get_option( 'subscrpt_live_qr_show_billing', '0' ),
571 ],
572 ],
573 [
574 'type' => 'toggle',
575 'group' => 'live_qr_settings',
576 'priority' => 4,
577 'field_data' => [
578 'id' => 'subscrpt_live_qr_show_timeline',
579 'title' => __( 'Timeline', 'subscription' ),
580 'label' => __( 'Show subscription timeline in the subscription quick view', 'subscription' ),
581 'value' => '1',
582 'checked' => '1' === get_option( 'subscrpt_live_qr_show_timeline', '0' ),
583 ],
584 ],
585 ];
586 }
587
588 /**
589 * Module: Payment Gateways (Illuminate/Gateways/PaymentGateways.php).
590 *
591 * @return array
592 */
593 private function payment_gateway_fields() {
594 $gateway_options = [];
595 if ( function_exists( 'WC' ) && WC()->payment_gateways ) {
596 foreach ( WC()->payment_gateways->get_available_payment_gateways() as $gateway_id => $gateway ) {
597 $gateway_options[ $gateway_id ] = $gateway->get_title();
598 }
599 }
600
601 return [
602 [
603 'type' => 'heading',
604 'group' => 'payment_gateways',
605 'priority' => 0.1,
606 'field_data' => [
607 'title' => __( 'Payment Gateway Settings', 'subscription' ),
608 ],
609 ],
610 [
611 'type' => 'multi_select',
612 'group' => 'payment_gateways',
613 'priority' => 2,
614 'field_data' => [
615 'id' => 'hidden_payment_gateways',
616 'title' => __( 'Hide Payment Gateways', 'subscription' ),
617 'description' => __( 'Select payment gateways to hide for subscription products. (keep empty for not hiding any)', 'subscription' ),
618 'options' => $gateway_options,
619 'selected' => get_option( 'hidden_payment_gateways', [] ),
620 ],
621 ],
622 ];
623 }
624 }
625