WP Email Encoder Bundle


/ Published in: PHP
Save to your folder(s)

Encode mailto links and (plain) email addresses on your site and hide them from spambots. Easy to use, plugin works directly when activated.

Features
* Protect all emails and mailto links
* Check posts, widgets, comments and RSS feeds
* Encode all kind of content (text and html)
* Email Encoder Form (in admin and also for your site)
* Use params in mailto links, like: cc, bcc, subject, body
* And more.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Plugin Name: Email Encoder Bundle
  4. Plugin URI: http://www.freelancephp.net/email-encoder-php-class-wp-plugin/
  5. Description: Protect email addresses on your site and hide them from spambots by using an encoding method. Easy to use, flexible .
  6. Author: Victor Villaverde Laan
  7. Version: 0.80
  8. Author URI: http://www.freelancephp.net
  9. License: Dual licensed under the MIT and GPL licenses
  10. */
  11.  
  12. /**
  13.  * Class WP_Email_Encoder_Bundle_Admin
  14.  * @package WP_Email_Encoder_Bundle
  15.  * @category WordPress Plugins
  16.  */
  17. if (!class_exists('WP_Email_Encoder_Bundle_Admin')):
  18.  
  19. class WP_Email_Encoder_Bundle_Admin {
  20.  
  21. /**
  22. * Current version
  23. * @var string
  24. */
  25. var $version = '0.80';
  26.  
  27. /**
  28. * Used as prefix for options entry and could be used as text domain (for translations)
  29. * @var string
  30. */
  31. var $domain = 'WP_Email_Encoder_Bundle';
  32.  
  33. /**
  34. * Name of the options
  35. * @var string
  36. */
  37. var $options_name = 'WP_Email_Encoder_Bundle_options';
  38.  
  39. /**
  40. * @var boolead
  41. */
  42. var $is_admin_user = FALSE;
  43.  
  44. /**
  45. * @var array
  46. */
  47. var $options = array(
  48. 'method' => 'enc_ascii',
  49. 'encode_mailtos' => 1,
  50. 'encode_emails' => 0,
  51. 'skip_posts' => '',
  52. 'class_name' => 'mailto-link',
  53. 'filter_posts' => 1,
  54. 'filter_widgets' => 1,
  55. 'filter_comments' => 1,
  56. 'filter_rss' => 1,
  57. 'protection_text' => '*protected email*',
  58. 'widget_logic_filter' => 0,
  59. 'show_encoded_check' => 0,
  60. 'own_admin_menu' => 1,
  61. 'powered_by' => 1,
  62. );
  63.  
  64. /**
  65. * @var array
  66. */
  67. var $skip_posts = array();
  68.  
  69. /**
  70. * @var string
  71. */
  72. var $method = 'enc_ascii';
  73.  
  74. /**
  75. * @var array
  76. */
  77. var $methods = array();
  78.  
  79. /**
  80. * PHP5 constructor
  81. */
  82. function __construct() {
  83. // load text domain for translations
  84. load_plugin_textdomain($this->domain, FALSE, dirname(plugin_basename(__FILE__)) . '/lang/');
  85.  
  86. // set methods
  87. $this->methods = array(
  88. 'enc_ascii' => array(
  89. 'name' => __('JavaScript ASCII (recommended)', $this->domain),
  90. 'description' => __('This encoding method uses javascript (<a href="http://rumkin.com/tools/mailto_encoder/" target="_blank">original source</a>). <br />Recommended, the savest method.', $this->domain),
  91. ),
  92. 'enc_escape' => array(
  93. 'name' => __('JavaScript Escape', $this->domain),
  94. 'description' => __('This encoding method uses the javascript eval() function (<a href="http://blueberryware.net/2008/09/14/email-spam-protection/" target="_blank">original source</a>). <br />Pretty save method.', $this->domain),
  95. ),
  96. 'enc_html' => array(
  97. 'name' => __('Html Encode', $this->domain),
  98. 'description' => __('This encoding method uses the antispambot() function, built-in WordPress (<a href="http://codex.wordpress.org/Function_Reference/antispambot" target="_blank">more info</a>). <br />Not recommended, especially when using the shortcode [encode_content]).', $this->domain),
  99. ),
  100. 'random' => array(
  101. 'name' => __('Random', $this->domain),
  102. 'description' => __('Pick each time a random encoding method. <br />Not recommended, especially when using the shortcode [encode_content]).', $this->domain),
  103. ),
  104. );
  105.  
  106. // set option values
  107. $this->set_options();
  108.  
  109. // prepare vars
  110. $skip_posts = $this->options['skip_posts'];
  111. $skip_posts = str_replace(' ', '', $skip_posts);
  112. $skip_posts = explode(',', $skip_posts);
  113. $this->skip_posts = $skip_posts;
  114.  
  115. // set uninstall hook
  116. if (function_exists('register_deactivation_hook')) {
  117. register_deactivation_hook(__FILE__, array($this, 'deactivation'));
  118. }
  119.  
  120. // add actions
  121. add_action('wp', array($this, 'wp'));
  122. add_action('admin_init', array($this, 'admin_init'));
  123. add_action('admin_menu', array($this, 'admin_menu'));
  124. }
  125.  
  126. /**
  127. * Set options from save values or defaults
  128. */
  129. function set_options() {
  130. // set options
  131. $saved_options = get_option($this->options_name);
  132.  
  133. // backwards compatible (old values)
  134. if (empty($saved_options)) {
  135. $saved_options = get_option($this->domain . 'options');
  136. }
  137.  
  138. // set all options
  139. if (!empty($saved_options)) {
  140. foreach ($saved_options AS $key => $value) {
  141. $this->options[$key] = $value;
  142. }
  143. }
  144.  
  145. // set encode method
  146. $this->method = $this->get_method($this->options['method']);
  147.  
  148. // set widget_content filter of Widget Logic plugin
  149. $widget_logic_opts = get_option('widget_logic');
  150. if (is_array($widget_logic_opts) AND key_exists('widget_logic-options-filter', $widget_logic_opts)) {
  151. $this->options['widget_logic_filter'] = ($widget_logic_opts['widget_logic-options-filter'] == 'checked') ? 1 : 0;
  152. }
  153. }
  154.  
  155. /**
  156. * Get method name
  157. * @param string $method
  158. * @param string $defaultMethod Optional, default 'enc_html'
  159. * @return string
  160. */
  161. function get_method($method, $defaultMethod = 'enc_html') {
  162. $method = strtolower($method);
  163.  
  164. if ('random' == $method) {
  165. // set a random method
  166. $method = array_rand($this->methods);
  167.  
  168. if ('random' == $method) {
  169. $method = $this->get_method($method, $defaultMethod);
  170. }
  171. } else {
  172. if (!method_exists($this, $method)) {
  173. $method = $defaultMethod; // set default method
  174. }
  175. }
  176.  
  177. return $method;
  178. }
  179.  
  180. /**
  181. * Deactivation plugin method
  182. */
  183. function deactivation() {
  184. delete_option($this->options_name);
  185. unregister_setting($this->domain, $this->options_name);
  186. }
  187.  
  188. /**
  189. * wp action
  190. */
  191. function wp() {
  192. // check admin
  193. $this->is_admin_user = current_user_can('manage_options');
  194. }
  195.  
  196. /**
  197. * admin_init action
  198. */
  199. function admin_init() {
  200. // register settings
  201. register_setting($this->domain, $this->options_name);
  202.  
  203. // actions
  204. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
  205. }
  206.  
  207. /**
  208. * admin_enqueue_scripts action
  209. * @param string $hook_suffix
  210. */
  211. function admin_enqueue_scripts($hook_suffix) {
  212. global $wp_version;
  213.  
  214. if ($hook_suffix == 'settings_page_email-encoder-bundle/email-encoder-bundle' || $hook_suffix == 'toplevel_page_email-encoder-bundle/email-encoder-bundle') {
  215. // set dashboard postbox
  216. wp_enqueue_script('dashboard');
  217.  
  218. // set dashboard style for wp < 3.2.0
  219. if (version_compare(preg_replace('/-.*$/', '', $wp_version), '3.2.0', '<')) {
  220. wp_admin_css('dashboard');
  221. }
  222.  
  223. // add style and script for ajax encoder
  224. wp_enqueue_script('email_encoder', plugins_url('js/email-encoder-bundle.js', __FILE__), array('jquery'), $this->version);
  225. wp_enqueue_script('email_encoder_admin', plugins_url('js/email-encoder-bundle-admin.js', __FILE__), array('jquery'), $this->version);
  226. }
  227. }
  228.  
  229. /**
  230. * admin_menu action
  231. */
  232. function admin_menu() {
  233. if ($this->is_admin_user) {
  234. return;
  235. }
  236.  
  237. // add page and menu item
  238. if ($this->options['own_admin_menu']) {
  239. // create main menu item
  240. $page_hook = add_menu_page(__('Email Encoder Bundle', $this->domain), __('Email Encoder Bundle', $this->domain),
  241. 'manage_options', __FILE__, array($this, 'show_options_page'),
  242. plugins_url('images/icon-email-encoder-bundle-16.png', __FILE__));
  243. } else {
  244. // create submenu item under "Settings"
  245. $page_hook = add_options_page(__('Email Encoder Bundle', $this->domain), __('Email Encoder Bundle', $this->domain),
  246. 'manage_options', __FILE__, array($this, 'show_options_page'));
  247. }
  248.  
  249. // load plugin page
  250. add_action('load-' . $page_hook, array($this, 'load_options_page'));
  251. }
  252.  
  253. /* -------------------------------------------------------------------------
  254. * Admin Options Page
  255. * ------------------------------------------------------------------------*/
  256.  
  257. /**
  258. * Load admin options page
  259. */
  260. function load_options_page() {
  261. // add help tabs
  262. $this->add_help_tabs();
  263.  
  264. // screen settings
  265. if (function_exists('add_screen_option')) {
  266. add_screen_option('layout_columns', array(
  267. 'max' => 2,
  268. 'default' => 2
  269. ));
  270. }
  271.  
  272. // add meta boxes
  273. add_meta_box('general_settings', __('General Settings'), array($this, 'show_meta_box_content'), null, 'normal', 'core', array('general_settings'));
  274. add_meta_box('admin_settings', __('Admin Settings'), array($this, 'show_meta_box_content'), null, 'normal', 'core', array('admin_settings'));
  275. add_meta_box('encode_form', __('Email Encoder Form'), array($this, 'show_meta_box_content'), null, 'normal', 'core', array('encode_form'));
  276. add_meta_box('other_plugins', __('Other Plugins'), array($this, 'show_meta_box_content'), null, 'side', 'core', array('other_plugins'));
  277. }
  278.  
  279. /**
  280. * Show admin options page
  281. */
  282. function show_options_page() {
  283. $this->set_options();
  284. ?>
  285. <div class="wrap">
  286. <div class="icon32" id="icon-options-custom" style="background:url(<?php echo plugins_url('images/icon-email-encoder-bundle.png', __FILE__) ?>) no-repeat 50% 50%"><br></div>
  287. <h2><?php echo get_admin_page_title() ?> - <em><small><?php _e('Protecting Email Addresses', $this->domain) ?></small></em></h2>
  288.  
  289. <?php if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true'): ?>
  290. <div class="updated settings-error" id="setting-error-settings_updated">
  291. <p><strong><?php _e('Settings saved.' ) ?></strong></p>
  292. </div>
  293. <?php endif; ?>
  294.  
  295. <form method="post" action="options.php">
  296. <?php settings_fields($this->domain); ?>
  297.  
  298. <input type="hidden" name="<?php echo $this->domain ?>_nonce" value="<?php echo wp_create_nonce($this->domain) ?>" />
  299. <?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false); ?>
  300. <?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false); ?>
  301.  
  302. <div id="poststuff">
  303. <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
  304. <!--<div id="post-body-content"></div>-->
  305.  
  306. <div id="postbox-container-1" class="postbox-container">
  307. <?php do_meta_boxes('', 'side', ''); ?>
  308. </div>
  309.  
  310. <div id="postbox-container-2" class="postbox-container">
  311. <?php do_meta_boxes('', 'normal', ''); ?>
  312. <?php do_meta_boxes('', 'advanced', ''); ?>
  313. </div>
  314. </div> <!-- #post-body -->
  315. </div> <!-- #poststuff -->
  316. </form>
  317. <script type="text/javascript">
  318. var methodInfo = <?php echo json_encode($this->methods) ?>;
  319. </script>
  320. </div>
  321. <?php
  322. }
  323.  
  324. /**
  325. * Show content of metabox (callback)
  326. * @param array $post
  327. * @param array $meta_box
  328. */
  329. function show_meta_box_content($post, $meta_box) {
  330. $key = $meta_box['args'][0];
  331. $options = $this->options;
  332.  
  333. if ($key === 'general_settings') {
  334. ?>
  335. <?php if (is_plugin_active('wp-mailto-links/wp-mailto-links.php')): ?>
  336. <p class="description"><?php _e('Warning: "WP Mailto Links"-plugin is also activated, which could cause conflicts.', $this->domain) ?></p>
  337. <?php endif; ?>
  338. <fieldset class="options">
  339. <table class="form-table">
  340. <tr>
  341. <th><?php _e('Encoding Method for Protection', $this->domain) ?></th>
  342. <td><select id="<?php echo $this->options_name ?>[method]" name="<?php echo $this->options_name ?>[method]" class="method-info-select postform">
  343. <?php foreach ($this->methods AS $method => $info): ?>
  344. <option value="<?php echo $method ?>" <?php if ($this->method == $method) echo 'selected="selected"' ?>><?php echo $info['name']; if ($method == 'lim_email_ascii'){ echo ' (recommended)'; } ?></option>
  345. <?php endforeach; ?>
  346. </select>
  347. <br />
  348. <label><span class="description"></span></label>
  349. </td>
  350. </tr>
  351. <tr>
  352. <th><?php _e('Auto-Protect Emails', $this->domain) ?></th>
  353. <td>
  354. <label><input type="checkbox" id="encode_mailtos" name="<?php echo $this->options_name ?>[encode_mailtos]" value="1" <?php checked('1', (int) $options['encode_mailtos']); ?> />
  355. <span><?php _e('Protect mailto links', $this->domain) ?></span> <span class="description"><?php _e('(example: &lt;a href="[email protected]"&gt;My Email&lt;/a&gt;)', $this->domain) ?></span>
  356. </label>
  357. <br/><label><input type="checkbox" id="encode_emails" name="<?php echo $this->options_name ?>[encode_emails]" value="1" <?php checked('1', (int) $options['encode_emails']); ?> />
  358. <span><?php _e('Replace plain email addresses to protected mailto links', $this->domain) ?></span> <span class="description"><?php _e('(not recommended)', $this->domain) ?></span>
  359. </label>
  360. <br/>
  361. <br/>
  362. Apply on:
  363. <br/>
  364. <label><input type="checkbox" name="<?php echo $this->options_name ?>[filter_posts]" value="1" <?php checked('1', (int) $options['filter_posts']); ?> />
  365. <span><?php _e('All posts', $this->domain) ?></span>
  366. </label>
  367. <br/><label><input type="checkbox" id="<?php echo $this->options_name ?>[filter_comments]" name="<?php echo $this->options_name ?>[filter_comments]" value="1" <?php checked('1', (int) $options['filter_comments']); ?> />
  368. <span><?php _e('All comments', $this->domain) ?></span></label>
  369. <br/><label><input type="checkbox" id="<?php echo $this->options_name ?>[filter_widgets]" name="<?php echo $this->options_name ?>[filter_widgets]" value="1" <?php checked('1', (int) $options['filter_widgets']); ?> />
  370. <span><?php if ($this->options['widget_logic_filter']) { _e('All widgets (uses the <code>widget_content</code> filter of the Widget Logic plugin)', $this->domain); } else { _e('All text widgets', $this->domain); } ?></span></label>
  371. <br/>
  372. <br/>
  373. <label>
  374. <span><?php _e('Do <strong>not</strong> apply Auto-Protect on posts with ID:', $this->domain) ?></span>
  375. <br/><input type="text" id="<?php echo $this->options_name ?>[skip_posts]" name="<?php echo $this->options_name ?>[skip_posts]" value="<?php echo $options['skip_posts']; ?>" />
  376. <span class="description"><?php _e('(comma seperated, f.e.: 2, 7, 13, 32)', $this->domain) ?></span>
  377. <br/><span class="description"><?php _e('Notice: shortcodes will still work on these posts.', $this->domain) ?></span>
  378. </label>
  379. </td>
  380. </tr>
  381. <tr>
  382. <th><?php _e('Class for Protected Links', $this->domain) ?></th>
  383. <td><label><input type="text" id="<?php echo $this->options_name ?>[class_name]" name="<?php echo $this->options_name ?>[class_name]" value="<?php echo $options['class_name']; ?>" />
  384. <span class="description"><?php _e('All protected mailto links will get these class(es) <em>(optional, else keep blank)</em>', $this->domain) ?></span></label></td>
  385. </tr>
  386. <tr>
  387. <th><?php _e('Protect Emails in RSS Feeds', $this->domain) ?></th>
  388. <td><label><input type="checkbox" id="filter_rss" name="<?php echo $this->options_name ?>[filter_rss]" value="1" <?php checked('1', (int) $options['filter_rss']); ?> />
  389. <span><?php _e('Replace emails in RSS feeds with the following text:', $this->domain) ?></span></label>
  390. <label><input type="text" id="protection_text" name="<?php echo $this->options_name ?>[protection_text]" value="<?php echo $options['protection_text']; ?>" />
  391. </td>
  392. </tr>
  393. </table>
  394. </fieldset>
  395. <p class="submit">
  396. <input class="button-primary" type="submit" disabled="disabled" value="<?php _e('Save Changes') ?>" />
  397. </p>
  398. <br class="clear" />
  399. <?php
  400. } else if ($key === 'admin_settings') {
  401. ?>
  402. <fieldset class="options">
  403. <table class="form-table">
  404. <tr>
  405. <th><?php _e('Check encoded content', $this->domain) ?></th>
  406. <td><label><input type="checkbox" id="<?php echo $this->options_name ?>[show_encoded_check]" name="<?php echo $this->options_name ?>[show_encoded_check]" value="1" <?php checked('1', (int) $options['show_encoded_check']); ?> /> <span><?php _e('Show "successfully encoded" text for all encoded content, only when logged in as admin user', $this->domain) ?></span> <br /><span class="description">(this way you can check if emails are really encoded on your site)</span></label></td>
  407. </tr>
  408. <tr>
  409. <th><?php _e('Admin menu position', $this->domain) ?></th>
  410. <td><label><input type="checkbox" id="<?php echo $this->options_name ?>[own_admin_menu]" name="<?php echo $this->options_name ?>[own_admin_menu]" value="1" <?php checked('1', (int) $options['own_admin_menu']); ?> /> <span><?php _e('Show as main menu item', $this->domain) ?></span> <span class="description">(when disabled item will be shown under "General settings")</span></label></td>
  411. </tr>
  412. <tr>
  413. <th><?php _e('Email Encoder Form Settings', $this->domain) ?></th>
  414. <td><label><input type="checkbox" id="<?php echo $this->options_name ?>[powered_by]" name="<?php echo $this->options_name ?>[powered_by]" value="1" <?php checked('1', (int) $options['powered_by']); ?> /> <span><?php _e('Show the "powered by"-link on bottom of the encoder form', $this->domain) ?></span></label></td>
  415. </tr>
  416. </table>
  417. </fieldset>
  418. <p class="submit">
  419. <input class="button-primary" type="submit" disabled="disabled" value="<?php _e('Save Changes') ?>" />
  420. </p>
  421. <br class="clear" />
  422. <?php
  423. } else if ($key === 'encode_form') {
  424. echo $this->get_encoder_form();
  425. } else if ($key === 'other_plugins') {
  426. ?>
  427. <h4><img src="<?php echo plugins_url('images/icon-wp-external-links.png', __FILE__) ?>" width="16" height="16" /> WP External Links -
  428. <?php if (is_plugin_active('wp-external-links/wp-external-links.php')): ?>
  429. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/options-general.php?page=wp-external-links/wp-external-links.php"><?php _e('Settings') ?></a>
  430. <?php elseif( file_exists( WP_PLUGIN_DIR . '/wp-external-links/wp-external-links.php')): ?>
  431. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/plugins.php?plugin_status=inactive"><?php _e('Activate', $this->domain) ?></a>
  432. <?php else: ?>
  433. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/plugin-install.php?tab=search&type=term&s=WP+External+Links+freelancephp&plugin-search-input=Search+Plugins"><?php _e('Get this plugin', $this->domain) ?></a>
  434. <?php endif; ?>
  435. </h4>
  436. <p><?php _e('Manage external links on your site: open in new window/tab, set icon, add "external", add "nofollow" and more.', $this->domain) ?>
  437. <br /><a href="http://wordpress.org/extend/plugins/wp-external-links/" target="_blank">WordPress.org</a> | <a href="http://www.freelancephp.net/wp-external-links-plugin/" target="_blank">FreelancePHP.net</a>
  438. </p>
  439.  
  440. <h4><img src="<?php echo plugins_url('images/icon-wp-mailto-links.png', __FILE__) ?>" width="16" height="16" /> WP Mailto Links -
  441. <?php if (is_plugin_active('wp-mailto-links/wp-mailto-links.php')): ?>
  442. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/options-general.php?page=wp-mailto-links/wp-mailto-links.php"><?php _e('Settings') ?></a>
  443. <?php elseif( file_exists( WP_PLUGIN_DIR . '/wp-mailto-links/wp-mailto-links.php')): ?>
  444. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/plugins.php?plugin_status=inactive"><?php _e('Activate', $this->domain) ?></a>
  445. <?php else: ?>
  446. <a href="<?php echo get_bloginfo('url') ?>/wp-admin/plugin-install.php?tab=search&type=term&s=WP+Mailto+Links+freelancephp&plugin-search-input=Search+Plugins"><?php _e('Get this plugin', $this->domain) ?></a>
  447. <?php endif; ?>
  448. </h4>
  449. <p><?php _e('Manage mailto links on your site and protect emails from spambots, set mail icon and more.', $this->domain) ?>
  450. <br /><a href="http://wordpress.org/extend/plugins/wp-mailto-links/" target="_blank">WordPress.org</a> | <a href="http://www.freelancephp.net/wp-mailto-links-plugin/" target="_blank">FreelancePHP.net</a>
  451. </p>
  452. <?php
  453. }
  454. }
  455.  
  456. /* -------------------------------------------------------------------------
  457. * Help Tabs
  458. * ------------------------------------------------------------------------*/
  459.  
  460. /**
  461. * Add help tabs
  462. */
  463. function add_help_tabs() {
  464. if (!function_exists('get_current_screen')) {
  465. return;
  466. }
  467.  
  468. $screen = get_current_screen();
  469.  
  470. $screen->set_help_sidebar($this->get_help_text('sidebar'));
  471.  
  472. $screen->add_help_tab(array(
  473. 'id' => 'about',
  474. 'title' => __('About'),
  475. 'content' => $this->get_help_text('about'),
  476. ));
  477. $screen->add_help_tab(array(
  478. 'id' => 'shortcodes',
  479. 'title' => __('Shortcodes'),
  480. 'content' => $this->get_help_text('shortcodes'),
  481. ));
  482. $screen->add_help_tab(array(
  483. 'id' => 'templatefunctions',
  484. 'title' => __('Template functions'),
  485. 'content' => $this->get_help_text('templatefunctions'),
  486. ));
  487. $screen->add_help_tab(array(
  488. 'id' => 'hooks',
  489. 'title' => __('Hooks'),
  490. 'content' => $this->get_help_text('hooks'),
  491. ));
  492. }
  493.  
  494. /**
  495. * Get text for given help tab
  496. * @param string $key
  497. * @return string
  498. */
  499. function get_help_text($key) {
  500. if ($key === 'about') {
  501. $plugin_title = get_admin_page_title();
  502. $icon_url = plugins_url('images/icon-email-encoder-bundle.png', __FILE__);
  503. $content = <<<ABOUT
  504. <p><strong><img src="{$icon_url}" width="16" height="16" /> {$plugin_title} - version {$this->version}</strong></p>
  505. <p>Encode mailto links and (plain) email addresses and hide them from spambots. Easy to use, plugin works directly when activated. Save way to protect email addresses on your site.</p>
  506. ABOUT;
  507. } else if ($key === 'shortcodes') {
  508. $content = <<<SHORTCODES
  509. <p>Encode an email address:
  510. <br/><code>[encode_email email="..." display="..."]</code> ("display" is optional)
  511. </p>
  512. <p>Encode some content:
  513. <br/><code>[encode_content method="..."]...[/encode_content]</code> ("method" is optional)
  514. </p>
  515. <p>Puts an encoder form in your post:
  516. <br/><code>[email_encoder_form]</code>
  517. </p>
  518. SHORTCODES;
  519. } else if ($key === 'templatefunctions') {
  520. $content = <<<TEMPLATEFUNCTIONS
  521. <p>Encode the given email (other params are optional):
  522. <br/><code><&#63;php echo encode_email(\$email, [\$display], [\$method], [\$extra_attrs]); &#63;></code>
  523. </p>
  524. <p>Encode the given content for emails to encode (other param is optional):
  525. <br/><code><&#63;php echo encode_content(\$content, [\$method]); &#63;></code>
  526. </p>
  527. <p>Filter the given content for emails to encode (other params are optional):
  528. <br/><code><&#63;php echo encode_email_filter(\$content, [\$enc_tags], [\$enc_mailtos], [\$enc_plain_emails]); &#63;></code>
  529. </p>
  530. TEMPLATEFUNCTIONS;
  531. } else if ($key === 'hooks') {
  532. $content = <<<HOOKS
  533. <p>Add extra code on initializing this plugin, like extra filters for encoding.</p>
  534. <pre>
  535. function extra_encode_filters(\$filter_callback, \$object) {
  536. add_filter('some_filter', \$filter_callback);
  537. }
  538. add_action('init_email_encoder_bundle', 'extra_encode_filters');
  539. </pre>
  540. HOOKS;
  541. } else if ($key === 'sidebar') {
  542. $content = <<<SIDEBAR
  543. <p>See <a href="http://wordpress.org/extend/plugins/email-encoder-bundle/faq/" target="_blank">FAQ</a> at WordPress.org</p>
  544. <p>Send your <a href="http://www.freelancephp.net/contact/" target="_blank">question</a></p>
  545. <p><strong>Please <a href="http://wordpress.org/extend/plugins/email-encoder-bundle/" target="_blank">rate this plugin</a> and vote if the plugin works.</strong></p>
  546. SIDEBAR;
  547. }
  548.  
  549. return ((empty($content)) ? '' : __($content, $this->domain));
  550. }
  551.  
  552. /* -------------------------------------------------------------------------
  553. * Encoder Form
  554. * -------------------------------------------------------------------------/
  555.  
  556. /**
  557. * Get the encoder form (to use as a demo, like on the options page)
  558. * @return string
  559. */
  560. function get_encoder_form() {
  561. $lang_email = __('Email Address:', $this->domain);
  562. $lang_display = __('Display Text:', $this->domain);
  563. $lang_mailto = __('Mailto Link:', $this->domain);
  564. $lang_method = __('Encoding Method:', $this->domain);
  565. $lang_create = __('Create Protected Mail Link &gt;&gt;', $this->domain);
  566. $lang_output = __('Protected Mail Link (code):', $this->domain);
  567.  
  568. $method_options = '';
  569. foreach ($this->methods as $method => $info) {
  570. $method_options .= '<option value="' . $method . '"' . (($this->method == $method) ? ' selected="selected"' : '') . '>' . $info['name'] . '</option>';
  571. }
  572.  
  573. $powered_by = '';
  574. if ($this->options['powered_by']) {
  575. $powered_by .= '<p class="powered-by">' . __('Powered by', $this->domain) . ' <a rel="external" href="http://www.freelancephp.net/email-encoder-php-class-wp-plugin/">Email Encoder Bundle</a></p>';
  576. }
  577.  
  578. return <<<FORM
  579. <div class="email-encoder-form">
  580. <form>
  581. <fieldset>
  582. <div class="input">
  583. <table>
  584. <tbody>
  585. <tr>
  586. <th><label for="email">{$lang_email}</label></th>
  587. <td><input type="text" class="regular-text" id="email" name="email" /></td>
  588. </tr>
  589. <tr>
  590. <th><label for="display">{$lang_display}</label></th>
  591. <td><input type="text" class="regular-text" id="display" name="display" /></td>
  592. </tr>
  593. <tr>
  594. <th>{$lang_mailto}</th>
  595. <td><span id="example"></span></td>
  596. </tr>
  597. <tr>
  598. <th><label for="encode_method">{$lang_method}</label></th>
  599. <td><select id="encode_method" name="encode_method" class="postform">
  600. {$method_options}
  601. </select>
  602. <input type="button" id="ajax_encode" value="{$lang_create}" />
  603. </td>
  604. </tr>
  605. </tbody>
  606. </table>
  607. </div>
  608. <div class="output nodis">
  609. <table>
  610. <tbody>
  611. <tr>
  612. <th><label for="encoded_output">{$lang_output}</label></th>
  613. <td><textarea class="large-text node" id="encoded_output" name="encoded_output" cols="50" rows="4"></textarea></td>
  614. </tr>
  615. </tbody>
  616. </table>
  617. </div>
  618. {$powered_by}
  619. </fieldset>
  620. </form>
  621. </div>
  622. FORM;
  623. }
  624.  
  625. } // end class WP_Email_Encoder_Bundle_Admin
  626.  
  627. endif;
  628.  
  629.  
  630. /**
  631.  * Class WP_Email_Encoder_Bundle
  632.  * @package WP_Email_Encoder_Bundle
  633.  * @category WordPress Plugins
  634.  */
  635. if (!class_exists('WP_Email_Encoder_Bundle')):
  636.  
  637. class WP_Email_Encoder_Bundle extends WP_Email_Encoder_Bundle_Admin {
  638.  
  639. /**
  640. * Regexp
  641. * @var array
  642. */
  643. var $regexp_patterns = array(
  644. 'mailto' => '/<a([^<>]*?)href=["\']mailto:(.*?)["\'](.*?)>(.*?)<\/a[\s+]*>/is',
  645. 'email' => '/([A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6})/is',
  646. );
  647.  
  648. /**
  649. * PHP4 constructor
  650. */
  651. function WP_Email_Encoder_Bundle() {
  652. parent::__construct();
  653. }
  654.  
  655. /**
  656. * wp action
  657. */
  658. function wp() {
  659. parent::wp();
  660.  
  661. if (is_feed()) {
  662. // rss feed
  663. if ($this->options['filter_rss']) {
  664. $rss_filters = array('the_title', 'the_content', 'the_excerpt', 'the_title_rss', 'the_content_rss', 'the_excerpt_rss',
  665. 'comment_text_rss', 'comment_author_rss', 'the_category_rss', 'the_content_feed', 'author_feed_link', 'feed_link');
  666.  
  667. foreach($rss_filters as $filter) {
  668. add_filter($filter, array($this, 'callback_filter_rss'), 100);
  669. }
  670. }
  671. } else {
  672. // site
  673. $filters = array();
  674.  
  675. // post content
  676. if ($this->options['filter_posts']) {
  677. array_push($filters, 'the_title', 'the_content', 'the_excerpt', 'get_the_excerpt');
  678. }
  679.  
  680. // comments
  681. if ($this->options['filter_comments']) {
  682. array_push($filters, 'comment_text', 'comment_excerpt', 'comment_url', 'get_comment_author_url', 'get_comment_author_link', 'get_comment_author_url_link');
  683. }
  684.  
  685. // widgets
  686. if ($this->options['filter_widgets']) {
  687. array_push($filters, 'widget_title', 'widget_text', 'widget_content');
  688.  
  689. // also replace shortcodes
  690. add_filter('widget_text', 'do_shortcode', 100);
  691. add_filter('widget_content', 'do_shortcode', 100); // widget_content id filter of Widget Logic plugin
  692. }
  693.  
  694. foreach($filters as $filter) {
  695. add_filter($filter, array($this, 'callback_filter'), 100);
  696. }
  697. }
  698.  
  699. // shortcodes
  700. add_shortcode('email_encoder_form', array($this, 'shortcode_email_encoder_form'));
  701. add_shortcode('encode_email', array($this, 'shortcode_encode_email'));
  702. add_shortcode('encode_content', array($this, 'shortcode_encode_content'));
  703.  
  704. // actions
  705. add_action('wp_head', array($this, 'wp_head'));
  706.  
  707. // hook
  708. do_action('init_email_encoder_bundle', array($this, 'callback_filter'), $this);
  709. }
  710.  
  711. /**
  712. * WP head
  713. */
  714. function wp_head() {
  715. // add styling for encoding check message + icon
  716. if ($this->is_admin_user && $this->options['show_encoded_check']) {
  717. echo <<<CSS
  718. <style type="text/css">
  719. a.encoded-check { opacity:0.5; position:absolute; text-decoration:none !important; font:10px Arial !important; margin-top:-3px; color:#629632; font-weight:bold; }
  720. a.encoded-check:hover { opacity:1; cursor:help; }
  721. a.encoded-check img { width:10px; height:10px; }
  722. </style>
  723. CSS;
  724. }
  725. }
  726.  
  727. /* -------------------------------------------------------------------------
  728. * Filter Callbacks
  729. * -------------------------------------------------------------------------/
  730.  
  731. /**
  732. * WP filter callback
  733. * @param string $content
  734. * @return string
  735. */
  736. function callback_filter($content) {
  737. global $post;
  738.  
  739. if (isset($post) && in_array($post->ID, $this->skip_posts)) {
  740. return $content;
  741. }
  742.  
  743. return $this->encode_email_filter($content, TRUE, $this->options['encode_mailtos'], $this->options['encode_emails']);
  744. }
  745.  
  746. /**
  747. * RSS Filter callback
  748. * @param string $content
  749. * @return string
  750. */
  751. function callback_filter_rss($content) {
  752. return preg_replace($this->regexp_patterns, $this->options['protection_text'], $content);
  753. }
  754.  
  755. /**
  756. * Filter content for encoding
  757. * @param string $content
  758. * @param boolean $enc_tags Optional, default TRUE
  759. * @param boolean $enc_mailtos Optional, default TRUE
  760. * @param boolean $enc_plain_emails Optional, default TRUE
  761. * @return string
  762. */
  763. function encode_email_filter($content, $enc_tags = TRUE, $enc_mailtos = TRUE, $enc_plain_emails = TRUE) {
  764. // encode mailto links
  765. if ($enc_mailtos) {
  766. $content = preg_replace_callback($this->regexp_patterns['mailto'], array($this, 'callback_encode_email'), $content);
  767. }
  768.  
  769. // replace plain emails
  770. if ($enc_plain_emails) {
  771. $content = preg_replace_callback($this->regexp_patterns['email'], array($this, 'callback_encode_email'), $content);
  772. }
  773.  
  774. // workaround for double encoding bug when auto-protect mailto is enabled and method is enc_html
  775. if ($this->options['encode_mailtos'] == 1) {
  776. // change back to html tag
  777. $content = str_replace('[a-replacement]', '<a', $content);
  778. }
  779.  
  780. return $content;
  781. }
  782.  
  783. /**
  784. * Callback for encoding email
  785. * @param array $match
  786. * @return string
  787. */
  788. function callback_encode_email($match) {
  789. if (count($match) < 3) {
  790. return $this->encode_email($match[1]);
  791. } else if (count($match) == 3) {
  792. return $this->encode_email($match[2]);
  793. }
  794.  
  795. return $this->encode_email($match[2], $match[4], null, $match[1] . ' ' . $match[3]);
  796. }
  797.  
  798. /* -------------------------------------------------------------------------
  799. * Shortcode Functions
  800. * -------------------------------------------------------------------------/
  801.  
  802. /**
  803. * Shortcode showing encoder form
  804. * @return string
  805. */
  806. function shortcode_email_encoder_form() {
  807. // add style and script for ajax encoder
  808. wp_enqueue_script('email_encoder', plugins_url('js/email-encoder-bundle.js', __FILE__), array('jquery'), $this->version);
  809.  
  810. return $this->get_encoder_form();
  811. }
  812.  
  813. /**
  814. * Shortcode encoding email
  815. * @param array $attrs
  816. * @return string
  817. */
  818. function shortcode_encode_email($attrs) {
  819. if (!is_array($attrs) || !key_exists('email', $attrs)) {
  820. return '';
  821. }
  822.  
  823. $email = $attrs['email'];
  824. $display = (key_exists('display', $attrs)) ? $attrs['display'] : $attrs['email'];
  825. $method = (key_exists('method', $attrs)) ? $attrs['method'] : NULL;
  826. $extra_attrs = (key_exists('extra_attrs', $attrs)) ? $attrs['extra_attrs'] : NULL;
  827.  
  828. $encoded = $this->encode_email($email, $display, $method, $extra_attrs);
  829.  
  830. // workaround for double encoding bug when auto-protect mailto is enabled and method is enc_html
  831. if ($this->options['encode_mailtos'] == 1 && $method === 'enc_html') {
  832. // change html tag to entity
  833. $encoded = str_replace('<a', '[a-replacement]', $encoded);
  834. }
  835.  
  836. return $encoded;
  837. }
  838.  
  839. /**
  840. * Shortcode encoding content
  841. * @param array $attrs
  842. * @param string $content Optional
  843. * @return string
  844. */
  845. function shortcode_encode_content($attrs, $content = '') {
  846. $method = (is_array($attrs) && key_exists('method', $attrs)) ? $attrs['method'] : NULL;
  847.  
  848. return $this->encode($content, $method);
  849. }
  850.  
  851. /* -------------------------------------------------------------------------
  852. * Encode Functions
  853. * -------------------------------------------------------------------------/
  854.  
  855. /**
  856. * Encode the given email into an encoded HTML link
  857. * @param string $content
  858. * @param string $method Optional, else the default setted method will; be used
  859. * @param boolean $no_html_checked
  860. * @return string
  861. */
  862. function encode($content, $method = NULL, $no_html_checked = FALSE) {
  863. // get encode method
  864. $method = $this->get_method($method, $this->method);
  865.  
  866. // get encoded email code
  867. $content = $this->{$method}($content);
  868.  
  869. // add visual check
  870. if ($no_html_checked !== TRUE) {
  871. $content = $this->get_success_check($content);
  872. }
  873.  
  874. return $content;
  875. }
  876.  
  877. /**
  878. * Encode the given email into an encoded HTML link
  879. * @param string $email
  880. * @param string $display Optional, if not set display will be the email
  881. * @param string $method Optional, else the default setted method will; be used
  882. * @param string $extra_attrs Optional
  883. * @return string
  884. */
  885. function encode_email($email, $display = NULL, $method = NULL, $extra_attrs = '', $no_html_checked = FALSE) {
  886. // get encode method
  887. $method = $this->get_method($method, $this->method);
  888.  
  889. // decode entities
  890. $email = html_entity_decode($email);
  891.  
  892. // set email as display
  893. if ($display === NULL) {
  894. $display = $email;
  895.  
  896. if ($method === 'enc_html') {
  897. $display = $this->enc_html($display);
  898. }
  899. } else {
  900. $display = html_entity_decode($display);
  901. }
  902.  
  903. if ($method === 'enc_html') {
  904. $email = $this->enc_html($email);
  905. }
  906.  
  907. $class = $this->options['class_name'];
  908. $extra_attrs = ' ' . trim($extra_attrs);
  909. $mailto = '<a class="'. $class .'" href="mailto:' . $email . '"'. $extra_attrs . '>' . $display . '</a>';
  910.  
  911. if ($method === 'enc_html') {
  912. // add visual check
  913. if ($no_html_checked !== TRUE) {
  914. $mailto = $this->get_success_check($mailto);
  915. }
  916. } else {
  917. $mailto = $this->encode($mailto, $method, $no_html_checked);
  918. }
  919.  
  920. // get encoded email code
  921. return $mailto;
  922. }
  923.  
  924. /**
  925. * Add html to encoded content to show check icon and text
  926. * @param string $content
  927. * @return string
  928. */
  929. function get_success_check($content) {
  930. if (!$this->is_admin_user || !$this->options['show_encoded_check']) {
  931. return $content;
  932. }
  933.  
  934. return $content
  935. . '<a href="javascript:;" class="encoded-check"'
  936. . ' title="' . __('Successfully Encoded (this is a check and only visible when logged in as admin)', $this->domain) . '">'
  937. . '<img class="encoded-check-icon" src="' . plugins_url('images/icon-email-encoder-bundle.png', __FILE__)
  938. . '" alt="' . __('Encoded', $this->domain) . '" />'
  939. . __('Successfully Encoded', $this->domain) . '</a>';
  940. }
  941.  
  942. /* -------------------------------------------------------------------------
  943. * Different Encoding Methods
  944. * -------------------------------------------------------------------------/
  945.  
  946. /**
  947. * ASCII method
  948. * Based on function from Tyler Akins (http://rumkin.com/tools/mailto_encoder/)
  949. *
  950. * @param string $value
  951. * @return string
  952. */
  953. function enc_ascii($value) {
  954. $mail_link = $value;
  955.  
  956. $mail_letters = '';
  957.  
  958. for ($i = 0; $i < strlen($mail_link); $i ++) {
  959. $l = substr($mail_link, $i, 1);
  960.  
  961. if (strpos($mail_letters, $l) === false) {
  962. $p = rand(0, strlen($mail_letters));
  963. $mail_letters = substr($mail_letters, 0, $p) .
  964. $l . substr($mail_letters, $p, strlen($mail_letters));
  965. }
  966. }
  967.  
  968. $mail_letters_enc = str_replace("\\", "\\\\", $mail_letters);
  969. $mail_letters_enc = str_replace("\"", "\\\"", $mail_letters_enc);
  970.  
  971. $mail_indices = '';
  972. for ($i = 0; $i < strlen($mail_link); $i ++) {
  973. $index = strpos($mail_letters, substr($mail_link, $i, 1));
  974. $index += 48;
  975. $mail_indices .= chr($index);
  976. }
  977.  
  978. $mail_indices = str_replace("\\", "\\\\", $mail_indices);
  979. $mail_indices = str_replace("\"", "\\\"", $mail_indices);
  980.  
  981. return '<script type="text/javascript">'
  982. . '(function(){'
  983. . 'var ml="'. $mail_letters_enc .'",mi="'. $mail_indices .'",o="";'
  984. . 'for(var j=0,l=mi.length;j<l;j++){'
  985. . 'o+=ml.charAt(mi.charCodeAt(j)-48);'
  986. . '}document.write(o);'
  987. . '}());'
  988. . '</script><noscript>'
  989. . $this->options['protection_text']
  990. . '</noscript>';
  991. }
  992.  
  993. /**
  994. * Escape method
  995. * Taken from the plugin "Email Spam Protection" by Adam Hunter (http://blueberryware.net/2008/09/14/email-spam-protection/)
  996. *
  997. * @param string $value
  998. * @return string
  999. */
  1000. function enc_escape($value) {
  1001. $string = 'document.write(\'' . $value . '\')';
  1002.  
  1003. /* break string into array of characters, we can't use string_split because its php5 only :( */
  1004. $split = preg_split('||', $string);
  1005. $out = '<script type="text/javascript">' . "eval(unescape('";
  1006.  
  1007. foreach ($split as $c) {
  1008. /* preg split will return empty first and last characters, check for them and ignore */
  1009. if (!empty($c)) {
  1010. $out .= '%' . dechex(ord($c));
  1011. }
  1012. }
  1013.  
  1014. $out .= "'))" . '</script><noscript>'
  1015. . $this->options['protection_text']
  1016. . '</noscript>';
  1017.  
  1018. return $out;
  1019. }
  1020.  
  1021. /**
  1022. * Convert randomly chars to htmlentities
  1023. * This method is partly taken from WordPress
  1024. * @link http://codex.wordpress.org/Function_Reference/antispambot
  1025. *
  1026. * @param string $value
  1027. * @return string
  1028. */
  1029. function enc_html($value) {
  1030. // check for built-in WP function
  1031. if (function_exists('antispambot')) {
  1032. $emailNOSPAMaddy = antispambot($value);
  1033. } else {
  1034. $emailNOSPAMaddy = '';
  1035. srand ((float) microtime() * 1000000);
  1036. for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
  1037. $j = floor(rand(0, 1+$mailto));
  1038. if ($j==0) {
  1039. $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
  1040. } elseif ($j==1) {
  1041. $emailNOSPAMaddy .= substr($emailaddy,$i,1);
  1042. } elseif ($j==2) {
  1043. $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
  1044. }
  1045. }
  1046. $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
  1047. }
  1048.  
  1049. $emailNOSPAMaddy = str_replace('@', '&#64;', $emailNOSPAMaddy);
  1050.  
  1051. return $emailNOSPAMaddy;
  1052. }
  1053.  
  1054. } // end class WP_Email_Encoder_Bundle
  1055.  
  1056. endif;
  1057.  
  1058.  
  1059. /*******************************************************************************
  1060.  * Create instance
  1061.  *******************************************************************************/
  1062.  
  1063. $WP_Email_Encoder_Bundle = new WP_Email_Encoder_Bundle;
  1064.  
  1065.  
  1066. /**
  1067.  * Ajax Encoding request
  1068.  */
  1069. if (!empty($_GET['ajaxEncodeEmail'])):
  1070. // input vars
  1071. $method = $_GET['method'];
  1072. $email = $_GET['email'];
  1073. $display = (empty($_GET['display'])) ? $email : $_GET['display'];
  1074.  
  1075. echo $WP_Email_Encoder_Bundle->encode_email($email, $display, $method, '', TRUE);
  1076. endif;
  1077.  
  1078.  
  1079. /*******************************************************************************
  1080.  * Template Functions
  1081.  *******************************************************************************/
  1082.  
  1083. /**
  1084.  * Template function for encoding email
  1085.  * @global WP_Email_Encoder $WP_Email_Encoder_Bundle
  1086.  * @param string $email
  1087.  * @param string $display if non given will be same as email
  1088.  * @param string $method Optional, else the default setted method will; be used
  1089.  * @param string $extra_attrs Optional
  1090.  * @return string
  1091.  */
  1092. if (!function_exists('encode_email')):
  1093. function encode_email($email, $display = NULL, $method = NULL, $extra_attrs = '') {
  1094. global $WP_Email_Encoder_Bundle;
  1095. return $WP_Email_Encoder_Bundle->encode_email($email, $display, $method, $extra_attrs);
  1096. }
  1097. endif;
  1098.  
  1099. /**
  1100.  * Template function for encoding content
  1101.  * @global WP_Email_Encoder $WP_Email_Encoder_Bundle
  1102.  * @param string $content
  1103.  * @param string $method Optional, default NULL
  1104.  * @return string
  1105.  */
  1106. if (!function_exists('encode_content')):
  1107. function encode_content($content, $method = NULL) {
  1108. global $WP_Email_Encoder_Bundle;
  1109. return $WP_Email_Encoder_Bundle->encode($content, $method);
  1110. }
  1111. endif;
  1112.  
  1113. /**
  1114.  * Template function for encoding emails in the given content
  1115.  * @global WP_Email_Encoder $WP_Email_Encoder_Bundle
  1116.  * @param string $content
  1117.  * @param boolean $enc_tags Optional, default TRUE
  1118.  * @param boolean $enc_mailtos Optional, default TRUE
  1119.  * @param boolean $enc_plain_emails Optional, default TRUE
  1120.  * @return string
  1121.  */
  1122. if (!function_exists('encode_email_filter')):
  1123. function encode_email_filter($content, $enc_tags = TRUE, $enc_mailtos = TRUE, $enc_plain_emails = TRUE) {
  1124. global $WP_Email_Encoder_Bundle;
  1125. return $WP_Email_Encoder_Bundle->encode_email_filter($content, $enc_tags, $enc_mailtos, $enc_plain_emails);
  1126. }
  1127. endif;
  1128.  
  1129. /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */

URL: http://www.freelancephp.net/email-encoder-php-class-wp-plugin/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.