Skip to content

Commit 88936b7

Browse files
authored
Merge pull request #176 from WebDevStudios/feature/CC-395-health-panel
Feature/cc 395 health panel
2 parents dc5cf5c + 49ad77c commit 88936b7

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,11 @@
5151
"phpcs.xml.dist",
5252
"tags"
5353
]
54+
},
55+
"config": {
56+
"allow-plugins": {
57+
"composer/installers": true,
58+
"dealerdirect/phpcodesniffer-composer-installer": true
59+
}
5460
}
5561
}

src/Plugin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Exception;
1313

14+
use WebDevStudios\CCForWoo\Utility\HealthPanel;
1415
use WebDevStudios\CCForWoo\Utility\PluginCompatibilityCheck;
1516
use WebDevStudios\OopsWP\Structure\ServiceRegistrar;
1617
use WebDevStudios\CCForWoo\View\ViewRegistrar;
@@ -211,6 +212,7 @@ public function register_hooks() {
211212
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
212213
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ], 99 );
213214
add_action( 'init', [ $this, 'load_plugin_textdomain' ] );
215+
add_action( 'init', [ $this, 'load_health_panel' ] );
214216

215217
register_activation_hook( $this->plugin_file, [ $this, 'do_activation_process' ] );
216218
register_deactivation_hook( $this->plugin_file, [ $this, 'do_deactivation_process' ] );
@@ -377,5 +379,9 @@ public function admin_enqueue_scripts() {
377379
public function load_plugin_textdomain() {
378380
load_plugin_textdomain( 'constant-contact-woocommerce' );
379381
}
382+
383+
public function load_health_panel() {
384+
new HealthPanel();
385+
}
380386
}
381387

src/Utility/HealthPanel.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Constant Contact + WooCommerce Health Panel
4+
*
5+
* Adds debugging information to the Site Health panel.
6+
*
7+
* @since 2.2.0
8+
* @package cc-woo
9+
*/
10+
11+
namespace WebDevStudios\CCForWoo\Utility;
12+
13+
use WebDevStudios\CCForWoo\Meta\ConnectionStatus;
14+
use WebDevStudios\CCForWoo\Plugin;
15+
use WebDevStudios\CCForWoo\AbandonedCheckouts\CheckoutsTable;
16+
17+
/**
18+
* Class HealthPanel
19+
*
20+
* @package WebDevStudios\CCForWoo\Utility
21+
* @since 2.2.0
22+
*/
23+
class HealthPanel {
24+
25+
public function __construct() {
26+
add_filter( 'debug_information', [ $this, 'health_information' ], 1 );
27+
}
28+
29+
/**
30+
* Callback to add in our own cusotm site health information.
31+
*
32+
* @since 2.2.0
33+
*
34+
* @throws Exception
35+
*
36+
* @param array $debug_info Array of debug info panels.
37+
* @return array
38+
*/
39+
public function health_information( $debug_info ) {
40+
41+
$connection = new ConnectionStatus();
42+
$debug_info['constant-contact-woocommerce'] = [
43+
'label' => esc_html__( 'Constant Contact + WooCommerce', 'constant-contact-woocommerce' ),
44+
'description' => esc_html__( 'Debugging and troubleshooting information for support purposes', 'constant-contact-woocommerce' ),
45+
'fields' => [
46+
[
47+
'label' => esc_html__( 'Plugin version', 'constant-contact-woocommerce' ),
48+
'value' => Plugin::PLUGIN_VERSION,
49+
],
50+
[
51+
'label' => esc_html__( 'Connection status', 'constant-contact-woocommerce' ),
52+
'value' => ( $connection->is_connected() )
53+
? esc_html__( 'Connected', 'constant-contact-woocommerce' )
54+
: esc_html__( 'Disconnected', 'constant-contact-woocommerce' )
55+
],
56+
[
57+
'label' => esc_html__( 'Abandoned checkouts total pending items', 'constant-contact-woocommerce' ),
58+
'value' => $this->abandoned_checkouts_count(),
59+
],
60+
[
61+
'label' => esc_html__( 'Abandoned checkouts expiration cron status', 'constant-contact-woocommerce' ),
62+
'value' => ( wp_next_scheduled( 'cc_woo_check_expired_checkouts' ) )
63+
? esc_html__( 'Scheduled', 'constant-contact-woocommerce' )
64+
: esc_html__( 'Not scheduled', 'constant-contact-woocommerce' ),
65+
],
66+
[
67+
'label' => esc_html__( 'Current user has a CC key', 'constant-contact-woocommerce' ),
68+
'value' => ( $this->user_has_cc_key() )
69+
? esc_html__( 'True', 'constant-contact-woocommerce' )
70+
: esc_html__( 'False', 'constant-contact-woocommerce' ),
71+
],
72+
]
73+
];
74+
75+
return $debug_info;
76+
}
77+
78+
/**
79+
* Check if the current user has a Constant Contact API key.
80+
*
81+
* @since 2.2.0
82+
*
83+
* @return bool
84+
*/
85+
private function user_has_cc_key(): bool {
86+
$user_id = get_current_user_id();
87+
88+
if ( ! $user_id ) {
89+
return false;
90+
}
91+
92+
$query = <<<SQL
93+
SELECT
94+
key_id
95+
FROM
96+
{$GLOBALS['wpdb']->prefix}woocommerce_api_keys
97+
WHERE
98+
user_id = %d
99+
AND
100+
(
101+
description LIKE '%Constant Contact%'
102+
OR
103+
description LIKE '%ConstantContact%'
104+
)
105+
SQL;
106+
107+
return ! empty( $GLOBALS['wpdb']->get_col( $GLOBALS['wpdb']->prepare( $query, $user_id ) ) );
108+
}
109+
110+
/**
111+
* Returns a count of total abandoned checkouts.
112+
*
113+
* @since 2.2.0
114+
*
115+
* @return string
116+
*/
117+
private function abandoned_checkouts_count() : string {
118+
$table = CheckoutsTable::get_table_name();
119+
$query = <<<SQL
120+
SELECT
121+
count(*)
122+
FROM
123+
{$table}
124+
SQL;
125+
$count = (string) $GLOBALS['wpdb']->get_var( $query );
126+
return ( ! empty( $count ) ) ? $count : '0';
127+
}
128+
}

0 commit comments

Comments
 (0)