Skip to content

Commit

Permalink
Update various functions across multiple files in Newsletter Builder …
Browse files Browse the repository at this point in the history
…plugin

This commit updates various functions across multiple files in the WP Newsletter Builder plugin. The updated functions offer enhanced readability and improved best practices, such as consistent comment format. Changes include updating an array structure for improved understanding, changing date format to GMT for international consistency, and replacing the file_get_contents function with WordPress's wp_remote_get function for performance and security improvement. Filtering hooks for modifying headers and various 'TODO' comments have been added for future enhancement work.
  • Loading branch information
attackant committed Nov 17, 2023
1 parent 7f5b556 commit 6ff255d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 63 deletions.
42 changes: 29 additions & 13 deletions src/class-omeda-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,21 @@ class Omeda_Client {
private string $client_abbr = '';

/**
* Omeda_Client Constructor.
*
* Initialize and set values for properties.
*
* @param array $config The configuration options for the Omeda Client.
* - license_key: The license key for authentication.
* - user: The API user.
* - app_id: The app ID.
* - brand: The brand.
* - client_abbr: The client abbreviation.
* - from_name: The name to be used as the sender.
* - input_id: The input ID.
* - mailbox: The mailbox.
* - namespace: The namespace.
* - Reply_to: The reply to address.
*/
public function __construct( $config ) {

Expand Down Expand Up @@ -489,7 +503,7 @@ public function set_from_name( string $from_name ): Omeda_Client {
* Get the headers for the specified Omeda API service.
*
* Allows other functions to modify headers with
* the nr_modify_omeda_headers filter.
* the wp_newsletter_builder_modify_headers filter.
*
* @param string $service The name of the API service.
*
Expand All @@ -501,7 +515,7 @@ private function get_headers( string $service = '' ): array {
'Content-Type' => 'application/json',
];

$http_response_header = apply_filters( 'nr_modify_omeda_headers', $http_response_header, $service );
$http_response_header = apply_filters( 'wp_newsletter_builder_modify_headers', $http_response_header, $service );

return $http_response_header;
}
Expand Down Expand Up @@ -567,6 +581,7 @@ private function log( string $message ): void {
* @param string $service The endpoint to send the request to.
* @param array|null $data The data to send with the request.
* @param string $api The API to use (client or brand).
* @param string $method The HTTP method to use (GET or POST).
*
* @return array|WP_Error The response data as an associative array, or a WP_Error object if there was an error.
*/
Expand All @@ -584,10 +599,10 @@ public function call( string $service, ?array $data, string $api = self::CLIENT,
[
'headers' => $this->get_headers( $service ),
'body' => wp_json_encode( $data ),
]
]
);

// TODO move content type to get header
// TODO move content type to get header.
if ( 'POST' === $method ) {
$response = wp_remote_post(
esc_url_raw( $endpoint ),
Expand All @@ -598,18 +613,18 @@ public function call( string $service, ?array $data, string $api = self::CLIENT,
'Content-Type' => is_string( $data ) ? 'application/xml' : 'application/json',
],
'body' => is_string( $data ) ? $data : wp_json_encode( $data ),
]
]
);
} else {
$response = wp_remote_get(
$response = wp_remote_get( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_post, WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
esc_url_raw( $endpoint ),
[
'headers' => [
'x-omeda-appid' => $this->get_app_id(),
'x-omeda-inputid' => $this->get_input_id(),
'Content-Type' => 'application/json',
],
]
]
);
}

Expand All @@ -630,13 +645,14 @@ public function call( string $service, ?array $data, string $api = self::CLIENT,
return new WP_Error( 'http_error', wp_remote_retrieve_body( $response ) );
}
$body = wp_remote_retrieve_body( $response );
// TODO.
if ( is_string( $data ) ) {
$xml = simplexml_load_string( $body );
$json = json_encode( $xml );
$json = wp_json_encode( $xml );
return json_decode( $json, true );
}
return json_decode( $body, true );
// return json_decode( wp_remote_retrieve_body( $response ), true );
// return json_decode( wp_remote_retrieve_body( $response ), true );.
}

/**
Expand Down Expand Up @@ -691,7 +707,7 @@ public function opt_in( WP_REST_Request $request ): array|WP_Error {
'DeleteOptOut' => 1,
],
],
]
]
);
}

Expand Down Expand Up @@ -721,7 +737,7 @@ public function opt_out( WP_REST_Request $request ): array|WP_Error {
'DeploymentTypeId' => $request->get_param( 'newsletters' ),
],
],
]
]
);
}

Expand Down Expand Up @@ -816,7 +832,7 @@ public function store_customer( string $email = '' ): WP_Error|array {
// This input ID needs to be included when calling
// the Store Customer and Order API.
add_filter(
'nr_modify_omeda_headers',
'wp_newsletter_builder_modify_headers',
function ( $headers ) {
$headers['x-omeda-inputid'] = '7900G2456689A2G';
return $headers;
Expand All @@ -832,7 +848,7 @@ function ( $headers ) {
[ 'EmailAddress' => $email ],
],
],
self::BRAND
self::BRAND
);
}
}
10 changes: 4 additions & 6 deletions src/class-rest-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function register_endpoints(): void {
'callback' => [ $this, 'get_lists' ],
'permission_callback' => function () {
return true;
// return current_user_can( 'edit_posts' );
// return current_user_can( 'edit_posts' ); TODO.
},
]
);
Expand Down Expand Up @@ -92,9 +92,7 @@ public function register_endpoints(): void {
*/
public function get_lists(): WP_Error|array {
if ( ! current_user_can( 'edit_posts' ) ) {
// phpcs:disable
// TODO return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
// phpcs:enable
return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
}
global $newsletter_builder_email_provider;

Expand Down Expand Up @@ -200,11 +198,11 @@ public function get_status( WP_REST_Request $request ): array {
/**
* Subscribes a user to a list.
*
* @param WP_Rest_Request $request The request object.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
public function subscribe( WP_REST_Request $request ): array {
public function subscribe( WP_REST_Request $request ): array { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
$email = $request->get_param( 'email' );
if ( empty( $email ) ) {
return [
Expand Down
83 changes: 47 additions & 36 deletions src/class-wp-newsletter-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,61 @@ public function __construct() {
*/
public function register_post_types(): void {

$post_types = [
register_post_type(
'nb_newsletter',
[
'name' => 'nb_newsletter',
'singular_name' => 'Newsletter',
'rewrite_slug' => 'nb_newsletters',
'menu_icon' => 'dashicons-email-alt2',
],
[
'name' => 'nb_template',
'singular_name' => 'Template',
'rewrite_slug' => 'nb_templates',
'menu_icon' => 'dashicons-admin-customizer',
'labels' => [
'name' => __( 'Newsletters', 'wp-newsletter-builder' ),
'singular_name' => __( 'Newsletter', 'wp-newsletter-builder' ),
],
'public' => true,
'has_archive' => true,
'rewrite' => [ 'slug' => 'nb-newsletters' ],
'supports' => [ 'title', 'editor', 'custom-fields' ],
'show_in_rest' => true,
'exclude_from_search' => true,
'template' => [
[
'wp-newsletter-builder/email-settings',
[
'lock' => [
'move' => true,
'remove' => true,
],
],
],
],
'menu_icon' => 'dashicons-email-alt2',
],
];
);

foreach ( $post_types as $post_type ) {
register_post_type(
$post_type['name'],
[
'labels' => [
'name' => __( ucfirst( $post_type['singular_name'] ) . 's', 'wp-newsletter_builder' ),
'singular_name' => __( ucfirst( $post_type['singular_name'] ), 'wp-newsletter_builder' ),
],
'public' => true,
'has_archive' => true,
'rewrite' => [ 'slug' => $post_type['rewrite_slug'] ],
'supports' => [ 'title', 'editor', 'custom_fields' ],
'show_in_rest' => true,
'exclude_from_search' => true,
'template' => [
register_post_type(
'nb_template',
[
'labels' => [
'name' => __( 'Templates', 'wp-newsletter-builder' ),
'singular_name' => __( 'Template', 'wp-newsletter-builder' ),
],
'public' => true,
'has_archive' => true,
'rewrite' => [ 'slug' => 'nb-templates' ],
'supports' => [ 'title', 'editor', 'custom-fields' ],
'show_in_rest' => true,
'exclude_from_search' => true,
'template' => [
[
'wp-newsletter-builder/email-settings',
[
'wp-newsletter-builder/email-settings',
[
'lock' => [
'move' => true,
'remove' => true,
],
'lock' => [
'move' => true,
'remove' => true,
],
],
],
'menu_icon' => $post_type['menu_icon'],
],
);
}
'menu_icon' => 'dashicons-admin-customizer',
],
);
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/email-providers/class-omeda.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace WP_Newsletter_Builder\Email_Providers;

use Simple_XML;
use WP_Newsletter_Builder\Omeda_Client;

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ public function create_campaign( $newsletter_id, $list_ids, $campaign_id = null
$params = [
'DeploymentName' => sprintf( '%s - Post %d - %s', $newsletter->post_title, $newsletter->ID, get_post_modified_time( 'Y-m-d H:i:s', false, $newsletter->ID ) ),
'DeploymentTypeId' => intval( $list_ids[0] ), // array not allowed - how do we handle properly? Maybe splits?
'DeploymentDate' => date( 'Y-m-d H:i', strtotime( '+1 minute' ) ), // Must be in the future. 'yyyy-MM-dd HH:mm' format.
'DeploymentDate' => gmdate( 'Y-m-d H:i', strtotime( '+1 minute' ) ), // Must be in the future. 'yyyy-MM-dd HH:mm' format.
'OwnerUserId' => 'nalley', // Need to figure this out.
'Splits' => 1,
'TrackOpens' => 1,
Expand All @@ -210,12 +211,12 @@ public function create_campaign( $newsletter_id, $list_ids, $campaign_id = null
$track_id = $response['TrackId'];

// Get the content from $url.
$html_content = file_get_contents( $url );
$html_content = file_get_contents( $url ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown

$settings = get_option( static::SETTINGS_KEY, [] );

$content_params = [
'UserId' => 'nalley', // Need to figure this out.'
'UserId' => 'nalley', // TODO Need to figure this out.
'TrackId' => $track_id,
'Subject' => get_post_meta( $newsletter->ID, 'nb_newsletter_subject', true ),
'FromName' => $nl_from_name,
Expand Down Expand Up @@ -244,13 +245,13 @@ public function create_campaign( $newsletter_id, $list_ids, $campaign_id = null
* @param string $campaign_id The campaign id.
* @return array|false The response from the API.
*/
public function send_campaign( $campaign_id ) {
public function send_campaign( $campaign_id ): false|array {
$params = [
'UserId' => 'nalley',
'TrackId' => $campaign_id,
];
$response = $this->client->call( 'omail/deployment/sendtest', $params, 'brand', 'POST' );
var_dump( $response );
return $response;
}

/**
Expand Down Expand Up @@ -311,11 +312,12 @@ public function remove_subscriber( $list_id, $email ) {
/**
* Converts an array to an XML string
*
* @param array $array
* @param \Simple_XML $root_element
* @param array $array
* @param Simple_XML $root_element
*
* @return string
*/
private function array_to_xml( $array, &$root_element ) {
private function array_to_xml( array $array, Simple_XML $root_element ): string {
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
if ( ! is_numeric( $key ) ) {
Expand Down

0 comments on commit 6ff255d

Please sign in to comment.