| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || 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 | // set default options for the plugin |
| 19 | function dnxcf_set_options() { |
| 20 | $defaults = array( |
| 21 | 'dnxcf_pid_key' => dnxcf_get_unique_code( 12 ), |
| 22 | 'dnxcf_recv_name' => 'admin', |
| 23 | 'dnxcf_recv_email' => get_bloginfo( 'admin_email' ), |
| 24 | 'dnxcf_from_email' => 'info@some.url', |
| 25 | 'dnxcf_from_name' => 'webmaster', |
| 26 | 'dnxcf_subject' => array( |
| 27 | __( 'I want to make a comment.', 'dnxcf' ), |
| 28 | __( 'I want to ask a question.', 'dnxcf' ), |
| 29 | __( 'I am interested in a product.', 'dnxcf' ), |
| 30 | __( 'I have to report a problem.', 'dnxcf' ), |
| 31 | __( 'Other (explain below)', 'dnxcf' ), |
| 32 | ), |
| 33 | // 1 = text/plain |
| 34 | // 2 = text/html |
| 35 | 'dnxcf_content_type' => '1', |
| 36 | 'dnxcf_privacy' => '', |
| 37 | 'dnxcf_DB_VERSION' => '2', |
| 38 | ); |
| 39 | return $defaults; |
| 40 | } |
| 41 | |
| 42 | // helper function that starts up the DB |
| 43 | function dnxcf_db_init() { |
| 44 | global $dnxcf_options; |
| 45 | $dnxcf_options = get_option( 'dnxcf_options' ); |
| 46 | if ( false === $dnxcf_options ) { |
| 47 | $dnxcf_options = dnxcf_set_options(); |
| 48 | } |
| 49 | update_option( 'dnxcf_options', $dnxcf_options ); |
| 50 | } |
| 51 | |
| 52 | // helper function that performs a DB version update when needed |
| 53 | function dnxcf_db_update( $db_version ) { |
| 54 | global $dnxcf_options; |
| 55 | $db_defaults = dnxcf_set_options(); |
| 56 | $merge = wp_parse_args( $dnxcf_options, $db_defaults ); |
| 57 | // update DB version |
| 58 | $merge['dnxcf_DB_VERSION'] = $db_version; |
| 59 | update_option( 'dnxcf_options', $merge ); |
| 60 | } |
| 61 | |
| 62 | // helper function that performs a DB check and then an init/update action |
| 63 | function dnxcf_db_check() { |
| 64 | global $dnxcf_options; |
| 65 | if ( false === $dnxcf_options ) { |
| 66 | dnxcf_db_init(); |
| 67 | } |
| 68 | $old_db_version = $dnxcf_options['dnxcf_DB_VERSION']; |
| 69 | $new_db_version = DNXCF_CURRENT_DB_VERSION; |
| 70 | if ( empty( $old_db_version ) ) { |
| 71 | dnxcf_db_init(); |
| 72 | } |
| 73 | if ( intval( $old_db_version ) < intval( $new_db_version ) ) { |
| 74 | dnxcf_db_update( $new_db_version ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // helper function that sets the current DB Version for comparison |
| 79 | /* |
| 80 | * Latest DB version: 5 |
| 81 | */ |
| 82 | function dnxcf_set_db_version() { |
| 83 | // Define plugin database version. This should only change when new settings are added. |
| 84 | if ( ! defined( 'DNXCF_CURRENT_DB_VERSION' ) ) { |
| 85 | define( 'DNXCF_CURRENT_DB_VERSION', 5 ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // set the "from" email name to a custom option specified by the user |
| 90 | function dnxcf_update_from_name() { |
| 91 | global $dnxcf_options; |
| 92 | $dnxcf_options = get_option( 'dnxcf_options' ); |
| 93 | $dnxcf_defaults = dnxcf_set_options(); |
| 94 | $from_name = $dnxcf_options['dnxcf_from_name']; |
| 95 | $orig_name = 'WordPress'; |
| 96 | |
| 97 | $name = ( $orig_name != $from_name ) ? $from_name : false; |
| 98 | return $name; |
| 99 | } |
| 100 | if ( dnxcf_update_from_name() ) { |
| 101 | add_filter( 'wp_mail_from_name', 'dnxcf_update_from_name' ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // set the "from" email address to a custom option specified by the user |
| 106 | function dnxcf_update_from_email() { |
| 107 | global $dnxcf_options; |
| 108 | $dnxcf_options = get_option( 'dnxcf_options' ); |
| 109 | $dnxcf_defaults = dnxcf_set_options(); |
| 110 | $from_mail = $dnxcf_options['dnxcf_from_email']; |
| 111 | $orig_mail = $dnxcf_defaults['dnxcf_from_email']; |
| 112 | |
| 113 | $mail = ( $orig_mail != $from_mail ) ? $from_mail : false; |
| 114 | return $mail; |
| 115 | } |
| 116 | if ( dnxcf_update_from_email() ) { |
| 117 | add_filter( 'wp_mail_from', 'dnxcf_update_from_email' ); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Set the content type for the email |
| 122 | */ |
| 123 | function dnxcf_update_content_type() { |
| 124 | global $dnxcf_options; |
| 125 | $dnxcf_options = get_option( 'dnxcf_options' ); |
| 126 | |
| 127 | // 1 = text/plain |
| 128 | // 2 = text/html |
| 129 | $content_type = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html'; |
| 130 | |
| 131 | return $content_type; |
| 132 | } |
| 133 | add_filter( 'wp_mail_content_type', 'dnxcf_update_content_type' ); |