Skip to content

Instantly share code, notes, and snippets.

@phillip-boombox
Last active September 18, 2017 22:56
Show Gist options
  • Save phillip-boombox/6f23fc6b491de7a4cc02826a50ef4ed7 to your computer and use it in GitHub Desktop.
Save phillip-boombox/6f23fc6b491de7a4cc02826a50ef4ed7 to your computer and use it in GitHub Desktop.
PHP DOMDocument manipulation sample
// Create new DOMDocument
$cmb2_mb = new DOMDocument();
// Load cmb2 metabox html string into document
$cmb2_mb->loadHTML( $form, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
// Add bootstrap classes to all labels
foreach ( $cmb2_mb->getElementsByTagName( 'label' ) as $node_label ) {
$node_label->setAttribute( 'class', 'control-label' );
}
// Add bootstrap classes to apprproiate inputs
$types = array(
'text',
'url',
'email',
);
foreach ( $cmb2_mb->getElementsByTagName( 'input' ) as $node_input ) {
if ( in_array( $node_input->getAttribute( 'type' ), $types ) ) {
$class_array = explode( ' ', $node_input->getAttribute( 'class' ) );
$class_array[] = 'form-control';
$node_input->setAttribute( 'class', implode( ' ', $class_array ) );
}
}
// Create DomXPath object for complex queries
$xpath = new DomXPath( $cmb2_mb );
$nodes_row_types = $xpath->query( "//div[@data-fieldtype='text_small' or @data-fieldtype='text_url']" );
for ( $i1 = 0; $i1 < $nodes_row_types->length; $i1++ ) {
// Shortcut reference
$this_row_node = $nodes_row_types->item( $i1 );
// Wrap cmb-th with bootstrap column
$nodes_th = $xpath->query( ".//*[contains(concat(' ', normalize-space(@class), ' '), ' cmb-th ')]", $this_row_node );
for ( $i2 = 0; $i2 < $nodes_th->length; $i2++ ) {
// Shortcut reference
$this_node = $nodes_th->item( $i2 );
$node_column = $cmb2_mb->createElement( 'div' );
$node_column->setAttribute( 'class', 'col-sm-3' );
$this_node->parentNode->appendChild( $node_column );
$node_column->appendChild( $this_node->cloneNode( true ) );
$this_node->parentNode->removeChild( $this_node );
}
// Wrap cmb-td with bootstrap column
$nodes_td = $xpath->query( ".//*[contains(concat(' ', normalize-space(@class), ' '), ' cmb-td ')]", $this_row_node );
for ( $i2 = 0; $i2 < $nodes_td->length; $i2++ ) {
// Shortcut reference
$this_node = $nodes_td->item( $i2 );
$node_column = $cmb2_mb->createElement( 'div' );
$node_column->setAttribute( 'class', 'col-sm-5' );
$this_node->parentNode->appendChild( $node_column );
$node_column->appendChild( $this_node->cloneNode( true ) );
$this_node->parentNode->removeChild( $this_node );
}
// Pull description out of one column and into a new column
$nodes_description = $xpath->query( ".//*[contains(concat(' ', normalize-space(@class), ' '), ' cmb2-metabox-description ')]", $this_row_node );
for ( $i2 = 0; $i2 < $nodes_description->length; $i2++ ) {
// Shortcut reference
$this_node = $nodes_description->item( $i2 );
$node_column = $cmb2_mb->createElement( 'div' );
$node_column->setAttribute( 'class', 'col-sm-4' );
$this_node->parentNode->parentNode->parentNode->appendChild( $node_column );
$node_column->appendChild( $this_node->cloneNode( true ) );
$this_node->parentNode->removeChild( $this_node );
}
}
// Select groups
$nodes_group = $xpath->query( "//div[@data-fieldtype='group']" );
for ( $i = 0; $i < $nodes_group->length; $i++ ) {
// Shortcut reference
$this_group = $nodes_group->item( $i );
// Make the group description a legend
$nodes_group_description = $xpath->query( ".//*[contains(concat(' ', normalize-space(@class), ' '), ' cmb-group-description ')]/descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' cmb2-metabox-description ')]", $this_group );
for ( $i = 0; $i < $nodes_group_description->length; $i++ ) {
// Shortcut reference
$this_description = $nodes_group_description->item( $i );
$node_legend = $cmb2_mb->createElement( 'legend', $this_description->nodeValue );
if ( $this_description->hasAttributes() ) {
foreach ( $this_description->attributes as $attr ) {
$node_legend->setAttribute( $attr->nodeName, $attr->nodeValue );
}
}
$this_description->parentNode->replaceChild( $node_legend, $this_description );
}
$nodes_group_rows = $xpath->query( ".//*[contains(concat(' ', normalize-space(@class), ' '), ' cmb-row ')]", $this_group );
for ( $i = 0; $i < $nodes_group_rows->length; $i++ ) {
// Shortcut reference
$this_group_row = $nodes_group_rows->item( $i );
$node_column = $cmb2_mb->createElement( 'div' );
$node_column->setAttribute( 'class', 'col-sm-12' );
$this_group_row->appendChild( $node_column );
while ( $this_group_row->childNodes->length > 1 ) {
$child = $this_group_row->childNodes->item( 0 );
$this_group_row->removeChild( $child );
$node_column->appendChild( $child );
}
$this_group_row->parentNode->replaceChild( $node_column, $this_group_row );
}
// Put row in a column
$node_column = $cmb2_mb->createElement( 'div' );
$node_column->setAttribute( 'class', 'col-sm-12' );
$this_group->appendChild( $node_column );
while ( $this_group->childNodes->length > 1 ) {
$child = $this_group->childNodes->item( 0 );
$this_group->removeChild( $child );
$node_column->appendChild( $child );
}
// Wrap group with fieldset
$node_fieldset = $cmb2_mb->createElement( 'fieldset' );
$this_group->parentNode->appendChild( $node_fieldset );
$node_fieldset->appendChild( $this_group->cloneNode( true ) );
$this_group->parentNode->removeChild( $this_group );
}
// Remove unnecessary "remove row" button
$remove_group_row = $xpath->query( "//button[contains(concat(' ', normalize-space(@class), ' '), ' cmb-remove-group-row ')][@title]" );
for ( $i = 0; $i < $remove_group_row->length; $i++ ) {
// Shortcut reference
$this_row = $remove_group_row->item( $i );
$this_row->parentNode->removeChild( $this_row );
}
// Remove unnecessary drag row handle
$drag_handle = $xpath->query( "//button[contains(concat(' ', normalize-space(@class), ' '), ' cmbhandle ')]" );
for ( $i = 0; $i < $drag_handle->length; $i++ ) {
// Shortcut reference
$this_handle = $drag_handle->item( $i );
$this_handle->parentNode->removeChild( $this_handle );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment