|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Plugin Name: WPGraphql Send Mail |
| 5 | + * Plugin URI: https://github.com/ashhitch/wp-graphql-send-mail |
| 6 | + * Description: A WPGraphQL Extension that adds support for Sending Mail vi a mutation |
| 7 | + * Author: Ash Hitchcock |
| 8 | + * Author URI: https://www.ashleyhitchcock.com |
| 9 | + * Text Domain: wp-graphql-send-mail |
| 10 | + * Domain Path: /languages |
| 11 | + * Version: 0.0.1 |
| 12 | + * |
| 13 | + * @package WP_Graphql_SEND_MAIL |
| 14 | + */ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +add_action('admin_menu', 'wpgraphql_send_mail_add_admin_menu'); |
| 19 | +add_action('admin_init', 'wpgraphql_send_mail_settings_init'); |
| 20 | + |
| 21 | +function wpgraphql_send_mail_add_admin_menu() |
| 22 | +{ |
| 23 | + add_options_page('WpGraphQl Send Mail Settings', 'WpGraphQl Mail', 'manage_options', 'wpgraphql-send-mail-page', 'wpgraphql_send_mail_options_page'); |
| 24 | +} |
| 25 | + |
| 26 | +function wpgraphql_send_mail_settings_init() |
| 27 | +{ |
| 28 | + register_setting('wsmPlugin', 'wpgraphql_send_mail_settings'); |
| 29 | + add_settings_section( |
| 30 | + 'wpgraphql_send_mail_wsmPlugin_section', |
| 31 | + __('Security', 'wp-graphql-send-mail'), |
| 32 | + 'wpgraphql_send_mail_settings_section_callback', |
| 33 | + 'wsmPlugin' |
| 34 | + ); |
| 35 | + |
| 36 | + add_settings_field( |
| 37 | + 'wpgraphql_send_mail_allowed_origins', |
| 38 | + __('Allowed Origins', 'wp-graphql-send-mail'), |
| 39 | + 'wpgraphql_send_mail_origins_textarea_render', |
| 40 | + 'wsmPlugin', |
| 41 | + 'wpgraphql_send_mail_wsmPlugin_section' |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function wpgraphql_send_mail_origins_textarea_render() |
| 46 | +{ |
| 47 | + $options = get_option('wpgraphql_send_mail_settings'); |
| 48 | +?> |
| 49 | + <textarea rows="6" name='wpgraphql_send_mail_settings[wpgraphql_send_mail_allowed_origins]'><?php echo $options['wpgraphql_send_mail_allowed_origins']; ?></textarea> |
| 50 | +<?php |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +function wpgraphql_send_mail_settings_section_callback() |
| 55 | +{ |
| 56 | + echo __('Enter a comma separated list of domains that can sent emails.', 'wp-graphql-send-mail'); |
| 57 | +} |
| 58 | + |
| 59 | +function wpgraphql_send_mail_options_page() |
| 60 | +{ |
| 61 | +?> |
| 62 | + <form action='options.php' method='post'> |
| 63 | + |
| 64 | + <h2>WPGraphQl Send Mail Settings</h2> |
| 65 | + |
| 66 | + <?php |
| 67 | + settings_fields('wsmPlugin'); |
| 68 | + do_settings_sections('wsmPlugin'); |
| 69 | + submit_button(); |
| 70 | + ?> |
| 71 | + |
| 72 | + </form> |
| 73 | +<?php |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +# This is the action that is executed as the GraphQL Schema is being built |
| 78 | +add_action('graphql_register_types', function () { |
| 79 | + |
| 80 | + # This function registers a mutation to the Schema. |
| 81 | + # The first argument, in this case `emailMutation`, is the name of the mutation in the Schema |
| 82 | + # The second argument is an array to configure the mutation. |
| 83 | + # The config array accepts 3 key/value pairs for: inputFields, outputFields and mutateAndGetPayload. |
| 84 | + register_graphql_mutation('sendEmail', [ |
| 85 | + |
| 86 | + # inputFields expects an array of Fields to be used for inputting values to the mutation |
| 87 | + 'inputFields' => [ |
| 88 | + 'to' => [ |
| 89 | + 'type' => 'String', |
| 90 | + 'description' => __('Who to send the email to', 'wp-graphql-send-mail'), |
| 91 | + ], |
| 92 | + 'subject' => [ |
| 93 | + 'type' => 'String', |
| 94 | + 'description' => __('Subject of email', 'wp-graphql-send-mail'), |
| 95 | + ], |
| 96 | + 'body' => [ |
| 97 | + 'type' => 'String', |
| 98 | + 'description' => __('Body of email', 'wp-graphql-send-mail'), |
| 99 | + ], |
| 100 | + ], |
| 101 | + |
| 102 | + # outputFields expects an array of fields that can be asked for in response to the mutation |
| 103 | + # the resolve function is optional, but can be useful if the mutateAndPayload doesn't return an array |
| 104 | + # with the same key(s) as the outputFields |
| 105 | + 'outputFields' => [ |
| 106 | + 'sent' => [ |
| 107 | + 'type' => 'Boolean', |
| 108 | + 'description' => __('Was the email sent', 'wp-graphql-send-mail'), |
| 109 | + 'resolve' => function ($payload, $args, $context, $info) { |
| 110 | + return isset($payload['sent']) ? $payload['sent'] : null; |
| 111 | + } |
| 112 | + ], |
| 113 | + 'origin' => [ |
| 114 | + 'type' => 'String', |
| 115 | + 'description' => __('Origin that sent the request', 'wp-graphql-send-mail'), |
| 116 | + 'resolve' => function ($payload, $args, $context, $info) { |
| 117 | + return isset($payload['origin']) ? $payload['origin'] : null; |
| 118 | + } |
| 119 | + ], |
| 120 | + 'message' => [ |
| 121 | + 'type' => 'String', |
| 122 | + 'description' => __('Message', 'wp-graphql-send-mail'), |
| 123 | + 'resolve' => function ($payload, $args, $context, $info) { |
| 124 | + return isset($payload['message']) ? $payload['message'] : null; |
| 125 | + } |
| 126 | + ] |
| 127 | + ], |
| 128 | + |
| 129 | + # mutateAndGetPayload expects a function, and the function gets passed the $input, $context, and $info |
| 130 | + # the function should return enough info for the outputFields to resolve with |
| 131 | + 'mutateAndGetPayload' => function ($input, $context, $info) { |
| 132 | + |
| 133 | + // Do any logic here to sanitize the input, check user capabilities, etc |
| 134 | + $options = get_option('wpgraphql_send_mail_settings'); |
| 135 | + $allowedOrigins = array_map('trim', explode(',', $options['wpgraphql_send_mail_allowed_origins'])); |
| 136 | + $http_origin = $_SERVER['HTTP_ORIGIN']; |
| 137 | + $message = null; |
| 138 | + $canSend = false; |
| 139 | + |
| 140 | + if ($allowedOrigins) { |
| 141 | + if (in_array($http_origin, $allowedOrigins)) { |
| 142 | + $canSend = true; |
| 143 | + } else { |
| 144 | + $message = __('Origin not allowed', 'wp-graphql-send-mail'); |
| 145 | + } |
| 146 | + } else { |
| 147 | + // if they did not enter any then we will allow any |
| 148 | + $canSend = true; |
| 149 | + } |
| 150 | + |
| 151 | + if ($canSend && !empty($input['to']) && !empty($input['body'])) { |
| 152 | + |
| 153 | + |
| 154 | + $to = $input['to']; |
| 155 | + $subject = $input['subject']; |
| 156 | + $body = $input['body']; |
| 157 | + $headers = array('Content-Type: text/html; charset=UTF-8'); |
| 158 | + |
| 159 | + $sent = wp_mail($to, $subject, $body, $headers); |
| 160 | + $message = $sent ? __('Email Sent', 'wp-graphql-send-mail') : __('Email Not Sent', 'wp-graphql-send-mail'); |
| 161 | + } else { |
| 162 | + $sent = false; |
| 163 | + $message = $message ? $message : __('Email Not Sent', 'wp-graphql-send-mail'); |
| 164 | + } |
| 165 | + return [ |
| 166 | + 'sent' => $sent, |
| 167 | + 'origin' => $http_origin, |
| 168 | + 'message' => $message, |
| 169 | + ]; |
| 170 | + } |
| 171 | + ]); |
| 172 | +}); |
0 commit comments