Skip to content

Commit

Permalink
Merge pull request #29 from getAlby/issue/12
Browse files Browse the repository at this point in the history
Issue/12
  • Loading branch information
bumi authored Apr 27, 2023
2 parents 032aa7a + fd6c007 commit 0bcc230
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
15 changes: 14 additions & 1 deletion admin/class-bln-publisher-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,20 @@ function render_webln_donation_button($attributes)
</div>';
}


/**
* Add a Bitcoin Lightning address field to WordPress user profile page
*
* @param array $methods
* @return array
*
* @link https://developer.wordpress.org/reference/hooks/user_contactmethods/
*/
function add_user_lnp_address( $methods )
{
$methods['_lnp_ln_address'] = __('Ligtning Address', 'lnp-alby');
return $methods;
}

/**
* Add settings link to plugin actions
*
Expand Down
44 changes: 37 additions & 7 deletions includes/class-bln-publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,49 @@ 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;

$this->lightningClientType = 'lnaddress';
$this->lightningClient = new BLN_Publisher_LNAddress_Client($this->connection_options);

return;
}
}

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);
}
Expand All @@ -331,6 +359,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');
$this->loader->add_filter('plugin_action_links', $this->plugin_admin, 'add_plugin_link', 10, 2);
}

Expand Down
1 change: 1 addition & 0 deletions includes/clients/class-bln-publisher-lnaddress-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
59 changes: 49 additions & 10 deletions public/class-bln-publisher-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,57 @@ 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');
// 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


// In case of WP_Post use authors lightning address
if ( is_singular( array('post') ) )
{
global $post;
$address = get_user_meta( $post->post_author, '_lnp_ln_address', true );

if ( $address )
{
$lnurl = $address;
}
$lnurl_without_protocol = preg_replace('/^https?:\/\//', '', $lnurl);
echo '<meta name="lightning" content="lnurlp:' . esc_attr($lnurl_without_protocol) . '" />';
}

/**
* Filter to enable programmatic update
* Usage:
*
* add_filter( 'lnurl_meta_tag_lnurlp', function( $lnurl ) {
*
* if ( is_page('about-us') )
* {
* $lnurl = '[email protected]';
* }
*
* return $lnurl;
* });
*/
$lnurl = apply_filters( 'lnurl_meta_tag_lnurlp', $lnurl );

// Strip protocol
$lnurl_without_protocol = preg_replace('/^https?:\/\//', '', $lnurl);

// Echo value
printf(
'<meta name="lightning" content="lnurlp:%s" />',
esc_attr($lnurl_without_protocol)
);
}

public function add_v4v_rss_ns_tag()
Expand Down

0 comments on commit 0bcc230

Please sign in to comment.