function delete_meta_fields_not_needed($object_id, $tt_id)
{
    // if (get_post_type($object_id) === 'soya_feature' && (is_tax('soya-range', $tt_id))){
    $term = get_term($tt_id);
    $term_slug = $term->slug;
    $term_meta_key = $term_slug . '_' . $tt_id;
    if (soya_is_term_child($tt_id)) {
        $term_parent_id = soya_get_term_parent_id($tt_id);
        $parent_term_slug = get_term($term_parent_id);
        $parent_term_depth = soya_get_term_custom_field($term_parent_id, 'soya_depth_meta');
        for ($i = 1; $i <= $parent_term_depth; $i++) {
            delete_post_meta($object_id, $i . '_' . $term_meta_key . '-' . $parent_term_slug . '_' . $term_parent_id);
        }
        // $term_meta_key =
        //  }else{
        delete_post_meta($object_id, 'silicone-effect_200-400');
        // }
    }
}
 public function get_parent_ranges($term_id)
 {
     if (!soya_is_term_child($term_id)) {
         return false;
     }
     $ranges = [];
     $parent_term_id = soya_get_term_parent_id($term_id);
     $depth = soya_get_term_custom_field($parent_term_id, 'soya_depth_meta');
     for ($i = 1; $i <= $depth; $i++) {
         $lower = soya_get_term_custom_field($parent_term_id, 'soya_lower_' . $i . '_depth_meta');
         $upper = soya_get_term_custom_field($parent_term_id, 'soya_upper_' . $i . '_depth_meta');
         $ranges[$lower] = !empty($lower && $upper) ? $lower . '-' . $upper : 0;
     }
     return $ranges;
     //return $this->default_qty;
 }
function soya_delete_term_post_meta_keys($term_id, $tax)
{
    if ($tax !== 'soya-range') {
        return;
    }
    $meta_keys[$term_id] = soya_make_meta_keys($term_id);
    if (soya_term_has_children($term_id) && !soya_is_term_child($term_id)) {
        $term_children = get_term_children($term_id, 'soya-range');
        do_action('delete_unwanted_range_values', $meta_keys, 'parent', $term_children);
    } else {
        do_action('delete_unwanted_range_values', $meta_keys, 'range_change');
    }
}
/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function soya_save_meta_box_data($post_id)
{
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */
    // Check if our nonce is set.
    if (!isset($_POST['soya_meta_box_nonce'])) {
        return;
    }
    // Verify that the nonce is valid.
    if (!wp_verify_nonce($_POST['soya_meta_box_nonce'], 'soya_meta_box')) {
        return;
    }
    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check the user's permissions.
    if (isset($_POST['soya_feature']) && 'page' == $_POST['soya_feature']) {
        if (!current_user_can('edit_page', $post_id)) {
            return;
        }
    } else {
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    }
    /* OK, it's safe for us to save the data now. */
    // Make sure that it is set.
    // get post terms ids
    $post_terms_ids = soya_get_post_terms_ids($post_id);
    //set up all post terms ranges
    $parent_post_terms_ranges = [];
    $child_post_terms_ranges = [];
    if (isset($post_terms_ids)) {
        foreach ($post_terms_ids as $post_term_id) {
            if (soya_is_term_child($post_term_id)) {
                // We want only parents ranges and slugs.
                $child_term_parent_id = esc_attr(soya_get_term_parent_id($post_term_id));
                $post_term_range = soya_loop_term_depth($child_term_parent_id);
                $post_child_term = get_term($post_term_id, 'soya-range');
                // $post_term_depth = count($post_term_range);
                $post_term_depth = soya_get_term_custom_field($post_term_id, 'soya_depth_meta');
                $post_term_slug = $post_child_term->slug;
                $post_term_range['depth'] = $post_term_depth;
                $post_term_range['slug'] = $post_term_slug;
                $child_post_terms_ranges[] = $post_term_range;
            } else {
                $post_term_range = soya_loop_term_depth($post_term_id);
                $parent_post_terms_ranges[] = $post_term_range;
            }
        }
    }
    //get keys from post terms ranges
    $all_meta_keys = [];
    $index = '';
    //Iterate for parent meta keys
    if (isset($parent_post_terms_ranges)) {
        foreach ($parent_post_terms_ranges as $post_term_range) {
            foreach ($post_term_range as $key => $label) {
                $all_meta_keys[] = $key;
            }
        }
    }
    // Build meta keys for child terms
    if (isset($child_post_terms_ranges)) {
        //$index = '';
        // $slug = '';
        foreach ($child_post_terms_ranges as $index => $post_term_range) {
            //get Depth and slug for each child term
            $indexi = $post_term_range['depth'];
            $post_child_term_slug = $post_term_range['slug'];
            // loop through each term's depth and get indices for meta keys
            for ($i = 1; $i <= $indexi; $i++) {
                //remove slug and depth from range arrange
                unset($post_term_range['slug']);
                unset($post_term_range['depth']);
                foreach ($post_term_range as $key => $label) {
                    // if($key != 'slug'):
                    $all_meta_keys[] = $i . '-' . $post_child_term_slug . '-' . $key;
                    // check for added fixed meta fields and save them too
                    if (has_action('soya_add_extra_meta_fields_' . $post_child_term_slug)) {
                        $all_meta_keys[] = $i . '-' . 'fixed-cost-' . $post_child_term_slug;
                    }
                }
            }
        }
    }
    // Apply filters
    $all_meta_keys = apply_filters('soya_filter_meta_keys', $all_meta_keys, $post_terms_ids);
    // Sanitize user input.
    foreach ($all_meta_keys as $key) {
        //check for data
        if (isset($_POST[$key])) {
            $value = sanitize_text_field($_POST[$key]);
        }
        update_post_meta($post_id, $key, $value);
    }
    //Check if a meta key is relevant and delete them
    $before_post_update_post_meta_keys = $_SESSION['post_terms'];
    $unwanted_meta_keys = array_diff($before_post_update_post_meta_keys, $all_meta_keys);
    foreach ($unwanted_meta_keys as $meta_key) {
        delete_post_meta($post_id, $meta_key);
    }
    unset($_SESSION['post_terms']);
}