ContactForm7_Serial_Numbers

The Contact Form 7 Serial Numbers ContactForm7 Serial Numbers class.

Defined (1)

The class is defined in the following location(s).

/contact-form-7-serial-numbers.php  
  1. class ContactForm7_Serial_Numbers { 
  2.  
  3. private $options; 
  4. private $is_active_cf7ac; 
  5. const OPTION_SAVE_FILE = 'wpcf7sn_options.txt'; 
  6. const DOMAIN = 'contact-form-7-serial-numbers'; 
  7.  
  8. function __construct() { 
  9. $this->options = $this->get_plugin_options(); 
  10.  
  11. // ************************** 
  12. if ( function_exists( 'register_activation_hook' ) ) 
  13. register_activation_hook( __FILE__, array( &$this, 'activation' ) ); 
  14. // ************************** 
  15. if ( function_exists( 'register_deactivation_hook' ) ) 
  16. register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ) ); 
  17.  
  18. // *********** 
  19. add_action( 'admin_init', array( &$this, 'admin_init' ) ); 
  20. add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); 
  21. add_action( 'wpcf7_before_send_mail', array( &$this, 'increment_count' ) ); 
  22.  
  23. // *********** 
  24. add_filter( 'wpcf7_special_mail_tags', array( &$this, 'special_mail_tags' ), 10, 2 ); 
  25. add_filter( 'wpcf7_posted_data', array( &$this, 'add_serial_number_to_posted_data' ), 10, 1 ); 
  26.  
  27. // ********** 
  28. add_shortcode( 'wpcf7sn_view_count', array( &$this, 'view_serial_number' ) ); 
  29.  
  30. // *********** 
  31. load_plugin_textdomain( self::DOMAIN, false, basename( dirname( __FILE__ ) ) . '/languages' ); 
  32.  
  33. // Contact Form 7 add confirm ********** 
  34. // ********** 1, ******** 0 
  35. $this->is_active_cf7ac = 
  36. ( $this->is_active_plugin('contact-form-7-add-confirm/contact-form-7-confirm.php') ) ? 
  37. intval( $this->is_active_plugin('contact-form-7-add-confirm/contact-form-7-confirm.php') ) : 0; 
  38.  
  39. // plugin activation 
  40. function activation() { 
  41. $option_file = dirname( __FILE__ ) . '/' . self::OPTION_SAVE_FILE; 
  42. if ( file_exists( $option_file ) ) { 
  43. $wk_options = unserialize( file_get_contents( $option_file ) ); 
  44. if ( $wk_options != $this->options ) { 
  45. $this->options = $wk_options; 
  46. foreach ( $this->options as $key=>$value ) { 
  47. update_option( $key, $value ); 
  48. unlink( $option_file ); 
  49.  
  50. // plugin deactivation 
  51. function deactivation() { 
  52. $option_file = dirname( __FILE__ ) . '/' . self::OPTION_SAVE_FILE; 
  53. $wk_options = serialize( $this->options ); 
  54. if ( file_put_contents( $option_file, $wk_options ) && file_exists( $option_file ) ) { 
  55. foreach( $this->options as $key=>$value ) { 
  56. delete_option( $key ); 
  57.  
  58. // get plugin options 
  59. function get_plugin_options() { 
  60. global $wpdb; 
  61. $values = array(); 
  62. $results = $wpdb->get_results( " 
  63. SELECT * 
  64. FROM $wpdb->options 
  65. WHERE 1 = 1 
  66. AND option_name like 'nklab_wpcf7sn_%' 
  67. ORDER BY option_name 
  68. " ); 
  69.  
  70. foreach ( $results as $result ) { 
  71. $values[ $result->option_name ] = $result->option_value; 
  72.  
  73. return $values; 
  74.  
  75. // admin init 
  76. function admin_init() { 
  77. wp_enqueue_style( 'contact-form-7-serial-numbers', plugin_dir_url( __FILE__ ) . 'css/style.css' ); 
  78.  
  79. // admin menu 
  80. function admin_menu() { 
  81. add_options_page( 
  82. __( 'Contact Form 7 Serial Numbers', self::DOMAIN ),  
  83. __( 'Contact Form 7 Serial Numbers', self::DOMAIN ),  
  84. 'level_8',  
  85. __FILE__,  
  86. array( &$this, 'wpcf7sn_admin_opt_page' ) 
  87. ); 
  88.  
  89. // option page 
  90. function wpcf7sn_admin_opt_page() { 
  91. $list_table = new NKLAB_WPCF7SN_Contact_List_Table(); 
  92. $list_table->prepare_items(); 
  93.  
  94. if ( is_admin() ) { 
  95. // jQuery ** 
  96. wp_enqueue_script( 'jquery' ); 
  97. wp_enqueue_script( self::DOMAIN, plugin_dir_url( __FILE__ ) . 'js/contact-form-7-serial-numbers.js' ); 
  98. ?> 
  99. <div class="wrap"> 
  100. <h2><?php _e( 'Contact Form 7 Serial Numbers', self::DOMAIN ); ?></h2> 
  101. <p></p> 
  102. <p><?php _e( 'Copy the code of mail tags and paste it into any location ( ex. message body or subject etc.) of mail templates of Contact Form 7.', self::DOMAIN ); ?></p> 
  103.  
  104. <?php $list_table->display(); ?> 
  105. </div> 
  106. <?php 
  107.  
  108. // increment count 
  109. function increment_count( $contactform ) { 
  110. // allow count up flag 
  111. $allow_count_up = false; 
  112.  
  113. // get form id 
  114. $id = intval( $contactform->id() ); 
  115.  
  116. // get count 
  117. $count = ( get_option( 'nklab_wpcf7sn_count_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_count_' . $id ) ) : 0; 
  118.  
  119. // check count up 
  120. if ( $this->is_active_cf7ac == 1 ) { 
  121. if ( isset( $_POST['_wpcf7c'] ) && ( "step1" != $_POST['_wpcf7c'] ) ) { 
  122. // Contact Form 7 add confirm ********** _wpcf7c * step1 ******* 
  123. $allow_count_up = true; 
  124. } else { 
  125. // Contact Form 7 add confirm ********* 
  126. $allow_count_up = true; 
  127.  
  128. if ( $allow_count_up ) { 
  129. update_option( 'nklab_wpcf7sn_count_' . $id, intval( $count + 1 ) ); 
  130.  
  131. // is active plugin 
  132. function is_active_plugin( $plugin ) { 
  133. if ( function_exists('is_plugin_active') ) { 
  134. return is_plugin_active( $plugin ); 
  135. } else { 
  136. return in_array( $plugin, get_option('active_plugins') ); 
  137.  
  138. // special mail tags 
  139. function special_mail_tags( $output, $name ) { 
  140. if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) ) return $output; 
  141. $name = preg_replace( '/^wpcf7\./', '_', $name ); 
  142.  
  143. if ( 'cf7_serial_number_' == substr( $name, 0, 18 ) ) { 
  144. // form id *** 
  145. $id = intval( substr( $name, 18 ) ); 
  146.  
  147. // ********* 
  148. $digits = ( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) : 0; 
  149. $type = ( get_option( 'nklab_wpcf7sn_type_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_type_' . $id ) ) : 1; 
  150. $prefix = ( get_option( 'nklab_wpcf7sn_prefix_' . $id ) ) ? get_option( 'nklab_wpcf7sn_prefix_' . $id ) : ''; 
  151. $count = ( get_option( 'nklab_wpcf7sn_count_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_count_' . $id ) ) : 0; 
  152.  
  153. switch( $type ) { 
  154. case 1: 
  155. // ** 
  156. $output = $count; 
  157. if ( $digits ) { 
  158. $output = sprintf( "%0" . $digits . "d", $output ); 
  159. break; 
  160. case 2: 
  161. // ******* 
  162. $output = microtime( true ) * 10000; 
  163. break; 
  164. default: 
  165. $output = ''; 
  166. $output = $prefix . $output; 
  167.  
  168. // ********Session****Cookie******* 
  169. if ( isset( $_SESSION ) ) { 
  170. // ******** 
  171. $_SESSION[ 'wpcf7sn_output_' . $id ] = $output; 
  172. } else { 
  173. // **************** 1 ** Cookie *** 
  174. setcookie( 'wpcf7sn_output_' . $id, $output, time() + 60 ); 
  175. return $output; 
  176.  
  177. // add serial number to posted data 
  178. function add_serial_number_to_posted_data( $posted_data ) { 
  179.  
  180. if ( !empty( $posted_data ) ) { 
  181. // id *** 
  182. $id = intval( $posted_data['_wpcf7'] ); 
  183.  
  184. // ********* 
  185. $digits = ( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) : 0; 
  186. $type = ( get_option( 'nklab_wpcf7sn_type_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_type_' . $id ) ) : 1; 
  187. $prefix = ( get_option( 'nklab_wpcf7sn_prefix_' . $id ) ) ? get_option( 'nklab_wpcf7sn_prefix_' . $id ) : ''; 
  188. $count = ( get_option( 'nklab_wpcf7sn_count_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_count_' . $id ) ) : 0; 
  189.  
  190. switch( $type ) { 
  191. case 1: 
  192. // ** 
  193. $output = $count; 
  194. if ( $digits ) { 
  195. $output = sprintf( "%0" . $digits . "d", $output ); 
  196. break; 
  197. case 2: 
  198. // ******* 
  199. $output = microtime( true ) * 10000; 
  200. break; 
  201. default: 
  202. $output = ''; 
  203. $output = $prefix . $output; 
  204.  
  205. $posted_data['Serial Number'] = $output; 
  206.  
  207. return $posted_data; 
  208.  
  209. // ShortCode 
  210. function view_serial_number( $atts ) { 
  211. // ***** 
  212. extract(shortcode_atts(array( 
  213. 'id' => 0,  
  214. ), $atts)); 
  215.  
  216. if ( isset( $_SESSION[ 'wpcf7sn_output_' . $id ] ) ) { 
  217. // ******** 
  218. $output = $_SESSION[ 'wpcf7sn_output_' . $id ]; 
  219. } else { 
  220. // ************Cookie * 
  221. if ( isset( $_COOKIE[ 'wpcf7sn_output_' . $id ] ) ) { 
  222. $output = $_COOKIE[ 'wpcf7sn_output_' . $id ]; 
  223. } else { 
  224. // ******Cookie*** 
  225. $digits = ( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_digits_' . $id ) ) : 0; 
  226. $type = ( get_option( 'nklab_wpcf7sn_type_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_type_' . $id ) ) : 1; 
  227. $prefix = ( get_option( 'nklab_wpcf7sn_prefix_' . $id ) ) ? get_option( 'nklab_wpcf7sn_prefix_' . $id ) : ''; 
  228. $count = ( get_option( 'nklab_wpcf7sn_count_' . $id ) ) ? intval( get_option( 'nklab_wpcf7sn_count_' . $id ) ) : 0; 
  229.  
  230. switch( $type ) { 
  231. case 1: 
  232. // ** 
  233. $output = $count; 
  234. if ( $digits ) { 
  235. $output = sprintf( "%0" . $digits . "d", $output ); 
  236. break; 
  237. case 2: 
  238. // **************************** 
  239. $output = microtime( true ) * 10000; 
  240. break; 
  241. default: 
  242. $output = ''; 
  243. $output = $prefix . $output; 
  244.  
  245. return $output; 
  246.