| |
| <?php
|
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
| define( 'AFFWP_GF_PRACTITIONER_FORM_ID', 5 );
|
|
|
|
|
|
|
|
|
| define( 'AFFWP_GF_PRACTITIONER_FIELD_ID', 12 );
|
|
|
|
|
|
|
|
|
| define( 'AFFWP_GF_PRACTITIONER_MAP', serialize( array(
|
| 'Jane Smith' => 42,
|
| 'Dr. Johnson' => 87,
|
| 'Mike Torres' => 103,
|
| ) ) );
|
|
|
|
|
|
|
|
|
|
|
| add_action( 'gform_after_submission', 'affwp_gf_create_referral_from_dropdown', 20, 2 );
|
|
|
| function affwp_gf_create_referral_from_dropdown( $entry, $form ) {
|
|
|
| if ( absint( $form['id'] ) !== AFFWP_GF_PRACTITIONER_FORM_ID ) {
|
| return;
|
| }
|
|
|
| if ( ! function_exists( 'affwp_add_referral' ) ) {
|
| return;
|
| }
|
|
|
| $field_id = AFFWP_GF_PRACTITIONER_FIELD_ID;
|
| $selected_label = isset( $entry[ $field_id ] ) ? sanitize_text_field( $entry[ $field_id ] ) : '';
|
|
|
| if ( empty( $selected_label ) ) {
|
| return;
|
| }
|
|
|
| $affiliate_map = unserialize( AFFWP_GF_PRACTITIONER_MAP );
|
|
|
| if ( ! isset( $affiliate_map[ $selected_label ] ) ) {
|
| return;
|
| }
|
|
|
| $affiliate_id = absint( $affiliate_map[ $selected_label ] );
|
|
|
| if ( empty( $affiliate_id ) ) {
|
| return;
|
| }
|
|
|
| if ( 'active' !== affwp_get_affiliate_status( $affiliate_id ) ) {
|
| return;
|
| }
|
|
|
| $entry_id = absint( $entry['id'] );
|
|
|
| $existing_native = affwp_get_referral_by( 'reference', $entry_id, 'gravityforms' );
|
| $existing_manual = affwp_get_referral_by( 'reference', $entry_id, 'gravityforms_manual' );
|
|
|
| if ( ! is_wp_error( $existing_native ) || ! is_wp_error( $existing_manual ) ) {
|
| return;
|
| }
|
|
|
| $description = isset( $form['title'] ) ? sanitize_text_field( $form['title'] ) : '';
|
|
|
| $referral_id = affwp_add_referral( array(
|
| 'affiliate_id' => $affiliate_id,
|
| 'reference' => $entry_id,
|
| 'description' => $description,
|
| 'context' => 'gravityforms_manual',
|
| 'status' => 'pending',
|
| ) );
|
|
|
| if ( empty( $referral_id ) ) {
|
| error_log( sprintf(
|
| 'AffiliateWP: Failed to create referral for entry #%d, affiliate #%d.',
|
| $entry_id,
|
| $affiliate_id
|
| ) );
|
| }
|
| }
|
|
|
| |
| |
Comments