admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aoli_admin' ), 'i18n' => array( 'selectForm' => __( '— Select Form —', 'abstract-one-crm-form-integration' ), 'selectField' => __( '— Select Field —', 'abstract-one-crm-form-integration' ), 'noForms' => __( 'No forms found for this plugin.', 'abstract-one-crm-form-integration' ), 'noFields' => __( 'No form fields found. Make sure the form has input fields and Contact Form 7 is active.', 'abstract-one-crm-form-integration' ), 'fieldsError' => __( 'Could not load form fields. Please refresh the page and try again.', 'abstract-one-crm-form-integration' ), 'loading' => __( 'Loading…', 'abstract-one-crm-form-integration' ), 'utmAuto' => __( 'Auto from URL / Cookie', 'abstract-one-crm-form-integration' ), ), ) ); if ( 'aoli-workflows' === $page ) { wp_add_inline_script( 'aoli-admin', "jQuery(function ($) { $('#cb-select-all-1, #cb-select-all-2').on('click', function () { $('input[name=\"workflow_ids[]\"]').prop('checked', this.checked); }); });" ); } } /** * Render workflows list or handle actions. */ public function render_workflows_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'abstract-one-crm-form-integration' ) ); } $this->handle_list_actions(); $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( 'edit' === $action ) { $this->render_workflow_form_page(); return; } $per_page = 20; $current_page = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $result = AOLI_Workflow_Repository::list( array( 'search' => $search, 'per_page' => $per_page, 'page' => $current_page, ) ); $workflows = $result['items']; $total = $result['total']; $total_pages = (int) ceil( $total / $per_page ); include AOLI_PLUGIN_DIR . 'admin/views/workflows-list.php'; } /** * Render workflow create/edit form. */ public function render_workflow_form_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'abstract-one-crm-form-integration' ) ); } $workflow = null; $errors = array(); if ( isset( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $workflow = AOLI_Workflow_Repository::get( absint( wp_unslash( $_GET['id'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['aoli_save_workflow'] ) ) { check_admin_referer( 'aoli_save_workflow', 'aoli_workflow_nonce' ); $data = $this->parse_workflow_post(); $errors = AOLI_Workflow_Repository::validate( $data, ! empty( $data['id'] ) ); if ( empty( $errors ) ) { $saved_id = AOLI_Workflow_Repository::save( $data ); if ( $saved_id ) { wp_safe_redirect( add_query_arg( array( 'page' => 'aoli-workflows', 'updated' => '1', ), admin_url( 'admin.php' ) ) ); exit; } $errors['general'] = __( 'Could not save workflow. Please try again.', 'abstract-one-crm-form-integration' ); } $workflow = $data; } include AOLI_PLUGIN_DIR . 'admin/views/workflow-form.php'; } /** * Render instructions page. */ public function render_instructions_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'abstract-one-crm-form-integration' ) ); } include AOLI_PLUGIN_DIR . 'admin/views/instructions.php'; } /** * Handle list page actions. */ private function handle_list_actions() { if ( ! current_user_can( 'manage_options' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( empty( $_POST['page'] ) || 'aoli-workflows' !== sanitize_key( wp_unslash( $_POST['page'] ) ) ) { $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( in_array( $action, array( 'activate', 'deactivate', 'delete' ), true ) && ! empty( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $id = absint( $_GET['id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( 'delete' === $action ) { check_admin_referer( 'aoli_delete_workflow_' . $id ); AOLI_Workflow_Repository::delete( array( $id ) ); } else { check_admin_referer( 'aoli_toggle_workflow_' . $id ); AOLI_Workflow_Repository::update_status( array( $id ), 'activate' === $action ? 'active' : 'inactive' ); } wp_safe_redirect( add_query_arg( array( 'page' => 'aoli-workflows', 'updated' => '1' ), admin_url( 'admin.php' ) ) ); exit; } return; } $action = isset( $_POST['action'] ) ? sanitize_key( wp_unslash( $_POST['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $action = ( '-1' === $action || '' === $action ) && ! empty( $_POST['action2'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended ? sanitize_key( wp_unslash( $_POST['action2'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended : $action; $allowed = array( 'activate', 'deactivate', 'delete' ); if ( in_array( $action, $allowed, true ) && ! empty( $_POST['workflow_ids'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended check_admin_referer( 'bulk-workflows' ); $ids = array_map( 'absint', (array) wp_unslash( $_POST['workflow_ids'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( 'delete' === $action ) { AOLI_Workflow_Repository::delete( $ids ); } else { AOLI_Workflow_Repository::update_status( $ids, 'activate' === $action ? 'active' : 'inactive' ); } wp_safe_redirect( add_query_arg( array( 'page' => 'aoli-workflows', 'updated' => '1' ), admin_url( 'admin.php' ) ) ); exit; } } /** * Parse workflow POST data. * * @return array */ private function parse_workflow_post() { $field_mapping = array(); if ( ! empty( $_POST['field_mapping'] ) && is_array( $_POST['field_mapping'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing foreach ( $_POST['field_mapping'] as $crm_field => $form_field ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $field_mapping[ sanitize_key( $crm_field ) ] = sanitize_text_field( wp_unslash( $form_field ) ); } } return array( 'id' => isset( $_POST['workflow_id'] ) ? absint( $_POST['workflow_id'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing 'name' => isset( $_POST['name'] ) ? wp_unslash( $_POST['name'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'description' => isset( $_POST['description'] ) ? wp_unslash( $_POST['description'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'form_plugin' => isset( $_POST['form_plugin'] ) ? wp_unslash( $_POST['form_plugin'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'form_id' => isset( $_POST['form_id'] ) ? wp_unslash( $_POST['form_id'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'api_key' => isset( $_POST['api_key'] ) ? wp_unslash( $_POST['api_key'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'project_id' => isset( $_POST['project_id'] ) ? wp_unslash( $_POST['project_id'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'campaign_id' => isset( $_POST['campaign_id'] ) ? wp_unslash( $_POST['campaign_id'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'sub_source' => isset( $_POST['sub_source'] ) ? wp_unslash( $_POST['sub_source'] ) : 'Organic', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'status' => isset( $_POST['status'] ) ? wp_unslash( $_POST['status'] ) : 'active', // phpcs:ignore WordPress.Security.NonceVerification.Missing 'field_mapping' => $field_mapping, ); } /** * AJAX: get forms for plugin. */ public function ajax_get_forms() { check_ajax_referer( 'aoli_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'abstract-one-crm-form-integration' ) ), 403 ); } $plugin = isset( $_POST['form_plugin'] ) ? sanitize_key( wp_unslash( $_POST['form_plugin'] ) ) : ''; $forms = array(); if ( 'cf7' === $plugin ) { $forms = AOLI_Form_Handler_CF7::get_forms(); } elseif ( 'wpforms' === $plugin ) { $forms = AOLI_Form_Handler_WPForms::get_forms(); } wp_send_json_success( array( 'forms' => $forms ) ); } /** * AJAX: get fields for a form. */ public function ajax_get_form_fields() { check_ajax_referer( 'aoli_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'abstract-one-crm-form-integration' ) ), 403 ); } $plugin = isset( $_POST['form_plugin'] ) ? sanitize_key( wp_unslash( $_POST['form_plugin'] ) ) : ''; $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; $fields = array(); if ( 'cf7' === $plugin ) { if ( ! class_exists( 'WPCF7' ) && ! post_type_exists( 'wpcf7_contact_form' ) ) { wp_send_json_error( array( 'message' => __( 'Contact Form 7 is not active.', 'abstract-one-crm-form-integration' ) ), 400 ); } $fields = AOLI_Form_Handler_CF7::get_form_fields( $form_id ); } elseif ( 'wpforms' === $plugin ) { if ( ! post_type_exists( 'wpforms' ) ) { wp_send_json_error( array( 'message' => __( 'WPForms is not active.', 'abstract-one-crm-form-integration' ) ), 400 ); } $fields = AOLI_Form_Handler_WPForms::get_form_fields( $form_id ); } else { wp_send_json_error( array( 'message' => __( 'Invalid form plugin.', 'abstract-one-crm-form-integration' ) ), 400 ); } wp_send_json_success( array( 'fields' => $fields ) ); } }