*/
class Ace_User_Management_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $pluginName The ID of this plugin.
*/
private $pluginName;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $pluginName The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $pluginName, $version ) {
$this->pluginName = $pluginName;
$this->version = $version;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function aceEnqueueStyles() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Ace_User_Management_Loader as all of the hooks are defined
* in that particular class.
*
* The Ace_User_Management_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style( $this->pluginName, plugin_dir_url( __FILE__ ) . 'css/ace-user-management-admin.css', array(), $this->version, 'all' );
wp_enqueue_style( $this->pluginName.'-fontawesome', plugin_dir_url( __FILE__ ) . 'css/ace-fontawesome.css', array(), $this->version, 'all' );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
if (!empty($page) && sanitize_text_field($page) == 'ace-custom-field') {
wp_enqueue_style( $this->pluginName . '-bootstrap', plugin_dir_url( __FILE__ ) . 'css/bootstrap.min.css', array(), $this->version, 'all' );
wp_enqueue_style( $this->pluginName . '-fontawesome', plugin_dir_url( __FILE__ ) . 'css/ace-fontawesome.css', array(), $this->version, 'all' );
}
}
/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function aceEnqueueScripts() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Ace_User_Management_Loader as all of the hooks are defined
* in that particular class.
*
* The Ace_User_Management_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_script( $this->pluginName, plugin_dir_url( __FILE__ ) . 'js/ace-user-management-admin.js', array( 'jquery' ), $this->version, false );
wp_localize_script( $this->pluginName, 'ajax', array('url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'delete_user_nonce' )) );
wp_enqueue_script( $this->pluginName.'-bootjs', plugin_dir_url( __FILE__ ) . 'js/ace-bootstrap.min.js', array(), $this->version, 'all' );
wp_enqueue_script( $this->pluginName.'-jquery-core', get_site_url() . '/wp-includes/js/jquery/ui/core.min.js', array(), $this->version, 'all' );
wp_enqueue_script( $this->pluginName.'-jquery-widget', get_site_url() . '/wp-includes/js/jquery/ui/widget.min.js', array(), $this->version, 'all' );
wp_enqueue_script( $this->pluginName.'-jquery-mouse', get_site_url() . '/wp-includes/js/jquery/ui/mouse.min.js', array(), $this->version, 'all' );
wp_enqueue_script( $this->pluginName.'-sortable', get_site_url() . '/wp-includes/js/jquery/ui/sortable.min.js', array(), $this->version, 'all' );
}
// wordpress menu
public function aceRegisterOptionsPage(){
add_menu_page(
'User Registration',
'User Registration',
'manage_options',
'ace-user-registration',
array($this, 'ace_register_page'),
'dashicons-dashboard',
52
);
add_submenu_page( 'ace-user-registration',
'Custom Fields',
'Custom Fields',
'manage_options',
'ace-custom-field',
array( $this, 'ace_add_fields_' )
);
add_submenu_page(
'ace-user-registration',
'Settings',
'Settings',
'manage_options',
'Ace-Setting',
array($this, 'add_all_setting_plugin')
);
}
public function languageLoadTextdomain() {
$mo_file = plugins_url( 'ace_user_managament/languages/ace_user_managament' ).'-'.get_locale().'mo';
load_textdomain('ace_user_managament', $mo_file );
}
public function add_all_setting_plugin() {
global $wpdb;
$recaptchaKeys = get_option('custom_reCapatcha_value');
if ( isset( $_POST['custom_css'] ) && isset( $_POST['ace_user_management_nonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_POST['ace_user_management_nonce'] ) );
if ( wp_verify_nonce( $nonce, 'ace_user_management_settings' )) {
if ( isset( $_POST['secretKEY'] ) ) {
$secretkey = sanitize_text_field( wp_unslash( $_POST['secretKEY'] ) );
}
if ( isset( $_POST['siteKEY'] ) ) {
$sitekey = sanitize_text_field( wp_unslash( $_POST['siteKEY'] ) );
}
$error = array();
$success = '';
if ( ! empty( $secretkey ) ) {
if ( strlen( $secretkey ) == 40 ) {
if ( ! empty( $sitekey ) ) {
if ( strlen( $sitekey ) == 40 ) {
$checked = ( isset( $_POST['reCapatchacheck'] ) && $_POST['reCapatchacheck'] == 1 ) ? 1 : 0;
$update_reCap = update_option( 'custom_reCapatcha_value',
array(
'c_reCaptcha_value' => $checked,
'author_reCap_secrectkey' => $secretkey,
'author_siteKey' => $sitekey
)
);
if ( $update_reCap ) {
$success = ' Successfully updated';
} else {
$error['update_error'] = 'There was an error updating reCaptcha settings.';
}
} else {
$error['sitekey_vald'] = '! Site key not valid';
}
} else {
$error['sitekey_vald'] = '! Site key field is empty';
}
} else {
$error['secretkey_vald'] = '! Secret key is not valid';
}
} else {
$error['empty_secret_key'] = '! Secret key field is empty';
}
if ( isset( $error ) ) {
foreach ( $error as $error_key => $error_value ) {
echo '
' . esc_html( $error_value ) . '
';
}
} else {
echo esc_html($success);
}
if ( isset( $_POST['custom_css_for_pages'] ) ) {
$customCssPlugin = sanitize_textarea_field( wp_unslash( $_POST['custom_css_for_pages'] ) );
$updateCss = update_option( 'ace_custom_css_plugin', $customCssPlugin );
if ( $updateCss ) {
echo ''.esc_html(" Successfully updated CSS ").'';
} else {
echo 'Failed to update CSS settings.
';
}
}
} else {
// Nonce verification failed
echo 'Nonce verification failed. Please try again.
';
}
}
if ( is_array( $recaptchaKeys ) && isset( $recaptchaKeys['c_reCaptcha_value'] ) && $recaptchaKeys['c_reCaptcha_value'] == 1 ) {
$reChecked = 'checked="checked"';
} else {
$reChecked = '';
}
$getCustomCss = get_option( 'ace_custom_css_plugin', '' );
require_once( __DIR__ . '/partials/ace-shortcode-admin-display.php' );
}
public function ace_register_page(){
global $title;
global $wpdb;
require_once plugin_dir_path( __FILE__ ) . 'partials/ace-user-management-admin-display.php';
}
public function ace_add_fields_(){
global $title;
global $wpdb, $post;
$success_message = '';
$fieldTable = $wpdb->prefix.'ace_register_fields';
echo " ". esc_html($title) ."
";
if ( isset( $_REQUEST['submit_form'] ) ) {
if (isset($_POST['ace_user_add_field'])) {
$nonce = sanitize_text_field(wp_unslash($_POST['ace_user_add_field']));
if (wp_verify_nonce($nonce, 'ace_user_management_settings')) {
$dropdownOptions = "";
$inputType = isset($_REQUEST['input_type']) ? sanitize_text_field(wp_unslash($_REQUEST['input_type'])) : '';
if ($inputType === "dropdown") {
if (empty($_REQUEST['dropdown_options'])) {
echo 'Please add dropdown options. This field is required.
';
return;
}
$dropdownOptions = sanitize_text_field(wp_unslash($_REQUEST['dropdown_options']));
} else {
$dropdownOptions = "";
}
$post = array(
'input_label' => isset($_REQUEST['input_label']) ? sanitize_text_field(wp_unslash($_REQUEST['input_label'])) : '',
'input_placeholder' => isset($_REQUEST['input_placeholder']) ? sanitize_text_field(wp_unslash($_REQUEST['input_placeholder'])): '',
'input_name' => isset($_REQUEST['input_name']) ? sanitize_text_field(wp_unslash($_REQUEST['input_name'])) : '',
'input_type' => isset($_REQUEST['input_type']) ? sanitize_text_field(wp_unslash($_REQUEST['input_type'])) : '',
'dropdown_options' => isset($dropdownOptions) ? sanitize_text_field(wp_unslash($dropdownOptions)) : '',
'is_required' => isset($_REQUEST['input_required']) ? 1 : 0
);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$inserted = $wpdb->insert($fieldTable, $post );
if ( $inserted ) {
$success_message = 'Field "' . esc_html($post['input_label']) . '" added successfully!';
}
}
}
}
echo '';
if( isset( $_REQUEST['update'] ) ) {
if (isset($_POST['ace_user_update_field'])) {
$nonce = sanitize_text_field(wp_unslash($_POST['ace_user_update_field']));
if (wp_verify_nonce($nonce, 'ace_user_management_settings')) {
$inputNameUpdate = isset($_REQUEST['input_name_update']) ? sanitize_text_field(wp_unslash($_REQUEST['input_name_update'])) : '';
$drOptionsUpdate = ( isset($_REQUEST['dr_options_update']) ) ? sanitize_text_field(wp_unslash($_REQUEST['dr_options_update'])) : '';
$inputTypeUpdate = ( isset($_REQUEST['input_type_update']) ) ? sanitize_text_field( wp_unslash($_REQUEST['input_type_update'])) : '';
$fPlaceUpdate = ( isset($_REQUEST['f_place_update']) ) ? sanitize_text_field( wp_unslash($_REQUEST['f_place_update'])) : '';
$fLableUpdate = ( isset($_REQUEST['f_lable_update']) ) ? sanitize_text_field( wp_unslash($_REQUEST['f_lable_update'])) : '';
$postId = isset($_REQUEST['id_update']) ? sanitize_text_field(wp_unslash($_REQUEST['id_update'])) : '';
$requiredUpdate = isset($_POST['input_required_update']) ? 1 : 0;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$updateSuces = $wpdb->update(
$fieldTable,
array(
'input_label' => $fLableUpdate,
'input_placeholder' => $fPlaceUpdate,
'input_type' => $inputTypeUpdate,
'dropdown_options' => $drOptionsUpdate,
'input_name' => $inputNameUpdate,
'is_required' => $requiredUpdate
),
array('id' => $postId)
);
if( $updateSuces){
$update_massage = ucfirst($fLableUpdate) .' '.'field are updated successfully';
}
}
}
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$resultTbl = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}ace_register_fields", OBJECT );
if( !empty($resultTbl ) ) {
if( isset( $_REQUEST['savelayout']) ){
$idUpdate = isset($_REQUEST['id_update_']) ? array_map('absint', wp_unslash($_REQUEST['id_update_'])) : [];
foreach ( $idUpdate as $sortKey => $sortbyValue ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->update( $fieldTable,array( 'sortby' => $sortKey ),array( 'id' => $sortbyValue ) );
}
}
}
$url = plugin_dir_path( __FILE__ );
$file = plugin_dir_path( __FILE__ ) . 'partials/register_form.php';
if ( file_exists( $file ) )
require $file;
echo '
';
}
/**
* Add new fields above 'Update' button in backend user edit form.
*
* @param WP_User $user User object.
*/
function ace_additional_profile_fields( $user ) {
global $wpdb;
// Fetch all custom fields
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$results = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}ace_register_fields",
OBJECT
);
?>
Ace Registered Fields
input_name;
$meta_val = get_user_meta( $user->ID, $meta_key, true );
$dropdownOptions = array_map( 'trim', explode( ',', $value->dropdown_options ) );
?>
get_results(
"SELECT * FROM {$wpdb->prefix}ace_register_fields",
OBJECT
);
foreach ( $fields as $field ) {
$key = $field->input_name;
$is_required = $field->is_required;
if ( isset( $_POST[ $key ] ) ) {
$value = ( $field->input_type === 'textarea' )
? sanitize_textarea_field( wp_unslash( $_POST[ $key ] ) )
: sanitize_text_field( wp_unslash( $_POST[ $key ] ) );
if ( $is_required && empty($value) ) {
// Required field is empty
wp_die(
esc_html(
sprintf(
/* translators: %s: Field label */
esc_html__( 'The field %s is required', 'ace-user-management' ),
$field->input_label
)
)
);
}
if ( ! empty( $value ) ) {
update_user_meta( $userId, $key, $value );
} else {
delete_user_meta( $userId, $key );
}
} elseif ( $is_required ) {
wp_die(
sprintf(
/* translators: %s: Field label */
esc_html__( 'The field %s is required', 'ace-user-management' ),
esc_html( $field->input_label )
)
);
}
}
// Get all registered fields
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$fields = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}ace_register_fields",
OBJECT
);
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
$key = sanitize_key( $field->input_name );
$is_required = (bool) $field->is_required;
// Grab and sanitize the posted value
$value = isset( $_POST[ $key ] )
? sanitize_text_field( wp_unslash( $_POST[ $key ] ) )
: '';
// Required field check
if ( $is_required && '' === $value ) {
wp_die(
sprintf(
/* translators: %s: Field label */
esc_html__( 'The field %s is required', 'ace-user-management' ),
esc_html( $field->input_label )
)
);
return false;
}
// Save or delete user meta
if ( '' !== $value ) {
update_user_meta( $userId, $key, $value );
} else {
delete_user_meta( $userId, $key );
}
}
}
return true;
}
function aceDeleteUser() {
if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field(wp_unslash( $_POST['nonce']) ), 'delete_user_nonce' ) ) {
wp_send_json_error( array( 'message' => 'Security check failed. Invalid request.' ) );
}
if ( !isset( $_POST['id'] ) || !is_numeric( $_POST['id'] ) ) {
wp_send_json_error( array( 'message' => 'Invalid user ID.' ) );
}
global $wpdb;
$id = (int) $_POST['id'];
$table = "{$wpdb->prefix}ace_register_fields";
$where = array( 'id' => $id );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$deleted = $wpdb->delete( $table, $where );
if ( $deleted ) {
wp_send_json_success( array( 'message' => 'Deleted successfully!' ) );
} else {
wp_send_json_error( array( 'message' => 'Error deleting user.' ) );
}
wp_die();
}
}