'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
)
);
register_setting(
'abdwp_becs_settings',
Plugin::OPTION_DELETE_DATA,
array(
'type' => 'boolean',
'sanitize_callback' => static function ( $value ): bool {
return (bool) $value;
},
'default' => false,
)
);
}
/**
* Enqueues settings page assets.
*
* @param string $hook_suffix Current admin page hook suffix.
*/
public function enqueue_assets( string $hook_suffix ): void {
if ( 'settings_page_' . self::PAGE_SLUG !== $hook_suffix ) {
return;
}
wp_enqueue_style(
'abdwp-becs-admin',
ABDWP_BECS_PLUGIN_URL . 'assets/admin.css',
array(),
ABDWP_BECS_VERSION
);
wp_enqueue_script(
'abdwp-becs-admin',
ABDWP_BECS_PLUGIN_URL . 'assets/admin.js',
array( 'jquery' ),
ABDWP_BECS_VERSION,
true
);
wp_localize_script(
'abdwp-becs-admin',
'abdwpBecsAdmin',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( Plugin::NONCE_ACTION ),
'i18n' => array(
'working' => __( 'Working...', 'abdullahwp-contact-sync-for-brevo-elementor' ),
),
)
);
}
/**
* Renders the settings page.
*/
public function render(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$constant_defined = ( defined( 'ABDWP_BECS_BREVO_API_KEY' ) && ABDWP_BECS_BREVO_API_KEY ) || ( defined( 'BREVO_API_KEY' ) && BREVO_API_KEY );
$lists = Plugin::get_saved_lists();
?>
guard_ajax_request();
$client = new Brevo_Client( Plugin::get_api_key() );
$result = $client->get_account();
if ( ! $result['success'] ) {
wp_send_json_error( array( 'message' => $result['message'] ) );
}
wp_send_json_success(
array(
'message' => __( 'Connection successful. Brevo accepted the API key.', 'abdullahwp-contact-sync-for-brevo-elementor' ),
)
);
}
/**
* AJAX handler for syncing Brevo lists.
*/
public function ajax_sync_lists(): void {
$this->guard_ajax_request();
$client = new Brevo_Client( Plugin::get_api_key() );
$result = $client->get_lists();
if ( ! $result['success'] ) {
wp_send_json_error( array( 'message' => $result['message'] ) );
}
$lists = Plugin::sanitize_lists( $result['body'] );
update_option( Plugin::OPTION_LISTS, $lists, false );
wp_send_json_success(
array(
'message' => sprintf(
/* translators: %d: number of synced lists. */
_n( 'Synced %d Brevo list.', 'Synced %d Brevo lists.', count( $lists ), 'abdullahwp-contact-sync-for-brevo-elementor' ),
count( $lists )
),
)
);
}
/**
* Verifies nonce and capability for admin AJAX actions.
*/
private function guard_ajax_request(): void {
check_ajax_referer( Plugin::NONCE_ACTION, 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => __( 'You do not have permission to manage this integration.', 'abdullahwp-contact-sync-for-brevo-elementor' ) ), 403 );
}
}
}