2 defined( 'ABSPATH' ) ||
die( 'No script kiddies please!' );
4 // generate a unique code, allows for lenght parameter
5 if ( ! function_exists( 'dnxcf_get_unique_code' ) ) {
7 function dnxcf_get_unique_code( $length = '' ) {
8 $code = md5( uniqid( rand(), true ) );
10 if ( $length != '' ) {
11 return substr( $code, 0, $length );
18 // set default options for the plugin
19 function dnxcf_set_options() {
21 'dnxcf_pid_key' => dnxcf_get_unique_code( 12 ),
22 'dnxcf_recv_name' => 'admin',
23 'dnxcf_recv_email' => get_bloginfo( 'admin_email' ),
24 'dnxcf_from_email' => 'info@some.url',
25 'dnxcf_from_name' => 'webmaster',
26 'dnxcf_subject' => array(
27 __( 'I want to make a comment.', 'dnxcf' ),
28 __( 'I want to ask a question.', 'dnxcf' ),
29 __( 'I am interested in a product.', 'dnxcf' ),
30 __( 'I have to report a problem.', 'dnxcf' ),
31 __( 'Other (explain below)', 'dnxcf' ),
35 'dnxcf_content_type' => '1',
36 'dnxcf_privacy' => '',
37 'dnxcf_DB_VERSION' => '2',
42 // helper function that starts up the DB
43 function dnxcf_db_init() {
44 global $dnxcf_options;
45 $dnxcf_options = get_option( 'dnxcf_options' );
46 if ( false === $dnxcf_options ) {
47 $dnxcf_options = dnxcf_set_options();
49 update_option( 'dnxcf_options', $dnxcf_options );
52 // helper function that performs a DB version update when needed
53 function dnxcf_db_update( $db_version ) {
54 global $dnxcf_options;
55 $db_defaults = dnxcf_set_options();
56 $merge = wp_parse_args( $dnxcf_options, $db_defaults );
58 $merge['dnxcf_DB_VERSION'] = $db_version;
59 update_option( 'dnxcf_options', $merge );
62 // helper function that performs a DB check and then an init/update action
63 function dnxcf_db_check() {
64 global $dnxcf_options;
65 if ( false === $dnxcf_options ) {
68 $old_db_version = $dnxcf_options['dnxcf_DB_VERSION'];
69 $new_db_version = DNXCF_CURRENT_DB_VERSION
;
70 if ( empty( $old_db_version ) ) {
73 if ( intval( $old_db_version ) < intval( $new_db_version ) ) {
74 dnxcf_db_update( $new_db_version );
78 // helper function that sets the current DB Version for comparison
80 * Latest DB version: 5
82 function dnxcf_set_db_version() {
83 // Define plugin database version. This should only change when new settings are added.
84 if ( ! defined( 'DNXCF_CURRENT_DB_VERSION' ) ) {
85 define( 'DNXCF_CURRENT_DB_VERSION', 5 );
89 // set the "from" email name to a custom option specified by the user
90 function dnxcf_update_from_name() {
91 global $dnxcf_options;
92 $dnxcf_options = get_option( 'dnxcf_options' );
93 $dnxcf_defaults = dnxcf_set_options();
94 $from_name = $dnxcf_options['dnxcf_from_name'];
95 $orig_name = 'WordPress';
97 $name = ( $orig_name != $from_name ) ?
$from_name : false;
100 if ( dnxcf_update_from_name() ) {
101 add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' );
105 // set the "from" email address to a custom option specified by the user
106 function dnxcf_update_from_email() {
107 global $dnxcf_options;
108 $dnxcf_options = get_option( 'dnxcf_options' );
109 $dnxcf_defaults = dnxcf_set_options();
110 $from_mail = $dnxcf_options['dnxcf_from_email'];
111 $orig_mail = $dnxcf_defaults['dnxcf_from_email'];
113 $mail = ( $orig_mail != $from_mail ) ?
$from_mail : false;
116 if ( dnxcf_update_from_email() ) {
117 add_filter( 'wp_mail_from', 'dnxcf_update_from_email' );
121 * Set the content type for the email
123 function dnxcf_update_content_type() {
124 global $dnxcf_options;
125 $dnxcf_options = get_option( 'dnxcf_options' );
129 $content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ?
'text/plain' : 'text/html';
131 return $content_type;
133 add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' );