| Total Complexity | 180 |
| Total Lines | 851 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FrmForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmForm { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @return int|boolean id on success or false on failure |
||
| 10 | */ |
||
| 11 | public static function create( $values ) { |
||
| 12 | global $wpdb; |
||
| 13 | |||
| 14 | $new_values = array( |
||
| 15 | 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), |
||
| 16 | 'name' => $values['name'], |
||
| 17 | 'description' => $values['description'], |
||
| 18 | 'status' => isset($values['status']) ? $values['status'] : 'draft', |
||
| 19 | 'logged_in' => isset($values['logged_in']) ? $values['logged_in'] : 0, |
||
| 20 | 'is_template' => isset($values['is_template']) ? (int) $values['is_template'] : 0, |
||
| 21 | 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0, |
||
|
|
|||
| 22 | 'editable' => isset($values['editable']) ? (int) $values['editable'] : 0, |
||
| 23 | 'default_template' => isset($values['default_template']) ? (int) $values['default_template'] : 0, |
||
| 24 | 'created_at' => isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1), |
||
| 25 | ); |
||
| 26 | |||
| 27 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
||
| 28 | FrmFormsHelper::fill_form_options( $options, $values ); |
||
| 29 | |||
| 30 | $options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before'); |
||
| 31 | $options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after'); |
||
| 32 | $options['submit_html'] = isset($values['options']['submit_html']) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit'); |
||
| 33 | |||
| 34 | $options = apply_filters('frm_form_options_before_update', $options, $values); |
||
| 35 | $new_values['options'] = serialize($options); |
||
| 36 | |||
| 37 | //if(isset($values['id']) && is_numeric($values['id'])) |
||
| 38 | // $new_values['id'] = $values['id']; |
||
| 39 | |||
| 40 | $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
||
| 41 | |||
| 42 | $id = $wpdb->insert_id; |
||
| 43 | |||
| 44 | // Clear form caching |
||
| 45 | self::clear_form_cache(); |
||
| 46 | |||
| 47 | return $id; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return int|boolean ID on success or false on failure |
||
| 52 | */ |
||
| 53 | public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { |
||
| 54 | global $wpdb; |
||
| 55 | |||
| 56 | $values = self::getOne( $id, $blog_id ); |
||
| 57 | if ( ! $values ) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | $new_key = $copy_keys ? $values->form_key : ''; |
||
| 62 | |||
| 63 | $new_values = array( |
||
| 64 | 'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ), |
||
| 65 | 'name' => $values->name, |
||
| 66 | 'description' => $values->description, |
||
| 67 | 'status' => $template ? 'published' : 'draft', |
||
| 68 | 'logged_in' => $values->logged_in ? $values->logged_in : 0, |
||
| 69 | 'editable' => $values->editable ? $values->editable : 0, |
||
| 70 | 'created_at' => current_time('mysql', 1), |
||
| 71 | 'is_template' => $template ? 1 : 0, |
||
| 72 | ); |
||
| 73 | |||
| 74 | if ( $blog_id ) { |
||
| 75 | $new_values['status'] = 'published'; |
||
| 76 | $new_options = maybe_unserialize($values->options); |
||
| 77 | $new_options['email_to'] = get_option('admin_email'); |
||
| 78 | $new_options['copy'] = false; |
||
| 79 | $new_values['options'] = $new_options; |
||
| 80 | } else { |
||
| 81 | $new_values['options'] = $values->options; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( is_array($new_values['options']) ) { |
||
| 85 | $new_values['options'] = serialize($new_values['options']); |
||
| 86 | } |
||
| 87 | |||
| 88 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
||
| 89 | |||
| 90 | if ( $query_results ) { |
||
| 91 | // Clear form caching |
||
| 92 | self::clear_form_cache(); |
||
| 93 | |||
| 94 | $form_id = $wpdb->insert_id; |
||
| 95 | FrmField::duplicate($id, $form_id, $copy_keys, $blog_id); |
||
| 96 | |||
| 97 | // update form settings after fields are created |
||
| 98 | do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) ); |
||
| 99 | return $form_id; |
||
| 100 | } |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | public static function after_duplicate( $form_id, $values ) { |
||
| 106 | $new_opts = maybe_unserialize( $values['options'] ); |
||
| 107 | $values['options'] = $new_opts; |
||
| 108 | |||
| 109 | if ( isset($new_opts['success_msg']) ) { |
||
| 110 | $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']); |
||
| 111 | } |
||
| 112 | |||
| 113 | $new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id); |
||
| 114 | |||
| 115 | if ( $new_opts != $values['options'] ) { |
||
| 116 | global $wpdb; |
||
| 117 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return int|boolean |
||
| 123 | */ |
||
| 124 | public static function update( $id, $values, $create_link = false ) { |
||
| 125 | global $wpdb; |
||
| 126 | |||
| 127 | if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
||
| 128 | $values['status'] = 'published'; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( isset($values['form_key']) ) { |
||
| 132 | $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
||
| 136 | |||
| 137 | $new_values = self::set_update_options( array(), $values); |
||
| 138 | |||
| 139 | foreach ( $values as $value_key => $value ) { |
||
| 140 | if ( $value_key && in_array( $value_key, $form_fields ) ) { |
||
| 141 | $new_values[ $value_key ] = $value; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { |
||
| 146 | $new_values['status'] = $values['new_status']; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ( ! empty( $new_values ) ) { |
||
| 150 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
||
| 151 | if ( $query_results ) { |
||
| 152 | self::clear_form_cache(); |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | $query_results = true; |
||
| 156 | } |
||
| 157 | unset($new_values); |
||
| 158 | |||
| 159 | $values = self::update_fields($id, $values); |
||
| 160 | |||
| 161 | do_action( 'frm_update_form', $id, $values ); |
||
| 162 | do_action( 'frm_update_form_' . $id, $values ); |
||
| 163 | |||
| 164 | return $query_results; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public static function set_update_options( $new_values, $values ) { |
||
| 171 | if ( ! isset($values['options']) ) { |
||
| 172 | return $new_values; |
||
| 173 | } |
||
| 174 | |||
| 175 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
||
| 176 | FrmFormsHelper::fill_form_options( $options, $values ); |
||
| 177 | |||
| 178 | $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0; |
||
| 179 | $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); |
||
| 180 | $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); |
||
| 181 | $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); |
||
| 182 | |||
| 183 | $options = apply_filters( 'frm_form_options_before_update', $options, $values ); |
||
| 184 | $new_values['options'] = serialize( $options ); |
||
| 185 | |||
| 186 | return $new_values; |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public static function update_fields( $id, $values ) { |
||
| 194 | |||
| 195 | if ( ! isset($values['item_meta']) && ! isset($values['field_options']) ) { |
||
| 196 | return $values; |
||
| 197 | } |
||
| 198 | |||
| 199 | $all_fields = FrmField::get_all_for_form($id); |
||
| 200 | if ( empty($all_fields) ) { |
||
| 201 | return $values; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ( ! isset($values['item_meta']) ) { |
||
| 205 | $values['item_meta'] = array(); |
||
| 206 | } |
||
| 207 | |||
| 208 | $field_array = array(); |
||
| 209 | $existing_keys = array_keys($values['item_meta']); |
||
| 210 | foreach ( $all_fields as $fid ) { |
||
| 211 | if ( ! in_array($fid->id, $existing_keys) && ( isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted']) ) || isset($values['options']) ) { |
||
| 212 | $values['item_meta'][ $fid->id ] = ''; |
||
| 213 | } |
||
| 214 | $field_array[ $fid->id ] = $fid; |
||
| 215 | } |
||
| 216 | unset($all_fields); |
||
| 217 | |||
| 218 | foreach ( $values['item_meta'] as $field_id => $default_value ) { |
||
| 219 | if ( isset( $field_array[ $field_id ] ) ) { |
||
| 220 | $field = $field_array[ $field_id ]; |
||
| 221 | } else { |
||
| 222 | $field = FrmField::getOne($field_id); |
||
| 223 | } |
||
| 224 | |||
| 225 | if ( ! $field ) { |
||
| 226 | continue; |
||
| 227 | } |
||
| 228 | |||
| 229 | $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
||
| 230 | if ( $is_settings_page ) { |
||
| 231 | self::get_settings_page_html( $values, $field ); |
||
| 232 | |||
| 233 | if ( ! defined( 'WP_IMPORTING' ) ) { |
||
| 234 | continue; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | //updating the form |
||
| 239 | $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field ); |
||
| 240 | unset( $update_options['custom_html'] ); // don't check for POST html |
||
| 241 | $update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
||
| 242 | |||
| 243 | foreach ( $update_options as $opt => $default ) { |
||
| 244 | $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default; |
||
| 245 | if ( is_string( $field->field_options[ $opt ] ) ) { |
||
| 246 | $field->field_options[ $opt ] = trim( sanitize_text_field( $field->field_options[ $opt ] ) ); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); |
||
| 251 | $default_value = maybe_serialize( $values['item_meta'][ $field_id ] ); |
||
| 252 | |||
| 253 | $new_field = array( |
||
| 254 | 'field_options' => $field->field_options, |
||
| 255 | 'default_value' => $default_value, |
||
| 256 | ); |
||
| 257 | |||
| 258 | self::prepare_field_update_values( $field, $values, $new_field ); |
||
| 259 | |||
| 260 | FrmField::update( $field_id, $new_field ); |
||
| 261 | |||
| 262 | FrmField::delete_form_transient($field->form_id); |
||
| 263 | } |
||
| 264 | self::clear_form_cache(); |
||
| 265 | |||
| 266 | return $values; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * updating the settings page |
||
| 271 | */ |
||
| 272 | private static function get_settings_page_html( $values, &$field ) { |
||
| 273 | if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) { |
||
| 274 | $prev_opts = array(); |
||
| 275 | $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ); |
||
| 276 | $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html; |
||
| 277 | } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
||
| 278 | $prev_opts = $field->field_options; |
||
| 279 | } |
||
| 280 | |||
| 281 | if ( isset( $prev_opts ) ) { |
||
| 282 | $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values ); |
||
| 283 | if ( $prev_opts != $field->field_options ) { |
||
| 284 | FrmField::update( $field->id, array( 'field_options' => $field->field_options ) ); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | private static function prepare_field_update_values( $field, $values, &$new_field ) { |
||
| 290 | $field_cols = array( |
||
| 291 | 'field_key' => '', |
||
| 292 | 'required' => false, |
||
| 293 | 'type' => '', |
||
| 294 | 'description' => '', |
||
| 295 | 'options' => '', |
||
| 296 | 'name' => '', |
||
| 297 | ); |
||
| 298 | foreach ( $field_cols as $col => $default ) { |
||
| 299 | $default = ( $default === '' ) ? $field->{$col} : $default; |
||
| 300 | $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $status |
||
| 306 | * @return int|boolean |
||
| 307 | */ |
||
| 308 | public static function set_status( $id, $status ) { |
||
| 309 | if ( 'trash' == $status ) { |
||
| 310 | return self::trash($id); |
||
| 311 | } |
||
| 312 | |||
| 313 | $statuses = array( 'published', 'draft', 'trash' ); |
||
| 314 | if ( ! in_array( $status, $statuses ) ) { |
||
| 315 | return false; |
||
| 316 | } |
||
| 317 | |||
| 318 | global $wpdb; |
||
| 319 | |||
| 320 | if ( is_array($id) ) { |
||
| 321 | $where = array( |
||
| 322 | 'id' => $id, |
||
| 323 | 'parent_form_id' => $id, |
||
| 324 | 'or' => 1, |
||
| 325 | ); |
||
| 326 | FrmDb::get_where_clause_and_values( $where ); |
||
| 327 | array_unshift( $where['values'], $status ); |
||
| 328 | |||
| 329 | $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); |
||
| 330 | } else { |
||
| 331 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
||
| 332 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
||
| 333 | } |
||
| 334 | |||
| 335 | if ( $query_results ) { |
||
| 336 | self::clear_form_cache(); |
||
| 337 | } |
||
| 338 | |||
| 339 | return $query_results; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return int|boolean |
||
| 344 | */ |
||
| 345 | public static function trash( $id ) { |
||
| 346 | if ( ! EMPTY_TRASH_DAYS ) { |
||
| 347 | return self::destroy( $id ); |
||
| 348 | } |
||
| 349 | |||
| 350 | $form = self::getOne($id); |
||
| 351 | if ( ! $form ) { |
||
| 352 | return false; |
||
| 353 | } |
||
| 354 | |||
| 355 | $options = $form->options; |
||
| 356 | $options['trash_time'] = time(); |
||
| 357 | |||
| 358 | global $wpdb; |
||
| 359 | $query_results = $wpdb->update( |
||
| 360 | $wpdb->prefix . 'frm_forms', |
||
| 361 | array( |
||
| 362 | 'status' => 'trash', |
||
| 363 | 'options' => serialize( $options ), |
||
| 364 | ), |
||
| 365 | array( |
||
| 366 | 'id' => $id, |
||
| 367 | ) |
||
| 368 | ); |
||
| 369 | |||
| 370 | $wpdb->update( |
||
| 371 | $wpdb->prefix . 'frm_forms', |
||
| 372 | array( |
||
| 373 | 'status' => 'trash', |
||
| 374 | 'options' => serialize( $options ), |
||
| 375 | ), |
||
| 376 | array( |
||
| 377 | 'parent_form_id' => $id, |
||
| 378 | ) |
||
| 379 | ); |
||
| 380 | |||
| 381 | if ( $query_results ) { |
||
| 382 | self::clear_form_cache(); |
||
| 383 | } |
||
| 384 | |||
| 385 | return $query_results; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return int|boolean |
||
| 390 | */ |
||
| 391 | public static function destroy( $id ) { |
||
| 392 | global $wpdb; |
||
| 393 | |||
| 394 | $form = self::getOne($id); |
||
| 395 | if ( ! $form ) { |
||
| 396 | return false; |
||
| 397 | } |
||
| 398 | $id = $form->id; |
||
| 399 | |||
| 400 | // Disconnect the entries from this form |
||
| 401 | $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
||
| 402 | foreach ( $entries as $entry_id ) { |
||
| 403 | FrmEntry::destroy($entry_id); |
||
| 404 | unset($entry_id); |
||
| 405 | } |
||
| 406 | |||
| 407 | // Disconnect the fields from this form |
||
| 408 | $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); |
||
| 409 | |||
| 410 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
||
| 411 | if ( $query_results ) { |
||
| 412 | // Delete all form actions linked to this form |
||
| 413 | $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
||
| 414 | $action_control->destroy($id, 'all'); |
||
| 415 | |||
| 416 | // Clear form caching |
||
| 417 | self::clear_form_cache(); |
||
| 418 | |||
| 419 | do_action( 'frm_destroy_form', $id ); |
||
| 420 | do_action( 'frm_destroy_form_' . $id ); |
||
| 421 | } |
||
| 422 | |||
| 423 | return $query_results; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Delete trashed forms based on how long they have been trashed |
||
| 428 | * @return int The number of forms deleted |
||
| 429 | */ |
||
| 430 | public static function scheduled_delete( $delete_timestamp = '' ) { |
||
| 431 | global $wpdb; |
||
| 432 | |||
| 433 | $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); |
||
| 434 | |||
| 435 | if ( ! $trash_forms ) { |
||
| 436 | return; |
||
| 437 | } |
||
| 438 | |||
| 439 | if ( empty( $delete_timestamp ) ) { |
||
| 440 | $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
||
| 441 | } |
||
| 442 | |||
| 443 | $count = 0; |
||
| 444 | foreach ( $trash_forms as $form ) { |
||
| 445 | $form->options = maybe_unserialize( $form->options ); |
||
| 446 | if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
||
| 447 | self::destroy( $form->id ); |
||
| 448 | $count++; |
||
| 449 | } |
||
| 450 | |||
| 451 | unset( $form ); |
||
| 452 | } |
||
| 453 | return $count; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return string form name |
||
| 458 | */ |
||
| 459 | public static function getName( $id ) { |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @since 3.0 |
||
| 475 | * @param string $key |
||
| 476 | * @return int form id |
||
| 477 | */ |
||
| 478 | public static function get_id_by_key( $key ) { |
||
| 479 | return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) ); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $key |
||
| 484 | * @return int form id |
||
| 485 | */ |
||
| 486 | public static function getIdByKey( $key ) { |
||
| 487 | _deprecated_function( __METHOD__, '3.0', 'FrmForm::get_id_by_key' ); |
||
| 488 | return self::get_id_by_key( $key ); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @since 3.0 |
||
| 493 | * @param int $id |
||
| 494 | * @return string form key |
||
| 495 | */ |
||
| 496 | public static function get_key_by_id( $id ) { |
||
| 497 | $id = (int) $id; |
||
| 498 | $cache = FrmDb::check_cache( $id, 'frm_form' ); |
||
| 499 | if ( $cache ) { |
||
| 500 | return $cache->form_key; |
||
| 501 | } |
||
| 502 | |||
| 503 | $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); |
||
| 504 | |||
| 505 | return $key; |
||
| 506 | } |
||
| 507 | |||
| 508 | public static function getKeyById( $id ) { |
||
| 509 | _deprecated_function( __METHOD__, '3.0', 'FrmForm::get_key_by_id' ); |
||
| 510 | return self::get_key_by_id( $id ); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * If $form is numeric, get the form object |
||
| 515 | * @param object|int $form |
||
| 516 | * @since 2.0.9 |
||
| 517 | */ |
||
| 518 | public static function maybe_get_form( &$form ) { |
||
| 519 | if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
||
| 520 | $form = self::getOne( $form ); |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return object form |
||
| 526 | */ |
||
| 527 | public static function getOne( $id, $blog_id = false ) { |
||
| 528 | global $wpdb; |
||
| 529 | |||
| 530 | if ( $blog_id && is_multisite() ) { |
||
| 531 | global $wpmuBaseTablePrefix; |
||
| 532 | $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); |
||
| 533 | |||
| 534 | $table_name = $prefix . 'frm_forms'; |
||
| 535 | } else { |
||
| 536 | $table_name = $wpdb->prefix . 'frm_forms'; |
||
| 537 | $cache = wp_cache_get($id, 'frm_form'); |
||
| 538 | if ( $cache ) { |
||
| 539 | if ( isset($cache->options) ) { |
||
| 540 | $cache->options = maybe_unserialize($cache->options); |
||
| 541 | } |
||
| 542 | |||
| 543 | return stripslashes_deep($cache); |
||
| 544 | } |
||
| 545 | } |
||
| 546 | |||
| 547 | if ( is_numeric($id) ) { |
||
| 548 | $where = array( 'id' => $id ); |
||
| 549 | } else { |
||
| 550 | $where = array( 'form_key' => $id ); |
||
| 551 | } |
||
| 552 | |||
| 553 | $results = FrmDb::get_row( $table_name, $where ); |
||
| 554 | |||
| 555 | if ( isset($results->options) ) { |
||
| 556 | FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
||
| 557 | $results->options = maybe_unserialize($results->options); |
||
| 558 | } |
||
| 559 | return stripslashes_deep($results); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return object|array of objects |
||
| 564 | */ |
||
| 565 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get all published forms |
||
| 596 | * @since 2.0 |
||
| 597 | * @return array of forms |
||
| 598 | */ |
||
| 599 | public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
||
| 600 | $query['is_template'] = 0; |
||
| 601 | $query['status'] = array( null, '', 'published' ); |
||
| 602 | if ( $inc_children == 'exclude' ) { |
||
| 603 | $query['parent_form_id'] = array( null, 0 ); |
||
| 604 | } |
||
| 605 | |||
| 606 | $forms = self::getAll( $query, 'name', $limit ); |
||
| 607 | return $forms; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @return int count of forms |
||
| 612 | */ |
||
| 613 | public static function get_count() { |
||
| 614 | global $wpdb; |
||
| 615 | |||
| 616 | $cache_key = 'frm_form_counts'; |
||
| 617 | |||
| 618 | $counts = wp_cache_get( $cache_key, 'frm_form' ); |
||
| 619 | if ( false !== $counts ) { |
||
| 620 | return $counts; |
||
| 621 | } |
||
| 622 | |||
| 623 | $results = (array) FrmDb::get_results( 'frm_forms', array( |
||
| 624 | 'or' => 1, |
||
| 625 | 'parent_form_id' => null, |
||
| 626 | 'parent_form_id <' => 0, |
||
| 627 | ), 'status, is_template' ); |
||
| 628 | |||
| 629 | $statuses = array( 'published', 'draft', 'template', 'trash' ); |
||
| 630 | $counts = array_fill_keys( $statuses, 0 ); |
||
| 631 | |||
| 632 | foreach ( $results as $row ) { |
||
| 633 | if ( 'trash' != $row->status ) { |
||
| 634 | if ( $row->is_template ) { |
||
| 635 | $counts['template']++; |
||
| 636 | } else { |
||
| 637 | $counts['published']++; |
||
| 638 | } |
||
| 639 | } else { |
||
| 640 | $counts['trash']++; |
||
| 641 | } |
||
| 642 | |||
| 643 | if ( 'draft' == $row->status ) { |
||
| 644 | $counts['draft']++; |
||
| 645 | } |
||
| 646 | |||
| 647 | unset($row); |
||
| 648 | } |
||
| 649 | |||
| 650 | $counts = (object) $counts; |
||
| 651 | FrmDb::set_cache( $cache_key, $counts, 'frm_form' ); |
||
| 652 | |||
| 653 | return $counts; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Clear form caching |
||
| 658 | * Called when a form is created, updated, duplicated, or deleted |
||
| 659 | * or when the form status is changed |
||
| 660 | * |
||
| 661 | * @since 2.0.4 |
||
| 662 | */ |
||
| 663 | public static function clear_form_cache() { |
||
| 664 | FrmDb::cache_delete_group( 'frm_form' ); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return array of errors |
||
| 669 | */ |
||
| 670 | public static function validate( $values ) { |
||
| 671 | $errors = array(); |
||
| 672 | |||
| 673 | return apply_filters('frm_validate_form', $errors, $values); |
||
| 674 | } |
||
| 675 | |||
| 676 | public static function get_params( $form = null ) { |
||
| 733 | } |
||
| 734 | |||
| 735 | public static function list_page_params() { |
||
| 736 | $values = array(); |
||
| 737 | $defaults = array( |
||
| 738 | 'template' => 0, |
||
| 739 | 'id' => '', |
||
| 740 | 'paged' => 1, |
||
| 741 | 'form' => '', |
||
| 742 | 'search' => '', |
||
| 743 | 'sort' => '', |
||
| 744 | 'sdir' => '', |
||
| 745 | ); |
||
| 746 | foreach ( $defaults as $var => $default ) { |
||
| 747 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
||
| 748 | } |
||
| 749 | |||
| 750 | return $values; |
||
| 751 | } |
||
| 752 | |||
| 753 | public static function get_admin_params( $form = null ) { |
||
| 779 | } |
||
| 780 | |||
| 781 | public static function get_current_form_id( $default_form = 'none' ) { |
||
| 782 | if ( 'first' == $default_form ) { |
||
| 783 | $form = self::get_current_form(); |
||
| 784 | } else { |
||
| 785 | $form = self::maybe_get_current_form(); |
||
| 786 | } |
||
| 787 | $form_id = $form ? $form->id : 0; |
||
| 788 | |||
| 789 | return $form_id; |
||
| 790 | } |
||
| 791 | |||
| 792 | public static function maybe_get_current_form( $form_id = 0 ) { |
||
| 804 | } |
||
| 805 | |||
| 806 | public static function get_current_form( $form_id = 0 ) { |
||
| 807 | $form = self::maybe_get_current_form( $form_id ); |
||
| 808 | if ( is_numeric( $form ) ) { |
||
| 809 | $form = self::set_current_form( $form ); |
||
| 810 | } |
||
| 811 | return $form; |
||
| 812 | } |
||
| 813 | |||
| 814 | public static function set_current_form( $form_id ) { |
||
| 825 | } |
||
| 826 | |||
| 827 | public static function is_form_loaded( $form, $this_load, $global_load ) { |
||
| 828 | global $frm_vars; |
||
| 829 | $small_form = new stdClass(); |
||
| 830 | foreach ( array( 'id', 'form_key', 'name' ) as $var ) { |
||
| 831 | $small_form->{$var} = $form->{$var}; |
||
| 832 | unset($var); |
||
| 833 | } |
||
| 834 | |||
| 835 | $frm_vars['forms_loaded'][] = $small_form; |
||
| 836 | |||
| 837 | if ( $this_load && empty( $global_load ) ) { |
||
| 838 | $global_load = true; |
||
| 839 | $frm_vars['load_css'] = true; |
||
| 840 | } |
||
| 841 | |||
| 842 | return ( ( ! isset($frm_vars['css_loaded']) || ! $frm_vars['css_loaded'] ) && $global_load ); |
||
| 843 | } |
||
| 844 | |||
| 845 | public static function show_submit( $form ) { |
||
| 846 | $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); |
||
| 847 | $show = apply_filters( 'frm_show_submit_button', $show, $form ); |
||
| 848 | return $show; |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * @since 2.3 |
||
| 853 | */ |
||
| 854 | public static function get_option( $atts ) { |
||
| 857 | } |
||
| 858 | } |
||
| 859 |