maybe_clear_log(); register_setting( 'lacrm_forms_settings_group', self::OPTION_KEY, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); add_settings_section( 'lacrm_forms_main', __( 'API', 'aal-connector-for-lacrm' ), array( $this, 'section_main_cb' ), 'aal-connector-for-lacrm' ); add_settings_field( 'lacrm_api_key', __( 'LACRM API key', 'aal-connector-for-lacrm' ), array( $this, 'field_api_key' ), 'aal-connector-for-lacrm', 'lacrm_forms_main', array( 'label_for' => 'lacrm_api_key' ) ); add_settings_field( 'lacrm_default_owner', __( 'Default Assignee (optional)', 'aal-connector-for-lacrm' ), array( $this, 'field_default_owner' ), 'aal-connector-for-lacrm', 'lacrm_forms_main', array( 'label_for' => 'lacrm_default_owner' ) ); add_settings_field( 'lacrm_enable_logging', __( 'Enable logging', 'aal-connector-for-lacrm' ), array( $this, 'field_enable_logging' ), 'aal-connector-for-lacrm', 'lacrm_forms_main', array( 'label_for' => 'lacrm_enable_logging' ) ); add_settings_section( 'lacrm_forms_plugins', __( 'Installed form plugins', 'aal-connector-for-lacrm' ), array( $this, 'section_plugins_cb' ), 'aal-connector-for-lacrm' ); add_settings_field( 'lacrm_form_plugins', __( 'Forms', 'aal-connector-for-lacrm' ), array( $this, 'field_form_plugins' ), 'aal-connector-for-lacrm', 'lacrm_forms_plugins' ); } /** * Sanitize settings. * * @param array $input Raw input. * @return array */ public function sanitize_settings( array $input ): array { $out = array( 'api_key' => isset( $input['api_key'] ) ? sanitize_text_field( $input['api_key'] ) : '', 'default_owner' => isset( $input['default_owner'] ) ? sanitize_text_field( $input['default_owner'] ) : '', 'enable_logging' => ! empty( $input['enable_logging'] ), ); return $out; } /** * Get detected form plugins with their admin URLs. * * @return array */ public function get_installed_form_plugins(): array { $plugins = array(); if ( class_exists( 'WPCF7' ) ) { $plugins[] = array( 'name' => __( 'Contact Form 7', 'aal-connector-for-lacrm' ), 'url' => admin_url( 'admin.php?page=wpcf7' ), 'slug' => 'cf7', ); } if ( class_exists( 'GFForms' ) ) { $plugins[] = array( 'name' => __( 'Gravity Forms', 'aal-connector-for-lacrm' ), 'url' => admin_url( 'admin.php?page=gf_edit_forms' ), 'slug' => 'gf', ); } return $plugins; } public function section_main_cb(): void { $programmer_api_url = 'https://account.lessannoyingcrm.com/app/Settings/Api'; echo wp_kses_post( '

' . sprintf( /* translators: %s: link to LACRM Programmer API (Settings → Programmer API) */ __( 'Create API key from LACRM %s.', 'aal-connector-for-lacrm' ), '' . esc_html__( 'Settings → Programmer API', 'aal-connector-for-lacrm' ) . '' ) . '

' ); echo '

' . esc_html__( 'Compatible with Contact Form 7 and Gravity Forms.', 'aal-connector-for-lacrm' ) . '

'; } public function section_plugins_cb(): void { echo '

' . esc_html__( 'We detected the following compatible form plugins with AAL Connector For LACRM.', 'aal-connector-for-lacrm' ) . '

'; } public function field_form_plugins(): void { $plugins = $this->get_installed_form_plugins(); if ( empty( $plugins ) ) { echo '

' . esc_html__( 'No supported form plugins detected. Install Contact Form 7 or Gravity Forms to sync submissions to LACRM.', 'aal-connector-for-lacrm' ) . '

'; return; } $logos = array( 'cf7' => 'contact-form-7-logo.png', 'gf' => 'gravity-form-logo.png', ); $images_url = plugin_dir_url( LACRM_FORMS_PLUGIN_FILE ) . 'assets/images/'; echo ''; echo '

' . esc_html__( 'Please enable sync for all the forms that needs to be synced with LACRM from the form\'s settings.', 'aal-connector-for-lacrm' ) . '

'; } public function field_api_key(): void { $val = lacrm_forms_get_option( 'api_key', '' ); ?>

false, 'users' => array() ); if ( $api_key !== '' ) { $api = new LACRM_Forms_API( (string) $api_key ); $users = $api->get_users(); } ?>

clear(); wp_safe_redirect( remove_query_arg( array( 'lacrm_clear_log', '_wpnonce', 'paged' ), admin_url( 'options-general.php?page=aal-connector-for-lacrm' ) ) ); exit; } /** * Verify API key via AJAX. On success, saves the API key globally and returns users for the Default Assignee dropdown. */ public function ajax_verify_api(): void { check_ajax_referer( 'lacrm_verify_api', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aal-connector-for-lacrm' ) ) ); } $api_key = isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : ''; if ( $api_key === '' ) { wp_send_json_error( array( 'message' => __( 'Please enter an API key first.', 'aal-connector-for-lacrm' ) ) ); } $api = new LACRM_Forms_API( $api_key ); $users = $api->get_users(); if ( $users['success'] ) { // Save API key globally so it persists and Default Assignee dropdown can show users. $options = get_option( self::OPTION_KEY, array() ); $options = is_array( $options ) ? $options : array(); $options['api_key'] = $api_key; update_option( self::OPTION_KEY, $options ); delete_transient( 'lacrm_cf_contact_defs' ); $users_list = array(); if ( ! empty( $users['users'] ) && is_array( $users['users'] ) ) { foreach ( $users['users'] as $user ) { $uid = isset( $user['UserId'] ) ? (string) $user['UserId'] : ''; $name = trim( ( $user['FirstName'] ?? '' ) . ' ' . ( $user['LastName'] ?? '' ) ); if ( $name === '' && $uid !== '' ) { $name = $uid; } if ( $uid !== '' ) { $users_list[] = array( 'value' => $uid, 'label' => $name ); } } } wp_send_json_success( array( 'message' => __( 'API key saved. Connection successful.', 'aal-connector-for-lacrm' ), 'users' => $users_list, ) ); } $msg = isset( $users['error_description'] ) ? $users['error_description'] : __( 'Invalid API key or connection failed.', 'aal-connector-for-lacrm' ); wp_send_json_error( array( 'message' => $msg ) ); } /** * Enqueue admin assets. * * @param string $hook Page hook. */ public function enqueue_assets( string $hook ): void { if ( $hook !== 'settings_page_aal-connector-for-lacrm' ) { return; } wp_enqueue_style( 'lacrm-connector-admin-settings', plugin_dir_url( LACRM_FORMS_PLUGIN_FILE ) . 'assets/css/admin-settings.css', array(), LACRM_FORMS_VERSION ); wp_enqueue_script( 'lacrm-connector-settings', plugin_dir_url( LACRM_FORMS_PLUGIN_FILE ) . 'assets/js/settings.js', array( 'jquery' ), LACRM_FORMS_VERSION, true ); wp_localize_script( 'lacrm-connector-settings', 'lacrmFormsSettings', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'lacrm_verify_api' ), 'i18n' => array( 'verifying' => __( 'Verifying…', 'aal-connector-for-lacrm' ), 'error' => __( 'Error', 'aal-connector-for-lacrm' ), 'enter_key' => __( 'Please enter an API key first.', 'aal-connector-for-lacrm' ), 'conn_failed' => __( 'Invalid API key or connection failed.', 'aal-connector-for-lacrm' ), 'req_failed' => __( 'Request failed.', 'aal-connector-for-lacrm' ), ), ) ); } /** * Render settings page. */ public function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings_url = admin_url( 'options-general.php?page=aal-connector-for-lacrm' ); ?>

render_log_block( $settings_url ); ?>
get_log_page( $per_page, $current_page ); $log_entries = $result['items']; $total = $result['total']; $total_pages = $total > 0 ? (int) ceil( $total / $per_page ) : 1; $clear_url = wp_nonce_url( add_query_arg( 'lacrm_clear_log', '1', $base_url ), 'lacrm_clear_log' ); ?>

'lacrm-level-info', 'error' => 'lacrm-level-error', 'warning' => 'lacrm-level-warning', 'debug' => 'lacrm-level-debug', )[ $level ] ?? 'lacrm-level-info'; $ctx = is_array( $entry['context'] ?? null ) ? $entry['context'] : array(); ?>
$v ) : ?> :
1 ) : ?>
1 ) { $prev_url = $current_page === 2 ? $base_url : add_query_arg( 'paged', $current_page - 1, $base_url ); echo '« '; } echo ''; /* translators: 1: current page, 2: total pages */ echo esc_html( sprintf( __( 'Page %1$s of %2$s', 'aal-connector-for-lacrm' ), number_format_i18n( $current_page ), number_format_i18n( $total_pages ) ) ); echo ''; if ( $current_page < $total_pages ) { $next_url = add_query_arg( 'paged', $current_page + 1, $base_url ); echo ' »'; } ?>
id !== 'settings_page_aal-connector-for-lacrm' ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display-only flag set by options.php after save. if ( empty( $_GET['settings-updated'] ) ) { return; } ?>

' . esc_html__( 'Settings', 'aal-connector-for-lacrm' ) . ''; return $links; } }