b96bc758 |
1 | <?php |
2 | defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
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 | |
18 | // add button in edit pages to help include our form |
19 | function dnxcf_show_form_button() { |
8c3e9b00 |
20 | $currentScreen = get_current_screen(); |
21 | if ( $currentScreen->parent_base == 'edit' ) { |
22 | echo '<button type="button" id="dnxcf-contact-form" class="button" onclick="dnxcf_send_code()"><span class="dashicons dashicons-testimonial"></span> ' . __( 'Add Contact Form', 'dnxcf' ) . '</button>'; |
23 | } |
b96bc758 |
24 | } |
25 | add_action( 'media_buttons', 'dnxcf_show_form_button', 11 ); |
26 | |
27 | // the actual function that outputs our shortcode once the button is pressed |
28 | function dnxcf_insert_shortcode() { |
8c3e9b00 |
29 | $currentScreen = get_current_screen(); |
30 | if ( $currentScreen->parent_base != 'edit' ) { |
31 | return; |
32 | } ?> |
b96bc758 |
33 | <script> |
8c3e9b00 |
34 | function dnxcf_send_code() { |
35 | //Send the shortcode to the editor |
36 | window.send_to_editor("[dnx_contactform]"); |
37 | } |
b96bc758 |
38 | </script> |
8c3e9b00 |
39 | <?php |
b96bc758 |
40 | } |
41 | add_action( 'admin_footer', 'dnxcf_insert_shortcode' ); |
42 | |
43 | |
44 | // set default options for the plugin |
45 | function dnxcf_set_options() { |
8c3e9b00 |
46 | $defaults = array( |
47 | 'dnxcf_pid_key' => dnxcf_get_unique_code( 12 ), |
48 | 'dnxcf_recv_name' => 'admin', |
49 | 'dnxcf_recv_email' => get_bloginfo( 'admin_email' ), |
50 | 'dnxcf_from_email' => 'info@some.url', |
51 | 'dnxcf_from_name' => 'webmaster', |
52 | 'dnxcf_subject' => array( |
53 | __( 'I want to make a comment.', 'dnxcf' ), |
54 | __( 'I want to ask a question.', 'dnxcf' ), |
55 | __( 'I am interested in a product.', 'dnxcf' ), |
56 | __( 'I have to report a problem.', 'dnxcf' ), |
57 | __( 'Other (explain below)', 'dnxcf' ), |
58 | ), |
59 | // 1 = text/plain |
60 | // 2 = text/html |
61 | 'dnxcf_content_type' => '1', |
62 | 'dnxcf_privacy' => '', |
63 | 'dnxcf_latitude' => '38.2704', |
64 | 'dnxcf_longitude' => '16.2971', |
65 | 'dnxcf_apikey' => '', |
66 | 'dnxcf_gmap_message' => '', |
67 | 'dnxcf_DB_VERSION' => '2', |
68 | ); |
69 | return $defaults; |
b96bc758 |
70 | } |
71 | |
72 | // helper function that starts up the DB |
73 | function dnxcf_db_init() { |
8c3e9b00 |
74 | global $dnxcf_options; |
75 | $dnxcf_options = get_option( 'dnxcf_options' ); |
76 | if ( false === $dnxcf_options ) { |
77 | $dnxcf_options = dnxcf_set_options(); |
78 | } |
79 | update_option( 'dnxcf_options', $dnxcf_options ); |
b96bc758 |
80 | } |
81 | |
82 | // helper function that performs a DB version update when needed |
8c3e9b00 |
83 | function dnxcf_db_update( $db_version ) { |
84 | global $dnxcf_options; |
85 | $db_defaults = dnxcf_set_options(); |
86 | $merge = wp_parse_args( $dnxcf_options, $db_defaults ); |
87 | // update DB version |
88 | $merge['dnxcf_DB_VERSION'] = $db_version; |
89 | update_option( 'dnxcf_options', $merge ); |
b96bc758 |
90 | } |
91 | |
92 | // helper function that performs a DB check and then an init/update action |
93 | function dnxcf_db_check() { |
8c3e9b00 |
94 | global $dnxcf_options; |
95 | if ( false === $dnxcf_options ) { |
96 | dnxcf_db_init(); |
97 | } |
98 | $old_db_version = $dnxcf_options['dnxcf_DB_VERSION']; |
99 | $new_db_version = DNXCF_CURRENT_DB_VERSION; |
100 | if ( empty( $old_db_version ) ) { |
101 | dnxcf_db_init(); |
102 | } |
103 | if ( intval( $old_db_version ) < intval( $new_db_version ) ) { |
104 | dnxcf_db_update( $new_db_version ); |
105 | } |
b96bc758 |
106 | } |
107 | |
108 | // helper function that sets the current DB Version for comparison |
109 | function dnxcf_set_db_version() { |
8c3e9b00 |
110 | // Define plugin database version. This should only change when new settings are added. |
111 | if ( ! defined( 'DNXCF_CURRENT_DB_VERSION' ) ) { |
112 | define( 'DNXCF_CURRENT_DB_VERSION', 4 ); |
113 | } |
b96bc758 |
114 | } |
115 | |
116 | // set the "from" email name to a custom option specified by the user |
117 | function dnxcf_update_from_name() { |
8c3e9b00 |
118 | global $dnxcf_options; |
119 | $dnxcf_options = get_option( 'dnxcf_options' ); |
120 | $dnxcf_defaults = dnxcf_set_options(); |
121 | $from_name = $dnxcf_options['dnxcf_from_name']; |
122 | $orig_name = 'WordPress'; |
123 | |
124 | $name = ( $orig_name != $from_name ) ? $from_name : false; |
125 | return $name; |
126 | } |
127 | if ( dnxcf_update_from_name() ) { |
128 | add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' ); |
b96bc758 |
129 | } |
b96bc758 |
130 | |
131 | |
132 | // set the "from" email address to a custom option specified by the user |
133 | function dnxcf_update_from_email() { |
8c3e9b00 |
134 | global $dnxcf_options; |
135 | $dnxcf_options = get_option( 'dnxcf_options' ); |
136 | $dnxcf_defaults = dnxcf_set_options(); |
137 | $from_mail = $dnxcf_options['dnxcf_from_email']; |
138 | $orig_mail = $dnxcf_defaults['dnxcf_from_email']; |
139 | |
140 | $mail = ( $orig_mail != $from_mail ) ? $from_mail : false; |
141 | return $mail; |
142 | } |
143 | if ( dnxcf_update_from_email() ) { |
144 | add_filter( 'wp_mail_from', 'dnxcf_update_from_email' ); |
b96bc758 |
145 | } |
b96bc758 |
146 | |
147 | function dnxcf_update_content_type() { |
8c3e9b00 |
148 | global $dnxcf_options; |
149 | $dnxcf_options = get_option( 'dnxcf_options' ); |
b96bc758 |
150 | |
8c3e9b00 |
151 | // 1 = text/plain |
152 | // 2 = text/html |
153 | $content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html'; |
b96bc758 |
154 | |
8c3e9b00 |
155 | return $content_type; |
b96bc758 |
156 | } |
157 | add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' ); |