Skip to content

Commit

Permalink
v 0.9.0
Browse files Browse the repository at this point in the history
- New form HTML helper.
- Migrate some forms to the new helper.
- New basic field types : textarea & multi-select.
  • Loading branch information
Darklg committed Aug 8, 2024
1 parent 6b95725 commit 09767dd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 63 deletions.
58 changes: 58 additions & 0 deletions inc/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
<?php
defined('ABSPATH') || die;

/* ----------------------------------------------------------
Forms
---------------------------------------------------------- */

function wpu_extranet_get_form_html($form_id, $fields = array(), $args = array()) {
$defaults = array(
'before_fields' => '',
'after_fields' => '',
'hidden_fields' => array(),
'has_honey_pot' => false,
'form_action' => '',
'form_title' => '',
'form_submit' => __('Submit', 'wpu_extranet')
);
$args = array_merge($defaults, $args);
if (!is_array($args['hidden_fields'])) {
$args['hidden_fields'] = array();
}
$settings = wpu_extranet_get_skin_settings();
$html = '';

$html .= '<div class="' . $settings['form_wrapper_classname'] . ' form-' . $form_id . '-wrapper">';
if ($args['form_title']) {
$html .= '<h3>' . $args['form_title'] . '</h3>';
}
$html .= '<form name="' . $form_id . '" id="' . $form_id . '" action="' . $args['form_action'] . '" method="post">';
$html .= $args['before_fields'];
$html .= '<ul class="' . $settings['form_items_classname'] . '">';
foreach ($fields as $field_id => $field) {
$html .= wpu_extranet__display_field($field_id, $field);
}
$html .= $args['after_fields'];

if (!empty($fields)) {
$html .= '<li class="' . $settings['form_box_submit_classname'] . '">';
if ($args['has_honey_pot']) {
$honeypot_id = wpu_extranet_register_get_honeypot_id();
$html .= '<label for="' . $honeypot_id . '" aria-hidden="true" class="visually-hidden"><input type="radio" name="' . $honeypot_id . '" id="' . $honeypot_id . '" style="display:none" value="1"></label>';
}
$html .= wp_nonce_field('wpuextranet_' . $form_id . '_action', 'wpuextranet_' . $form_id, true, false);
foreach ($args['hidden_fields'] as $id => $value) {
$html .= '<input type="hidden" name="' . $id . '" value="' . esc_attr($value) . '" />';
}
$html .= '<button class="' . $settings['form_submit_button_classname'] . '" type="submit"><span>' . $args['form_submit'] . '</span></button>';
$html .= '</li>';
}
$html .= '</ul>';
$html .= '</form>';
$html .= '</div>';

return $html;

}

/* ----------------------------------------------------------
Error messages
---------------------------------------------------------- */

function wpuextranet_get_html_errors($errors = array(), $args = array()) {
/* Do not */
if (empty($errors)) {
Expand Down
36 changes: 14 additions & 22 deletions inc/modules/change-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function wpu_extranet_change_email__action() {
return '';
}

if (!isset($_POST['wpuextranet_changeemail']) || !wp_verify_nonce($_POST['wpuextranet_changeemail'], 'wpuextranet_changeemail_action')) {
if (!isset($_POST['wpuextranet_changeemailform']) || !wp_verify_nonce($_POST['wpuextranet_changeemailform'], 'wpuextranet_changeemailform_action')) {
return '';
}

Expand Down Expand Up @@ -91,40 +91,32 @@ function wpu_extranet_change_email__form($args = array()) {
if (!isset($args['before_fields'])) {
$args['before_fields'] = '';
}
$html = '';

$fields = array();
$userdata = get_userdata(get_current_user_id());

$settings = wpu_extranet_get_skin_settings();

$html .= '<div class="' . $settings['form_wrapper_classname'] . ' form-changeemail-wrapper">';
$html .= '<h3>' . __('Change email', 'wpu_extranet') . '</h3>';
$html .= '<form name="changeemailform" id="changeemailform" action="' . get_permalink() . '#changeemailform" method="post">';
$html .= $args['before_fields'];
$html .= '<ul class="' . $settings['form_items_classname'] . '">';
$html .= wpu_extranet__display_field('current_email', array(
$fields['current_email'] = array(
'type' => 'email',
'attributes' => 'readonly minlength="6" autocomplete="off" required="required"',
'label' => __('Your current email', 'wpu_extranet'),
'value' => $userdata->user_email
));
$html .= wpu_extranet__display_field('new_email', array(
);
$fields['new_email'] = array(
'type' => 'email',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('New email', 'wpu_extranet')
));
$html .= wpu_extranet__display_field('confirm_new_email', array(
);
$fields['confirm_new_email'] = array(
'type' => 'email',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('Confirm new email', 'wpu_extranet')
);

return wpu_extranet_get_form_html('changeemailform', $fields, array(
'before_fields' => $args['before_fields'],
'form_action' => get_permalink() . '#changeemailform',
'form_title' => __('Change email', 'wpu_extranet'),
'form_submit' => __('Change email', 'wpu_extranet')
));
$html .= '<li class="' . $settings['form_box_submit_classname'] . '">';
$html .= wp_nonce_field('wpuextranet_changeemail_action', 'wpuextranet_changeemail', true, false);
$html .= '<button class="' . $settings['form_submit_button_classname'] . '" type="submit"><span>' . __('Change email', 'wpu_extranet') . '</span></button>';
$html .= '</li>';
$html .= '</ul>';
$html .= '</form>';
$html .= '</div>';

return $html;
}
Expand Down
37 changes: 14 additions & 23 deletions inc/modules/change-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function wpu_extranet_change_password__action() {
return '';
}

if (!isset($_POST['wpuextranet_changepassword']) || !wp_verify_nonce($_POST['wpuextranet_changepassword'], 'wpuextranet_changepassword_action')) {
if (!isset($_POST['wpuextranet_changepasswordform']) || !wp_verify_nonce($_POST['wpuextranet_changepasswordform'], 'wpuextranet_changepasswordform_action')) {
return '';
}

Expand Down Expand Up @@ -82,39 +82,30 @@ function wpu_extranet_change_password__form($args = array()) {
if (!isset($args['before_fields'])) {
$args['before_fields'] = '';
}
$html = '';

$settings = wpu_extranet_get_skin_settings();

$html .= '<div class="' . $settings['form_wrapper_classname'] . ' form-changepassword-wrapper">';
$html .= '<h3>' . __('Change password', 'wpu_extranet') . '</h3>';
$html .= '<form name="changepasswordform" id="changepasswordform" action="' . get_permalink() . '#changepasswordform" method="post">';
$html .= $args['before_fields'];
$html .= '<ul class="' . $settings['form_items_classname'] . '">';
$html .= wpu_extranet__display_field('current_password', array(
$fields = array();
$fields['current_password'] = array(
'type' => 'password',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('Enter your current password', 'wpu_extranet')
));
$html .= wpu_extranet__display_field('new_password', array(
);
$fields['new_password'] = array(
'type' => 'password',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('New password', 'wpu_extranet')
));
$html .= wpu_extranet__display_field('confirm_new_password', array(
);
$fields['confirm_new_password'] = array(
'type' => 'password',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('Confirm new password', 'wpu_extranet')
);

return wpu_extranet_get_form_html('changepasswordform', $fields, array(
'before_fields' => $args['before_fields'],
'form_action' => get_permalink() . '#changepasswordform',
'form_title' => __('Change password', 'wpu_extranet'),
'form_submit' => __('Change password', 'wpu_extranet')
));
$html .= '<li class="' . $settings['form_box_submit_classname'] . '">';
$html .= wp_nonce_field('wpuextranet_changepassword_action', 'wpuextranet_changepassword', true, false);
$html .= '<button class="' . $settings['form_submit_button_classname'] . '" type="submit"><span>' . __('Change password', 'wpu_extranet') . '</span></button>';
$html .= '</li>';
$html .= '</ul>';
$html .= '</form>';
$html .= '</div>';

return $html;
}

/* ----------------------------------------------------------
Expand Down
27 changes: 11 additions & 16 deletions inc/modules/delete-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function wpu_extranet_delete_account__action() {
return '';
}

if (!isset($_POST['wpuextranet_delete_account']) || !wp_verify_nonce($_POST['wpuextranet_delete_account'], 'wpuextranet_delete_account_action')) {
if (!isset($_POST['wpuextranet_deleteaccountform']) || !wp_verify_nonce($_POST['wpuextranet_deleteaccountform'], 'wpuextranet_deleteaccountform_action')) {
return '';
}

Expand Down Expand Up @@ -100,27 +100,22 @@ function wpu_extranet_delete_account__form($args = array()) {
));
}

$fields = array();
$settings = wpu_extranet_get_skin_settings();
$html .= '<div class="' . $settings['form_wrapper_classname'] . ' form-delete_account-wrapper">';
$html .= '<h3>' . __('Delete your account', 'wpu_extranet') . '</h3>';
$html .= '<form name="delete_accountform" id="delete_accountform" action="' . get_permalink() . '#delete_accountform" method="post">';
$html .= $args['before_fields'];
if ($user_can_delete_account) {
$html .= '<ul class="' . $settings['form_items_classname'] . '">';
$html .= wpu_extranet__display_field('current_password', array(
$fields['current_password'] = array(
'type' => 'password',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('Enter your current password', 'wpu_extranet')
));
$html .= '<li class="' . $settings['form_box_submit_classname'] . '">';
$html .= wp_nonce_field('wpuextranet_delete_account_action', 'wpuextranet_delete_account', true, false);
$html .= '<button class="' . $settings['form_submit_button_classname'] . '" type="submit"><span>' . __('Delete your account', 'wpu_extranet') . '</span></button>';
);
}
$html .= '</li>';
$html .= '</ul>';
$html .= '</form>';
$html .= '</div>';
return $html;

return wpu_extranet_get_form_html('deleteaccountform', $fields, array(
'before_fields' => $args['before_fields'],
'form_action' => get_permalink() . '#deleteaccountform',
'form_title' => __('Delete your account', 'wpu_extranet'),
'form_submit' => __('Delete your account', 'wpu_extranet')
));
}

/* ----------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions inc/modules/edit-metas.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ function wpu_extranet_update_metas__form($args = array()) {
'attributes' => 'readonly',
'value' => $user->user_email
));
$html .= wpu_extranet__display_field('prefs_pwet', array(
'label' => __('Pwet', 'wpu_extranet'),
'type' => 'multi-checkbox',
'options' => array(
'1' => __('Yes', 'wpu_extranet'),
'0' => __('No', 'wpu_extranet')
),
'attributes' => 'readonly',
));

/* Custom fields */
foreach ($extra_fields as $field_id => $field):
Expand Down
25 changes: 24 additions & 1 deletion inc/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ function wpu_extranet__display_field($field_id, $field) {
if (!isset($field['value'])) {
$field['value'] = '';
}
if (!isset($field['options']) || !is_array($field['options'])) {
$field['options'] = array();
}
if (!isset($field['attributes'])) {
$field['attributes'] = '';
}
Expand All @@ -73,7 +76,27 @@ function wpu_extranet__display_field($field_id, $field) {
$html .= '<li class="' . $settings['form_box_classname'] . '">';
$html .= $field['before_content'];
$html .= '<label for="' . $field_id . '">' . $field['label'] . ' :</label>';
$html .= '<input ' . $field['attributes'] . ' type="' . $field['type'] . '" name="' . $field_id . '" value="' . esc_attr($field['value']) . '" id="' . $field_id . '" class="input" size="20" autocapitalize="off" />';
switch ($field['type']) {
case 'multi-checkbox':
if(!is_array($field['value'])) {
$field['value'] = array();
}
$html .= '<ul>';
foreach ($field['options'] as $option_id => $option) {
$html .= '<li>';
$html .= '<input ' . $field['attributes'] . ' type="checkbox" name="' . $field_id . '[]" value="' . $option_id . '" id="' . $field_id . '_' . $option_id . '" ' . (in_array($option_id, $field['value']) ? 'checked="checked"' : '') . ' />';
$html .= '<label for="' . $field_id . '_' . $option_id . '">' . esc_html($option) . '</label>';
$html .= '</li>';
}
$html .= '</ul>';
break;
case 'textarea':
$html .= '<textarea ' . $field['attributes'] . ' name="' . $field_id . '" id="' . $field_id . '" class="input" autocapitalize="off">' . esc_textarea($field['value']) . '</textarea>';
break;
default:
$html .= '<input ' . $field['attributes'] . ' type="' . $field['type'] . '" name="' . $field_id . '" value="' . esc_attr($field['value']) . '" id="' . $field_id . '" class="input" size="20" autocapitalize="off" />';
}

$html .= $field['after_content'];
$html .= '</li>';
return $html;
Expand Down
2 changes: 1 addition & 1 deletion wpu_extranet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
Plugin Name: WPU Extranet
Description: Simple toolbox to create an extranet or a customer account
Version: 0.8.0
Version: 0.9.0
Author: Darklg
Author URI: https://darklg.me/
Text Domain: wpu_extranet
Expand Down

0 comments on commit 09767dd

Please sign in to comment.