Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor taxonomies saving process #9

Open
wants to merge 5 commits into
base: v5.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions includes/class-cf7-2-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
Expand Down