$extra Extra query args (e.g. afromewo_notice). * @return string */ private function redirect_hub_settings( string $section, array $extra = array() ): string { return HubPage::url( array_merge( array( 'tab' => 'settings', 'section' => $this->hub_settings_section_from_posted_tab( $section ), ), $extra ) ); } /** * Register hooks. * * @return void */ public function register() { add_action( 'admin_post_afromewo_save_settings', array( $this, 'save_settings' ) ); add_action( 'admin_post_afromewo_save_business', array( $this, 'save_business' ) ); add_action( 'admin_post_wp_wacc_delete_template', array( $this, 'delete_template' ) ); add_action( 'admin_post_afromewo_delete_automation', array( $this, 'delete_automation' ) ); add_action( 'admin_post_afromewo_toggle_automation', array( $this, 'toggle_automation' ) ); add_action( 'admin_post_afromewo_delete_execution_queue_item', array( $this, 'delete_execution_queue_item' ) ); add_action( 'admin_post_afromewo_dismiss_pro_upgrade', array( $this, 'dismiss_pro_upgrade' ) ); } /** * Dismiss the Pro add-on notice on the workflow builder (per user). * * @return void */ public function dismiss_pro_upgrade(): void { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } check_admin_referer( 'afromewo_dismiss_pro_upgrade' ); ProUpgrade::dismiss_for_current_user(); wp_safe_redirect( ProUpgrade::builder_url( absint( Request::get( 'workflow_id', '0' ) ) ) ); exit; } /** * Delete a workflow queue item. * * @return void */ public function delete_execution_queue_item() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } $id = (int) Request::get( 'queue_id', 0 ); $redirect = Request::get_raw( 'redirect', '' ); $redirect = '' !== $redirect ? rawurldecode( $redirect ) : HubPage::url( array( 'tab' => 'automations', 'section' => 'executed' ) ); if ( $id <= 0 ) { wp_safe_redirect( add_query_arg( array( 'afromewo_notice' => 'queue_delete_missing_id' ), $redirect ) ); exit; } check_admin_referer( 'afromewo_delete_execution_queue_item_' . $id ); global $wpdb; $table = $wpdb->prefix . 'afromewo_workflow_queue'; $deleted = (bool) $wpdb->delete( $table, array( 'id' => $id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom plugin table; delete by primary key, not a cached WP object. $notice = $deleted ? 'queue_item_deleted' : 'queue_item_delete_failed'; wp_safe_redirect( add_query_arg( array( 'afromewo_notice' => $notice ), $redirect ) ); exit; } /** * Save settings posted from the admin page. * * @return void */ public function save_settings() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } check_admin_referer( 'afromewo_save_settings' ); $tab = Request::post( '_tab', 'prerequisites' ); // Only update values that were actually posted from the current tab, // so saving one tab doesn't wipe fields from another. $settings = PluginSettings::get_instance(); $needs_reconnect = false; if ( 'facebook_app' === $tab ) { if ( Request::has_post( 'afromewo_facebook_app_id' ) ) { $facebook_app_id = Request::post( 'afromewo_facebook_app_id', '' ); $settings->set_facebook_app_id( $facebook_app_id ); $needs_reconnect = true; } if ( Request::has_post( 'afromewo_return_url' ) ) { $return_url = Request::post_url( 'afromewo_return_url', '' ); $settings->set_return_url( $return_url ); $needs_reconnect = true; } $facebook_app_secret = Request::post_raw( 'afromewo_facebook_app_secret', '' ); if ( '' !== trim( $facebook_app_secret ) ) { ( new SecureOptions() )->set( 'facebook_app_secret', $facebook_app_secret ); $needs_reconnect = true; } // System user token (manual entry; encrypted). Same tab only. $system_user_token = Request::post_raw( 'afromewo_system_user_token', '' ); if ( '' !== trim( $system_user_token ) ) { // @TODO validate the system user token before saving ( new SecureOptions() )->set( 'system_user_token', $system_user_token ); $needs_reconnect = true; } } if ( 'twilio' === $tab ) { $twilio_account_sid = trim( Request::post( 'afromewo_twilio_account_sid', '' ) ); $twilio_auth_token = trim( Request::post( 'afromewo_twilio_auth_token', '' ) ); $twilio_from_number = Request::post( 'afromewo_twilio_from_number', '' ); if ( '' !== $twilio_account_sid ) { ( new SecureOptions() )->set( 'twilio_account_sid', $twilio_account_sid ); } if ( '' !== $twilio_auth_token ) { ( new SecureOptions() )->set( 'twilio_auth_token', $twilio_auth_token ); } if ( Request::has_post( 'afromewo_twilio_from_number' ) ) { $settings->set_twilio_from_number( $twilio_from_number ); } } if ( 'prerequisites' === $tab ) { $settings->set_debug_logging( (string) Request::post( 'afromewo_enable_debug_logging', '0' ) === '1' ? 'yes' : 'no' ); $settings->set_ask_consent( (string) Request::post( 'afromewo_ask_consent', '0' ) === '1' ? 'yes' : 'no' ); $settings->set_check_tracking_consent( (string) Request::post( 'afromewo_check_tracking_consent', '0' ) === '1' ? 'yes' : 'no' ); } // If connection-related settings changed, force a reconnect. if ( $needs_reconnect ) { $settings->set_connected( false ); } wp_safe_redirect( $this->redirect_hub_settings( $tab, array( 'afromewo_saved' => '1' ) ) ); exit; } public function save_business() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } check_admin_referer( 'afromewo_save_business' ); $selected_business = Request::post( 'wp_waba_selected_business', '' ); if ( ! $selected_business ) { wp_safe_redirect( $this->redirect_hub_settings( 'facebook_business', array( 'afromewo_notice' => 'no_business_selected' ) ) ); exit; } // @TODO split the business to the three parts and validate them $business_parts = explode( ',', $selected_business ); if ( count( $business_parts ) !== 3 ) { wp_safe_redirect( $this->redirect_hub_settings( 'facebook_business', array( 'afromewo_notice' => 'invalid_business' ) ) ); exit; } $secure_options = new SecureOptions(); $secure_options->set( 'selected_business', $selected_business ); list( $business_id, $waba_id, $phone_id ) = explode( ',', $selected_business ); $user_access_token = $secure_options->get( 'user_access_token', '' ); if ( ! $user_access_token ) { wp_safe_redirect( $this->redirect_hub_settings( 'facebook_business', array( 'afromewo_notice' => 'no_user_access_token' ) ) ); exit; } $meta_onboarding = new MetaOnboarding(); $system_user_and_token = $meta_onboarding->ensureSystemUserAndToken( $business_id, $waba_id, $phone_id, $user_access_token ); if ( ! $system_user_and_token ) { wp_safe_redirect( $this->redirect_hub_settings( 'facebook_business', array( 'afromewo_notice' => 'failed_to_ensure_system_user_and_token' ) ) ); exit; } $secure_options->set( 'system_user_id', $system_user_and_token['system_user_id'] ); $secure_options->set( 'system_user_token', $system_user_and_token['system_user_token'] ); wp_safe_redirect( $this->redirect_hub_settings( 'facebook_business', array( 'afromewo_notice' => 'business_selected' ) ) ); exit; } /** * Delete a message template. * * @return void */ public function delete_template() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } $id = Request::get( 'id', '' ); $name = Request::get( 'name', '' ); if ( '' === $id || '' === $name ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'templates', 'afromewo_notice' => 'template_delete_missing_id_or_name', ) ) ); exit; } check_admin_referer( 'delete_template_' . $id ); $secure = new SecureOptions(); $business = $secure->get_selected_business(); if ( empty( $business['waba_id'] ) ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'templates', 'afromewo_notice' => 'no_business', ) ) ); exit; } $messaging = new MetaMessaging(); if ( ! $messaging->deleteTemplate( $business['waba_id'], $id, $name ) ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'templates', 'afromewo_notice' => 'template_delete_failed', ) ) ); exit; } wp_safe_redirect( HubPage::url( array( 'tab' => 'templates', 'afromewo_notice' => 'template_deleted', ) ) ); exit; } /** * Delete an automation workflow. * * @return void */ public function delete_automation() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } $id = Request::get( 'workflow_id', 0 ); if ( ! $id ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_delete_missing_id', ) ) ); exit; } check_admin_referer( 'delete_automation_' . $id ); $store = WorkflowStoreFactory::create(); $result = $store->delete( (int) $id ); if ( empty( $result['success'] ) ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_delete_failed', ) ) ); exit; } wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_deleted', ) ) ); exit; } /** * Toggle automation active status. * * @return void */ public function toggle_automation() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'afroit-messaging-workflows' ) ); } $id = Request::get( 'workflow_id', 0 ); if ( ! $id ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_toggle_missing_id', ) ) ); exit; } check_admin_referer( 'toggle_automation_' . $id ); $store = WorkflowStoreFactory::create(); $workflow = $store->find( (int) $id ); if ( ! $workflow ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_not_found', ) ) ); exit; } $new_active = ! $workflow->isActive(); $result = $store->update_active_status( (int) $id, $new_active ); if ( empty( $result['success'] ) ) { wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => 'automation_toggle_failed', ) ) ); exit; } // @TODO: maybe delete all the workflow queue items for this workflow if deactivated. // FIXME: alert the user that events already triggered will continue to be processed. $notice = $new_active ? 'automation_activated' : 'automation_deactivated'; wp_safe_redirect( HubPage::url( array( 'tab' => 'automations', 'afromewo_notice' => $notice, ) ) ); exit; } }