Skip to content

Commit a645c34

Browse files
authored
Merge pull request #164 from WebDevStudios/feature/CC-332-webhook-logging
Add logging to various parts of our code, mostly around connections and webhooks.
2 parents 4e89324 + 19d8b94 commit a645c34

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

src/Api/KeyManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace WebDevStudios\CCForWoo\Api;
1010

1111
use WebDevStudios\OopsWP\Structure\Service;
12+
use WebDevStudios\CCForWoo\Utility\DebugLogging;
1213

1314
/**
1415
* KeyManager class
@@ -56,6 +57,13 @@ public function maybe_revoke_api_key( string $query ) : string {
5657
*/
5758
do_action( 'cc_woo_key_revoked' );
5859

60+
$ctct_logger = new DebugLogging(
61+
wc_get_logger(),
62+
'WooCommerce API Key with Constant Contact revoked',
63+
'info'
64+
);
65+
$ctct_logger->log();
66+
5967
return $query;
6068
}
6169

src/Meta/ConnectionStatus.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace WebDevStudios\CCForWoo\Meta;
1111

12+
use WebDevStudios\CCForWoo\Utility\DebugLogging;
13+
1214
/**
1315
* Class PluginOption
1416
*
@@ -105,5 +107,13 @@ public function set_connection( int $connected, int $user_id ) {
105107
if ( $this->connected ) {
106108
update_option( self::CC_FIRST_CONNECTION, true );
107109
}
110+
111+
$ctct_logger = new DebugLogging(
112+
wc_get_logger(),
113+
'Constant Contact connection set',
114+
'info',
115+
[ 'status' => $connected ]
116+
);
117+
$ctct_logger->log();
108118
}
109119
}

src/Rest/AbandonedCheckouts/Controller.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace WebDevStudios\CCForWoo\Rest\AbandonedCheckouts;
1010

11+
use WebDevStudios\CCForWoo\Utility\DebugLogging;
1112
use WP_REST_Server;
1213
use WP_REST_Request;
1314
use WP_REST_Controller;
@@ -79,6 +80,15 @@ public function register_routes() {
7980
*/
8081
public function get_items_permissions_check( $request ) {
8182
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
83+
84+
$ctct_logger = new DebugLogging(
85+
wc_get_logger(),
86+
'CTCT Woo: no permission to check abandoned checkout',
87+
'warning',
88+
[ 'cc-woo-rest-not-allowed' => $request ]
89+
);
90+
$ctct_logger->log();
91+
8292
return new WP_Error( 'cc-woo-rest-not-allowed', esc_html__( 'Sorry, you cannot list resources.', 'cc-woo' ), [ 'status' => rest_authorization_required_code() ] );
8393
}
8494

src/Rest/PluginVersion/Controller.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace WebDevStudios\CCForWoo\Rest\PluginVersion;
1010

11+
use WebDevStudios\CCForWoo\Utility\DebugLogging;
1112
use WP_REST_Server;
1213
use WP_REST_Request;
1314
use WP_REST_Controller;
@@ -76,6 +77,15 @@ public function register_routes() {
7677
*/
7778
public function get_item_permissions_check( $request ) {
7879
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
80+
81+
$ctct_logger = new DebugLogging(
82+
wc_get_logger(),
83+
'CTCT Woo: no permission to check version',
84+
'warning',
85+
[ 'cc-woo-rest-not-allowed' => $request ]
86+
);
87+
$ctct_logger->log();
88+
7989
return new WP_Error( 'cc-woo-rest-not-allowed', esc_html__( 'Sorry, you cannot list that resource.', 'cc-woo' ), [ 'status' => rest_authorization_required_code() ] );
8090
}
8191

src/Utility/DebugLogging.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace WebDevStudios\CCForWoo\Utility;
4+
5+
/**
6+
* Class DebugLogging
7+
*/
8+
class DebugLogging {
9+
10+
/**
11+
* The logger from WooCommerce.
12+
*
13+
* @since NEXT
14+
* @var object
15+
*/
16+
private $logger;
17+
18+
/**
19+
* The message to log.
20+
*
21+
* @var mixed|string
22+
*/
23+
private $message;
24+
25+
/**
26+
* The message level.
27+
*
28+
* See https://developer.woocommerce.com/2017/01/26/improved-logging-in-woocommerce-2-7/
29+
*
30+
* @var mixed|string
31+
*/
32+
private $level;
33+
34+
/**
35+
* Extra contextual information for the log item.
36+
*
37+
* @var array|mixed
38+
*/
39+
private $extras;
40+
41+
/**
42+
* Construct our compatibility checker with the main plugin class.
43+
*
44+
* @param string $classname The classname to use for testing.
45+
*
46+
* @author Zach Owen <zach@webdevstudios>
47+
* @since 0.0.1
48+
*/
49+
public function __construct( $logger, $message = '', $level = '', $extras = [] ) {
50+
$this->logger = $logger;
51+
$this->message = $message;
52+
$this->level = $level;
53+
$this->extras = $extras;
54+
}
55+
56+
/**
57+
* Perform our logging, if WP_DEBUG_LOG is enabled.
58+
*
59+
* @since NEXT
60+
*/
61+
public function log() {
62+
if ( defined( 'WP_DEBUG_LOG' ) ) {
63+
$this->logger->{$this->level}(
64+
$this->message,
65+
$this->extras
66+
);
67+
}
68+
}
69+
}

src/View/Admin/Disconnect.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?php
1+
<?php
22
// Handles the plugin disconnection.
33

44
namespace WebDevStudios\CCForWoo\View\Admin;
5+
use WebDevStudios\CCForWoo\Utility\DebugLogging;
56
use WebDevStudios\OopsWP\Structure\Service;
67
use WebDevStudios\CCForWoo\Meta\ConnectionStatus;
78
use WebDevStudios\CCForWoo\AbandonedCheckouts\CheckoutsTable;
@@ -12,7 +13,7 @@
1213
* @return void
1314
*/
1415
class Disconnect extends Service {
15-
16+
1617
/**
1718
* Constructor.
1819
*
@@ -22,7 +23,7 @@ class Disconnect extends Service {
2223
public function register_hooks() {
2324
add_action( 'admin_init', array( $this, 'disconnect' ) );
2425
}
25-
26+
2627
/**
2728
* Disconnects the plugin from Constant Contact WOO.
2829
*
@@ -34,10 +35,17 @@ public function disconnect() {
3435
return;
3536
}
3637

38+
$ctct_logger = new DebugLogging(
39+
wc_get_logger(),
40+
'CTCT Woo: Plugin disconnected from Constant Contact',
41+
'info'
42+
);
43+
$ctct_logger->log();
44+
3745
$this->disconnect_plugin();
3846
$this->redirect();
3947
}
40-
48+
4149
/**
4250
* Disconnects the plugin from Constant Contact WOO.
4351
*
@@ -61,7 +69,7 @@ public function disconnect_plugin() {
6169
delete_option( ConnectionStatus::CC_CONNECTION_USER_ID );
6270
delete_option( ConnectionStatus::CC_FIRST_CONNECTION );
6371
delete_option( ConnectionStatus::CC_CONNECTION_ESTABLISHED_KEY );
64-
72+
6573

6674
// WooCommerce Options
6775
delete_option( 'cc_woo_store_information_first_name' );
@@ -75,7 +83,7 @@ public function disconnect_plugin() {
7583
delete_option( 'constant_contact_for_woo_has_setup' );
7684
delete_option( 'cc_woo_customer_data_allow_import' );
7785
}
78-
86+
7987
/**
8088
* Redirects to the admin page.
8189
*
@@ -87,8 +95,8 @@ public function redirect() {
8795
$url = add_query_arg([
8896
'tab' => 'wc-settings' === $_GET['page'] ? 'cc_woo' : '',
8997
'cc-connect' => '',
90-
], $url );
98+
], $url );
9199
wp_redirect( $url );
92100
exit;
93101
}
94-
}
102+
}

0 commit comments

Comments
 (0)