From be8fa221f10991f2427c75d953b16319763d515f Mon Sep 17 00:00:00 2001
From: Tetrakern <26898880+Tetrakern@users.noreply.github.com>
Date: Fri, 10 Feb 2023 02:00:43 +0100
Subject: [PATCH] Add utility function to get support links
---
includes/functions/_utility.php | 78 +++++++++++++++++++++
includes/functions/hooks/_chapter_hooks.php | 54 ++++----------
2 files changed, 93 insertions(+), 39 deletions(-)
diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php
index e5ef16d1..bd08ec53 100644
--- a/includes/functions/_utility.php
+++ b/includes/functions/_utility.php
@@ -1416,4 +1416,82 @@ 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 );
+ $post_type = get_post_type( $post_id );
+ $links = array(
+ 'topwebfiction' => null,
+ 'patreon' => null,
+ 'kofi' => null,
+ 'subscribestar' => null,
+ 'paypal' => null,
+ 'donation' => null
+ );
+
+ // Get story ID if chapter and parent ID not given
+ if ( empty( $parent_id ) && $post_type == '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;
+ }
+}
+
?>
diff --git a/includes/functions/hooks/_chapter_hooks.php b/includes/functions/hooks/_chapter_hooks.php
index f770de7c..9332fd81 100644
--- a/includes/functions/hooks/_chapter_hooks.php
+++ b/includes/functions/hooks/_chapter_hooks.php
@@ -398,9 +398,9 @@ if ( ! function_exists( 'fictioneer_chapter_support_links' ) ) {
*
* @since Fictioneer 5.0
*
- * @param WP_User $args['author'] Author of the post.
+ * @param WP_User $args['author'] Author of the post.
* @param WP_Post|null $args['story_post'] Optional. Post object of the story.
- * @param int $args['chapter_id'] The chapter ID.
+ * @param int $args['chapter_id'] The chapter ID.
*/
function fictioneer_chapter_support_links( $args ) {
@@ -409,84 +409,60 @@ if ( ! function_exists( 'fictioneer_chapter_support_links' ) ) {
// Setup
$author_id = $args['author']->ID;
- $patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $args['chapter_id'] );
- $kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $args['chapter_id'] );
- $subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $args['chapter_id'] );
- $paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $args['chapter_id'] );
- $donation_link = fictioneer_get_field( 'fictioneer_donation_link', $args['chapter_id'] );
- $topwebfiction_link = null;
+ $links = fictioneer_get_support_links( $args['chapter_id'], $args['story_post']->ID, $author_id );
$support_links = [];
- // Story level support links
- if ( ! empty( $args['story_post'] ) ) {
- $story_id = $args['story_post']->ID;
- $topwebfiction_link = fictioneer_get_field( 'fictioneer_story_topwebfiction_link', $story_id );
-
- if ( ! $patreon_link ) $patreon_link = fictioneer_get_field( 'fictioneer_patreon_link', $story_id );
- if ( ! $kofi_link ) $kofi_link = fictioneer_get_field( 'fictioneer_kofi_link', $story_id );
- if ( ! $subscribestar_link ) $subscribestar_link = fictioneer_get_field( 'fictioneer_subscribestar_link', $story_id );
- if ( ! $paypal_link ) $paypal_link = fictioneer_get_field( 'fictioneer_paypal_link', $story_id );
- if ( ! $donation_link ) $donation_link = fictioneer_get_field( 'fictioneer_donation_link', $story_id );
- }
-
- // Author level support links
- if ( ! $patreon_link ) $patreon_link = get_the_author_meta( 'fictioneer_user_patreon_link', $author_id );
- if ( ! $kofi_link ) $kofi_link = get_the_author_meta( 'fictioneer_user_kofi_link', $author_id );
- if ( ! $subscribestar_link ) $subscribestar_link = get_the_author_meta( 'fictioneer_user_subscribestar_link', $author_id );
- if ( ! $paypal_link ) $paypal_link = get_the_author_meta( 'fictioneer_user_paypal_link', $author_id );
- if ( ! $donation_link ) $donation_link = get_the_author_meta( 'fictioneer_user_donation_link', $author_id );
-
// Topwebfiction?
- if ( $topwebfiction_link ) {
+ if ( $links['topwebfiction'] ) {
$support_links['topwebfiction'] = array(
'label' => __( 'Top Web Fiction', 'fictioneer' ),
'icon' => '',
- 'link' => $topwebfiction_link
+ 'link' => $links['topwebfiction']
);
}
// Patreon?
- if ( $patreon_link ) {
+ if ( $links['patreon'] ) {
$support_links['patreon'] = array(
'label' => __( 'Patreon', 'fictioneer' ),
'icon' => '',
- 'link' => $patreon_link
+ 'link' => $links['patreon']
);
}
// Ko-Fi?
- if ( $kofi_link ) {
+ if ( $links['kofi'] ) {
$support_links['kofi'] = array(
'label' => __( 'Ko-Fi', 'fictioneer' ),
'icon' => fictioneer_get_icon( 'kofi' ),
- 'link' => $kofi_link
+ 'link' => $links['kofi']
);
}
// SubscribeStar?
- if ( $subscribestar_link ) {
+ if ( $links['subscribestar'] ) {
$support_links['subscribestar'] = array(
'label' => __( 'SubscribeStar', 'fictioneer' ),
'icon' => '',
- 'link' => $subscribestar_link
+ 'link' => $links['subscribestar']
);
}
// PayPal?
- if ( $paypal_link ) {
+ if ( $links['paypal'] ) {
$support_links['paypal'] = array(
'label' => __( 'PayPal', 'fictioneer' ),
'icon' => '',
- 'link' => $paypal_link
+ 'link' => $links['paypal']
);
}
// Donation?
- if ( $paypal_link ) {
+ if ( $links['donation'] ) {
$support_links['donation'] = array(
'label' => __( 'Donation', 'fictioneer' ),
'icon' => '',
- 'link' => $donation_link
+ 'link' => $links['donation']
);
}