class-avcf-abilities-cf7-pro.php
4 weeks ago
class-avcf-abilities-cf7.php
4 weeks ago
class-avcf-cf7-detector.php
1 month ago
class-avcf-cf7-helpers.php
1 month ago
class-avcf-cf7-helpers.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Contact Form 7 — shared helpers for the standalone CF7 ability cluster. |
| 4 | * |
| 5 | * CF7 config is read/written through the WPCF7_ContactForm model (prop / |
| 6 | * set_properties / save); fields are parsed from the [form] template markup; |
| 7 | * notifications are the "mail" and "mail_2" templates. CF7 stores no |
| 8 | * submissions — the entry side lives in the separate Flamingo cluster. Built on |
| 9 | * CF7's model; not runtime-tested here. |
| 10 | * |
| 11 | * @package atarim-visual-collaboration |
| 12 | */ |
| 13 | |
| 14 | if ( ! defined('ABSPATH') ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | class AVCF_CF7_Helpers { |
| 19 | |
| 20 | const PLUGIN_SLUG = 'cf7'; |
| 21 | const PLUGIN_LABEL = 'Contact Form 7'; |
| 22 | |
| 23 | public static function get_cf7( $form_id ) { |
| 24 | return function_exists( 'wpcf7_contact_form' ) ? wpcf7_contact_form( (int) $form_id ) : null; |
| 25 | } |
| 26 | |
| 27 | public static function parse_fields( $template ) { |
| 28 | $fields = []; |
| 29 | if ( (string) $template === '' ) { return $fields; } |
| 30 | if ( preg_match_all( '/\[([a-z][a-z0-9_]*\*?)\s+([a-zA-Z0-9_\-]+)/', (string) $template, $matches, PREG_SET_ORDER ) ) { |
| 31 | foreach ( $matches as $m ) { |
| 32 | $type = (string) $m[1]; |
| 33 | $required = ( substr( $type, -1 ) === '*' ); |
| 34 | if ( $required ) { $type = substr( $type, 0, -1 ); } |
| 35 | if ( in_array( $type, [ 'submit', 'response' ], true ) ) { continue; } |
| 36 | $fields[] = [ 'id' => (string) $m[2], 'type' => $type, 'label' => (string) $m[2], 'required' => $required, 'options' => [] ]; |
| 37 | } |
| 38 | } |
| 39 | return $fields; |
| 40 | } |
| 41 | |
| 42 | public static function map_mail( $mail, $key ) { |
| 43 | if ( ! is_array( $mail ) ) { return null; } |
| 44 | return [ |
| 45 | 'id' => $key, |
| 46 | 'name' => $key === 'mail' ? 'Mail' : 'Mail (2)', |
| 47 | 'recipient' => isset( $mail['recipient'] ) ? (string) $mail['recipient'] : '', |
| 48 | 'recipients' => self::parse_recipients( isset( $mail['recipient'] ) ? (string) $mail['recipient'] : '' ), |
| 49 | 'sender' => isset( $mail['sender'] ) ? (string) $mail['sender'] : '', |
| 50 | 'subject' => isset( $mail['subject'] ) ? (string) $mail['subject'] : '', |
| 51 | 'body' => isset( $mail['body'] ) ? (string) $mail['body'] : '', |
| 52 | 'additional_headers' => isset( $mail['additional_headers'] ) ? (string) $mail['additional_headers'] : '', |
| 53 | 'use_html' => ! empty( $mail['use_html'] ), |
| 54 | 'active' => $key === 'mail' ? true : ! empty( $mail['active'] ), |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | public static function map_form_summary( $post ) { |
| 59 | $form_id = (int) $post->ID; |
| 60 | return [ |
| 61 | 'id' => $form_id, |
| 62 | 'plugin' => self::PLUGIN_SLUG, |
| 63 | 'title' => (string) $post->post_title, |
| 64 | 'status' => $post->post_status === 'publish' ? 'active' : (string) $post->post_status, |
| 65 | 'entries_total' => null, |
| 66 | 'last_submission' => null, |
| 67 | 'created_at' => (string) $post->post_date, |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | public static function map_form_full( $post, $cf7 ) { |
| 72 | $form_id = (int) $post->ID; |
| 73 | $fields = []; |
| 74 | $notifications = []; |
| 75 | if ( $cf7 && method_exists( $cf7, 'prop' ) ) { |
| 76 | $fields = self::parse_fields( (string) $cf7->prop( 'form' ) ); |
| 77 | foreach ( [ 'mail', 'mail_2' ] as $key ) { |
| 78 | $m = self::map_mail( $cf7->prop( $key ), $key ); |
| 79 | if ( $m && $m['recipient'] !== '' ) { |
| 80 | $notifications[] = [ 'id' => $key, 'name' => $m['name'], 'recipients' => $m['recipients'], 'subject' => $m['subject'], 'enabled' => $m['active'] ]; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return [ |
| 85 | 'id' => $form_id, |
| 86 | 'plugin' => self::PLUGIN_SLUG, |
| 87 | 'title' => (string) $post->post_title, |
| 88 | 'status' => $post->post_status === 'publish' ? 'active' : (string) $post->post_status, |
| 89 | 'field_count' => count( $fields ), |
| 90 | 'fields' => $fields, |
| 91 | 'notifications' => $notifications, |
| 92 | 'created_at' => (string) $post->post_date, |
| 93 | 'modified_at' => (string) $post->post_modified, |
| 94 | 'shortcode' => sprintf( '[contact-form-7 id="%d" title="%s"]', $form_id, esc_attr( $post->post_title ) ), |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | public static function parse_recipients( $str ) { |
| 99 | if ( (string) $str === '' ) { return []; } |
| 100 | $out = []; |
| 101 | foreach ( (array) preg_split( '/[,\n;]+/', (string) $str ) as $part ) { |
| 102 | $clean = trim( (string) $part ); |
| 103 | if ( $clean !== '' ) { $out[] = $clean; } |
| 104 | } |
| 105 | return $out; |
| 106 | } |
| 107 | } |
| 108 |