-
Notifications
You must be signed in to change notification settings - Fork 825
Newsletter Categories: add helper class #44537
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7ba9793
Newsletter Categories: add helper class
arcangelini db9234d
Use helper class across Jetpack
arcangelini 58730e3
changelog
arcangelini 0b36a36
Add tests
arcangelini 27d0ab7
Merge branch 'trunk' into arcanagelinis/newsletter-fix-v2
arcangelini a69489b
fix stuff
arcangelini aa8187a
remove unneccessary test
arcangelini bbc7e9b
remove unneccessary test
arcangelini 0279a43
fix race condition
arcangelini 86f03ad
Merge branch 'trunk' into arcanagelinis/newsletter-fix-v2
arcangelini 1f58197
Merge branch 'trunk' into arcanagelinis/newsletter-fix-v2
arcangelini 637c9bc
Fix toggling
arcangelini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
projects/plugins/jetpack/_inc/lib/class-jetpack-newsletter-category-helper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Contains utilities related to the Jetpack Newsletter Categories. | ||
* | ||
* @package automattic/jetpack | ||
*/ | ||
|
||
/** | ||
* Jetpack_Newsletter_Category_Helper class | ||
*/ | ||
class Jetpack_Newsletter_Category_Helper { | ||
|
||
const NEWSLETTER_CATEGORIES_OPTION = 'wpcom_newsletter_categories'; | ||
|
||
/** | ||
* Return category ID's | ||
* | ||
* @return array An array of integers | ||
*/ | ||
public static function get_category_ids() { | ||
$newsletter_categories = maybe_unserialize( get_option( 'wpcom_newsletter_categories', array() ) ); | ||
|
||
if ( ! is_array( $newsletter_categories ) || empty( $newsletter_categories ) ) { | ||
return array(); | ||
} | ||
|
||
// Check if it is an array of integers. | ||
// [123, 456] | ||
if ( isset( $newsletter_categories[0] ) && is_int( $newsletter_categories[0] ) ) { | ||
return $newsletter_categories; | ||
} | ||
|
||
// Check if it is an array of arrays with term_id keys. | ||
// [{term_id: 123}, {term_id: 456}] | ||
if ( isset( $newsletter_categories[0] ) && is_array( $newsletter_categories[0] ) && isset( $newsletter_categories[0]['term_id'] ) ) { | ||
$ids = array(); | ||
foreach ( $newsletter_categories as $category ) { | ||
if ( isset( $category['term_id'] ) && is_numeric( $category['term_id'] ) ) { | ||
$ids[] = (int) $category['term_id']; | ||
} | ||
} | ||
return $ids; | ||
} | ||
|
||
return array(); | ||
} | ||
|
||
/** | ||
* Handles category ID's ready to be saved as an option | ||
* | ||
* @param array $newsletter_categories An array of id's that could be in a few different forms. | ||
* @return array|bool An associated array with term_id keys on success, or the boolean result from update_option. | ||
* [{term_id: 123}, {term_id: 456}] | ||
*/ | ||
public static function save_category_ids( $newsletter_categories ) { | ||
if ( ! is_array( $newsletter_categories ) || empty( $newsletter_categories ) ) { | ||
return false; | ||
} | ||
|
||
$formatted_categories = array(); | ||
|
||
// Check if it is an array of integers. | ||
// [123, 456] | ||
if ( isset( $newsletter_categories[0] ) && is_numeric( $newsletter_categories[0] ) ) { | ||
foreach ( $newsletter_categories as $id ) { | ||
if ( is_numeric( $id ) ) { | ||
$formatted_categories[] = array( 'term_id' => (int) $id ); | ||
} | ||
} | ||
} | ||
|
||
// Check if it is an array of arrays with term_id keys. | ||
// [{term_id: 123}, {term_id: 456}] | ||
if ( isset( $newsletter_categories[0] ) && is_array( $newsletter_categories[0] ) && isset( $newsletter_categories[0]['term_id'] ) ) { | ||
foreach ( $newsletter_categories as $category ) { | ||
if ( isset( $category['term_id'] ) && is_numeric( $category['term_id'] ) ) { | ||
$formatted_categories[] = array( 'term_id' => (int) $category['term_id'] ); | ||
} | ||
} | ||
} | ||
|
||
if ( empty( $formatted_categories ) ) { | ||
return false; | ||
} | ||
|
||
return update_option( self::NEWSLETTER_CATEGORIES_OPTION, $formatted_categories ) | ||
? $formatted_categories | ||
: false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/arcanagelinis-newsletter-fix-v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: bugfix | ||
|
||
Newsletter: fix bug in category settings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was causing an error because it is possible for
newsletterCategories
to be undefined in the beginning