Move function to content helpers

Fits better here than in utilities.
This commit is contained in:
Tetrakern 2023-02-10 11:23:07 +01:00
parent e35e9a2d74
commit c891152022
2 changed files with 70 additions and 70 deletions

View File

@ -1362,4 +1362,74 @@ if ( ! function_exists( 'fictioneer_echo_card' ) ) {
}
}
// =============================================================================
// GET SUPPORT LINKS
// =============================================================================
if ( ! function_exists( 'fictioneer_get_support_links' ) ) {
/**
* Returns support links for the post or author
*
* @since 5.0.19
*
* @param int|null $post_id The post ID. Defaults to global post.
* @param int|null $parent_id The parent ID. Default null.
* @param int|null $author_id The post ID. Defaults to post author ID.
*
* @return array Array of support links.
*/
function fictioneer_get_support_links( $post_id = null, $parent_id = null, $author_id = null ) {
global $post;
// Setup
$post_id = $post_id ?? $post->ID;
$author_id = $author_id ?? get_post_field( 'post_author', $post_id );
$links = [];
// Get story ID if chapter and parent ID not given
if ( $parent_id === null && get_post_type( $post_id ) == 'fcn_chapter' ) {
$parent_id = fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
}
// Post level (e.g. chapter)
$topwebfiction_link = fictioneer_get_field( 'fictioneer_story_topwebfiction_link', $post_id );
$patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $post_id );
$kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $post_id );
$subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $post_id );
$paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $post_id );
$donation_link = fictioneer_get_field( 'fictioneer_donation_link', $post_id );
// Parent level (e.g. story)
if ( ! empty( $parent_id ) ) {
if ( empty( $topwebfiction_link ) ) $topwebfiction_link = fictioneer_get_field( 'fictioneer_story_topwebfiction_link', $parent_id );
if ( empty( $patreon_link ) ) $patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $parent_id );
if ( empty( $kofi_link ) ) $kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $parent_id );
if ( empty( $subscribestar_link ) ) $subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $parent_id );
if ( empty( $paypal_link ) ) $paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $parent_id );
if ( empty( $donation_link ) ) $donation_link = fictioneer_get_field( 'fictioneer_donation_link', $parent_id );
}
// Author level
if ( $author_id ) {
if ( empty( $patreon_link ) ) $patreon_link = get_the_author_meta( 'fictioneer_user_patreon_link', $author_id );
if ( empty( $kofi_link ) ) $kofi_link = get_the_author_meta( 'fictioneer_user_kofi_link', $author_id );
if ( empty( $subscribestar_link ) ) $subscribestar_link = get_the_author_meta( 'fictioneer_user_subscribestar_link', $author_id );
if ( empty( $paypal_link ) ) $paypal_link = get_the_author_meta( 'fictioneer_user_paypal_link', $author_id );
if ( empty( $donation_link ) ) $donation_link = get_the_author_meta( 'fictioneer_user_donation_link', $author_id );
}
// Add links if found
if ( ! empty( $topwebfiction_link ) ) $links['topwebfiction'] = $topwebfiction_link;
if ( ! empty( $patreon_link ) ) $links['patreon'] = $patreon_link;
if ( ! empty( $kofi_link ) ) $links['kofi'] = $kofi_link;
if ( ! empty( $subscribestar_link ) ) $links['subscribestar'] = $subscribestar_link;
if ( ! empty( $paypal_link ) ) $links['paypal'] = $paypal_link;
if ( ! empty( $donation_link ) ) $links['donation'] = $donation_link;
// Return
return $links;
}
}
?>

View File

@ -1416,74 +1416,4 @@ if ( ! function_exists( 'fictioneer_bbcodes' ) ) {
}
}
// =============================================================================
// GET SUPPORT LINKS
// =============================================================================
if ( ! function_exists( 'fictioneer_get_support_links' ) ) {
/**
* Returns support links for the post or author
*
* @since 5.0.19
*
* @param int|null $post_id The post ID. Defaults to global post.
* @param int|null $parent_id The parent ID. Default null.
* @param int|null $author_id The post ID. Defaults to post author ID.
*
* @return array Array of support links.
*/
function fictioneer_get_support_links( $post_id = null, $parent_id = null, $author_id = null ) {
global $post;
// Setup
$post_id = $post_id ?? $post->ID;
$author_id = $author_id ?? get_post_field( 'post_author', $post_id );
$links = [];
// Get story ID if chapter and parent ID not given
if ( $parent_id === null && get_post_type( $post_id ) == 'fcn_chapter' ) {
$parent_id = fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
}
// Post level (e.g. chapter)
$topwebfiction_link = fictioneer_get_field( 'fictioneer_story_topwebfiction_link', $post_id );
$patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $post_id );
$kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $post_id );
$subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $post_id );
$paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $post_id );
$donation_link = fictioneer_get_field( 'fictioneer_donation_link', $post_id );
// Parent level (e.g. story)
if ( ! empty( $parent_id ) ) {
if ( empty( $topwebfiction_link ) ) $topwebfiction_link = fictioneer_get_field( 'fictioneer_story_topwebfiction_link', $parent_id );
if ( empty( $patreon_link ) ) $patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $parent_id );
if ( empty( $kofi_link ) ) $kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $parent_id );
if ( empty( $subscribestar_link ) ) $subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $parent_id );
if ( empty( $paypal_link ) ) $paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $parent_id );
if ( empty( $donation_link ) ) $donation_link = fictioneer_get_field( 'fictioneer_donation_link', $parent_id );
}
// Author level
if ( $author_id ) {
if ( empty( $patreon_link ) ) $patreon_link = get_the_author_meta( 'fictioneer_user_patreon_link', $author_id );
if ( empty( $kofi_link ) ) $kofi_link = get_the_author_meta( 'fictioneer_user_kofi_link', $author_id );
if ( empty( $subscribestar_link ) ) $subscribestar_link = get_the_author_meta( 'fictioneer_user_subscribestar_link', $author_id );
if ( empty( $paypal_link ) ) $paypal_link = get_the_author_meta( 'fictioneer_user_paypal_link', $author_id );
if ( empty( $donation_link ) ) $donation_link = get_the_author_meta( 'fictioneer_user_donation_link', $author_id );
}
// Add links if found
if ( ! empty( $topwebfiction_link ) ) $links['topwebfiction'] = $topwebfiction_link;
if ( ! empty( $patreon_link ) ) $links['patreon'] = $patreon_link;
if ( ! empty( $kofi_link ) ) $links['kofi'] = $kofi_link;
if ( ! empty( $subscribestar_link ) ) $links['subscribestar'] = $subscribestar_link;
if ( ! empty( $paypal_link ) ) $links['paypal'] = $paypal_link;
if ( ! empty( $donation_link ) ) $links['donation'] = $donation_link;
// Return
return $links;
}
}
?>