Your Ad Here

Posted By

freelancephp on 02/14/11


Tagged

email wordpress wp spam encoder anti-spam


Versions (?)

WP Email Encoder Bundle


 / Published in: PHP
 

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

Features: * Encodes email adresses (plain text and mailto links) * Choose the preferred method (or on every request randomly pick one of the methods) * Easy to use.

Extra: * Put an email encoder form on your own site. * Add your own methods.

  1. <?php
  2. /*
  3. Plugin Name: Email Encoder Bundle
  4. Plugin URI: http://www.freelancephp.net/email-encoder
  5. Description: Protecting email-spamming by replacing them with one of the registered encoding-methods
  6. Author: Victor Villaverde Laan
  7. Version: 0.1
  8. Author URI: http://www.freelancephp.net
  9. */
  10. // include parent class
  11. require_once dirname(__FILE__) . '/lim-email-encoder.php';
  12.  
  13. /**
  14.  * Class WP_Email_Encoder, child of Lim_Email_Encoder
  15.  * @package Lim_Email_Encoder
  16.  * @category WordPress Plugins
  17.  */
  18. class WP_Email_Encoder extends Lim_Email_Encoder {
  19.  
  20. /**
  21. * Prefix for options entry and being used as text domain (for translations)
  22. * @var string
  23. */
  24. var $prefix = 'wp_esp';
  25.  
  26. /**
  27. * @var array
  28. */
  29. var $wp_options = array(
  30. 'filter_comments' => TRUE,
  31. 'form_on_site' => FALSE, // set encoder form on the website
  32. 'powered_by' => TRUE,
  33. );
  34.  
  35. /**
  36. * PHP4 constructor
  37. */
  38. function WP_Email_Encoder() {
  39. $this->__construct();
  40. }
  41.  
  42. /**
  43. * PHP5 constructor
  44. */
  45. function __construct() {
  46. parent::__construct();
  47.  
  48. // add all $wp_options to $options
  49. $this->options = array_merge( $this->options, $this->wp_options );
  50. // set option values
  51. $this->_set_options();
  52.  
  53. // add filters
  54. add_filter( 'the_content', array( &$this, 'encode_filter' ), 100 );
  55.  
  56. // also filter comments
  57. if ( $this->options['filter_comments'] )
  58. add_filter( 'comment_text', array( &$this, 'encode_filter' ), 100 );
  59.  
  60. // add actions
  61. add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
  62. add_action( 'admin_init', array( &$this, 'admin_init' ) );
  63.  
  64. if ( $this->options['form_on_site'] )
  65. add_action( 'the_posts', array( &$this, 'the_posts' ) );
  66.  
  67. // set uninstall hook
  68. if ( function_exists( 'register_deactivation_hook' ) )
  69. register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ));
  70. }
  71.  
  72. /**
  73. * Callback for the_post action
  74. * @param array $posts
  75. */
  76. function the_posts( $posts ) {
  77. if ( empty( $posts ) )
  78. return $posts;
  79.  
  80. foreach ( $posts as $key => $post ) {
  81. if ( stripos( $post->post_content, '[email_encoder_form]' ) > -1 ) {
  82. // add style and script for ajax encoder
  83. wp_enqueue_script( 'email_encoder', plugins_url( 'js/wp_esp.js', __FILE__ ), array( 'jquery' ) );
  84. // replace tag by form
  85. $posts[$key]->post_content = str_replace( '[email_encoder_form]', $this->get_encoder_form(), $post->post_content );
  86. break;
  87. }
  88. }
  89.  
  90. return $posts;
  91. }
  92.  
  93. /**
  94. * Callback admin_menu
  95. */
  96. function admin_menu() {
  97. if ( function_exists('add_options_page') AND current_user_can('manage_options') ) {
  98. // add options page
  99. $page = add_options_page( 'Email Encoder Bundle', 'Email Encoder Bundle',
  100. 'manage_options', __FILE__, array( &$this, 'options_page' ) );
  101.  
  102. // add scripts
  103. add_action( 'admin_print_scripts-' . $page, array( &$this, 'admin_print_scripts' ) );
  104. }
  105. }
  106.  
  107. /**
  108. * Callback admin_init
  109. */
  110. function admin_init() {
  111. // register settings
  112. register_setting( $this->prefix, $this->prefix . 'options' );
  113. }
  114.  
  115. /**
  116. * Callback admin_print_scripts
  117. */
  118. function admin_print_scripts() {
  119. // add script for ajax encoder
  120. wp_enqueue_script( 'email_encoder', plugins_url( 'js/wp_esp.js', __FILE__ ), array( 'jquery' ) );
  121. }
  122.  
  123. /**
  124. * Admin options page
  125. */
  126. function options_page() {
  127. ?>
  128. <div class="wrap">
  129. <h2>Email Encoder Bundle</h2>
  130.  
  131. <div>
  132. <h3>Settings</h3>
  133. <form method="post" action="options.php">
  134. <?php
  135. settings_fields( $this->prefix );
  136. $this->_set_options();
  137. $options = $this->options;
  138. ?>
  139. <fieldset class="options">
  140. <table class="form-table">
  141. <tr>
  142. <th><label for="<?php echo $this->prefix ?>options[method]"><?php _e( 'Encoding method', $this->prefix ) ?></label></th>
  143. <td><select id="<?php echo $this->prefix ?>options[method]" name="<?php echo $this->prefix ?>options[method]" class="postform">
  144. <?php foreach ( $this->methods AS $key => $method ): ?>
  145. <option value="<?php echo $key ?>" <?php if ( $options['method'] == $key ) echo 'selected="selected"' ?>><?php _e( $key, $this->prefix ) ?></option>
  146. <?php endforeach; ?>
  147. <option value="random" <?php if ( $options['method'] == 'random' ) echo 'selected="selected"' ?>><?php _e( 'random', $this->prefix ) ?></option>
  148. </select></td>
  149. </tr>
  150. <tr>
  151. <th><label for="<?php echo $this->prefix ?>options[encode_display]"><?php _e( 'Encode email titles (display)', $this->prefix ) ?></label></th>
  152. <td><input type="checkbox" id="<?php echo $this->prefix ?>options[encode_display]" name="<?php echo $this->prefix ?>options[encode_display]" value="1" <?php checked('1', (int) $options['encode_display']); ?> /> <span class="description"><?php _e( '(recommended)', $this->prefix ) ?></span></td>
  153. </tr>
  154. <tr>
  155. <th><label for="<?php echo $this->prefix ?>options[encode_mailto]"><?php _e( 'Encode mailto links', $this->prefix ) ?></label></th>
  156. <td><input type="checkbox" id="<?php echo $this->prefix ?>options[encode_mailto]" name="<?php echo $this->prefix ?>options[encode_mailto]" value="1" <?php checked('1', (int) $options['encode_mailto']); ?> /> <span class="description"><?php _e( 'encode all mailto links in the content', $this->prefix ) ?></span></td>
  157. </tr>
  158. <tr>
  159. <th><label for="<?php echo $this->prefix ?>options[replace_emails]"><?php _e( 'Replace plain text emails', $this->prefix ) ?></label></th>
  160. <td><input type="checkbox" id="<?php echo $this->prefix ?>options[replace_emails]" name="<?php echo $this->prefix ?>options[replace_emails]" value="1" <?php checked('1', (int) $options['replace_emails']); ?> /> <span class="description"><?php _e( 'replacing plain text emails in the content to an encoded mailto link', $this->prefix ) ?></span></td>
  161. </tr>
  162. <tr>
  163. <th><label for="<?php echo $this->prefix ?>options[filter_comments]"><?php _e( 'Also filter comments', $this->prefix ) ?></label></th>
  164. <td><input type="checkbox" id="<?php echo $this->prefix ?>options[filter_comments]" name="<?php echo $this->prefix ?>options[filter_comments]" value="1" <?php checked('1', (int) $options['filter_comments']); ?> /> <span class="description"><?php _e( 'also filter all comments for encoding', $this->prefix ) ?></span></td>
  165. </tr>
  166. <tr>
  167. <th style="padding-top:25px"><label for="<?php echo $this->prefix ?>options[form_on_site]"><?php _e( 'Encode form on your site', $this->prefix ) ?></label></th>
  168. <td style="padding-top:25px"><input type="checkbox" id="<?php echo $this->prefix ?>options[form_on_site]" name="<?php echo $this->prefix ?>options[form_on_site]" value="1" <?php checked('1', (int) $options['form_on_site']); ?> /> <span class="description"><?php _e( 'put an encode form on your site, use this tag in a post or page:', $this->prefix ) ?></span> <code>[email_encoder_form]</code></td>
  169. </tr>
  170. <tr>
  171. <th><label for="<?php echo $this->prefix ?>options[powered_by]"><?php _e( 'Show powered by link', $this->prefix ) ?></label></th>
  172. <td><input type="checkbox" id="<?php echo $this->prefix ?>options[powered_by]" name="<?php echo $this->prefix ?>options[powered_by]" value="1" <?php checked('1', (int) $options['powered_by']); ?> /> <span class="description"><?php _e( 'show the powered by link on bottom of the encode form', $this->prefix ) ?></span></td>
  173. </tr>
  174. </table>
  175. </fieldset>
  176. <p class="submit">
  177. <input class="button-primary" type="submit" value="<?php _e( 'Save Changes', $this->prefix ) ?>" />
  178. </p>
  179. </form>
  180. </div>
  181.  
  182. <div>
  183. <h3>How To Use?</h3>
  184. <p>Inside a post or page use this code: <code>[encode_email email="info@myemail.com" display="My Email"]</code></p>
  185. <p>And for templates use: <code>&lt;?php echo encode_email( 'info@myemail.com', 'My Email' ); ?&gt;</code></p>
  186. </div>
  187.  
  188. <div>
  189. <h3>Encoder Form</h3>
  190. <?php echo $this->get_encoder_form(); ?>
  191. </div>
  192.  
  193. </div>
  194. <?php
  195. }
  196.  
  197. /**
  198. * Get the encoder form (to use as a demo, like on the options page)
  199. * @return string
  200. */
  201. function get_encoder_form() {
  202. ?>
  203. <form class="email-encoder-form">
  204. <fieldset>
  205. <table class="form-table">
  206. <tr>
  207. <tr>
  208. <th><label for="email"><?php _e( 'Email', $this->prefix ) ?></label></th>
  209. <td><input type="text" class="regular-text" id="email" name="email" /></td>
  210. </tr>
  211. <tr>
  212. <th><label for="display"><?php _e( 'Display (optional)', $this->prefix ) ?></label></th>
  213. <td><input type="text" class="regular-text" id="display" name="display" /></td>
  214. </tr>
  215. <tr>
  216. <th><label for="encode_method"><?php _e( 'Encode method', $this->prefix ) ?></label></th>
  217. <td><select id="encode_method" name="encode_method" class="postform">
  218. <?php foreach ( $this->methods AS $key => $method ): ?>
  219. <option value="<?php echo $key ?>" <?php if ( $this->options['method'] == $key ) echo 'selected="selected"' ?>><?php _e( $key, $this->prefix ) ?></option>
  220. <?php endforeach; ?>
  221. <option value="random" <?php if ( $this->options['method'] == 'random' ) echo 'selected="selected"' ?>><?php _e( 'random', $this->prefix ) ?></option>
  222. </select>
  223. <input type="button" id="ajax_encode" value="Encode &gt;" /></td>
  224. </tr>
  225. </tr>
  226. <tr>
  227. <tr>
  228. <th><?php _e( 'Example', $this->prefix ) ?></th>
  229. <td><span id="example"></span></td>
  230. </tr>
  231. <tr>
  232. <th><label for="encoded_output"><?php _e( 'Code', $this->prefix ) ?></label></th>
  233. <td><textarea class="large-text node" id="encoded_output" name="encoded_output"></textarea></td>
  234. </tr>
  235. </tr>
  236. </table>
  237. <?php if ( $this->options['powered_by'] ): ?>
  238. <p class="powered-by">Powered by <a rel="external" href="http://www.freelancephp.net/email-encoder/">Email Encoder Bundle</a></p>
  239. <?php endif; ?>
  240. </fieldset>
  241. </form>
  242. <?php
  243. $form = ob_get_contents();
  244.  
  245. return $form;
  246. }
  247.  
  248. /**
  249. * Deactivation plugin method
  250. */
  251. function deactivation() {
  252. delete_option( $this->prefix . 'options' );
  253. unregister_setting( $this->prefix, $this->prefix . 'options' );
  254. }
  255.  
  256. /**
  257. * Set options from save values or defaults
  258. */
  259. function _set_options() {
  260. // set options
  261. $saved_options = get_option( $this->prefix . 'options' );
  262. if ( empty( $saved_options ) ) {
  263. // set defaults
  264. $this->options['encode_display'] = (int) $this->options['encode_display'];
  265. $this->options['encode_mailto'] = (int) $this->options['encode_mailto'];
  266. $this->options['replace_emails'] = (int) $this->options['replace_emails'];
  267. $this->options['filter_comments'] = (int) $this->options['filter_comments'];
  268. $this->options['form_on_site'] = (int) $this->options['form_on_site'];
  269. $this->options['powered_by'] = (int) $this->options['powered_by'];
  270. } else {
  271. // set saved option values
  272. $this->set_method( $saved_options['method'] );
  273. $this->options['encode_display'] = ! empty( $saved_options['encode_display'] );
  274. $this->options['encode_mailto'] = ! empty( $saved_options['encode_mailto'] );
  275. $this->options['replace_emails'] = ! empty( $saved_options['replace_emails'] );
  276. $this->options['filter_comments'] = ! empty( $saved_options['filter_comments'] );
  277. $this->options['form_on_site'] = ! empty( $saved_options['form_on_site'] );
  278. $this->options['powered_by'] = ! empty( $saved_options['powered_by'] );
  279. }
  280. }
  281.  
  282. } // end class WP_Email_Encoder
  283.  
  284.  
  285. /**
  286.  * Create instance
  287.  */
  288. $WP_Email_Encoder = new WP_Email_Encoder;
  289.  
  290.  
  291. /**
  292.  * Ajax request
  293.  */
  294. if ( ! empty( $_GET['ajax'] ) ):
  295. // input vars
  296. $method = $_GET['method'];
  297. $email = $_GET['email'];
  298. $display = ( empty( $_GET['display'] ) ) ? $email : $_GET['display'];
  299.  
  300. $WP_Email_Encoder->set_method( $method );
  301.  
  302. echo $WP_Email_Encoder->encode_email( $email, $display );
  303. endif;
  304.  
  305.  
  306. /**
  307.  * Template function for encoding email
  308.  * @global WP_Email_Encoder $WP_Email_Encoder
  309.  * @param string $email
  310.  * @param string $display if non given will be same as email
  311.  * @return string
  312.  */
  313. if ( ! function_exists( 'encode_email' ) ):
  314. function encode_email( $email, $display = NULL ) {
  315. global $WP_Email_Encoder;
  316. return $WP_Email_Encoder->encode_email( $email, $display );
  317. }
  318. endif;
  319.  
  320. ?>

Report this snippet  

You need to login to post a comment.