Automattic / Automattic/Taxonomy-Images

Add helper functions

Open
#11 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
21
Forks
6
PR merge metrics
No merged PRs in 30d

Description

It would be very nice for plugins and scripts to have at least two helper functions for programmatically associating/removing the association of term featured images.

Current workaround, based off the existing ajax handler functions:
```php

// @see https://github.com/Automattic/Taxonomy-Images/blob/0c4bb009820ff6db871f9e5707a7811c6f70878a/taxonomy-images.php#L498
function taxonomy_image_plugin_create_association_function( $tt_id, $attachment_id ) {

if ( empty( $tt_id ) ) {
return new WP_Error( 'taxonomy_images_tt_id_empty', __( 'tt_id is empty', 'taxonomy-images' ) );
}

if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) {
return new WP_Error( 'taxonomy_images_tt_id_missing_caps', __( 'You do not have the correct capability to manage this term', 'taxonomy-images' ) );
}

if ( ! isset( $attachment_id ) ) {
return new WP_Error( 'taxonomy_images_image_id_empty', __( 'Image id is empty', 'taxonomy-images' ) );
}

$image_id = absint( $attachment_id );
if ( empty( $image_id ) ) {
return new WP_Error( 'taxonomy_images_image_id_not_absint', __( 'Image id is not a positive integer', 'taxonomy-images' ) );
}

$assoc = taxonomy_image_plugin_get_associations();
$assoc[$tt_id] = $image_id;
if ( update_option( 'taxonomy_image_plugin', taxonomy_image_plugin_sanitize_associations( $assoc ) ) ) {
return true; // success
}
else {
return new WP_Error( 'taxonomy_images_fail_assoc_create', __( 'Association could not be created', 'taxonomy-images' ) );
}

}

// @see https://github.com/Automattic/Taxonomy-Images/blob/0c4bb009820ff6db871f9e5707a7811c6f70878a/taxonomy-images.php#L580
function taxonomy_image_plugin_remove_association_function( $tt_id ) {
if ( ! isset( $tt_id ) ) {
return new WP_Error( 'taxonomy_images_tt_id_empty', __( 'tt_id is empty', 'taxonomy-images' ) );
}

$tt_id = absint( $tt_id );
if ( empty( $tt_id ) ) {
return new WP_Error( 'taxonomy_images_tt_id_not_absint', __( 'tt_id is not a positive integer', 'taxonomy-images' ) );
}

if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) {
return new WP_Error( 'taxonomy_images_tt_id_missing_caps', __( 'You do not have the correct capability to manage this term', 'taxonomy-images' ) );
}

$assoc = taxonomy_image_plugin_get_associations();
if ( ! isset( $assoc[$tt_id] ) ) {
return new WP_Error( 'taxonomy_images_nothing_to_remove', __( 'Nothing to remove', 'taxonomy-images' ) );
}

unset( $assoc[$tt_id] );

if ( update_option( 'taxonomy_image_plugin', $assoc ) ) {
return; // success
}
else {
return new WP_Error( 'taxonomy_images_fail_assoc_remove', __( 'Association could not be removed', 'taxonomy-images' ) );
}

}

````

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in taxonomy-images.php at the existing AJAX handler functions referenced around lines 498 and 580, and compare their validation, permission, and association logic with the proposed examples. Add public helpers for creating and removing term featured-image associations, preserving the shown error handling; done means plugins and scripts can call both operations programmatically.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.