| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Formidable_Forms\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_formidable_forms_ajax_get_forms() { |
| 17 |
// Security check |
| 18 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 19 |
|
| 20 |
// Permissions check |
| 21 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 22 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
$results = array(); |
| 28 |
|
| 29 |
// Pull back the search string |
| 30 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; |
| 31 |
|
| 32 |
// Get the forms |
| 33 |
$forms = FrmForm::getAll(); |
| 34 |
|
| 35 |
foreach( $forms as $form ) { |
| 36 |
|
| 37 |
if( ! empty( $search ) ) { |
| 38 |
if( strpos( strtolower( $form->name ), strtolower( $search ) ) === false ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Results should meet the Select2 structure |
| 44 |
$results[] = array( |
| 45 |
'id' => $form->id, |
| 46 |
'text' => $form->name, |
| 47 |
); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
// Prepend option none |
| 52 |
$results = automatorwp_ajax_get_ajax_results_option_none( $results ); |
| 53 |
|
| 54 |
// Return our results |
| 55 |
wp_send_json_success( $results ); |
| 56 |
die; |
| 57 |
|
| 58 |
} |
| 59 |
add_action( 'wp_ajax_automatorwp_formidable_forms_get_forms', 'automatorwp_formidable_forms_ajax_get_forms', 5 ); |