newsletter
3 months ago
contact-forms.php
3 months ago
fluent-booking.php
3 months ago
iconvert-email-marketer.php
1 week ago
index.php
1 week ago
siteleads.php
3 months ago
iconvert-email-marketer.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Utils; |
| 4 | |
| 5 | add_action( |
| 6 | 'rest_api_init', |
| 7 | function () { |
| 8 | $namespace = 'kubio/v1'; |
| 9 | |
| 10 | register_rest_route( |
| 11 | $namespace, |
| 12 | '/prepare_iconvert_email_marketer_plugin', |
| 13 | array( |
| 14 | 'methods' => 'POST', |
| 15 | 'callback' => 'kubio_api_prepare_iconvert_email_marketer_plugin', |
| 16 | 'permission_callback' => function () { |
| 17 | return current_user_can( 'edit_posts' ); |
| 18 | }, |
| 19 | |
| 20 | ) |
| 21 | ); |
| 22 | } |
| 23 | ); |
| 24 | |
| 25 | |
| 26 | |
| 27 | function kubio_api_prepare_iconvert_email_marketer_plugin( WP_REST_Request $request ) { |
| 28 | |
| 29 | if ( ! Utils::getIsIconvertEmailMarketerActive() ) { |
| 30 | wp_send_json_error( __( 'Required plugin is missing', 'kubio' ), 400 ); |
| 31 | } |
| 32 | |
| 33 | $form_id = sanitize_text_field( $request->get_param( 'form_id' ) ); |
| 34 | $form_id = intval( $form_id ); |
| 35 | if ( empty( $form_id ) || is_nan( $form_id ) ) { |
| 36 | wp_send_json_error( __( 'Invalid form id', 'kubio' ), 400 ); |
| 37 | } |
| 38 | |
| 39 | //in case of failures only try init once |
| 40 | $already_setup = get_option( 'iconvertEmailMarketerThirdPartyInited', null ); |
| 41 | |
| 42 | if ( $already_setup ) { |
| 43 | // wp_send_json_success(); |
| 44 | } |
| 45 | update_option( 'iconvertEmailMarketerThirdPartyInited', true ); |
| 46 | try { |
| 47 | |
| 48 | kubio_iconvert_email_marketer_create_default_template( $form_id ); |
| 49 | wp_send_json_success(); |
| 50 | |
| 51 | } catch ( Exception $e ) { |
| 52 | wp_send_json_error( $e->getMessage(), 400 ); |
| 53 | |
| 54 | } |
| 55 | } |
| 56 | function kubio_iconvert_email_marketer_create_default_template( $form_id ) { |
| 57 | if ( empty( $form_id ) ) { |
| 58 | return; |
| 59 | } |
| 60 | $request = new WP_REST_Request( |
| 61 | 'POST', |
| 62 | '/iconvertem/v1/create-template' |
| 63 | ); |
| 64 | |
| 65 | // get contact form title |
| 66 | $cf_title = get_post_field( 'post_title', $form_id ); |
| 67 | $title = sprintf( __( '%s - Confirmation Email', 'kubio' ), $cf_title ); |
| 68 | |
| 69 | $request->set_body_params( |
| 70 | array( |
| 71 | 'title' => $title, |
| 72 | 'email_type' => 'icem-mail-tpl-taxonomy-type-transactional', |
| 73 | 'sending_event_form_id' => $form_id, |
| 74 | 'sending_event_type' => 'afterContactForm7Submission', |
| 75 | 'template_id' => '227', |
| 76 | 'email_subject' => __( 'Thanks for contacting us', 'kubio' ), |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | $response = rest_do_request( $request ); |
| 81 | |
| 82 | $data = $response->get_data(); |
| 83 | if(is_array($data) && isset($data['success'])) { |
| 84 | |
| 85 | $template_id = $data['data']['id'] ?? null; |
| 86 | if ( ! empty( $template_id ) ) { |
| 87 | $originally_disabled = get_post_meta( $template_id, '_iconvertem_email_disabled', true ); |
| 88 | update_post_meta( $template_id, '_iconvertem_email_disabled', '1', $originally_disabled ); |
| 89 | } |
| 90 | |
| 91 | return $data['success']; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 |