slug() ] = $obj; } } return self::$items; } /** @return Integration|null */ public static function get( $slug ) { $all = self::all(); return $all[ $slug ] ?? null; } /** @return array Category labels. */ public static function categories() { return array( 'crm' => __( 'CRM', 'fieldwren' ), 'email' => __( 'Email marketing', 'fieldwren' ), 'chat' => __( 'Chat & SMS', 'fieldwren' ), 'automation' => __( 'Automation', 'fieldwren' ), 'sheets' => __( 'Sheets & databases', 'fieldwren' ), 'other' => __( 'Productivity & other', 'fieldwren' ), ); } /** Each integration answers its own feeds. */ public static function register_feed_hooks() { foreach ( self::all() as $slug => $int ) { add_filter( 'fwren/feed/' . $slug, array( $int, 'handle' ), 10, 4 ); } } /** @return mixed Module setting (integrations.general). */ public static function setting( $key, $default = null ) { $g = Settings::credentials( 'integrations', 'general' ); return array_key_exists( $key, $g ) ? $g[ $key ] : $default; } /** @return string Capability of every integrations screen and action. */ private static function cap() { return apply_filters( 'fwren/integrations/capability', 'fwren_manage_settings' ); } /* ------------------------------------------------------------------ * * Resend * ------------------------------------------------------------------ */ /** * Send an entry to its form's integration feeds again (conditions still apply). * * @param int $entry_id Entry id. * @param string $feed_id One feed (runs even when the feed is paused); empty = every enabled feed. * @return array|\WP_Error feed id => true|string|WP_Error|'skipped'. */ public static function resend( $entry_id, $feed_id = '' ) { $entry = Entries::get( (int) $entry_id ); if ( ! $entry ) { return new \WP_Error( 'fwren_no_entry', __( 'Entry not found.', 'fieldwren' ) ); } $values = (array) $entry['response']; $out = array(); foreach ( self::form_feeds( (int) $entry['form_id'] ) as $feed ) { if ( $feed_id ? (string) $feed['id'] !== (string) $feed_id : empty( $feed['enabled'] ) ) { continue; } if ( ! empty( $feed['conditions']['enabled'] ) && ! Conditions::passes( $feed['conditions'], $values ) ) { $out[ $feed['id'] ] = 'skipped'; continue; } $slug = sanitize_key( $feed['integration'] ); try { $res = apply_filters( 'fwren/feed/' . $slug, null, $feed, $entry, $values ); } catch ( \Throwable $e ) { $res = new \WP_Error( 'fwren_feed_exception', $e->getMessage() ); } if ( null === $res ) { $out[ $feed['id'] ] = 'skipped'; continue; } $ok = ! is_wp_error( $res ); /* translators: %s feed name */ Logs::add( (int) $entry['form_id'], (int) $entry['id'], $slug, $ok ? 'success' : 'failed', sprintf( __( '%s (resent)', 'fieldwren' ), $feed['name'] ?? $slug ), $ok ? ( is_string( $res ) ? $res : __( 'Sent.', 'fieldwren' ) ) : $res->get_error_message() ); if ( ! $ok ) { do_action( 'fwren/feed_failed', $feed, $entry, $res ); } $out[ $feed['id'] ] = $res; } return $out; } /** * Resend several entries. * * @param int[] $entry_ids Ids. * @return array [ sent, failed, skipped ]. */ public static function bulk_resend( array $entry_ids ) { $c = array( 'sent' => 0, 'failed' => 0, 'skipped' => 0 ); foreach ( array_filter( array_map( 'absint', $entry_ids ) ) as $id ) { $r = self::resend( $id ); foreach ( is_wp_error( $r ) ? array( $r ) : $r as $res ) { if ( is_wp_error( $res ) ) { $c['failed']++; } elseif ( 'skipped' === $res ) { $c['skipped']++; } else { $c['sent']++; } } } return $c; } /** @return array Feeds of a form that belong to this module. */ public static function form_feeds( $form_id ) { $all = self::all(); return array_values( array_filter( Form::feeds( (int) $form_id ), static function ( $f ) use ( $all ) { return is_array( $f ) && ! empty( $f['id'] ) && isset( $all[ $f['integration'] ?? '' ] ); } ) ); } /** Bulk action label (if the entries screen offers the filter). */ public static function bulk_actions( $actions ) { $actions = (array) $actions; $actions['fwren_resend_integrations'] = __( 'Resend to integrations', 'fieldwren' ); return $actions; } /** Bulk action handler (if the entries screen fires it). */ public static function handle_bulk_action( $action, $ids ) { if ( 'fwren_resend_integrations' === $action && current_user_can( self::cap() ) ) { self::bulk_resend( (array) $ids ); } } /** "Integrations" box on the entry screen with per-feed resend buttons. */ public static function entry_box( $entry ) { $entry = (array) $entry; if ( empty( $entry['id'] ) || ! current_user_can( self::cap() ) ) { return; } $feeds = self::form_feeds( (int) $entry['form_id'] ); if ( ! $feeds ) { return; } echo '

' . esc_html__( 'Integrations', 'fieldwren' ) . '

'; echo '
'; } /* ------------------------------------------------------------------ * * Failed-integration email * ------------------------------------------------------------------ */ /** * Email the admin when a feed fails (one email per feed per 10 minutes). * * @param array $feed Feed. * @param array $entry Entry. * @param \WP_Error $error Error. */ public static function feed_failed( $feed, $entry, $error ) { if ( ! self::setting( 'failed_email', false ) ) { return; } $feed = (array) $feed; $entry = (array) $entry; $int = self::get( $feed['integration'] ?? '' ); if ( ! $int ) { return; } $lock = 'fwren_int_fail_' . md5( ( $entry['form_id'] ?? 0 ) . '|' . ( $feed['id'] ?? '' ) ); if ( get_transient( $lock ) ) { return; } set_transient( $lock, 1, (int) apply_filters( 'fwren/integrations/failed_email_throttle', 10 * MINUTE_IN_SECONDS ) ); $to = \FWREN\Notifications::emails( (string) self::setting( 'failed_email_to', get_option( 'admin_email' ) ) ); if ( ! $to ) { $to = array( get_option( 'admin_email' ) ); } $form_id = (int) ( $entry['form_id'] ?? 0 ); $msg = is_wp_error( $error ) ? $error->get_error_message() : (string) $error; /* translators: 1: integration, 2: form */ $subject = sprintf( __( '[%1$s] Integration failed: %2$s', 'fieldwren' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $int->title() . ' – ' . get_the_title( $form_id ) ); $rows = array( __( 'Form', 'fieldwren' ) => get_the_title( $form_id ) . ' (#' . $form_id . ')', __( 'Feed', 'fieldwren' ) => ( $feed['name'] ?? '' ) . ' (' . $int->title() . ')', __( 'Entry', 'fieldwren' ) => ! empty( $entry['id'] ) ? '#' . (int) $entry['id'] : '—', __( 'Error', 'fieldwren' ) => $msg, __( 'Time', 'fieldwren' ) => wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), ); $body = '

' . esc_html__( 'An integration feed could not send a form entry.', 'fieldwren' ) . '

'; foreach ( $rows as $k => $v ) { $body .= ''; } $body .= '
' . esc_html( $k ) . '' . esc_html( $v ) . '
'; if ( ! empty( $entry['id'] ) ) { $body .= '

' . esc_html__( 'Open the entry', 'fieldwren' ) . ' · ' . esc_html__( 'Fix the cause, then use "Resend to integrations" on the entry.', 'fieldwren' ) . '

'; } $body .= '

' . esc_html__( 'Further failures of this feed within 10 minutes are not emailed. Turn these emails off in Settings → Integrations.', 'fieldwren' ) . '

'; $html = class_exists( '\FWREN\Notifications' ) ? \FWREN\Notifications::wrap( $body, $subject, $form_id ) : $body; wp_mail( $to, $subject, $html, array( 'Content-Type: text/html; charset=UTF-8' ) ); } /* ------------------------------------------------------------------ * * Assets * ------------------------------------------------------------------ */ /** Enqueue on the settings screens only. */ public static function enqueue() { $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( ! in_array( $page, array( 'fwren-form-settings', 'fwren-settings', 'fwren-entries' ), true ) ) { return; } $url = plugins_url( 'assets/', __FILE__ ); $ver = defined( 'FWREN_VERSION' ) ? FWREN_VERSION : '1.0.0'; wp_enqueue_style( 'fwren-integrations', $url . 'integrations.css', array(), $ver ); wp_enqueue_script( 'fwren-integrations', $url . 'integrations.js', array(), $ver, true ); wp_localize_script( 'fwren-integrations', 'FWRENIntL10n', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'i18n' => self::i18n(), ) ); } /** @return array UI strings. */ private static function i18n() { return array( 'addFeed' => __( 'Add integration feed', 'fieldwren' ), 'noFeeds' => __( 'No integration feeds yet. Add one to send entries of this form to another service.', 'fieldwren' ), 'name' => __( 'Feed name', 'fieldwren' ), 'integration' => __( 'Integration', 'fieldwren' ), 'status' => __( 'Status', 'fieldwren' ), 'actions' => __( 'Actions', 'fieldwren' ), 'edit' => __( 'Edit', 'fieldwren' ), 'duplicate' => __( 'Duplicate', 'fieldwren' ), 'delete' => __( 'Delete', 'fieldwren' ), 'confirmDel' => __( 'Delete this feed?', 'fieldwren' ), 'done' => __( 'Done', 'fieldwren' ), 'cancel' => __( 'Cancel', 'fieldwren' ), 'back' => __( '← All feeds', 'fieldwren' ), 'choose' => __( 'Choose an integration', 'fieldwren' ), 'notEnabled' => __( 'Turned off or not connected – set it up in Settings → Integrations.', 'fieldwren' ), 'openSettings' => __( 'Open settings', 'fieldwren' ), 'unsaved' => __( 'Feed changes are not saved yet – click Save.', 'fieldwren' ), 'none' => __( '— None —', 'fieldwren' ), 'custom' => __( 'Custom value / smartcodes…', 'fieldwren' ), 'smartcodes' => __( 'Insert smartcode', 'fieldwren' ), 'loading' => __( 'Loading…', 'fieldwren' ), 'refresh' => __( 'Refresh', 'fieldwren' ), 'pickFirst' => __( 'Choose the setting above first.', 'fieldwren' ), 'addRow' => __( 'Add row', 'fieldwren' ), 'addField' => __( 'Add field', 'fieldwren' ), 'remove' => __( 'Remove', 'fieldwren' ), 'key' => __( 'Key', 'fieldwren' ), 'value' => __( 'Value', 'fieldwren' ), 'remoteField' => __( 'Remote field', 'fieldwren' ), 'formField' => __( 'Form field / value', 'fieldwren' ), 'conditions' => __( 'Conditional logic', 'fieldwren' ), 'condEnable' => __( 'Run this feed only when conditions match', 'fieldwren' ), 'matchAll' => __( 'All rules match', 'fieldwren' ), 'matchAny' => __( 'Any rule matches', 'fieldwren' ), 'addRule' => __( 'Add rule', 'fieldwren' ), 'enabled' => __( 'Enabled', 'fieldwren' ), 'paused' => __( 'Paused', 'fieldwren' ), 'newFeed' => __( 'New feed', 'fieldwren' ), 'copy' => __( '(copy)', 'fieldwren' ), 'saving' => __( 'Saving…', 'fieldwren' ), 'saved' => __( 'Saved.', 'fieldwren' ), 'verifying' => __( 'Checking…', 'fieldwren' ), 'confirmDisc' => __( 'Remove the stored credentials of this integration?', 'fieldwren' ), 'error' => __( 'Something went wrong.', 'fieldwren' ), 'secretKeep' => __( 'Saved – leave as is to keep', 'fieldwren' ), 'required' => __( 'Required', 'fieldwren' ), 'copied' => __( 'Copied.', 'fieldwren' ), 'sending' => __( 'Sending…', 'fieldwren' ), 'sent' => __( 'Done – see the log.', 'fieldwren' ), 'search' => __( 'Search integrations…', 'fieldwren' ), 'all' => __( 'All', 'fieldwren' ), 'badges' => self::badge_labels(), ); } /** @return array Connection status labels. */ private static function badge_labels() { return array( 'ready' => __( 'No setup needed', 'fieldwren' ), 'verified' => __( 'Connected', 'fieldwren' ), 'saved' => __( 'Not verified', 'fieldwren' ), 'missing' => __( 'Not connected', 'fieldwren' ), 'unavailable' => __( 'Plugin not active', 'fieldwren' ), ); } /* ------------------------------------------------------------------ * * Global tab * ------------------------------------------------------------------ */ /** Add the Integrations tab to global settings. */ public static function global_tabs( $tabs ) { $tabs = (array) $tabs; $tabs['integrations'] = __( 'Integrations', 'fieldwren' ); return $tabs; } /** @return string Logo markup (inline SVG). */ public static function logo_html( Integration $int ) { $logo = $int->logo(); if ( 0 !== strpos( ltrim( (string) $logo ), '