-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathson-content-protector.php
194 lines (170 loc) · 8.24 KB
/
son-content-protector.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
<?php
/*
Plugin Name: Son Secure Content Guard
Author: tsquare07
Version: 1.0.0
Description: You can stop copycats from easily taking away your hard work. Protect your text, images, and other media with this lightweight plugin. Keep your content safe by deactivating right click, copying and text highlighting even when JavaScript is disabled.
Author URI: https://www.iamtsquare07.com
License: GPLv3 or later
Text Domain: son-secure-content-guard
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
function son_cp_plugin_menu() {
add_options_page(
'Son Content Protector Settings',
'Son Content Protector',
'manage_options',
'son-content-protector-settings',
'son_cp_settings_page'
);
}
add_action('admin_menu', 'son_cp_plugin_menu');
function son_cp_settings_page() {
?>
<div class="wrap">
<h2>Son Content Protector Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('son-cp-settings-group'); ?>
<?php do_settings_sections('son-content-protector-settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Copy Protection:</th>
<td>
<label>
<input type="radio" id="enableCopy" name="son_cp_enable_copy_protection" value="1" <?php checked(get_option('son_cp_enable_copy_protection'), 1); ?>/> Enable
</label>
<label>
<input type="radio" id="disableCopy" name="son_cp_enable_copy_protection" value="0" <?php checked(get_option('son_cp_enable_copy_protection'), 0); ?> /> Disable
</label>
</td>
</tr>
<tr valign="top">
<th scope="row">Right-Click Protection:</th>
<td>
<label>
<input type="radio" id="enableRightClick" name="son_cp_enable_right_click_protection" value="1" <?php checked(get_option('son_cp_enable_right_click_protection'), 1); ?>/> Enable
</label>
<label>
<input type="radio" id="disableRightClick" name="son_cp_enable_right_click_protection" value="0" <?php checked(get_option('son_cp_enable_right_click_protection'), 0); ?> /> Disable
</label>
</td>
</tr>
<tr valign="top">
<th scope="row">Edit Copy Protection Alert:</th>
<td>
<textarea rows="5" cols="30" name="son_cp_copy_message" value="<?php echo esc_attr(get_option('son_cp_copy_message', 'Gotcha!😜 Content is protected. Copying is not allowed.')); ?>"><?php echo esc_attr(get_option('son_cp_copy_message', 'Gotcha!😜 Content is protected. Copying is not allowed.')); ?></textarea>
</td>
</tr>
<tr valign="top">
<th scope="row">Edit Right-Click Protection Alert:</th>
<td>
<textarea rows="5" cols="30" name="son_cp_right_click_message" value=""><?php echo esc_attr(get_option('son_cp_right_click_message', 'For copyright protection, right-clicking is currently disabled on this website. Sorry for the inconvenience.')); ?></textarea>
</td>
</tr>
</table>
<script>
function handleRadioButtonChange(event) {
const target = event.target;
if (target.name === 'son_cp_enable_copy_protection' || target.name === 'son_cp_enable_right_click_protection') {
localStorage.setItem(target.name, target.value);
}
}
// Load the initial button values from localStorage
function loadRadioButtonsFromLocalStorage() {
const enableCopy = document.getElementById('enableCopy');
const disableCopy = document.getElementById('disableCopy');
const enableRightClick = document.getElementById('enableRightClick');
const disableRightClick = document.getElementById('disableRightClick');
const copyProtectionValue = localStorage.getItem('son_cp_enable_copy_protection');
const rightClickProtectionValue = localStorage.getItem('son_cp_enable_right_click_protection');
if (copyProtectionValue === '1') {
enableCopy.checked = true;
} else {
disableCopy.checked = true;
}
if (rightClickProtectionValue === '1') {
enableRightClick.checked = true;
} else {
disableRightClick.checked = true;
}
}
// Attach event listeners
const enableCopy = document.getElementById('enableCopy');
const disableCopy = document.getElementById('disableCopy');
const enableRightClick = document.getElementById('enableRightClick');
const disableRightClick = document.getElementById('disableRightClick');
enableCopy.addEventListener('change', handleRadioButtonChange);
disableCopy.addEventListener('change', handleRadioButtonChange);
enableRightClick.addEventListener('change', handleRadioButtonChange);
disableRightClick.addEventListener('change', handleRadioButtonChange);
// Load the initial values from localStorage
loadRadioButtonsFromLocalStorage();
</script>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Add a settings link to the plugin actions
function son_cp_add_settings_link($links) {
$settings_link = '<a href="options-general.php?page=son-content-protector-settings">Settings</a>';
array_push($links, $settings_link);
return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'son_cp_add_settings_link');
function son_cp_register_settings() {
register_setting('son-cp-settings-group', 'son_cp_enable_copy_protection', 'sanitize_text_field');
register_setting('son-cp-settings-group', 'son_cp_enable_right_click_protection', 'sanitize_text_field');
register_setting('son-cp-settings-group', 'son_cp_copy_message', 'sanitize_text_field');
register_setting('son-cp-settings-group', 'son_cp_right_click_message', 'sanitize_text_field');
}
add_action('admin_init', 'son_cp_register_settings');
// Enqueue JavaScript
function son_cp_enqueue_scripts() {
// Add JavaScript to detect whether JavaScript is enabled or disabled
echo '<noscript>
document.addEventListener("DOMContentLoaded", function() {
var body = document.body;
body.classList.add("js-enabled");
});
</noscript>';
wp_enqueue_script('content-protector', plugin_dir_url(__FILE__) . 'includes/content-protector.js', array(), '1.0', true);
// Retrieve custom messages from options
$custom_admin_message = get_option('son_cp_admin_message');
$custom_right_click_message = get_option('son_cp_right_click_message');
// Pass custom messages to JavaScript
wp_localize_script('content-protector', 'cp_custom_messages', array(
'adminMessage' => $custom_admin_message,
'rightClickMessage' => $custom_right_click_message,
));
}
add_action('wp_enqueue_scripts', 'son_cp_enqueue_scripts');
// Add CSS to prevent right-clicking and copying when JavaScript is disabled
function scp_add_css() {
echo '<noscript>
<style>
/* Prevent text selection when JavaScript is disabled */
body {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
</noscript>';
}
add_action('wp_head', 'scp_add_css');
// Cleaning up the plugin data on plugin deactivation
function son_cp_uninstall() {
// Delete the plugin options when the plugin is uninstalled
delete_option('son_cp_enable_copy_protection');
delete_option('son_cp_enable_right_click_protection');
delete_option('son_cp_copy_message');
delete_option('son_cp_right_click_message');
}
// Register the uninstall hook
register_uninstall_hook(__FILE__, 'son_cp_uninstall');
?>