| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\BBForms\Ajax_Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Ajax function for selecting forms |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
function automatorwp_bbforms_ajax_get_forms() { |
| 17 |
|
| 18 |
// Security check |
| 19 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 20 |
|
| 21 |
// Permissions check |
| 22 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 23 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
$results = automatorwp_bbforms_get_forms( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' ); |
| 27 |
|
| 28 |
// Prepend option none |
| 29 |
$results = automatorwp_ajax_get_ajax_results_option_none( $results ); |
| 30 |
|
| 31 |
// Return our results |
| 32 |
wp_send_json_success( $results ); |
| 33 |
die; |
| 34 |
|
| 35 |
} |
| 36 |
add_action( 'wp_ajax_automatorwp_bbforms_get_forms', 'automatorwp_bbforms_ajax_get_forms', 5 ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Function for selecting forms |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* |
| 43 |
* @param string $search |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
function automatorwp_bbforms_get_forms( $search = '' ) { |
| 48 |
|
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
$search = $wpdb->esc_like( $search ); |
| 52 |
|
| 53 |
$results = array(); |
| 54 |
|
| 55 |
// Setup table |
| 56 |
$ct_table = ct_setup_table( 'bbforms_forms' ); |
| 57 |
|
| 58 |
$forms = $wpdb->get_results( $wpdb->prepare( |
| 59 |
"SELECT id, title |
| 60 |
FROM {$ct_table->db->table_name} |
| 61 |
WHERE title LIKE %s", |
| 62 |
"%%{$search}%%" |
| 63 |
) ); |
| 64 |
|
| 65 |
ct_reset_setup_table(); |
| 66 |
|
| 67 |
foreach( $forms as $form ) { |
| 68 |
|
| 69 |
if( $form->title === '' ) $form->title = '(no title)'; |
| 70 |
|
| 71 |
$results[] = array( |
| 72 |
'id' => $form->id, |
| 73 |
'text' => $form->title, |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
return $results; |
| 78 |
|
| 79 |
} |
| 80 |
|