PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | _inc/lib/class-jetpack-newsletter-category-helper.php +90 -0 16.2-beta → 16.3 View file →
@@ -1,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 +}