Skip to content

Instantly share code, notes, and snippets.

@BobbyRuby
Created December 11, 2019 23:16
Show Gist options
  • Select an option

  • Save BobbyRuby/092e007900095e85bc23e878f5b8d605 to your computer and use it in GitHub Desktop.

Select an option

Save BobbyRuby/092e007900095e85bc23e878f5b8d605 to your computer and use it in GitHub Desktop.
/**
* @param $post_ID
*/
public static function task_type_save_custom_field_builder( $post_ID ) {
$boxes_arr = [];
// Loop through the post keys
foreach ( $_POST as $pk => $d ) {
// Find a field
if ( stripos( $pk, '-fields-' ) ) {
// Get form box number
$fbn = self::task_type_save_get_field_builder_box_fbn( $pk );
// Get the field type
$type = self::task_type_save_get_field_builder_field_type( $pk );
// Get the field count
$fc = self::task_type_save_get_field_builder_field_count( $pk );
// Get the field name
if ( stripos( $pk, 'name' ) ) {
$name = $d;
}
// Get the field description
if ( stripos( $pk, 'description' ) ) {
$description = $d;
}
// If this is a select option
if ( stripos( $pk, 'option_fields' ) ) {
// Set Fields
$boxes_arr[ $fbn ]['fields'][ $fc ]['type'] = $type;
$boxes_arr[ $fbn ]['fields'][ $fc ]['name'] = $name;
$boxes_arr[ $fbn ]['fields'][ $fc ]['description'] = $description;
$boxes_arr[ $fbn ]['fields'][ $fc ]['options'] = $d;
}else {
// Set Fields
$boxes_arr[ $fbn ]['fields'][ $fc ]['type'] = $type;
$boxes_arr[ $fbn ]['fields'][ $fc ]['name'] = $name;
$boxes_arr[ $fbn ]['fields'][ $fc ]['description'] = $description;
}
} else // Find the box name
if ( preg_match( '/form_box_[0-9]+-name/', $pk ) ) {
// Get form box number
$fbn = self::task_type_save_get_field_builder_box_fbn( $pk );
$boxes_arr[ $fbn ]['name'] = $d;
} else // Find the box description
if ( preg_match( '/form_box_[0-9]+-description/', $pk ) ) {
// Get form box number
$fbn = self::task_type_save_get_field_builder_box_fbn( $pk );
$boxes_arr[ $fbn ]['description'] = $d;
}
}
// Reindex array to begin at 1 and remove multiple selects
foreach ($boxes_arr as $key => $box_arr){
// Loop through fields and remove selects with no options
foreach ( $boxes_arr[$key]['fields'] as $fk => $field ) {
// If select
if($field['type'] === 'select'){
// Check for options
if(!array_key_exists('options', $field)){
// No options so unset this select
unset($boxes_arr[$key]['fields'][$fk]);
}
}
}
$boxes_arr[$key]['fields'] = array_values($boxes_arr[$key]['fields']);
array_unshift($boxes_arr[$key]['fields'], "phoney");
unset($boxes_arr[$key]['fields'][0]);
}
delete_post_meta($post_ID, self::PLUGIN_PREFIX . 'task_type_boxes');
update_post_meta( $post_ID, self::PLUGIN_PREFIX . 'task_type_boxes', $boxes_arr );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment