| 1 |
<?php |
| 2 |
|
| 3 |
use LearnPress\Databases\WebhookDB; |
| 4 |
use LearnPress\Filters\WebhookFilter; |
| 5 |
use LearnPress\Models\Webhook\WebhookModel; |
| 6 |
use LearnPress\Webhook\WebhookEvents; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 11 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Admin table for LearnPress webhooks. |
| 16 |
*/ |
| 17 |
class LP_Admin_Webhooks_Table_List extends WP_List_Table { |
| 18 |
/** |
| 19 |
* Define visible columns. |
| 20 |
* |
| 21 |
* @return array<string, string> |
| 22 |
*/ |
| 23 |
public function get_columns() { |
| 24 |
return array( |
| 25 |
'name' => esc_html__( 'Name', 'learnpress' ), |
| 26 |
'status' => esc_html__( 'Status', 'learnpress' ), |
| 27 |
'delivery_url' => esc_html__( 'Delivery URL', 'learnpress' ), |
| 28 |
'events' => esc_html__( 'Events', 'learnpress' ), |
| 29 |
'updated_at' => esc_html__( 'Updated', 'learnpress' ), |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Define sortable columns. |
| 35 |
* |
| 36 |
* @return array<string, array{0:string,1:bool}> |
| 37 |
*/ |
| 38 |
protected function get_sortable_columns() { |
| 39 |
return array( |
| 40 |
'name' => array( 'name', false ), |
| 41 |
'status' => array( 'status', false ), |
| 42 |
'updated_at' => array( 'updated_at', true ), |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Render name and row actions. |
| 48 |
* |
| 49 |
* @param object $item Webhook row. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
protected function column_name( $item ) { |
| 54 |
$edit_data = wp_json_encode( |
| 55 |
array( |
| 56 |
'webhook_id' => absint( $item->webhook_id ), |
| 57 |
'name' => (string) $item->name, |
| 58 |
'delivery_url' => (string) $item->delivery_url, |
| 59 |
'status' => (string) $item->status, |
| 60 |
'events' => $this->decode_events( $item->events ), |
| 61 |
) |
| 62 |
); |
| 63 |
$webhook_id = absint( $item->webhook_id ); |
| 64 |
$actions = array( |
| 65 |
'edit' => sprintf( |
| 66 |
'<a href="#" class="lp-webhook-edit" data-webhook="%1$s">%2$s</a>', |
| 67 |
esc_attr( $edit_data ), |
| 68 |
esc_html__( 'Edit', 'learnpress' ) |
| 69 |
), |
| 70 |
/*'regenerate' => sprintf( |
| 71 |
'<a href="#" class="lp-webhook-regenerate" data-webhook-id="%1$d">%2$s</a>', |
| 72 |
$webhook_id, |
| 73 |
esc_html__( 'Regenerate secret', 'learnpress' ) |
| 74 |
),*/ |
| 75 |
'delete' => sprintf( |
| 76 |
'<a href="#" class="lp-webhook-delete" data-webhook-id="%1$d">%2$s</a>', |
| 77 |
$webhook_id, |
| 78 |
esc_html__( 'Delete', 'learnpress' ) |
| 79 |
), |
| 80 |
); |
| 81 |
|
| 82 |
return sprintf( '<strong>%1$s</strong> %2$s', esc_html( (string) $item->name ), $this->row_actions( $actions ) ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Render status column. |
| 87 |
* |
| 88 |
* @param object $item Webhook row. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
protected function column_status( $item ) { |
| 93 |
return WebhookModel::STATUS_ACTIVE === $item->status |
| 94 |
? esc_html__( 'Active', 'learnpress' ) |
| 95 |
: esc_html__( 'Paused', 'learnpress' ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Render delivery URL column. |
| 100 |
* |
| 101 |
* @param object $item Webhook row. |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
protected function column_delivery_url( $item ) { |
| 106 |
return sprintf( |
| 107 |
'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>', |
| 108 |
esc_url( (string) $item->delivery_url ), |
| 109 |
esc_html( (string) $item->delivery_url ) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Render selected event labels. |
| 115 |
* |
| 116 |
* @param object $item Webhook row. |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
protected function column_events( $item ) { |
| 121 |
$registry = WebhookEvents::all(); |
| 122 |
$labels = array(); |
| 123 |
|
| 124 |
foreach ( $this->decode_events( $item->events ) as $event_key ) { |
| 125 |
$labels[] = $registry[ $event_key ] ?? $event_key; |
| 126 |
} |
| 127 |
|
| 128 |
return esc_html( implode( ', ', $labels ) ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render last updated or created time. |
| 133 |
* |
| 134 |
* @param object $item Webhook row. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
protected function column_updated_at( $item ) { |
| 139 |
$date = ! empty( $item->updated_at ) ? $item->updated_at : $item->created_at; |
| 140 |
|
| 141 |
return $date ? esc_html( get_date_from_gmt( (string) $date, 'Y-m-d H:i:s' ) ) : '—'; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Render fallback columns. |
| 146 |
* |
| 147 |
* @param object $item Webhook row. |
| 148 |
* @param string $column_name Column key. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
protected function column_default( $item, $column_name ) { |
| 153 |
return isset( $item->{$column_name} ) ? esc_html( (string) $item->{$column_name} ) : ''; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Render status filter. |
| 158 |
* |
| 159 |
* @param string $which Table position. |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
protected function extra_tablenav( $which ) { |
| 164 |
if ( 'top' !== $which ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$status = sanitize_key( $_REQUEST['webhook_status'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 169 |
?> |
| 170 |
<div class="alignleft actions"> |
| 171 |
<label class="screen-reader-text" for="webhook_status"><?php esc_html_e( 'Filter by status', 'learnpress' ); ?></label> |
| 172 |
<select id="webhook_status" name="webhook_status"> |
| 173 |
<option value=""><?php esc_html_e( 'All statuses', 'learnpress' ); ?></option> |
| 174 |
<option value="active" <?php selected( $status, WebhookModel::STATUS_ACTIVE ); ?>><?php esc_html_e( 'Active', 'learnpress' ); ?></option> |
| 175 |
<option value="paused" <?php selected( $status, WebhookModel::STATUS_PAUSED ); ?>><?php esc_html_e( 'Paused', 'learnpress' ); ?></option> |
| 176 |
</select> |
| 177 |
<?php submit_button( __( 'Filter', 'learnpress' ), 'button', 'filter_action', false ); ?> |
| 178 |
</div> |
| 179 |
<?php |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Query and prepare table rows. |
| 184 |
* |
| 185 |
* @return void |
| 186 |
* @throws Exception |
| 187 |
*/ |
| 188 |
public function prepare_items() { |
| 189 |
$status = sanitize_key( $_REQUEST['webhook_status'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 190 |
$orderby = sanitize_key( $_REQUEST['orderby'] ?? 'updated_at' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 191 |
$order = sanitize_key( $_REQUEST['order'] ?? 'desc' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 192 |
$search = sanitize_text_field( wp_unslash( $_REQUEST['s'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 193 |
$order_by = array( |
| 194 |
'name' => 'w.' . WebhookFilter::COL_NAME, |
| 195 |
'status' => 'w.' . WebhookFilter::COL_STATUS, |
| 196 |
'updated_at' => 'COALESCE(w.updated_at, w.created_at)', |
| 197 |
); |
| 198 |
|
| 199 |
$filter = new WebhookFilter(); |
| 200 |
$filter->limit = 20; |
| 201 |
$filter->page = $this->get_pagenum(); |
| 202 |
$filter->key_word = $search; |
| 203 |
$filter->order_by = $order_by[ $orderby ] ?? $order_by['updated_at']; |
| 204 |
$filter->order = 'asc' === strtolower( $order ) ? WebhookFilter::ORDER_ASC : WebhookFilter::ORDER_DESC; |
| 205 |
|
| 206 |
if ( in_array( $status, array( WebhookModel::STATUS_ACTIVE, WebhookModel::STATUS_PAUSED ), true ) ) { |
| 207 |
$filter->status = $status; |
| 208 |
} |
| 209 |
|
| 210 |
$total_items = 0; |
| 211 |
$items = WebhookDB::getInstance()->get_webhooks( $filter, $total_items ); |
| 212 |
|
| 213 |
$this->items = is_array( $items ) ? $items : array(); |
| 214 |
$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); |
| 215 |
|
| 216 |
$this->set_pagination_args( |
| 217 |
array( |
| 218 |
'total_items' => $total_items, |
| 219 |
'per_page' => $filter->limit, |
| 220 |
) |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Render empty state. |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public function no_items() { |
| 230 |
echo esc_html__( 'No webhooks found.', 'learnpress' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Decode stored event JSON. |
| 235 |
* |
| 236 |
* @param mixed $events Stored events. |
| 237 |
* |
| 238 |
* @return string[] |
| 239 |
*/ |
| 240 |
protected function decode_events( $events ): array { |
| 241 |
if ( is_string( $events ) ) { |
| 242 |
$events = json_decode( $events, true ); |
| 243 |
} |
| 244 |
|
| 245 |
return is_array( $events ) ? WebhookEvents::sanitize( $events ) : array(); |
| 246 |
} |
| 247 |
} |
| 248 |
|