Skip to content

Commit

Permalink
Further improve the Taxonomy sync option and include new option to hi…
Browse files Browse the repository at this point in the history
…de the taxonomy UI for that sync taxonomy
  • Loading branch information
sc0ttkclark committed Aug 28, 2024
1 parent 8b2b87c commit 32af949
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 38 deletions.
26 changes: 26 additions & 0 deletions classes/PodsMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,32 @@ public function meta_post_add( $post_type, $post = null ) {
if ( $pods_field_found ) {
// Only add the classes to forms that actually have pods fields
add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) );

$pod = pods( $post_type, null, true );

if ( $pod ) {
// Check if we need to disable any specific taxonomies.
$taxonomy_sync_fields = $pod->pod_data->get_fields( [
'type' => 'pick',
'args' => [
'pick_object' => 'taxonomy',
'pick_sync_taxonomy' => 1,
'pick_sync_taxonomy_hide_taxonomy_ui' => 1,
],
] );

foreach ( $taxonomy_sync_fields as $taxonomy_sync_field ) {
$taxonomy_name = $taxonomy_sync_field->get_related_object_name();

if ( $taxonomy_name ) {
if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
remove_meta_box( "{$taxonomy_name}div", $post_type, 'side' );
} else {
remove_meta_box( "tagsdiv-{$taxonomy_name}", $post_type, 'side' );
}
}
}
}
}
}

Expand Down
27 changes: 22 additions & 5 deletions classes/fields/pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,21 @@ public function options() {
],
'type' => 'boolean',
'default' => 0,
]
],
static::$type . '_sync_taxonomy_hide_taxonomy_ui' => [
'label' => __( 'Hide the associated taxonomy UI from the Editor', 'pods' ),
'help' => __( 'This will hide the taxonomy meta box from the Classic Editor and disable the taxonomy panel in the Block Editor.', 'pods' ),
'depends-on' => [
static::$type . '_sync_taxonomy' => true,
],
'wildcard-on' => [
static::$type . '_object' => [
'^taxonomy-.*$',
],
],
'type' => 'boolean',
'default' => 0,
],
];

$post_type_pick_objects = array();
Expand Down Expand Up @@ -1926,15 +1940,15 @@ public function save( $value, $id = null, $name = null, $options = null, $fields
}
} elseif ( $options instanceof Field || $options instanceof Value_Field ) {
$related_field = $options->get_bidirectional_field();

if ( $related_field ) {
$related_pod = $related_field->get_parent_object();
$related_pick_limit = $related_field->get_limit();

if ( null === $current_ids ) {
$current_ids = self::$api->lookup_related_items( $options['id'], $pod['id'], $id, $options, $pod );
}

// Get ids to remove.
if ( null === $remove_ids ) {
$remove_ids = array_diff( $current_ids, $value_ids );
Expand All @@ -1943,7 +1957,10 @@ public function save( $value, $id = null, $name = null, $options = null, $fields
}

if (
( empty( $related_field ) || empty( $related_pod ) )
(
empty( $related_field )
|| empty( $related_pod )
)
&& empty( $options[ static::$type . '_sync_taxonomy' ] )
) {
return;
Expand Down
87 changes: 54 additions & 33 deletions src/Pods/Blocks/API.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ui/js/blocks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import createBlockCollection from './block-collections';
import createBlock from './blocks';
import createCommand from './commands';
import disablePanel from './panels';

import './editor.scss';

Expand All @@ -15,3 +16,6 @@ window.podsBlocksConfig.blocks.forEach( createBlock );

// Register commands from the config.
window.podsBlocksConfig.commands.forEach( createCommand );

// Disable certain panels in the block editor from the config.
window.podsBlocksConfig.panelsToDisable.forEach( disablePanel );
3 changes: 3 additions & 0 deletions ui/js/blocks/src/panels/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# disablePanel

Dynamically disables a panel in the block editor.
10 changes: 10 additions & 0 deletions ui/js/blocks/src/panels/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Disables a panel in the block editor.
*
* @param {string} panel
*/
const disablePanel = ( panel ) => {
window.wp.data.dispatch('core/edit-post').removeEditorPanel(panel);
};

export default disablePanel;

0 comments on commit 32af949

Please sign in to comment.