d6b4aa0ea7f6893d796a48d0b525b42756352bf0
[danixland-contact-form.git] / include / dnxcf_helper.php
1 <?php
2 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
3
4 // generate a unique code, allows for lenght parameter
5 if ( ! function_exists( 'dnxcf_get_unique_code' ) ) {
6
7 function dnxcf_get_unique_code( $length = '' ) {
8 $code = md5( uniqid( rand(), true ) );
9
10 if ( $length != '' ) {
11 return substr( $code, 0, $length );
12 } else {
13 return $code;
14 }
15 }
16 }
17
18 // add button in edit pages to help include our form
19 function dnxcf_show_form_button() {
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 }
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() {
29 $currentScreen = get_current_screen();
30 if ( $currentScreen->parent_base != 'edit' ) {
31 return;
32 } ?>
33 <script>
34 function dnxcf_send_code() {
35 //Send the shortcode to the editor
36 window.send_to_editor("[dnx_contactform]");
37 }
38 </script>
39 <?php
40 }
41 add_action( 'admin_footer', 'dnxcf_insert_shortcode' );
42
43
44 // set default options for the plugin
45 function dnxcf_set_options() {
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;
70 }
71
72 // helper function that starts up the DB
73 function dnxcf_db_init() {
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 );
80 }
81
82 // helper function that performs a DB version update when needed
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 );
90 }
91
92 // helper function that performs a DB check and then an init/update action
93 function dnxcf_db_check() {
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 }
106 }
107
108 // helper function that sets the current DB Version for comparison
109 function dnxcf_set_db_version() {
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 }
114 }
115
116 // set the "from" email name to a custom option specified by the user
117 function dnxcf_update_from_name() {
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' );
129 }
130
131
132 // set the "from" email address to a custom option specified by the user
133 function dnxcf_update_from_email() {
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' );
145 }
146
147 function dnxcf_update_content_type() {
148 global $dnxcf_options;
149 $dnxcf_options = get_option( 'dnxcf_options' );
150
151 // 1 = text/plain
152 // 2 = text/html
153 $content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html';
154
155 return $content_type;
156 }
157 add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' );