| 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 |
|