migrate_legacy_options(); $this->settings_page = new Settings_Page(); add_action( 'admin_notices', array( $this, 'render_dependency_notice' ) ); add_action( 'elementor_pro/forms/actions/register', array( $this, 'register_form_action' ) ); } /** * Moves settings saved under the plugin's original development prefix. */ private function migrate_legacy_options(): void { $legacy_options = array( 'ebs_brevo_api_key' => self::OPTION_API_KEY, 'ebs_brevo_lists' => self::OPTION_LISTS, 'ebs_delete_data_on_uninstall' => self::OPTION_DELETE_DATA, ); foreach ( $legacy_options as $legacy_name => $current_name ) { $legacy_value = get_option( $legacy_name, null ); if ( null === $legacy_value ) { continue; } if ( null === get_option( $current_name, null ) ) { add_option( $current_name, $legacy_value, '', false ); } delete_option( $legacy_name ); } } /** * Registers the Brevo action with Elementor Pro Forms. * * @param object $form_actions_registrar Elementor form action registrar. */ public function register_form_action( $form_actions_registrar ): void { if ( ! class_exists( '\ElementorPro\Modules\Forms\Classes\Action_Base' ) ) { return; } require_once ABDWP_BECS_PLUGIN_DIR . 'includes/class-elementor-action.php'; $form_actions_registrar->register( new Elementor_Action() ); } /** * Shows an admin notice when Elementor Pro Forms is unavailable. */ public function render_dependency_notice(): void { if ( ! current_user_can( 'activate_plugins' ) ) { return; } if ( did_action( 'elementor/loaded' ) && class_exists( '\ElementorPro\Plugin' ) ) { return; } $screen = get_current_screen(); if ( ! $screen || 'plugins' !== $screen->id ) { return; } printf( '

%s

', esc_html__( 'AbdullahWP Contact Sync for Brevo and Elementor Forms requires Elementor and Elementor Pro Forms to add the Brevo form action.', 'abdullahwp-contact-sync-for-brevo-elementor' ) ); } /** * Gets the configured Brevo API key. */ public static function get_api_key(): string { if ( defined( 'ABDWP_BECS_BREVO_API_KEY' ) && ABDWP_BECS_BREVO_API_KEY ) { return (string) ABDWP_BECS_BREVO_API_KEY; } // Backward compatibility for installations configured before version 1.0.0. if ( defined( 'BREVO_API_KEY' ) && BREVO_API_KEY ) { return (string) BREVO_API_KEY; } return (string) get_option( self::OPTION_API_KEY, '' ); } /** * Gets Brevo lists cached by the settings page. */ public static function get_saved_lists(): array { $lists = get_option( self::OPTION_LISTS, array() ); return is_array( $lists ) ? $lists : array(); } /** * Sanitizes Brevo list records before saving them. * * @param array $lists Raw Brevo list records. */ public static function sanitize_lists( array $lists ): array { $clean = array(); foreach ( $lists as $list ) { if ( ! is_array( $list ) || empty( $list['id'] ) ) { continue; } $id = absint( $list['id'] ); if ( ! $id ) { continue; } $clean[] = array( 'id' => $id, 'name' => sanitize_text_field( $list['name'] ?? sprintf( 'List %d', $id ) ), ); } return $clean; } }