LLMS_AJAX_Handler::check_voucher_duplicate()

Determines if voucher codes already exist.


Return Return

(void)


Top ↑

Source Source

File: includes/class.llms.ajax.handler.php

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
public static function check_voucher_duplicate() {
 
    $post_id = ! empty( $_REQUEST['postId'] ) ? absint( llms_filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT ) ) : 0;
    $codes   = ! empty( $_REQUEST['codes'] ) ? llms_filter_input_sanitize_string( INPUT_POST, 'codes', array( FILTER_REQUIRE_ARRAY ) ) : array();
 
    if ( ! $post_id || ! $codes ) {
        return new WP_Error( 400, __( 'Missing required parameters', 'lifterlms' ) );
    } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
        return new WP_Error( 401, __( 'Missing required permissions to perform this action.', 'lifterlms' ) );
    }
 
    $codes = implode(
        ',',
        array_map(
            function( $code ) {
                return sprintf( "'%s'", esc_sql( $code ) );
            },
            array_filter( $codes )
        )
    );
 
    global $wpdb;
    $table = $wpdb->prefix . 'lifterlms_vouchers_codes';
    $res   = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT code FROM $table WHERE code IN( $codes ) AND voucher_id != %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
            array( $post_id )
        ),
        ARRAY_A
    );
 
    wp_send_json(
        array(
            'success'    => true,
            'duplicates' => $res,
        )
    );
    wp_die();
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.