Plugin Directory

source: userswp/tags/1.2.58/admin/class-admin.php

Last change on this file was 3394255, checked in by stiofansisland, 5 months ago

Update to version 1.2.48 from GitHub

File size: 50.5 KB
Line 
, '</a>' ),
 . __( 'Resend Activation', 'userswp' ) . '</a>';
 . __( 'Activate User', 'userswp' ) . '</a>';
747                }
748
749                return $actions;
750        }
751
752        /**
753         * Returns users bulk actions
754         *
755         * @param array $bulk_actions Bulk actions.
756         *
757         * @return      array   Bulk actions.
758         * @package     userswp
759         *
760         */
761        public function users_bulk_actions( $bulk_actions ) {
762                $bulk_actions['uwp_resend']        = __( 'Resend Activation', 'userswp' );
763                $bulk_actions['uwp_activate_user'] = __( 'Activate Users', 'userswp' );
764
765                return $bulk_actions;
766        }
767
768        /**
769         * Handles users bulk actions
770         *
771         * @param string $redirect_to Bulk actions.
772         * @param string $doaction    Current action.
773         * @param array  $user_ids    User IDs to process.
774         *
775         * @return      string   Redirect URL.
776         * @package     userswp
777         *
778         */
779        public function handle_users_bulk_actions( $redirect_to, $doaction, $user_ids ) {
780                if ( 'uwp_resend' == $doaction ) {
781                        foreach ( $user_ids as $user_id ) {
782                                uwp_resend_activation_mail( $user_id );
783                        }
784
785                        $redirect_to = add_query_arg( 'update', 'uwp_resend', $redirect_to );
786                } elseif ( 'uwp_activate_user' == $doaction ) {
787                        foreach ( $user_ids as $user_id ) {
788                                $this->activate_user( $user_id );
789                        }
790                        $redirect_to = add_query_arg( 'update', 'uwp_activate_user', $redirect_to );
791                }
792
793                return $redirect_to;
794        }
795
796        /**
797         * Activates user
798         *
799         * @param int $user_id User ID
800         *
801         * @return bool
802         */
803        public function activate_user( $user_id = 0 ) {
804                if ( ! $user_id ) {
805                        return false;
806                }
807
808                $uwp_mode = get_user_meta( $user_id, 'uwp_mod', true );
809                if ( 'email_unconfirmed' == $uwp_mode ) {
810                        delete_user_meta( $user_id, 'uwp_mod' );
811                        do_action( 'uwp_email_activation_success', $user_id );
812                }
813
814                return true;
815        }
816
817        /**
818         * Processes user action
819         *
820         * @return      mixed
821         * @package     userswp
822         *
823         */
824        public function process_user_actions() {
825                $user_id = isset( $_REQUEST['user_id'] ) ? (int) $_REQUEST['user_id'] : 0;
826                $action  = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : false;
827                $nonce   = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( $_REQUEST['_nonce'] ) : false;
828                $is_admin   = isset( $_REQUEST['uwp_is_admin'] ) ? sanitize_text_field( $_REQUEST['uwp_is_admin'] ) : false;
829
830                if ( $user_id && 'uwp_resend' == $action && ! empty( $nonce ) && wp_verify_nonce( $nonce, 'uwp_resend' ) ) {
831                        uwp_resend_activation_mail( $user_id );
832                        if ( isset( $is_admin ) && $is_admin ) {
833                                wp_redirect( add_query_arg( 'update', 'uwp_resend', admin_url( 'users.php' ) ) );
834                                exit();
835                        } else {
836                                global $uwp_notices;
837                                $message       = __( 'Activation email has been sent!', 'userswp' );
838                                $uwp_notices[] = aui()->alert(
839                    array(
840                                                'type'    => 'success',
841                                                'content' => $message,
842                    )
843                );
844                        }
845                } elseif ( $user_id && 'uwp_activate_user' == $action && wp_verify_nonce( $nonce, 'uwp_activate_user' ) ) {
846                        if ( isset( $is_admin ) && $is_admin && current_user_can( 'edit_users' ) ) {
847                                $this->activate_user( $user_id );
848                                wp_redirect( add_query_arg( 'update', 'uwp_activate_user', admin_url( 'users.php' ) ) );
849                        }
850                }
851        }
852
853        /**
854         * Show row meta on the plugin screen.
855         *
856         * @param   mixed $links Plugin Row Meta
857         * @param   mixed $file  Plugin Base file
858         * @return  array
859         */
860        public static function plugin_row_meta( $links, $file ) {
861                if ( USERSWP_PLUGIN_BASENAME == $file ) {
862                        $row_meta = array(
863                                'docs'        => '<a href="' . esc_url( 'https://userswp.io/documentation/' ) . '" aria-label="' . esc_attr__( 'View UsersWP Documentation', 'userswp' ) . '">' . esc_html__( 'Docs', 'userswp' ) . '</a>',
864                                'support'     => '<a href="' . esc_url( 'https://userswp.io/support/' ) . '" aria-label="' . esc_attr__( 'Visit UsersWP support', 'userswp' ) . '">' . esc_html__( 'Support', 'userswp' ) . '</a>',
865                                'translation' => '<a href="' . esc_url( 'https://userswp.io/translate/projects' ) . '" aria-label="' . esc_attr__( 'View translations', 'userswp' ) . '">' . esc_html__( 'Translations', 'userswp' ) . '</a>',
866                        );
867
868                        return array_merge( $links, $row_meta );
869                }
870
871                return (array) $links;
872        }
873
874    /**
875     * Process the creation of a new registration form.
876     *
877     * This method handles the AJAX request to create a new registration form,
878     * including validation, sanitization, and database operations.
879     */
880    public function process_create_register_form() {
881        check_ajax_referer( 'uwp-create-register-form-nonce', 'uwp_create_register_form_nonce' );
882
883        if ( ! current_user_can( 'manage_options' ) ) {
884            wp_die( -1 );
885        }
886
887        $form_title = isset( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
888        $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
889        $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
890        $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
891        $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
892        $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
893        $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
894
895        if ( empty( $form_title ) ) {
896            wp_send_json_error(
897                                array(
898                                        'message' => esc_html__( 'Form title is required.', 'userswp' ),
899                                )
900                        );
901        }
902
903        $status = false;
904        $redirect = '';
905
906        $new_form_id = uwp_get_next_register_form_id();
907        if ( $new_form_id ) {
908            $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
909            // Filter out invalid items.
910            $register_forms = array_filter(
911                $register_forms,
912                function ( $form ) {
913                return isset( $form['id'] );
914                }
915            );
916
917            $register_forms[] = array(
918                'id'          => $new_form_id,
919                'title'       => ! empty( $form_title ) ? $form_title : sprintf( __( 'Form %d', 'userswp' ), $new_form_id ),
920                'slug'        => ! empty( $form_title ) ? sanitize_title( $form_title ) : sprintf( 'form-%d', $new_form_id ),
921                'user_role'   => $user_role,
922                'reg_action'  => $action,
923                'redirect_to' => $redirect_to,
924                'custom_url'  => $custom_url,
925                'gdpr_page'   => $gdpr_page,
926                'tos_page'    => $tos_page,
927            );
928
929            $register_forms = array_values( $register_forms );
930            $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $new_form_id );
931
932            uwp_update_option( 'multiple_registration_forms', $register_forms );
933
934            $fields = UsersWP_Activator::uwp_default_custom_fields_account();
935            $form_builder = new UsersWP_Form_Builder();
936
937            foreach ( $fields as $field ) {
938                $field['form_id'] = $new_form_id;
939                $form_builder->admin_form_field_save( $field );
940            }
941
942            UsersWP_Activator::insert_form_extras( $new_form_id );
943
944            do_action( 'uwp_create_register_form', $new_form_id );
945
946            $status = true;
947            $redirect = admin_url( sprintf( 'admin.php?page=uwp_user_types&form=%d', $new_form_id ) );
948        }
949
950        wp_send_json_success(
951            array(
952                                'status'   => $status,
953                'message'  => esc_html__( 'User Type added successfully.', 'userswp' ),
954                                'redirect' => $redirect,
955            )
956        );
957    }
958
959    /**
960     * Process the update of an existing registration form.
961     *
962     * This method handles the AJAX request to update an existing registration form,
963     * including validation, sanitization, and database operations.
964     */
965    public function process_update_register_form() {
966        check_ajax_referer( 'uwp-update-register-form-nonce', 'uwp_update_register_form_nonce' );
967
968        if ( ! current_user_can( 'manage_options' ) ) {
969            wp_die( -1 );
970        }
971
972        $form_id = isset( $_POST['manage_field_form_id'] ) ? (int)$_POST['manage_field_form_id'] : 0;
973        $form_title = isset( $_POST['form_title'] ) && ! empty( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
974        $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
975        $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
976        $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
977        $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
978        $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
979        $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
980
981        if ( ! $form_id ) {
982            wp_send_json_error(
983                                array(
984                                        'message' => esc_html__( 'Something went wrong. Please try again.', 'userswp' ),
985                                )
986                        );
987        }
988
989        $redirect = '';
990        $status = false;
991
992        $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
993
994        foreach ( $register_forms as &$register_form ) {
995            if ( $register_form['id'] == $form_id ) {
996                $register_form['title'] = $form_title;
997                $register_form['user_role'] = $user_role;
998                $register_form['reg_action'] = $action;
999                $register_form['redirect_to'] = $redirect_to;
1000                $register_form['custom_url'] = $custom_url;
1001                $register_form['gdpr_page'] = $gdpr_page;
1002                $register_form['tos_page'] = $tos_page;
1003                $status = true;
1004                break;
1005            }
1006        }
1007
1008        if ( $status ) {
1009            $register_forms = array_values( $register_forms );
1010            $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $form_id );
1011            uwp_update_option( 'multiple_registration_forms', $register_forms );
1012            $redirect = add_query_arg(
1013                array(
1014                    'page' => 'uwp_user_types',
1015                    'form' => $form_id,
1016                ),
1017                admin_url( 'admin.php' )
1018            );
1019        }
1020
1021        wp_send_json_success(
1022            array(
1023                                'status'  => $status,
1024                'message' => esc_html__( 'The user type has been updated successfully.', 'userswp' ),
1025            )
1026        );
1027    }
1028
1029    public function reorder_user_types() {
1030        check_ajax_referer( 'uwp_reorder_user_types', 'nonce' );
1031
1032        if ( ! current_user_can( 'manage_options' ) ) {
1033            wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'userswp' ) ) );
1034        }
1035
1036        $order = isset( $_POST['order'] ) ? array_map( 'absint', $_POST['order'] ) : array();
1037
1038        if ( empty( $order ) ) {
1039            wp_send_json_error( array( 'message' => __( 'Invalid order data.', 'userswp' ) ) );
1040        }
1041
1042        $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
1043        $new_order = array();
1044
1045        foreach ( $order as $id ) {
1046            foreach ( $register_forms as $form ) {
1047                if ( (int) $form['id'] === (int) $id ) {
1048                    $new_order[] = $form;
1049                    break;
1050                }
1051            }
1052        }
1053
1054        uwp_update_option( 'multiple_registration_forms', $new_order );
1055
1056        wp_send_json_success( array( 'message' => __( 'User types order updated successfully.', 'userswp' ) ) );
1057    }
1058
1059        public function process_remove_register_form() {
1060
1061                check_ajax_referer( 'uwp_delete_user_types', 'nonce' );
1062
1063                if ( ! current_user_can( 'manage_options' ) ) {
1064                        wp_die( -1 );
1065                }
1066
1067                $type    = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '';
1068                $form_id = ! empty( $_POST['form_id'] ) ? (int) $_POST['form_id'] : '';
1069
1070                $status   = false;
1071                $message  = __( 'Security nonce failed. Please try again.', 'userswp' );
1072                $redirect = '';
1073
1074                if ( ! empty( $type ) && ! empty( $form_id ) && $type === 'remove' ) {
1075                        $status = self::remove_registration_form( (int) $form_id );
1076                        $redirect = admin_url( 'admin.php?page=uwp_user_types' );
1077                }
1078
1079                wp_send_json(
1080                        array(
1081                                'status'   => $status,
1082                                'message'  => $message,
1083                                'redirect' => $redirect,
1084                        )
1085                );
1086        }
1087
1088        /**
1089     * Removes the registration form and its associated fields.
1090     *
1091     * @global wpdb $wpdb WordPress database abstraction object.
1092     *
1093     * @param int $form_id The form ID to remove.
1094     * @return bool True if the form was found and removed; false otherwise.
1095     */
1096    public static function remove_registration_form( int $form_id ) {
1097        global $wpdb;
1098
1099        $table_name     = uwp_get_table_prefix() . 'uwp_form_fields';
1100        $status         = false;
1101        $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
1102        $form_builder   = new UsersWP_Form_Builder();
1103
1104        if ( empty( $register_forms ) || ! is_array( $register_forms ) ) {
1105            return $status;
1106                }
1107
1108        foreach ( $register_forms as $key => $register_form ) {
1109            if ( empty( $register_form['id'] ) || (int) $register_form['id'] !== $form_id ) {
1110                continue;
1111            }
1112
1113            $status = true;
1114            unset( $register_forms[ $key ] );
1115
1116            $fields = $wpdb->get_results(
1117                $wpdb->prepare(
1118                    "SELECT id FROM {$table_name} WHERE form_type = %s AND form_id = %d ORDER BY sort_order ASC",
1119                    'account',
1120                    $form_id
1121                )
1122            );
1123
1124            if ( ! empty( $fields ) ) {
1125                foreach ( $fields as $field ) {
1126                    $form_builder->admin_form_field_delete( (int) $field->id, false, $form_id );
1127                }
1128            }
1129            }
1130
1131        $register_forms = array_values( $register_forms );
1132        uwp_update_option( 'multiple_registration_forms', $register_forms );
1133
1134        return $status;
1135    }
1136
1137        /**
1138         * Tell AyeCode UI to load on certain admin pages.
1139         *
1140         * @since 1.2.3.22
1141         *
1142         * @param array $screen_ids Screen IDs.
1143         * @return array Screen IDs.
1144         */
1145        public function add_aui_screens( $screen_ids ) {
1146                // Load on these pages if set
1147                if ( is_admin() && ! wp_doing_ajax() ) {
1148                        $screen_ids = array_merge( $screen_ids, uwp_get_screen_ids() );
1149                }
1150
1151                // AUI is also needed for setup wizard.
1152                $screen_ids[] = 'uwp-setup';
1153
1154                return $screen_ids;
1155        }
1156
1157        /**
1158         * Adds a new user type column to the users list table.
1159         *
1160         * @param array $columns Existing columns in the users list table.
1161         * @return array Modified columns array with the new user type column.
1162         */
1163        public function add_user_type_column( array $columns ) {
1164                $columns['uwp_user_type'] = __( 'User Type', 'userswp' );
1165                return $columns;
1166        }
1167
1168        /**
1169         * Displays user type in the user list column.
1170         *
1171         * @param string $value       Current column value.
1172         * @param string $column_name Column name being displayed.
1173         * @param int    $user_id     Current user ID.
1174         * @return string user type title.
1175         */
1176        public function display_user_type_column( $value, $column_name, $user_id ) {
1177                if ( 'uwp_user_type' === $column_name ) {
1178                        $form_id                = uwp_get_register_form_id( $user_id );
1179                        $uwp_user_type  = uwp_get_user_register_form( $form_id );
1180
1181                        if ( ! isset( $uwp_user_type['id'], $uwp_user_type['title'] ) ) {
1182                                return '&mdash;';
1183                        }
1184
1185                        return $uwp_user_type['title'];
1186                }
1187
1188                return $value;
1189        }
1190
1191        /**
1192         * Display user type options in the admin user edit screen.
1193         *
1194         * @param WP_User $user The WP user object.
1195         * @return void
1196         */
1197        public function display_admin_change_user_type( $user ) {
1198                if ( ! current_user_can( 'manage_options' ) ) {
1199                        return;
1200                }
1201
1202                $register_forms = UsersWP_User_Types::get_register_forms();
1203                $user_type_id   = (int) uwp_get_register_form_id( $user->ID );
1204
1205                ob_start();
1206                ?>
1207                <table class="form-table">
1208                        <tbody>
1209                                <tr>
1210                                        <th>
1211                                                <label for="uwp_user_type_id" class="fw-semibold">
1212                                                        <?php esc_html_e( 'User Type', 'userswp' ); ?>
1213                                                </label>
1214                                        </th>
1215                                        <td>
1216                                                <select name="uwp_user_type_id" id="uwp_user_type_id" class="regular-text">
1217                                                        <?php foreach ( $register_forms as $form ) : ?>
1218                                                                <?php 
1219                                                                $form_id = absint( $form['id'] ); 
1220                                                                $is_current = ( $form_id === $user_type_id );
1221                                                                ?>
1222                                                                <option value="<?php echo esc_attr( $form_id ); ?>" <?php selected( $form_id, $user_type_id ); ?>>
1223                                                                        <?php echo esc_html( $form['title'] ); ?><?php echo $is_current ? ' (' . esc_html__( 'Active', 'userswp' ) . ')' : ''; ?>
1224                                                                </option>
1225                                                        <?php endforeach; ?>
1226                                                </select>
1227                                                <p class="description">
1228                                                        <?php esc_html_e( "Select the user type for this account.", 'userswp' ); ?>
1229                                                </p>
1230                                        </td>
1231                                </tr>
1232                        </tbody>
1233                </table>
1234                <?php
1235
1236                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1237                echo apply_filters( 'uwp_admin_change_user_type_display', ob_get_clean(), $user );
1238        }
1239
1240        /**
1241         * Update user membership.
1242         *
1243         * @param int $user_id The ID of the user to update.
1244         * @return void
1245         */
1246        public function update_user_type( $user_id ) {
1247                if ( ! current_user_can( 'manage_options' ) ) {
1248                        return;
1249                }
1250
1251                check_admin_referer( 'update-user_' . $user_id );
1252
1253                if ( isset( $_POST['uwp_user_type_id'] ) && ! empty( $_POST['uwp_user_type_id'] ) ) {
1254                        $user_type_id = absint( $_POST['uwp_user_type_id'] );
1255
1256                        // Validate new membership.
1257                        $uwp_user_type = uwp_get_user_register_form( $user_type_id );
1258                        if ( ! $uwp_user_type ) {
1259                                return;
1260                        }
1261
1262                        update_user_meta( $user_id, '_uwp_register_form_id', $user_type_id );
1263                }
1264        }
1265
1266        /**
1267     * Adds a notice after updating user type.
1268     */
1269    public function add_user_type_updated_notice() {
1270        if ( isset( $_GET['user_type_updated'] ) && 'true' === $_GET['user_type_updated'] ) {
1271            ?>
1272            <div class="updated notice is-dismissible">
1273                <p><?php esc_html_e( 'User type updated successfully.', 'userswp' ); ?></p>
1274            </div>
1275            <?php
1276        }
1277    }
1278
1279    /**
1280     * Adds a bulk action dropdown for changing user types on the Users list table.
1281     *
1282     * @param string $which The position of the table (top or bottom).
1283     */
1284    public function add_bulk_user_type_dropdown( $which ) {
1285        if ( ! is_admin() || 'users' !== get_current_screen()->id || 'top' !== $which ) {
1286            return;
1287        }
1288
1289        $user_types = UsersWP_User_Types::get_register_forms();
1290
1291        if ( empty( $user_types ) ) {
1292            return;
1293        }
1294
1295        $options = array_map( function( $user_type ) {
1296            return sprintf(
1297                '<option value="%d">%s</option>',
1298                absint( $user_type['id'] ),
1299                esc_html( $user_type['title'] )
1300            );
1301        }, $user_types );
1302
1303                ob_start();
1304        ?>
1305        <div class="alignleft actions">
1306            <label class="screen-reader-text" for="uwp_new_user_type">
1307                <?php esc_html_e( 'Change user type to…', 'userswp' ); ?>
1308            </label>
1309            <select name="uwp_new_user_type" id="uwp_new_user_type">
1310                <option value=""><?php esc_html_e( 'Change user type to…', 'userswp' ); ?></option>
1311                <?php echo implode( '', $options ); ?>
1312            </select>
1313            <input type="submit" name="uwp_change_user_type" id="uwp_change_user_type" class="button" value="<?php esc_attr_e( 'Change', 'userswp' ); ?>">
1314        </div>
1315        <?php
1316
1317                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1318                echo apply_filters( 'uwp_bulk_change_user_type_display', ob_get_clean() );
1319    }
1320
1321    /**
1322     * Handles the bulk user type change.
1323     */
1324    public function handle_bulk_user_type_change() {
1325        if ( ! isset( $_GET['uwp_change_user_type'], $_GET['uwp_new_user_type'] ) ) {
1326            return;
1327        }
1328
1329        if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'bulk-users' ) ) {
1330            wp_die( __( 'Security check failed. Please try again.' ) );
1331        }
1332
1333        if ( ! current_user_can( 'edit_users' ) ) {
1334            wp_die( __( 'You do not have permission to perform this action.' ) );
1335        }
1336
1337        $new_user_type = absint( $_GET['uwp_new_user_type'] );
1338
1339        if ( ! $new_user_type ) {
1340            return;
1341        }
1342
1343                $users = isset( $_REQUEST['users'] ) && ! empty( $_REQUEST['users'] ) ? (array) $_REQUEST['users'] : array();
1344
1345        if ( ! empty( $users ) ) {
1346            array_map( function( $user_id ) use ( $new_user_type ) {
1347                update_user_meta( absint( $user_id ), '_uwp_register_form_id', (int) $new_user_type );
1348            }, $users );
1349        } 
1350
1351        wp_safe_redirect( add_query_arg( 'user_type_updated', 'true', admin_url( 'users.php' ) ) );
1352        exit;
1353    }
1354
1355    /**
1356     * Adds JavaScript validation script.
1357     */
1358    public function add_validation_script() {
1359                $screen = get_current_screen();
1360
1361                if ( 'users' !== $screen->id ) {
1362                        return;
1363                }
1364        ?>
1365        <script type="text/javascript">
1366        (function($) {
1367            $(document).ready(function() {
1368                                var $userTypeErrorNotice = $(
1369                                        '<div id="no-user-type-selected" class="notice notice-error is-dismissible" style="display:none;">' +
1370                                                '<p><?php esc_html_e( "Please select a user type to perform this action.", "userswp" ); ?></p>' +
1371                                                '<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( "Dismiss this notice.", "userswp" ); ?></span></button>' +
1372                                        '</div>',
1373                                );
1374
1375                                var $itemSelectionErrorNotice = $(
1376                                        '<div id="no-item-selected" class="notice notice-error is-dismissible" style="display:none;">' +
1377                                                '<p><?php esc_html_e( "Please select at least one item to perform this action on.", "userswp" ); ?></p>' +
1378                                                '<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( "Dismiss this notice.", "userswp" ); ?></span></button>' +
1379                                        '</div>',
1380                                );
1381
1382                                $("#wpbody-content").prepend($userTypeErrorNotice, $itemSelectionErrorNotice)
1383
1384                                $("#uwp_change_user_type").on("click", (e) => {
1385                                        if ($('input[name="users[]"]:checked').length === 0) {
1386                                                e.preventDefault();
1387                                                showErrorNotice($itemSelectionErrorNotice);
1388                                                hideErrorNotice($userTypeErrorNotice);
1389                                        } else if ($("#new_user_type").val() === "") {
1390                                                e.preventDefault();
1391                                                showErrorNotice($userTypeErrorNotice);
1392                                                hideErrorNotice($itemSelectionErrorNotice);
1393                                        } else {
1394                                                hideErrorNotice($userTypeErrorNotice);
1395                                                hideErrorNotice($itemSelectionErrorNotice);
1396                                        }
1397                                });
1398
1399                                $(".notice-dismiss").on("click", function () {
1400                                        hideErrorNotice($(this).closest(".notice"));
1401                                })
1402
1403                                function showErrorNotice($notice) {
1404                                        $notice.fadeIn(300);
1405                                }
1406
1407                                function hideErrorNotice($notice) {
1408                                        $notice.fadeOut(300);
1409                                }
1410            });
1411        })(jQuery);
1412        </script>
1413        <?php
1414    }
1415}
Note: See TracBrowser for help on using the repository browser.