<commit_before><?php

function wp_bootstrap_dynamic_sidebar_params( $sidebar_params ) {
  if ( is_admin() ) {
    return $sidebar_params;
  }
  foreach($sidebar_params as $index => $widget_params) {
    $widget_name = isset($widget_params['widget_name']) ? sanitize_title($widget_params['widget_name']) : ''; 
    $sidebar_params[$index] = array_merge( 
      $widget_params, 
      array(
        //'before_widget' => '<div class="widget">',
        //'before_widget' => "<div class=\"widget widget-$widget_name card card-$widget_name\">",
        'after_widget'  => '</div>',
        'before_title'  => '<div class="card-header">',
        'after_title'   => '</div>',
        //'class'         => '.testo'
      )
    );
  }
  global $wp_registered_widgets;
  $widget_id = $sidebar_params[0]['widget_id'];
  $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
  $wp_registered_widgets[ $widget_id ]['callback'] = 'wp_bootstrap_widget_callback_function';
  return $sidebar_params;
}


function wp_bootstrap_widget_callback_function() {
 
  global $wp_registered_widgets;
  $original_callback_params = func_get_args();
  $widget_id = $original_callback_params[0]['widget_id'];
 
  $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
  $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
 
  $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
 
  if ( is_callable( $original_callback ) ) {
    ob_start();
    call_user_func_array( $original_callback, $original_callback_params );
    $widget_output = ob_get_clean();
    echo apply_filters( 'widget_output', $widget_output, $widget_id_base, $widget_id );
  }
}

function wp_bootstrap_widget_output( $widget_output, $widget_id_base, $widget_id) {
  $component_class = 'card';
  $widget_id_base_hyphens = preg_replace("~_~Ui", "-", $widget_id_base);
  
  if ($widget_output) {
    $html = new DOMDocument();
    @$html->loadHTML('<?xml encoding="utf-8" ?>' . $widget_output );
    $html_xpath = new DOMXpath($html);
    
    $body_elem = $html->getElementsByTagName( 'body' )->item(0);
    $widget_root_node = $body_elem->firstChild;
    $panel_body = null;
    
    $content_fragment = $html->createDocumentFragment();
    $elems = array();
    
    if ($widget_root_node) {
      
      if ($widget_id_base === "search") {
        $class = $widget_root_node->getAttribute("class");
        $class = preg_replace("/card/Ui", "", $class);
        $class.= strlen($class) > 0 ? " " . $class : $class; 
        $widget_root_node->setAttribute('class', $class);
        $style = $widget_root_node->getAttribute('style');
        $widget_root_node->setAttribute('style', $style . " border: none;");
      }
      
      $content_parent = $html_xpath->query("//*[contains(@class, '$component_class') and not(contains(@class, '$component_class-'))]")->item(0);
      
      if (!$content_parent) {
        // If no element with component class is found yet, we'll add one and wrap the actual content in it
        $content_parent = $html->createElement('div');
        $content_parent->setAttribute("class", $component_class);
        $content_children = array();
        foreach ($widget_root_node->childNodes as $child) {
          array_push($content_children, $child);
        }
        foreach ($content_children as $child) {
          $content_parent->appendChild($child);
        }
        $widget_root_node->appendChild($content_parent);
      }
      
      // Setup component extra classes
      $component_extra_classes = array($widget_id_base_hyphens, "widget");
      // Try to find additional classes that specify the widget more further
      $widget_class_elem = $html_xpath->query("//*[contains(@class, '$widget_id_base') or contains(@class, '$widget_id_base_hyphens')]")->item(0);
      $widget_class_elem_classes = explode(" ", $widget_class_elem->getAttribute('class'));
      $widget_leading_class = "";
      foreach ($widget_class_elem_classes as $widget_class_elem_class) {
        $matched = preg_match("~(" . preg_quote($widget_id_base, "~") . "|" . preg_quote($widget_id_base_hyphens) . ")$~", $widget_class_elem_class, $match);
        if ($matched) {
          $widget_leading_class = $widget_class_elem_class;
          break;
        }
      }
      foreach ($widget_class_elem_classes as $widget_class_elem_class) {
        $matched = preg_match("~^" .preg_quote($widget_leading_class, "~") . "[-_]~", $widget_class_elem_class, $match);
        if ($match) {
          // Additional class found:
          $widget_base_class = (strpos($widget_class_elem_class, $widget_id_base_hyphens) !== false) ? $widget_id_base_hyphens : $widget_id_base;
          $widget_additional_class = preg_replace("~.*(" . preg_quote($widget_base_class, "~") . ")~", "$1", $widget_class_elem_class);
          array_push($component_extra_classes, $widget_additional_class);
        }
      }
      $component_extra_classes = array_unique($component_extra_classes);
      // Clean up extra classes
      foreach ($component_extra_classes as $index => $component_extra_class) {
        // FIXME: Remove null-prefix, i.e. Instagram widget
        $component_extra_class = preg_replace("~^null-~", "", $component_extra_class);
        // Remove -widget suffix
        $component_extra_class = preg_replace("~-widget$~Ui", "", $component_extra_class);
        // Remove -widget prefix
        $component_extra_class = preg_replace("~^widget-~Ui", "", $component_extra_class);
        $component_extra_classes[$index] = $component_extra_class;
      }
      
      // Add component widget class
      $content_parent_classes = explode(" ", $content_parent->getAttribute('class'));
      foreach ($component_extra_classes as $component_extra_class) {
        array_push($content_parent_classes, $component_class . "-" . $component_extra_class);
      }
      $content_parent->setAttribute('class', implode(" ", $content_parent_classes));
      
      // Clean up header
      $content_header = $html_xpath->query("//*[contains(@class, '$component_class-header')]")->item(0);
      $content_parent->insertBefore($content_header, $content_parent->firstChild);
      
      // Collect content elems
      foreach ($content_parent->childNodes as $child) {
        if ($child !== $content_header) {
          array_push($elems, $child);
        }
      }
      
      $panel_body = null;
      foreach ($elems as $widget_content_node) {
        
        if ($widget_content_node->nodeType === 1 && strpos($widget_content_node->getAttribute('class'), 'card-header') !== false) {
          $content_fragment->appendChild($widget_content_node);
          $panel_body = null;<commit_msg>Fixed null-pointers<commit_after><?php

function wp_bootstrap_dynamic_sidebar_params( $sidebar_params ) {
  if ( is_admin() ) {
    return $sidebar_params;
  }
  foreach($sidebar_params as $index => $widget_params) {
    $widget_name = isset($widget_params['widget_name']) ? sanitize_title($widget_params['widget_name']) : ''; 
    $sidebar_params[$index] = array_merge( 
      $widget_params, 
      array(
        //'before_widget' => '<div class="widget">',
        //'before_widget' => "<div class=\"widget widget-$widget_name card card-$widget_name\">",
        'after_widget'  => '</div>',
        'before_title'  => '<div class="card-header">',
        'after_title'   => '</div>',
        //'class'         => '.testo'
      )
    );
  }
  global $wp_registered_widgets;
  $widget_id = $sidebar_params[0]['widget_id'];
  $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
  $wp_registered_widgets[ $widget_id ]['callback'] = 'wp_bootstrap_widget_callback_function';
  return $sidebar_params;
}


function wp_bootstrap_widget_callback_function() {
 
  global $wp_registered_widgets;
  $original_callback_params = func_get_args();
  $widget_id = $original_callback_params[0]['widget_id'];
 
  $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
  $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
 
  $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
 
  if ( is_callable( $original_callback ) ) {
    ob_start();
    call_user_func_array( $original_callback, $original_callback_params );
    $widget_output = ob_get_clean();
    echo apply_filters( 'widget_output', $widget_output, $widget_id_base, $widget_id );
  }
}

function wp_bootstrap_widget_output( $widget_output, $widget_id_base, $widget_id) {
  $component_class = 'card';
  $widget_id_base_hyphens = preg_replace("~_~Ui", "-", $widget_id_base);
  
  if ($widget_output) {
    $html = new DOMDocument();
    @$html->loadHTML('<?xml encoding="utf-8" ?>' . $widget_output );
    $html_xpath = new DOMXpath($html);
    
    $body_elem = $html->getElementsByTagName( 'body' )->item(0);
    $widget_root_node = $body_elem->firstChild;
    $panel_body = null;
    
    $content_fragment = $html->createDocumentFragment();
    $elems = array();
    
    if ($widget_root_node) {
      
      if ($widget_id_base === "search") {
        $class = $widget_root_node->getAttribute("class");
        $class = preg_replace("/card/Ui", "", $class);
        $class.= strlen($class) > 0 ? " " . $class : $class; 
        $widget_root_node->setAttribute('class', $class);
        $style = $widget_root_node->getAttribute('style');
        $widget_root_node->setAttribute('style', $style . " border: none;");
      }
      
      $content_parent = $html_xpath->query("//*[contains(@class, '$component_class') and not(contains(@class, '$component_class-'))]")->item(0);
      
      if (!$content_parent) {
        // If no element with component class is found yet, we'll add one and wrap the actual content in it
        $content_parent = $html->createElement('div');
        $content_parent->setAttribute("class", $component_class);
        $content_children = array();
        foreach ($widget_root_node->childNodes as $child) {
          array_push($content_children, $child);
        }
        foreach ($content_children as $child) {
          $content_parent->appendChild($child);
        }
        $widget_root_node->appendChild($content_parent);
      }
      
      // Setup component extra classes
      $component_extra_classes = array($widget_id_base_hyphens, "widget");
      // Try to find additional classes that specify the widget more further
      $widget_class_elem = $html_xpath->query("//*[contains(@class, '$widget_id_base') or contains(@class, '$widget_id_base_hyphens')]")->item(0);
      if ($widget_class_elem) {
        $widget_class_elem_class_attr = $widget_class_elem->getAttribute('class');
        if ($widget_class_elem_class_attr !== null) {
          $widget_class_elem_classes = explode(" ", $widget_class_elem_class_attr);
          $widget_leading_class = "";
          foreach ($widget_class_elem_classes as $widget_class_elem_class) {
            $matched = preg_match("~(" . preg_quote($widget_id_base, "~") . "|" . preg_quote($widget_id_base_hyphens) . ")$~", $widget_class_elem_class, $match);
            if ($matched) {
              $widget_leading_class = $widget_class_elem_class;
              break;
            }
          }
          foreach ($widget_class_elem_classes as $widget_class_elem_class) {
            $matched = preg_match("~^" .preg_quote($widget_leading_class, "~") . "[-_]~", $widget_class_elem_class, $match);
            if ($match) {
              // Additional class found:
              $widget_base_class = (strpos($widget_class_elem_class, $widget_id_base_hyphens) !== false) ? $widget_id_base_hyphens : $widget_id_base;
              $widget_additional_class = preg_replace("~.*(" . preg_quote($widget_base_class, "~") . ")~", "$1", $widget_class_elem_class);
              array_push($component_extra_classes, $widget_additional_class);
            }
          }
          }
      }
      $component_extra_classes = array_unique($component_extra_classes);
      // Clean up extra classes
      foreach ($component_extra_classes as $index => $component_extra_class) {
        // FIXME: Remove null-prefix, i.e. Instagram widget
        $component_extra_class = preg_replace("~^null-~", "", $component_extra_class);
        // Remove -widget suffix
        $component_extra_class = preg_replace("~-widget$~Ui", "", $component_extra_class);
        // Remove -widget prefix
        $component_extra_class = preg_replace("~^widget-~Ui", "", $component_extra_class);
        $component_extra_classes[$index] = $component_extra_class;
      }
      
      // Add component widget class
      $content_parent_classes = explode(" ", $content_parent->getAttribute('class'));
      foreach ($component_extra_classes as $component_extra_class) {
        array_push($content_parent_classes, $component_class . "-" . $component_extra_class);
      }
      $content_parent->setAttribute('class', implode(" ", $content_parent_classes));
      
      // Clean up header position in markup
      $content_header = $html_xpath->query("//*[contains(@class, '$component_class-header')]")->item(0);
      if ($content_header && $content_parent->firstChild !== $content_header) {
        if ($content_parent->firstChild !== null) {
          $content_parent->insertBefore($content_header, $content_parent->firstChild);
        } else {
          $content_parent->appendChild($content_header);
        }
      }
      
      // Collect content elems
      foreach ($content_parent->childNodes as $child) {
        if ($child !== $content_header) {
          array_push($elems, $child);
        }
      }
      
      $panel_body = null;
      foreach ($elems as $widget_content_node) {
        
        if ($widget_content_node->nodeType === 1 && strpos($widget_content_node->getAttribute('class'), 'card-header') !== false) {
          $content_fragment->appendChild($widget_content_node);
          $panel_body = null;
{'fraction_non_alphanumeric': 0.14888001141389642, 'fraction_numerical': 0.0015694107575973748, 'mean_word_length': 3.519342359767892, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599228', 'n_tokens_mistral': 4311, 'n_tokens_neox': 4260, 'n_words': 709}