| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Ajax; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Settings |
| 9 |
* |
| 10 |
* @package SpringDevs\Subscription\Admin |
| 11 |
*/ |
| 12 |
class Settings { |
| 13 |
/** |
| 14 |
* Settings fields. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
public $settings_fields = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize the class. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
// Load the settings helper. |
| 25 |
SettingsHelper::get_instance(); |
| 26 |
|
| 27 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 28 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 29 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wc_admin_styles' ) ); |
| 30 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_installer_script' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register submenu on `Subscriptions` menu. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function admin_menu() { |
| 39 |
// Initialize & process settings fields. |
| 40 |
$this->initiate_settings_fields(); |
| 41 |
|
| 42 |
// Add submenu page. |
| 43 |
$parent_slug = 'wp-subscription'; |
| 44 |
add_submenu_page( |
| 45 |
$parent_slug, |
| 46 |
__( 'Settings', 'subscription' ), |
| 47 |
__( 'Settings', 'subscription' ), |
| 48 |
'manage_options', |
| 49 |
'wp-subscription-settings', |
| 50 |
[ $this, 'settings_content' ] |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialize settings fields. |
| 56 |
*/ |
| 57 |
public function initiate_settings_fields() { |
| 58 |
global $wp_roles; |
| 59 |
$roles = []; |
| 60 |
foreach ( ( $wp_roles->roles ?? [] ) as $role_key => $role ) { |
| 61 |
$roles[ $role_key ] = $role['name']; |
| 62 |
} |
| 63 |
|
| 64 |
// Setting fields. |
| 65 |
$settings_fields = [ |
| 66 |
[ |
| 67 |
'type' => 'heading', |
| 68 |
'group' => 'renewals', |
| 69 |
'priority' => 0, |
| 70 |
'field_data' => [ |
| 71 |
'title' => __( 'Renewals', 'subscription' ), |
| 72 |
], |
| 73 |
], |
| 74 |
[ |
| 75 |
'type' => 'select', |
| 76 |
'group' => 'renewals', |
| 77 |
'priority' => 1, |
| 78 |
'field_data' => [ |
| 79 |
'id' => 'wp_subscription_renewal_process', |
| 80 |
'title' => __( 'Renewal Process', 'subscription' ), |
| 81 |
'description' => __( 'How renewal process will be done after Subscription Expired.', 'subscription' ), |
| 82 |
'options' => [ |
| 83 |
'auto' => __( 'Automatic', 'subscription' ), |
| 84 |
'manual' => __( 'Manual', 'subscription' ), |
| 85 |
], |
| 86 |
'selected' => esc_attr( subscrpt_get_renewal_process() ), |
| 87 |
], |
| 88 |
], |
| 89 |
[ |
| 90 |
'type' => 'input', |
| 91 |
'group' => 'renewals', |
| 92 |
'priority' => 2, |
| 93 |
'field_data' => [ |
| 94 |
'id' => 'wp_subscription_manual_renew_cart_notice', |
| 95 |
'title' => __( 'Renewal Cart Notice', 'subscription' ), |
| 96 |
'description' => __( 'Display Notice when Renewal Subscription product add to cart. Only available for Manual Renewal Process.', 'subscription' ), |
| 97 |
'value' => esc_attr( subscrpt_get_manual_renew_cart_notice() ), |
| 98 |
], |
| 99 |
], |
| 100 |
[ |
| 101 |
'type' => 'toggle', |
| 102 |
'group' => 'renewals', |
| 103 |
'priority' => 3, |
| 104 |
'field_data' => [ |
| 105 |
'id' => 'wp_subscription_stripe_auto_renew', |
| 106 |
'title' => __( 'Stripe Auto Renewal', 'subscription' ), |
| 107 |
'label' => __( 'Accept Stripe Auto Renewals', 'subscription' ), |
| 108 |
'description' => class_exists( 'WC_Stripe' ) |
| 109 |
? sprintf( |
| 110 |
/* translators: 1: opening plugin link tag, 2: closing link tag, 3: opening settings link tag */ |
| 111 |
__( '✓ %1$sWooCommerce Stripe Payment Gateway%2$s is active. %3$sConfigure%2$s', 'subscription' ), |
| 112 |
'<a href="https://wordpress.org/plugins/woocommerce-gateway-stripe/" target="_blank">', |
| 113 |
'</a>', |
| 114 |
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe' ) ) . '">' |
| 115 |
) |
| 116 |
: sprintf( |
| 117 |
/* translators: HTML tags */ |
| 118 |
__( '%1$s WooCommerce Stripe Payment Gateway %2$s plugin is required!', 'subscription' ), |
| 119 |
'<a href="https://wordpress.org/plugins/woocommerce-gateway-stripe/" target="_blank">', |
| 120 |
'</a>' |
| 121 |
) . $this->get_stripe_plugin_action(), |
| 122 |
'value' => '1', |
| 123 |
'checked' => '1' === get_option( 'wp_subscription_stripe_auto_renew', '1' ), |
| 124 |
], |
| 125 |
], |
| 126 |
[ |
| 127 |
'type' => 'toggle', |
| 128 |
'group' => 'renewals', |
| 129 |
'priority' => 4, |
| 130 |
'field_data' => [ |
| 131 |
'id' => 'wp_subscription_auto_renewal_toggle', |
| 132 |
'title' => __( 'Auto Renewal Toggle', 'subscription' ), |
| 133 |
'label' => __( 'Display the auto renewal toggle', 'subscription' ), |
| 134 |
'description' => __( 'Allow customers to turn on and off automatic renewals from their Subscription details page', 'subscription' ), |
| 135 |
'value' => '1', |
| 136 |
'checked' => '1' === get_option( 'wp_subscription_auto_renewal_toggle', '1' ), |
| 137 |
], |
| 138 |
], |
| 139 |
[ |
| 140 |
'type' => 'heading', |
| 141 |
'group' => 'stock_management', |
| 142 |
'priority' => 0, |
| 143 |
'field_data' => [ |
| 144 |
'title' => __( 'Stock Management', 'subscription' ), |
| 145 |
], |
| 146 |
], |
| 147 |
[ |
| 148 |
'type' => 'toggle', |
| 149 |
'group' => 'stock_management', |
| 150 |
'priority' => 1, |
| 151 |
'field_data' => [ |
| 152 |
'id' => 'wp_subscription_stock_pause', |
| 153 |
'title' => __( 'Pause on Low Stock', 'subscription' ), |
| 154 |
'label' => __( 'Pause subscriptions when the product runs low on stock', 'subscription' ), |
| 155 |
'description' => __( 'Active subscriptions of a product are put on hold when its stock reaches the threshold, and the customer is emailed, so no renewal is charged for something you cannot ship.', 'subscription' ), |
| 156 |
'value' => '1', |
| 157 |
'checked' => '1' === get_option( 'wp_subscription_stock_pause', '' ), |
| 158 |
], |
| 159 |
], |
| 160 |
[ |
| 161 |
'type' => 'input', |
| 162 |
'group' => 'stock_management', |
| 163 |
'priority' => 2, |
| 164 |
'field_data' => [ |
| 165 |
'id' => 'wp_subscription_stock_pause_threshold', |
| 166 |
'title' => __( 'Stock Threshold', 'subscription' ), |
| 167 |
'description' => __( 'Pause when the stock quantity is at or below this number. Products that do not manage a quantity pause only when out of stock.', 'subscription' ), |
| 168 |
'type' => 'number', |
| 169 |
'value' => esc_attr( get_option( 'wp_subscription_stock_pause_threshold', 0 ) ), |
| 170 |
'attributes' => [ |
| 171 |
'min' => 0, |
| 172 |
'step' => 1, |
| 173 |
], |
| 174 |
], |
| 175 |
], |
| 176 |
[ |
| 177 |
'type' => 'toggle', |
| 178 |
'group' => 'stock_management', |
| 179 |
'priority' => 3, |
| 180 |
'field_data' => [ |
| 181 |
'id' => 'wp_subscription_stock_auto_resume', |
| 182 |
'title' => __( 'Auto Resume', 'subscription' ), |
| 183 |
'label' => __( 'Resume paused subscriptions when stock recovers', 'subscription' ), |
| 184 |
'description' => __( 'Only subscriptions paused because of stock are resumed; ones you put on hold yourself stay on hold.', 'subscription' ), |
| 185 |
'value' => '1', |
| 186 |
'checked' => '1' === get_option( 'wp_subscription_stock_auto_resume', '1' ), |
| 187 |
], |
| 188 |
], |
| 189 |
[ |
| 190 |
'type' => 'select', |
| 191 |
'group' => 'role_based_settings', |
| 192 |
'priority' => 2, |
| 193 |
'field_data' => [ |
| 194 |
'id' => 'wp_subscription_active_role', |
| 195 |
'title' => __( 'Subscriber Default Role', 'subscription' ), |
| 196 |
'description' => __( 'When a subscription is activated, either manually or after a successful purchase, new users will be assigned this role.', 'subscription' ), |
| 197 |
'options' => $roles, |
| 198 |
'selected' => esc_attr( get_option( 'wp_subscription_active_role', 'subscriber' ) ), |
| 199 |
], |
| 200 |
], |
| 201 |
[ |
| 202 |
'type' => 'select', |
| 203 |
'group' => 'role_based_settings', |
| 204 |
'priority' => 3, |
| 205 |
'field_data' => [ |
| 206 |
'id' => 'wp_subscription_unactive_role', |
| 207 |
'title' => __( 'Subscriber Inactive Role', 'subscription' ), |
| 208 |
'description' => __( "If a subscriber's subscription is manually cancelled or expires, they will be assigned this role.", 'subscription' ), |
| 209 |
'options' => $roles, |
| 210 |
'selected' => esc_attr( get_option( 'wp_subscription_unactive_role', 'customer' ) ), |
| 211 |
], |
| 212 |
], |
| 213 |
]; |
| 214 |
|
| 215 |
// Allow other modules to add/modify settings fields. |
| 216 |
$settings_fields = apply_filters( 'subscrpt_settings_fields', $settings_fields ); |
| 217 |
|
| 218 |
// Process settings fields (group & sort). |
| 219 |
$settings_fields = apply_filters( 'process_subscrpt_settings_fields', $settings_fields ); |
| 220 |
|
| 221 |
// Set the settings fields. |
| 222 |
$this->settings_fields = $settings_fields; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Install / Activate link for the Stripe gateway plugin. |
| 227 |
* |
| 228 |
* Rendered inside a field description, which goes through `wp_kses_post()`, |
| 229 |
* so the link carries data attributes rather than an inline handler; |
| 230 |
* `integration_settings.js` picks it up. |
| 231 |
* |
| 232 |
* @return string Link HTML, or an empty string when the user cannot act on it. |
| 233 |
*/ |
| 234 |
private function get_stripe_plugin_action() { |
| 235 |
$slug = 'woocommerce-gateway-stripe'; |
| 236 |
$installed = '' !== Ajax::get_installed_plugin_file( $slug ); |
| 237 |
|
| 238 |
if ( $installed && current_user_can( 'activate_plugins' ) ) { |
| 239 |
$label = __( 'Activate', 'subscription' ); |
| 240 |
$busy_label = __( 'Activating…', 'subscription' ); |
| 241 |
} elseif ( ! $installed && current_user_can( 'install_plugins' ) ) { |
| 242 |
$label = __( 'Install & Activate', 'subscription' ); |
| 243 |
$busy_label = __( 'Installing…', 'subscription' ); |
| 244 |
} else { |
| 245 |
return ''; |
| 246 |
} |
| 247 |
|
| 248 |
return sprintf( |
| 249 |
' <a href="#" data-subscrpt-install-plugin="%1$s" data-busy-label="%2$s">%3$s</a>', |
| 250 |
esc_attr( $slug ), |
| 251 |
esc_attr( $busy_label ), |
| 252 |
esc_html( $label ) |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Enqueue the plugin installer on the settings page. |
| 258 |
* |
| 259 |
* @param string $hook The current admin page hook. |
| 260 |
*/ |
| 261 |
public function enqueue_installer_script( $hook ) { |
| 262 |
if ( false === strpos( $hook, 'wp-subscription-settings' ) ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
wp_enqueue_script( 'subscrpt-integrations', SUBSCRPT_ASSETS . '/js/integration_settings.js', [], SUBSCRPT_VERSION, true ); |
| 267 |
|
| 268 |
wp_localize_script( |
| 269 |
'subscrpt-integrations', |
| 270 |
'subscrptIntegrations', |
| 271 |
array( |
| 272 |
'nonce' => wp_create_nonce( 'subscrpt_integration_install_nonce' ), |
| 273 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 274 |
) |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Register settings options. |
| 280 |
**/ |
| 281 |
public function register_settings() { |
| 282 |
register_setting( |
| 283 |
'wp_subscription_settings', |
| 284 |
'wp_subscription_renewal_process', |
| 285 |
array( |
| 286 |
'type' => 'string', |
| 287 |
'sanitize_callback' => 'sanitize_text_field', |
| 288 |
) |
| 289 |
); |
| 290 |
register_setting( |
| 291 |
'wp_subscription_settings', |
| 292 |
'wp_subscription_manual_renew_cart_notice', |
| 293 |
array( |
| 294 |
'type' => 'string', |
| 295 |
'sanitize_callback' => 'sanitize_textarea_field', |
| 296 |
) |
| 297 |
); |
| 298 |
register_setting( |
| 299 |
'wp_subscription_settings', |
| 300 |
'wp_subscription_active_role', |
| 301 |
array( |
| 302 |
'type' => 'string', |
| 303 |
'sanitize_callback' => 'sanitize_text_field', |
| 304 |
) |
| 305 |
); |
| 306 |
register_setting( |
| 307 |
'wp_subscription_settings', |
| 308 |
'wp_subscription_unactive_role', |
| 309 |
array( |
| 310 |
'type' => 'string', |
| 311 |
'sanitize_callback' => 'sanitize_text_field', |
| 312 |
) |
| 313 |
); |
| 314 |
register_setting( |
| 315 |
'wp_subscription_settings', |
| 316 |
'wp_subscription_stripe_auto_renew', |
| 317 |
array( |
| 318 |
'type' => 'string', |
| 319 |
'sanitize_callback' => 'sanitize_text_field', |
| 320 |
) |
| 321 |
); |
| 322 |
register_setting( |
| 323 |
'wp_subscription_settings', |
| 324 |
'wp_subscription_stock_pause', |
| 325 |
array( |
| 326 |
'type' => 'string', |
| 327 |
'sanitize_callback' => 'sanitize_text_field', |
| 328 |
) |
| 329 |
); |
| 330 |
register_setting( |
| 331 |
'wp_subscription_settings', |
| 332 |
'wp_subscription_stock_pause_threshold', |
| 333 |
array( |
| 334 |
'type' => 'integer', |
| 335 |
'sanitize_callback' => 'absint', |
| 336 |
) |
| 337 |
); |
| 338 |
register_setting( |
| 339 |
'wp_subscription_settings', |
| 340 |
'wp_subscription_stock_auto_resume', |
| 341 |
array( |
| 342 |
'type' => 'string', |
| 343 |
'sanitize_callback' => 'sanitize_text_field', |
| 344 |
) |
| 345 |
); |
| 346 |
register_setting( |
| 347 |
'wp_subscription_settings', |
| 348 |
'wp_subscription_auto_renewal_toggle', |
| 349 |
array( |
| 350 |
'type' => 'string', |
| 351 |
'sanitize_callback' => 'sanitize_text_field', |
| 352 |
) |
| 353 |
); |
| 354 |
|
| 355 |
do_action( 'subscrpt_register_settings', 'subscrpt_settings' ); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Settings HTML. |
| 360 |
*/ |
| 361 |
public function settings_content() { |
| 362 |
$settings_fields = $this->settings_fields; |
| 363 |
$category_groups = SettingsHelper::category_groups( $settings_fields ); |
| 364 |
$active_cat = $this->get_active_category( $category_groups ); |
| 365 |
|
| 366 |
// Header. |
| 367 |
$menu = new Menu(); |
| 368 |
$menu->render_admin_header( __( 'Settings', 'subscription' ) ); |
| 369 |
|
| 370 |
include 'views/settings.php'; |
| 371 |
|
| 372 |
// Footer. |
| 373 |
$menu->render_admin_footer(); |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Which settings section the page opens on. |
| 378 |
* |
| 379 |
* Read from `?cat=`, and it has to be a query argument rather than a |
| 380 |
* fragment: the form posts to `options.php`, which redirects back to |
| 381 |
* `_wp_http_referer`, and a fragment never reaches the server. Keeping the |
| 382 |
* section in the query string is what returns you to the panel you saved |
| 383 |
* from. |
| 384 |
* |
| 385 |
* Validated against the sections that actually hold groups, so an unknown |
| 386 |
* or hostile value — or one naming a section only Pro fills — opens the |
| 387 |
* first real section rather than an empty panel. |
| 388 |
* |
| 389 |
* @param array<string,string[]> $category_groups Section key => group keys. |
| 390 |
* @return string Section key, or an empty string when there are no groups. |
| 391 |
*/ |
| 392 |
private function get_active_category( array $category_groups ) { |
| 393 |
$cats = array_keys( $category_groups ); |
| 394 |
|
| 395 |
if ( empty( $cats ) ) { |
| 396 |
return ''; |
| 397 |
} |
| 398 |
|
| 399 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only section selection, validated against the list above. |
| 400 |
$requested = isset( $_GET['cat'] ) ? sanitize_key( wp_unslash( $_GET['cat'] ) ) : ''; |
| 401 |
|
| 402 |
// `all` stacks every section on one page; a known section opens just that |
| 403 |
// one. With nothing requested (or something invalid), open the first |
| 404 |
// section rather than the stacked "All Settings" view. |
| 405 |
if ( 'all' === $requested || in_array( $requested, $cats, true ) ) { |
| 406 |
return $requested; |
| 407 |
} |
| 408 |
|
| 409 |
return $cats[0]; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Enqueue WooCommerce admin styles for settings page. |
| 414 |
* |
| 415 |
* @param string $hook The current admin page hook. |
| 416 |
*/ |
| 417 |
public function enqueue_wc_admin_styles( $hook ) { |
| 418 |
// Only load on our settings page |
| 419 |
if ( isset( $_GET['post_type'] ) && strpos( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), 'subscrpt_order' ) !== false ) { |
| 420 |
// WooCommerce admin styles |
| 421 |
wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), SUBSCRPT_VERSION ); |
| 422 |
// Optional: WooCommerce enhanced select2 |
| 423 |
wp_enqueue_style( 'woocommerce_admin_select2', WC()->plugin_url() . '/assets/css/select2.css', array(), SUBSCRPT_VERSION ); |
| 424 |
wp_enqueue_script( 'select2' ); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|