| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class JP\CC\Widgets |
| 11 |
*/ |
| 12 |
class Widget { |
| 13 |
|
| 14 |
/** |
| 15 |
* Retrieve data for a widget from options table. |
| 16 |
* |
| 17 |
* @param string $widget_id The unique ID of a widget. |
| 18 |
* |
| 19 |
* @return array The array of widget settings or empty array if none |
| 20 |
*/ |
| 21 |
public static function get_options( $widget_id ) { |
| 22 |
|
| 23 |
static $options = array(); |
| 24 |
|
| 25 |
// If already loaded, return existing settings. |
| 26 |
if ( ! isset( $options[ $widget_id ] ) ) { |
| 27 |
|
| 28 |
$split_pos = strrpos( $widget_id, '-' ); |
| 29 |
|
| 30 |
if ( false === $split_pos ) { |
| 31 |
return array(); |
| 32 |
} |
| 33 |
|
| 34 |
$basename = substr( $widget_id, 0, $split_pos ); // Examples: "text-2" will return "text", "recent-post-2" will return "recent-post" |
| 35 |
$index = substr( $widget_id, $split_pos + 1 ); // Examples: "text-2" will return "2", "recent-post-2" will return "2" |
| 36 |
|
| 37 |
$widget_settings = get_option( 'widget_' . $basename ); |
| 38 |
|
| 39 |
if ( isset( $widget_settings[ $index ] ) ) { |
| 40 |
$options[ $widget_id ] = static::parse_options( $widget_settings[ $index ] ); |
| 41 |
} |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
return $options[ $widget_id ]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Checks for & adds missing widget options to prevent errors or missing data. |
| 50 |
* |
| 51 |
* @param array $options |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public static function parse_options( $options = array() ) { |
| 56 |
|
| 57 |
if ( ! is_array( $options ) ) { |
| 58 |
$options = array(); |
| 59 |
} |
| 60 |
|
| 61 |
return wp_parse_args( $options, array( |
| 62 |
'which_users' => '', |
| 63 |
'roles' => array(), |
| 64 |
) ); |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|