| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class Notifier_SureForms { |
| 7 |
|
| 8 |
/** |
| 9 |
* Init |
| 10 |
*/ |
| 11 |
public static function init() { |
| 12 |
add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_triggers' ), 10 ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Add notification triggers |
| 17 |
*/ |
| 18 |
public static function add_triggers( $existing_triggers ) { |
| 19 |
|
| 20 |
if ( ! class_exists( 'SRFM\Plugin_Loader' ) || ! post_type_exists( 'sureforms_form' ) ) { |
| 21 |
return $existing_triggers; |
| 22 |
} |
| 23 |
|
| 24 |
$triggers = array(); |
| 25 |
|
| 26 |
$form_ids = get_posts( array( |
| 27 |
'post_type' => 'sureforms_form', |
| 28 |
'post_status' => 'publish', |
| 29 |
'numberposts' => -1, |
| 30 |
'fields' => 'ids', |
| 31 |
) ); |
| 32 |
|
| 33 |
foreach ( $form_ids as $form_id ) { |
| 34 |
|
| 35 |
$trigger_id = 'sureforms_' . $form_id; |
| 36 |
$title = get_the_title( $form_id ); |
| 37 |
|
| 38 |
$triggers[] = array( |
| 39 |
'id' => $trigger_id, |
| 40 |
'label' => 'Form "' . $title . '" is submitted', |
| 41 |
'description' => 'Trigger notification when <b>' . $title . '</b> form is submitted.', |
| 42 |
'merge_tags' => self::get_merge_tags( $form_id ), |
| 43 |
'recipient_fields' => self::get_recipient_fields( $form_id ), |
| 44 |
'action' => array( |
| 45 |
'hook' => 'srfm_form_submit', |
| 46 |
'args_num' => 1, |
| 47 |
'callback' => function ( $response ) use ( $trigger_id, $form_id ) { |
| 48 |
|
| 49 |
if ( empty( $response['success'] ) || (int) $response['form_id'] !== (int) $form_id ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$payload = isset( $response['data'] ) ? $response['data'] : array(); |
| 54 |
|
| 55 |
Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $payload ); |
| 56 |
}, |
| 57 |
), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$existing_triggers['SureForms'] = $triggers; |
| 62 |
|
| 63 |
return $existing_triggers; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get merge tags |
| 68 |
*/ |
| 69 |
public static function get_merge_tags( $form_id ) { |
| 70 |
|
| 71 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags(); |
| 72 |
$post = get_post( $form_id ); |
| 73 |
|
| 74 |
if ( ! $post ) { |
| 75 |
return $merge_tags; |
| 76 |
} |
| 77 |
|
| 78 |
$blocks = parse_blocks( $post->post_content ); |
| 79 |
|
| 80 |
foreach ( $blocks as $block ) { |
| 81 |
|
| 82 |
if ( empty( $block['blockName'] ) || strpos( $block['blockName'], 'srfm/' ) !== 0 ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
if ( empty( $block['attrs']['slug'] ) ) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
$slug = $block['attrs']['slug']; |
| 91 |
$block_id = ! empty( $block['attrs']['block_id'] ) ? $block['attrs']['block_id'] : $slug; |
| 92 |
$label = ! empty( $block['attrs']['label'] ) ? $block['attrs']['label'] : $slug; |
| 93 |
|
| 94 |
$merge_tags['SureForms'][] = array( |
| 95 |
'id' => 'sureforms_' . $form_id . '_' . sanitize_key( $block_id . '_' . $slug ), |
| 96 |
'label' => $label, |
| 97 |
'preview_value' => '', |
| 98 |
'return_type' => 'text', |
| 99 |
'value' => function ( $payload ) use ( $slug ) { |
| 100 |
$value = isset( $payload[ $slug ] ) ? $payload[ $slug ] : ''; |
| 101 |
if ( is_array( $value ) ) { |
| 102 |
$value = implode( ', ', $value ); |
| 103 |
} |
| 104 |
return sanitize_text_field( $value ); |
| 105 |
}, |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
return $merge_tags; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get recipient fields |
| 114 |
*/ |
| 115 |
public static function get_recipient_fields( $form_id ) { |
| 116 |
|
| 117 |
$recipient_fields = array(); |
| 118 |
$post = get_post( $form_id ); |
| 119 |
|
| 120 |
if ( ! $post ) { |
| 121 |
return $recipient_fields; |
| 122 |
} |
| 123 |
|
| 124 |
$blocks = parse_blocks( $post->post_content ); |
| 125 |
|
| 126 |
foreach ( $blocks as $block ) { |
| 127 |
|
| 128 |
if ( empty( $block['blockName'] ) || $block['blockName'] !== 'srfm/phone' ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
if ( empty( $block['attrs']['slug'] ) ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
$slug = $block['attrs']['slug']; |
| 137 |
$block_id = ! empty( $block['attrs']['block_id'] ) ? $block['attrs']['block_id'] : $slug; |
| 138 |
$label = ! empty( $block['attrs']['label'] ) ? $block['attrs']['label'] : $slug; |
| 139 |
|
| 140 |
$recipient_fields['SureForms'][] = array( |
| 141 |
'id' => 'sureforms_' . $form_id . '_' . sanitize_key( $block_id . '_' . $slug ), |
| 142 |
'label' => $label, |
| 143 |
'value' => function ( $payload ) use ( $slug ) { |
| 144 |
$value = isset( $payload[ $slug ] ) ? $payload[ $slug ] : ''; |
| 145 |
return sanitize_text_field( $value ); |
| 146 |
}, |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return $recipient_fields; |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|