Skip to content

Commit

Permalink
feat: update campagin on save, send campaign on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb committed Apr 13, 2020
1 parent eb6ed6f commit c316923
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function __construct() {
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'disable_gradients' ] );
add_action( 'rest_api_init', [ __CLASS__, 'rest_api_init' ] );
add_action( 'publish_' . self::NEWSPACK_NEWSLETTERS_CPT, [ __CLASS__, 'newsletter_published' ], 10, 2 );
add_action( 'save_post_' . self::NEWSPACK_NEWSLETTERS_CPT, [ __CLASS__, 'send_to_mailchimp' ], 10, 2 );
add_action( 'publish_' . self::NEWSPACK_NEWSLETTERS_CPT, [ __CLASS__, 'send_campaign' ], 10, 2 );
add_filter( 'allowed_block_types', [ __CLASS__, 'newsletters_allowed_block_types' ], 10, 2 );
include_once dirname( __FILE__ ) . '/class-newspack-newsletters-settings.php';
include_once dirname( __FILE__ ) . '/class-newspack-newsletters-renderer.php';
Expand Down Expand Up @@ -172,26 +173,6 @@ public static function rest_api_init() {
],
]
);
\register_rest_route(
'newspack-newsletters/v1/',
'mailchimp/(?P<id>[\a-z]+)/send',
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'api_send_mailchimp_campaign' ],
'permission_callback' => [ __CLASS__, 'api_permissions_check' ],
'args' => [
'id' => [
'sanitize_callback' => 'absint',
],
'sender_email' => [
'sanitize_callback' => 'sanitize_email',
],
'sender_name' => [
'sanitize_callback' => 'sanitize_text_field',
],
],
]
);
\register_rest_route(
'newspack-newsletters/v1/',
'mailchimp/(?P<id>[\a-z]+)/list/(?P<list_id>[\a-z]+)',
Expand Down Expand Up @@ -414,12 +395,12 @@ public static function api_permissions_check( $request ) {
}

/**
* Callback for CPT publishing. Will sync with Mailchimp.
* Callback for CPT save. Will sync with Mailchimp.
*
* @param string $id post ID.
* @param WP_Post $post the post.
*/
public static function newsletter_published( $id, $post ) {
public static function send_to_mailchimp( $id, $post ) {
$api_key = self::mailchimp_api_key();
if ( ! $api_key ) {
return;
Expand Down Expand Up @@ -459,5 +440,36 @@ public static function disable_gradients() {
add_theme_support( 'editor-gradient-presets', array() );
add_theme_support( 'disable-custom-gradients' );
}

/**
* Callback for CPT publish. Sends the campaign.
*
* @param string $id post ID.
* @param WP_Post $post the post.
*/
public static function send_campaign( $id, $post ) {
if ( self::NEWSPACK_NEWSLETTERS_CPT !== get_post_type( $id ) ) {
return new WP_Error(
'newspack_newsletters_incorrect_post_type',
__( 'Post is not a Newsletter.', 'newspack-newsletters' )
);
}


$mc_campaign_id = get_post_meta( $id, 'mc_campaign_id', true );
if ( ! $mc_campaign_id ) {
return new WP_Error(
'newspack_newsletters_no_campaign_id',
__( 'Mailchimp campaign ID not found.', 'newspack-newsletters' )
);
}

$mc = new Mailchimp( self::mailchimp_api_key() );

$payload = [
'send_type' => 'html',
];
$result = $mc->post( "campaigns/$mc_campaign_id/actions/send", $payload );
}
}
Newspack_Newsletters::instance();

0 comments on commit c316923

Please sign in to comment.