diff --git a/includes/class-cf7-2-post-factory.php b/includes/class-cf7-2-post-factory.php index 9be004e..37888d0 100755 --- a/includes/class-cf7-2-post-factory.php +++ b/includes/class-cf7-2-post-factory.php @@ -1436,23 +1436,47 @@ public function save_form_2_post($submission){ // //--------------- taxonomies // - foreach($this->post_map_taxonomy as $form_field => $taxonomy){ - $value = ''; - if( 0 === strpos($form_field,'cf7_2_post_filter-') ) { - $value = apply_filters($form_field, array(), $post_id, $cf7_form_data); - }else if(isset( $cf7_form_data[$form_field] )){ - if( is_array( $cf7_form_data[$form_field] ) ){ - $value = array_map( 'intval', $cf7_form_data[$form_field] ); - }else{ - //debug_msg($cf7_form_data[$form_field], $taxonomy." values "); - $value = array_map( 'intval', array( $cf7_form_data[$form_field] ) ); - } + foreach ( $this->post_map_taxonomy as $form_field => $taxonomy ) { + + $value = isset($cf7_form_data[$form_field]) ? $cf7_form_data[$form_field] : ''; + $filter_name = $form_field; + + // more specific filter names for menu fields + if ( strpos($filter_name, 'cf7_2_post_filter-') === false ) { + $filter_name = 'cf7_2_post_filter-' . $filter_name; + } + + /** + * Filter introduced for plugin developers to map custom plugin tag fields, allows for submitted values to be filtered before being stored. + * @since + * @param mixed $value string|int|array $terms post field value to return, by default it is empty. If you are filtering a taxonomy you can return either slug/id/array. in case of ids make sure to cast them integers.(see https://codex.wordpress.org/Function_Reference/wp_set_object_terms for more information.) + * @param int $post_id ID of the post to which the form values are being mapped to + * @param array $cf7_form_data Submitted form data as an array of field-name=>value pairs Access current field with $cf7_form_data[$form_field] + * + * @return mixed $value string|int|array $terms + */ + $value = apply_filters($filter_name, $value, $post_id, $cf7_form_data); + + // cleanup to array + if ( !is_array($value) ) { + $value = array($value); } - if( !empty($value) ){ - $term_taxonomy_ids = wp_set_object_terms( $post_id , $value, $taxonomy ); - if ( is_wp_error( $term_taxonomy_ids ) ) { - debug_msg($term_taxonomy_ids, " Unable to set taxonomy (".$taxonomy.") terms"); - debug_msg($value, "Attempted to set these term values "); + $value = array_filter($value); + + if ( !empty($value) ) { + + // Check for IDs in strings + array_walk($value, function(&$value_item){ + if ( ctype_digit($value_item) ) { + $value_item = intval($value_item); + } + }); + + $term_taxonomy_ids = wp_set_object_terms($post_id, $value, $taxonomy); + + if ( is_wp_error($term_taxonomy_ids) ) { + debug_msg($term_taxonomy_ids, 'Unable to set taxonomy "' . $taxonomy . '" terms'); + debug_msg($value, 'Attempted to set these term values'); } } }