From ca97fd78077263c8c65d43e5c37d4b375f932b52 Mon Sep 17 00:00:00 2001 From: Vlado Bosnjak Date: Sun, 19 Mar 2023 17:47:40 +0700 Subject: [PATCH 1/5] custom lightning address per user account --- admin/class-bln-publisher-admin.php | 13 +++++++ includes/class-bln-publisher.php | 2 ++ public/class-bln-publisher-public.php | 50 +++++++++++++++++++++------ 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/admin/class-bln-publisher-admin.php b/admin/class-bln-publisher-admin.php index 73adcbc..8ad9ce2 100644 --- a/admin/class-bln-publisher-admin.php +++ b/admin/class-bln-publisher-admin.php @@ -175,5 +175,18 @@ function render_webln_donation_button($attributes) '; } + /** + * Add a Bitcoin Lightning address field to the WordPress user profile + * + * @param [type] $user_contactmethods [description] + * @return [type] [description] + * + * @link https://developer.wordpress.org/reference/hooks/user_contactmethods/ + */ + function add_user_lnp_address( $methods ) + { + $methods['_lnurl_meta_tag_lnurlp'] = __('Ligtning Address', 'lnp-alby'); + return $methods; + } } diff --git a/includes/class-bln-publisher.php b/includes/class-bln-publisher.php index 12dc1b3..9c97f76 100644 --- a/includes/class-bln-publisher.php +++ b/includes/class-bln-publisher.php @@ -331,6 +331,8 @@ private function define_admin_hooks() $this->loader->add_action('admin_menu', $this->plugin_admin, 'lightning_menu'); // Register the donation block $this->loader->add_action('init', $this->plugin_admin, 'init_gutenberg_blocks'); + // Register custom field in user profile for custom address + $this->loader->add_filter('user_contactmethods', $this->plugin_admin, 'add_user_lnp_address'); } /** diff --git a/public/class-bln-publisher-public.php b/public/class-bln-publisher-public.php index b322152..15b5ad9 100644 --- a/public/class-bln-publisher-public.php +++ b/public/class-bln-publisher-public.php @@ -119,18 +119,46 @@ public function add_lnurl_to_rss_item_filter() public function hook_meta_tags() { - if ( - (!empty($this->plugin->getGeneralOptions()['lnurl_meta_tag']) && $this->plugin->getGeneralOptions()['lnurl_meta_tag']) || - (!empty($this->plugin->getGeneralOptions()['lnurl_meta_tag_lnurlp']) && $this->plugin->getGeneralOptions()['lnurl_meta_tag_lnurlp']) - ) { - if (!empty($this->plugin->getGeneralOptions()['lnurl_meta_tag_lnurlp'])) { - $lnurl = $this->plugin->getGeneralOptions()['lnurl_meta_tag_lnurlp']; - } else { - $lnurl = get_rest_url(null, '/lnp-alby/v1/lnurlp'); - } - $lnurl_without_protocol = preg_replace('/^https?:\/\//', '', $lnurl); - echo ''; + // All options + $options = $this->plugin->getGeneralOptions(); + + // In case options not saved or disabled + if ( empty($options['lnurl_meta_tag']) ) + { + return; } + + // LN Address + $lnurl = ( empty($options['lnurl_meta_tag_lnurlp']) ) + ? get_rest_url(null, '/lnp-alby/v1/lnurlp') // Default + : $options['lnurl_meta_tag_lnurlp']; // Custom option + + + /** + * Filter to enable programmatic update + * Usage: + * + * add_filter( 'lnurl_meta_tag_lnurlp', function( $lnurl ) { + * + * if ( is_page('about-us') ) + * { + * $lnurl = 'me@you.io'; + * } + * + * return $lnurl; + * }); + */ + + $lnurl = apply_filters( 'lnurl_meta_tag_lnurlp', $lnurl ); + + // Strip protocol + $lnurl_without_protocol = preg_replace('/^https?:\/\//', '', $lnurl); + + // Echo value + printf( + '', + esc_attr($lnurl_without_protocol) + ); } public function add_v4v_rss_ns_tag() From 2b1deb8828852eda8a66cec270b56e5922a3d586 Mon Sep 17 00:00:00 2001 From: Vlado Bosnjak Date: Sun, 19 Mar 2023 18:00:41 +0700 Subject: [PATCH 2/5] add meta tag to post --- public/class-bln-publisher-public.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/public/class-bln-publisher-public.php b/public/class-bln-publisher-public.php index 15b5ad9..7645ed3 100644 --- a/public/class-bln-publisher-public.php +++ b/public/class-bln-publisher-public.php @@ -133,6 +133,18 @@ public function hook_meta_tags() ? get_rest_url(null, '/lnp-alby/v1/lnurlp') // Default : $options['lnurl_meta_tag_lnurlp']; // Custom option + + // In case of WP_Post use authors lightning address + if ( is_singular( 'post' ) ) + { + global $post; + $address = get_user_meta( $post->post_author, '_lnurl_meta_tag_lnurlp', true ); + + if ( $address ) + { + $lnurl = $address; + } + } /** * Filter to enable programmatic update @@ -148,7 +160,6 @@ public function hook_meta_tags() * return $lnurl; * }); */ - $lnurl = apply_filters( 'lnurl_meta_tag_lnurlp', $lnurl ); // Strip protocol From 6870239e27e9804dd7c884c4df3a36e0b8e77fc5 Mon Sep 17 00:00:00 2001 From: Vlado Bosnjak Date: Sun, 19 Mar 2023 18:06:15 +0700 Subject: [PATCH 3/5] inline comments update --- admin/class-bln-publisher-admin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/admin/class-bln-publisher-admin.php b/admin/class-bln-publisher-admin.php index 8ad9ce2..ee1ca58 100644 --- a/admin/class-bln-publisher-admin.php +++ b/admin/class-bln-publisher-admin.php @@ -176,10 +176,10 @@ function render_webln_donation_button($attributes) } /** - * Add a Bitcoin Lightning address field to the WordPress user profile + * Add a Bitcoin Lightning address field to WordPress user profile page * - * @param [type] $user_contactmethods [description] - * @return [type] [description] + * @param array $methods + * @return array * * @link https://developer.wordpress.org/reference/hooks/user_contactmethods/ */ From 93665eb079a8895fc2c9af9ca1472032c00d8cb5 Mon Sep 17 00:00:00 2001 From: Vlado Bosnjak Date: Sun, 26 Mar 2023 17:25:29 +0700 Subject: [PATCH 4/5] Update setup_client() method to use custom author LN address on posts in case exists --- admin/class-bln-publisher-admin.php | 2 +- includes/class-bln-publisher.php | 37 +++++++++++++++---- .../class-bln-publisher-lnaddress-client.php | 1 + public/class-bln-publisher-public.php | 4 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/admin/class-bln-publisher-admin.php b/admin/class-bln-publisher-admin.php index ee1ca58..0ab9fa9 100644 --- a/admin/class-bln-publisher-admin.php +++ b/admin/class-bln-publisher-admin.php @@ -185,7 +185,7 @@ function render_webln_donation_button($attributes) */ function add_user_lnp_address( $methods ) { - $methods['_lnurl_meta_tag_lnurlp'] = __('Ligtning Address', 'lnp-alby'); + $methods['_lnp_ln_address'] = __('Ligtning Address', 'lnp-alby'); return $methods; } diff --git a/includes/class-bln-publisher.php b/includes/class-bln-publisher.php index 9c97f76..fccb781 100644 --- a/includes/class-bln-publisher.php +++ b/includes/class-bln-publisher.php @@ -290,21 +290,44 @@ private function setup_client() $this->lightningClient = null; $this->lightningClientType = null; - if (!$this->lightningClient) { - try { - if (!empty($this->connection_options['lnd_address'])) { + if (!$this->lightningClient) + { + // Default to post author LNaddress + // if specified in user profile + if ( is_singular( array('post') ) ) + { + global $post; + $address = get_user_meta( $post->post_author, '_lnp_ln_address', true ); + + if ( $address ) + { + $this->connection_options['lnaddress_address'] = $address; + } + } + + try + { + if (!empty($this->connection_options['lnd_address'])) + { $this->lightningClientType = 'lnd'; $this->lightningClient = new BLN_Publisher_LND_Client($this->connection_options); - } elseif (!empty($this->connection_options['lnbits_apikey'])) { + } + elseif (!empty($this->connection_options['lnbits_apikey'])) { $this->lightningClientType = 'lnbits'; $this->lightningClient = new BLN_Publisher_LNBits_Client($this->connection_options); - } elseif (!empty($this->connection_options['lnaddress_address']) || !empty($this->connection_options['lnaddress_lnurl'])) { + } + elseif (!empty($this->connection_options['lnaddress_address']) || !empty($this->connection_options['lnaddress_lnurl'])) + { $this->lightningClientType = 'lnaddress'; $this->lightningClient = new BLN_Publisher_LNAddress_Client($this->connection_options); - } elseif (!empty($this->connection_options['btcpay_host'])) { + } + elseif (!empty($this->connection_options['btcpay_host'])) + { $this->lightningClientType = 'btcpay'; $this->lightningClient = new BLN_Publisher_BTCPay_Client($this->connection_options); - } elseif (!empty($this->connection_options['lndhub_url']) && !empty($this->connection_options['lndhub_login']) && !empty($this->connection_options['lndhub_password'])) { + } + elseif (!empty($this->connection_options['lndhub_url']) && !empty($this->connection_options['lndhub_login']) && !empty($this->connection_options['lndhub_password'])) + { $this->lightningClientType = 'lndhub'; $this->lightningClient = new BLN_Publisher_LNDHub_Client($this->connection_options); } diff --git a/includes/clients/class-bln-publisher-lnaddress-client.php b/includes/clients/class-bln-publisher-lnaddress-client.php index 3a18958..c3dfae7 100644 --- a/includes/clients/class-bln-publisher-lnaddress-client.php +++ b/includes/clients/class-bln-publisher-lnaddress-client.php @@ -15,6 +15,7 @@ class BLN_Publisher_LNAddress_Client extends Abstract_BLN_Publisher_Client public function __construct($options) { parent::__construct($options); + try { $this->client = new LightningAddress(); if (!empty($this->options['lnaddress_address'])) { diff --git a/public/class-bln-publisher-public.php b/public/class-bln-publisher-public.php index 7645ed3..d1a273d 100644 --- a/public/class-bln-publisher-public.php +++ b/public/class-bln-publisher-public.php @@ -135,10 +135,10 @@ public function hook_meta_tags() // In case of WP_Post use authors lightning address - if ( is_singular( 'post' ) ) + if ( is_singular( array('post') ) ) { global $post; - $address = get_user_meta( $post->post_author, '_lnurl_meta_tag_lnurlp', true ); + $address = get_user_meta( $post->post_author, '_lnp_ln_address', true ); if ( $address ) { From 525cbf935630b3fd5d61fed465430b896a259aa6 Mon Sep 17 00:00:00 2001 From: Vlado Bosnjak Date: Mon, 17 Apr 2023 10:07:43 +0700 Subject: [PATCH 5/5] force BLN_Publisher_LNAddress_Client for posts in case author has ln address in his profile --- includes/class-bln-publisher.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/class-bln-publisher.php b/includes/class-bln-publisher.php index fccb781..1382389 100644 --- a/includes/class-bln-publisher.php +++ b/includes/class-bln-publisher.php @@ -302,6 +302,11 @@ private function setup_client() if ( $address ) { $this->connection_options['lnaddress_address'] = $address; + + $this->lightningClientType = 'lnaddress'; + $this->lightningClient = new BLN_Publisher_LNAddress_Client($this->connection_options); + + return; } }