kudos-admin-debug.php 9.5 KB
Newer Older
1
2
<?php

3
4
5
6
use Kudos\Entity\DonorEntity;
use Kudos\Service\LoggerService;
use Kudos\Service\MapperService;
use Kudos\Service\MollieService;
7
8
9
10
11
12
13
14
15
use Mollie\Api\Resources\Subscription;

/**
 * Debug page render
 *
 * @since   2.0.0
 */

//Get the active tab from the $_GET param
16
$default_tab = 'log';
Michael Iseard's avatar
Michael Iseard committed
17
$tab         = isset( $_GET['tab'] ) ? $_GET['tab'] : $default_tab;
Michael Iseard's avatar
Michael Iseard committed
18

19
20
21
22
23
24
25
?>

<div class="wrap">

    <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

    <nav class="nav-tab-wrapper">
Michael Iseard's avatar
Michael Iseard committed
26
27
28
29
30
31
        <a href="?page=kudos-debug&tab=log"
           class="nav-tab <?php if ( $tab === 'log' ): ?>nav-tab-active<?php endif; ?>">Log</a>
        <a href="?page=kudos-debug&tab=actions"
           class="nav-tab <?php if ( $tab === 'actions' ): ?>nav-tab-active<?php endif; ?>">Actions</a>
        <a href="?page=kudos-debug&tab=subscriptions"
           class="nav-tab <?php if ( $tab === 'subscriptions' ): ?>nav-tab-active<?php endif; ?>">Subscriptions</a>
32
33
34
35
    </nav>

    <div class="tab-content">

Michael Iseard's avatar
Michael Iseard committed
36
		<?php
Michael Iseard's avatar
Michael Iseard committed
37

Michael Iseard's avatar
Michael Iseard committed
38
		$url            = admin_url( 'admin.php?page=kudos-debug' );
Michael Iseard's avatar
Michael Iseard committed
39

Michael Iseard's avatar
Michael Iseard committed
40
41
		switch ( $tab ):
			case 'actions':
Michael Iseard's avatar
Michael Iseard committed
42
				$url = add_query_arg( 'tab', 'actions', $url );
Michael Iseard's avatar
Michael Iseard committed
43
44
45
				?>
                <p>Please use the following actions only if you are having issues. Remember to backup your data before
                    performing any of these actions.</p>
Michael Iseard's avatar
Michael Iseard committed
46
47
48
49

                <hr/>

                <p>This will remove all the settings from the database and reset them back to default.</p>
Michael Iseard's avatar
Michael Iseard committed
50
                <form action="<?php echo $url ?>" method='post'>
Michael Iseard's avatar
Michael Iseard committed
51
					<?php wp_nonce_field( 'kudos_clear_settings', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
52
53
54
55
56
57
                    <input type='hidden' name='kudos_action' value='kudos_clear_settings'>
                    <input class="button-secondary" type='submit' value='Clear settings'>
                </form>

                <hr/>

58
59
60
61
62
63
64
65
66
                <p>This will clear the twig cache.</p>
                <form action="<?php echo $url ?>" method='post'>
					<?php wp_nonce_field( 'kudos_clear_cache', '_wpnonce' ) ?>
                    <input type='hidden' name='kudos_action' value='kudos_clear_cache'>
                    <input class="button-secondary" type='submit' value='Clear cache'>
                </form>

                <hr/>

Michael Iseard's avatar
Michael Iseard committed
67
                <p>Remove all transactions</p>
Michael Iseard's avatar
Michael Iseard committed
68
                <form action="<?php echo $url ?>" method='post'>
Michael Iseard's avatar
Michael Iseard committed
69
					<?php wp_nonce_field( 'kudos_clear_transactions', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
70
71
72
73
74
75
76
                    <input type='hidden' name='kudos_action' value='kudos_clear_transactions'>
                    <input class="button-secondary" type='submit' value='Delete all transactions'>
                </form>

                <hr/>

                <p>Remove all donors</p>
Michael Iseard's avatar
Michael Iseard committed
77
                <form action="<?php echo $url ?>" method='post'>
Michael Iseard's avatar
Michael Iseard committed
78
					<?php wp_nonce_field( 'kudos_clear_donors', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
79
80
81
82
83
84
85
                    <input type='hidden' name='kudos_action' value='kudos_clear_donors'>
                    <input class="button-secondary" type='submit' value='Delete all donors'>
                </form>

                <hr/>

                <p>Remove all subscriptions</p>
Michael Iseard's avatar
Michael Iseard committed
86
                <form action="<?php echo $url ?>" method='post'>
Michael Iseard's avatar
Michael Iseard committed
87
					<?php wp_nonce_field( 'kudos_clear_subscriptions', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
88
89
90
                    <input type='hidden' name='kudos_action' value='kudos_clear_subscriptions'>
                    <input class="button-secondary" type='submit' value='Delete all subscriptions'>
                </form>
Michael Iseard's avatar
Michael Iseard committed
91
92
				<?php
				break;
93

Michael Iseard's avatar
Michael Iseard committed
94
			case 'log':
Michael Iseard's avatar
Michael Iseard committed
95
96
				$url = add_query_arg( 'tab', 'log', $url );
				$file   = LoggerService::LOG_FILE;
97

Michael Iseard's avatar
Michael Iseard committed
98
99
100
101
				// Quit if file does not exist
				if ( ! file_exists( $file ) ) {
					return;
				}
102

103
				$kudos_logger = LoggerService::factory();
104
				$logArray     = $kudos_logger->get_as_array();
Michael Iseard's avatar
Michael Iseard committed
105
				?>
Michael Iseard's avatar
Michael Iseard committed
106
107

                <p>This logfile location: <?php echo $file ?></p>
Michael Iseard's avatar
Michael Iseard committed
108
                <p>Current filesize: <?php echo filesize( $file ) ?> bytes</p>
Michael Iseard's avatar
Michael Iseard committed
109

Michael Iseard's avatar
Michael Iseard committed
110
                <form style="display:inline-block;" action="<?php echo $url ?>"
Michael Iseard's avatar
Michael Iseard committed
111
112
                      method='post'>
					<?php wp_nonce_field( 'kudos_log_clear', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
113
114
115
116
                    <input type='hidden' name='kudos_action' value='kudos_log_clear'>
                    <input class="button-secondary" type='submit' value='Clear'>
                </form>

Michael Iseard's avatar
Michael Iseard committed
117
                <form style="display:inline-block;" action="<?php echo $url ?>"
Michael Iseard's avatar
Michael Iseard committed
118
119
                      method='post'>
					<?php wp_nonce_field( 'kudos_log_download', '_wpnonce' ) ?>
Michael Iseard's avatar
Michael Iseard committed
120
121
122
123
                    <input type='hidden' name='kudos_action' value='kudos_log_download'>
                    <input class="button-secondary" type='submit' value='Download'>
                </form>

Michael Iseard's avatar
Michael Iseard committed
124
125
                <table class='form-table'>
                    <tbody>
Michael Iseard's avatar
Michael Iseard committed
126
127
128
129
130
                    <tr>
                        <th class='row-title'>Date</th>
                        <th>Level</th>
                        <th>Message</th>
                    </tr>
131

Michael Iseard's avatar
Michael Iseard committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
					<?php
					foreach ( $logArray as $key => $log ) {

						$level = $log['type'];
						$style = 'border-left-width: 4px; border-left-style: solid;';

						switch ( $level ) {
							case 'CRITICAL':
							case 'ERROR':
								$class = 'notice-error';
								break;
							case 'DEBUG':
								$class = '';
								$style = '';
								break;
							default:
								$class = 'notice-' . strtolower( $level );
						}
						?>

                        <tr style='<?php echo $style ?>'
                            class='<?php echo ( $key % 2 === 0 ? 'alternate ' : null ) . $class ?>'>
154

Michael Iseard's avatar
Michael Iseard committed
155
                            <td>
156
								<?php echo wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
Michael Iseard's avatar
Michael Iseard committed
157
									strtotime( $log['date'] ) ); ?>
Michael Iseard's avatar
Michael Iseard committed
158
159
                            </td>
                            <td>
Michael Iseard's avatar
Michael Iseard committed
160
								<?php echo( $log['type'] ); ?>
Michael Iseard's avatar
Michael Iseard committed
161
162
                            </td>
                            <td>
Michael Iseard's avatar
Michael Iseard committed
163
								<?php echo( $log['message'] ); ?>
Michael Iseard's avatar
Michael Iseard committed
164
                            </td>
165

Michael Iseard's avatar
Michael Iseard committed
166
                        </tr>
167

Michael Iseard's avatar
Michael Iseard committed
168
					<?php } ?>
169

Michael Iseard's avatar
Michael Iseard committed
170
171
                    </tbody>
                </table>
172

Michael Iseard's avatar
Michael Iseard committed
173
				<?php
174

Michael Iseard's avatar
Michael Iseard committed
175
				break;
176

Michael Iseard's avatar
Michael Iseard committed
177
178
179
180
181
182
			case 'subscriptions':
				$mapper = new MapperService( DonorEntity::class );
				$donors = $mapper->get_all_by();
				if ( $donors ) {
					$kudos_mollie = MollieService::factory();
					foreach ( $donors as $donor ) {
183

Michael Iseard's avatar
Michael Iseard committed
184
						$subscriptions = $kudos_mollie->get_subscriptions( $donor->customer_id );
185

Michael Iseard's avatar
Michael Iseard committed
186
187
188
189
						if ( ! count( $subscriptions ) ) {
							continue;
						}
						?>
190

Michael Iseard's avatar
Michael Iseard committed
191
192
193
194
195
                        <h3><strong><?php echo $donor->email ?></strong>
                            <span>(<?php echo $donor->customer_id ?>)</span></h3>
                        <form action="<?php echo admin_url( 'admin.php?page=kudos-debug&tab=subscriptions' ) ?>"
                              method='post'>
							<?php wp_nonce_field( 'kudos_cancel_subscription', '_wpnonce' ) ?>
196
                            <input type='hidden' name='kudos_action' value='kudos_cancel_subscription'>
Michael Iseard's avatar
Michael Iseard committed
197
198
                            <input type='hidden' name='customerId' value='<?php echo $donor->customer_id ?>'>

Michael Iseard's avatar
Michael Iseard committed
199
200
201
202
							<?php
							/** @var Subscription $subscription */
							foreach ( $subscriptions as $subscription ) {
								?>
Michael Iseard's avatar
Michael Iseard committed
203

Michael Iseard's avatar
Michael Iseard committed
204
205
                                <table class='widefat'>
                                    <tbody>
Michael Iseard's avatar
Michael Iseard committed
206
207
208
209
210
211
212
213
214

                                    <tr>
                                        <td class='row-title'>id</td>
                                        <td><?php echo $subscription->id ?></td>
                                    </tr>

                                    <tr class='alternate'>
                                        <td class='row-title'>status</td>
                                        <td>
Michael Iseard's avatar
Michael Iseard committed
215
216
217
218
219
220
221
											<?php echo $subscription->status ?>
											<?php if ( $subscription->status !== 'canceled' ) : ?>
                                            <button name='subscriptionId' type='submit'
                                                    value='<?php echo $subscription->id ?>'>Cancel
                                            </button>
                                        </td>
										<?php endif; ?>
Michael Iseard's avatar
Michael Iseard committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                                    </tr>

                                    <tr>
                                        <td class='row-title'>amount</td>
                                        <td><?php echo $subscription->amount->value ?></td>
                                    </tr>

                                    <tr class='alternate'>
                                        <td class='row-title'>interval</td>
                                        <td><?php echo $subscription->interval ?></td>
                                    </tr>

                                    <tr>
                                        <td class='row-title'>times</td>
                                        <td><?php echo $subscription->times ?></td>
                                    </tr>

                                    <tr class='alternate'>
                                        <td class='row-title'>next payment</td>
Michael Iseard's avatar
Michael Iseard committed
241
                                        <td><?php echo( $subscription->nextPaymentDate ?? 'n/a' ) ?></td>
Michael Iseard's avatar
Michael Iseard committed
242
243
244
245
246
247
248
                                    </tr>

                                    <tr>
                                        <td class='row-title'>webhookUrl</td>
                                        <td><?php echo $subscription->webhookUrl ?></td>
                                    </tr>

Michael Iseard's avatar
Michael Iseard committed
249
250
251
252
                                    </tbody>
                                </table>
                                <br class='clear'>
							<?php } ?>
Michael Iseard's avatar
Michael Iseard committed
253
                        </form>
Michael Iseard's avatar
Michael Iseard committed
254
255
256
						<?php
					}
				}
257

Michael Iseard's avatar
Michael Iseard committed
258
				break;
259

Michael Iseard's avatar
Michael Iseard committed
260
		endswitch;
261

Michael Iseard's avatar
Michael Iseard committed
262
		?>
263
264
265
266

    </div>

</div>