-
Notifications
You must be signed in to change notification settings - Fork 2
/
openstack_shell_widget.php
136 lines (117 loc) · 4.7 KB
/
openstack_shell_widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
Plugin Name: OpenStack Shell
Plugin URI: https://thecustomizewindows.com
Description: A Better Way to Use OpenStack Python Clients to Upload to OpenStack Object Storage (SWIFT) From WordPress. Intended for sysadmins, advanced users and developers. Needs SSH access.
Version: 1.5
Author: Abhishek Ghosh
Author URI: https://thecustomizewindows.com
License: GNU GPLv3
*/
add_action('openstack_shell_setup', array('OpenStack_Shell_Widget','init') );
defined('WP_PLUGIN_URL') or die('Restricted access');
define('OpenStack_Shell_Widget', ABSPATH.PLUGINDIR.'/shell_plugin/');
define('OpenStack_Shell_Widget', WP_PLUGIN_URL.'/shell_plugin/');
class OpenStack_Shell_Widget {
/**
* The id of this widget.
*/
const wid = 'openstack_shell_widget';
/**
* Hook to wp_dashboard_setup to add the widget.
*/
public static function init() {
//Register widget settings...
self::update_dashboard_widget_options(
self::wid, //The widget id
array( //Associative array of options & default values
'example_number' => 42,
),
true //Add only (will not update existing options)
);
//Register the widget...
wp_add_dashboard_widget(
self::wid, //A unique slug/ID
__( 'OpenStack Shell Instruction', 'nouveau' ),//Visible name for the widget
array('OpenStack_Shell_Widget','widget'), //Callback for the main widget content
array('OpenStack_Shell_Widget','config') //Optional callback for widget configuration content
);
}
/**
* Load the widget code
*/
public static function widget() {
require_once( 'shell.php' );
}
/**
* Load widget config code.
*
* This is what will display when an admin clicks
*/
public static function config() {
require_once( 'shell-config.php' );
}
/**
* Gets the options for a widget of the specified name.
*
* @param string $widget_id Optional. If provided, will only get options for the specified widget.
* @return array An associative array containing the widget's options and values. False if no opts.
*/
public static function get_dashboard_widget_options( $widget_id='' )
{
//Fetch ALL dashboard widget options from the db...
$opts = get_option( 'dashboard_widget_options' );
//If no widget is specified, return everything
if ( empty( $widget_id ) )
return $opts;
//If we request a widget and it exists, return it
if ( isset( $opts[$widget_id] ) )
return $opts[$widget_id];
//Something went wrong...
return false;
}
/**
* Gets one specific option for the specified widget.
* @param $widget_id
* @param $option
* @param null $default
*
* @return string
*/
public static function get_dashboard_widget_option( $widget_id, $option, $default=NULL ) {
$opts = self::get_dashboard_widget_options($widget_id);
//If widget opts dont exist, return false
if ( ! $opts )
return false;
//Otherwise fetch the option or use default
if ( isset( $opts[$option] ) && ! empty($opts[$option]) )
return $opts[$option];
else
return ( isset($default) ) ? $default : false;
}
/**
* Saves an array of options for a single dashboard widget to the database.
* Can also be used to define default values for a widget.
*
* @param string $widget_id The name of the widget being updated
* @param array $args An associative array of options being saved.
* @param bool $add_only If true, options will not be added if widget options already exist
*/
public static function update_dashboard_widget_options( $widget_id , $args=array(), $add_only=false )
{
//Fetch ALL dashboard widget options from the db...
$opts = get_option( 'dashboard_widget_options' );
//Get just our widget's options, or set empty array
$w_opts = ( isset( $opts[$widget_id] ) ) ? $opts[$widget_id] : array();
if ( $add_only ) {
//Flesh out any missing options (existing ones overwrite new ones)
$opts[$widget_id] = array_merge($args,$w_opts);
}
else {
//Merge new options with existing ones, and add it back to the widgets array
$opts[$widget_id] = array_merge($w_opts,$args);
}
//Save the entire widgets array back to the db
return update_option('dashboard_widget_options', $opts);
}
}