<?php

/*
Plugin Name: Simple Bitcoin donations widget
Plugin URI: https://profiles.wordpress.org/theorcawp
Description: This plugin adds a simple Bitcioin donation widget to your WordPress site. 
Author: orcawp
Author URI: https://orcawp.com/
Version: 1.1
Tested up to: 6.8
Text Domain: simple-bitcoin-donations-widget
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

if ( ! function_exists( 'sbdw_fs' ) ) {
    // Create a helper function for easy SDK access.
    function sbdw_fs() {
        global $sbdw_fs;

        if ( ! isset( $sbdw_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $sbdw_fs = fs_dynamic_init( array(
                'id'                  => '20467',
                'slug'                => 'simple-bitcoin-donations-widget',
                'type'                => 'plugin',
                'public_key'          => 'pk_46d1024c4b81d7616f0667dc720a4',
                'is_premium'          => false,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'menu'                => array(
                    'first-path'     => 'plugins.php',
                    'account'        => false,
                    'contact'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $sbdw_fs;
    }

    // Init Freemius.
    sbdw_fs();
    // Signal that SDK was initiated.
    do_action( 'sbdw_fs_loaded' );
}

  class simple_btc_donate_widget extends WP_Widget {

    public function __construct() {
      parent::__construct(false, $name = 'Simple Bitcoin Donations'); 
    }

    function widget($args, $instance) { 
    extract($args);

    $title          = apply_filters('widget_title', @$instance['title']);
    $textAbove      = wp_strip_all_tags(@$instance['btc_donate_above']);
    $bitcoinAddress = wp_strip_all_tags(@$instance['btc_Address']);
    $makeLink       = wp_strip_all_tags(@$instance['btc_create_link']);

    if (!preg_match('/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/', $bitcoinAddress)) {
        echo esc_html__("This is an invalid bitcoin address!", "simple-bitcoin-donations-widget");
        return;
    }

    // QR code generator using phpqrcode
    function qr_code_for_bitcoin_donation($data, $width = 200, $height = 200) {
        if (!class_exists('QRcode')) {
            require_once __DIR__ . '/lib/qrlib.php'; // make sure phpqrcode is included in your plugin folder
        }

        // Uploads directory
        $upload_dir = wp_upload_dir();
        $qr_dir = $upload_dir['basedir'] . '/bitcoin-qr/';
        $qr_url = $upload_dir['baseurl'] . '/bitcoin-qr/';

        if (!file_exists($qr_dir)) {
            wp_mkdir_p($qr_dir);
        }

        // Unique filename for each QR code
        $file_name = 'btc_qr_' . md5($data) . '.png';
        $file_path = $qr_dir . $file_name;

        // Generate QR if it does not exist
        if (!file_exists($file_path)) {
            QRcode::png($data, $file_path, QR_ECLEVEL_H, 6, 2);
        }

        return $qr_url . $file_name;
    }

    echo wp_kses_post($before_widget);

    echo "<p style='border: 1px solid #e0dadf; padding: 20px; margin: 2em 0; text-align: center; border-radius: 5px;'>";

    if ($title) {
        echo wp_kses_post($before_title) . esc_html($title) . wp_kses_post($after_title);
    }

    if ($textAbove) {
        echo esc_html($textAbove) . "<br>";
    }

    if ($bitcoinAddress) {
        $qrLinkShort = "bitcoin:" . $bitcoinAddress;
        $image = qr_code_for_bitcoin_donation($qrLinkShort, 300, 300);

        if ($makeLink == 'yes') {
            echo "<a href='" . esc_url($qrLinkShort) . "' target='_blank'><img style='display:block;margin:0 auto;' src='" . esc_url($image) . "' alt='Bitcoin QR Code'/></a><br>";
        } else {
            echo "<img src='" . esc_url($image) . "' style='display:block;margin:0 auto;' alt='Bitcoin QR' />";
        }
    }

    echo "</p>";
    echo wp_kses_post($after_widget);
}

   
    function update($new_instance, $old_instance) {   
      $instance = $old_instance;
      $instance['title']          = wp_strip_all_tags($new_instance['title']);
      $instance['btc_donate_above']      = wp_strip_all_tags($new_instance['btc_donate_above']);
      $instance['btc_Address'] = wp_strip_all_tags($new_instance['btc_Address']);
      $instance['btc_create_link']       = wp_strip_all_tags($new_instance['btc_create_link']);
      return $instance;
    }
   
    function form($instance) {  


      $title          = esc_attr(@$instance['title']);
      $textAbove      = esc_attr(@$instance['btc_donate_above']);
      $bitcoinAddress = esc_attr(@$instance['btc_Address']);
      $makeLink       = esc_attr(@$instance['btc_create_link']);
      ?>
       <p>

        <label for="<?php echo esc_html( $this->get_field_id('title') ); ?>"><b><?php esc_html_e('Title:', 'simple-bitcoin-donations-widget'); ?></b></label> 
        <input class="widefat" id="<?php echo esc_html( $this->get_field_id('title') ); ?>" name="<?php echo esc_html($this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_html($title ); ?>" /><br>
        <span class='helptext'><?php esc_html_e("The title of your widget.", 'simple-bitcoin-donations-widget'); ?></span>
      </p>
       <p>
        <label for="<?php echo esc_html($this->get_field_id('btc_donate_above') ); ?>"><b><?php esc_html_e('Your custom message above the QR Code:', 'simple-bitcoin-donations-widget'); ?></b></label> 
        <input class="widefat" id="<?php echo esc_html( $this->get_field_id('btc_donate_above') ); ?>" name="<?php echo esc_html($this->get_field_name('btc_donate_above') ); ?>" type="text" value="<?php echo esc_html($textAbove ); ?>" /><br>
        <span class='helptext'><?php esc_html_e("This is a custom message to your site visitors asking for donations.", 'simple-bitcoin-donations-widget'); ?></span>
      </p>

       <p>
        <label for="<?php echo esc_html( $this->get_field_id('btc_Address') ); ?>"><b><?php esc_html_e('Bitcoin address:', 'simple-bitcoin-donations-widget'); ?></b></label> 
        <input class="widefat" id="<?php echo esc_html( $this->get_field_id('btc_Address') ); ?>" name="<?php echo esc_html($this->get_field_name('btc_Address') ); ?>" type="text" value="<?php echo esc_html($bitcoinAddress ); ?>" /><br>
        <span class='helptext'><?php esc_html_e("Your valid Bitcoin address. This is required to receive donations.", 'simple-bitcoin-donations-widget'); ?></span>
      </p>


       <p>
        <label for="<?php echo esc_attr( $this->get_field_id('btc_create_link') ); ?>"><b><?php esc_html_e('Enable this to make the QR clickable:', 'simple-bitcoin-donations-widget'); ?></b></label><br> 
        <input class="widefat" id="<?php echo esc_attr($this->get_field_id('btc_create_link') ); ?>" name="<?php echo esc_attr( $this->get_field_name('btc_create_link') ); ?>" type="checkbox" value="yes" <?php checked( $makeLink, 'yes' ); ?> /> 
        <span class='helptext'><?php esc_html_e("This is good for those on mobile devices with wallets.", 'simple-bitcoin-donations-widget'); ?></span>
      </p>

      <span class='helptext'><span style='color:black'><?php esc_html_e( "Please send me a Bitcoin donation if you like this widget", 'simple-bitcoin-donations-widget'); ?></span></span><br></br>
      <span class='helptext'><span style='color:#96588a'><?php echo esc_html( "1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG"); ?></span></span><br></br>

      <?php
    }
  } 

  function register_btc_donate_widget() {
  register_widget( 'simple_btc_donate_widget' );
}
add_action( 'widgets_init', 'register_btc_donate_widget' );
