b96bc758 |
1 | <?php |
244ec72c |
2 | defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); |
b96bc758 |
3 | |
4 | // generate a unique code, allows for lenght parameter |
8c3e9b00 |
5 | if ( ! function_exists( 'dnxcf_get_unique_code' ) ) { |
b96bc758 |
6 | |
8c3e9b00 |
7 | function dnxcf_get_unique_code( $length = '' ) { |
8 | $code = md5( uniqid( rand(), true ) ); |
b96bc758 |
9 | |
8c3e9b00 |
10 | if ( $length != '' ) { |
11 | return substr( $code, 0, $length ); |
12 | } else { |
13 | return $code; |
14 | } |
15 | } |
b96bc758 |
16 | } |
17 | |
b96bc758 |
18 | // set default options for the plugin |
19 | function dnxcf_set_options() { |
8c3e9b00 |
20 | $defaults = array( |
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' ), |
32 | ), |
33 | // 1 = text/plain |
34 | // 2 = text/html |
35 | 'dnxcf_content_type' => '1', |
36 | 'dnxcf_privacy' => '', |
8c3e9b00 |
37 | 'dnxcf_DB_VERSION' => '2', |
38 | ); |
39 | return $defaults; |
b96bc758 |
40 | } |
41 | |
42 | // helper function that starts up the DB |
43 | function dnxcf_db_init() { |
8c3e9b00 |
44 | global $dnxcf_options; |
45 | $dnxcf_options = get_option( 'dnxcf_options' ); |
46 | if ( false === $dnxcf_options ) { |
47 | $dnxcf_options = dnxcf_set_options(); |
48 | } |
49 | update_option( 'dnxcf_options', $dnxcf_options ); |
b96bc758 |
50 | } |
51 | |
52 | // helper function that performs a DB version update when needed |
8c3e9b00 |
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 ); |
57 | // update DB version |
58 | $merge['dnxcf_DB_VERSION'] = $db_version; |
59 | update_option( 'dnxcf_options', $merge ); |
b96bc758 |
60 | } |
61 | |
62 | // helper function that performs a DB check and then an init/update action |
63 | function dnxcf_db_check() { |
8c3e9b00 |
64 | global $dnxcf_options; |
65 | if ( false === $dnxcf_options ) { |
66 | dnxcf_db_init(); |
67 | } |
68 | $old_db_version = $dnxcf_options['dnxcf_DB_VERSION']; |
69 | $new_db_version = DNXCF_CURRENT_DB_VERSION; |
70 | if ( empty( $old_db_version ) ) { |
71 | dnxcf_db_init(); |
72 | } |
73 | if ( intval( $old_db_version ) < intval( $new_db_version ) ) { |
74 | dnxcf_db_update( $new_db_version ); |
75 | } |
b96bc758 |
76 | } |
77 | |
78 | // helper function that sets the current DB Version for comparison |
244ec72c |
79 | /* |
80 | * Latest DB version: 5 |
81 | */ |
b96bc758 |
82 | function dnxcf_set_db_version() { |
8c3e9b00 |
83 | // Define plugin database version. This should only change when new settings are added. |
84 | if ( ! defined( 'DNXCF_CURRENT_DB_VERSION' ) ) { |
244ec72c |
85 | define( 'DNXCF_CURRENT_DB_VERSION', 5 ); |
8c3e9b00 |
86 | } |
b96bc758 |
87 | } |
88 | |
89 | // set the "from" email name to a custom option specified by the user |
90 | function dnxcf_update_from_name() { |
8c3e9b00 |
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'; |
96 | |
97 | $name = ( $orig_name != $from_name ) ? $from_name : false; |
98 | return $name; |
99 | } |
100 | if ( dnxcf_update_from_name() ) { |
101 | add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' ); |
b96bc758 |
102 | } |
b96bc758 |
103 | |
104 | |
105 | // set the "from" email address to a custom option specified by the user |
106 | function dnxcf_update_from_email() { |
8c3e9b00 |
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']; |
112 | |
113 | $mail = ( $orig_mail != $from_mail ) ? $from_mail : false; |
114 | return $mail; |
115 | } |
116 | if ( dnxcf_update_from_email() ) { |
117 | add_filter( 'wp_mail_from', 'dnxcf_update_from_email' ); |
b96bc758 |
118 | } |
b96bc758 |
119 | |
244ec72c |
120 | /* |
121 | * Set the content type for the email |
122 | */ |
b96bc758 |
123 | function dnxcf_update_content_type() { |
8c3e9b00 |
124 | global $dnxcf_options; |
125 | $dnxcf_options = get_option( 'dnxcf_options' ); |
b96bc758 |
126 | |
8c3e9b00 |
127 | // 1 = text/plain |
128 | // 2 = text/html |
129 | $content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html'; |
b96bc758 |
130 | |
8c3e9b00 |
131 | return $content_type; |
b96bc758 |
132 | } |
133 | add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' ); |