summaryrefslogtreecommitdiffstats
path: root/include/dnxcf_helper.php
blob: 50c37398a083423cd479153f62f55051ec699df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );

// generate a unique code, allows for lenght parameter
if ( ! function_exists( 'dnxcf_get_unique_code' ) ) {

	function dnxcf_get_unique_code( $length = '' ) {
		$code = md5( uniqid( rand(), true ) );

		if ( $length != '' ) {
			return substr( $code, 0, $length );
		} else {
			return $code;
		}
	}
}

// set default options for the plugin
function dnxcf_set_options() {
	$defaults = array(
		'dnxcf_pid_key'         => dnxcf_get_unique_code( 12 ),
		'dnxcf_recv_name'       => 'admin',
		'dnxcf_recv_email'      => get_bloginfo( 'admin_email' ),
		'dnxcf_from_email'      => 'info@some.url',
		'dnxcf_from_name'       => 'webmaster',
		'dnxcf_subject'         => array(
			__( 'I want to make a comment.', 'dnxcf' ),
			__( 'I want to ask a question.', 'dnxcf' ),
			__( 'I am interested in a product.', 'dnxcf' ),
			__( 'I have to report a problem.', 'dnxcf' ),
			__( 'Other (explain below)', 'dnxcf' ),
		),
		// 1 = text/plain
		// 2 = text/html
		'dnxcf_content_type'    => '1',
		'dnxcf_privacy'         => '',
		'dnxcf_DB_VERSION'      => '2',
	);
	return $defaults;
}

// helper function that starts up the DB
function dnxcf_db_init() {
	global $dnxcf_options;
	$dnxcf_options = get_option( 'dnxcf_options' );
	if ( false === $dnxcf_options ) {
		$dnxcf_options = dnxcf_set_options();
	}
	update_option( 'dnxcf_options', $dnxcf_options );
}

// helper function that performs a DB version update when needed
function dnxcf_db_update( $db_version ) {
	global $dnxcf_options;
	$db_defaults = dnxcf_set_options();
	$merge = wp_parse_args( $dnxcf_options, $db_defaults );
	// update DB version
	$merge['dnxcf_DB_VERSION'] = $db_version;
	update_option( 'dnxcf_options', $merge );
}

// helper function that performs a DB check and then an init/update action
function dnxcf_db_check() {
	global $dnxcf_options;
	if ( false === $dnxcf_options ) {
		dnxcf_db_init();
	}
	$old_db_version = $dnxcf_options['dnxcf_DB_VERSION'];
	$new_db_version = DNXCF_CURRENT_DB_VERSION;
	if ( empty( $old_db_version ) ) {
		dnxcf_db_init();
	}
	if ( intval( $old_db_version ) < intval( $new_db_version ) ) {
		dnxcf_db_update( $new_db_version );
	}
}

// helper function that sets the current DB Version for comparison
/*
 * Latest DB version: 5
*/
function dnxcf_set_db_version() {
	// Define plugin database version. This should only change when new settings are added.
	if ( ! defined( 'DNXCF_CURRENT_DB_VERSION' ) ) {
		define( 'DNXCF_CURRENT_DB_VERSION', 5 );
	}
}

// set the "from" email name to a custom option specified by the user
function dnxcf_update_from_name() {
	global $dnxcf_options;
	$dnxcf_options = get_option( 'dnxcf_options' );
	$dnxcf_defaults = dnxcf_set_options();
	$from_name = $dnxcf_options['dnxcf_from_name'];
	$orig_name = 'WordPress';

	$name = ( $orig_name != $from_name ) ? $from_name : false;
	return $name;
}
if ( dnxcf_update_from_name() ) {
	add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' );
}


// set the "from" email address to a custom option specified by the user
function dnxcf_update_from_email() {
	global $dnxcf_options;
	$dnxcf_options = get_option( 'dnxcf_options' );
	$dnxcf_defaults = dnxcf_set_options();
	$from_mail = $dnxcf_options['dnxcf_from_email'];
	$orig_mail = $dnxcf_defaults['dnxcf_from_email'];

	$mail = ( $orig_mail != $from_mail ) ? $from_mail : false;
	return $mail;
}
if ( dnxcf_update_from_email() ) {
	add_filter( 'wp_mail_from', 'dnxcf_update_from_email' );
}

/*
 * Set the content type for the email
*/
function dnxcf_update_content_type() {
	global $dnxcf_options;
	$dnxcf_options = get_option( 'dnxcf_options' );

	// 1 = text/plain
	// 2 = text/html
	$content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html';

	return $content_type;
}
add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' );