Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Hierarchical taxonomies as checkboxes in attachment modal #3767

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
75 changes: 75 additions & 0 deletions classes/PodsMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,28 @@ public function core () {
if ( function_exists( 'pll_current_language' ) )
add_action( 'init', array( $this, 'cache_pods' ), 101 );

// Priority 20 because PodsInit has priority 15
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ), 20 );
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 20 );

do_action( 'pods_meta_init' );

return $this;
}

/**
* Add styles and scripts globally or conditionally
* @since 2.6.8
*/
public function assets() {

// Add Pods form styles to the Media Library
if ( is_admin() && get_current_screen()->base == 'upload' ) {
wp_enqueue_style( 'pods-form' );
//wp_enqueue_script( 'pods' );
}
}

public static function enqueue () {
foreach ( self::$queue as $type => $objects ) {
foreach ( $objects as $pod_name => $pod ) {
Expand Down Expand Up @@ -1301,6 +1318,46 @@ public function meta_media ( $form_fields, $post ) {
}
}

/**
* Add hierarchical taxonomies as checkboxes
* Based on Enhanced Media Library
*/
if ( function_exists( 'wp_terms_checklist' ) ) {
foreach ( $form_fields as $field => $args ) {
if ( ! empty( $args[ 'hierarchical' ] ) && ( ! isset( $form_fields[ $field ][ 'input' ] ) || $form_fields[ $field ][ 'input' ] != 'html' ) ) {

ob_start();

wp_terms_checklist( $post->ID, array( 'taxonomy' => $field, 'checked_ontop' => false ) );

$content = ob_get_contents();

if ( $content ) {
$html = '<ul class="term-list pods-term-list">' . $content . '</ul>';
} else {
$html = '<ul class="term-list pods-term-list"><li>No ' . $args[ 'label' ] . ' found.</li></ul>';
}

ob_end_clean();

/**
* Ensure data is send and saved.
* Default array structures not working in update
* @todo file bug at WP trac?
*/
$counter = 0;
while( false !== strpos( $html, '['.$field.'][]' ) ) {
$html = preg_replace( '/\['.$field.'\]\[\]/', '['.$field.']['.($counter++).']', $html, 1 );
}

unset( $form_fields[ $field ][ 'value' ] );

$form_fields[ $field ][ 'input' ] = 'html';
$form_fields[ $field ][ 'html' ] = $html;
}
}
}

$form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );

return $form_fields;
Expand Down Expand Up @@ -1414,6 +1471,24 @@ public function save_media_ajax () {

if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
$_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;

/**
* Update taxonomies when changed to checkboxes in the attachment modal
* @since 2.6.8
*/
if ( ! empty( $_POST['tax_input'] ) ) {
foreach ( $_POST['tax_input'] as $tax => $terms ) {
/**
* Validate for an array
* Otherwise we didn't change this functionality since this is a string by default in WP core
*/
if ( is_taxonomy_hierarchical( $tax ) && is_array( $terms ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also verify that this is a Pods field?

$tax = sanitize_title( $tax );
$terms = array_map( 'intval', $terms );
wp_set_post_terms( $id, $terms, $tax );
}
}
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions ui/css/pods-form.css
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,23 @@ input.pods-form-ui-field-type-color {
clear: both;
display: block;
}

/**
* Modal fields
*/
.media-modal .compat-item .pods-term-list {
margin: 0;
padding: 3px;
background: #fff;
border: 1px solid #dfdfdf;
}
.media-modal .compat-item .pods-term-list input {
vertical-align: text-bottom;
margin: 3px 3px 0;
}
.media-modal .compat-item .pods-term-list > li:last-child {
margin-bottom: 3px;
}
.media-modal .compat-item .pods-term-list ul.children {
margin: 6px 0 0 18px;
}