| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Brizy\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_brizy_ajax_get_posts() { |
| 18 |
|
| 19 |
// Security check, forces to die if not security passed |
| 20 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 21 |
|
| 22 |
// Check if user can manage GamiPress |
| 23 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 24 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'brizy_form', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$results = array(); |
| 33 |
|
| 34 |
$forms = gamipress_brizy_get_forms(); |
| 35 |
|
| 36 |
foreach ( $forms as $form ) { |
| 37 |
|
| 38 |
// Results should meet same structure like posts |
| 39 |
$results[] = array( |
| 40 |
'ID' => $form['id'], |
| 41 |
'post_title' => $form['name'], |
| 42 |
); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
// Return our results |
| 47 |
wp_send_json_success( $results ); |
| 48 |
die; |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_brizy_ajax_get_posts', 5 ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Get all forms |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @return string|null |
| 60 |
*/ |
| 61 |
function gamipress_brizy_get_forms( ) { |
| 62 |
|
| 63 |
global $wpdb; |
| 64 |
|
| 65 |
$all_forms = array(); |
| 66 |
|
| 67 |
$forms = $wpdb->get_results( |
| 68 |
$wpdb->prepare( |
| 69 |
"SELECT ID, post_title, post_content |
| 70 |
FROM {$wpdb->posts} |
| 71 |
WHERE post_content LIKE %s |
| 72 |
AND post_status = 'publish' |
| 73 |
AND post_type IN ('post', 'page')", |
| 74 |
'%' . $wpdb->esc_like('data-brz-form-id') . '%' |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
foreach ( $forms as $post ) { |
| 79 |
|
| 80 |
$form_title = $post->post_title; |
| 81 |
$form_content = $post->post_content; |
| 82 |
|
| 83 |
preg_match_all( '/data-brz-form-id="([^"]+)"/', $form_content, $matches ); |
| 84 |
$form_ids = array_unique($matches[1]); |
| 85 |
|
| 86 |
foreach ( $form_ids as $id ) { |
| 87 |
$all_forms[] = array( |
| 88 |
'id' => $id, |
| 89 |
'name' => $form_title . ' - ' . $id, |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
return $all_forms; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the form title |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @param int $form_id |
| 105 |
* |
| 106 |
* @return string|null |
| 107 |
*/ |
| 108 |
function gamipress_brizy_get_form_title( $form_id ) { |
| 109 |
|
| 110 |
global $wpdb; |
| 111 |
|
| 112 |
// Empty title if no ID provided |
| 113 |
if( empty( $form_id ) ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
$post_title = $wpdb->get_var( |
| 118 |
$wpdb->prepare( |
| 119 |
"SELECT post_title |
| 120 |
FROM {$wpdb->posts} |
| 121 |
WHERE post_content LIKE %s |
| 122 |
AND post_status = 'publish' |
| 123 |
AND post_type IN ('post', 'page') |
| 124 |
LIMIT 1", |
| 125 |
'%' . $wpdb->esc_like($form_id) . '%' |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
return $post_title . ' - ' . $form_id; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get form fields values |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
* |
| 138 |
* @param array $fields |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
function gamipress_brizy_get_form_fields_values( $fields ) { |
| 143 |
|
| 144 |
$form_fields = array(); |
| 145 |
|
| 146 |
// Loop all fields |
| 147 |
foreach ( $fields as $field_name => $field_value ) { |
| 148 |
|
| 149 |
$field_name = $field_value->label; |
| 150 |
$value = ( isset( $field_value->value ) ? $field_value->value : '' ); |
| 151 |
|
| 152 |
$form_fields[$field_name] = $value; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
return $form_fields; |
| 157 |
|
| 158 |
} |