public function validate_post_relationship_tree() { if (!current_user_can('manage_options')) { echo __('You do not have permissions for that.', 'wpv-views'); die; } if (!wp_verify_nonce($_POST['wpnonce'], 'wpv_parametric_validate_post_relationship_tree')) { die("Security check"); } $return = ''; if ($_POST['local_tree'] == 'tree') { $return = 'OK'; } else { $view_settings = get_post_meta($_POST['id'], '_wpv_settings', true); $returned_post_types = $view_settings['post_type']; $multi_post_relations = wpv_recursive_post_hierarchy($returned_post_types); $flatten_post_relations = wpv_recursive_flatten_post_relationships($multi_post_relations); if (strlen($flatten_post_relations) > 0) { $current_used_tree = explode(',', $_POST['local_tree']); $relations_tree = wpv_get_all_post_relationship_options($flatten_post_relations); $trees_are_valid = true; foreach ($current_used_tree as $current_tree) { if (!in_array($current_tree, $relations_tree)) { $trees_are_valid = false; } } if ($trees_are_valid) { $return = 'OK'; } else { $return = __('Types ancestors tree not valid.', 'wpv-views'); $return .= ' ' . __('Please follow the tip hint below. ', 'wpv-views'); // $return .= '<ul><li><code>' . implode( '</code></li><li><code>', $relations_tree ) .'</code></li></ul>'; } } else { $return = __('The post types selected in this View do not have Types ancestors', 'wpv-views'); } } echo $return; die; }
/** * wpv_recursive_flatten_post_relationships * * Flatten a multidimensional array of Types post relationships * * After creating a multidimensional array of Types post relationships with wpv_recursive_post_hierarchy we use this function to flatten it * and return the same structure as a string using specific signs for parent and adjacent relations * * @param $relations_array (array) multidimensional array of Types post relationships, following the format: * Array( * [father] => Array( * [grandpa-one] => Array( * [bisa] => Array() * ) * [grandma-one] => Array() * ) * [mother] => Array() * ) * @param $low_level (string) auxiliar string containing the lower levels in recursion, defaults to '' * @param $parent_sep (string) separator for parent relationships, defaults to ">" * @param $adjacent_sep (string) separator for adjacent relationships, defaults to "," * * @return (string) * * @note this function is recursive * @note the returning string has the format * bisa>grandpa-one>father,grandma-one>father,mother * * @since 1.6.0 */ function wpv_recursive_flatten_post_relationships( $relations_array, $low_level = '', $parent_sep = '>', $adjacent_sep = ',' ) { $my_aux = array(); foreach ( $relations_array as $my_key => $my_value ) { if ( empty( $low_level ) ) { $my_aux[$my_key] = $my_key; } else { $my_aux[$my_key] = $my_key . $parent_sep . $low_level; } if ( !empty( $my_value ) ) { $my_aux[$my_key] = wpv_recursive_flatten_post_relationships( $my_value, $my_aux[$my_key], $parent_sep, $adjacent_sep ); } } $flatten = implode( $adjacent_sep, $my_aux ); return $flatten; }