@@ -1,262 +1,262 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined('ABSPATH') ) { |
| 3 | - die('You are not allowed to call this page directly.'); |
|
| 3 | + die('You are not allowed to call this page directly.'); |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmForm { |
| 7 | 7 | |
| 8 | - /** |
|
| 9 | - * @return int|boolean id on success or false on failure |
|
| 10 | - */ |
|
| 11 | - public static function create( $values ) { |
|
| 12 | - global $wpdb; |
|
| 8 | + /** |
|
| 9 | + * @return int|boolean id on success or false on failure |
|
| 10 | + */ |
|
| 11 | + public static function create( $values ) { |
|
| 12 | + global $wpdb; |
|
| 13 | 13 | |
| 14 | - $new_values = array( |
|
| 14 | + $new_values = array( |
|
| 15 | 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, |
|
| 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 | 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 | - ); |
|
| 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 | 26 | |
| 27 | 27 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 28 | 28 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 29 | 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'); |
|
| 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 | 33 | |
| 34 | - $options = apply_filters('frm_form_options_before_update', $options, $values); |
|
| 35 | - $new_values['options'] = serialize($options); |
|
| 34 | + $options = apply_filters('frm_form_options_before_update', $options, $values); |
|
| 35 | + $new_values['options'] = serialize($options); |
|
| 36 | 36 | |
| 37 | - //if(isset($values['id']) && is_numeric($values['id'])) |
|
| 38 | - // $new_values['id'] = $values['id']; |
|
| 37 | + //if(isset($values['id']) && is_numeric($values['id'])) |
|
| 38 | + // $new_values['id'] = $values['id']; |
|
| 39 | 39 | |
| 40 | 40 | $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 41 | 41 | |
| 42 | - $id = $wpdb->insert_id; |
|
| 42 | + $id = $wpdb->insert_id; |
|
| 43 | 43 | |
| 44 | 44 | // Clear form caching |
| 45 | 45 | self::clear_form_cache(); |
| 46 | 46 | |
| 47 | - return $id; |
|
| 48 | - } |
|
| 47 | + return $id; |
|
| 48 | + } |
|
| 49 | 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; |
|
| 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 | 55 | |
| 56 | - $values = self::getOne( $id, $blog_id ); |
|
| 57 | - if ( ! $values ) { |
|
| 58 | - return false; |
|
| 59 | - } |
|
| 56 | + $values = self::getOne( $id, $blog_id ); |
|
| 57 | + if ( ! $values ) { |
|
| 58 | + return false; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $new_key = $copy_keys ? $values->form_key : ''; |
|
| 61 | + $new_key = $copy_keys ? $values->form_key : ''; |
|
| 62 | 62 | |
| 63 | - $new_values = array( |
|
| 63 | + $new_values = array( |
|
| 64 | 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 | - } |
|
| 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 | 87 | |
| 88 | 88 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 89 | 89 | |
| 90 | - if ( $query_results ) { |
|
| 90 | + if ( $query_results ) { |
|
| 91 | 91 | // Clear form caching |
| 92 | 92 | self::clear_form_cache(); |
| 93 | 93 | |
| 94 | - $form_id = $wpdb->insert_id; |
|
| 95 | - FrmField::duplicate($id, $form_id, $copy_keys, $blog_id); |
|
| 94 | + $form_id = $wpdb->insert_id; |
|
| 95 | + FrmField::duplicate($id, $form_id, $copy_keys, $blog_id); |
|
| 96 | 96 | |
| 97 | - // update form settings after fields are created |
|
| 97 | + // update form settings after fields are created |
|
| 98 | 98 | do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) ); |
| 99 | - return $form_id; |
|
| 100 | - } |
|
| 99 | + return $form_id; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - return false; |
|
| 103 | - } |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | public static function after_duplicate( $form_id, $values ) { |
| 106 | 106 | $new_opts = maybe_unserialize( $values['options'] ); |
| 107 | 107 | $values['options'] = $new_opts; |
| 108 | 108 | |
| 109 | - if ( isset($new_opts['success_msg']) ) { |
|
| 110 | - $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']); |
|
| 111 | - } |
|
| 109 | + if ( isset($new_opts['success_msg']) ) { |
|
| 110 | + $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - $new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id); |
|
| 113 | + $new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id); |
|
| 114 | 114 | |
| 115 | - if ( $new_opts != $values['options'] ) { |
|
| 116 | - global $wpdb; |
|
| 115 | + if ( $new_opts != $values['options'] ) { |
|
| 116 | + global $wpdb; |
|
| 117 | 117 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 118 | - } |
|
| 119 | - } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @return int|boolean |
|
| 123 | - */ |
|
| 124 | - public static function update( $id, $values, $create_link = false ) { |
|
| 125 | - global $wpdb; |
|
| 121 | + /** |
|
| 122 | + * @return int|boolean |
|
| 123 | + */ |
|
| 124 | + public static function update( $id, $values, $create_link = false ) { |
|
| 125 | + global $wpdb; |
|
| 126 | 126 | |
| 127 | - if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
|
| 128 | - $values['status'] = 'published'; |
|
| 129 | - } |
|
| 127 | + if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
|
| 128 | + $values['status'] = 'published'; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - if ( isset($values['form_key']) ) { |
|
| 131 | + if ( isset($values['form_key']) ) { |
|
| 132 | 132 | $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
| 133 | - } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | 135 | $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
| 136 | 136 | |
| 137 | - $new_values = self::set_update_options( array(), $values); |
|
| 137 | + $new_values = self::set_update_options( array(), $values); |
|
| 138 | 138 | |
| 139 | - foreach ( $values as $value_key => $value ) { |
|
| 139 | + foreach ( $values as $value_key => $value ) { |
|
| 140 | 140 | if ( $value_key && in_array( $value_key, $form_fields ) ) { |
| 141 | 141 | $new_values[ $value_key ] = $value; |
| 142 | - } |
|
| 143 | - } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { |
|
| 146 | - $new_values['status'] = $values['new_status']; |
|
| 147 | - } |
|
| 145 | + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { |
|
| 146 | + $new_values['status'] = $values['new_status']; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - if ( ! empty( $new_values ) ) { |
|
| 149 | + if ( ! empty( $new_values ) ) { |
|
| 150 | 150 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
| 151 | - if ( $query_results ) { |
|
| 151 | + if ( $query_results ) { |
|
| 152 | 152 | self::clear_form_cache(); |
| 153 | - } |
|
| 154 | - } else { |
|
| 155 | - $query_results = true; |
|
| 156 | - } |
|
| 157 | - unset($new_values); |
|
| 153 | + } |
|
| 154 | + } else { |
|
| 155 | + $query_results = true; |
|
| 156 | + } |
|
| 157 | + unset($new_values); |
|
| 158 | 158 | |
| 159 | - $values = self::update_fields($id, $values); |
|
| 159 | + $values = self::update_fields($id, $values); |
|
| 160 | 160 | |
| 161 | 161 | do_action( 'frm_update_form', $id, $values ); |
| 162 | 162 | do_action( 'frm_update_form_' . $id, $values ); |
| 163 | 163 | |
| 164 | - return $query_results; |
|
| 165 | - } |
|
| 164 | + return $query_results; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @return array |
|
| 169 | - */ |
|
| 167 | + /** |
|
| 168 | + * @return array |
|
| 169 | + */ |
|
| 170 | 170 | public static function set_update_options( $new_values, $values ) { |
| 171 | - if ( ! isset($values['options']) ) { |
|
| 172 | - return $new_values; |
|
| 173 | - } |
|
| 171 | + if ( ! isset($values['options']) ) { |
|
| 172 | + return $new_values; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 176 | 176 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 177 | 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'); |
|
| 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 | 182 | |
| 183 | - $options = apply_filters('frm_form_options_before_update', $options, $values); |
|
| 184 | - $new_values['options'] = serialize($options); |
|
| 183 | + $options = apply_filters('frm_form_options_before_update', $options, $values); |
|
| 184 | + $new_values['options'] = serialize($options); |
|
| 185 | 185 | |
| 186 | - return $new_values; |
|
| 187 | - } |
|
| 186 | + return $new_values; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 190 | + /** |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | 193 | public static function update_fields( $id, $values ) { |
| 194 | 194 | |
| 195 | - if ( ! isset($values['item_meta']) && ! isset($values['field_options']) ) { |
|
| 196 | - return $values; |
|
| 197 | - } |
|
| 195 | + if ( ! isset($values['item_meta']) && ! isset($values['field_options']) ) { |
|
| 196 | + return $values; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - $all_fields = FrmField::get_all_for_form($id); |
|
| 200 | - if ( empty($all_fields) ) { |
|
| 201 | - return $values; |
|
| 202 | - } |
|
| 199 | + $all_fields = FrmField::get_all_for_form($id); |
|
| 200 | + if ( empty($all_fields) ) { |
|
| 201 | + return $values; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - if ( ! isset($values['item_meta']) ) { |
|
| 205 | - $values['item_meta'] = array(); |
|
| 206 | - } |
|
| 204 | + if ( ! isset($values['item_meta']) ) { |
|
| 205 | + $values['item_meta'] = array(); |
|
| 206 | + } |
|
| 207 | 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']) ) { |
|
| 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 | 212 | $values['item_meta'][ $fid->id ] = ''; |
| 213 | - } |
|
| 213 | + } |
|
| 214 | 214 | $field_array[ $fid->id ] = $fid; |
| 215 | - } |
|
| 216 | - unset($all_fields); |
|
| 215 | + } |
|
| 216 | + unset($all_fields); |
|
| 217 | 217 | |
| 218 | - foreach ( $values['item_meta'] as $field_id => $default_value ) { |
|
| 218 | + foreach ( $values['item_meta'] as $field_id => $default_value ) { |
|
| 219 | 219 | if ( isset( $field_array[ $field_id ] ) ) { |
| 220 | 220 | $field = $field_array[ $field_id ]; |
| 221 | - } else { |
|
| 222 | - $field = FrmField::getOne($field_id); |
|
| 223 | - } |
|
| 221 | + } else { |
|
| 222 | + $field = FrmField::getOne($field_id); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - if ( ! $field ) { |
|
| 226 | - continue; |
|
| 227 | - } |
|
| 225 | + if ( ! $field ) { |
|
| 226 | + continue; |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | 229 | $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
| 230 | 230 | if ( $is_settings_page ) { |
| 231 | - //updating the settings page |
|
| 231 | + //updating the settings page |
|
| 232 | 232 | if ( isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ) { |
| 233 | 233 | $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ? $values['field_options'][ 'custom_html_' . $field_id ] : ( isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ) ); |
| 234 | - $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
|
| 234 | + $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
|
| 235 | 235 | FrmField::update( $field_id, array( 'field_options' => $field->field_options ) ); |
| 236 | - } else if ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
|
| 237 | - $prev_opts = $field->field_options; |
|
| 238 | - $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
|
| 239 | - if ( $prev_opts != $field->field_options ) { |
|
| 236 | + } else if ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
|
| 237 | + $prev_opts = $field->field_options; |
|
| 238 | + $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
|
| 239 | + if ( $prev_opts != $field->field_options ) { |
|
| 240 | 240 | FrmField::update( $field_id, array( 'field_options' => $field->field_options ) ); |
| 241 | - } |
|
| 242 | - unset($prev_opts); |
|
| 243 | - } |
|
| 244 | - } |
|
| 241 | + } |
|
| 242 | + unset($prev_opts); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | if ( $is_settings_page && ! defined( 'WP_IMPORTING' ) ) { |
| 247 | - continue; |
|
| 248 | - } |
|
| 247 | + continue; |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - //updating the form |
|
| 250 | + //updating the form |
|
| 251 | 251 | $update_options = FrmFieldsHelper::get_default_field_opts( $field->type, $field, true ); |
| 252 | 252 | unset( $update_options['custom_html'] ); // don't check for POST html |
| 253 | 253 | $update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
| 254 | 254 | |
| 255 | 255 | foreach ( $update_options as $opt => $default ) { |
| 256 | 256 | $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? trim( sanitize_text_field( $values['field_options'][ $opt . '_' . $field_id ] ) ) : $default; |
| 257 | - } |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); |
|
| 259 | + $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); |
|
| 260 | 260 | $default_value = maybe_serialize( $values['item_meta'][ $field_id ] ); |
| 261 | 261 | |
| 262 | 262 | $new_field = array( |
@@ -268,12 +268,12 @@ discard block |
||
| 268 | 268 | |
| 269 | 269 | FrmField::update( $field_id, $new_field ); |
| 270 | 270 | |
| 271 | - FrmField::delete_form_transient($field->form_id); |
|
| 272 | - } |
|
| 271 | + FrmField::delete_form_transient($field->form_id); |
|
| 272 | + } |
|
| 273 | 273 | self::clear_form_cache(); |
| 274 | 274 | |
| 275 | - return $values; |
|
| 276 | - } |
|
| 275 | + return $values; |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | 278 | private static function prepare_field_update_values( $field, $values, &$new_field ) { |
| 279 | 279 | $field_cols = array( |
@@ -286,113 +286,113 @@ discard block |
||
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * @param string $status |
|
| 291 | - * @return int|boolean |
|
| 292 | - */ |
|
| 289 | + /** |
|
| 290 | + * @param string $status |
|
| 291 | + * @return int|boolean |
|
| 292 | + */ |
|
| 293 | 293 | public static function set_status( $id, $status ) { |
| 294 | - if ( 'trash' == $status ) { |
|
| 295 | - return self::trash($id); |
|
| 296 | - } |
|
| 294 | + if ( 'trash' == $status ) { |
|
| 295 | + return self::trash($id); |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | 298 | $statuses = array( 'published', 'draft', 'trash' ); |
| 299 | - if ( ! in_array( $status, $statuses ) ) { |
|
| 300 | - return false; |
|
| 301 | - } |
|
| 299 | + if ( ! in_array( $status, $statuses ) ) { |
|
| 300 | + return false; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - global $wpdb; |
|
| 303 | + global $wpdb; |
|
| 304 | 304 | |
| 305 | - if ( is_array($id) ) { |
|
| 305 | + if ( is_array($id) ) { |
|
| 306 | 306 | $where = array( 'id' => $id, 'parent_form_id' => $id, 'or' => 1 ); |
| 307 | 307 | FrmDb::get_where_clause_and_values( $where ); |
| 308 | 308 | array_unshift( $where['values'], $status ); |
| 309 | 309 | |
| 310 | 310 | $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); |
| 311 | - } else { |
|
| 311 | + } else { |
|
| 312 | 312 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
| 313 | 313 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
| 314 | - } |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - if ( $query_results ) { |
|
| 316 | + if ( $query_results ) { |
|
| 317 | 317 | self::clear_form_cache(); |
| 318 | - } |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - return $query_results; |
|
| 321 | - } |
|
| 320 | + return $query_results; |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | - /** |
|
| 324 | - * @return int|boolean |
|
| 325 | - */ |
|
| 323 | + /** |
|
| 324 | + * @return int|boolean |
|
| 325 | + */ |
|
| 326 | 326 | public static function trash( $id ) { |
| 327 | - if ( ! EMPTY_TRASH_DAYS ) { |
|
| 328 | - return self::destroy( $id ); |
|
| 329 | - } |
|
| 327 | + if ( ! EMPTY_TRASH_DAYS ) { |
|
| 328 | + return self::destroy( $id ); |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - $form = self::getOne($id); |
|
| 332 | - if ( ! $form ) { |
|
| 333 | - return false; |
|
| 334 | - } |
|
| 331 | + $form = self::getOne($id); |
|
| 332 | + if ( ! $form ) { |
|
| 333 | + return false; |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | - $options = $form->options; |
|
| 337 | - $options['trash_time'] = time(); |
|
| 336 | + $options = $form->options; |
|
| 337 | + $options['trash_time'] = time(); |
|
| 338 | 338 | |
| 339 | - global $wpdb; |
|
| 340 | - $query_results = $wpdb->update( |
|
| 339 | + global $wpdb; |
|
| 340 | + $query_results = $wpdb->update( |
|
| 341 | 341 | $wpdb->prefix . 'frm_forms', |
| 342 | 342 | array( 'status' => 'trash', 'options' => serialize( $options ) ), |
| 343 | 343 | array( 'id' => $id ) |
| 344 | - ); |
|
| 344 | + ); |
|
| 345 | 345 | |
| 346 | - $wpdb->update( |
|
| 346 | + $wpdb->update( |
|
| 347 | 347 | $wpdb->prefix . 'frm_forms', |
| 348 | 348 | array( 'status' => 'trash', 'options' => serialize( $options ) ), |
| 349 | 349 | array( 'parent_form_id' => $id ) |
| 350 | - ); |
|
| 350 | + ); |
|
| 351 | 351 | |
| 352 | - if ( $query_results ) { |
|
| 352 | + if ( $query_results ) { |
|
| 353 | 353 | self::clear_form_cache(); |
| 354 | - } |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - return $query_results; |
|
| 357 | - } |
|
| 356 | + return $query_results; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - /** |
|
| 360 | - * @return int|boolean |
|
| 361 | - */ |
|
| 359 | + /** |
|
| 360 | + * @return int|boolean |
|
| 361 | + */ |
|
| 362 | 362 | public static function destroy( $id ) { |
| 363 | - global $wpdb; |
|
| 363 | + global $wpdb; |
|
| 364 | 364 | |
| 365 | - $form = self::getOne($id); |
|
| 366 | - if ( ! $form ) { |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 365 | + $form = self::getOne($id); |
|
| 366 | + if ( ! $form ) { |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | 369 | $id = $form->id; |
| 370 | 370 | |
| 371 | - // Disconnect the entries from this form |
|
| 371 | + // Disconnect the entries from this form |
|
| 372 | 372 | $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
| 373 | - foreach ( $entries as $entry_id ) { |
|
| 374 | - FrmEntry::destroy($entry_id); |
|
| 375 | - unset($entry_id); |
|
| 376 | - } |
|
| 373 | + foreach ( $entries as $entry_id ) { |
|
| 374 | + FrmEntry::destroy($entry_id); |
|
| 375 | + unset($entry_id); |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - // Disconnect the fields from this form |
|
| 378 | + // Disconnect the fields from this form |
|
| 379 | 379 | $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 ) ); |
| 380 | 380 | |
| 381 | 381 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 382 | - if ( $query_results ) { |
|
| 383 | - // Delete all form actions linked to this form |
|
| 384 | - $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
|
| 385 | - $action_control->destroy($id, 'all'); |
|
| 382 | + if ( $query_results ) { |
|
| 383 | + // Delete all form actions linked to this form |
|
| 384 | + $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
|
| 385 | + $action_control->destroy($id, 'all'); |
|
| 386 | 386 | |
| 387 | 387 | // Clear form caching |
| 388 | 388 | self::clear_form_cache(); |
| 389 | 389 | |
| 390 | 390 | do_action( 'frm_destroy_form', $id ); |
| 391 | 391 | do_action( 'frm_destroy_form_' . $id ); |
| 392 | - } |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | - return $query_results; |
|
| 395 | - } |
|
| 394 | + return $query_results; |
|
| 395 | + } |
|
| 396 | 396 | |
| 397 | 397 | /** |
| 398 | 398 | * Delete trashed forms based on how long they have been trashed |
@@ -424,46 +424,46 @@ discard block |
||
| 424 | 424 | return $count; |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | - /** |
|
| 428 | - * @return string form name |
|
| 429 | - */ |
|
| 430 | - public static function getName( $id ) { |
|
| 431 | - $form = FrmAppHelper::check_cache($id, 'frm_form'); |
|
| 432 | - if ( $form ) { |
|
| 433 | - $r = stripslashes($form->name); |
|
| 434 | - return $r; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
|
| 438 | - $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
|
| 439 | - $r = stripslashes($r); |
|
| 440 | - |
|
| 441 | - return $r; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * @param string $key |
|
| 446 | - * @return int form id |
|
| 447 | - */ |
|
| 427 | + /** |
|
| 428 | + * @return string form name |
|
| 429 | + */ |
|
| 430 | + public static function getName( $id ) { |
|
| 431 | + $form = FrmAppHelper::check_cache($id, 'frm_form'); |
|
| 432 | + if ( $form ) { |
|
| 433 | + $r = stripslashes($form->name); |
|
| 434 | + return $r; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
|
| 438 | + $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
|
| 439 | + $r = stripslashes($r); |
|
| 440 | + |
|
| 441 | + return $r; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * @param string $key |
|
| 446 | + * @return int form id |
|
| 447 | + */ |
|
| 448 | 448 | public static function getIdByKey( $key ) { |
| 449 | 449 | return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) ); |
| 450 | - } |
|
| 450 | + } |
|
| 451 | 451 | |
| 452 | - /** |
|
| 453 | - * @param int $id |
|
| 454 | - * @return string form key |
|
| 455 | - */ |
|
| 452 | + /** |
|
| 453 | + * @param int $id |
|
| 454 | + * @return string form key |
|
| 455 | + */ |
|
| 456 | 456 | public static function getKeyById( $id ) { |
| 457 | - $id = (int) $id; |
|
| 458 | - $cache = FrmAppHelper::check_cache($id, 'frm_form'); |
|
| 459 | - if ( $cache ) { |
|
| 460 | - return $cache->form_key; |
|
| 461 | - } |
|
| 457 | + $id = (int) $id; |
|
| 458 | + $cache = FrmAppHelper::check_cache($id, 'frm_form'); |
|
| 459 | + if ( $cache ) { |
|
| 460 | + return $cache->form_key; |
|
| 461 | + } |
|
| 462 | 462 | |
| 463 | - $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); |
|
| 463 | + $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); |
|
| 464 | 464 | |
| 465 | - return $key; |
|
| 466 | - } |
|
| 465 | + return $key; |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | 468 | /** |
| 469 | 469 | * If $form is numeric, get the form object |
@@ -476,47 +476,47 @@ discard block |
||
| 476 | 476 | } |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * @return object form |
|
| 481 | - */ |
|
| 482 | - public static function getOne( $id, $blog_id = false ) { |
|
| 483 | - global $wpdb; |
|
| 479 | + /** |
|
| 480 | + * @return object form |
|
| 481 | + */ |
|
| 482 | + public static function getOne( $id, $blog_id = false ) { |
|
| 483 | + global $wpdb; |
|
| 484 | 484 | |
| 485 | - if ( $blog_id && is_multisite() ) { |
|
| 486 | - global $wpmuBaseTablePrefix; |
|
| 485 | + if ( $blog_id && is_multisite() ) { |
|
| 486 | + global $wpmuBaseTablePrefix; |
|
| 487 | 487 | $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); |
| 488 | 488 | |
| 489 | 489 | $table_name = $prefix . 'frm_forms'; |
| 490 | - } else { |
|
| 490 | + } else { |
|
| 491 | 491 | $table_name = $wpdb->prefix . 'frm_forms'; |
| 492 | - $cache = wp_cache_get($id, 'frm_form'); |
|
| 493 | - if ( $cache ) { |
|
| 494 | - if ( isset($cache->options) ) { |
|
| 495 | - $cache->options = maybe_unserialize($cache->options); |
|
| 496 | - } |
|
| 492 | + $cache = wp_cache_get($id, 'frm_form'); |
|
| 493 | + if ( $cache ) { |
|
| 494 | + if ( isset($cache->options) ) { |
|
| 495 | + $cache->options = maybe_unserialize($cache->options); |
|
| 496 | + } |
|
| 497 | 497 | |
| 498 | - return stripslashes_deep($cache); |
|
| 499 | - } |
|
| 500 | - } |
|
| 498 | + return stripslashes_deep($cache); |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - if ( is_numeric($id) ) { |
|
| 503 | - $where = array( 'id' => $id ); |
|
| 504 | - } else { |
|
| 505 | - $where = array( 'form_key' => $id ); |
|
| 506 | - } |
|
| 502 | + if ( is_numeric($id) ) { |
|
| 503 | + $where = array( 'id' => $id ); |
|
| 504 | + } else { |
|
| 505 | + $where = array( 'form_key' => $id ); |
|
| 506 | + } |
|
| 507 | 507 | |
| 508 | - $results = FrmDb::get_row( $table_name, $where ); |
|
| 508 | + $results = FrmDb::get_row( $table_name, $where ); |
|
| 509 | 509 | |
| 510 | - if ( isset($results->options) ) { |
|
| 510 | + if ( isset($results->options) ) { |
|
| 511 | 511 | FrmAppHelper::set_cache( $results->id, $results, 'frm_form' ); |
| 512 | - $results->options = maybe_unserialize($results->options); |
|
| 513 | - } |
|
| 514 | - return stripslashes_deep($results); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * @return object|array of objects |
|
| 519 | - */ |
|
| 512 | + $results->options = maybe_unserialize($results->options); |
|
| 513 | + } |
|
| 514 | + return stripslashes_deep($results); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * @return object|array of objects |
|
| 519 | + */ |
|
| 520 | 520 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 521 | 521 | if ( is_array( $where ) && ! empty( $where ) ) { |
| 522 | 522 | $results = FrmDb::get_results( 'frm_forms', $where, '*', array( 'order_by' => $order_by, 'limit' => $limit ) ); |
@@ -540,8 +540,8 @@ discard block |
||
| 540 | 540 | $results = reset( $results ); |
| 541 | 541 | } |
| 542 | 542 | |
| 543 | - return stripslashes_deep($results); |
|
| 544 | - } |
|
| 543 | + return stripslashes_deep($results); |
|
| 544 | + } |
|
| 545 | 545 | |
| 546 | 546 | /** |
| 547 | 547 | * Get all published forms |
@@ -559,47 +559,47 @@ discard block |
||
| 559 | 559 | return $forms; |
| 560 | 560 | } |
| 561 | 561 | |
| 562 | - /** |
|
| 563 | - * @return int count of forms |
|
| 564 | - */ |
|
| 565 | - public static function get_count() { |
|
| 566 | - global $wpdb; |
|
| 562 | + /** |
|
| 563 | + * @return int count of forms |
|
| 564 | + */ |
|
| 565 | + public static function get_count() { |
|
| 566 | + global $wpdb; |
|
| 567 | 567 | |
| 568 | - $cache_key = 'frm_form_counts'; |
|
| 568 | + $cache_key = 'frm_form_counts'; |
|
| 569 | 569 | |
| 570 | - $counts = wp_cache_get( $cache_key, 'frm_form' ); |
|
| 571 | - if ( false !== $counts ) { |
|
| 572 | - return $counts; |
|
| 573 | - } |
|
| 570 | + $counts = wp_cache_get( $cache_key, 'frm_form' ); |
|
| 571 | + if ( false !== $counts ) { |
|
| 572 | + return $counts; |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | - $results = (array) FrmDb::get_results( 'frm_forms', array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 0 ), 'status, is_template' ); |
|
| 575 | + $results = (array) FrmDb::get_results( 'frm_forms', array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 0 ), 'status, is_template' ); |
|
| 576 | 576 | |
| 577 | 577 | $statuses = array( 'published', 'draft', 'template', 'trash' ); |
| 578 | - $counts = array_fill_keys( $statuses, 0 ); |
|
| 578 | + $counts = array_fill_keys( $statuses, 0 ); |
|
| 579 | 579 | |
| 580 | - foreach ( $results as $row ) { |
|
| 581 | - if ( 'trash' != $row->status ) { |
|
| 582 | - if ( $row->is_template ) { |
|
| 580 | + foreach ( $results as $row ) { |
|
| 581 | + if ( 'trash' != $row->status ) { |
|
| 582 | + if ( $row->is_template ) { |
|
| 583 | 583 | $counts['template']++; |
| 584 | - } else { |
|
| 584 | + } else { |
|
| 585 | 585 | $counts['published']++; |
| 586 | - } |
|
| 587 | - } else { |
|
| 586 | + } |
|
| 587 | + } else { |
|
| 588 | 588 | $counts['trash']++; |
| 589 | - } |
|
| 589 | + } |
|
| 590 | 590 | |
| 591 | - if ( 'draft' == $row->status ) { |
|
| 591 | + if ( 'draft' == $row->status ) { |
|
| 592 | 592 | $counts['draft']++; |
| 593 | - } |
|
| 593 | + } |
|
| 594 | 594 | |
| 595 | - unset($row); |
|
| 596 | - } |
|
| 595 | + unset($row); |
|
| 596 | + } |
|
| 597 | 597 | |
| 598 | - $counts = (object) $counts; |
|
| 598 | + $counts = (object) $counts; |
|
| 599 | 599 | FrmAppHelper::set_cache( $cache_key, $counts, 'frm_form' ); |
| 600 | 600 | |
| 601 | - return $counts; |
|
| 602 | - } |
|
| 601 | + return $counts; |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | 604 | /** |
| 605 | 605 | * Clear form caching |
@@ -612,14 +612,14 @@ discard block |
||
| 612 | 612 | FrmAppHelper::cache_delete_group( 'frm_form' ); |
| 613 | 613 | } |
| 614 | 614 | |
| 615 | - /** |
|
| 616 | - * @return array of errors |
|
| 617 | - */ |
|
| 615 | + /** |
|
| 616 | + * @return array of errors |
|
| 617 | + */ |
|
| 618 | 618 | public static function validate( $values ) { |
| 619 | - $errors = array(); |
|
| 619 | + $errors = array(); |
|
| 620 | 620 | |
| 621 | - return apply_filters('frm_validate_form', $errors, $values); |
|
| 622 | - } |
|
| 621 | + return apply_filters('frm_validate_form', $errors, $values); |
|
| 622 | + } |
|
| 623 | 623 | |
| 624 | 624 | public static function get_params( $form = null ) { |
| 625 | 625 | global $frm_vars; |