| 1 |
<?php |
| 2 |
|
| 3 |
use LearnPress\Webhook\WebhookEvents; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Admin controller for LearnPress webhook management. |
| 9 |
*/ |
| 10 |
class LP_Admin_Webhooks { |
| 11 |
/** |
| 12 |
* @var self|null |
| 13 |
*/ |
| 14 |
protected static $instance; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $required_capability = 'manage_options'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get singleton instance. |
| 23 |
* |
| 24 |
* @return self |
| 25 |
*/ |
| 26 |
public static function instance(): self { |
| 27 |
if ( ! self::$instance ) { |
| 28 |
self::$instance = new self(); |
| 29 |
} |
| 30 |
|
| 31 |
return self::$instance; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register dependencies and hooks. |
| 36 |
*/ |
| 37 |
protected function __construct() { |
| 38 |
add_action( 'admin_enqueue_scripts', array( $this, 'localize_admin_script' ) ); |
| 39 |
|
| 40 |
require_once LP_PLUGIN_PATH . 'inc/admin/class-lp-admin-webhooks-table-list.php'; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Localize webhook CRUD settings for admin JavaScript. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function localize_admin_script(): void { |
| 49 |
if ( ! $this->is_webhook_settings_screen() || ! $this->is_webhook_integration_enabled() || ! wp_script_is( 'lp-admin-webhooks', 'enqueued' ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
wp_localize_script( |
| 53 |
'lp-admin-webhooks', |
| 54 |
'lpWebhooksSettings', |
| 55 |
array( |
| 56 |
'is_webhook_section' => true, |
| 57 |
'actions' => array( |
| 58 |
'create' => 'create_webhook', |
| 59 |
'update' => 'update_webhook', |
| 60 |
'delete' => 'delete_webhook', |
| 61 |
'regenerate' => 'regenerate_webhook_secret', |
| 62 |
), |
| 63 |
'i18n' => array( |
| 64 |
'processing' => __( 'Processing...', 'learnpress' ), |
| 65 |
'request_failed' => __( 'Request failed. Please try again.', 'learnpress' ), |
| 66 |
'confirm_delete' => __( 'Delete this webhook?', 'learnpress' ), |
| 67 |
'confirm_regenerate' => __( 'Regenerate this webhook secret?', 'learnpress' ), |
| 68 |
'copy_success' => __( 'Copied.', 'learnpress' ), |
| 69 |
'copy_fallback' => __( 'Copy this value manually.', 'learnpress' ), |
| 70 |
'create_title' => __( 'Create Webhook', 'learnpress' ), |
| 71 |
'created_title' => __( 'Webhook Created', 'learnpress' ), |
| 72 |
'edit_title' => __( 'Edit Webhook', 'learnpress' ), |
| 73 |
'create_button' => __( 'Create Webhook', 'learnpress' ), |
| 74 |
'update_button' => __( 'Update Webhook', 'learnpress' ), |
| 75 |
'secret_create_placeholder' => __( 'Leave blank to auto-generate a secret.', 'learnpress' ), |
| 76 |
'secret_edit_placeholder' => __( 'Leave blank to keep the current secret.', 'learnpress' ), |
| 77 |
), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Render webhook management after Advanced webhook settings. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function render_page(): void { |
| 88 |
if ( ! current_user_can( $this->required_capability ) ) { |
| 89 |
wp_die( esc_html__( 'Sorry, you are not allowed to manage webhooks.', 'learnpress' ) ); |
| 90 |
} |
| 91 |
if ( ! $this->is_webhook_integration_enabled() ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$table = new LP_Admin_Webhooks_Table_List(); |
| 96 |
$table->prepare_items(); |
| 97 |
$events = WebhookEvents::all(); |
| 98 |
|
| 99 |
require LP_PLUGIN_PATH . 'inc/admin/views/settings/webhooks-form.php'; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check whether current request is the Advanced webhook section. |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
protected function is_webhook_settings_screen(): bool { |
| 108 |
$page = sanitize_key( $_REQUEST['page'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 109 |
$tab = sanitize_key( $_REQUEST['tab'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 110 |
$section = sanitize_key( $_REQUEST['section'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 111 |
|
| 112 |
return 'learn-press-settings' === $page && 'advanced' === $tab && 'webhook' === $section; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Check whether webhook integration is enabled. |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
protected function is_webhook_integration_enabled(): bool { |
| 121 |
return 'yes' === LP_Settings::get_option( 'enable_webhook_integration', 'no' ); |
| 122 |
} |
| 123 |
} |
| 124 |
|