Advertisement
verygoodplugins

Untitled

Jan 30th, 2020
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.23 KB | None | 0 0
  1. <?php
  2.  
  3. use Elementor\Controls_Manager;
  4. use Elementor\Settings;
  5. use ElementorPro\Modules\Forms\Classes\Form_Record;
  6. use ElementorPro\Modules\Forms\Controls\Fields_Map;
  7. use ElementorPro\Modules\Forms\Classes\Integration_Base;
  8. use ElementorPro\Classes\Utils;
  9.  
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit; // Exit if accessed directly.
  12. }
  13.  
  14. function wpf_add_form_actions() {
  15.     \ElementorPro\Plugin::instance()->modules_manager->get_modules( 'forms' )->add_form_action( 'wpfusion', new WPF_Elementor_Forms() );
  16. }
  17.  
  18. add_action( 'elementor_pro/init', 'wpf_add_form_actions' );
  19.  
  20. class WPF_Elementor_Forms extends ElementorPro\Modules\Forms\Classes\Integration_Base {
  21.  
  22.     /**
  23.      * @var string
  24.      */
  25.     public $slug = 'elementor-forms';
  26.  
  27.     /**
  28.      * Gets things started
  29.      *
  30.      * @access  public
  31.      * @since   1.0
  32.      * @return  void
  33.      */
  34.  
  35.     public function __construct() {
  36.  
  37.         wp_fusion()->integrations->{'elementor-forms'} = $this;
  38.  
  39.         add_filter( 'get_post_metadata', array( $this, 'update_saved_forms' ), 10, 4 );
  40.  
  41.     }
  42.  
  43.     /**
  44.      * Get action ID
  45.      *
  46.      * @access  public
  47.      * @return  string ID
  48.      */
  49.  
  50.     public function get_name() {
  51.         return 'wpfusion';
  52.     }
  53.  
  54.     /**
  55.      * Get action label
  56.      *
  57.      * @access  public
  58.      * @return  string Label
  59.      */
  60.  
  61.     public function get_label() {
  62.         return 'WP Fusion';
  63.     }
  64.  
  65.     /**
  66.      * Get CRM fields
  67.      *
  68.      * @access  public
  69.      * @return  array fields
  70.      */
  71.  
  72.     public function get_fields() {
  73.  
  74.         $fields = array();
  75.  
  76.         $fields_merged    = array();
  77.         $available_fields = wp_fusion()->settings->get( 'crm_fields', array() );
  78.  
  79.         if ( isset( $available_fields['Standard Fields'] ) ) {
  80.  
  81.             $available_fields = array_merge( $available_fields['Standard Fields'], $available_fields['Custom Fields'] );
  82.  
  83.         }
  84.  
  85.         foreach ( $available_fields as $field_id => $field_label ) {
  86.  
  87.             $remote_required = false;
  88.  
  89.             if ( $field_label == 'Email' ) {
  90.                 $remote_required = true;
  91.             }
  92.  
  93.             $fields[] = array(
  94.                 'remote_label'    => $field_label,
  95.                 'remote_type'     => 'text',
  96.                 'remote_id'       => $field_id,
  97.                 'remote_required' => $remote_required,
  98.             );
  99.  
  100.         }
  101.  
  102.         // Add as tag
  103.         if ( is_array( wp_fusion()->crm->supports ) && in_array( 'add_tags', wp_fusion()->crm->supports ) ) {
  104.  
  105.             $fields[] = array(
  106.                 'remote_label'    => '+ Create tag(s) from',
  107.                 'remote_type'     => 'text',
  108.                 'remote_id'       => 'add_tag_e',
  109.                 'remote_required' => false,
  110.             );
  111.  
  112.         }
  113.  
  114.         return $fields;
  115.  
  116.     }
  117.  
  118.     /**
  119.      * Get available tags for select
  120.      *
  121.      * @access  public
  122.      * @return  array Tags
  123.      */
  124.  
  125.     public function get_tags() {
  126.  
  127.         $available_tags = wp_fusion()->settings->get( 'available_tags', array() );
  128.  
  129.         $data = array();
  130.  
  131.         foreach ( $available_tags as $id => $label ) {
  132.  
  133.             if ( is_array( $label ) ) {
  134.                 $label = $label['label'];
  135.             }
  136.  
  137.             $data[ $id ] = $label;
  138.  
  139.         }
  140.  
  141.         return $data;
  142.  
  143.     }
  144.  
  145.     /**
  146.      * Registers settings
  147.      *
  148.      * @access  public
  149.      * @return  void
  150.      */
  151.  
  152.     public function register_settings_section( $widget ) {
  153.  
  154.         $widget->start_controls_section(
  155.             'section_wpfusion',
  156.             [
  157.                 'label'     => 'WP Fusion',
  158.                 'condition' => [
  159.                     'submit_actions' => $this->get_name(),
  160.                 ],
  161.             ]
  162.         );
  163.  
  164.         $widget->add_control(
  165.             'wpf_apply_tags',
  166.             [
  167.                 'label'       => __( 'Apply Tags', 'wp-fusion' ),
  168.                 'description' => sprintf( __( 'The selected tags will be applied in %s when the form is submitted.', 'wp-fusion' ), wp_fusion()->crm->name ),
  169.                 'type'        => Controls_Manager::SELECT2,
  170.                 'options'     => $this->get_tags(),
  171.                 'multiple'    => true,
  172.                 'label_block' => true,
  173.                 'show_label'  => true,
  174.             ]
  175.         );
  176.  
  177.         $widget->add_control(
  178.             'wpf_add_only',
  179.             [
  180.                 'label'       => __( 'Add Only', 'wp-fusion' ),
  181.                 'description' => __( 'Only add new contacts, don\'t update existing ones.', 'wp-fusion' ),
  182.                 'type'        => Controls_Manager::SWITCHER,
  183.                 'label_block' => false,
  184.                 'show_label'  => true,
  185.             ]
  186.         );
  187.  
  188.         $widget->add_control(
  189.             'wpf_fields_map',
  190.             [
  191.                 'label'     => sprintf( __( '%s Field Mapping', 'elementor-pro' ), wp_fusion()->crm->name ),
  192.                 'type'      => Fields_Map::CONTROL_TYPE,
  193.                 'separator' => 'before',
  194.                 'fields'    => [
  195.                     [
  196.                         'name' => 'remote_id',
  197.                         'type' => Controls_Manager::HIDDEN,
  198.                     ],
  199.                     [
  200.                         'name' => 'local_id',
  201.                         'type' => Controls_Manager::SELECT,
  202.                     ],
  203.                 ],
  204.                 'default'   => $this->get_fields(),
  205.             ]
  206.         );
  207.  
  208.         $widget->end_controls_section();
  209.  
  210.     }
  211.  
  212.     /**
  213.      * Update saved form data when it's loaded from the DB to detect new form fields (because Elementor support has been useless at helping with this, see https://github.com/elementor/elementor/issues/8938)
  214.      *
  215.      * @access  public
  216.      * @return  null / array Value
  217.      */
  218.  
  219.     public function update_saved_forms( $value, $object_id, $meta_key, $single ) {
  220.  
  221.         if ( is_admin() && $meta_key == '_elementor_data' ) {
  222.  
  223.             // Prevent looping
  224.             remove_filter( 'get_post_metadata', array( $this, 'update_saved_forms' ), 10, 4 );
  225.  
  226.             $settings = get_post_meta( $object_id, '_elementor_data', true );
  227.  
  228.             // Quit if the desired setting isn't found or if settings are already an array (no idea why it does that)
  229.             if ( is_array( $settings ) || false === strpos( $settings, 'wpf_fields_map' ) ) {
  230.                 return $value;
  231.             }
  232.  
  233.             $settings = json_decode( $settings, true );
  234.  
  235.             $settings = $this->parse_elements_for_form( $settings );
  236.  
  237.             $value = json_encode( $settings );
  238.  
  239.         }
  240.  
  241.         return $value;
  242.  
  243.     }
  244.  
  245.     /**
  246.      * Loop through saved elements, updating values as necessary
  247.      *
  248.      * @access  public
  249.      * @return  array Elements
  250.      */
  251.  
  252.     private function parse_elements_for_form( $elements ) {
  253.  
  254.         foreach ( $elements as $i => $element ) {
  255.  
  256.             if ( isset( $element['settings'] ) && isset( $element['settings']['wpf_fields_map'] ) ) {
  257.  
  258.                 $new_settings = $this->get_fields();
  259.  
  260.                 foreach ( $new_settings as $n => $setting ) {
  261.  
  262.                     foreach ( $element['settings']['wpf_fields_map'] as $saved_value ) {
  263.  
  264.                         if ( $saved_value['remote_id'] == $setting['remote_id'] ) {
  265.  
  266.                             $new_settings[ $n ] = array_merge( $setting, $saved_value );
  267.  
  268.                         }
  269.                     }
  270.                 }
  271.  
  272.                 $elements[ $i ]['settings']['wpf_fields_map'] = $new_settings;
  273.  
  274.             }
  275.  
  276.             if ( ! empty( $element['elements'] ) ) {
  277.  
  278.                 $elements[ $i ]['elements'] = $this->parse_elements_for_form( $element['elements'] );
  279.  
  280.             }
  281.         }
  282.  
  283.         return $elements;
  284.  
  285.     }
  286.  
  287.     /**
  288.      * Unsets WPF settings on export
  289.      *
  290.      * @access  public
  291.      * @return  object Element
  292.      */
  293.  
  294.     public function on_export( $element ) {
  295.  
  296.         unset(
  297.             $element['settings']['wpf_fields_map'],
  298.             $element['settings']['wpf_apply_tags']
  299.         );
  300.  
  301.         return $element;
  302.     }
  303.  
  304.     /**
  305.      * Process form submission
  306.      *
  307.      * @access  public
  308.      * @return  void
  309.      */
  310.  
  311.     public function run( $record, $ajax_handler ) {
  312.  
  313.         $sent_data     = $record->get( 'sent_data' );
  314.         $form_settings = $record->get( 'form_settings' );
  315.  
  316.         $update_data   = array();
  317.         $email_address = false;
  318.  
  319.         foreach ( $form_settings['wpf_fields_map'] as $field ) {
  320.  
  321.             if ( ! empty( $sent_data[ $field['local_id'] ] ) ) {
  322.  
  323.                 $update_data[ $field['remote_id'] ] = apply_filters( 'wpf_format_field_value', $sent_data[ $field['local_id'] ], 'text', $field['remote_id'] );
  324.  
  325.                 if ( $email_address == false && is_email( $sent_data[ $field['local_id'] ] ) ) {
  326.                     $email_address = $sent_data[ $field['local_id'] ];
  327.                 }
  328.             }
  329.         }
  330.  
  331.         if ( isset( $form_settings['wpf_add_only'] ) && $form_settings['wpf_add_only'] == 'yes' ) {
  332.             $add_only = true;
  333.         } else {
  334.             $add_only = false;
  335.         }
  336.  
  337.         if ( empty ( $form_settings['wpf_apply_tags'] ) ) {
  338.             $form_settings['wpf_apply_tags'] = array();
  339.         }
  340.  
  341.         $args = array(
  342.             'email_address'    => $email_address,
  343.             'update_data'      => $update_data,
  344.             'apply_tags'       => $form_settings['wpf_apply_tags'],
  345.             'add_only'         => $add_only,
  346.             'integration_slug' => 'elementor_forms',
  347.             'integration_name' => 'Elementor Forms',
  348.             'form_id'          => null,
  349.             'form_title'       => null,
  350.             'form_edit_link'   => null,
  351.         );
  352.  
  353.         require_once WPF_DIR_PATH . 'includes/integrations/class-forms-helper.php';
  354.  
  355.         $contact_id = WPF_Forms_Helper::process_form_data( $args );
  356.  
  357.     }
  358.  
  359.     /**
  360.      * @param array $data
  361.      *
  362.      * @return void
  363.      */
  364.  
  365.     public function handle_panel_request( array $data ) { }
  366.  
  367.  
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement