plugin_path = WP_PLUGIN_DIR . '/add-customer-for-woocommerce/';
}
/**
* Adds the Settings menu item in the Wordpress backend.
*
* @return void
*/
public function setup_options()
{
$title = __('Add customer settings', 'add-customer-for-woocommerce');
add_submenu_page('woocommerce', $title, $title, 'manage_options', 'wac_general_options', [$this, 'render_options']);
}
/**
* Loads the backend page template /template/backend/backend-options-page.php
*
* @return void
*/
public function render_options()
{
$wac = new woo_add_customer();
echo $wac->load_template_to_var('backend-options-page', 'backend/'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The template escapes its output
return;
}
/**
* Registers the settings for the Page.
*
* @return void
*/
public function wac_register_settings()
{
global $pagenow;
if($pagenow !== 'admin.php' && $pagenow !== 'options.php' ) {
return false;
}
//Only used to check if the settings page is loaded
$page = isset($_REQUEST['page']) ? sanitize_key(wp_unslash($_REQUEST['page'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ($page !== 'wac_general_options') {
return false;
}
$wac = new woo_add_customer();
$blog_name = get_bloginfo('name');
$default_email_from = $wac->get_mail_from();
$user_miss_match_message = ($wac->is_customer_created_miss_match())
? $wac->wac_format_notice(__('Warning: There were some customers created before this feature got introduced. If you activate that, some of the customers will still get the notifications.', 'add-customer-for-woocommerce'), 'warning')
: "";
$notification_confilct_message = ($this->get_wac_option('wac_suppress_all_notification') === 'yes' and $this->get_wac_option('wac_send_notification') === 'yes')
? $wac->wac_format_notice(__('Warning: Emails to user regarding account creation will still be sent. Uncheck "Send notifications to new user" to deactivate those as well', 'add-customer-for-woocommerce'), 'warning')
: "";
register_setting('wac_general_options', 'wac_general_options', [$this, 'wac_options_validate']);
add_settings_section('wac_main_settings', __('Main settings', 'add-customer-for-woocommerce'), null, 'wac_general_options');
add_settings_section('wac_template_settings', __('Template settings', 'add-customer-for-woocommerce'), null, 'wac_general_options');
add_settings_field(
'wac_preselect',
__('Add new customer by default', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_preselect',
'type' => 'checkbox',
'class' => 'wac-checkbox wac_preselect',
'description' => __('Select this box if you\'d like to have the "Add Customer" Checkbox activated by default.', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_preselect_update',
__('Update new customer by default', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_preselect_update',
'type' => 'checkbox',
'class' => 'wac-checkbox wac_preselect_update',
'description' => __('If this is checked, the existing customer data will be updated when the order is updated.', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_send_notification',
__('Send notifications to new user', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_send_notification',
'type' => 'checkbox',
'class' => 'wac-checkbox wac_preselect',
'description' => __('Check this to send an "account created" email to the customer after account creation.', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_suppress_all_notification',
__('Suppress all notifications to users created by the plugin', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_suppress_all_notification',
'type' => 'checkbox',
'class' => 'wac-checkbox wac_preselect',
'description' => __('Check this if you like to not send any emails to users who were created by the plugin.', 'add-customer-for-woocommerce') . $user_miss_match_message . $notification_confilct_message,
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_add_customer_order_to_user',
__('Link orders to customer', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_add_customer_order_to_user',
'type' => 'checkbox',
'class' => 'wac-checkbox',
'description' => __('Check this to link orders created in the frontend. The email will be used as an identifier. Only works if the user creates an order as a guest.', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_allow_pay_for_order',
__('Allow payment without login', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_allow_pay_for_order',
'type' => 'checkbox',
'class' => 'wac-checkbox',
'description' => __('Check this to let allow the order to be paid by logged out users or admins via the "Customer payment page" link . This only works if the customer, assigned to the order, was created by this plugin. Administrators and shop managers can also open the payment page of any customer order.', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_define_user_role',
__('Define user role individually', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_define_user_role',
'type' => 'checkbox',
'class' => 'wac-checkbox wac_preselect',
'description' => __('Allow user role selection before creating a new customer', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options'
)
);
add_settings_field(
'wac_default_user_role',
__('User role for new customer', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_default_user_role',
'type' => 'select',
'class' => 'wac-select-input',
'description' => __('Define the user role for new customers', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options',
'values_array' => array_merge($wac->get_user_role_array()),
'default_value' => 'customer'
)
);
add_settings_field(
'wac_fakemail_format',
__('Auto-generated email format', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_main_settings',
array(
'label_for' => 'wac_fakemail_format',
'type' => 'text',
'class' => 'wac-text-input',
'description' =>
__('The format you\'d like to have for the automatically generated email.', 'add-customer-for-woocommerce') . ' ' .
__('You can use the supported tags below wrapping them in [ ]', 'add-customer-for-woocommerce') . '
' .
__('Supported tags:', 'add-customer-for-woocommerce') . ' ' . implode(', ', apply_filters('wac_supported_fake_email_parts', $wac->supported_fake_email_parts)) . '', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public hook, renaming would break existing customizations
'page' => 'wac_general_options',
'default_value' => '[first_name].[last_name].[company]@' . $wac->get_domain_name()
)
);
//Template settings fields
add_settings_field(
'wac_template_subject_add_account',
__('New account created email subject', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_template_settings',
array(
'label_for' => 'wac_template_subject_add_account',
'type' => 'text',
'class' => 'wac-text-input',
'description' => __('The subject / header for the email sent to new customers', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options',
/* translators: %s: Name of the website */
'default_value' => sprintf(__("New account created at %s", 'add-customer-for-woocommerce'), $blog_name)
)
);
add_settings_field(
'wac_email_from',
__('Sender email', 'add-customer-for-woocommerce'),
[$this, 'get_settings_option'],
'wac_general_options',
'wac_template_settings',
array(
'label_for' => 'wac_email_from',
'type' => 'text',
'class' => 'wac-text-input',
'description' => __('The sender email', 'add-customer-for-woocommerce'),
'page' => 'wac_general_options',
'default_value' => $default_email_from
)
);
}
/**
* Validates the Input of the options page
*
* @param array $input - The input to validate
* @return array
*/
public function wac_options_validate($input)
{
foreach ($input as $key => $value) {
switch ($key) {
case 'wac_fakemail_format':
if (!empty($value)) {
$valid_format = $this->sanitize_placeholder_email($value, $key);
//Keep the previously saved format, if the new one is not valid
$previous_format = (string) $this->get_wac_option($key);
$input[$key] = ($valid_format !== false) ? $valid_format : $previous_format;
} else {
$input[$key] = '';
}
break;
case 'wac_default_user_role':
$wac = new woo_add_customer();
$allowed_roles = $wac->get_user_role_array();
if (!$allowed_roles[$value]) {
$this->wac_set_notice(esc_html__('You are not allowed to set this user role', 'add-customer-for-woocommerce'), "error", $key);
} else {
$input[$key] = htmlspecialchars($value);
}
break;
case 'wac_email_from':
$input[$key] = sanitize_email($value);
break;
default:
$input[$key] = htmlspecialchars($value);
break;
}
}
return $input;
}
/**
* Checks if the option field contains some errors.
*
* @param string $field_name
* @param string $value
* @return string Error message or empty string
*/
public function check_option($field_name, $value)
{
switch ($field_name) {
case 'wac_fakemail_format':
//Check if multiple @ exists
if (!empty($value) and substr_count($value, '@') !== 1) {
return "
" . __('Invalid email address', 'add-customer-for-woocommerce') . "