Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/layout blocks #41

Merged
merged 5 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"no-alert": "off",
"prettier/prettier": "off",
"object-shorthand": "off",
"space-before-function-paren": "warn"
"space-before-function-paren": "warn",
"react-hooks/rules-of-hooks": "off"
}
}
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ No more long **to** or BCCs.
- Marketing Email creator
- Periodical Email(experimental)

### Enter API Key

You need [SendGrid API Key](https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html).

For more details, go to hamail setting screen.

## Installation

### From Plugin Repository

Click install and activate it.
Search "hamail" in WordPress admin screen. Click install and activate it.

### From GitHub

Expand All @@ -47,12 +53,6 @@ composer install
npm run package
</pre>

### Enter API Key

You need [SendGrid API Key](https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html).

For more details, go to hamail setting screen.

## FAQ

### Where can I get supported?
Expand All @@ -61,6 +61,11 @@ To get supported, please go to [Support Forum](https://wordpress.org/support/plu

## Changelog

### 2.6.0

* Add layout block( `&lt;table&gt;` tag) because some email client does not support grid/flex layout.
* Each transaction mail can specify unsubscribe group.

### 2.5.1

* Add filter hook `hamail_transaction_content` to override transactional mail body.
Expand Down
25 changes: 25 additions & 0 deletions app/Hametuha/Hamail/API/TransactionMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Hametuha\Hamail\Pattern\Singleton;
use Hametuha\Hamail\Service\TemplateSelector;
use Hametuha\Hamail\Ui\SettingsScreen;
use Hametuha\Hamail\Utility\ApiUtility;

/**
* Transaction mail API
*/
class TransactionMails extends Singleton {

use ApiUtility;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -73,6 +76,7 @@ public function save_post_id( $post_id, $post ) {
// Send as admin.
if ( wp_verify_nonce( filter_input( INPUT_POST, '_hamailadminnonce' ), 'hamail_as_admin' ) ) {
update_post_meta( $post_id, '_hamail_as_admin', filter_input( INPUT_POST, 'hamail_as_admin' ) );
update_post_meta( $post_id, '_unsubscribe_group', filter_input( INPUT_POST, 'unsubscribe_group' ) );
}
// Save meta data.
if ( wp_verify_nonce( filter_input( INPUT_POST, '_hamail_recipients' ), 'hamail_recipients' ) ) {
Expand Down Expand Up @@ -321,10 +325,31 @@ public function status_meta_box( $post ) {
<?php esc_html_e( 'Send as Site Admin', 'hamail' ); ?>
</label>
<?php endif; ?>
<hr />
<h4><label for="unsubscribe-group"><?php esc_html_e( 'Unsubscribe', 'hamail' ); ?></label></h4>
<select name="unsubscribe_group" id="unsubscribe-group">
<?php
$options = [ '' => __( 'Not Set', 'hamail' ) ];
$group = $this->get_unsubscribe_group();
foreach ( $group as $item ) {
$options[ $item['id'] ] = $item['name'] . ( $item['is_admin'] ? __( 'Default', 'hamail' ) : '' );
}
foreach ( $options as $value => $label ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $value ),
selected( get_post_meta( $post->ID, '_unsubscribe_group', true ), $value, false ),
esc_html( $label )
);
}
?>
</select>
<p class="description"><?php esc_html_e( 'このメールを受け取り拒否するグループを指定できます。', 'hamail' ); ?></p>
<?php
$logs = get_post_meta( $post->ID, '_hamail_log' );
if ( $logs ) :
?>
<hr />
<h4><?php esc_html_e( 'Error Logs', 'hamail' ); ?></h4>
<?php foreach ( $logs as $log ) : ?>
<pre class="hamail-success-log"><?php echo nl2br( esc_html( $log ) ); ?></pre>
Expand Down
16 changes: 14 additions & 2 deletions app/Hametuha/Hamail/Ui/MarketingTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function template_meta_box( $post ) {
<li>
<?php
// translators: %s is {%subject%}.
echo wp_kses_post( sprintf( __( '%s will be replaced with unsubscribing link.', 'hamail' ), '<code>[unsubscribe]</code>' ) );
echo wp_kses_post( sprintf( __( '%s will be replaced with unsubscribing URL. Alternatively, you can use <code>&lt;%%asm_group_unsubscribe_raw_url%%&gt;</code>.', 'hamail' ), '<code>[unsubscribe]</code>' ) );
?>
<span class="required"><?php echo esc_html_x( 'Required', 'Required input element', 'hamail' ); ?></span>
</li>
Expand All @@ -182,6 +182,14 @@ public function template_meta_box( $post ) {
echo wp_kses_post( sprintf( __( '%s will be replaced with excerpt. Use one for pre-header text..', 'hamail' ), '<code>{%excerpt%}</code>' ) );
?>
</li>
<?php
foreach ( [
'<%asm_preferences_raw_url%>' => __( 'Replaced with the users of subscribing list page.', 'hamail' ),
'<%asm_global_unsubscribe_raw_url%>' => __( 'Replaced with global unsubscribe URL. This is optional and not recommended. Use unsubscribe groups.', 'hamail' ),
] as $tag => $desc ) {
printf( '<li><code>%s</code>: %s</li>', esc_html( $tag ), wp_kses_post( $desc ) );
}
?>
</ol>
<textarea id="hamail-template-body" name="template_body" class="code-input"><?php echo esc_textarea( get_post_meta( $post->ID, self::META_KEY_BODY, true ) ); ?></textarea>
<p class="description">
Expand Down Expand Up @@ -397,8 +405,12 @@ public function render_marketing( $post, $format = 'text' ) {
] );
wp_reset_postdata();
$body = apply_filters( 'hamail_marketing_body', $body, $post, $format );
$body = hamail_apply_css_to_body( $body, $format );
// Replace body and subject.
return $this->replace( $string, $subject, $body );
$replaced_body = $this->replace( $string, $subject, $body );

// Replace [unsubscribe] to <%asm_group_unsubscribe_url%>
return str_replace( '[unsubscribe]', '<%asm_group_unsubscribe_raw_url%>', $replaced_body );
}

/**
Expand Down
76 changes: 70 additions & 6 deletions app/Hametuha/Hamail/Ui/SettingsScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Hametuha\Hamail\API\MarketingEmail;
use Hametuha\Hamail\Pattern\Singleton;
use Hametuha\Hamail\Service\TemplateSelector;
use Hametuha\Hamail\Utility\ApiUtility;

/**
* Settings screen.
*/
class SettingsScreen extends Singleton {

use ApiUtility;

/**
* @var string Slug of this screen.
*/
Expand Down Expand Up @@ -191,28 +194,45 @@ public function settings_fields() {
}, 'hamail-setting' );
foreach (
[
'hamail_api_key' => [
'hamail_api_key' => [
__( 'SendGrid API key', 'hamail' ),
'',
'',
],
'hamail_default_from' => [
'hamail_default_from' => [
__( 'Default Mail From', 'hamail' ),
'',
get_option( 'admin_email' ),
],
'hamail_keep_wp_mail' => [
'hamail_keep_wp_mail' => [
__( 'wp_mail function', 'hamail' ),
__( 'Hamail can override all mail sent with <code>wp_mail</code> function. SMTP API works fine with other plugins that send emails(e.g. WooCommerce or Contact Form 7).', 'hamail' ),
'',
],
TemplateSelector::OPTION_KEY => [
TemplateSelector::OPTION_KEY => [
__( 'Template ID', 'hamail' ),
sprintf(
// translators: %s document URL.
__( 'If Template ID is set, all your default mail will be HTML. For more detail, see <a href="%1$s" target="_blank">SendGrid API doc</a>. This feature work with legacy templates. If you have none, create one via <a href="%2$s" target="_blank" rel="noopener noreferrer">SendGrind Legacy Templates</a>.', 'hamail' ),
__( 'Default template is used when you choose "%3$s". For more detail, see <a href="%1$s" target="_blank">SendGrid API doc</a>. This feature work only with legacy templates. If you have none, create one via <a href="%2$s" target="_blank" rel="noopener noreferrer">SendGrind Legacy Templates</a>.', 'hamail' ),
'https://sendgrid.com/docs/Glossary/transactional_email_templates.html',
'https://sendgrid.com/templates'
'https://sendgrid.com/templates',
esc_html__( 'Override with Template', 'hamail' )
),
'',
],
'hamail_default_unsubscribe_group' => [
__( 'Default Unsubscribe Group', 'hamail' ),
sprintf(
__( 'You can choose default unsubscribe group. This affects <code>%s</code>.', 'hamail' ),
esc_html( '<%asm_group_unsubscribe_url%>' )
),
'',
],
'hamail_unsubscribe_group' => [
__( 'Unsubscribe Group', 'hamail' ),
sprintf(
__( 'You can choose unsubscribe group to display if you set <code>%s</code> in your email. This helps in case you have multiple list in your SendGrid account.', 'hamail' ),
esc_html( '<%asm_preferences_url%>' )
),
'',
],
Expand Down Expand Up @@ -274,6 +294,50 @@ function ( $option ) use ( $current ) {
);
$input = sprintf( '<select name="%s">%s</select>', esc_attr( $key ), implode( ' ', $options ) );
break;
case 'hamail_default_unsubscribe_group':
case 'hamail_unsubscribe_group':
if ( ! hamail_enabled() ) {
// No API key, display just message.
$type = 'hidden';
$message .= __( 'No API key is set.', 'hamail' );
} else {
$groups = $this->get_unsubscribe_group( true );
if ( is_wp_error( $groups ) ) {
$message = $groups->get_error_message();
$type = 'hidden';
} else {
switch ( $key ) {
case 'hamail_default_unsubscribe_group':
$input .= sprintf( '<select name="%s"><option value="">%s</option>', esc_html( $key ), esc_html__( 'Not Set', 'hamail' ) );
foreach ( $groups as $group ) {
$input .= sprintf(
'<option value="%s" %s>%s</option>',
esc_attr( $group['id'] ),
selected( (string) $group['id'], get_option( 'hamail_default_unsubscribe_group' ), false ),
esc_html( $group['name'] ),
);
}
$input .= '</select>';
break;
case 'hamail_unsubscribe_group':
foreach ( $groups as $group ) {
$name = $group['name'];
if ( $group['is_default'] ) {
$name .= __( '(Default)', 'hamail' );
}
$input .= sprintf(
'<label style="display: block; margin: 1em 0;"><input type="checkbox" name="hamail_unsubscribe_group[]" value="%s" %s/> %s<br /><span class="description">%s</span></label>',
esc_attr( $group['id'] ),
checked( in_array( (string) $group['id'], get_option( 'hamail_unsubscribe_group', [] ), true ), true, false ),
esc_html( $name ),
esc_html( $group['description'] )
);
}
break;
}
}
}
break;
}
if ( ! $input ) {
$input = sprintf(
Expand Down
21 changes: 19 additions & 2 deletions functions/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ function hamail_get_recipients_data( $recipients, $subject = '', $body = '' ) {
* @param string $body
* @param array $additional_headers
* @param array $attachments
* @param int $asm_id If set, unsubscribe group will be this.
*
* @return bool|WP_Error
*/
function hamail_simple_mail( $recipients, $subject, $body, $additional_headers = [], $attachments = [] ) {
function hamail_simple_mail( $recipients, $subject, $body, $additional_headers = [], $attachments = [], $asm_id = 0 ) {
// Parse recipients.
$recipients = (array) $recipients;
$recipient_data = hamail_get_recipients_data( $recipients, $subject, $body );
Expand Down Expand Up @@ -457,6 +458,15 @@ function hamail_simple_mail( $recipients, $subject, $body, $additional_headers =
$mail->setSubject( $subject );
$mail->setReplyTo( $reply_to );
$mail->addContent( $content );
// Set unsubscribe group.
$asm_id = $asm_id ?: get_option( 'hamail_default_unsubscribe_group', 0 );
if ( $asm_id ) {
$groups = array_map( 'intval', (array) get_option( 'hamail_unsubscribe_group', [] ) );
if ( empty( $groups ) ) {
$groups = null;
}
$mail->setAsm( (int) $asm_id, $groups );
}
// Add attachments.
if ( ! empty( $mail_attachments ) ) {
$mail->addAttachments( $mail_attachments );
Expand Down Expand Up @@ -628,8 +638,12 @@ function hamail_send_message( $post = null, $force = false ) {
if ( empty( $to ) ) {
return false;
}
// Get unsubscribe group.
$unsubscribe = get_post_meta( $post->ID, '_unsubscribe_group', true ) ?: 0;
// Attachments.
$attachments = apply_filters( 'hamail_transaction_attachments', [], $post );
// Send.
$result = hamail_simple_mail( $to, $subject, $body, $headers );
$result = hamail_simple_mail( $to, $subject, $body, $headers, $attachments, (int) $unsubscribe );
if ( is_wp_error( $result ) ) {
$message = sprintf( '[Error] %s: %s', $result->get_error_code(), current_time( 'mysql' ) ) . "\n";
foreach ( $result->get_error_messages() as $err_message ) {
Expand Down Expand Up @@ -684,6 +698,9 @@ function hamail_is_sending( $flag = null ) {
*/
function hamail_get_mail_css() {
$css_path = [];
// Default style of plugin.
$css_path[] = plugin_dir_path( __DIR__ ) . 'assets/css/hamail-mail-layout.css';
// Theme css.
foreach ( [ get_template_directory(), get_stylesheet_directory() ] as $dir ) {
$css = $dir . '/hamail.css';
if ( file_exists( $css ) ) {
Expand Down
20 changes: 20 additions & 0 deletions hooks/style.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,37 @@ function hamail_allowed_block_types( $allowed_blocks, $editor_context ) {
return [
'core/paragraph',
'core/list',
'core/list-item',
'core/heading',
'core/image',
'core/spacer',
'core/quote',
'core/separator',
'core/shortcode',
'core/html',
'core/buttons',
'core/button',
'core/group',
'hamail/col',
'hamail/row',
'hamail/table',
];
default:
return $allowed_blocks;
}
}
add_filter( 'allowed_block_types_all', 'hamail_allowed_block_types', 10, 2 );

/**
* Register block.
*
* @return void
*/
function hamail_register_block() {
register_block_type( 'hamail/table', [
'editor_script_handles' => [ 'hamail-block-table' ],
'editor_style_handles' => [ 'hamail-block-table' ],
'style_handles' => [ 'hamail-block-table-view' ],
] );
}
add_action( 'init', 'hamail_register_block' );
Loading
Loading