| Conditions | 19 |
| Paths | 176 |
| Total Lines | 113 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 162 | public static function generate_xml( $type, $args = array() ) { |
||
| 163 | global $wpdb; |
||
| 164 | |||
| 165 | $type = (array) $type; |
||
| 166 | if ( in_array( 'items', $type) && ! in_array( 'forms', $type) ) { |
||
| 167 | // make sure the form is included if there are entries |
||
| 168 | $type[] = 'forms'; |
||
| 169 | } |
||
| 170 | |||
| 171 | if ( in_array( 'forms', $type) ) { |
||
| 172 | // include actions with forms |
||
| 173 | $type[] = 'actions'; |
||
| 174 | } |
||
| 175 | |||
| 176 | $tables = array( |
||
| 177 | 'items' => $wpdb->prefix .'frm_items', |
||
| 178 | 'forms' => $wpdb->prefix .'frm_forms', |
||
| 179 | 'posts' => $wpdb->posts, |
||
| 180 | 'styles' => $wpdb->posts, |
||
| 181 | 'actions' => $wpdb->posts, |
||
| 182 | ); |
||
| 183 | |||
| 184 | $defaults = array( 'ids' => false ); |
||
| 185 | $args = wp_parse_args( $args, $defaults ); |
||
| 186 | |||
| 187 | $sitename = sanitize_key( get_bloginfo( 'name' ) ); |
||
| 188 | |||
| 189 | if ( ! empty( $sitename ) ) { |
||
| 190 | $sitename .= '.'; |
||
| 191 | } |
||
| 192 | $filename = $sitename . 'formidable.' . date( 'Y-m-d' ) . '.xml'; |
||
| 193 | |||
| 194 | header( 'Content-Description: File Transfer' ); |
||
| 195 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
||
| 196 | header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
||
| 197 | |||
| 198 | //make sure ids are numeric |
||
| 199 | if ( is_array( $args['ids'] ) && ! empty( $args['ids'] ) ) { |
||
| 200 | $args['ids'] = array_filter( $args['ids'], 'is_numeric' ); |
||
| 201 | } |
||
| 202 | |||
| 203 | $records = array(); |
||
| 204 | |||
| 205 | foreach ( $type as $tb_type ) { |
||
| 206 | $where = array(); |
||
| 207 | $join = ''; |
||
| 208 | $table = $tables[ $tb_type ]; |
||
| 209 | |||
| 210 | $select = $table .'.id'; |
||
| 211 | $query_vars = array(); |
||
| 212 | |||
| 213 | switch ( $tb_type ) { |
||
| 214 | case 'forms': |
||
| 215 | //add forms |
||
| 216 | if ( $args['ids'] ) { |
||
| 217 | $where[] = array( 'or' => 1, $table . '.id' => $args['ids'], $table .'.parent_form_id' => $args['ids'] ); |
||
| 218 | } else { |
||
| 219 | $where[ $table . '.status !' ] = 'draft'; |
||
| 220 | } |
||
| 221 | break; |
||
| 222 | case 'actions': |
||
| 223 | $select = $table .'.ID'; |
||
| 224 | $where['post_type'] = FrmFormActionsController::$action_post_type; |
||
| 225 | if ( ! empty($args['ids']) ) { |
||
| 226 | $where['menu_order'] = $args['ids']; |
||
| 227 | } |
||
| 228 | break; |
||
| 229 | case 'items': |
||
| 230 | //$join = "INNER JOIN {$wpdb->prefix}frm_item_metas im ON ($table.id = im.item_id)"; |
||
| 231 | if ( $args['ids'] ) { |
||
| 232 | $where[ $table . '.form_id' ] = $args['ids']; |
||
| 233 | } |
||
| 234 | break; |
||
| 235 | case 'styles': |
||
| 236 | // Loop through all exported forms and get their selected style IDs |
||
| 237 | $form_ids = $args['ids']; |
||
| 238 | $style_ids = array(); |
||
| 239 | foreach ( $form_ids as $form_id ) { |
||
| 240 | $form_data = FrmForm::getOne( $form_id ); |
||
| 241 | // For forms that have not been updated while running 2.0, check if custom_style is set |
||
| 242 | if ( isset( $form_data->options['custom_style'] ) ) { |
||
| 243 | $style_ids[] = $form_data->options['custom_style']; |
||
| 244 | } |
||
| 245 | unset( $form_id, $form_data ); |
||
| 246 | } |
||
| 247 | $select = $table .'.ID'; |
||
| 248 | $where['post_type'] = 'frm_styles'; |
||
| 249 | |||
| 250 | // Only export selected styles |
||
| 251 | if ( ! empty( $style_ids ) ) { |
||
| 252 | $where['ID'] = $style_ids; |
||
| 253 | } |
||
| 254 | break; |
||
| 255 | default: |
||
| 256 | $select = $table .'.ID'; |
||
| 257 | $join = ' INNER JOIN ' . $wpdb->postmeta . ' pm ON (pm.post_id=' . $table . '.ID)'; |
||
| 258 | $where['pm.meta_key'] = 'frm_form_id'; |
||
| 259 | |||
| 260 | if ( empty($args['ids']) ) { |
||
| 261 | $where['pm.meta_value >'] = 1; |
||
| 262 | } else { |
||
| 263 | $where['pm.meta_value'] = $args['ids']; |
||
| 264 | } |
||
| 265 | break; |
||
| 266 | } |
||
| 267 | |||
| 268 | $records[ $tb_type ] = FrmDb::get_col( $table . $join, $where, $select ); |
||
| 269 | unset($tb_type); |
||
| 270 | } |
||
| 271 | |||
| 272 | echo '<?xml version="1.0" encoding="' . esc_attr( get_bloginfo('charset') ) . "\" ?>\n"; |
||
| 273 | include(FrmAppHelper::plugin_path() .'/classes/views/xml/xml.php'); |
||
| 274 | } |
||
| 275 | |||
| 290 |