loadVars(); $this->enqueueScripts(); parent::render(); } /** * Load template variables. */ protected function loadVars(): void { $this->vars['workflow_id'] = Request::get( 'workflow_id', 0 ); $this->vars['workflow'] = null; if ( $this->vars['workflow_id'] > 0 ) { $workflow = WorkflowStoreFactory::create()->find( (int) $this->vars['workflow_id'] ); if ( empty( $workflow ) ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'create_automation', 'afromewo_notice' => 'automation_not_found' ) ) ); exit; } $this->vars['workflow'] = $workflow; } //$this->vars['templates'] = $this->getApprovedTemplates(); $this->vars['actions'] = Actions::getActions(); $this->vars['triggers'] = $this->getTriggers(); $this->vars['variables'] = Conditions::getVariables(); $this->vars['operators'] = Conditions::getOperators(); $this->vars['whatsappTemplates'] = $this->getApprovedTemplates(); $this->vars['whatsappTemplatesUrl'] = HubPage::url( array( 'tab' => 'templates' ) ); $this->vars['emailTemplates'] = SendEmailAction::getEmailTemplatesForDropdown(); $this->vars['emailSettingsUrl'] = admin_url( 'admin.php?page=wc-settings&tab=email' ); $this->vars['twilioSettingsUrl'] = HubPage::url( array( 'tab' => 'settings', 'section' => 'twilio' ) ); $this->vars['twilioIsConfigured'] = ( new TwilioClient() )->is_configured(); $this->vars['metaSettingsUrl'] = HubPage::url( array( 'tab' => 'settings', 'section' => 'facebook_app' ) ); $this->vars['metaIsConfigured'] = SecureOptions::get_instance()->get_selected_business() !== null; } /** * Get available workflow triggers. * * @return array */ private function getTriggers(): array { $labels = ['' => '']; $triggers = WorkflowTriggers::getTriggers(); foreach ( $triggers as $trigger => $data ) { $labels[$trigger] = $data['label'] ?? $trigger; } asort( $labels ); return $labels; } /** * Get WhatsApp message templates for the template selector. * * @return array */ private function getApprovedTemplates(): array { $secure = new SecureOptions(); $business = $secure->get_selected_business(); if ( empty( $business['waba_id'] ) ) { return array(); } $response = ( new MetaMessaging() )->getMessageTemplates( $business['waba_id'], array() ); if ( false === $response || empty( $response['data'] ) ) { return array(); } $out = array(); foreach ( $response['data'] as $t ) { if ( $t['status'] !== 'APPROVED' ) { continue; } $language = isset( $t['language'] ) ? $t['language'] : ''; $name = isset( $t['name'] ) ? $t['name'] : ''; $id = isset( $t['id'] ) ? $t['id'] : ''; if ( $name && $id && $language ) { $out[] = array( 'id' => $id, 'name' => $name, 'language' => $language ); } } return $out; } /** * Localize workflow builder script (enqueued earlier on admin_enqueue_scripts by AdminScripts). */ public function enqueueScripts(): void { $workflow_data = null; if ( ! empty( $this->vars['workflow'] ) ) { /** @var Workflow $w */ $w = $this->vars['workflow']; $workflow_data = array( 'id' => $w->getId(), 'name' => $w->getName(), 'trigger' => $w->getTrigger(), 'active' => $w->isActive(), 'conditions' => $w->getConditions(), 'steps' => $w->getSteps(), ); } wp_localize_script( AdminScripts::get_workflow_script_handle(), 'AfroitMessagingWorkflowAutomations', $this->getBuilderScriptData( $workflow_data ) ); } /** * Data passed to the workflow builder script. * * @param array|null $workflow_data Workflow payload or null when creating. * @return array */ protected function getBuilderScriptData( ?array $workflow_data ): array { $data = array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'afromewo_save_workflow' ), 'fetchActionTemplateNonce' => wp_create_nonce( 'afromewo_fetch_action_template' ), 'workflow' => $workflow_data, 'whatsappTemplates' => $this->vars['whatsappTemplates'] ?? array(), 'emailTemplates' => SendEmailAction::getEmailTemplatesForDropdown(), 'variables' => Conditions::getVariables(), 'operators' => Conditions::getOperators(), 'delayUnits' => array( 'minutes' => __( 'Minutes', 'afroit-messaging-workflows' ), 'hours' => __( 'Hours', 'afroit-messaging-workflows' ), 'days' => __( 'Days', 'afroit-messaging-workflows' ), ), 'i18n' => array( 'saving' => __( 'Saving…', 'afroit-messaging-workflows' ), 'saved' => __( 'Workflow saved successfully.', 'afroit-messaging-workflows' ), 'error' => __( 'An error occurred. Please try again.', 'afroit-messaging-workflows' ), 'save' => __( 'Save Workflow', 'afroit-messaging-workflows' ), 'edit' => __( 'Edit', 'afroit-messaging-workflows' ), 'delete' => __( 'Delete', 'afroit-messaging-workflows' ), 'label' => __( 'Step label', 'afroit-messaging-workflows' ), 'cancel' => __( 'Cancel', 'afroit-messaging-workflows' ), 'update' => __( 'Update', 'afroit-messaging-workflows' ), 'actions' => __( 'Actions', 'afroit-messaging-workflows' ), 'workflow' => __( 'Workflow', 'afroit-messaging-workflows' ), 'dragHere' => __( 'Drag actions here to build your workflow', 'afroit-messaging-workflows' ), 'minutes' => __( 'Minutes', 'afroit-messaging-workflows' ), 'template' => __( 'Template', 'afroit-messaging-workflows' ), 'selectTemplate' => __( 'Select template', 'afroit-messaging-workflows' ), 'sendWhatsAppInstructions' => __( 'The customer must have a phone number saved. This action runs in the context of a WooCommerce order.', 'afroit-messaging-workflows' ), 'wait' => __( 'Wait', 'afroit-messaging-workflows' ), 'sendWhatsApp' => __( 'Send WhatsApp Message', 'afroit-messaging-workflows' ), 'sendSms' => __( 'Send SMS', 'afroit-messaging-workflows' ), 'sendEmail' => __( 'Send Email', 'afroit-messaging-workflows' ), 'stop' => __( 'Stop', 'afroit-messaging-workflows' ), 'branch' => __( 'Branch (If/Else)', 'afroit-messaging-workflows' ), 'ifTrue' => __( 'IF TRUE', 'afroit-messaging-workflows' ), 'ifFalse' => __( 'IF FALSE', 'afroit-messaging-workflows' ), 'dragActionsHere' => __( 'Drag actions here', 'afroit-messaging-workflows' ), 'collapse' => __( 'Collapse/Expand', 'afroit-messaging-workflows' ), ), ); /** * Filter localized data for the workflow builder. * * @param array $data Script data. * @param array|null $workflow_data Workflow being edited, if any. */ $filtered = apply_filters( 'afromewo_workflow_builder_script_data', $data, $workflow_data ); return is_array( $filtered ) ? $filtered : $data; } /** * Get view path. * * @return string */ protected function getViewPath() { if ( $this->vars['workflow_id'] > 0 && ! empty( $this->vars['workflow'] ) ) { return AFROMEWO_PLUGIN_DIR . 'includes/Admin/Automation/Views/edit-automation.php'; } return AFROMEWO_PLUGIN_DIR . 'includes/Admin/Automation/Views/create-automation.php'; } }