Advertisement
verygoodplugins

Untitled

Feb 16th, 2021
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7.  
  8. class WPF_Contact_Form_7 extends WPF_Integrations_Base {
  9.  
  10.     /**
  11.      * Gets things started
  12.      *
  13.      * @access  public
  14.      * @return  void
  15.      */
  16.  
  17.     public function init() {
  18.  
  19.         $this->slug = 'cf7';
  20.  
  21.         add_filter( 'wpcf7_editor_panels', array( $this, 'add_panel' ) );
  22.         add_action( 'wpcf7_after_save', array( $this, 'save_form' ) );
  23.         add_action( 'wpcf7_mail_sent', array( $this, 'send_data' ), 5 );
  24.  
  25.     }
  26.  
  27.     /**
  28.      * Adds panel to CF7 settings page
  29.      *
  30.      * @access  public
  31.      * @return  array Panels
  32.      */
  33.  
  34.     public function add_panel( $panels ) {
  35.  
  36.         $panels['wp-fusion-tab'] = array(
  37.             'title'    => 'WP Fusion',
  38.             'callback' => array( $this, 'add_form' )
  39.         );
  40.  
  41.         return $panels;
  42.  
  43.     }
  44.  
  45.  
  46.     /**
  47.      * Adds form content to panel
  48.      *
  49.      * @access  public
  50.      * @return  mixed Panel content
  51.      */
  52.  
  53.     public function add_form( $info ) {
  54.  
  55.         wp_nonce_field( 'cf7_wpf_nonce', 'cf7_wpf_nonce' );
  56.  
  57.         $post_id = $info->id();
  58.         $content = $info->prop( 'form' );
  59.         $inputs  = array();
  60.  
  61.         // Get inputs from saved form config
  62.         preg_match_all( "/\[.*\]/", $content, $matches );
  63.  
  64.         foreach ( $matches[0] as $input ) {
  65.             $input    = substr( $input, 1, - 1 );
  66.             $input    = str_replace( '*', '', $input );
  67.             $elements = explode( ' ', $input );
  68.  
  69.             if ( ! ( $elements[1] == '"Send"' ) ) {
  70.                 $inputs[ $elements[1] ] = $elements[0];
  71.             }
  72.         }
  73.  
  74.         $settings = get_post_meta( $post_id, 'cf7_wpf_settings', true );
  75.  
  76.         if( empty( $settings ) ) {
  77.             $settings = array();
  78.         }
  79.  
  80.         echo '<h2>' . wp_fusion()->crm->name . ' Settings</h2>';
  81.  
  82.         echo '<fieldset><legend>For each field in the form, select a field to sync with in ' . wp_fusion()->crm->name . '</legend>';
  83.  
  84.         echo '<table id="wpf-cf7-table">';
  85.         echo '<tbody id="wp-fusion-inputs">';
  86.         foreach ( $inputs as $name => $type ) {
  87.  
  88.             $capital_name = str_replace( '-', '  ', $name );
  89.  
  90.             if( ! isset( $settings[ $name ] ) ) {
  91.                 $settings[ $name ] = array( 'crm_field' => '' );
  92.             }
  93.  
  94.             if ( ! isset( $settings[ $name ]['crm_field'] ) ) {
  95.                 $settings[ $name ]['crm_field'] = '';
  96.             }
  97.  
  98.             echo '<tr id="input-row">';
  99.             echo '<td><label> ' . ucwords( $capital_name ) . ' <label></td>';
  100.             echo '<td><label for ="cf7_wpf_settings"> ' . $type . ' <label></td>';
  101.             echo '<td class="crm-field">';
  102.             wpf_render_crm_field_select( $settings[ $name ]['crm_field'], 'cf7_wpf_settings', $name );
  103.             echo '</td>';
  104.             echo '</tr>';
  105.         }
  106.         echo '</tbody>';
  107.         echo '</table>';
  108.  
  109.         $multiselect = $name;
  110.         if ( isset( $settings['tags'] ) ) {
  111.             $multiselect = $settings['tags'];
  112.         }
  113.  
  114.  
  115.         echo '<p class="description"><label for="tags">Apply the following tags when the form is submitted:</label><br />';
  116.  
  117.         wpf_render_tag_multiselect( array( 'setting' => $multiselect, 'meta_name' => 'cf7_wpf_settings', 'field_id' => 'tags' ) );
  118.  
  119.         echo '</p>';
  120.  
  121.         echo '</fieldset>';
  122.  
  123.     }
  124.  
  125.     /**
  126.      * Save WPF settings fields
  127.      * @access public
  128.      *
  129.      * @param unknown $contact_form
  130.      */
  131.  
  132.     public function save_form( $contact_form ) {
  133.  
  134.         if ( empty( $_POST ) || ! isset( $_POST['cf7_wpf_nonce'] ) ) {
  135.             return;
  136.         }
  137.  
  138.         $post_id = $contact_form->id();
  139.  
  140.         update_post_meta( $post_id, 'cf7_wpf_settings', $_POST['cf7_wpf_settings'] );
  141.  
  142.     }
  143.  
  144.     /**
  145.      * Send data to CRM on form submission
  146.      *
  147.      * @access  public
  148.      * @return  array Classes
  149.      */
  150.  
  151.     public function send_data( $contact_form ) {
  152.  
  153.         $contact_form_id = $contact_form->id();
  154.         $submission      = WPCF7_Submission::get_instance();
  155.         $posted_data     = $submission->get_posted_data();
  156.  
  157.         $wpf_settings = get_post_meta( $contact_form_id, 'cf7_wpf_settings', true );
  158.  
  159.         if ( empty( $wpf_settings ) ) {
  160.             return;
  161.         }
  162.  
  163.         $email_address = false;
  164.         $update_data = array();
  165.  
  166.         foreach ( $posted_data as $key => $value ) {
  167.  
  168.             if ( ! isset( $wpf_settings[ $key ] ) || empty( $wpf_settings[ $key ]['crm_field'] ) || empty( $value ) ) {
  169.                 continue;
  170.             }
  171.  
  172.             $type = 'text';
  173.  
  174.             if ( is_array( $value ) ) {
  175.                 $value = implode( ', ', $value );
  176.                 $type = 'multiselect';
  177.             }
  178.  
  179.             if ( is_email( $value ) ) {
  180.                 $email_address = $value;
  181.             }
  182.  
  183.             $value = apply_filters( 'wpf_format_field_value', $value, $type, $wpf_settings[ $key ]['crm_field'] );
  184.  
  185.             $update_data[ $wpf_settings[ $key ]['crm_field'] ] = $value;
  186.  
  187.         }
  188.  
  189.         if ( ! isset( $wpf_settings['tags'] ) ) {
  190.             $wpf_settings['tags'] = array();
  191.         }
  192.  
  193.         $args = array(
  194.             'email_address'     => $email_address,
  195.             'update_data'       => $update_data,
  196.             'apply_tags'        => $wpf_settings['tags'],
  197.             'integration_slug'  => 'cf7',
  198.             'integration_name'  => 'Contact Form 7',
  199.             'form_id'           => $contact_form_id,
  200.             'form_title'        => get_the_title( $contact_form_id ),
  201.             'form_edit_link'    => admin_url( 'admin.php?page=wpcf7&post=' . $contact_form_id . '&action=edit' )
  202.         );
  203.  
  204.         require_once WPF_DIR_PATH . 'includes/integrations/class-forms-helper.php';
  205.  
  206.         $contact_id = WPF_Forms_Helper::process_form_data( $args );
  207.  
  208.     }
  209.  
  210. }
  211.  
  212. new WPF_Contact_Form_7;
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement