-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfluent-crm-custom-features.php
More file actions
201 lines (169 loc) · 5.89 KB
/
fluent-crm-custom-features.php
File metadata and controls
201 lines (169 loc) · 5.89 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Plugin Name: FluentCRM - Custom events, actions and conditionals.
* Plugin URI: https://github.com/danieliser/fluent-crm-json-events
* Description:
* Version:
* Author: Code Atlantic LLC
* Author URI: https://code-atlantic.com/
* License:
* License URI:
* Minimum PHP: 7.4
* Minimum WP: 6.2
*
* @package FluentCRM\CustomFeatures
* @author Code Atlantic
* @copyright Copyright (c) 2024, Code Atlantic LLC.
*/
// Register autoloader.
require_once __DIR__ . '/vendor/autoload.php';
add_action(
'init',
function () {
( new \CustomCRM\JSONEventTrackingHandler() )->register();
$edd_rules = new \CustomCRM\EDDSubscriptionRules();
$edd_rules->register();
( new \CustomCRM\Actions\RandomWaitTimeAction() )->register();
// Remove the default update contact property action (broken).
remove_all_actions( 'fluentcrm_funnel_sequence_handle_update_contact_property' );
// Register our custom update contact property action.
( new \CustomCRM\Actions\UpdateContactPropertyAction() )->register();
// Enable our custom webhook handler.
( new \CustomCRM\Webhooks() );
// Remove the default smart link handler.
remove_all_actions( 'fluentcrm_smartlink_clicked' );
remove_all_actions( 'fluentcrm_smartlink_clicked_direct' );
// Register our custom smart link handler.
$fix_smart_link_redirects = new \CustomCRM\SmartLinkHandler();
add_action( 'fluentcrm_smartlink_clicked', [ $fix_smart_link_redirects, 'handleClick' ], 9, 1 );
add_action( 'fluentcrm_smartlink_clicked_direct', [ $fix_smart_link_redirects, 'handleClick' ], 9, 2 );
},
99
);
// Hook to add custom dashboard metrics
// add_action( 'fluent_crm/dashboard_stats', 'add_custom_dashboard_metrics' );
/**
* Add custom dashboard metrics.
*
* @param array<string,mixed> $data The dashboard data.
*
* @return array<string,mixed>
*/
function add_custom_dashboard_metrics( $data ) {
// Example: Adding a new metric for total subscribers.
$total_subscribers = \FluentCrm\App\Models\Subscriber::count();
$data['total_subscribers_metric'] = [
'title' => __( 'Total Subscribers', 'fluent-crm-custom-features' ),
'count' => $total_subscribers,
'route' => [
'name' => 'subscribers',
],
];
// Add more metrics as needed
return $data;
}
// Hook to register a custom report
add_action( 'fluent_crm/reporting/reports', 'register_custom_report' );
/**
* Register a custom report.
*
* @param array<string,mixed> $reports The reports array.
*
* @return array<string,mixed>
*/
function register_custom_report( $reports ) {
$reports['custom_report'] = [
'title' => __( 'Custom Report', 'fluent-crm-custom-features' ),
'callback' => 'render_custom_report',
];
return $reports;
}
/**
* Render the custom report.
*
* @return void
*/
function render_custom_report() {
// Logic to render your custom report
$subscribers = \FluentCrm\App\Models\Subscriber::all();
// Output your report data here
echo '<h2>' . esc_html__( 'Custom Report', 'fluent-crm-custom-features' ) . '</h2>';
echo '<ul>';
foreach ( $subscribers as $subscriber ) {
echo '<li>' . esc_html( $subscriber->email ) . '</li>';
}
echo '</ul>';
}
// Hook to register a custom REST API endpoint
add_action('rest_api_init', function () {
register_rest_route('fluent-crm/v1', '/list-growth', [
'methods' => 'GET',
'callback' => 'get_list_growth',
'permission_callback' => '__return_true',
]);
});
/**
* Get List Growth metrics
*
* @param WP_REST_Request $request The REST request object.
* @return WP_REST_Response
*/
function get_list_growth( WP_REST_Request $request ) {
$from = $request->get_param( 'from' );
$to = $request->get_param( 'to' );
// Convert dates to Carbon instances
$from_date = \Carbon\Carbon::parse( $from );
$to_date = \Carbon\Carbon::parse( $to );
// Count new subscribers
$new_subscribers = fluentCrmDb()->table( 'fc_subscribers' )
->whereBetween( 'created_at', [ $from_date->format( 'Y-m-d' ), $to_date->format( 'Y-m-d' ) ] )
->where( 'status', 'subscribed' )
->count();
// Count unsubscribed
$unsubscribed = fluentCrmDb()->table( 'fc_subscriber_meta' )
->whereBetween( 'created_at', [ $from_date->format( 'Y-m-d' ), $to_date->format( 'Y-m-d' ) ] )
->where( 'key', 'unsubscribe_reason' )
->count();
// Calculate net growth
$net_growth = $new_subscribers - $unsubscribed;
return new WP_REST_Response([
'new_subscribers' => $new_subscribers,
'unsubscribed' => $unsubscribed,
'net_growth' => $net_growth,
], 200);
}
// Hook to add custom metrics to the dashboard.
add_filter( 'fluent_crm/dashboard_data', 'add_custom_dashboard_metrics_for_list_growth' );
/**
* Add custom dashboard metrics for list growth.
*
* @param array<string,mixed> $data The dashboard data.
*
* @return array<string,mixed>
*/
function add_custom_dashboard_metrics_for_list_growth( $data ) {
// Get the date range from the request or set default values.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$from = isset( $_GET['from'] ) ? sanitize_text_field( wp_unslash( $_GET['from'] ) ) : gmdate( 'Y-m-01' );
$to = isset( $_GET['to'] ) ? sanitize_text_field( wp_unslash( $_GET['to'] ) ) : gmdate( 'Y-m-t' );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// Calculate new subscribers and unsubscribes.
$new_subscribers = fluentCrmDb()->table( 'fc_subscribers' )
->whereBetween( 'created_at', [ $from, $to ] )
->where( 'status', 'subscribed' )
->count();
$unsubscribed = fluentCrmDb()->table( 'fc_subscriber_meta' )
->whereBetween( 'created_at', [ $from, $to ] )
->where( 'key', 'unsubscribe_reason' )
->count();
// Calculate net growth.
$net_growth = $new_subscribers - $unsubscribed;
// Add the new metrics to the dashboard data.
$data['list_growth'] = [
'title' => __( 'List Growth', 'fluent-crm-custom-features' ),
'new_subscribers' => $new_subscribers,
'unsubscribed' => $unsubscribed,
'net_growth' => $net_growth,
];
return $data;
}