| 1 |
<?php |
| 2 |
/** |
| 3 |
Plugin Name: WPCopyRights网站防复制插件 |
| 4 |
Plugin URI: https://www.laojiang.me/5931.html |
| 5 |
Description: 功能强大的WordPress网站防复制插件,支持多种防护方式。� |
| 6 |
�众号:老蒋朋友圈 |
| 7 |
Version: 6.6 |
| 8 |
Author: 老蒋和他的小伙伴 |
| 9 |
Author URI: https://www.laojiang.me |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
class WPCopyRights { |
| 17 |
private static $instance = null; |
| 18 |
private $options; |
| 19 |
private $version = '6.6'; |
| 20 |
|
| 21 |
public static function getInstance() { |
| 22 |
if (null === self::$instance) { |
| 23 |
self::$instance = new self(); |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
private function __construct() { |
| 29 |
$this->defineConstants(); |
| 30 |
$this->initHooks(); |
| 31 |
} |
| 32 |
|
| 33 |
private function defineConstants() { |
| 34 |
define('WP_COPY_RIGHTS_VERSION', $this->version); |
| 35 |
define('WP_COPY_RIGHTS_PATH', plugin_dir_path(__FILE__)); |
| 36 |
define('WP_COPY_RIGHTS_URL', plugin_dir_url(__FILE__)); |
| 37 |
define('WP_COPY_RIGHTS_BASE_FOLDER', plugin_basename(dirname(__FILE__))); |
| 38 |
} |
| 39 |
|
| 40 |
private function initHooks() { |
| 41 |
// 初始化钩子 |
| 42 |
register_activation_hook(__FILE__, array($this, 'activate')); |
| 43 |
add_action('wp_enqueue_scripts', array($this, 'enqueueAssets')); |
| 44 |
add_action('admin_menu', array($this, 'addSettingPage')); |
| 45 |
add_filter('plugin_action_links', array($this, 'addPluginLinks'), 10, 2); |
| 46 |
} |
| 47 |
|
| 48 |
public function activate() { |
| 49 |
$default_options = array( |
| 50 |
'version' => WP_COPY_RIGHTS_VERSION, |
| 51 |
'switch' => false, |
| 52 |
'options' => array( |
| 53 |
'disable_right_click' => false, |
| 54 |
'disable_select_text' => false, |
| 55 |
'disable_drag_image' => false, |
| 56 |
'disable_f12' => false, |
| 57 |
'disable_ctrl_shift_i' => false, |
| 58 |
'disable_ctrl_shift_j' => false, |
| 59 |
'disable_print' => false, |
| 60 |
'disable_view_source' => false, |
| 61 |
'disable_save_page' => false, |
| 62 |
'disable_select_all' => false, |
| 63 |
'disable_copy_content' => false, |
| 64 |
'disable_cut_content' => false, |
| 65 |
'enable_adminer' => false, |
| 66 |
'enable_loginer' => false, |
| 67 |
'enable_devtools_open_notice' => false, |
| 68 |
'enable_endcopyrights' => false, |
| 69 |
'enable_watermark' => false, |
| 70 |
'enable_watermark_member' => false, |
| 71 |
'watermark_member_mode' => 'nickname', |
| 72 |
'watermark_member_custom' => '', |
| 73 |
'custom_message' => '', |
| 74 |
'watermark_text' => '', |
| 75 |
'watermark_font_size' => 24, |
| 76 |
'watermark_color' => '#000000', |
| 77 |
'watermark_angle' => -30, |
| 78 |
'watermark_opacity' => 0.10, |
| 79 |
'watermark_gap_x' => 220, |
| 80 |
'watermark_gap_y' => 140, |
| 81 |
'exclude_pages' => '', |
| 82 |
'exclude_posts' => '', |
| 83 |
'exclude_categories' => '', |
| 84 |
), |
| 85 |
); |
| 86 |
|
| 87 |
if (!get_option('wp_copy_rights_options')) { |
| 88 |
add_option('wp_copy_rights_options', $default_options); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public function enqueueAssets() { |
| 93 |
if ($this->shouldProtectContent()) { |
| 94 |
wp_enqueue_style( |
| 95 |
'wp-copy-rights-style', |
| 96 |
WP_COPY_RIGHTS_URL . 'assets/css/protect.css', |
| 97 |
array(), |
| 98 |
WP_COPY_RIGHTS_VERSION |
| 99 |
); |
| 100 |
|
| 101 |
wp_enqueue_script( |
| 102 |
'wp-copy-rights-script', |
| 103 |
WP_COPY_RIGHTS_URL . 'assets/js/protect.js', |
| 104 |
array('jquery'), |
| 105 |
WP_COPY_RIGHTS_VERSION, |
| 106 |
true |
| 107 |
); |
| 108 |
|
| 109 |
$options = $this->getOptions(); |
| 110 |
$opts = isset($options['options']) && is_array($options['options']) ? $options['options'] : array(); |
| 111 |
$resolved_watermark = $this->resolve_watermark_display_text($opts); |
| 112 |
wp_localize_script('wp-copy-rights-script', 'wpCopyRightsSettings', array( |
| 113 |
'options' => array_merge( |
| 114 |
array('switch' => isset($options['switch']) ? $options['switch'] : false), |
| 115 |
$opts, |
| 116 |
array( |
| 117 |
'watermark_display_text' => $resolved_watermark, |
| 118 |
'watermark_enabled' => $resolved_watermark !== '', |
| 119 |
) |
| 120 |
), |
| 121 |
'messages' => array( |
| 122 |
'copyWarning' => esc_html(isset($options['options']['custom_message']) ? $options['options']['custom_message'] : ''), |
| 123 |
'devtoolsOpenNotice' => esc_html('检测到您可能打开了开发� |
| 124 |
工� |
| 125 |
�,请� |
| 126 |
�闭后正常浏览。本站� |
| 127 |
容受版权保护。'), |
| 128 |
), |
| 129 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 130 |
'nonce' => wp_create_nonce('wp_copy_rights_nonce') |
| 131 |
)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
private function shouldProtectContent() { |
| 136 |
$options = $this->getOptions(); |
| 137 |
|
| 138 |
if (!isset($options['switch']) || !$options['switch']) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
// 检查管理员例外 |
| 143 |
if (isset($options['options']['enable_adminer']) && $options['options']['enable_adminer'] && current_user_can('administrator')) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
// 检查登录用户例外 |
| 148 |
if (isset($options['options']['enable_loginer']) && $options['options']['enable_loginer'] && is_user_logged_in()) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// 检查特定页面例外 |
| 153 |
if ($this->isExcludedPage()) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
private function isExcludedPage() { |
| 161 |
$options = $this->getOptions(); |
| 162 |
|
| 163 |
// 检查排除的页面 |
| 164 |
if (isset($options['options']['exclude_pages']) && !empty($options['options']['exclude_pages'])) { |
| 165 |
$excluded_pages = array_map('trim', explode(',', $options['options']['exclude_pages'])); |
| 166 |
if (is_page() && in_array(get_the_ID(), $excluded_pages)) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// 检查排除的文章 |
| 172 |
if (isset($options['options']['exclude_posts']) && !empty($options['options']['exclude_posts'])) { |
| 173 |
$excluded_posts = array_map('trim', explode(',', $options['options']['exclude_posts'])); |
| 174 |
if (is_single() && in_array(get_the_ID(), $excluded_posts)) { |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// 检查排除的分类 |
| 180 |
if (isset($options['options']['exclude_categories']) && !empty($options['options']['exclude_categories'])) { |
| 181 |
$excluded_categories = array_filter(array_map('intval', array_map('trim', explode(',', $options['options']['exclude_categories'])))); |
| 182 |
|
| 183 |
// 分类归档页排除 |
| 184 |
if (is_category()) { |
| 185 |
$current_category_id = (int) get_queried_object_id(); |
| 186 |
if (in_array($current_category_id, $excluded_categories, true)) { |
| 187 |
return true; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// 文章属于指定分类时排除 |
| 192 |
if (is_single() && has_category($excluded_categories)) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* 根据当前访问� |
| 202 |
(游客 / 已登录)解析应显示的水印文案。 |
| 203 |
* |
| 204 |
* @param array $opts 已合并默认值的 options['options']。 |
| 205 |
* @return string 非空则显示水印。 |
| 206 |
*/ |
| 207 |
private function resolve_watermark_display_text(array $opts) { |
| 208 |
if (!is_user_logged_in()) { |
| 209 |
if (empty($opts['enable_watermark'])) { |
| 210 |
return ''; |
| 211 |
} |
| 212 |
$text = isset($opts['watermark_text']) ? trim((string) $opts['watermark_text']) : ''; |
| 213 |
return $text; |
| 214 |
} |
| 215 |
|
| 216 |
if (empty($opts['enable_watermark_member'])) { |
| 217 |
return ''; |
| 218 |
} |
| 219 |
|
| 220 |
$mode = isset($opts['watermark_member_mode']) ? $opts['watermark_member_mode'] : 'nickname'; |
| 221 |
if (!in_array($mode, array('nickname', 'username', 'custom'), true)) { |
| 222 |
$mode = 'nickname'; |
| 223 |
} |
| 224 |
|
| 225 |
$user = wp_get_current_user(); |
| 226 |
if (!$user || !$user->exists()) { |
| 227 |
return ''; |
| 228 |
} |
| 229 |
|
| 230 |
if ($mode === 'username') { |
| 231 |
return '@' . $user->user_login; |
| 232 |
} |
| 233 |
|
| 234 |
if ($mode === 'nickname') { |
| 235 |
$nick = get_user_meta($user->ID, 'nickname', true); |
| 236 |
if ($nick === '' || $nick === null) { |
| 237 |
$nick = $user->display_name; |
| 238 |
} |
| 239 |
if ($nick === '' || $nick === null) { |
| 240 |
$nick = $user->user_login; |
| 241 |
} |
| 242 |
return '@' . $nick; |
| 243 |
} |
| 244 |
|
| 245 |
return isset($opts['watermark_member_custom']) ? trim((string) $opts['watermark_member_custom']) : ''; |
| 246 |
} |
| 247 |
|
| 248 |
public function getOptions() { |
| 249 |
if (null === $this->options) { |
| 250 |
$this->options = get_option('wp_copy_rights_options'); |
| 251 |
|
| 252 |
// 确保options数组结构完整,� |
| 253 |
�容PHP 8+ |
| 254 |
if (!is_array($this->options)) { |
| 255 |
$this->options = array(); |
| 256 |
} |
| 257 |
if (!isset($this->options['options']) || !is_array($this->options['options'])) { |
| 258 |
$this->options['options'] = array(); |
| 259 |
} |
| 260 |
|
| 261 |
$default_options = array( |
| 262 |
'disable_right_click' => false, |
| 263 |
'disable_select_text' => false, |
| 264 |
'disable_drag_image' => false, |
| 265 |
'disable_f12' => false, |
| 266 |
'disable_ctrl_shift_i' => false, |
| 267 |
'disable_ctrl_shift_j' => false, |
| 268 |
'disable_print' => false, |
| 269 |
'disable_view_source' => false, |
| 270 |
'disable_save_page' => false, |
| 271 |
'disable_select_all' => false, |
| 272 |
'disable_copy_content' => false, |
| 273 |
'disable_cut_content' => false, |
| 274 |
'enable_adminer' => false, |
| 275 |
'enable_loginer' => false, |
| 276 |
'enable_devtools_open_notice' => false, |
| 277 |
'enable_endcopyrights' => false, |
| 278 |
'enable_watermark' => false, |
| 279 |
'enable_watermark_member' => false, |
| 280 |
'watermark_member_mode' => 'nickname', |
| 281 |
'watermark_member_custom' => '', |
| 282 |
'custom_message' => '', |
| 283 |
'watermark_text' => '', |
| 284 |
'watermark_font_size' => 24, |
| 285 |
'watermark_color' => '#000000', |
| 286 |
'watermark_angle' => -30, |
| 287 |
'watermark_opacity' => 0.10, |
| 288 |
'watermark_gap_x' => 220, |
| 289 |
'watermark_gap_y' => 140, |
| 290 |
'exclude_pages' => '', |
| 291 |
'exclude_posts' => '', |
| 292 |
'exclude_categories' => '', |
| 293 |
); |
| 294 |
$this->options['options'] = wp_parse_args($this->options['options'], $default_options); |
| 295 |
if (!isset($this->options['switch'])) { |
| 296 |
$this->options['switch'] = false; |
| 297 |
} |
| 298 |
} |
| 299 |
return $this->options; |
| 300 |
} |
| 301 |
|
| 302 |
public function addSettingPage() { |
| 303 |
add_submenu_page( |
| 304 |
'tools.php', |
| 305 |
'WPCopyRights设置', |
| 306 |
'WPCopyRights设置', |
| 307 |
'manage_options', |
| 308 |
'wp-copy-rights', |
| 309 |
array($this, 'renderSettingPage') |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
public function renderSettingPage() { |
| 314 |
if (!current_user_can('manage_options')) { |
| 315 |
wp_die(__('您没有足够的权限访问此页面。')); |
| 316 |
} |
| 317 |
|
| 318 |
require_once WP_COPY_RIGHTS_PATH . 'templates/admin-settings.php'; |
| 319 |
} |
| 320 |
|
| 321 |
public function addPluginLinks($links, $file) { |
| 322 |
if (plugin_basename(__FILE__) === $file) { |
| 323 |
$settings_link = '<a href="' . admin_url('tools.php?page=wp-copy-rights') . '">设置</a>'; |
| 324 |
array_unshift($links, $settings_link); |
| 325 |
} |
| 326 |
return $links; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// 初始化插件 |
| 331 |
function wp_copy_rights_init() { |
| 332 |
return WPCopyRights::getInstance(); |
| 333 |
} |
| 334 |
|
| 335 |
wp_copy_rights_init(); |