Add woocommerce form locations
Validate Build / validate-build (push) Failing after 27s
Validate Build / validate-build (push) Failing after 27s
This commit is contained in:
+49
-2
@@ -28,6 +28,11 @@ final class Settings {
|
||||
public const LOCATION_COMMENT_FORM = 'comment_form';
|
||||
public const LOCATION_POST_PASSWORD_FORM = 'post_password_form';
|
||||
|
||||
public const LOCATION_WC_LOGIN = 'wc_login';
|
||||
public const LOCATION_WC_LOST_PASSWORD = 'wc_lost_password';
|
||||
public const LOCATION_WC_REGISTER = 'wc_register';
|
||||
public const LOCATION_WC_CHECKOUT = 'wc_checkout';
|
||||
|
||||
protected function __construct() {
|
||||
add_action('admin_menu', [$this, 'menu']);
|
||||
add_action('admin_init', [$this, 'init']);
|
||||
@@ -70,7 +75,14 @@ final class Settings {
|
||||
}
|
||||
|
||||
public static function get_form_locations():array {
|
||||
return (array) self::get('form_locations');
|
||||
$form_locations = (array) self::get('form_locations');
|
||||
if (Plugin::is_woocommerce_active()) {
|
||||
$form_locations = array_merge(
|
||||
$form_locations,
|
||||
(array) self::get('wc_form_locations')
|
||||
);
|
||||
}
|
||||
return $form_locations;
|
||||
}
|
||||
|
||||
public static function is_form_location_enabled(string $name):bool {
|
||||
@@ -101,6 +113,22 @@ final class Settings {
|
||||
return self::is_form_location_enabled(self::LOCATION_POST_PASSWORD_FORM);
|
||||
}
|
||||
|
||||
public static function is_wc_login_enabled():bool {
|
||||
return self::is_form_location_enabled(self::LOCATION_WC_LOGIN);
|
||||
}
|
||||
|
||||
public static function is_wc_lost_password_enabled():bool {
|
||||
return self::is_form_location_enabled(self::LOCATION_WC_LOST_PASSWORD);
|
||||
}
|
||||
|
||||
public static function is_wc_register_enabled():bool {
|
||||
return self::is_form_location_enabled(self::LOCATION_WC_REGISTER);
|
||||
}
|
||||
|
||||
public static function is_wc_checkout_enabled():bool {
|
||||
return self::is_form_location_enabled(self::LOCATION_WC_CHECKOUT);
|
||||
}
|
||||
|
||||
public function menu() {
|
||||
if (!current_user_can(self::CAPABILITY)) return;
|
||||
add_options_page(
|
||||
@@ -176,7 +204,7 @@ final class Settings {
|
||||
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_form_locations',
|
||||
Plugin::__('Form locations'),
|
||||
Plugin::__('WordPress form locations'),
|
||||
[$this, 'checkbox_field'],
|
||||
Plugin::get_id(),
|
||||
'forms',
|
||||
@@ -193,6 +221,25 @@ final class Settings {
|
||||
'description' => Plugin::__('Custom login form requires login page to also be enabled.'),
|
||||
]
|
||||
);
|
||||
|
||||
if (Plugin::is_woocommerce_active()) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_wc_form_locations',
|
||||
Plugin::__('WooCommerce form locations'),
|
||||
[$this, 'checkbox_field'],
|
||||
Plugin::get_id(),
|
||||
'forms',
|
||||
[
|
||||
'name' => 'wc_form_locations',
|
||||
'options' => [
|
||||
self::LOCATION_WC_LOGIN => Plugin::__('Login form'),
|
||||
self::LOCATION_WC_LOST_PASSWORD => Plugin::__('Lost password form'),
|
||||
self::LOCATION_WC_REGISTER => Plugin::__('Register form'),
|
||||
self::LOCATION_WC_CHECKOUT => Plugin::__('Checkout form'),
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function page() {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-captcha
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Captcha\Location\WooCommerce;
|
||||
|
||||
use Override;
|
||||
use Ogre\Captcha\Location;
|
||||
use Ogre\Captcha\Settings;
|
||||
use WP_Error;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Checkout extends Location {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct(Settings::LOCATION_WC_CHECKOUT);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function enabled(): bool
|
||||
{
|
||||
return is_user_logged_in() ? false : parent::enabled();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function activate(): void
|
||||
{
|
||||
add_action('woocommerce_after_checkout_billing_form', [$this, 'render']);
|
||||
add_action('woocommerce_after_checkout_validation', [$this, 'process_form'], 10, 2);
|
||||
}
|
||||
|
||||
public function process_form(array $data, WP_Error $errors) {
|
||||
$this->process($errors);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Checkout::instance();
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-captcha
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Captcha\Location\WooCommerce;
|
||||
|
||||
use Override;
|
||||
use Ogre\Captcha\Location;
|
||||
use Ogre\Captcha\Settings;
|
||||
use WP_Error;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Login extends Location {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct(Settings::LOCATION_WC_LOGIN);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function enabled(): bool
|
||||
{
|
||||
return is_user_logged_in() ? false : parent::enabled();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function activate(): void
|
||||
{
|
||||
add_action('woocommerce_login_form', [$this, 'render']);
|
||||
if (isset($_POST['woocommerce-login-nonce'])) {
|
||||
add_filter('woocommerce_process_login_errors', [$this, 'process_form'], 10, 3);
|
||||
}
|
||||
}
|
||||
|
||||
public function process_form(WP_Error $validation_error, string $user_login, string $user_password):WP_Error {
|
||||
$this->process($validation_error);
|
||||
return $validation_error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Login::instance();
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-captcha
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Captcha\Location\WooCommerce;
|
||||
|
||||
use Override;
|
||||
use Ogre\Captcha\Location;
|
||||
use Ogre\Captcha\Settings;
|
||||
use WP_Error;
|
||||
use WP_User;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class LostPassword extends Location {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct(Settings::LOCATION_WC_LOST_PASSWORD);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function enabled(): bool
|
||||
{
|
||||
return is_user_logged_in() ? false : parent::enabled();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function activate(): void
|
||||
{
|
||||
add_action('woocommerce_lostpassword_form', [$this, 'render']);
|
||||
if (isset($_POST['woocommerce-lost-password-nonce'])) {
|
||||
add_action('lostpassword_post', [$this, 'process_form']);
|
||||
}
|
||||
}
|
||||
|
||||
public function process_form(WP_Error $errors, WP_User|bool $user_data) {
|
||||
$this->process($errors);
|
||||
// NOTE: Potentially use `allow_password_reset` to display error message?
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LostPassword::instance();
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-captcha
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Captcha\Location\WooCommerce;
|
||||
|
||||
use Override;
|
||||
use Ogre\Captcha\Location;
|
||||
use Ogre\Captcha\Settings;
|
||||
use WP_Error;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Register extends Location {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct(Settings::LOCATION_WC_REGISTER);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function enabled(): bool
|
||||
{
|
||||
return is_user_logged_in() ? false : parent::enabled();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function activate(): void
|
||||
{
|
||||
add_action('woocommerce_register_form', [$this, 'render']);
|
||||
if (isset($_POST['woocommerce-register-nonce'])) {
|
||||
add_filter('woocommerce_process_registration_errors', [$this, 'process_form'], 10, 4);
|
||||
}
|
||||
}
|
||||
|
||||
public function process_form(WP_Error $validation_error, string $username, string $password, string $email):WP_Error {
|
||||
$this->process($validation_error);
|
||||
return $validation_error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Register::instance();
|
||||
+18
-1
@@ -35,6 +35,10 @@ require_once 'vendor/autoload.php';
|
||||
|
||||
final class Captcha extends Plugin {
|
||||
|
||||
public static function is_woocommerce_active():bool {
|
||||
return is_plugin_active('woocommerce/woocommerce.php');
|
||||
}
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
@@ -50,6 +54,8 @@ final class Captcha extends Plugin {
|
||||
'inc/location/class-post-password-form.php',
|
||||
'inc/location/class-registration-page.php',
|
||||
]);
|
||||
|
||||
add_action('plugins_loaded', [$this, 'load_woocommerce'], 9, 0);
|
||||
}
|
||||
|
||||
protected function enable():void {
|
||||
@@ -62,7 +68,7 @@ final class Captcha extends Plugin {
|
||||
remove_filter('plugin_action_links_' . self::get_basename(), [$this, 'action_links']);
|
||||
}
|
||||
|
||||
public function action_links($links) {
|
||||
public function action_links(array $links):array {
|
||||
if (current_user_can('manage_options')) {
|
||||
array_unshift($links, sprintf(
|
||||
'<a href="%s">%s</a>',
|
||||
@@ -73,6 +79,17 @@ final class Captcha extends Plugin {
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function load_woocommerce():void {
|
||||
if (!self::is_woocommerce_active()) return;
|
||||
|
||||
$this->add_files([
|
||||
'inc/location/woocommerce/class-checkout.php',
|
||||
'inc/location/woocommerce/class-login.php',
|
||||
'inc/location/woocommerce/class-lost-password.php',
|
||||
'inc/location/woocommerce/class-register.php',
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Captcha::instance();
|
||||
|
||||
Reference in New Issue
Block a user