Skip to content

Commit 2c24701

Browse files
committed
Inital Commit
0 parents  commit 2c24701

File tree

6 files changed

+913
-0
lines changed

6 files changed

+913
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
Thumbs.db
3+
wp-cli.local.yml
4+
node_modules/
5+
*.sql
6+
*.tar.gz
7+
*.zip
8+
.idea*

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.0.1] - 2020-05-04
9+
10+
Initial Release

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WPGraphQl Send Email Plugin
2+
3+
One of the simple things about a traditional WordPress sites is sending emails, this plugin makes it easy to do this via a simple mutation when you are using WpGraphQl.
4+
5+
## Usage
6+
7+
```
8+
mutation SEND_EMAIL {
9+
sendEmail(
10+
input: {
11+
12+
subject: "test email"
13+
body: "test email"
14+
clientMutationId: "test"
15+
}
16+
) {
17+
origin
18+
sent
19+
message
20+
}
21+
}
22+
23+
```
24+
25+
To try stop unauthorised emails you can set a list of domains that can send emails through the mutation.
26+
27+
These are set under `Settings > WpGraphQl Mail` from your WordPress Admin
28+
29+
## Support
30+
31+
[Open an issue](https://github.com/ashhitch/wp-graphql-send-mail/issues)
32+
33+
## Other Plugins
34+
35+
Want to get Yoast data via WpGraphQl? [Checkout my other plugin](https://github.com/ashhitch/wp-graphql-yoast-seo)

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "ashhitch/wp-graphql-yoast-seo",
3+
"description": "Query Yoast SEO data with wp-graphql",
4+
"type": "wordpress-plugin",
5+
"license": "GPL-3.0-or-later",
6+
"authors": [{
7+
"name": "Ash Hitchcock",
8+
"email": "[email protected]"
9+
}],
10+
"require": {
11+
"wp-graphql/wp-graphql": ">=0.3.8",
12+
"yoast/wordpress-seo": ">=14.0"
13+
}
14+
}

wp-graphql-send-mail.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)