Skip to content

Commit

Permalink
Merge pull request #1382 from visual-framework/taxonomy
Browse files Browse the repository at this point in the history
taxonomy update
  • Loading branch information
kasprzyk-sz authored Nov 18, 2024
2 parents 1660ffc + 366b924 commit 2902675
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
padding: 2px 5px;
background-color: #E58F9E;
}
.statusHidden {
width: fit-content;
padding: 2px 5px;
background-color: #D0D0CE;
}



Expand Down
126 changes: 125 additions & 1 deletion wp-content/plugins/embl-taxonomy/embl-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,138 @@ function embl_taxonomy_get_uuid($term_id) {
return $uuid;
}


/**
* Return the VF stylesheet URL
*/
function embl_taxonomy_get_url() {
return embl_taxonomy()->settings->get_field('embl_taxonomy');
}



$active_theme = wp_get_theme();
$is_news_theme = ($active_theme->get('Name') === 'VF-WP News');

if ($active_theme == $is_news_theme) {
/**
* Hides EMBL Taxonomy default meta box
*/

add_filter( 'rest_prepare_taxonomy', function( $response, $taxonomy, $request ){
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
// Context is edit in the editor
if( $context === 'edit' && $taxonomy->meta_box_cb === false ){
$data_response = $response->get_data();
$data_response['visibility']['show_ui'] = false;
$response->set_data( $data_response );
}
return $response;
}, 10, 3 );



/**
* Adds bulk actions
*/

// Step 1: Add "Mark as Deprecated", "Mark as Active", and "Mark as Hidden" to the Bulk Actions dropdown
add_filter('bulk_actions-edit-embl_taxonomy', function($bulk_actions) {
$bulk_actions['mark_as_deprecated'] = __('Mark as Deprecated', 'text-domain');
$bulk_actions['mark_as_active'] = __('Mark as Active', 'text-domain');
$bulk_actions['mark_as_hidden'] = __('Mark as Hidden', 'text-domain');
return $bulk_actions;
});

// Step 2: Handle the bulk edit actions
add_action('handle_bulk_actions-edit-embl_taxonomy', function($redirect_to, $doaction, $term_ids) {
if (in_array($doaction, ['mark_as_deprecated', 'mark_as_active', 'mark_as_hidden'])) {
// Set values based on the action
$deprecated_value = $doaction === 'mark_as_deprecated' ? 1 : 0;
$hidden_value = $doaction === 'mark_as_hidden' ? 1 : 0;

foreach ($term_ids as $term_id) {
if ($doaction === 'mark_as_deprecated') {
update_field('field_embl_taxonomy_deprecated', $deprecated_value, 'embl_taxonomy_' . $term_id);
} elseif ($doaction === 'mark_as_hidden') {
update_field('field_embl_taxonomy_hidden', $hidden_value, 'embl_taxonomy_' . $term_id);
} elseif ($doaction === 'mark_as_active') {
// Mark both as active (0)
update_field('field_embl_taxonomy_deprecated', 0, 'embl_taxonomy_' . $term_id);
update_field('field_embl_taxonomy_hidden', 0, 'embl_taxonomy_' . $term_id);
}
}

// Redirect back with a success message
$redirect_to = add_query_arg(
$doaction === 'mark_as_deprecated' ? 'bulk_mark_as_deprecated' : ($doaction === 'mark_as_hidden' ? 'bulk_mark_as_hidden' : 'bulk_mark_as_active'),
count($term_ids),
$redirect_to
);
}
return $redirect_to;
}, 10, 3);

// Step 3: Display a confirmation message after bulk edit
add_action('admin_notices', function() {
if (!empty($_GET['bulk_mark_as_deprecated'])) {
$edited_count = intval($_GET['bulk_mark_as_deprecated']);
printf('<div id="message" class="updated notice is-dismissible"><p>' .
__('Marked %s terms as deprecated successfully.', 'text-domain') . '</p></div>', $edited_count);
}
if (!empty($_GET['bulk_mark_as_active'])) {
$edited_count = intval($_GET['bulk_mark_as_active']);
printf('<div id="message" class="updated notice is-dismissible"><p>' .
__('Marked %s terms as active successfully.', 'text-domain') . '</p></div>', $edited_count);
}
if (!empty($_GET['bulk_mark_as_hidden'])) {
$edited_count = intval($_GET['bulk_mark_as_hidden']);
printf('<div id="message" class="updated notice is-dismissible"><p>' .
__('Marked %s terms as hidden successfully.', 'text-domain') . '</p></div>', $edited_count);
}
});


/**
* Filters the list of terms in the taxonomy picker
*/
add_filter('acf/fields/taxonomy/query/name=embl_2taxonomy', function( $args, $field, $post_id ) {

// Set up the arguments for retrieving terms
$args_terms = array(
'taxonomy' => 'embl_taxonomy',
'hide_empty' => 0, // Include terms even if they are not assigned to any posts
);

// Get all terms in the 'embl_taxonomy' taxonomy
$terms = get_terms( $args_terms );

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$hidden_terms = [];

// Loop through each term to check the custom field value
foreach ( $terms as $term ) {
// Get the value of the 'field_embl_taxonomy_hidden' ACF field for each term
$custom_field = get_field('field_embl_taxonomy_hidden', $term);

// If the field value is 1, add the term ID to the hidden terms array
if ( $custom_field == '1' ) {
$hidden_terms[] = $term->term_id;
}
}

// If there are hidden terms, exclude them from the ACF picker
if ( ! empty( $hidden_terms ) ) {
$args['exclude'] = $hidden_terms;
}
}

return $args;

}, 10, 3);

}


endif;

?>
75 changes: 46 additions & 29 deletions wp-content/plugins/embl-taxonomy/includes/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ public function embl_taxonomy_column_content($content, $column_name, $term_id) {
// $content = '<code>' . get_term_meta($term_id, EMBL_Taxonomy::META_NAME, true) . '</code>';
} elseif ($column_name === 'embl_taxonomy_meta_deprecated') {
$deprecatedValue = get_term_meta($term_id, EMBL_Taxonomy::META_DEPRECATED, true);
$statusClass = $deprecatedValue == '1' ? 'statusDeprecated' : 'statusActive';
$statusText = $deprecatedValue == '1' ? 'Deprecated' : 'Active';
$hiddenValue = get_field('field_embl_taxonomy_hidden', "term_{$term_id}");


if ($hiddenValue == '1') {
$statusClass = 'statusHidden';
$statusText = 'Hidden';
} else {
$statusClass = $deprecatedValue == '1' ? 'statusDeprecated' : 'statusActive';
$statusText = $deprecatedValue == '1' ? 'Deprecated' : 'Active';
}

$content = '<p class="' . $statusClass . '">' . $statusText . '</p>';
}
Expand Down Expand Up @@ -140,6 +148,7 @@ public function register_taxonomy() {
'hierarchical' => false,
'public' => $is_news_theme, // true only for the news theme
'show_ui' => true,
'meta_box_cb' => false,
'show_in_menu' => true,
'show_admin_column' => true,
'show_in_rest' => true,
Expand Down Expand Up @@ -669,32 +678,40 @@ static private function generate_terms(& $api_terms, & $terms) {

// Check if the term has parents and retrieve the correct one based on the primary term
if (is_array($term['parents']) && !empty($term['parents'])) {
// Initialize a variable to track if a parent was found
$found_parent = false;

// Loop through parents to find the one that corresponds to the primary term
foreach ($term['parents'] as $parent_id) {
if (array_key_exists($parent_id, $api_terms)) {
$parent_term = $api_terms[$parent_id];

// Check if the parent term is of the same type as the primary
if ($parent_term['primary'] === $term['primary']) {
// Append the parent's name to the prefix name
$prefix_name .= $parent_term['name'] . EMBL_Taxonomy::TAXONOMY_SEPARATOR;
// Insert the parent term's IDs in the second position of prefix_ids
array_splice($prefix_ids, 1, 0, (array)$parent_term[EMBL_Taxonomy::META_IDS]);
$found_parent = true;
break; // Stop after finding the first matching parent
}
} else {
error_log("Parent ID '$parent_id' not found in api_terms.");
}
}

if (!$found_parent) {
error_log("No valid parent found for term '{$term['name']}' with primary '{$term['primary']}'.");
}
}
// Initialize a variable to track if a parent was found
$found_parent = false;

// Loop through parents to find the one that corresponds to the primary term
foreach ($term['parents'] as $parent_id) {
if (array_key_exists($parent_id, $api_terms)) {
$parent_term = $api_terms[$parent_id];

// Check if the parent term is of the same type as the primary
if ($parent_term['primary'] === $term['primary']) {
// Check if the prefix is different from the parent term name and if the parent name is different from the term name
if ($prefix_name !== $parent_term['name'] . EMBL_Taxonomy::TAXONOMY_SEPARATOR
&& $parent_term['name'] !== $term['name']) {
// Append the parent's name to the prefix name
$prefix_name .= $parent_term['name'] . EMBL_Taxonomy::TAXONOMY_SEPARATOR;
// Insert the parent term's IDs in the second position of prefix_ids
array_splice($prefix_ids, 1, 0, (array)$parent_term[EMBL_Taxonomy::META_IDS]);
}
$found_parent = true;
break; // Stop after finding the first matching parent
}
} else {
error_log("Parent ID '$parent_id' not found in api_terms.");
}
}

if (!$found_parent) {
error_log("No valid parent found for term '{$term['name']}' with primary '{$term['primary']}'.");
}
}





// Append the current term's name
$prefix_name .= $term['name'];
Expand Down Expand Up @@ -885,7 +902,7 @@ public function filter_term_name($value, $term_id, $context) {
if ($context === 'display') {
$deprecated = get_term_meta($term_id, EMBL_Taxonomy::META_DEPRECATED, true);
if (intval($deprecated) === 1) {
return "⚠️ {$value} (deprecated but retained as local content still tagged by term)";
return "⚠️ (Deprecated) {$value} ";
}
}
return $value;
Expand Down
51 changes: 49 additions & 2 deletions wp-content/plugins/embl-taxonomy/includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function acf_init() {
'label' => 'Deprecated',
'name' => 'embl_taxonomy_deprecated',
'type' => 'true_false',
'instructions' => '',
'instructions' => 'Select "Yes" to manually deprecate the term.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
Expand Down Expand Up @@ -181,7 +181,54 @@ function acf_init() {
'active' => true,
'description' => '',
));


$active_theme = wp_get_theme();
$is_news_theme = ($active_theme->get('Name') === 'VF-WP News');

if ($active_theme == $is_news_theme) {

acf_add_local_field_group(array(
'key' => 'group_embl_taxonomy_hidden',
'fields' => array(
array(
'key' => 'field_embl_taxonomy_hidden',
'label' => 'Hidden',
'name' => 'embl_taxonomy_hidden',
'type' => 'true_false',
'instructions' => 'Select "Yes" to not show the term in the taxonomy picker.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => 'Yes',
'ui_off_text' => 'No',
),
),
'location' => array(
array(
array(
'param' => 'taxonomy',
'operator' => '==',
'value' => 'embl_taxonomy', // Specify the taxonomy
),
),
),
'menu_order' => 20,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
}
}

/**
Expand Down

0 comments on commit 2902675

Please sign in to comment.