| @@ -61,17 +61,17 @@ | ||
| 61 | 61 | // Setting fields. |
| 62 | 62 | $settings_fields = [ |
| 63 | 63 | [ |
| 64 | 64 | 'type' => 'heading', |
| 65 | - 'group' => 'main', | |
| 65 | + 'group' => 'renewals', | |
| 66 | 66 | 'priority' => 0, |
| 67 | 67 | 'field_data' => [ |
| 68 | - 'title' => __( 'General Settings', 'subscription' ), | |
| 68 | + 'title' => __( 'Renewals', 'subscription' ), | |
| 69 | 69 | ], |
| 70 | 70 | ], |
| 71 | 71 | [ |
| 72 | 72 | 'type' => 'select', |
| 73 | - 'group' => 'main', | |
| 73 | + 'group' => 'renewals', | |
| 74 | 74 | 'priority' => 1, |
| 75 | 75 | 'field_data' => [ |
| 76 | 76 | 'id' => 'wp_subscription_renewal_process', |
| 77 | 77 | 'title' => __( 'Renewal Process', 'subscription' ), |
| @@ -79,25 +79,25 @@ | ||
| 79 | 79 | 'options' => [ |
| 80 | 80 | 'auto' => __( 'Automatic', 'subscription' ), |
| 81 | 81 | 'manual' => __( 'Manual', 'subscription' ), |
| 82 | 82 | ], |
| 83 | - 'selected' => esc_attr( get_option( 'wp_subscription_renewal_process', 'auto' ) ), | |
| 83 | + 'selected' => esc_attr( subscrpt_get_renewal_process() ), | |
| 84 | 84 | ], |
| 85 | 85 | ], |
| 86 | 86 | [ |
| 87 | 87 | 'type' => 'input', |
| 88 | - 'group' => 'main', | |
| 88 | + 'group' => 'renewals', | |
| 89 | 89 | 'priority' => 2, |
| 90 | 90 | 'field_data' => [ |
| 91 | 91 | 'id' => 'wp_subscription_manual_renew_cart_notice', |
| 92 | 92 | 'title' => __( 'Renewal Cart Notice', 'subscription' ), |
| 93 | 93 | 'description' => __( 'Display Notice when Renewal Subscription product add to cart. Only available for Manual Renewal Process.', 'subscription' ), |
| 94 | - 'value' => esc_attr( get_option( 'wp_subscription_manual_renew_cart_notice' ) ), | |
| 94 | + 'value' => esc_attr( subscrpt_get_manual_renew_cart_notice() ), | |
| 95 | 95 | ], |
| 96 | 96 | ], |
| 97 | 97 | [ |
| 98 | 98 | 'type' => 'toggle', |
| 99 | - 'group' => 'main', | |
| 99 | + 'group' => 'renewals', | |
| 100 | 100 | 'priority' => 3, |
| 101 | 101 | 'field_data' => [ |
| 102 | 102 | 'id' => 'wp_subscription_stripe_auto_renew', |
| 103 | 103 | 'title' => __( 'Stripe Auto Renewal', 'subscription' ), |
| @@ -113,9 +113,9 @@ | ||
| 113 | 113 | ], |
| 114 | 114 | ], |
| 115 | 115 | [ |
| 116 | 116 | 'type' => 'toggle', |
| 117 | - 'group' => 'main', | |
| 117 | + 'group' => 'renewals', | |
| 118 | 118 | 'priority' => 4, |
| 119 | 119 | 'field_data' => [ |
| 120 | 120 | 'id' => 'wp_subscription_auto_renewal_toggle', |
| 121 | 121 | 'title' => __( 'Auto Renewal Toggle', 'subscription' ), |
| @@ -126,10 +126,10 @@ | ||
| 126 | 126 | ], |
| 127 | 127 | ], |
| 128 | 128 | [ |
| 129 | 129 | 'type' => 'select', |
| 130 | - 'group' => 'main', | |
| 131 | - 'priority' => 5, | |
| 130 | + 'group' => 'role_based_settings', | |
| 131 | + 'priority' => 2, | |
| 132 | 132 | 'field_data' => [ |
| 133 | 133 | 'id' => 'wp_subscription_active_role', |
| 134 | 134 | 'title' => __( 'Subscriber Default Role', 'subscription' ), |
| 135 | 135 | 'description' => __( 'When a subscription is activated, either manually or after a successful purchase, new users will be assigned this role.', 'subscription' ), |
| @@ -138,10 +138,10 @@ | ||
| 138 | 138 | ], |
| 139 | 139 | ], |
| 140 | 140 | [ |
| 141 | 141 | 'type' => 'select', |
| 142 | - 'group' => 'main', | |
| 143 | - 'priority' => 6, | |
| 142 | + 'group' => 'role_based_settings', | |
| 143 | + 'priority' => 3, | |
| 144 | 144 | 'field_data' => [ |
| 145 | 145 | 'id' => 'wp_subscription_unactive_role', |
| 146 | 146 | 'title' => __( 'Subscriber Inactive Role', 'subscription' ), |
| 147 | 147 | 'description' => __( "If a subscriber's subscription is manually cancelled or expires, they will be assigned this role.", 'subscription' ), |
| @@ -148,34 +148,8 @@ | ||
| 148 | 148 | 'options' => $roles, |
| 149 | 149 | 'selected' => esc_attr( get_option( 'wp_subscription_unactive_role', 'customer' ) ), |
| 150 | 150 | ], |
| 151 | 151 | ], |
| 152 | - [ | |
| 153 | - 'type' => 'toggle', | |
| 154 | - 'group' => 'main', | |
| 155 | - 'priority' => 9.5, | |
| 156 | - 'field_data' => [ | |
| 157 | - 'id' => 'subscrpt_cancellation_feedback_enabled', | |
| 158 | - 'title' => __( 'Cancellation Feedback', 'subscription' ), | |
| 159 | - 'label' => __( 'Ask customers why they are cancelling', 'subscription' ), | |
| 160 | - 'description' => __( 'Show a short feedback form when a customer cancels a subscription, and record the reason for churn tracking.', 'subscription' ), | |
| 161 | - 'value' => '1', | |
| 162 | - 'checked' => '1' === get_option( 'subscrpt_cancellation_feedback_enabled', '1' ), | |
| 163 | - ], | |
| 164 | - ], | |
| 165 | - [ | |
| 166 | - 'type' => 'toggle', | |
| 167 | - 'group' => 'main', | |
| 168 | - 'priority' => 9.6, | |
| 169 | - 'field_data' => [ | |
| 170 | - 'id' => 'subscrpt_cancellation_feedback_comment', | |
| 171 | - 'title' => __( 'Feedback Comment Box', 'subscription' ), | |
| 172 | - 'label' => __( 'Allow an additional comment', 'subscription' ), | |
| 173 | - 'description' => __( 'Show an optional free-text comment field in the cancellation feedback form.', 'subscription' ), | |
| 174 | - 'value' => '1', | |
| 175 | - 'checked' => '1' === get_option( 'subscrpt_cancellation_feedback_comment', '1' ), | |
| 176 | - ], | |
| 177 | - ], | |
| 178 | 152 | ]; |
| 179 | 153 | |
| 180 | 154 | // Allow other modules to add/modify settings fields. |
| 181 | 155 | $settings_fields = apply_filters( 'subscrpt_settings_fields', $settings_fields ); |
| @@ -238,24 +212,8 @@ | ||
| 238 | 212 | 'type' => 'string', |
| 239 | 213 | 'sanitize_callback' => 'sanitize_text_field', |
| 240 | 214 | ) |
| 241 | 215 | ); |
| 242 | - register_setting( | |
| 243 | - 'wp_subscription_settings', | |
| 244 | - 'subscrpt_cancellation_feedback_enabled', | |
| 245 | - array( | |
| 246 | - 'type' => 'string', | |
| 247 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 248 | - ) | |
| 249 | - ); | |
| 250 | - register_setting( | |
| 251 | - 'wp_subscription_settings', | |
| 252 | - 'subscrpt_cancellation_feedback_comment', | |
| 253 | - array( | |
| 254 | - 'type' => 'string', | |
| 255 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 256 | - ) | |
| 257 | - ); | |
| 258 | 216 | |
| 259 | 217 | do_action( 'subscrpt_register_settings', 'subscrpt_settings' ); |
| 260 | 218 | } |
| 261 | 219 | |
| @@ -263,8 +221,10 @@ | ||
| 263 | 221 | * Settings HTML. |
| 264 | 222 | */ |
| 265 | 223 | public function settings_content() { |
| 266 | 224 | $settings_fields = $this->settings_fields; |
| 225 | + $category_groups = SettingsHelper::category_groups( $settings_fields ); | |
| 226 | + $active_cat = $this->get_active_category( $category_groups ); | |
| 267 | 227 | |
| 268 | 228 | // Header. |
| 269 | 229 | $menu = new Menu(); |
| 270 | 230 | $menu->render_admin_header( __( 'Settings', 'subscription' ) ); |
| @@ -272,8 +232,44 @@ | ||
| 272 | 232 | include 'views/settings.php'; |
| 273 | 233 | |
| 274 | 234 | // Footer. |
| 275 | 235 | $menu->render_admin_footer(); |
| 236 | + } | |
| 237 | + | |
| 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]; | |
| 276 | 272 | } |
| 277 | 273 | |
| 278 | 274 | /** |
| 279 | 275 | * Enqueue WooCommerce admin styles for settings page. |