2 defined( 'ABSPATH' ) or 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 ) );
11 return substr($code, 0, $length);
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>';
26 add_action( 'media_buttons', 'dnxcf_show_form_button', 11 );
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" ) {
35 function dnxcf_send_code() {
36 //Send the shortcode to the editor
37 window
.send_to_editor("[dnx_contactform]");
42 add_action( 'admin_footer', 'dnxcf_insert_shortcode' );
45 // set default options for the plugin
46 function dnxcf_set_options() {
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')
62 'dnxcf_content_type' => '1',
63 'dnxcf_privacy' => '',
64 'dnxcf_latitude' => '38.2704',
65 'dnxcf_longitude' => '16.2971',
67 'dnxcf_gmap_message' => '',
68 'dnxcf_DB_VERSION' => '2'
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();
80 update_option('dnxcf_options', $dnxcf_options);
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 );
89 $merge['dnxcf_DB_VERSION'] = $db_version;
90 update_option('dnxcf_options', $merge);
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) {
99 $old_db_version = $dnxcf_options['dnxcf_DB_VERSION'];
100 $new_db_version = DNXCF_CURRENT_DB_VERSION
;
101 if(empty($old_db_version)) {
104 if( intval($old_db_version) < intval($new_db_version) ) {
105 dnxcf_db_update( $new_db_version );
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 );
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';
125 $name = ( $orig_name != $from_name ) ?
$from_name : false;
128 if (dnxcf_update_from_name())
129 add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' );
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'];
140 $mail = ( $orig_mail != $from_mail ) ?
$from_mail : false;
143 if (dnxcf_update_from_email())
144 add_filter( 'wp_mail_from', 'dnxcf_update_from_email' );
146 function dnxcf_update_content_type() {
147 global $dnxcf_options;
148 $dnxcf_options = get_option('dnxcf_options');
152 $content_type = ( "1" == $dnxcf_options['dnxcf_content_type'] ) ?
'text/plain' : 'text/html';
154 return $content_type;
156 add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' );