| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: CryptX |
| 4 |
* Plugin URI: https://wordpress.org/plugins/cryptx/ |
| 5 |
* Description: CryptX encrypts email addresses in your posts, pages, comments, and text widgets to protect them from spam bots while keeping them readable for your visitors. |
| 6 |
* Version: 4.0.10 |
| 7 |
* Requires at least: 6.7 |
| 8 |
* Tested up to: 6.9 |
| 9 |
* Requires PHP: 8.3 |
| 10 |
* Author: Ralf Weber |
| 11 |
* Author URI: https://weber-nrw.de/ |
| 12 |
* License: GPL v2 or later |
| 13 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 14 |
* Text Domain: cryptx |
| 15 |
* |
| 16 |
* CryptX is free software: you can redistribute it and/or modify |
| 17 |
* it under the terms of the GNU General Public License as published by |
| 18 |
* the Free Software Foundation, either version 2 of the License, or |
| 19 |
* any later version. |
| 20 |
* |
| 21 |
* CryptX is distributed in the hope that it will be useful, |
| 22 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
* GNU General Public License for more details. |
| 25 |
* |
| 26 |
* You should have received a copy of the GNU General Public License |
| 27 |
* along with CryptX. If not, see https://www.gnu.org/licenses/gpl-2.0.html. |
| 28 |
* |
| 29 |
* @package CryptX |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
|
| 33 |
// Prevent direct access |
| 34 |
if (!defined('ABSPATH')) { |
| 35 |
exit; |
| 36 |
} |
| 37 |
|
| 38 |
// Plugin constants |
| 39 |
define('CRYPTX_VERSION', '4.0.10'); |
| 40 |
define('CRYPTX_PLUGIN_FILE', __FILE__); |
| 41 |
define('CRYPTX_PLUGIN_BASENAME', plugin_basename(__FILE__)); |
| 42 |
define('CRYPTX_BASENAME', plugin_basename(__FILE__)); // Add this missing constant |
| 43 |
define('CRYPTX_DIR_PATH', plugin_dir_path(__FILE__)); |
| 44 |
define('CRYPTX_DIR_URL', plugin_dir_url(__FILE__)); |
| 45 |
define('CRYPTX_BASEFOLDER', dirname(CRYPTX_PLUGIN_BASENAME)); |
| 46 |
|
| 47 |
// Minimum requirements check |
| 48 |
if (version_compare(PHP_VERSION, '8.1', '<')) { |
| 49 |
add_action('admin_notices', function() { |
| 50 |
echo '<div class="notice notice-error"><p>'; |
| 51 |
printf( |
| 52 |
/* translators: %1$s: Required PHP version, %2$s: Current PHP version */ |
| 53 |
esc_html__('CryptX requires PHP version %1$s or higher. You are running version %2$s. Please update PHP.', 'cryptx'), |
| 54 |
'8.1.0', |
| 55 |
PHP_VERSION |
| 56 |
); |
| 57 |
echo '</p></div>'; |
| 58 |
}); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// WordPress version check |
| 63 |
global $wp_version; |
| 64 |
if (version_compare($wp_version, '6.7', '<')) { |
| 65 |
add_action('admin_notices', function() { |
| 66 |
echo '<div class="notice notice-error"><p>'; |
| 67 |
printf( |
| 68 |
/* translators: %1$s: Required WordPress version, %2$s: Current WordPress version */ |
| 69 |
esc_html__('CryptX requires WordPress version %1$s or higher. You are running version %2$s. Please update WordPress.', 'cryptx'), |
| 70 |
'5.0', |
| 71 |
esc_html($GLOBALS['wp_version']) |
| 72 |
); |
| 73 |
echo '</p></div>'; |
| 74 |
}); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Autoloader for plugin classes |
| 79 |
spl_autoload_register(function ($class) { |
| 80 |
// Check if the class belongs to our namespace |
| 81 |
if (strpos($class, 'CryptX\\') !== 0) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Remove namespace prefix |
| 86 |
$class = substr($class, 7); |
| 87 |
|
| 88 |
// Convert namespace separators to directory separators |
| 89 |
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class); |
| 90 |
|
| 91 |
// Build the full path |
| 92 |
$file = CRYPTX_DIR_PATH . 'classes' . DIRECTORY_SEPARATOR . $class . '.php'; |
| 93 |
|
| 94 |
// Include the file if it exists |
| 95 |
if (file_exists($file)) { |
| 96 |
require_once $file; |
| 97 |
} |
| 98 |
}); |
| 99 |
|
| 100 |
function cryptx_nonce_check() { |
| 101 |
if ( isset($_POST['cryptX_var']) && isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'cryptX' ) ) { |
| 102 |
wp_die( esc_html__( 'Security check failed. Please try again.', 'cryptx' ), esc_html__( 'Error', 'cryptx' ) ); |
| 103 |
} |
| 104 |
} |
| 105 |
add_action( 'admin_init', 'cryptx_nonce_check' ); |
| 106 |
|
| 107 |
// Initialize the plugin |
| 108 |
add_action('plugins_loaded', function() { |
| 109 |
// Check if all required classes can be loaded |
| 110 |
$requiredClasses = [ |
| 111 |
'CryptX\\CryptX', |
| 112 |
'CryptX\\Config', |
| 113 |
'CryptX\\CryptXSettingsTabs', |
| 114 |
'CryptX\\SecureEncryption', |
| 115 |
'CryptX\\Admin\\ChangelogSettingsTab', |
| 116 |
'CryptX\\Admin\\GeneralSettingsTab', |
| 117 |
'CryptX\\Admin\\PresentationSettingsTab', |
| 118 |
'CryptX\\Util\\DataSanitizer', |
| 119 |
]; |
| 120 |
|
| 121 |
$missingClasses = []; |
| 122 |
foreach ($requiredClasses as $class) { |
| 123 |
if (!class_exists($class)) { |
| 124 |
$missingClasses[] = $class; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (!empty($missingClasses)) { |
| 129 |
add_action('admin_notices', function() use ($missingClasses) { |
| 130 |
echo '<div class="notice notice-error"><p>'; |
| 131 |
echo esc_html__('CryptX: Missing required classes: ', 'cryptx') . esc_html( implode(', ', $missingClasses), 'cryptx'); |
| 132 |
echo '</p></div>'; |
| 133 |
}); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// Initialize the main plugin class |
| 138 |
try { |
| 139 |
$cryptx_instance = CryptX\CryptX::get_instance(); |
| 140 |
$cryptx_instance->startCryptX(); |
| 141 |
} catch (Exception $e) { |
| 142 |
add_action('admin_notices', function() use ($e) { |
| 143 |
echo '<div class="notice notice-error"><p>'; |
| 144 |
echo esc_html__('CryptX initialization failed: ', 'cryptx') . esc_html($e->getMessage()); |
| 145 |
echo '</p></div>'; |
| 146 |
}); |
| 147 |
} |
| 148 |
}); |
| 149 |
|
| 150 |
// Remove the strict activation requirements - let the plugin handle fallbacks |
| 151 |
register_activation_hook(__FILE__, function() { |
| 152 |
// Just flush rewrite rules |
| 153 |
flush_rewrite_rules(); |
| 154 |
}); |
| 155 |
|
| 156 |
// Plugin deactivation hook |
| 157 |
register_deactivation_hook(__FILE__, function() { |
| 158 |
// Clean up any transients or cached data |
| 159 |
global $wpdb; |
| 160 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_cryptx_%'"); |
| 161 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_cryptx_%'"); |
| 162 |
}); |
| 163 |
|
| 164 |
// Add plugin action links - updated to match the settings page slug |
| 165 |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), function($links) { |
| 166 |
$settings_link = '<a href="' . admin_url('options-general.php?page=cryptx') . '">' . |
| 167 |
esc_html__('Settings', 'cryptx') . '</a>'; |
| 168 |
array_unshift($links, $settings_link); |
| 169 |
return $links; |
| 170 |
}); |
| 171 |
|
| 172 |
/** |
| 173 |
* Encrypts the given content using the CryptX library and wraps it with a shortcode. |
| 174 |
* |
| 175 |
* @param string $content The content to be encrypted. |
| 176 |
* @param array|null $args Optional arguments to customize the encryption process. |
| 177 |
* |
| 178 |
* @return string The encrypted content wrapped in the appropriate shortcode. |
| 179 |
*/ |
| 180 |
if (!function_exists('cryptx_encrypt')) { |
| 181 |
function cryptx_encrypt(string $content, ?array $args = []): string |
| 182 |
{ |
| 183 |
$cryptXInstance = Cryptx\CryptX::get_instance(); |
| 184 |
// $attributesString contains the escaped (esc_attr()) shortcode attributes from $args |
| 185 |
$attributesString = $cryptXInstance->convertArrayToArgumentString($args); |
| 186 |
$shortcode = '[cryptx' . $attributesString . ']' . esc_html($content) . '[/cryptx]'; |
| 187 |
|
| 188 |
return do_shortcode($shortcode); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Encrypts the given content using the CryptX library and wraps it with a shortcode. |
| 194 |
* |
| 195 |
* @deprecated 4.0.5 Use cryptx_encrypt() instead. |
| 196 |
* @see cryptx_encrypt() |
| 197 |
*/ |
| 198 |
if (!function_exists('encryptx')) { |
| 199 |
function encryptx(string $content, ?array $args = []): string |
| 200 |
{ |
| 201 |
__doing_it_wrong( 'badly_named', 'This method has been deprecated in favor of better_named_function "cryptx_encrypt"', '6.9' ); |
| 202 |
|
| 203 |
return cryptx_encrypt($content, $args); |
| 204 |
} |
| 205 |
} |