__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ), 403 ); } check_ajax_referer( 'afromewo_create_template', 'nonce' ); $name = Request::post( 'template_name', '' ); $body = Request::post_textarea( 'template_body', '' ); $language = Request::post( 'template_language', 'en_US' ); $category = Request::post( 'template_category', 'UTILITY' ); $header = Request::post( 'template_header', '' ); $footer = Request::post( 'template_footer', '' ); $buttons = array(); $buttons_raw = Request::post_json( 'template_buttons' ); if ( ! empty( $buttons_raw ) ) { foreach ( $buttons_raw as $btn ) { if ( ! empty( $btn['type'] ) ) { $buttons[] = array( 'type' => sanitize_text_field( $btn['type'] ), 'text' => isset( $btn['text'] ) ? sanitize_text_field( $btn['text'] ) : '', 'url' => isset( $btn['url'] ) ? sanitize_text_field( $btn['url'] ) : '', 'phone_number' => isset( $btn['phone_number'] ) ? sanitize_text_field( $btn['phone_number'] ) : '', 'example' => isset( $btn['example'] ) ? sanitize_text_field( $btn['example'] ) : '', ); } } } if ( empty( $name ) ) { wp_send_json_error( array( 'message' => __( 'Template name is required.', 'afroit-messaging-workflows' ) ), 400 ); } if ( ! preg_match( '/^[a-z0-9_]+$/', $name ) ) { wp_send_json_error( array( 'message' => __( 'Template name must contain only lowercase letters, numbers, and underscores.', 'afroit-messaging-workflows' ) ), 400 ); } $secure = new SecureOptions(); $business = $secure->get_selected_business(); if ( empty( $business['waba_id'] ) ) { wp_send_json_error( array( 'message' => __( 'No WhatsApp Business Account selected.', 'afroit-messaging-workflows' ) ), 400 ); } $payload = array(); try { $meta_messaging = new MetaMessaging(); $payload = $meta_messaging->createTemplate( $name, $language, $category, $header, $body, $footer, $buttons ); } catch (\Exception $e) { Log::error( 'Template creation failed', array( 'error' => $e->getMessage() ) ); wp_send_json_error( array( 'message' => __( 'Failed to create template. Please try again.', 'afroit-messaging-workflows' ) ), 400 ); } wp_send_json_success( array( 'payload' => $payload, 'message' => __( 'Template created successfully.', 'afroit-messaging-workflows' ), 'redirect' => HubPage::url( array( 'tab' => 'templates' ) ), ) ); } /** * Save workflow via AJAX. * * @return void */ public function save_workflow() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ), 403 ); } check_ajax_referer( 'afromewo_save_workflow', 'nonce' ); $steps_raw = Request::post_raw( 'steps', '' ); $conditions_raw = Request::post_raw( 'conditions', '' ); $name = Request::post( 'name', '' ); $trigger = Request::post( 'trigger', '' ); $active = '1' === Request::post( 'active', '1' ); $conditions = array(); $steps = array(); $errors = array(); $variables = Conditions::getVariables(); $operators = Conditions::getOperators(); if ( empty( $name ) ) { $errors[] = __( 'Workflow name is required.', 'afroit-messaging-workflows' ); } if ( empty( $trigger ) ) { $errors[] = __( 'Workflow trigger is required.', 'afroit-messaging-workflows' ); } if ( '' !== $steps_raw ) { $decoded = json_decode( stripslashes( $steps_raw ), true ); if ( is_array( $decoded ) ) { foreach ( $decoded as $step ) { if ( ! empty( $step['type'] ) ) { $action = Actions::getActions( $step['type'] ); if ( ! $action ) { /* translators: %s: Workflow action type. */ $errors[] = sprintf( __( 'Invalid action type: %s', 'afroit-messaging-workflows' ), $step['type'] ); continue; } $action = new $action['action_class'](); if ( ! $action->validate( $step ) ) { $errors = array_merge( $errors, $action->getValidationErrors() ); continue; } // @TODO: valiate condition: variable, operator, value $steps[] = $action->getStepData( $step ); } } } } if ( '' !== $conditions_raw ) { $decoded = json_decode( stripslashes( $conditions_raw ), true ); if ( is_array( $decoded ) ) { foreach ( $decoded as $c ) { // check if variable and operator exists if (!isset( $variables[$c['variable']] ) || !isset( $operators[$c['operator']] )) { /* translators: %s: Condition variable name. */ $errors[] = sprintf( __( 'Invalid Condition for variable: %s', 'afroit-messaging-workflows' ), $c['variable'] ); continue; } $conditions[] = array( 'variable' => sanitize_text_field( $c['variable'] ), 'operator' => sanitize_text_field( $c['operator'] ), 'value' => isset( $c['value'] ) ? sanitize_text_field( $c['value'] ) : '', ); } } } if ( empty( $steps ) ) { $errors[] = __( 'Workflow steps are required.', 'afroit-messaging-workflows' ); } if ( ! empty( $errors ) ) { wp_send_json_error( array( 'message' => implode( '
', array_map( 'esc_html', $errors ) ) ), 400 ); } $workflow_id = absint( Request::post( 'workflow_id', 0 ) ); $store = WorkflowStoreFactory::create(); $result = $store->save( array( 'workflow_id' => $workflow_id, 'name' => $name, 'trigger' => $trigger, 'steps' => $steps, 'conditions' => $conditions, 'is_active' => $active, ) ); if ( empty( $result['success'] ) ) { $message = isset( $result['error_message'] ) && '' !== (string) $result['error_message'] ? (string) $result['error_message'] : __( 'Failed to save workflow.', 'afroit-messaging-workflows' ); wp_send_json_error( array( 'message' => esc_html( $message ) ), 500 ); } wp_send_json_success( array( 'message' => __( 'Workflow saved successfully.', 'afroit-messaging-workflows' ), 'redirect' => HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'workflow_created' ) ), ) ); } public function capture_cart_email() { check_ajax_referer( 'capture_cart_email', 'nonce' ); $email = sanitize_email( Request::post( 'email', '' ) ); $phone = Request::post( 'phone', '' ); if ( ! $email && ! $phone ) { wp_send_json_error(); } $session_id = WC()->session->get_customer_id(); if ( ! $session_id ) { wp_send_json_error(); } $service = new AbandonedCartService(); $service->captureCart( $session_id, $email, $phone ); Log::info( 'Email and phone captured for cart tracking', array( 'session_id' => $session_id ) ); wp_send_json_success(); } }