taler-turnstile.php (6062B)
1 <?php 2 /** 3 * Plugin Name: GNU Taler Turnstile 4 * Plugin URI: https://git.taler.net/wordpress-turnstile.git 5 * Description: Adds price fields to posts and requires payment for access via GNU Taler. 6 * Version: 1.1.0 7 * Author: GNU Taler 8 * Author URI: https://grothoff.org/christian/ 9 * License: GPL v2 or later 10 * Text Domain: taler-turnstile 11 */ 12 13 // Exit if accessed directly 14 if (!defined('ABSPATH')) { 15 exit; 16 } 17 18 // Define plugin constants 19 define('TALER_TURNSTILE_VERSION', '0.9.0'); 20 define('TALER_TURNSTILE_PLUGIN_DIR', plugin_dir_path(__FILE__)); 21 define('TALER_TURNSTILE_PLUGIN_URL', plugin_dir_url(__FILE__)); 22 23 // Include required files 24 require_once TALER_TURNSTILE_PLUGIN_DIR . 'includes/class-taler-merchant-api.php'; 25 require_once TALER_TURNSTILE_PLUGIN_DIR . 'includes/class-price-category.php'; 26 require_once TALER_TURNSTILE_PLUGIN_DIR . 'includes/class-field-manager.php'; 27 require_once TALER_TURNSTILE_PLUGIN_DIR . 'includes/class-content-filter.php'; 28 require_once TALER_TURNSTILE_PLUGIN_DIR . 'admin/class-admin-settings.php'; 29 require_once TALER_TURNSTILE_PLUGIN_DIR . 'admin/class-price-category-admin.php'; 30 require_once TALER_TURNSTILE_PLUGIN_DIR . 'admin/class-subscription-prices-admin.php'; 31 32 /** 33 * Main plugin class 34 */ 35 class Taler_Turnstile { 36 37 private static $instance = null; 38 39 public static function get_instance() { 40 if (null === self::$instance) { 41 self::$instance = new self(); 42 } 43 return self::$instance; 44 } 45 46 private function __construct() { 47 // Initialize plugin 48 add_action('plugins_loaded', array($this, 'init')); 49 add_action('admin_menu', array($this, 'add_admin_menu')); 50 add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); 51 add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts')); 52 53 // Activation and deactivation hooks 54 register_activation_hook(__FILE__, array($this, 'activate')); 55 register_deactivation_hook(__FILE__, array($this, 'deactivate')); 56 } 57 58 public function init() { 59 Taler_Field_Manager::init(); 60 Taler_Content_Filter::init(); 61 if (is_admin()) { 62 new Taler_Turnstile_Admin_Settings(); 63 new Taler_Turnstile_Price_Category_Admin(); 64 new Taler_Subscription_Prices_Admin(); 65 } 66 } 67 68 public function add_admin_menu() { 69 // Main settings page 70 add_options_page( 71 __('GNU Taler Turnstile Settings', 'taler-turnstile'), 72 __('Taler Turnstile', 'taler-turnstile'), 73 'manage_options', 74 'taler-turnstile-settings', 75 array('Taler_Turnstile_Admin_Settings', 'render_settings_page') 76 ); 77 78 // Subscription prices page 79 add_submenu_page( 80 'options-general.php', 81 __('Taler Subscription Prices', 'taler-turnstile'), 82 __('Taler Subscriptions', 'taler-turnstile'), 83 'manage_options', 84 'taler-subscription-prices', 85 array('Taler_Subscription_Prices_Admin', 'render_settings_page') 86 ); 87 88 // Price categories management 89 add_menu_page( 90 __('Taler Price Categories', 'taler-turnstile'), 91 __('Taler Prices', 'taler-turnstile'), 92 'manage_options', 93 'taler-price-categories', 94 array('Taler_Turnstile_Price_Category_Admin', 'render_list_page'), 95 'dashicons-money-alt', 96 30 97 ); 98 99 add_submenu_page( 100 'taler-price-categories', 101 __('Add Price Category', 'taler-turnstile'), 102 __('Add New', 'taler-turnstile'), 103 'manage_options', 104 'taler-price-category-add', 105 array('Taler_Turnstile_Price_Category_Admin', 'render_edit_page') 106 ); 107 } 108 109 public function enqueue_admin_scripts($hook) { 110 if (strpos($hook, 'taler') === false) { 111 return; 112 } 113 114 wp_enqueue_style( 115 'taler-turnstile-admin', 116 TALER_TURNSTILE_PLUGIN_URL . 'assets/css/admin.css', 117 array(), 118 TALER_TURNSTILE_VERSION 119 ); 120 121 wp_enqueue_script( 122 'taler-turnstile-admin', 123 TALER_TURNSTILE_PLUGIN_URL . 'assets/js/admin.js', 124 array('jquery'), 125 TALER_TURNSTILE_VERSION, 126 true 127 ); 128 } 129 130 public function enqueue_frontend_scripts() { 131 // Only enqueue on singular posts that might have paywall 132 if (!is_singular()) { 133 return; 134 } 135 136 $post_id = get_the_ID(); 137 $price_category = get_post_meta($post_id, '_taler_price_category', true); 138 139 // Only load scripts if this post has a price category 140 if (empty($price_category)) { 141 return; 142 } 143 144 // Enqueue QRCode library 145 wp_enqueue_script( 146 'qrcode', 147 TALER_TURNSTILE_PLUGIN_URL . 'assets/js/qrcode.min.js', 148 array(), 149 '1.0.0', 150 true 151 ); 152 153 // Enqueue payment button script 154 wp_enqueue_script( 155 'taler-payment-button', 156 TALER_TURNSTILE_PLUGIN_URL . 'assets/js/payment-button.js', 157 array('jquery', 'qrcode'), 158 TALER_TURNSTILE_VERSION, 159 true 160 ); 161 162 // Enqueue frontend styles 163 wp_enqueue_style( 164 'taler-turnstile-frontend', 165 TALER_TURNSTILE_PLUGIN_URL . 'assets/css/frontend.css', 166 array(), 167 TALER_TURNSTILE_VERSION 168 ); 169 } 170 171 public function activate() { 172 // Set default options 173 add_option('taler_turnstile_enabled_post_types', array('post')); 174 add_option('taler_turnstile_payment_backend_url', ''); 175 add_option('taler_turnstile_access_token', ''); 176 add_option('taler_turnstile_grant_access_on_error', false); 177 add_option('taler_turnstile_subscription_prices', array()); 178 } 179 180 public function deactivate() { 181 // Cleanup if needed 182 } 183 } 184 185 // Initialize the plugin 186 Taler_Turnstile::get_instance();