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

feat(marketplace): email settings #718

Merged
merged 2 commits into from
Sep 20, 2023
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
47 changes: 46 additions & 1 deletion includes/marketplace/class-api.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
/**
* Newspack Ads Marketplace
* Newspack Ads Marketplace API.
*
* @package Newspack
*/

namespace Newspack_Ads\Marketplace;

use Newspack_Ads\Marketplace;
use Newspack_Ads\Settings;
use WC_Product_Simple;

Expand All @@ -32,6 +33,28 @@ public static function init() {
* Register API endpoints.
*/
public static function register_rest_routes() {
/**
* Settings.
*/
\register_rest_route(
Settings::API_NAMESPACE,
'/marketplace/settings',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'api_get_settings' ],
'permission_callback' => [ 'Newspack_Ads\Settings', 'api_permissions_check' ],
]
);
\register_rest_route(
Settings::API_NAMESPACE,
'/marketplace/settings',
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'api_update_settings' ],
'permission_callback' => [ 'Newspack_Ads\Settings', 'api_permissions_check' ],
'args' => Marketplace::get_settings_args(),
]
);
/**
* Ad Product.
*/
Expand Down Expand Up @@ -114,6 +137,28 @@ public static function register_rest_routes() {
);
}

/**
* Get marketplace settings.
*
* @return \WP_REST_Response containing the ad product data or error.
*/
public static function api_get_settings() {
return \rest_ensure_response( Marketplace::get_settings() );
}

/**
* Update marketplace settings.
*
* @param \WP_REST_Request $request Full details about the request.
*
* @return \WP_REST_Response containing the ad product data or error.
*/
public static function api_update_settings( $request ) {
$args = array_intersect_key( $request->get_params(), Marketplace::get_settings_args() );
Marketplace::update_settings( $args );
return \rest_ensure_response( Marketplace::get_settings() );
}

/**
* Get a product by placement.
*
Expand Down
81 changes: 81 additions & 0 deletions includes/marketplace/class-email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Newspack Ads Marketplace: Email notification.
*
* @package Newspack
*/

namespace Newspack_Ads\Marketplace;

use Newspack_Ads\Marketplace;

/**
* Newspack Ads Marketplace Email Notification Class.
*/
final class Email {
/**
* Initialize hooks.
*/
public static function init() {
add_filter( 'woocommerce_email_headers', [ __CLASS__, 'email_headers' ], 10, 3 );
add_filter( 'woocommerce_email_subject_new_order', [ __CLASS__, 'email_subject' ], 10, 2 );
add_filter( 'woocommerce_email_additional_content_new_order', [ __CLASS__, 'email_additional_content' ], 10, 2 );
}

/**
* Modify email headers.
*
* @param string $header Email headers.
* @param string $method_id Method ID.
* @param object $object Object.
*/
public static function email_headers( $header, $method_id, $object ) {
if ( 'new_order' !== $method_id || ! $object->get_meta( 'newspack_ads_is_ad_order' ) ) {
return $header;
}
$settings = Marketplace::get_settings();
if ( ! $settings['enable_email_notification'] || empty( $settings['notification_email_address'] ) ) {
return $header;
}
$header .= 'Cc: ' . $settings['notification_email_address'] . "\r\n";
return $header;
}

/**
* Modify email subject.
*
* @param string $subject Email subject.
* @param object $object Object.
*/
public static function email_subject( $subject, $object ) {
if ( ! $object->get_meta( 'newspack_ads_is_ad_order' ) ) {
return $subject;
}
return sprintf(
/* translators: %1$s: site name, %2$s: order ID */
__( '[%1$s] New Marketplace Ad Order #%2$s', 'newspack-ads' ),
get_bloginfo( 'name' ),
$object->get_id()
);
}

/**
* Modify email additional content.
*
* @param string $content Email content.
* @param object $object Object.
*/
public static function email_additional_content( $content, $object ) {
if ( ! $object->get_meta( 'newspack_ads_is_ad_order' ) ) {
return $content;
}
$gam_link = sprintf(
'<a href="%s">%s</a>',
esc_url( Product_Order::get_gam_order_url( $object ) ),
esc_html__( 'View order in GAM', 'newspack-ads' )
);
$content .= '<p>' . $gam_link . '</p>';
return $content;
}
}
Email::init();
49 changes: 49 additions & 0 deletions includes/marketplace/class-marketplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* Newspack Ads Marketplace Class.
*/
final class Marketplace {

const SETTINGS_OPTION_NAME = 'newspack_ads_marketplace_settings';

/**
* Initialize hooks.
*/
Expand All @@ -26,6 +29,7 @@ public static function init() {
require_once 'class-product.php';
require_once 'class-product-cart.php';
require_once 'class-product-order.php';
require_once 'class-email.php';
require_once 'class-api.php';

\add_filter( 'get_edit_post_link', [ __CLASS__, 'get_edit_post_link' ], PHP_INT_MAX, 3 );
Expand Down Expand Up @@ -68,5 +72,50 @@ public static function get_edit_post_link( $link, $post_id, $context ) {
return $link;
}

/**
* Marketplace Settings REST Arguments.
*
* @return array
*/
public static function get_settings_args() {
return [
'enable_email_notification' => [
'required' => true,
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
],
'notification_email_address' => [
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_email',
],
];
}

/**
* Get marketplace settings.
*
* @return array Settings.
*/
public static function get_settings() {
$default_settings = [
'enable_email_notification' => true,
'notification_email_address' => get_option( 'admin_email' ),
];
$settings = get_option( self::SETTINGS_OPTION_NAME, [] );
return wp_parse_args( $settings, $default_settings );
}

/**
* Update marketplace settings.
*
* @param array $settings Settings.
*
* @return bool
*/
public static function update_settings( $settings ) {
return update_option( self::SETTINGS_OPTION_NAME, $settings );
}

}
Marketplace::init();
2 changes: 1 addition & 1 deletion includes/marketplace/class-product.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Newspack Ads Marketplace
* Newspack Ads Marketplace Product.
*
* @package Newspack
*/
Expand Down