Advertisement
aazztech13

Sanitize Builder Data

May 13th, 2021 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. add_action( 'init', 'swbd_sanitize_builder_data_structure');
  2.  
  3. // swbd_sanitize_builder_data_structure
  4. function swbd_sanitize_builder_data_structure() {
  5.     $directory_types = get_terms([
  6.         'taxonomy'   => ATBDP_DIRECTORY_TYPE,
  7.         'hide_empty' => false,
  8.     ]);
  9.  
  10.     if ( empty( $directory_types ) ) { return; }
  11.  
  12.     foreach ( $directory_types as $directory_type ) {
  13.         swbd_sanitize_single_listings_contents_data_structure( $directory_type->term_id );
  14.     }
  15. }
  16.  
  17. // swbd_sanitize_single_listings_contents_data_structure
  18. function swbd_sanitize_single_listings_contents_data_structure( $directory_type_id = 0 ) {
  19.     $single_listings_contents = get_term_meta( $directory_type_id, 'single_listings_contents', true );
  20.    
  21.     if ( ! isset( $single_listings_contents['fields'] ) && ! isset( $single_listings_contents['groups'] ) ) return;
  22.     if ( ! is_array( $single_listings_contents['groups'] ) ) return;
  23.    
  24.     $fields_keys = ( is_array( $single_listings_contents['fields'] ) ) ? array_keys( $single_listings_contents['fields'] ) : [];
  25.    
  26.     $fields_log = [];
  27.    
  28.     foreach( $single_listings_contents['groups'] as $group_key => $group ) {
  29.         $fields_in_group = $group['fields'];
  30.        
  31.         foreach ( $fields_keys as $field_key ) {
  32.             if ( ! isset( $fields_log[ $field_key ] ) ) {
  33.                 $fields_log[ $field_key ] = [];
  34.             }
  35.            
  36.             if ( in_array( $field_key, $fields_in_group ) ) {
  37.                 $fields_log[ $field_key ][] = $group_key;
  38.             }
  39.         }
  40.     }
  41.    
  42.     foreach ( $fields_log as $field_key => $field_value ) {
  43.         if ( ! empty( $field_value ) ) continue;
  44.        
  45.         unset( $single_listings_contents['fields'][ $field_key ] );
  46.     }
  47.    
  48.     update_term_meta( $directory_type_id, 'single_listings_contents', $single_listings_contents );
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement