| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace SpringDevs\Subscription\Admin; |
| 4 | 4 | |
| 5 | +use SpringDevs\Subscription\Ajax; | |
| 6 | + | |
| 5 | 7 | /** |
| 6 | 8 | * Class Settings |
| 7 | 9 | * |
| 8 | 10 | * @package SpringDevs\Subscription\Admin |
| @@ -24,8 +26,9 @@ | ||
| 24 | 26 | |
| 25 | 27 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 26 | 28 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 27 | 29 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wc_admin_styles' ) ); |
| 30 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_installer_script' ) ); | |
| 28 | 31 | } |
| 29 | 32 | |
| 30 | 33 | /** |
| 31 | 34 | * Register submenu on `Subscriptions` menu. |
| @@ -101,14 +104,22 @@ | ||
| 101 | 104 | 'field_data' => [ |
| 102 | 105 | 'id' => 'wp_subscription_stripe_auto_renew', |
| 103 | 106 | 'title' => __( 'Stripe Auto Renewal', 'subscription' ), |
| 104 | 107 | '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 | - ), | |
| 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(), | |
| 111 | 122 | 'value' => '1', |
| 112 | 123 | 'checked' => '1' === get_option( 'wp_subscription_stripe_auto_renew', '1' ), |
| 113 | 124 | ], |
| 114 | 125 | ], |
| @@ -125,8 +136,58 @@ | ||
| 125 | 136 | 'checked' => '1' === get_option( 'wp_subscription_auto_renewal_toggle', '1' ), |
| 126 | 137 | ], |
| 127 | 138 | ], |
| 128 | 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 | + [ | |
| 129 | 190 | 'type' => 'select', |
| 130 | 191 | 'group' => 'role_based_settings', |
| 131 | 192 | 'priority' => 2, |
| 132 | 193 | 'field_data' => [ |
| @@ -161,8 +222,61 @@ | ||
| 161 | 222 | $this->settings_fields = $settings_fields; |
| 162 | 223 | } |
| 163 | 224 | |
| 164 | 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 | + /** | |
| 165 | 279 | * Register settings options. |
| 166 | 280 | **/ |
| 167 | 281 | public function register_settings() { |
| 168 | 282 | register_setting( |
| @@ -199,8 +313,32 @@ | ||
| 199 | 313 | ); |
| 200 | 314 | register_setting( |
| 201 | 315 | 'wp_subscription_settings', |
| 202 | 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', | |
| 203 | 341 | array( |
| 204 | 342 | 'type' => 'string', |
| 205 | 343 | 'sanitize_callback' => 'sanitize_text_field', |
| 206 | 344 | ) |