-
Notifications
You must be signed in to change notification settings - Fork 0
/
somenano-wordpress.php
230 lines (188 loc) · 7.48 KB
/
somenano-wordpress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Plugin Name: SomeNano for Wordpress
* Plugin URI: https://wordpress.org/plugins/somenano/
* Description: Accept Nano cryptocurrency as payment for users to view content on your Wordpress site
* Version: 0.1.4
* Author: SomeNano
* Author URI: https://somenano.com
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
require_once( plugin_dir_path( __FILE__ ) . 'somenano-options.php' );
require_once( plugin_dir_path( __FILE__ ) . 'somenano-install.php' );
require_once( plugin_dir_path( __FILE__ ) . 'somenano-defaults.php' );
require_once( plugin_dir_path( __FILE__ ) . 'somenano-log.php' );
register_activation_hook( __FILE__, 'somenano_install' );
register_activation_hook( __FILE__, 'somenano_install_data' );
add_action( 'init', 'somenano_post_handler' );
add_action( 'the_content', 'somenano_content_handler' );
global $payment_success;
$payment_success = false;
global $payment_posted;
$payment_posted = false;
function somenano_enqueue_scripts()
{
wp_register_script( 'somenano-wordpress.js', plugins_url( '/js/somenano-wordpress.js', __FILE__ ), array(), $somenano_version, true );
wp_enqueue_script( 'somenano-wordpress.js' );
wp_register_style( 'somenano-wordpress.css', plugins_url('/css/somenano-wordpress.css', __FILE__), array(), $somenano_version, 'all' );
wp_enqueue_style( 'somenano-wordpress.css' );
}
add_action( 'wp_enqueue_scripts', 'somenano_enqueue_scripts' );
add_action( 'admin_enqueue_scripts', 'somenano_enqueue_scripts' );
add_action( 'wp_enqueue_scripts', 'somenano_enqueue_bb_scripts' );
add_action( 'admin_enqueue_scripts', 'somenano_enqueue_bb_scripts' );
function somenano_enqueue_admin_scripts()
{
wp_register_style( 'somenano-admin.css', plugins_url('/css/somenano-admin.css', __FILE__), array(), $somenano_version, 'all' );
wp_enqueue_style( 'somenano-admin.css' );
}
add_action( 'admin_enqueue_scripts', 'somenano_enqueue_admin_scripts' );
function somenano_ready()
{
// Check is somenano plugin is ready for use
$options = get_option( 'somenano_options' );
if ( !isset($options['default_paywall_account']) ) {
return false;
}
return true;
}
function somenano_action_links( $links )
{
$alert = '';
if ( !somenano_ready() ) {
$alert = '<span style="color:red;">*Nano Account Required*</span>';
}
$links = array_merge( array(
$alert,
'<a href="' . esc_url( admin_url( '/options-general.php?page=somenano-settings' ) ) . '">' . __( 'Settings', 'textdomain' ) . '</a>'
), $links );
return $links;
}
add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'somenano_action_links' );
function somenano_bypass_paywall()
{
global $wpdb;
$table_name = somenano_default('db_payments');
// Has registered user paid?
if ( get_current_user_id() > 0 ) {
$sql = 'select count(*) from ' . $table_name . ' where user_id = ' . get_current_user_id() . ' and post_id = ' . get_the_ID();
$num_rows = $wpdb->get_var( $sql );
if ( $num_rows > 0 ) return true;
}
// Is cookie set?
$cookie_name = somenano_default('cookie_prefix').get_the_ID();
if(!isset($_COOKIE[$cookie_name]) || !ctype_alnum($_COOKIE[$cookie_name])) {
return false;
}
$sql = 'select count(*) from ' . $table_name . ' where token = "' . $_COOKIE[$cookie_name] . '" and post_id = ' . get_the_ID();
$num_rows = $wpdb->get_var( $sql );
if ( $num_rows > 0 ) return true;
return false;
}
function somenano_post_handler()
{
// Check for handling of payment
if ( isset( $_POST['token'] ) && isset( $_POST['post_id'] ) ) {
// Sanitize/reject invalid inputs
if ( !ctype_alnum($_POST['token']) ) {
error_log('somenano_post_handler: token has invalid characters');
return;
}
if ( !is_numeric($_POST['post_id']) ) {
error_log('somenano_post_handler: post_id is invalid');
return;
}
// Handle inputs
global $payment_posted;
$payment_posted = true;
global $payment_success;
$payment_success = somenano_log( $_POST['token'], $_POST['post_id'] );
}
}
function somenano_content_handler( $content )
{
// Check for bypass of paywall
if ( $payment_success || somenano_bypass_paywall() ) {
return $content;
}
// Paywall in place, truncate content
$shortcode_regex = "/\[somenano_paywall(?!_).*]/";
$parts = preg_split($shortcode_regex, $content);
preg_match($shortcode_regex, $content, $m);
if ( is_front_page() ) {
return $parts[0];
}
return $parts[0] . $m[0];
}
function somenano_paywall_shortcode( $atts )
{
$options = get_option( 'somenano_options' );
if ( !isset($options['default_paywall_paid_note']) ) {
$options['default_paywall_paid_note'] = somenano_default('paywall_paid_note');
}
if ( !isset($options['default_paywall_account']) && !isset($atts['account']) ) {
return '<div id="somenano-paywall">** SomeNano Error: Nano account required for paywall **</div>';
}
if ( !isset($options['default_paywall_currency']) ) {
$options['default_paywall_currency'] = somenano_default('paywall_currency');
}
if ( !isset($options['default_paywall_amount']) ) {
$options['default_paywall_amount'] = somenano_default('paywall_amount');
}
if ( !isset($options['default_paywall_preface']) ) {
$options['default_paywall_preface'] = somenano_default('paywall_preface');
}
if ( !isset($options['default_paywall_paid_note']) ) {
$options['default_paywall_paid_note'] = somenano_default('paywall_paid_note');
}
$a = shortcode_atts( array(
'amount' => $options['default_paywall_amount'],
'account' => $options['default_paywall_account'],
'currency' => $options['default_paywall_currency'],
'preface' => $options['default_paywall_preface'],
'paid_note' => $options['default_paywall_paid_note']
), $atts );
if ( somenano_bypass_paywall() ) {
if ( strlen($a['paid_note']) == 0 ) return '';
$ret = '<div id="somenano-paywall">';
$ret .= $a['paid_note'];
$ret .= '</div>';
return $ret;
}
if ( is_front_page() ) {
return '';
}
global $wp;
// Log paywall in the db for later verification
somenano_log_paywall($a['currency'], $a['amount'], get_the_ID(), $a['account']);
$on_payment = 'document.getElementById("somenano-paywall-note").innerHTML = "Processing, please wait...";
log_payment("'. home_url( $wp->request ) .'", data.token, '. get_the_ID() .');';
somenano_bb_render(
$a['currency'],
$a['amount'],
$a['account'],
$on_payment
);
$error = '';
if ( $payment_posted && !$payment_success ) {
$error = 'Error validating payment. Please contact site admin for refund and try again.<b>';
}
$ret = '<div id="somenano-paywall">';
$ret .= '<div id="somenano-paywall-note">' . $error . $a['preface'] . '</div>';
$ret .= '<div>' . somenano_bb_button() . '</div>';
$ret .= '</div>';
return $ret;
}
add_shortcode( 'somenano_paywall', 'somenano_paywall_shortcode' );
function somenano_paywall_numpaid_shortcode( $atts )
{
$ret = '';
global $wpdb;
$table_name = somenano_default('db_payments');
$sql = 'select count(*) from ' . $table_name . ' where post_id = ' . get_the_ID();
$num_rows = $wpdb->get_var( $sql );
$ret .= $num_rows;
return $ret;
}
add_shortcode( 'somenano_paywall_numpaid', 'somenano_paywall_numpaid_shortcode' );
?>