Skip to content

Commit 8e6e8a9

Browse files
authored
Newsletter Categories: add helper class (#44537)
1 parent ec35e7c commit 8e6e8a9

File tree

6 files changed

+331
-85
lines changed

6 files changed

+331
-85
lines changed

projects/plugins/jetpack/_inc/client/newsletter/newsletter-categories.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ function NewsletterCategories( props ) {
5454
dispatch,
5555
} = props;
5656

57-
const checkedCategoriesIds = newsletterCategories.map( mapCategoriesIds );
57+
const checkedCategoriesIds = useMemo(
58+
() => newsletterCategories?.map( mapCategoriesIds ) ?? [],
59+
[ newsletterCategories ]
60+
);
5861

5962
const mappedCategories = useMemo(
6063
() =>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Contains utilities related to the Jetpack Newsletter Categories.
4+
*
5+
* @package automattic/jetpack
6+
*/
7+
8+
/**
9+
* Jetpack_Newsletter_Category_Helper class
10+
*/
11+
class Jetpack_Newsletter_Category_Helper {
12+
13+
const NEWSLETTER_CATEGORIES_OPTION = 'wpcom_newsletter_categories';
14+
15+
/**
16+
* Return category ID's
17+
*
18+
* @return array An array of integers
19+
*/
20+
public static function get_category_ids() {
21+
$newsletter_categories = maybe_unserialize( get_option( 'wpcom_newsletter_categories', array() ) );
22+
23+
if ( ! is_array( $newsletter_categories ) || empty( $newsletter_categories ) ) {
24+
return array();
25+
}
26+
27+
// Check if it is an array of integers.
28+
// [123, 456]
29+
if ( isset( $newsletter_categories[0] ) && is_int( $newsletter_categories[0] ) ) {
30+
return $newsletter_categories;
31+
}
32+
33+
// Check if it is an array of arrays with term_id keys.
34+
// [{term_id: 123}, {term_id: 456}]
35+
if ( isset( $newsletter_categories[0] ) && is_array( $newsletter_categories[0] ) && isset( $newsletter_categories[0]['term_id'] ) ) {
36+
$ids = array();
37+
foreach ( $newsletter_categories as $category ) {
38+
if ( isset( $category['term_id'] ) && is_numeric( $category['term_id'] ) ) {
39+
$ids[] = (int) $category['term_id'];
40+
}
41+
}
42+
return $ids;
43+
}
44+
45+
return array();
46+
}
47+
48+
/**
49+
* Handles category ID's ready to be saved as an option
50+
*
51+
* @param array $newsletter_categories An array of id's that could be in a few different forms.
52+
* @return array|bool An associated array with term_id keys on success, or the boolean result from update_option.
53+
* [{term_id: 123}, {term_id: 456}]
54+
*/
55+
public static function save_category_ids( $newsletter_categories ) {
56+
if ( ! is_array( $newsletter_categories ) || empty( $newsletter_categories ) ) {
57+
return false;
58+
}
59+
60+
$formatted_categories = array();
61+
62+
// Check if it is an array of integers.
63+
// [123, 456]
64+
if ( isset( $newsletter_categories[0] ) && is_numeric( $newsletter_categories[0] ) ) {
65+
foreach ( $newsletter_categories as $id ) {
66+
if ( is_numeric( $id ) ) {
67+
$formatted_categories[] = array( 'term_id' => (int) $id );
68+
}
69+
}
70+
}
71+
72+
// Check if it is an array of arrays with term_id keys.
73+
// [{term_id: 123}, {term_id: 456}]
74+
if ( isset( $newsletter_categories[0] ) && is_array( $newsletter_categories[0] ) && isset( $newsletter_categories[0]['term_id'] ) ) {
75+
foreach ( $newsletter_categories as $category ) {
76+
if ( isset( $category['term_id'] ) && is_numeric( $category['term_id'] ) ) {
77+
$formatted_categories[] = array( 'term_id' => (int) $category['term_id'] );
78+
}
79+
}
80+
}
81+
82+
if ( empty( $formatted_categories ) ) {
83+
return false;
84+
}
85+
86+
return update_option( self::NEWSLETTER_CATEGORIES_OPTION, $formatted_categories )
87+
? $formatted_categories
88+
: false;
89+
}
90+
}

projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ public function get_all_options() {
504504
$response['newsletter_has_active_plan'] = count( Jetpack_Memberships::get_all_newsletter_plan_ids( false ) ) > 0;
505505
}
506506

507+
// Make sure we are returning a consistent type
508+
if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) {
509+
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php';
510+
}
511+
$response['wpcom_newsletter_categories'] = Jetpack_Newsletter_Category_Helper::get_category_ids();
512+
507513
return rest_ensure_response( $response );
508514
}
509515

@@ -650,6 +656,10 @@ public function update_data( $request ) {
650656
}
651657
}
652658

659+
if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) {
660+
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php';
661+
}
662+
653663
foreach ( $params as $option => $value ) {
654664

655665
// Used if there was an error. Can be overwritten with specific error messages.
@@ -1050,43 +1060,24 @@ function ( &$value ) {
10501060
}
10511061
break;
10521062

1053-
case 'wpcom_newsletter_categories':
1054-
$sanitized_category_ids = (array) $value;
1055-
1056-
array_walk_recursive(
1057-
$sanitized_category_ids,
1058-
function ( &$value ) {
1059-
if ( is_int( $value ) && $value > 0 ) {
1060-
return;
1061-
}
1062-
1063-
$value = (int) $value;
1064-
if ( $value <= 0 ) {
1065-
$value = null;
1066-
}
1067-
}
1068-
);
1069-
1070-
$sanitized_category_ids = array_unique(
1071-
array_filter(
1072-
$sanitized_category_ids,
1073-
function ( $category_id ) {
1074-
return $category_id !== null;
1075-
}
1076-
)
1077-
);
1063+
case Jetpack_Newsletter_Category_Helper::NEWSLETTER_CATEGORIES_OPTION:
1064+
if ( ! is_array( $value ) || empty( $value ) ) {
1065+
break;
1066+
}
10781067

1079-
$new_value = array_map(
1080-
function ( $category_id ) {
1081-
return array( 'term_id' => $category_id );
1082-
},
1083-
$sanitized_category_ids
1084-
);
1068+
// If we are already current, do nothing
1069+
$current_value = Jetpack_Newsletter_Category_Helper::get_category_ids();
1070+
if ( $value === $current_value ) {
1071+
break;
1072+
}
10851073

1086-
if ( ! update_option( $option, $new_value ) ) {
1074+
if ( Jetpack_Newsletter_Category_Helper::save_category_ids( $value ) ) {
1075+
$updated = true;
1076+
} else {
10871077
$updated = false;
1088-
$error = esc_html__( 'WPCOM Newsletter Categories failed to process.', 'jetpack' );
1078+
$error = esc_html__( 'Newsletter category did not update.', 'jetpack' );
10891079
}
1080+
10901081
break;
10911082

10921083
default:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: bugfix
3+
4+
Newsletter: fix bug in category settings

projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,11 @@ public function get_settings_response() {
374374
)
375375
);
376376

377-
$newsletter_categories = maybe_unserialize( get_option( 'wpcom_newsletter_categories', array() ) );
378-
$newsletter_category_ids = array_filter(
379-
array_map(
380-
function ( $newsletter_category ) {
381-
if ( is_array( $newsletter_category ) && isset( $newsletter_category['term_id'] ) ) {
382-
// This is the expected format.
383-
return (int) $newsletter_category['term_id'];
384-
} elseif ( is_numeric( $newsletter_category ) ) {
385-
// This is a previous format caused by a bug.
386-
return (int) $newsletter_category;
387-
}
388-
return null;
389-
},
390-
$newsletter_categories
391-
)
392-
);
377+
// Make sure we are returning a consistent type
378+
if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) {
379+
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php';
380+
}
381+
$newsletter_category_ids = Jetpack_Newsletter_Category_Helper::get_category_ids();
393382

394383
$api_cache = $site->is_jetpack() ? (bool) get_option( 'jetpack_api_cache_enabled' ) : true;
395384

@@ -673,6 +662,10 @@ public function update_settings() {
673662
$sharing_options = array();
674663
$updated = array();
675664

665+
if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) {
666+
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php';
667+
}
668+
676669
foreach ( $input as $key => $value ) {
677670

678671
if ( ! is_array( $value ) ) {
@@ -1070,42 +1063,12 @@ function ( &$value ) {
10701063
$updated[ $key ] = (int) (bool) $value;
10711064
break;
10721065

1073-
case 'wpcom_newsletter_categories':
1074-
$sanitized_category_ids = (array) $value;
1075-
1076-
array_walk_recursive(
1077-
$sanitized_category_ids,
1078-
function ( &$value ) {
1079-
if ( is_int( $value ) && $value > 0 ) {
1080-
return;
1081-
}
1082-
1083-
$value = (int) $value;
1084-
if ( $value <= 0 ) {
1085-
$value = null;
1086-
}
1087-
}
1088-
);
1089-
1090-
$sanitized_category_ids = array_unique(
1091-
array_filter(
1092-
$sanitized_category_ids,
1093-
function ( $category_id ) {
1094-
return $category_id !== null;
1095-
}
1096-
)
1097-
);
1098-
1099-
$new_value = array_map(
1100-
function ( $category_id ) {
1101-
return array( 'term_id' => $category_id );
1102-
},
1103-
$sanitized_category_ids
1104-
);
1105-
1106-
if ( update_option( $key, $new_value ) ) {
1107-
$updated[ $key ] = $sanitized_category_ids;
1066+
case Jetpack_Newsletter_Category_Helper::NEWSLETTER_CATEGORIES_OPTION:
1067+
$update_newsletter_categories = Jetpack_Newsletter_Category_Helper::save_category_ids( (array) $value );
1068+
if ( $update_newsletter_categories ) {
1069+
$updated[ $key ] = $update_newsletter_categories;
11081070
}
1071+
11091072
break;
11101073

11111074
case 'wpcom_newsletter_categories_enabled':

0 commit comments

Comments
 (0)