Skip to content

Commit

Permalink
See #7
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed May 31, 2020
1 parent e79c34f commit 98b0ef5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
21 changes: 21 additions & 0 deletions inc/AdminPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function init() {
$this->data = new Data();

add_action( 'admin_menu', [ $this, 'add_menu_page' ] );
add_action( 'network_admin_menu', [ $this, 'add_menu_page' ] );
add_action( 'carbon_offset_admin_tab_contents', [ $this, 'details_tab' ] );
add_action( 'carbon_offset_admin_tab_contents', [ $this, 'settings_tab' ] );
add_action( 'carbon_offset_settings_page_fields', [ $this, 'settings_fields' ], 5 );
Expand Down Expand Up @@ -157,7 +158,27 @@ public function details_tab( $current_tab ) {
}

$carbon_data = $this->data->get();

if ( is_network_admin() ) {
$carbon_data = [
'carbon_pending' => 0,
'carbon_offset' => 0,
];
$multisite_carbon_data = [];
$sites = get_sites();
foreach ( $sites as $site ) {
$data = $this->data->get( $site->blog_id );
$carbon_data['carbon_pending'] += $data['carbon_pending'];
$carbon_data['carbon_offset'] += $data['carbon_offset'];
$multisite_carbon_data[ $site->blog_id ] = $data;
}
}
?>
<?php if ( is_network_admin() ) : ?>
<p class="description">
<?php esc_html_e( 'This page shows carbon footprint across all sites on your network.', 'carbon-offset' ); ?>
</p>
<?php endif; ?>
<div class="postbox">
<div class="inside">
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(30em, 1fr));grid-gap:1px;background:#aaa;">
Expand Down
21 changes: 18 additions & 3 deletions inc/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ class Data {
*
* @since 1.0.0
*
* @param int $blog_id The blog-ID on a multisite installation.
*
* @return array
*/
public function get() {
public function get( $blog_id = 0 ) {
if ( $blog_id ) {
switch_to_blog( $blog_id );
}
$value = (array) get_option(
$this->option_name,
[
'carbon_pending' => 0,
'carbon_offset' => 0,
]
);
if ( $blog_id ) {
restore_current_blog();
}
return $value;
}

Expand Down Expand Up @@ -76,11 +84,18 @@ public function add( $carbon = 1 ) {
*
* @since 1.0.0
*
* @param array $value The value we want to save.
* @param array $value The value we want to save.
* @param int $blog_id The blog-ID in a multisite installation.
*
* @return void
*/
public function save( $value ) {
public function save( $value, $blog_id = 0 ) {
if ( $blog_id ) {
switch_to_blog( $blog_id );
}
update_option( $this->option_name, $value );
if ( $blog_id ) {
restore_current_blog();
}
}
}

0 comments on commit 98b0ef5

Please sign in to comment.