Skip to content

Commit

Permalink
feat: move MJML creds to plugin settings
Browse files Browse the repository at this point in the history
Closes #16.
  • Loading branch information
adekbadek committed Apr 15, 2020
1 parent dc6bbc9 commit 06840ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# newspack-newsletters
Author email newsletters in WordPress

## Setup

Copy your Mailchimp API key, which can be found in Mailchimp in `Account->Extras->API Keys`.
Request [MJML API access](https://mjml.io/api).

Navigate to `Settings->Newspack Newsletters`. Input Mailchimp API key and MJML API key and secret.

## Use

Copy your Mailchimp API key, which can be found in Mailchimp in `Account->Extras->API Keys`. Navigate to `Settings->Newspack Newsletters`. Input Mailchimp API key. Click Newsletters in the left menu. Create a new one.
Click Newsletters in the left menu. Create a new one.

## Development

Run `composer update && npm install`.

Run `npm run build`.

#### Environment variables

This feature requires environment variables to be set (e.g. in `wp-config.php`):

```php
define( 'NEWSPACK_MJML_API_KEY', 'abc1' );
define( 'NEWSPACK_MJML_API_SECRET', 'abc1' );
```
4 changes: 2 additions & 2 deletions includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ private static function render_mjml( $post ) {
* @return string API key and API secret as a key:secret string.
*/
public static function mjml_api_credentials() {
$key = ( defined( 'NEWSPACK_MJML_API_KEY' ) && NEWSPACK_MJML_API_KEY ) ? NEWSPACK_MJML_API_KEY : false;
$secret = ( defined( 'NEWSPACK_MJML_API_SECRET' ) && NEWSPACK_MJML_API_SECRET ) ? NEWSPACK_MJML_API_SECRET : false;
$key = get_option( 'newspack_newsletters_mjml_api_key', false );
$secret = get_option( 'newspack_newsletters_mjml_api_secret', false );
if ( isset( $key, $secret ) ) {
return "$key:$secret";
}
Expand Down
62 changes: 45 additions & 17 deletions includes/class-newspack-newsletters-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ public static function init() {
add_action( 'admin_init', [ __CLASS__, 'page_init' ] );
}

/**
* Retreives list of settings.
*
* @return array Settings list.
*/
public static function get_settings_list() {
return array(
array(
'description' => __( 'Mailchimp API Key', 'newspack' ),
'key' => 'newspack_newsletters_mailchimp_api_key',
),
array(
'description' => __( 'MJML API Key', 'newspack' ),
'key' => 'newspack_newsletters_mjml_api_key',
),
array(
'description' => __( 'MJML API Secret', 'newspack' ),
'key' => 'newspack_newsletters_mjml_api_secret',
),
);
}

/**
* Add options page
*/
Expand All @@ -36,7 +58,6 @@ public static function add_plugin_page() {
* Options page callback
*/
public static function create_admin_page() {
$newspack_newsletters_mailchimp_api_key = get_option( 'newspack_newsletters_mailchimp_api_key' );
?>
<div class="wrap">
<h1>Newspack Newsletters Settings</h1>
Expand All @@ -55,33 +76,40 @@ public static function create_admin_page() {
* Register and add settings
*/
public static function page_init() {
register_setting(
'newspack_newsletters_options_group',
'newspack_newsletters_mailchimp_api_key'
);
add_settings_section(
'newspack_newsletters_options_group',
'Newspack Newsletters Custom Settings',
null,
'newspack-newsletters-settings-admin'
);
add_settings_field(
'newspack_newsletters_mailchimp_api_key',
__( 'Mailchimp API Key', 'newspack' ),
[ __CLASS__, 'newspack_newsletters_mailchimp_api_key_callback' ],
'newspack-newsletters-settings-admin',
'newspack_newsletters_options_group'
);
foreach ( self::get_settings_list() as $setting ) {
register_setting(
'newspack_newsletters_options_group',
$setting['key']
);
add_settings_field(
$setting['key'],
$setting['description'],
[ __CLASS__, 'newspack_newsletters_settings_callback' ],
'newspack-newsletters-settings-admin',
'newspack_newsletters_options_group',
$setting['key']
);
};
}

/**
* Render Mailchimp API field.
* Render settings fields.
*
* @param string $key Setting key.
*/
public static function newspack_newsletters_mailchimp_api_key_callback() {
$newspack_newsletters_mailchimp_api_key = get_option( 'newspack_newsletters_mailchimp_api_key', false );
public static function newspack_newsletters_settings_callback( $key ) {
$value = get_option( $key, false );
printf(
'<input type="text" id="newspack_newsletters_mailchimp_api_key" name="newspack_newsletters_mailchimp_api_key" value="%s" />',
esc_attr( $newspack_newsletters_mailchimp_api_key )
'<input type="text" id="%s" name="%s" value="%s" class="widefat" />',
esc_attr( $key ),
esc_attr( $key ),
esc_attr( $value )
);
}
}
Expand Down

0 comments on commit 06840ad

Please sign in to comment.