| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Settings |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription\Admin |
| 9 |
*/ |
| 10 |
class Settings { |
| 11 |
/** |
| 12 |
* Settings fields. |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
public $settings_fields = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize the class. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
// Load the settings helper. |
| 23 |
SettingsHelper::get_instance(); |
| 24 |
|
| 25 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 26 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 27 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wc_admin_styles' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register submenu on `Subscriptions` menu. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function admin_menu() { |
| 36 |
// Initialize & process settings fields. |
| 37 |
$this->initiate_settings_fields(); |
| 38 |
|
| 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' ] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 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' => 'main', |
| 66 |
'priority' => 0, |
| 67 |
'field_data' => [ |
| 68 |
'title' => __( 'General Settings', 'subscription' ), |
| 69 |
], |
| 70 |
], |
| 71 |
[ |
| 72 |
'type' => 'select', |
| 73 |
'group' => 'main', |
| 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( get_option( 'wp_subscription_renewal_process', 'auto' ) ), |
| 84 |
], |
| 85 |
], |
| 86 |
[ |
| 87 |
'type' => 'input', |
| 88 |
'group' => 'main', |
| 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( get_option( 'wp_subscription_manual_renew_cart_notice' ) ), |
| 95 |
], |
| 96 |
], |
| 97 |
[ |
| 98 |
'type' => 'toggle', |
| 99 |
'group' => 'main', |
| 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' => 'main', |
| 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' => 'main', |
| 131 |
'priority' => 5, |
| 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' => 'main', |
| 143 |
'priority' => 6, |
| 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 |
'type' => 'toggle', |
| 154 |
'group' => 'main', |
| 155 |
'priority' => 9.5, |
| 156 |
'field_data' => [ |
| 157 |
'id' => 'subscrpt_cancellation_feedback_enabled', |
| 158 |
'title' => __( 'Cancellation Survey', 'subscription' ), |
| 159 |
'label' => __( 'Ask customers why they are cancelling', 'subscription' ), |
| 160 |
'description' => __( 'Show a short cancellation survey 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' => __( 'Survey Comment Box', 'subscription' ), |
| 172 |
'label' => __( 'Allow an additional comment', 'subscription' ), |
| 173 |
'description' => __( 'Show an optional free-text comment field in the cancellation survey.', 'subscription' ), |
| 174 |
'value' => '1', |
| 175 |
'checked' => '1' === get_option( 'subscrpt_cancellation_feedback_comment', '1' ), |
| 176 |
], |
| 177 |
], |
| 178 |
]; |
| 179 |
|
| 180 |
// Allow other modules to add/modify settings fields. |
| 181 |
$settings_fields = apply_filters( 'subscrpt_settings_fields', $settings_fields ); |
| 182 |
|
| 183 |
// Process settings fields (group & sort). |
| 184 |
$settings_fields = apply_filters( 'process_subscrpt_settings_fields', $settings_fields ); |
| 185 |
|
| 186 |
// Set the settings fields. |
| 187 |
$this->settings_fields = $settings_fields; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Register settings options. |
| 192 |
**/ |
| 193 |
public function register_settings() { |
| 194 |
register_setting( |
| 195 |
'wp_subscription_settings', |
| 196 |
'wp_subscription_renewal_process', |
| 197 |
array( |
| 198 |
'type' => 'string', |
| 199 |
'sanitize_callback' => 'sanitize_text_field', |
| 200 |
) |
| 201 |
); |
| 202 |
register_setting( |
| 203 |
'wp_subscription_settings', |
| 204 |
'wp_subscription_manual_renew_cart_notice', |
| 205 |
array( |
| 206 |
'type' => 'string', |
| 207 |
'sanitize_callback' => 'sanitize_textarea_field', |
| 208 |
) |
| 209 |
); |
| 210 |
register_setting( |
| 211 |
'wp_subscription_settings', |
| 212 |
'wp_subscription_active_role', |
| 213 |
array( |
| 214 |
'type' => 'string', |
| 215 |
'sanitize_callback' => 'sanitize_text_field', |
| 216 |
) |
| 217 |
); |
| 218 |
register_setting( |
| 219 |
'wp_subscription_settings', |
| 220 |
'wp_subscription_unactive_role', |
| 221 |
array( |
| 222 |
'type' => 'string', |
| 223 |
'sanitize_callback' => 'sanitize_text_field', |
| 224 |
) |
| 225 |
); |
| 226 |
register_setting( |
| 227 |
'wp_subscription_settings', |
| 228 |
'wp_subscription_stripe_auto_renew', |
| 229 |
array( |
| 230 |
'type' => 'string', |
| 231 |
'sanitize_callback' => 'sanitize_text_field', |
| 232 |
) |
| 233 |
); |
| 234 |
register_setting( |
| 235 |
'wp_subscription_settings', |
| 236 |
'wp_subscription_auto_renewal_toggle', |
| 237 |
array( |
| 238 |
'type' => 'string', |
| 239 |
'sanitize_callback' => 'sanitize_text_field', |
| 240 |
) |
| 241 |
); |
| 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 |
|
| 259 |
do_action( 'subscrpt_register_settings', 'subscrpt_settings' ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Settings HTML. |
| 264 |
*/ |
| 265 |
public function settings_content() { |
| 266 |
$settings_fields = $this->settings_fields; |
| 267 |
|
| 268 |
// Header. |
| 269 |
$menu = new Menu(); |
| 270 |
$menu->render_admin_header( __( 'Settings', 'subscription' ) ); |
| 271 |
|
| 272 |
include 'views/settings.php'; |
| 273 |
|
| 274 |
// Footer. |
| 275 |
$menu->render_admin_footer(); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Enqueue WooCommerce admin styles for settings page. |
| 280 |
* |
| 281 |
* @param string $hook The current admin page hook. |
| 282 |
*/ |
| 283 |
public function enqueue_wc_admin_styles( $hook ) { |
| 284 |
// Only load on our settings page |
| 285 |
if ( isset( $_GET['post_type'] ) && strpos( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), 'subscrpt_order' ) !== false ) { |
| 286 |
// WooCommerce admin styles |
| 287 |
wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), SUBSCRPT_VERSION ); |
| 288 |
// Optional: WooCommerce enhanced select2 |
| 289 |
wp_enqueue_style( 'woocommerce_admin_select2', WC()->plugin_url() . '/assets/css/select2.css', array(), SUBSCRPT_VERSION ); |
| 290 |
wp_enqueue_script( 'select2' ); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|