| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\ARForms\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_arforms_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( 'arforms_form', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 33 |
$results = array(); |
| 34 |
|
| 35 |
if( gamipress_is_network_wide_active() ) { |
| 36 |
|
| 37 |
// Look for results on all sites on a multisite install |
| 38 |
|
| 39 |
foreach( gamipress_get_network_site_ids() as $site_id ) { |
| 40 |
|
| 41 |
// Switch to site |
| 42 |
switch_to_blog( $site_id ); |
| 43 |
|
| 44 |
// Get the current site name to append it to results |
| 45 |
$site_name = get_bloginfo( 'name' ); |
| 46 |
|
| 47 |
// Check if plugin is active on this site |
| 48 |
if( class_exists('arfliteformcontroller') || class_exists('arformcontroller')) { |
| 49 |
|
| 50 |
// Get the forms |
| 51 |
$sql_query = gamipress_arforms_get_query_forms(); |
| 52 |
$site_forms = $wpdb->get_results( $sql_query ); |
| 53 |
|
| 54 |
foreach ( $site_forms as $form ) { |
| 55 |
|
| 56 |
// Results should meet same structure like posts |
| 57 |
$results[] = array( |
| 58 |
'ID' => $form->id, |
| 59 |
'post_title' => $form->name, |
| 60 |
'site_id' => $site_id, |
| 61 |
'site_name' => $site_name, |
| 62 |
); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
// Restore current site |
| 69 |
restore_current_blog(); |
| 70 |
|
| 71 |
} |
| 72 |
} else { |
| 73 |
|
| 74 |
// Get the forms |
| 75 |
$sql_query = gamipress_arforms_get_query_forms(); |
| 76 |
$forms = $wpdb->get_results( $sql_query ); |
| 77 |
|
| 78 |
foreach( $forms as $form ) { |
| 79 |
$results[] = array( |
| 80 |
'id' => $form->id, |
| 81 |
'text' => $form->name, |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
// Return our results |
| 88 |
wp_send_json_success( $results ); |
| 89 |
die; |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_arforms_ajax_get_posts', 5 ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Get plugin version |
| 97 |
* |
| 98 |
* @since 1.0.0 |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
function gamipress_arforms_get_plugin_version() { |
| 103 |
|
| 104 |
global $wpdb; |
| 105 |
|
| 106 |
// Check installed versions |
| 107 |
if( is_plugin_active( 'arforms-form-builder/arforms-form-builder.php' ) && is_plugin_active( 'arforms/arforms.php' ) ) { |
| 108 |
$version = "pro"; |
| 109 |
} elseif( !is_plugin_active( 'arforms-form-builder/arforms-form-builder.php' ) && is_plugin_active( 'arforms/arforms.php' ) ) { |
| 110 |
$version = "only_pro"; |
| 111 |
} |
| 112 |
else { |
| 113 |
$version = "lite"; |
| 114 |
} |
| 115 |
|
| 116 |
return $version; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get query for forms |
| 122 |
* |
| 123 |
* @since 1.0.0 |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
function gamipress_arforms_get_query_forms() { |
| 128 |
|
| 129 |
global $wpdb; |
| 130 |
|
| 131 |
$version = gamipress_arforms_get_plugin_version(); |
| 132 |
|
| 133 |
// Check installed versions to get table |
| 134 |
if( $version === "pro" ) { |
| 135 |
$table_name = "{$wpdb->prefix}arf_forms"; |
| 136 |
$sql_query = $wpdb->prepare( "SELECT id, name FROM {$table_name} WHERE is_template = 0" ); |
| 137 |
} elseif ( $version === "only_pro" ) { |
| 138 |
$table_name = "{$wpdb->prefix}arf_forms"; |
| 139 |
$sql_query = $wpdb->prepare( "SELECT id, name FROM {$table_name} WHERE is_template = 0 AND arf_is_lite_form = 0" ); |
| 140 |
} |
| 141 |
else { |
| 142 |
$table_name = "{$wpdb->prefix}arflite_forms"; |
| 143 |
$sql_query = "SELECT id, name FROM {$table_name}"; |
| 144 |
} |
| 145 |
|
| 146 |
return $sql_query; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get form name |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
function gamipress_arforms_get_form_name( $form_id ) { |
| 158 |
|
| 159 |
global $wpdb; |
| 160 |
|
| 161 |
$version = gamipress_arforms_get_plugin_version(); |
| 162 |
|
| 163 |
// Check installed versions to get table |
| 164 |
if( $version === "pro" || $version === "only_pro" ) { |
| 165 |
$table_name = "{$wpdb->prefix}arf_forms"; |
| 166 |
} else { |
| 167 |
$table_name = "{$wpdb->prefix}arflite_forms"; |
| 168 |
} |
| 169 |
|
| 170 |
$sql_query = $wpdb->prepare( "SELECT name FROM $table_name WHERE id = %d", $form_id ); |
| 171 |
$form_name = $wpdb->get_var( $sql_query ); |
| 172 |
|
| 173 |
return $form_name; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get form fields values |
| 179 |
* |
| 180 |
* @since 1.0.0 |
| 181 |
* |
| 182 |
* @param array $field_data |
| 183 |
* @param string $field_name |
| 184 |
* @param string $field_value |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
function gamipress_arforms_get_form_fields_values( $form_id, $item_meta_values ) { |
| 189 |
|
| 190 |
global $wpdb; |
| 191 |
|
| 192 |
$form_fields = array(); |
| 193 |
|
| 194 |
$version = gamipress_arforms_get_plugin_version(); |
| 195 |
|
| 196 |
// Check installed versions to get table |
| 197 |
if( $version === "pro" || $version === "only_pro" ) { |
| 198 |
$table_name = "{$wpdb->prefix}arf_fields"; |
| 199 |
} else { |
| 200 |
$table_name = "{$wpdb->prefix}arflite_fields"; |
| 201 |
} |
| 202 |
|
| 203 |
$sql_query = $wpdb->prepare( "SELECT id, name FROM $table_name WHERE form_id = %d", $form_id ); |
| 204 |
$field_data = $wpdb->get_results( $sql_query ); |
| 205 |
|
| 206 |
foreach( $field_data as $field ) { |
| 207 |
|
| 208 |
$name = $field->name; |
| 209 |
$value = $item_meta_values[$field->id]; |
| 210 |
|
| 211 |
$form_fields[$name] = $value; |
| 212 |
} |
| 213 |
|
| 214 |
return $form_fields; |
| 215 |
|
| 216 |
} |