DidouS

Disable node attributes when in fl_builder

Jul 4th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. // run these with a priority of 5 so they can remove later callbacks
  2. add_filter( 'fl_builder_row_attributes' , 'remove_toolbox_attr_in_builder' , 5, 2 );
  3. add_filter( 'fl_builder_column_attributes' , 'remove_toolbox_attr_in_builder' , 5, 2 );
  4. add_filter( 'fl_builder_module_attributes' , 'remove_toolbox_attr_in_builder' , 5, 2 );
  5.  
  6. /**
  7.  * When in the builder, test for certain attributes; if present, stop loading these attributes on that node
  8.  *
  9.  * @param  [type] $attrs [description]
  10.  * @param  [type] $node  [description]
  11.  * @return [type]        [description]
  12.  */
  13. function remove_toolbox_attr_in_builder( $attrs , $node ) {
  14.  
  15.     if ( !isset( $_GET['fl_builder'] ) ) return $attrs;
  16.  
  17.     // get the nodetype from the filter hookname
  18.     $nodetype = match_filter_to_nodetype( current_filter() );
  19.  
  20.     if ( isset( $node->settings->toolbox_custom_attr ) ) {
  21.         for ( $i = 0; $i < count( $node->settings->toolbox_custom_attr );$i++ ):
  22.  
  23.             if( empty( $node->settings->toolbox_custom_attr[$i])) {continue;}
  24.  
  25.             switch( $node->settings->toolbox_custom_attr[$i]->my_attr ) {
  26.  
  27.                 // you can add more here by adding more case "attribute-name": instances
  28.                 case "uk-parallax":
  29.                 case "uk-sticky":
  30.                 // case "attribute-name":
  31.                     // remove the filter on this nodetype
  32.                     remove_filter( "fl_builder_{$nodetype}_attributes" , 'toolboxFilters::check_for_extra_toolbox_attributes' , 10 );
  33.  
  34.                     // add a filter that will reset the removed filters so the next row/col/module runs it again
  35.                     add_filter( "fl_builder_{$nodetype}_attributes" , 're_add_attr_in_builder' , 100 , 1 );
  36.  
  37.                     // bail so we don't add more filters
  38.                     return $attrs;
  39.                 break;
  40.             }
  41.  
  42.         endfor;
  43.     }
  44.  
  45.     return $attrs;
  46.  
  47. }
  48.  
  49. /**
  50.  * callback to return the nodetype from the passed in filter hookname
  51.  * @param  [type] $filter [description]
  52.  * @return [type]         [description]
  53.  */
  54. function match_filter_to_nodetype( $filter ) {
  55.  
  56.     $re = '/fl_builder_([a-z]{0,})_attributes/';
  57.  
  58.     preg_match($re, $filter, $matches );
  59.  
  60.     return $matches[1];
  61. }
  62.  
  63. /**
  64.  * Re-add the filter after the fact; remove itself
  65.  *
  66.  * @param  [type] $attrs [description]
  67.  * @return [type]        [description]
  68.  */
  69. function re_add_attr_in_builder( $attrs ) {
  70.  
  71.     // get the nodetype from the filter hookname
  72.     $nodetype = match_filter_to_nodetype( current_filter() );
  73.  
  74.     // re-add the removed filter
  75.     add_filter( "fl_builder_{$nodetype}_attributes" , 'toolboxFilters::check_for_extra_toolbox_attributes' , 10 , 3 );
  76.  
  77.     // remove this method once it has been run
  78.     remove_filter( "fl_builder_{$nodetype}_attributes" , __METHOD__ , 100 );
  79.  
  80.     // don't forget to return the attrs
  81.     return $attrs;
  82. }
Add Comment
Please, Sign In to add comment