Skip to content

Commit da93dd4

Browse files
committed
Update freemkit with audit logging, language handling, and various improvements
1 parent 06114a9 commit da93dd4

30 files changed

Lines changed: 1949 additions & 289 deletions

freemkit.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://webberzone.com
99
*
1010
* @wordpress-plugin
11-
* Plugin Name: WebberZone FreemKit - Glue for Freemius and Kit
11+
* Plugin Name: FreemKit - Glue for Freemius and Kit
1212
* Plugin URI: https://webberzone.com/plugins/freemkit/
1313
* Description: Easily subscribe Freemius customers to Kit email lists.
1414
* Version: 1.0.0-beta1
@@ -124,27 +124,3 @@ function freemkit() {
124124
return $freemkit;
125125
}
126126
}
127-
128-
/**
129-
* Show plugin bootstrap errors in wp-admin.
130-
*
131-
* @since 1.0.0
132-
*
133-
* @return void
134-
*/
135-
function show_boot_error_notice() {
136-
if ( ! current_user_can( 'manage_options' ) ) {
137-
return;
138-
}
139-
140-
$error = get_option( 'freemkit_boot_error' );
141-
if ( empty( $error ) ) {
142-
return;
143-
}
144-
145-
printf(
146-
'<div class="notice notice-error"><p>%s</p></div>',
147-
esc_html( (string) $error )
148-
);
149-
}
150-
add_action( 'admin_notices', __NAMESPACE__ . '\\show_boot_error_notice' );

includes/admin/class-admin-banner.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,42 @@ class Admin_Banner {
2929
*
3030
* @var array<string, mixed>
3131
*/
32-
private array $config = array();
32+
public array $config = array();
3333

3434
/**
3535
* Derived class names keyed by component.
3636
*
3737
* @var array<string, array<int, string>>
3838
*/
39-
private array $class_names = array();
39+
public array $class_names = array();
4040

4141
/**
4242
* Localized strings.
4343
*
4444
* @var array<string, string>
4545
*/
46-
private array $strings = array();
46+
public array $strings = array();
4747

4848
/**
4949
* Style configuration.
5050
*
5151
* @var array<string, mixed>
5252
*/
53-
private array $style = array();
53+
public array $style = array();
5454

5555
/**
5656
* Base class prefix shared by all banners.
5757
*
5858
* @var string
5959
*/
60-
private string $base_prefix = 'wz-admin-banner';
60+
public string $base_prefix = 'wz-admin-banner';
6161

6262
/**
6363
* Unique class prefix derived from the provided prefix.
6464
*
6565
* @var string
6666
*/
67-
private string $unique_prefix = 'admin-banner';
67+
public string $unique_prefix = 'admin-banner';
6868

6969
/**
7070
* Constructor.
@@ -108,7 +108,7 @@ public function __construct( array $config ) {
108108
/**
109109
* Register hooks.
110110
*/
111-
private function hooks(): void {
111+
public function hooks(): void {
112112
Hook_Registry::add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_styles' ) );
113113
Hook_Registry::add_action( 'in_admin_header', array( $this, 'render' ) );
114114
}
@@ -197,7 +197,7 @@ public function render(): void {
197197
/**
198198
* Enqueue the banner stylesheet.
199199
*/
200-
private function enqueue_style(): void {
200+
public function enqueue_style(): void {
201201
wp_register_style(
202202
$this->style['handle'],
203203
$this->style['url'],
@@ -213,7 +213,7 @@ private function enqueue_style(): void {
213213
* @param \WP_Screen $screen Current admin screen.
214214
* @param string $page_slug Current request page slug.
215215
*/
216-
private function should_render_on_screen( \WP_Screen $screen, string $page_slug ): bool {
216+
public function should_render_on_screen( \WP_Screen $screen, string $page_slug ): bool {
217217
$screen_base = (string) $screen->base;
218218
if ( '' !== $screen_base && in_array( $screen_base, (array) $this->config['exclude_screen_bases'], true ) ) {
219219
return false;
@@ -237,7 +237,7 @@ private function should_render_on_screen( \WP_Screen $screen, string $page_slug
237237
* @param \WP_Screen $screen Current admin screen.
238238
* @param string $page_slug Current request page slug.
239239
*/
240-
private function resolve_current_section( \WP_Screen $screen, string $page_slug ): string {
240+
public function resolve_current_section( \WP_Screen $screen, string $page_slug ): string {
241241
$screen_id = (string) $screen->id;
242242

243243
foreach ( $this->config['sections'] as $section_key => $section ) {
@@ -260,7 +260,7 @@ private function resolve_current_section( \WP_Screen $screen, string $page_slug
260260
*
261261
* @param array $strings Raw strings array.
262262
*/
263-
private function prepare_strings( array $strings ): array {
263+
public function prepare_strings( array $strings ): array {
264264
$defaults = array(
265265
'region_label' => '',
266266
'nav_label' => '',
@@ -277,7 +277,7 @@ private function prepare_strings( array $strings ): array {
277277
*
278278
* @param string $prefix Base prefix.
279279
*/
280-
private function resolve_wrapper_prefix( string $prefix ): string {
280+
public function resolve_wrapper_prefix( string $prefix ): string {
281281
$prefix = sanitize_key( $prefix );
282282

283283
if ( '' === $prefix ) {
@@ -292,7 +292,7 @@ private function resolve_wrapper_prefix( string $prefix ): string {
292292
*
293293
* @param array $style Style configuration.
294294
*/
295-
private function prepare_style_config( array $style ): array {
295+
public function prepare_style_config( array $style ): array {
296296
$defaults = array(
297297
'handle' => $this->sanitize_handle( "{$this->unique_prefix}-styles" ),
298298
'deps' => array(),
@@ -319,7 +319,7 @@ private function prepare_style_config( array $style ): array {
319319
*
320320
* @return array
321321
*/
322-
private function sanitize_sections( array $sections ): array {
322+
public function sanitize_sections( array $sections ): array {
323323
$sanitized = array();
324324

325325
foreach ( $sections as $key => $section ) {
@@ -348,7 +348,7 @@ private function sanitize_sections( array $sections ): array {
348348
*
349349
* @return array<string, array<int, string>>
350350
*/
351-
private function derive_class_names(): array {
351+
public function derive_class_names(): array {
352352
$build = function ( string $suffix = '' ): array {
353353
$classes = array( $this->base_prefix . $suffix );
354354

@@ -381,7 +381,7 @@ private function derive_class_names(): array {
381381
*
382382
* @return array
383383
*/
384-
private function collect_targets_from_sections( string $target_key ): array {
384+
public function collect_targets_from_sections( string $target_key ): array {
385385
$values = array();
386386

387387
foreach ( $this->config['sections'] as $section ) {
@@ -401,7 +401,7 @@ private function collect_targets_from_sections( string $target_key ): array {
401401
*
402402
* @param array $section Section configuration.
403403
*/
404-
private function get_section_link_classes( array $section ): array {
404+
public function get_section_link_classes( array $section ): array {
405405
$classes = $this->class_names['link'] ?? array();
406406
$type = isset( $section['type'] ) ? sanitize_key( $section['type'] ) : 'secondary';
407407
$type = '' !== $type ? $type : 'secondary';
@@ -422,7 +422,7 @@ private function get_section_link_classes( array $section ): array {
422422
* @param array $classes Class list.
423423
* @return string Class attribute string.
424424
*/
425-
private function implode_classes( array $classes ): string {
425+
public function implode_classes( array $classes ): string {
426426
return implode( ' ', array_unique( array_filter( $classes ) ) );
427427
}
428428

@@ -432,7 +432,7 @@ private function implode_classes( array $classes ): string {
432432
* @param string $key Classes array key.
433433
* @return string Class attribute string.
434434
*/
435-
private function class_attr( string $key ): string {
435+
public function class_attr( string $key ): string {
436436
return $this->implode_classes( $this->class_names[ $key ] ?? array() );
437437
}
438438

@@ -441,14 +441,14 @@ private function class_attr( string $key ): string {
441441
*
442442
* @param string $handle Raw handle.
443443
*/
444-
private function sanitize_handle( string $handle ): string {
444+
public function sanitize_handle( string $handle ): string {
445445
return sanitize_title_with_dashes( $handle );
446446
}
447447

448448
/**
449449
* Get the current page slug from the request.
450450
*/
451-
private function get_request_page_slug(): string {
451+
public function get_request_page_slug(): string {
452452
$page_param_raw = filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW );
453453

454454
if ( is_string( $page_param_raw ) && '' !== $page_param_raw ) {

includes/admin/class-admin-notices-api.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Admin_Notices_API {
2828
*
2929
* @var array Registered notices.
3030
*/
31-
private array $notices = array();
31+
public array $notices = array();
3232

3333
/**
3434
* Constructor class.
@@ -132,7 +132,7 @@ public function display_notices() {
132132
*
133133
* @since 4.1.0
134134
*/
135-
private function print_scripts() {
135+
public function print_scripts() {
136136
static $printed = false;
137137

138138
if ( $printed ) {
@@ -197,7 +197,7 @@ public function handle_notice_dismissal() {
197197
* @param string $notice_id Notice ID.
198198
* @return bool Whether the notice has been dismissed.
199199
*/
200-
private function is_notice_dismissed( $notice_id ) {
200+
public function is_notice_dismissed( $notice_id ) {
201201
$notice = $this->notices[ $notice_id ] ?? null;
202202

203203
if ( ! $notice ) {

includes/admin/class-kit-oauth.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace WebberZone\FreemKit\Admin;
1010

1111
use WebberZone\FreemKit\Kit_API;
12+
use WebberZone\FreemKit\Kit_Audit_Log;
1213
use WebberZone\FreemKit\Kit_Settings;
14+
use WebberZone\FreemKit\Util\Hook_Registry;
1315

1416
/**
1517
* Class Kit_OAuth
@@ -21,7 +23,7 @@ class Kit_OAuth {
2123
*
2224
* @var string
2325
*/
24-
private string $menu_slug;
26+
public string $menu_slug;
2527

2628
/**
2729
* Constructor.
@@ -30,7 +32,7 @@ class Kit_OAuth {
3032
*/
3133
public function __construct( string $menu_slug ) {
3234
$this->menu_slug = $menu_slug;
33-
add_action( 'admin_init', array( $this, 'maybe_handle_requests' ) );
35+
Hook_Registry::add_action( 'admin_init', array( $this, 'maybe_handle_requests' ) );
3436
}
3537

3638
/**
@@ -46,16 +48,26 @@ public function maybe_handle_requests(): void {
4648
$api = new Kit_API();
4749
$settings = new Kit_Settings();
4850

51+
// If ConvertKit plugin owns credentials, FreemKit stays read-only.
52+
if ( $settings->using_convertkit_credentials() && isset( $_GET['code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
53+
Kit_Audit_Log::add( 'oauth_callback_skipped_convertkit_owned' );
54+
Admin::add_notice( esc_html__( 'Connection is managed by the ConvertKit plugin. Reconnect from that plugin if needed.', 'freemkit' ), 'notice-warning' );
55+
wp_safe_redirect( $this->get_settings_url() );
56+
exit;
57+
}
58+
4959
if ( isset( $_GET['freemkit_oauth_disconnect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
5060
check_admin_referer( 'freemkit_oauth_disconnect' );
5161
if ( $settings->using_convertkit_credentials() ) {
62+
Kit_Audit_Log::add( 'disconnect_skipped_convertkit_owned' );
5263
Admin::add_notice( esc_html__( 'Connection is managed by the ConvertKit plugin. Disconnect it there if needed.', 'freemkit' ), 'notice-warning' );
5364
wp_safe_redirect( $this->get_settings_url() );
5465
exit;
5566
}
5667

5768
$settings->delete_credentials();
5869
$this->clear_kit_cache();
70+
Kit_Audit_Log::add( 'disconnect_local_success' );
5971
Admin::add_notice( esc_html__( 'Disconnected from Kit.', 'freemkit' ), 'notice-success' );
6072
wp_safe_redirect( $this->get_settings_url() );
6173
exit;
@@ -69,13 +81,15 @@ public function maybe_handle_requests(): void {
6981
$result = $api->get_access_token( $authorization_code );
7082

7183
if ( is_wp_error( $result ) ) {
84+
Kit_Audit_Log::add( 'oauth_connect_failed', array( 'error' => $result->get_error_message() ), 'warning' );
7285
Admin::add_notice( sprintf( esc_html__( 'Kit OAuth failed: %s', 'freemkit' ), $result->get_error_message() ), 'notice-error' );
7386
wp_safe_redirect( $this->get_settings_url() );
7487
exit;
7588
}
7689

7790
$settings->update_credentials( $result );
7891
$this->clear_kit_cache();
92+
Kit_Audit_Log::add( 'oauth_connect_success' );
7993
Admin::add_notice( esc_html__( 'Successfully connected to Kit via OAuth.', 'freemkit' ), 'notice-success' );
8094
wp_safe_redirect( $this->get_settings_url() );
8195
exit;
@@ -183,7 +197,7 @@ public static function get_status_html( string $menu_slug, array $query_args = a
183197
*
184198
* @return bool
185199
*/
186-
private function is_settings_page(): bool {
200+
public function is_settings_page(): bool {
187201
if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
188202
return false;
189203
}
@@ -196,7 +210,7 @@ private function is_settings_page(): bool {
196210
*
197211
* @return string
198212
*/
199-
private function get_settings_url(): string {
213+
public function get_settings_url(): string {
200214
$args = array(
201215
'page' => $this->menu_slug,
202216
'tab' => 'kit',
@@ -218,7 +232,7 @@ private function get_settings_url(): string {
218232
*
219233
* @return void
220234
*/
221-
private function clear_kit_cache(): void {
235+
public function clear_kit_cache(): void {
222236
foreach ( array( 'forms', 'tags', 'sequences', 'custom_fields' ) as $transient ) {
223237
delete_transient( 'freemkit_kit_' . $transient );
224238
}

includes/admin/class-settings-wizard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ protected function get_completion_buttons() {
423423
* @param array<string,mixed> $args Optional query args.
424424
* @return string
425425
*/
426-
private function get_wizard_url( array $args = array() ): string {
426+
public function get_wizard_url( array $args = array() ): string {
427427
$default = array(
428428
'page' => $this->page_slug,
429429
);

0 commit comments

Comments
 (0)