Skip to content

Commit 8232edd

Browse files
committed
new health panel class
1 parent 3298c35 commit 8232edd

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

src/Utility/HealthPanel.php

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

0 commit comments

Comments
 (0)