| @@ -1,30 +1,27 @@ | ||
| 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 | - * @param array $values | |
| 10 | - * @return int|bool id on success or false on failure. | |
| 11 | - */ | |
| 12 | - public static function create( $values ) { | |
| 13 | - global $wpdb; | |
| 8 | + /** | |
| 9 | + * @return int|boolean id on success or false on failure | |
| 10 | + */ | |
| 11 | + public static function create( $values ) { | |
| 12 | + global $wpdb; | |
| 14 | 13 | |
| 15 | - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); | |
| 16 | - | |
| 17 | 14 | $new_values = array( |
| 18 | - 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), | |
| 19 | - 'name' => $values['name'], | |
| 20 | - 'description' => $values['description'], | |
| 21 | - 'status' => isset( $values['status'] ) ? $values['status'] : 'published', | |
| 22 | - 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0, | |
| 23 | - 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0, | |
| 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, | |
| 24 | 21 | 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0, |
| 25 | - 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0, | |
| 26 | - 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ), | |
| 22 | + 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0, | |
| 23 | + 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ), | |
| 27 | 24 | ); |
| 28 | 25 | |
| 29 | 26 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 30 | 27 | FrmFormsHelper::fill_form_options( $options, $values ); |
| @@ -29,81 +26,60 @@ | ||
| 29 | 26 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 30 | 27 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 31 | 28 | |
| 32 | 29 | $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); |
| 33 | - $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 30 | + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 34 | 31 | $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); |
| 35 | 32 | |
| 36 | - /** | |
| 37 | - * Allows modifying form options before updating or creating. | |
| 38 | - * | |
| 39 | - * @since 5.4 Add the third param. | |
| 40 | - * | |
| 41 | - * @param array $options Form options. | |
| 42 | - * @param array $values Form data. | |
| 43 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 44 | - */ | |
| 45 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, false ); | |
| 46 | - $options = self::maybe_filter_form_options( $options ); | |
| 33 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 47 | 34 | $new_values['options'] = serialize( $options ); |
| 48 | 35 | |
| 36 | + //if(isset($values['id']) && is_numeric($values['id'])) | |
| 37 | + // $new_values['id'] = $values['id']; | |
| 38 | + | |
| 49 | 39 | $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 50 | 40 | |
| 51 | - $id = $wpdb->insert_id; | |
| 41 | + $id = $wpdb->insert_id; | |
| 52 | 42 | |
| 53 | 43 | // Clear form caching |
| 54 | 44 | self::clear_form_cache(); |
| 55 | 45 | |
| 56 | - return $id; | |
| 57 | - } | |
| 46 | + return $id; | |
| 47 | + } | |
| 58 | 48 | |
| 59 | - /** | |
| 60 | - * @since 5.0.08 | |
| 61 | - * | |
| 62 | - * @param array $options | |
| 63 | - * @return array | |
| 64 | - */ | |
| 65 | - private static function maybe_filter_form_options( $options ) { | |
| 66 | - if ( ! FrmAppHelper::allow_unfiltered_html() && ! empty( $options['submit_html'] ) ) { | |
| 67 | - $options['submit_html'] = FrmAppHelper::kses_submit_button( $options['submit_html'] ); | |
| 68 | - } | |
| 69 | - return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) ); | |
| 70 | - } | |
| 49 | + /** | |
| 50 | + * @return int|boolean ID on success or false on failure | |
| 51 | + */ | |
| 52 | + public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { | |
| 53 | + global $wpdb; | |
| 71 | 54 | |
| 72 | - /** | |
| 73 | - * @return int|boolean ID on success or false on failure | |
| 74 | - */ | |
| 75 | - public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { | |
| 76 | - global $wpdb; | |
| 55 | + $values = self::getOne( $id, $blog_id ); | |
| 56 | + if ( ! $values ) { | |
| 57 | + return false; | |
| 58 | + } | |
| 77 | 59 | |
| 78 | - $values = self::getOne( $id, $blog_id ); | |
| 79 | - if ( ! $values ) { | |
| 80 | - return false; | |
| 81 | - } | |
| 60 | + $new_key = $copy_keys ? $values->form_key : ''; | |
| 82 | 61 | |
| 83 | - $new_key = $copy_keys ? $values->form_key : ''; | |
| 62 | + $new_values = array( | |
| 63 | + 'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ), | |
| 64 | + 'name' => $values->name, | |
| 65 | + 'description' => $values->description, | |
| 66 | + 'status' => $template ? 'published' : 'draft', | |
| 67 | + 'logged_in' => $values->logged_in ? $values->logged_in : 0, | |
| 68 | + 'editable' => $values->editable ? $values->editable : 0, | |
| 69 | + 'created_at' => current_time( 'mysql', 1 ), | |
| 70 | + 'is_template' => $template ? 1 : 0, | |
| 71 | + ); | |
| 84 | 72 | |
| 85 | - $new_values = array( | |
| 86 | - 'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ), | |
| 87 | - 'name' => $values->name, | |
| 88 | - 'description' => $values->description, | |
| 89 | - 'status' => $values->status ? $values->status : 'published', | |
| 90 | - 'logged_in' => $values->logged_in ? $values->logged_in : 0, | |
| 91 | - 'editable' => $values->editable ? $values->editable : 0, | |
| 92 | - 'created_at' => current_time( 'mysql', 1 ), | |
| 93 | - 'is_template' => $template ? 1 : 0, | |
| 94 | - ); | |
| 95 | - | |
| 96 | - if ( $blog_id ) { | |
| 97 | - $new_values['status'] = 'published'; | |
| 98 | - $new_options = $values->options; | |
| 99 | - FrmAppHelper::unserialize_or_decode( $new_options ); | |
| 73 | + if ( $blog_id ) { | |
| 74 | + $new_values['status'] = 'published'; | |
| 75 | + $new_options = maybe_unserialize( $values->options ); | |
| 100 | 76 | $new_options['email_to'] = get_option( 'admin_email' ); |
| 101 | - $new_options['copy'] = false; | |
| 102 | - $new_values['options'] = $new_options; | |
| 103 | - } else { | |
| 104 | - $new_values['options'] = $values->options; | |
| 105 | - } | |
| 77 | + $new_options['copy'] = false; | |
| 78 | + $new_values['options'] = $new_options; | |
| 79 | + } else { | |
| 80 | + $new_values['options'] = $values->options; | |
| 81 | + } | |
| 106 | 82 | |
| 107 | 83 | if ( is_array( $new_values['options'] ) ) { |
| 108 | 84 | $new_values['options'] = serialize( $new_values['options'] ); |
| 109 | 85 | } |
| @@ -109,27 +85,25 @@ | ||
| 109 | 85 | } |
| 110 | 86 | |
| 111 | 87 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 112 | 88 | |
| 113 | - if ( $query_results ) { | |
| 89 | + if ( $query_results ) { | |
| 114 | 90 | // Clear form caching |
| 115 | 91 | self::clear_form_cache(); |
| 116 | 92 | |
| 117 | - $form_id = $wpdb->insert_id; | |
| 93 | + $form_id = $wpdb->insert_id; | |
| 118 | 94 | FrmField::duplicate( $id, $form_id, $copy_keys, $blog_id ); |
| 119 | 95 | |
| 120 | - // update form settings after fields are created | |
| 96 | + // update form settings after fields are created | |
| 121 | 97 | do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) ); |
| 98 | + return $form_id; | |
| 99 | + } | |
| 122 | 100 | |
| 123 | - return $form_id; | |
| 124 | - } | |
| 101 | + return false; | |
| 102 | + } | |
| 125 | 103 | |
| 126 | - return false; | |
| 127 | - } | |
| 128 | - | |
| 129 | 104 | public static function after_duplicate( $form_id, $values ) { |
| 130 | - $new_opts = $values['options']; | |
| 131 | - FrmAppHelper::unserialize_or_decode( $new_opts ); | |
| 105 | + $new_opts = maybe_unserialize( $values['options'] ); | |
| 132 | 106 | $values['options'] = $new_opts; |
| 133 | 107 | |
| 134 | 108 | if ( isset( $new_opts['success_msg'] ) ) { |
| 135 | 109 | $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids( $new_opts['success_msg'] ); |
| @@ -136,127 +110,50 @@ | ||
| 136 | 110 | } |
| 137 | 111 | |
| 138 | 112 | $new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id ); |
| 139 | 113 | |
| 140 | - if ( $new_opts != $values['options'] ) { | |
| 141 | - global $wpdb; | |
| 114 | + if ( $new_opts != $values['options'] ) { | |
| 115 | + global $wpdb; | |
| 142 | 116 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 143 | - } | |
| 117 | + } | |
| 118 | + } | |
| 144 | 119 | |
| 145 | - self::switch_field_ids_in_fields( $form_id ); | |
| 146 | - } | |
| 120 | + /** | |
| 121 | + * @return int|boolean | |
| 122 | + */ | |
| 123 | + public static function update( $id, $values, $create_link = false ) { | |
| 124 | + global $wpdb; | |
| 147 | 125 | |
| 148 | - /** | |
| 149 | - * Switches field ID in fields. | |
| 150 | - * | |
| 151 | - * @since 5.3 | |
| 152 | - * | |
| 153 | - * @param int $form_id Form ID. | |
| 154 | - */ | |
| 155 | - private static function switch_field_ids_in_fields( $form_id ) { | |
| 156 | - global $wpdb; | |
| 126 | + if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { | |
| 127 | + $values['status'] = 'published'; | |
| 128 | + } | |
| 157 | 129 | |
| 158 | - // Keys of fields that you want to check to replace field ID. | |
| 159 | - $keys = array( 'default_value', 'field_options' ); | |
| 160 | - $sql_cols = 'fi.id'; | |
| 161 | - foreach ( $keys as $key ) { | |
| 162 | - $sql_cols .= ( ',fi.' . $key ); | |
| 163 | - } | |
| 164 | - | |
| 165 | - $fields = FrmDb::get_results( | |
| 166 | - "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id", | |
| 167 | - array( | |
| 168 | - 'or' => 1, | |
| 169 | - 'fi.form_id' => $form_id, | |
| 170 | - 'fr.parent_form_id' => $form_id, | |
| 171 | - ), | |
| 172 | - $sql_cols | |
| 173 | - ); | |
| 174 | - | |
| 175 | - if ( ! $fields || ! is_array( $fields ) ) { | |
| 176 | - return; | |
| 177 | - } | |
| 178 | - | |
| 179 | - foreach ( $fields as $field ) { | |
| 180 | - self::switch_field_ids_in_field( (array) $field ); | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * Switches field ID in a field. | |
| 186 | - * | |
| 187 | - * @since 5.3 | |
| 188 | - * | |
| 189 | - * @param array $field Field array. | |
| 190 | - */ | |
| 191 | - private static function switch_field_ids_in_field( $field ) { | |
| 192 | - $new_values = array(); | |
| 193 | - foreach ( $field as $key => $value ) { | |
| 194 | - if ( 'id' === $key || ! $value ) { | |
| 195 | - continue; | |
| 196 | - } | |
| 197 | - | |
| 198 | - if ( ! is_string( $value ) && ! is_array( $value ) ) { | |
| 199 | - continue; | |
| 200 | - } | |
| 201 | - | |
| 202 | - if ( 'field_options' === $key ) { | |
| 203 | - // Need to loop through field_options to prevent breaking serialized string when length changed. | |
| 204 | - FrmAppHelper::unserialize_or_decode( $value ); | |
| 205 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 206 | - $new_val = serialize( $new_val ); | |
| 207 | - } else { | |
| 208 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 209 | - } | |
| 210 | - | |
| 211 | - if ( $new_val !== $value ) { | |
| 212 | - $new_values[ $key ] = $new_val; | |
| 213 | - } | |
| 214 | - } | |
| 215 | - | |
| 216 | - if ( ! empty( $new_values ) ) { | |
| 217 | - FrmField::update( $field['id'], $new_values ); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - | |
| 221 | - /** | |
| 222 | - * @return int|boolean | |
| 223 | - */ | |
| 224 | - public static function update( $id, $values, $create_link = false ) { | |
| 225 | - global $wpdb; | |
| 226 | - | |
| 227 | - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); | |
| 228 | - | |
| 229 | - if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { | |
| 230 | - $values['status'] = 'published'; | |
| 231 | - } | |
| 232 | - | |
| 233 | 130 | if ( isset( $values['form_key'] ) ) { |
| 234 | 131 | $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
| 235 | - } | |
| 132 | + } | |
| 236 | 133 | |
| 237 | 134 | $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
| 238 | 135 | |
| 239 | - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) ); | |
| 136 | + $new_values = self::set_update_options( array(), $values ); | |
| 240 | 137 | |
| 241 | - foreach ( $values as $value_key => $value ) { | |
| 138 | + foreach ( $values as $value_key => $value ) { | |
| 242 | 139 | if ( $value_key && in_array( $value_key, $form_fields ) ) { |
| 243 | 140 | $new_values[ $value_key ] = $value; |
| 244 | - } | |
| 245 | - } | |
| 141 | + } | |
| 142 | + } | |
| 246 | 143 | |
| 247 | - if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { | |
| 248 | - $new_values['status'] = $values['new_status']; | |
| 249 | - } | |
| 144 | + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { | |
| 145 | + $new_values['status'] = $values['new_status']; | |
| 146 | + } | |
| 250 | 147 | |
| 251 | - if ( ! empty( $new_values ) ) { | |
| 148 | + if ( ! empty( $new_values ) ) { | |
| 252 | 149 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
| 253 | - if ( $query_results ) { | |
| 150 | + if ( $query_results ) { | |
| 254 | 151 | self::clear_form_cache(); |
| 255 | - } | |
| 256 | - } else { | |
| 257 | - $query_results = true; | |
| 258 | - } | |
| 152 | + } | |
| 153 | + } else { | |
| 154 | + $query_results = true; | |
| 155 | + } | |
| 259 | 156 | unset( $new_values ); |
| 260 | 157 | |
| 261 | 158 | $values = self::update_fields( $id, $values ); |
| 262 | 159 | |
| @@ -262,54 +159,42 @@ | ||
| 262 | 159 | |
| 263 | 160 | do_action( 'frm_update_form', $id, $values ); |
| 264 | 161 | do_action( 'frm_update_form_' . $id, $values ); |
| 265 | 162 | |
| 266 | - return $query_results; | |
| 267 | - } | |
| 163 | + return $query_results; | |
| 164 | + } | |
| 268 | 165 | |
| 269 | - /** | |
| 270 | - * @param array $new_values | |
| 271 | - * @param array $values | |
| 272 | - * @param array $args | |
| 273 | - * @return array | |
| 274 | - */ | |
| 275 | - public static function set_update_options( $new_values, $values, $args = array() ) { | |
| 166 | + /** | |
| 167 | + * @return array | |
| 168 | + */ | |
| 169 | + public static function set_update_options( $new_values, $values ) { | |
| 276 | 170 | if ( ! isset( $values['options'] ) ) { |
| 277 | - return $new_values; | |
| 278 | - } | |
| 171 | + return $new_values; | |
| 172 | + } | |
| 279 | 173 | |
| 280 | 174 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 281 | 175 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 282 | 176 | |
| 283 | 177 | $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0; |
| 284 | - $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); | |
| 285 | - $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 286 | - $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 178 | + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); | |
| 179 | + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 180 | + $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 287 | 181 | |
| 288 | - /** | |
| 289 | - * Allows modifying form options before updating or creating. | |
| 290 | - * | |
| 291 | - * @since 5.4 Added the third param. | |
| 292 | - * | |
| 293 | - * @param array $options Form options. | |
| 294 | - * @param array $values Form data. | |
| 295 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 296 | - */ | |
| 297 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, true ); | |
| 298 | - $options = self::maybe_filter_form_options( $options ); | |
| 182 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 299 | 183 | $new_values['options'] = serialize( $options ); |
| 300 | 184 | |
| 301 | 185 | return $new_values; |
| 302 | - } | |
| 186 | + } | |
| 303 | 187 | |
| 304 | - /** | |
| 305 | - * @return array | |
| 306 | - */ | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * @return array | |
| 191 | + */ | |
| 307 | 192 | public static function update_fields( $id, $values ) { |
| 308 | 193 | |
| 309 | 194 | if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) { |
| 310 | - return $values; | |
| 311 | - } | |
| 195 | + return $values; | |
| 196 | + } | |
| 312 | 197 | |
| 313 | 198 | $all_fields = FrmField::get_all_for_form( $id ); |
| 314 | 199 | if ( empty( $all_fields ) ) { |
| 315 | 200 | return $values; |
| @@ -318,28 +203,28 @@ | ||
| 318 | 203 | if ( ! isset( $values['item_meta'] ) ) { |
| 319 | 204 | $values['item_meta'] = array(); |
| 320 | 205 | } |
| 321 | 206 | |
| 322 | - $field_array = array(); | |
| 323 | - $existing_keys = array_keys( $values['item_meta'] ); | |
| 324 | - foreach ( $all_fields as $fid ) { | |
| 207 | + $field_array = array(); | |
| 208 | + $existing_keys = array_keys( $values['item_meta'] ); | |
| 209 | + foreach ( $all_fields as $fid ) { | |
| 325 | 210 | if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) { |
| 326 | 211 | $values['item_meta'][ $fid->id ] = ''; |
| 327 | - } | |
| 212 | + } | |
| 328 | 213 | $field_array[ $fid->id ] = $fid; |
| 329 | - } | |
| 214 | + } | |
| 330 | 215 | unset( $all_fields ); |
| 331 | 216 | |
| 332 | - foreach ( $values['item_meta'] as $field_id => $default_value ) { | |
| 217 | + foreach ( $values['item_meta'] as $field_id => $default_value ) { | |
| 333 | 218 | if ( isset( $field_array[ $field_id ] ) ) { |
| 334 | 219 | $field = $field_array[ $field_id ]; |
| 335 | - } else { | |
| 220 | + } else { | |
| 336 | 221 | $field = FrmField::getOne( $field_id ); |
| 337 | - } | |
| 222 | + } | |
| 338 | 223 | |
| 339 | - if ( ! $field ) { | |
| 340 | - continue; | |
| 341 | - } | |
| 224 | + if ( ! $field ) { | |
| 225 | + continue; | |
| 226 | + } | |
| 342 | 227 | |
| 343 | 228 | $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
| 344 | 229 | if ( $is_settings_page ) { |
| 345 | 230 | self::get_settings_page_html( $values, $field ); |
| @@ -356,113 +241,47 @@ | ||
| 356 | 241 | |
| 357 | 242 | foreach ( $update_options as $opt => $default ) { |
| 358 | 243 | $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default; |
| 359 | 244 | self::sanitize_field_opt( $opt, $field->field_options[ $opt ] ); |
| 360 | - } | |
| 245 | + } | |
| 361 | 246 | |
| 362 | 247 | $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values ); |
| 248 | + $default_value = maybe_serialize( $values['item_meta'][ $field_id ] ); | |
| 363 | 249 | |
| 364 | 250 | $new_field = array( |
| 365 | 251 | 'field_options' => $field->field_options, |
| 366 | - 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '', | |
| 252 | + 'default_value' => $default_value, | |
| 367 | 253 | ); |
| 368 | 254 | |
| 369 | - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { | |
| 370 | - foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) { | |
| 371 | - if ( is_array( $option ) ) { | |
| 372 | - foreach ( $option as $key => $item ) { | |
| 373 | - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' ); | |
| 374 | - } | |
| 375 | - } | |
| 376 | - } | |
| 377 | - } | |
| 378 | - | |
| 379 | 255 | self::prepare_field_update_values( $field, $values, $new_field ); |
| 380 | 256 | |
| 381 | 257 | FrmField::update( $field_id, $new_field ); |
| 382 | 258 | |
| 383 | 259 | FrmField::delete_form_transient( $field->form_id ); |
| 384 | - } | |
| 260 | + } | |
| 385 | 261 | self::clear_form_cache(); |
| 386 | 262 | |
| 387 | - return $values; | |
| 388 | - } | |
| 263 | + return $values; | |
| 264 | + } | |
| 389 | 265 | |
| 390 | - /** | |
| 391 | - * @param string $opt | |
| 392 | - * @param mixed $value | |
| 393 | - * @return void | |
| 394 | - */ | |
| 395 | 266 | private static function sanitize_field_opt( $opt, &$value ) { |
| 396 | - if ( ! is_string( $value ) ) { | |
| 397 | - return; | |
| 267 | + if ( is_string( $value ) ) { | |
| 268 | + if ( $opt === 'calc' ) { | |
| 269 | + $value = strip_tags( $value ); | |
| 270 | + } else { | |
| 271 | + $value = FrmAppHelper::kses( $value, 'all' ); | |
| 272 | + } | |
| 273 | + $value = trim( $value ); | |
| 398 | 274 | } |
| 399 | - | |
| 400 | - /** | |
| 401 | - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead. | |
| 402 | - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized. | |
| 403 | - * | |
| 404 | - * @since 6.0 | |
| 405 | - * | |
| 406 | - * @param bool $should_sanitize | |
| 407 | - * @param string $opt | |
| 408 | - */ | |
| 409 | - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt ); | |
| 410 | - | |
| 411 | - if ( ! $should_sanitize ) { | |
| 412 | - return; | |
| 413 | - } | |
| 414 | - | |
| 415 | - if ( $opt === 'calc' ) { | |
| 416 | - $value = self::sanitize_calc( $value ); | |
| 417 | - } else { | |
| 418 | - $value = FrmAppHelper::kses( $value, 'all' ); | |
| 419 | - } | |
| 420 | - | |
| 421 | - $value = trim( $value ); | |
| 422 | 275 | } |
| 423 | 276 | |
| 424 | 277 | /** |
| 425 | - * @param string $value | |
| 426 | - * @return string | |
| 427 | - */ | |
| 428 | - private static function sanitize_calc( $value ) { | |
| 429 | - if ( false !== strpos( $value, '<' ) ) { | |
| 430 | - $value = self::normalize_calc_spaces( $value ); | |
| 431 | - } | |
| 432 | - $allow = array( '<= ', ' >=' ); // Allow <= and >= | |
| 433 | - $temp = array( '< = ', ' > =' ); | |
| 434 | - $value = str_replace( $allow, $temp, $value ); | |
| 435 | - $value = strip_tags( $value ); | |
| 436 | - $value = str_replace( $temp, $allow, $value ); | |
| 437 | - return $value; | |
| 438 | - } | |
| 439 | - | |
| 440 | - /** | |
| 441 | - * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations. | |
| 442 | - * This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called. | |
| 443 | - * | |
| 444 | - * @param string $calc | |
| 445 | - * @return string | |
| 446 | - */ | |
| 447 | - private static function normalize_calc_spaces( $calc ) { | |
| 448 | - // Check for a pattern with 5 parts | |
| 449 | - // $1 \d the first comparison digit. | |
| 450 | - // $2 a space (optional). | |
| 451 | - // $3 an equals sign (optional) that follows the < operator for <= comparisons. | |
| 452 | - // $4 another space (optional). | |
| 453 | - // $5 \d the second comparison digit. | |
| 454 | - return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc ); | |
| 455 | - } | |
| 456 | - | |
| 457 | - /** | |
| 458 | 278 | * Updating the settings page |
| 459 | 279 | */ |
| 460 | 280 | private static function get_settings_page_html( $values, &$field ) { |
| 461 | 281 | if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) { |
| 462 | - $prev_opts = array(); | |
| 282 | + $prev_opts = array(); | |
| 463 | 283 | $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ); |
| 464 | - | |
| 465 | 284 | $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html; |
| 466 | 285 | } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
| 467 | 286 | $prev_opts = $field->field_options; |
| 468 | 287 | } |
| @@ -476,9 +295,8 @@ | ||
| 476 | 295 | } |
| 477 | 296 | |
| 478 | 297 | private static function prepare_field_update_values( $field, $values, &$new_field ) { |
| 479 | 298 | $field_cols = array( |
| 480 | - 'field_order' => 0, | |
| 481 | 299 | 'field_key' => '', |
| 482 | 300 | 'required' => false, |
| 483 | 301 | 'type' => '', |
| 484 | 302 | 'description' => '', |
| @@ -486,16 +304,10 @@ | ||
| 486 | 304 | 'name' => '', |
| 487 | 305 | ); |
| 488 | 306 | foreach ( $field_cols as $col => $default ) { |
| 489 | 307 | $default = ( $default === '' ) ? $field->{$col} : $default; |
| 490 | - | |
| 491 | 308 | $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; |
| 492 | 309 | } |
| 493 | - | |
| 494 | - // Don't save the template option. | |
| 495 | - if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) { | |
| 496 | - unset( $new_field['options']['000'] ); | |
| 497 | - } | |
| 498 | 310 | } |
| 499 | 311 | |
| 500 | 312 | /** |
| 501 | 313 | * Get a list of all form settings that should be translated |
| @@ -510,71 +322,66 @@ | ||
| 510 | 322 | 'description', |
| 511 | 323 | 'submit_value', |
| 512 | 324 | 'submit_msg', |
| 513 | 325 | 'success_msg', |
| 514 | - 'invalid_msg', | |
| 515 | - 'failed_msg', | |
| 516 | - 'login_msg', | |
| 517 | - 'admin_permission', | |
| 518 | 326 | ); |
| 519 | 327 | |
| 520 | 328 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 521 | 329 | } |
| 522 | 330 | |
| 523 | - /** | |
| 524 | - * @param string $status | |
| 525 | - * | |
| 526 | - * @return int|boolean | |
| 527 | - */ | |
| 331 | + /** | |
| 332 | + * @param string $status | |
| 333 | + * @return int|boolean | |
| 334 | + */ | |
| 528 | 335 | public static function set_status( $id, $status ) { |
| 529 | - if ( 'trash' == $status ) { | |
| 336 | + if ( 'trash' == $status ) { | |
| 530 | 337 | return self::trash( $id ); |
| 531 | - } | |
| 338 | + } | |
| 532 | 339 | |
| 533 | - $statuses = array( 'published', 'draft', 'trash' ); | |
| 534 | - if ( ! in_array( $status, $statuses ) ) { | |
| 535 | - return false; | |
| 536 | - } | |
| 340 | + $statuses = array( 'published', 'draft', 'trash' ); | |
| 341 | + if ( ! in_array( $status, $statuses ) ) { | |
| 342 | + return false; | |
| 343 | + } | |
| 537 | 344 | |
| 538 | - global $wpdb; | |
| 345 | + global $wpdb; | |
| 539 | 346 | |
| 540 | 347 | if ( is_array( $id ) ) { |
| 541 | 348 | $where = array( |
| 542 | - 'id' => $id, | |
| 349 | + 'id' => $id, | |
| 543 | 350 | 'parent_form_id' => $id, |
| 544 | - 'or' => 1, | |
| 351 | + 'or' => 1, | |
| 545 | 352 | ); |
| 546 | 353 | FrmDb::get_where_clause_and_values( $where ); |
| 547 | 354 | array_unshift( $where['values'], $status ); |
| 548 | 355 | |
| 549 | - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 550 | - } else { | |
| 356 | + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok. | |
| 357 | + } else { | |
| 551 | 358 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
| 552 | 359 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
| 553 | - } | |
| 360 | + } | |
| 554 | 361 | |
| 555 | - if ( $query_results ) { | |
| 362 | + if ( $query_results ) { | |
| 556 | 363 | self::clear_form_cache(); |
| 557 | - } | |
| 364 | + } | |
| 558 | 365 | |
| 559 | - return $query_results; | |
| 560 | - } | |
| 366 | + return $query_results; | |
| 367 | + } | |
| 561 | 368 | |
| 562 | - /** | |
| 563 | - * @return int|boolean | |
| 564 | - */ | |
| 369 | + /** | |
| 370 | + * @return int|boolean | |
| 371 | + */ | |
| 565 | 372 | public static function trash( $id ) { |
| 566 | - if ( ! EMPTY_TRASH_DAYS ) { | |
| 567 | - return self::destroy( $id ); | |
| 568 | - } | |
| 373 | + if ( ! EMPTY_TRASH_DAYS ) { | |
| 374 | + return self::destroy( $id ); | |
| 375 | + } | |
| 569 | 376 | |
| 570 | 377 | $form = self::getOne( $id ); |
| 571 | - if ( ! $form ) { | |
| 572 | - return false; | |
| 573 | - } | |
| 378 | + if ( ! $form ) { | |
| 379 | + return false; | |
| 380 | + } | |
| 574 | 381 | |
| 575 | - $options = $form->options; | |
| 576 | - $options['trash_time'] = time(); | |
| 382 | + $options = $form->options; | |
| 383 | + $options['trash_time'] = time(); | |
| 577 | 384 | |
| 578 | 385 | global $wpdb; |
| 579 | 386 | $query_results = $wpdb->update( |
| 580 | 387 | $wpdb->prefix . 'frm_forms', |
| @@ -597,28 +404,28 @@ | ||
| 597 | 404 | 'parent_form_id' => $id, |
| 598 | 405 | ) |
| 599 | 406 | ); |
| 600 | 407 | |
| 601 | - if ( $query_results ) { | |
| 408 | + if ( $query_results ) { | |
| 602 | 409 | self::clear_form_cache(); |
| 603 | - } | |
| 410 | + } | |
| 604 | 411 | |
| 605 | - return $query_results; | |
| 606 | - } | |
| 412 | + return $query_results; | |
| 413 | + } | |
| 607 | 414 | |
| 608 | - /** | |
| 609 | - * @return int|boolean | |
| 610 | - */ | |
| 415 | + /** | |
| 416 | + * @return int|boolean | |
| 417 | + */ | |
| 611 | 418 | public static function destroy( $id ) { |
| 612 | - global $wpdb; | |
| 419 | + global $wpdb; | |
| 613 | 420 | |
| 614 | 421 | $form = self::getOne( $id ); |
| 615 | - if ( ! $form ) { | |
| 616 | - return false; | |
| 617 | - } | |
| 422 | + if ( ! $form ) { | |
| 423 | + return false; | |
| 424 | + } | |
| 618 | 425 | $id = $form->id; |
| 619 | 426 | |
| 620 | - // Disconnect the entries from this form | |
| 427 | + // Disconnect the entries from this form | |
| 621 | 428 | $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
| 622 | 429 | foreach ( $entries as $entry_id ) { |
| 623 | 430 | FrmEntry::destroy( $entry_id ); |
| 624 | 431 | unset( $entry_id ); |
| @@ -623,15 +430,15 @@ | ||
| 623 | 430 | FrmEntry::destroy( $entry_id ); |
| 624 | 431 | unset( $entry_id ); |
| 625 | 432 | } |
| 626 | 433 | |
| 627 | - // Disconnect the fields from this form | |
| 434 | + // Disconnect the fields from this form | |
| 628 | 435 | $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 ) ); |
| 629 | 436 | |
| 630 | 437 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 631 | - if ( $query_results ) { | |
| 632 | - // Delete all form actions linked to this form | |
| 633 | - $action_control = FrmFormActionsController::get_form_actions( 'email' ); | |
| 438 | + if ( $query_results ) { | |
| 439 | + // Delete all form actions linked to this form | |
| 440 | + $action_control = FrmFormActionsController::get_form_actions( 'email' ); | |
| 634 | 441 | $action_control->destroy( $id, 'all' ); |
| 635 | 442 | |
| 636 | 443 | // Clear form caching |
| 637 | 444 | self::clear_form_cache(); |
| @@ -637,12 +444,12 @@ | ||
| 637 | 444 | self::clear_form_cache(); |
| 638 | 445 | |
| 639 | 446 | do_action( 'frm_destroy_form', $id ); |
| 640 | 447 | do_action( 'frm_destroy_form_' . $id ); |
| 641 | - } | |
| 448 | + } | |
| 642 | 449 | |
| 643 | - return $query_results; | |
| 644 | - } | |
| 450 | + return $query_results; | |
| 451 | + } | |
| 645 | 452 | |
| 646 | 453 | /** |
| 647 | 454 | * Delete trashed forms based on how long they have been trashed |
| 648 | 455 | * |
| @@ -653,9 +460,9 @@ | ||
| 653 | 460 | |
| 654 | 461 | $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); |
| 655 | 462 | |
| 656 | 463 | if ( ! $trash_forms ) { |
| 657 | - return 0; | |
| 464 | + return; | |
| 658 | 465 | } |
| 659 | 466 | |
| 660 | 467 | if ( empty( $delete_timestamp ) ) { |
| 661 | 468 | $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
| @@ -662,75 +469,66 @@ | ||
| 662 | 469 | } |
| 663 | 470 | |
| 664 | 471 | $count = 0; |
| 665 | 472 | foreach ( $trash_forms as $form ) { |
| 666 | - FrmAppHelper::unserialize_or_decode( $form->options ); | |
| 473 | + $form->options = maybe_unserialize( $form->options ); | |
| 667 | 474 | if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
| 668 | 475 | self::destroy( $form->id ); |
| 669 | - $count ++; | |
| 476 | + $count++; | |
| 670 | 477 | } |
| 671 | 478 | |
| 672 | 479 | unset( $form ); |
| 673 | 480 | } |
| 674 | - | |
| 675 | 481 | return $count; |
| 676 | 482 | } |
| 677 | 483 | |
| 678 | - /** | |
| 679 | - * @return string form name | |
| 680 | - */ | |
| 681 | - public static function getName( $id ) { | |
| 484 | + /** | |
| 485 | + * @return string form name | |
| 486 | + */ | |
| 487 | + public static function getName( $id ) { | |
| 682 | 488 | $form = FrmDb::check_cache( $id, 'frm_form' ); |
| 683 | - if ( $form ) { | |
| 489 | + if ( $form ) { | |
| 684 | 490 | $r = stripslashes( $form->name ); |
| 491 | + return $r; | |
| 492 | + } | |
| 685 | 493 | |
| 686 | - return $r; | |
| 687 | - } | |
| 494 | + $query_key = is_numeric( $id ) ? 'id' : 'form_key'; | |
| 495 | + $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); | |
| 496 | + $r = stripslashes( $r ); | |
| 688 | 497 | |
| 689 | - $query_key = is_numeric( $id ) ? 'id' : 'form_key'; | |
| 690 | - $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); | |
| 498 | + return $r; | |
| 499 | + } | |
| 691 | 500 | |
| 692 | - // An empty form name can result in a null value. | |
| 693 | - $r = is_null( $r ) ? '' : stripslashes( $r ); | |
| 694 | - | |
| 695 | - return $r; | |
| 696 | - } | |
| 697 | - | |
| 698 | - /** | |
| 501 | + /** | |
| 699 | 502 | * @since 3.0 |
| 700 | - * | |
| 701 | - * @param string $key | |
| 702 | - * | |
| 703 | - * @return int form id | |
| 704 | - */ | |
| 503 | + * @param string $key | |
| 504 | + * @return int form id | |
| 505 | + */ | |
| 705 | 506 | public static function get_id_by_key( $key ) { |
| 706 | 507 | return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) ); |
| 707 | - } | |
| 508 | + } | |
| 708 | 509 | |
| 709 | - /** | |
| 510 | + /** | |
| 710 | 511 | * @since 3.0 |
| 711 | - * | |
| 712 | - * @param int $id | |
| 713 | - * | |
| 714 | - * @return string form key | |
| 715 | - */ | |
| 512 | + * @param int $id | |
| 513 | + * @return string form key | |
| 514 | + */ | |
| 716 | 515 | public static function get_key_by_id( $id ) { |
| 717 | - $id = (int) $id; | |
| 516 | + $id = (int) $id; | |
| 718 | 517 | $cache = FrmDb::check_cache( $id, 'frm_form' ); |
| 719 | - if ( $cache ) { | |
| 720 | - return $cache->form_key; | |
| 721 | - } | |
| 518 | + if ( $cache ) { | |
| 519 | + return $cache->form_key; | |
| 520 | + } | |
| 722 | 521 | |
| 723 | - $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); | |
| 522 | + $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); | |
| 724 | 523 | |
| 725 | - return $key; | |
| 726 | - } | |
| 524 | + return $key; | |
| 525 | + } | |
| 727 | 526 | |
| 728 | 527 | /** |
| 729 | 528 | * If $form is numeric, get the form object |
| 730 | 529 | * |
| 731 | 530 | * @param object|int $form |
| 732 | - * | |
| 733 | 531 | * @since 2.0.9 |
| 734 | 532 | */ |
| 735 | 533 | public static function maybe_get_form( &$form ) { |
| 736 | 534 | if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
| @@ -737,53 +535,52 @@ | ||
| 737 | 535 | $form = self::getOne( $form ); |
| 738 | 536 | } |
| 739 | 537 | } |
| 740 | 538 | |
| 741 | - /** | |
| 742 | - * @return object form | |
| 743 | - */ | |
| 744 | - public static function getOne( $id, $blog_id = false ) { | |
| 745 | - global $wpdb; | |
| 539 | + /** | |
| 540 | + * @return object form | |
| 541 | + */ | |
| 542 | + public static function getOne( $id, $blog_id = false ) { | |
| 543 | + global $wpdb; | |
| 746 | 544 | |
| 747 | - if ( $blog_id && is_multisite() ) { | |
| 748 | - global $wpmuBaseTablePrefix; | |
| 545 | + if ( $blog_id && is_multisite() ) { | |
| 546 | + global $wpmuBaseTablePrefix; | |
| 749 | 547 | $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); |
| 750 | 548 | |
| 751 | 549 | $table_name = $prefix . 'frm_forms'; |
| 752 | - } else { | |
| 550 | + } else { | |
| 753 | 551 | $table_name = $wpdb->prefix . 'frm_forms'; |
| 754 | - $cache = wp_cache_get( $id, 'frm_form' ); | |
| 755 | - if ( $cache ) { | |
| 552 | + $cache = wp_cache_get( $id, 'frm_form' ); | |
| 553 | + if ( $cache ) { | |
| 756 | 554 | if ( isset( $cache->options ) ) { |
| 757 | - FrmAppHelper::unserialize_or_decode( $cache->options ); | |
| 758 | - } | |
| 555 | + $cache->options = maybe_unserialize( $cache->options ); | |
| 556 | + } | |
| 759 | 557 | |
| 760 | - return apply_filters( 'frm_form_object', wp_unslash( $cache ) ); | |
| 761 | - } | |
| 762 | - } | |
| 558 | + return stripslashes_deep( $cache ); | |
| 559 | + } | |
| 560 | + } | |
| 763 | 561 | |
| 764 | 562 | if ( is_numeric( $id ) ) { |
| 765 | - $where = array( 'id' => $id ); | |
| 766 | - } else { | |
| 767 | - $where = array( 'form_key' => $id ); | |
| 768 | - } | |
| 563 | + $where = array( 'id' => $id ); | |
| 564 | + } else { | |
| 565 | + $where = array( 'form_key' => $id ); | |
| 566 | + } | |
| 769 | 567 | |
| 770 | - $results = FrmDb::get_row( $table_name, $where ); | |
| 568 | + $results = FrmDb::get_row( $table_name, $where ); | |
| 771 | 569 | |
| 772 | 570 | if ( isset( $results->options ) ) { |
| 773 | 571 | FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
| 774 | - FrmAppHelper::unserialize_or_decode( $results->options ); | |
| 775 | - } | |
| 572 | + $results->options = maybe_unserialize( $results->options ); | |
| 573 | + } | |
| 574 | + return stripslashes_deep( $results ); | |
| 575 | + } | |
| 776 | 576 | |
| 777 | - return apply_filters( 'frm_form_object', wp_unslash( $results ) ); | |
| 778 | - } | |
| 779 | - | |
| 780 | - /** | |
| 781 | - * @return object|array of objects | |
| 782 | - */ | |
| 577 | + /** | |
| 578 | + * @return object|array of objects | |
| 579 | + */ | |
| 783 | 580 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 784 | 581 | if ( is_array( $where ) && ! empty( $where ) ) { |
| 785 | - if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { | |
| 582 | + if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) ) { | |
| 786 | 583 | // don't get trashed templates |
| 787 | 584 | $where['status'] = array( null, '', 'published' ); |
| 788 | 585 | } |
| 789 | 586 | |
| @@ -791,16 +588,16 @@ | ||
| 791 | 588 | } else { |
| 792 | 589 | global $wpdb; |
| 793 | 590 | |
| 794 | 591 | // the query has already been prepared if this is not an array |
| 795 | - $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); | |
| 796 | - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 592 | + $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); | |
| 593 | + $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok. | |
| 797 | 594 | } |
| 798 | 595 | |
| 799 | 596 | if ( $results ) { |
| 800 | 597 | foreach ( $results as $result ) { |
| 801 | 598 | FrmDb::set_cache( $result->id, $result, 'frm_form' ); |
| 802 | - FrmAppHelper::unserialize_or_decode( $result->options ); | |
| 599 | + $result->options = maybe_unserialize( $result->options ); | |
| 803 | 600 | } |
| 804 | 601 | } |
| 805 | 602 | |
| 806 | 603 | if ( $limit == ' LIMIT 1' || $limit == 1 ) { |
| @@ -807,45 +604,40 @@ | ||
| 807 | 604 | // return the first form object if we are only getting one form |
| 808 | 605 | $results = reset( $results ); |
| 809 | 606 | } |
| 810 | 607 | |
| 811 | - return wp_unslash( $results ); | |
| 812 | - } | |
| 608 | + return stripslashes_deep( $results ); | |
| 609 | + } | |
| 813 | 610 | |
| 814 | 611 | /** |
| 815 | 612 | * Get all published forms |
| 816 | 613 | * |
| 817 | 614 | * @since 2.0 |
| 818 | - * | |
| 819 | - * @param array $query | |
| 820 | - * @param int $limit | |
| 821 | - * @param string $inc_children | |
| 822 | - * @return array|object of forms A single form object would be passed if $limit was set to 1. | |
| 615 | + * @return array of forms | |
| 823 | 616 | */ |
| 824 | 617 | public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
| 825 | 618 | $query['is_template'] = 0; |
| 826 | - $query['status'] = array( null, '', 'published' ); | |
| 619 | + $query['status'] = array( null, '', 'published' ); | |
| 827 | 620 | if ( $inc_children == 'exclude' ) { |
| 828 | 621 | $query['parent_form_id'] = array( null, 0 ); |
| 829 | 622 | } |
| 830 | 623 | |
| 831 | 624 | $forms = self::getAll( $query, 'name', $limit ); |
| 832 | - | |
| 833 | 625 | return $forms; |
| 834 | 626 | } |
| 835 | 627 | |
| 836 | - /** | |
| 837 | - * @return object count of forms | |
| 838 | - */ | |
| 839 | - public static function get_count() { | |
| 840 | - global $wpdb; | |
| 628 | + /** | |
| 629 | + * @return object count of forms | |
| 630 | + */ | |
| 631 | + public static function get_count() { | |
| 632 | + global $wpdb; | |
| 841 | 633 | |
| 842 | - $cache_key = 'frm_form_counts'; | |
| 634 | + $cache_key = 'frm_form_counts'; | |
| 843 | 635 | |
| 844 | - $counts = wp_cache_get( $cache_key, 'frm_form' ); | |
| 845 | - if ( false !== $counts ) { | |
| 846 | - return $counts; | |
| 847 | - } | |
| 636 | + $counts = wp_cache_get( $cache_key, 'frm_form' ); | |
| 637 | + if ( false !== $counts ) { | |
| 638 | + return $counts; | |
| 639 | + } | |
| 848 | 640 | |
| 849 | 641 | $results = (array) FrmDb::get_results( |
| 850 | 642 | 'frm_forms', |
| 851 | 643 | array( |
| @@ -856,33 +648,33 @@ | ||
| 856 | 648 | 'status, is_template' |
| 857 | 649 | ); |
| 858 | 650 | |
| 859 | 651 | $statuses = array( 'published', 'draft', 'template', 'trash' ); |
| 860 | - $counts = array_fill_keys( $statuses, 0 ); | |
| 652 | + $counts = array_fill_keys( $statuses, 0 ); | |
| 861 | 653 | |
| 862 | - foreach ( $results as $row ) { | |
| 863 | - if ( 'trash' != $row->status ) { | |
| 864 | - if ( $row->is_template ) { | |
| 865 | - $counts['template'] ++; | |
| 866 | - } else { | |
| 867 | - $counts['published'] ++; | |
| 868 | - } | |
| 869 | - } else { | |
| 870 | - $counts['trash'] ++; | |
| 871 | - } | |
| 654 | + foreach ( $results as $row ) { | |
| 655 | + if ( 'trash' != $row->status ) { | |
| 656 | + if ( $row->is_template ) { | |
| 657 | + $counts['template']++; | |
| 658 | + } else { | |
| 659 | + $counts['published']++; | |
| 660 | + } | |
| 661 | + } else { | |
| 662 | + $counts['trash']++; | |
| 663 | + } | |
| 872 | 664 | |
| 873 | - if ( 'draft' == $row->status ) { | |
| 874 | - $counts['draft'] ++; | |
| 875 | - } | |
| 665 | + if ( 'draft' == $row->status ) { | |
| 666 | + $counts['draft']++; | |
| 667 | + } | |
| 876 | 668 | |
| 877 | 669 | unset( $row ); |
| 878 | - } | |
| 670 | + } | |
| 879 | 671 | |
| 880 | - $counts = (object) $counts; | |
| 672 | + $counts = (object) $counts; | |
| 881 | 673 | FrmDb::set_cache( $cache_key, $counts, 'frm_form' ); |
| 882 | 674 | |
| 883 | - return $counts; | |
| 884 | - } | |
| 675 | + return $counts; | |
| 676 | + } | |
| 885 | 677 | |
| 886 | 678 | /** |
| 887 | 679 | * Clear form caching |
| 888 | 680 | * Called when a form is created, updated, duplicated, or deleted |
| @@ -893,16 +685,16 @@ | ||
| 893 | 685 | public static function clear_form_cache() { |
| 894 | 686 | FrmDb::cache_delete_group( 'frm_form' ); |
| 895 | 687 | } |
| 896 | 688 | |
| 897 | - /** | |
| 898 | - * @return array of errors | |
| 899 | - */ | |
| 689 | + /** | |
| 690 | + * @return array of errors | |
| 691 | + */ | |
| 900 | 692 | public static function validate( $values ) { |
| 901 | - $errors = array(); | |
| 693 | + $errors = array(); | |
| 902 | 694 | |
| 903 | 695 | return apply_filters( 'frm_validate_form', $errors, $values ); |
| 904 | - } | |
| 696 | + } | |
| 905 | 697 | |
| 906 | 698 | public static function get_params( $form = null ) { |
| 907 | 699 | global $frm_vars; |
| 908 | 700 | |
| @@ -915,10 +707,10 @@ | ||
| 915 | 707 | if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) { |
| 916 | 708 | return $frm_vars['form_params'][ $form->id ]; |
| 917 | 709 | } |
| 918 | 710 | |
| 919 | - $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 920 | - $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form ); | |
| 711 | + $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok. | |
| 712 | + $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form ); | |
| 921 | 713 | |
| 922 | 714 | $default_values = array( |
| 923 | 715 | 'id' => '', |
| 924 | 716 | 'form_name' => '', |
| @@ -931,9 +723,9 @@ | ||
| 931 | 723 | 'sdir' => '', |
| 932 | 724 | 'action' => $action, |
| 933 | 725 | ); |
| 934 | 726 | |
| 935 | - $values = array(); | |
| 727 | + $values = array(); | |
| 936 | 728 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' ); |
| 937 | 729 | if ( ! $values['posted_form_id'] ) { |
| 938 | 730 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
| 939 | 731 | } |
| @@ -954,11 +746,9 @@ | ||
| 954 | 746 | unset( $var, $default ); |
| 955 | 747 | } |
| 956 | 748 | } |
| 957 | 749 | |
| 958 | - if ( in_array( $values['action'], array( 'create', 'update' ) ) && | |
| 959 | - ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 960 | - ) { | |
| 750 | + if ( in_array( $values['action'], array( 'create', 'update' ) ) && ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) ) { // WPCS: CSRF ok. | |
| 961 | 751 | $values['action'] = 'new'; |
| 962 | 752 | } |
| 963 | 753 | |
| 964 | 754 | return $values; |
| @@ -964,17 +754,17 @@ | ||
| 964 | 754 | return $values; |
| 965 | 755 | } |
| 966 | 756 | |
| 967 | 757 | public static function list_page_params() { |
| 968 | - $values = array(); | |
| 758 | + $values = array(); | |
| 969 | 759 | $defaults = array( |
| 970 | 760 | 'template' => 0, |
| 971 | - 'id' => '', | |
| 972 | - 'paged' => 1, | |
| 973 | - 'form' => '', | |
| 974 | - 'search' => '', | |
| 975 | - 'sort' => '', | |
| 976 | - 'sdir' => '', | |
| 761 | + 'id' => '', | |
| 762 | + 'paged' => 1, | |
| 763 | + 'form' => '', | |
| 764 | + 'search' => '', | |
| 765 | + 'sort' => '', | |
| 766 | + 'sdir' => '', | |
| 977 | 767 | ); |
| 978 | 768 | foreach ( $defaults as $var => $default ) { |
| 979 | 769 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 980 | 770 | } |
| @@ -985,13 +775,13 @@ | ||
| 985 | 775 | public static function get_admin_params( $form = null ) { |
| 986 | 776 | $form_id = $form; |
| 987 | 777 | if ( $form === null ) { |
| 988 | 778 | $form_id = self::get_current_form_id(); |
| 989 | - } elseif ( $form && is_object( $form ) ) { | |
| 779 | + } else if ( $form && is_object( $form ) ) { | |
| 990 | 780 | $form_id = $form->id; |
| 991 | 781 | } |
| 992 | 782 | |
| 993 | - $values = array(); | |
| 783 | + $values = array(); | |
| 994 | 784 | $defaults = array( |
| 995 | 785 | 'id' => '', |
| 996 | 786 | 'form_name' => '', |
| 997 | 787 | 'paged' => 1, |
| @@ -1031,9 +821,8 @@ | ||
| 1031 | 821 | $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
| 1032 | 822 | if ( $form_id ) { |
| 1033 | 823 | $form_id = self::set_current_form( $form_id ); |
| 1034 | 824 | } |
| 1035 | - | |
| 1036 | 825 | return $form_id; |
| 1037 | 826 | } |
| 1038 | 827 | |
| 1039 | 828 | public static function get_current_form( $form_id = 0 ) { |
| @@ -1038,11 +827,10 @@ | ||
| 1038 | 827 | |
| 1039 | 828 | public static function get_current_form( $form_id = 0 ) { |
| 1040 | 829 | $form = self::maybe_get_current_form( $form_id ); |
| 1041 | 830 | if ( is_numeric( $form ) ) { |
| 1042 | - $form = self::set_current_form( $form ); | |
| 831 | + $form = self::set_current_form( $form ); | |
| 1043 | 832 | } |
| 1044 | - | |
| 1045 | 833 | return $form; |
| 1046 | 834 | } |
| 1047 | 835 | |
| 1048 | 836 | public static function set_current_form( $form_id ) { |
| @@ -1068,9 +856,9 @@ | ||
| 1068 | 856 | |
| 1069 | 857 | $frm_vars['forms_loaded'][] = $small_form; |
| 1070 | 858 | |
| 1071 | 859 | if ( $this_load && empty( $global_load ) ) { |
| 1072 | - $global_load = true; | |
| 860 | + $global_load = true; | |
| 1073 | 861 | $frm_vars['load_css'] = true; |
| 1074 | 862 | } |
| 1075 | 863 | |
| 1076 | 864 | return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load ); |
| @@ -1075,29 +863,11 @@ | ||
| 1075 | 863 | |
| 1076 | 864 | return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load ); |
| 1077 | 865 | } |
| 1078 | 866 | |
| 1079 | - /** | |
| 1080 | - * @since 4.06.03 | |
| 1081 | - * | |
| 1082 | - * @param object $form | |
| 1083 | - * | |
| 1084 | - * @return bool | |
| 1085 | - */ | |
| 1086 | - public static function &is_visible_to_user( $form ) { | |
| 1087 | - if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) { | |
| 1088 | - $visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] ); | |
| 1089 | - } else { | |
| 1090 | - $visible = true; | |
| 1091 | - } | |
| 1092 | - | |
| 1093 | - return $visible; | |
| 1094 | - } | |
| 1095 | - | |
| 1096 | 867 | public static function show_submit( $form ) { |
| 1097 | 868 | $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); |
| 1098 | 869 | $show = apply_filters( 'frm_show_submit_button', $show, $form ); |
| 1099 | - | |
| 1100 | 870 | return $show; |
| 1101 | 871 | } |
| 1102 | 872 | |
| 1103 | 873 | /** |
| @@ -1104,37 +874,13 @@ | ||
| 1104 | 874 | * @since 2.3 |
| 1105 | 875 | */ |
| 1106 | 876 | public static function get_option( $atts ) { |
| 1107 | 877 | $form = $atts['form']; |
| 1108 | - $default = isset( $atts['default'] ) ? $atts['default'] : ''; | |
| 1109 | - | |
| 1110 | - return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default; | |
| 878 | + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default']; | |
| 1111 | 879 | } |
| 1112 | 880 | |
| 1113 | 881 | /** |
| 1114 | - * Get the link to edit this form. | |
| 1115 | - * | |
| 1116 | - * @since 4.0 | |
| 1117 | - * @param int $form_id The id of the form. | |
| 1118 | - */ | |
| 1119 | - public static function get_edit_link( $form_id ) { | |
| 1120 | - return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ); | |
| 1121 | - } | |
| 1122 | - | |
| 1123 | - /** | |
| 1124 | - * Check if the "Submit this form with AJAX" setting is toggled on. | |
| 1125 | - * | |
| 1126 | - * @since 6.2 | |
| 1127 | - * | |
| 1128 | - * @param stdClass $form | |
| 1129 | - * @return bool | |
| 1130 | - */ | |
| 1131 | - public static function is_ajax_on( $form ) { | |
| 1132 | - return ! empty( $form->options['ajax_submit'] ); | |
| 1133 | - } | |
| 1134 | - | |
| 1135 | - /** | |
| 1136 | - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations). | |
| 882 | + * @deprecated 3.0 | |
| 1137 | 883 | * @codeCoverageIgnore |
| 1138 | 884 | * |
| 1139 | 885 | * @param string $key |
| 1140 | 886 | * @return int form id |
| @@ -1139,20 +885,15 @@ | ||
| 1139 | 885 | * @param string $key |
| 1140 | 886 | * @return int form id |
| 1141 | 887 | */ |
| 1142 | 888 | public static function getIdByKey( $key ) { |
| 1143 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' ); | |
| 1144 | - return self::get_id_by_key( $key ); | |
| 889 | + return FrmFormDeprecated::getIdByKey( $key ); | |
| 1145 | 890 | } |
| 1146 | 891 | |
| 1147 | 892 | /** |
| 1148 | - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13. | |
| 893 | + * @deprecated 3.0 | |
| 1149 | 894 | * @codeCoverageIgnore |
| 1150 | - * | |
| 1151 | - * @param string|int $id | |
| 1152 | - * @return string | |
| 1153 | 895 | */ |
| 1154 | 896 | public static function getKeyById( $id ) { |
| 1155 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' ); | |
| 1156 | - return self::get_key_by_id( $id ); | |
| 897 | + return FrmFormDeprecated::getKeyById( $id ); | |
| 1157 | 898 | } |
| 1158 | 899 | } |