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