admin-pages
4 weeks ago
core-api
4 weeks ago
debugger
4 weeks ago
markdown
7 months ago
class-jetpack-ai-helper.php
4 months ago
class-jetpack-application-password-extras.php
3 months ago
class-jetpack-blog-stats-helper.php
7 months ago
class-jetpack-currencies.php
1 year ago
class-jetpack-instagram-gallery-helper.php
4 weeks ago
class-jetpack-mapbox-helper.php
3 years ago
class-jetpack-newsletter-category-helper.php
7 months ago
class-jetpack-podcast-feed-locator.php
4 weeks ago
class-jetpack-podcast-helper.php
4 months ago
class-jetpack-recommendations.php
1 month ago
class-jetpack-spinner.php
1 month ago
class-jetpack-top-posts-helper.php
4 weeks ago
class.color.php
1 year ago
class.core-rest-api-endpoints.php
4 weeks ago
class.jetpack-automatic-install-skin.php
2 years ago
class.jetpack-iframe-embed.php
3 years ago
class.jetpack-password-checker.php
7 months ago
class.jetpack-search-performance-logger.php
4 years ago
class.media-extractor.php
1 month ago
class.media-summary.php
1 month ago
class.media.php
7 months ago
components.php
7 months ago
debugger.php
4 weeks ago
icalendar-reader.php
1 month ago
markdown.php
7 months ago
plans.php
4 years ago
plugins.php
4 years ago
tonesque.php
7 months ago
widgets.php
2 months ago
class-jetpack-newsletter-category-helper.php
91 lines
| 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 | } |
| 91 |