-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarbon-footprint.php
100 lines (83 loc) · 3.48 KB
/
carbon-footprint.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
<?php
/**
* Plugin Name: Carbon Footprint
* Plugin URI: https://littlebigthings.be
* Description: A plugin to spread awareness about the carbon footprint of websites and to help making WordPress sites become more sustainable. It adds information to WordPress’ Site Health tool about the carbon footprint of the homepage and the nature of the energy used by the host to power the website. The information is based on data from the Website Carbon Calculator.
* Requires at least: 5.8
* Requires PHP: 5.6
* Version: 1.2.1
* Author: LittleBigThings
* Author URI: https://littlebigthings.be
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: carbon-footprint
*
* @package carbon-footprint
*/
// Loading only in admin or when cron is executed.
if ( is_admin() || wp_doing_cron() ) {
require_once plugin_dir_path( __FILE__ ) . 'inc/helpers.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/class-site-health.php';
}
// Only load when in admin.
if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'dashboard/widget.php';
}
// Get translation going.
function carbonfootprint_plugin_init() {
load_plugin_textdomain( 'carbon-footprint', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'carbonfootprint_plugin_init' );
/**
* Set transient when plugin is activated to show an admin notice on plugin activation (only).
*
* @since 1.0
*/
function carbonfootprint_admin_notice_hook() {
set_transient( 'carbonfootprint_activated', 1, 5 );
}
register_activation_hook( __FILE__, 'carbonfootprint_admin_notice_hook' );
/**
* Show admin notice as a guide for user right after plugin activation
*
* @since 1.0
*/
function carbonfootprint_admin_notice() {
// Display notice if the transient is available
if ( get_transient( 'carbonfootprint_activated' ) ) {
$class = 'notice notice-info';
// if this site is local show the notice that this plugin won't do a lot for now
if ( 'local' === wp_get_environment_type() ) {
printf(
'<div class="%1$s"><p>%2$s</p></div>',
esc_attr( $class ),
esc_html__( 'Sorry, it seems that your website is a local site, so it cannot be accessed to measure its carbon footprint. Try to activate the plugin on a site that can be accessed through the internet. ', 'carbon-footprint' )
);
} else {
$site_health = admin_url( 'site-health.php' );
printf(
'<div class="%1$s"><p>%2$s</p><p><a href="%3$s">%4$s</a></p><p><i>%5$s</i></p></div>',
esc_attr( $class ),
esc_html__( 'Learn more about how sustainable your website is. Discover what kind of energy your server uses and how your site’s carbon footprint compares to others. ', 'carbon-footprint' ),
esc_url( $site_health ),
esc_html__( 'Test your site using Site Health', 'carbon-footprint' ),
esc_html__( 'Note that the test may take a while and make sure to look for passed tests if you do not see any new information after testing.', 'carbon-footprint' )
);
}
// Delete transient to only display notice once
delete_transient( 'carbonfootprint_activated' );
}
}
add_action( 'admin_notices', 'carbonfootprint_admin_notice' );
/**
* Clean up plugin data when plugin is deactivated.
*
* @since 1.0
*/
function carbonfootprint_clean_up() {
// Delete transient with report data
delete_transient( 'carbonfootprint_test' );
// Delete options if saved
delete_option( 'carbonfootprint_data' );
}
register_deactivation_hook( __FILE__, 'carbonfootprint_clean_up' );