+
+ wp_newsletter_builder_selected_provider'
+ );
+ ?>
+
+
+ setup();
}
main();
diff --git a/plugins/newsletter-from-post/index.php b/plugins/newsletter-from-post/index.php
index 630ad1e5..9d7df23f 100644
--- a/plugins/newsletter-from-post/index.php
+++ b/plugins/newsletter-from-post/index.php
@@ -38,12 +38,29 @@ function action_enqueue_post_sidebar_assets() {
if ( 'post' !== $post_type ) {
return;
}
+ $settings = new Settings();
+
+ $templates = get_posts( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
+ [
+ 'post_type' => 'nb_template',
+ 'posts_per_page' => -1,
+ 'orderby' => 'ID',
+ 'suppress_filters' => false,
+ ]
+ );
+ $template_map = [];
+
+ foreach ( $templates as $template ) {
+ $template_map[ $template->ID ] = $template->post_title;
+ }
+
wp_enqueue_script( 'plugin-newsletter-from-post' );
wp_localize_script(
'plugin-newsletter-from-post',
'newsletterBuilder',
[
- 'fromNames' => Campaign_Monitor_Client::instance()->get_from_names(),
+ 'fromNames' => $settings->get_from_names(),
+ 'templates' => $template_map,
'breakingLists' => ( new Breaking_Recipients() )->get_breaking_recipients(),
]
);
diff --git a/src/assets.php b/src/assets.php
index 6094c5b3..47d69770 100644
--- a/src/assets.php
+++ b/src/assets.php
@@ -125,11 +125,12 @@ function action_enqueue_block_editor_assets() {
get_asset_dependency_array( 'editor' ),
get_asset_version( 'editor' )
);
+ $settings = new Settings();
wp_localize_script(
'wp-newsletter-builder-email-settings-editor-script',
'newsletterBuilder',
[
- 'fromNames' => Campaign_Monitor_Client::instance()->get_from_names(),
+ 'fromNames' => $settings->get_from_names(),
'templates' => $template_map,
]
);
diff --git a/src/class-breaking-recipients.php b/src/class-breaking-recipients.php
index ffb6e7a8..1636aa15 100644
--- a/src/class-breaking-recipients.php
+++ b/src/class-breaking-recipients.php
@@ -93,7 +93,8 @@ public function get_breaking_recipients() {
* @return array
*/
private function get_options() {
- $lists = Campaign_Monitor_Client::instance()->get_lists();
+ global $newsletter_builder_email_provider;
+ $lists = $newsletter_builder_email_provider->get_lists();
$options = [];
foreach ( $lists as $list ) {
$options[ $list->ListID ] = $list->Name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
diff --git a/src/class-email-types.php b/src/class-email-types.php
index 161313c8..f9677f9a 100644
--- a/src/class-email-types.php
+++ b/src/class-email-types.php
@@ -46,8 +46,9 @@ public function maybe_register_settings_page() {
* @return void
*/
public function register_fields() {
- $from_names = Campaign_Monitor_Client::instance()->get_from_names();
- $settings = new \Fieldmanager_Group(
+ $plugin_settings = new Settings();
+ $from_names = $plugin_settings->get_from_names();
+ $settings = new \Fieldmanager_Group(
[
'name' => static::SETTINGS_KEY,
'children' => [
diff --git a/src/class-rest-api-endpoints.php b/src/class-rest-api-endpoints.php
index 822c3117..f8fc5d17 100644
--- a/src/class-rest-api-endpoints.php
+++ b/src/class-rest-api-endpoints.php
@@ -90,7 +90,8 @@ public function get_lists() {
if ( ! current_user_can( 'edit_posts' ) ) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
}
- $lists = Campaign_Monitor_Client::instance()->get_lists();
+ global $newsletter_builder_email_provider;
+ $lists = $newsletter_builder_email_provider->get_lists();
return $lists;
}
@@ -123,7 +124,8 @@ public function get_footer_settings() {
if ( ! current_user_can( 'edit_posts' ) ) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
}
- $footer_settings = Campaign_Monitor_Client::instance()->get_footer_settings();
+ $settings = new Settings();
+ $footer_settings = $settings->get_footer_settings();
return $footer_settings;
}
@@ -155,7 +157,8 @@ public function get_status( $request ) {
'Status' => __( 'Not sent', 'wp-newsletter-builder' ),
];
}
- $status = Campaign_Monitor_Client::instance()->get_campaign_summary( $campaign_id );
+ global $newsletter_builder_email_provider;
+ $status = $newsletter_builder_email_provider->get_campaign_summary( $campaign_id );
if ( ! empty( $status ) && 200 === $status['http_status_code'] ) {
$status['response']->Status = __( 'Sent' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
wp_cache_set( $cache_key, $status['response'], null, 5 * MINUTE_IN_SECONDS );
@@ -209,8 +212,9 @@ public function subscribe( $request ) {
];
}
$list_results = [];
+ global $newsletter_builder_email_provider;
foreach ( $list_ids as $list_id ) {
- $result = Campaign_Monitor_Client::instance()->add_subscriber( $list_id, $email );
+ $result = $newsletter_builder_email_provider->add_subscriber( $list_id, $email );
if ( ! empty( $result ) && 200 === $result['http_status_code'] ) {
$list_results[ $list_id ] = [
'success' => true,
diff --git a/src/class-settings.php b/src/class-settings.php
new file mode 100644
index 00000000..7cb91c50
--- /dev/null
+++ b/src/class-settings.php
@@ -0,0 +1,176 @@
+ static::SETTINGS_KEY,
+ 'children' => [
+ 'from_email' => new \Fieldmanager_TextField( __( 'From Email', 'wp-newsletter-builder' ) ),
+ 'reply_to_email' => new \Fieldmanager_TextField( __( 'Reply To Email', 'wp-newsletter-builder' ) ),
+ 'from_names' => new \Fieldmanager_TextField(
+ [
+ 'label' => __( 'From Names', 'wp-newsletter-builder' ),
+ 'limit' => 0,
+ 'add_more_label' => __( 'Add From Name', 'wp-newsletter-builder' ),
+ 'one_label_per_item' => false,
+ ]
+ ),
+ 'footer_settings' => new \Fieldmanager_Group(
+ [
+ 'label' => __( 'Footer Settings', 'wp-newsletter-builder' ),
+ 'collapsed' => true,
+ 'collapsible' => true,
+ 'children' => [
+ 'facebook_url' => new \Fieldmanager_Link(
+ [
+ 'label' => __( 'Facebook URL', 'wp-newsletter-builder' ),
+ ]
+ ),
+ 'twitter_url' => new \Fieldmanager_Link(
+ [
+ 'label' => __( 'Twitter URL', 'wp-newsletter-builder' ),
+ ]
+ ),
+ 'instagram_url' => new \Fieldmanager_Link(
+ [
+ 'label' => __( 'Instagram URL', 'wp-newsletter-builder' ),
+ ]
+ ),
+ 'youtube_url' => new \Fieldmanager_Link(
+ [
+ 'label' => __( 'YouTube URL', 'wp-newsletter-builder' ),
+ ]
+ ),
+ 'image' => new \Fieldmanager_Media(
+ [
+ 'label' => __( 'Footer Image', 'wp-newsletter-builder' ),
+ 'preview_size' => 'medium',
+ ]
+ ),
+ 'address' => new \Fieldmanager_TextField(
+ [
+ 'label' => __( 'Company Address', 'wp-newsletter-builder' ),
+ ]
+ ),
+ ],
+ ]
+ ),
+ ],
+ ]
+ );
+
+ $settings->activate_submenu_page();
+ }
+
+ /**
+ * Get the API key and instantiate a client using the API key.
+ *
+ * @return \CS_REST_General
+ */
+ public function get_client() {
+ $settings = get_option( static::SETTINGS_KEY );
+ if ( empty( $settings ) || empty( $settings['api_key'] ) ) {
+ return false;
+ }
+ $auth = [ 'api_key' => $settings['api_key'] ];
+ $wrap = new \CS_REST_General( $auth );
+
+ return $wrap;
+ }
+
+ /**
+ * Gets the lists for the client.
+ *
+ * @TODO: Add caching that works on Pantheon and WordPress VIP.
+ *
+ * @return array|false
+ */
+ public function get_lists() {
+ $settings = get_option( static::SETTINGS_KEY );
+ if ( empty( $settings ) || empty( $settings['api_key'] ) || empty( $settings['client_id'] ) ) {
+ return false;
+ }
+ $auth = [ 'api_key' => $settings['api_key'] ];
+
+ $wrap = new \CS_REST_Clients(
+ $settings['client_id'],
+ $auth
+ );
+
+ return $wrap->get_lists()->response;
+ }
+
+ /**
+ * Gets footer settings.
+ *
+ * @TODO: Add caching that works on Pantheon and WordPress VIP.
+ *
+ * @return array|false
+ */
+ public function get_footer_settings() {
+ $settings = get_option( static::SETTINGS_KEY );
+ if ( empty( $settings ) || empty( $settings['footer_settings'] ) ) {
+ return false;
+ }
+
+ return $settings['footer_settings'];
+ }
+
+ /**
+ * Gets From Names.
+ *
+ * @return array|false
+ */
+ public function get_from_names() {
+ $settings = get_option( static::SETTINGS_KEY );
+ if ( empty( $settings ) || empty( $settings['from_names'] ) ) {
+ return false;
+ }
+
+ return $settings['from_names'];
+ }
+}
diff --git a/src/class-wp-newsletter-builder.php b/src/class-wp-newsletter-builder.php
index 1e266fea..7c0f97b9 100644
--- a/src/class-wp-newsletter-builder.php
+++ b/src/class-wp-newsletter-builder.php
@@ -246,10 +246,11 @@ public function do_send( $post_id ) {
return;
}
$campaign_id = get_post_meta( $post_id, 'nb_newsletter_campaign_id', true );
- $result = Campaign_Monitor_Client::instance()->create_campaign( $post_id, $lists );
+ global $newsletter_builder_email_provider;
+ $result = $newsletter_builder_email_provider->create_campaign( $post_id, $lists );
if ( 201 === $result['http_status_code'] ) {
update_post_meta( $post_id, 'nb_newsletter_campaign_id', $result['response'] );
- $send_result = Campaign_Monitor_Client::instance()->send_campaign( $result['response'] );
+ $send_result = $newsletter_builder_email_provider->send_campaign( $result['response'] );
update_post_meta( $post_id, 'nb_newsletter_send_result', $send_result );
}
update_post_meta( $post_id, 'nb_newsletter_campaign_result', $result );
diff --git a/src/class-campaign-monitor-client.php b/src/email-providers/class-campaign-monitor.php
similarity index 62%
rename from src/class-campaign-monitor-client.php
rename to src/email-providers/class-campaign-monitor.php
index 0b838013..4658a953 100644
--- a/src/class-campaign-monitor-client.php
+++ b/src/email-providers/class-campaign-monitor.php
@@ -5,14 +5,14 @@
* @package wp-newsletter-builder
*/
-namespace WP_Newsletter_Builder;
+namespace WP_Newsletter_Builder\Email_Providers;
use WP_Newsletter_Builder\Singleton;
/**
* Campaign Monitor Client class
*/
-class Campaign_Monitor_Client {
+class Campaign_Monitor implements Email_Provider {
use Singleton;
/**
@@ -31,41 +31,14 @@ public function setup() {
add_action( 'init', [ $this, 'maybe_register_settings_page' ] );
}
- /**
- * Displays an error message if Fieldmanager is not installed.
- *
- * @return void
- */
- public function fieldmanager_not_found_error() {
- ?>
-