__( 'PropertyHive', 'advanced-form-integration' ), 'tasks' => array( 'add_property' => __( 'Create Property', 'advanced-form-integration' ), ), ); return $actions; } add_action( 'adfoin_action_fields', 'adfoin_propertyhive_action_fields' ); function adfoin_propertyhive_action_fields() { ?> 'title', 'value' => __( 'Property Title', 'advanced-form-integration' ), 'description' => __( 'Required.', 'advanced-form-integration' ) ), array( 'key' => 'description', 'value' => __( 'Description', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'price', 'value' => __( 'Price / Rent', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'department', 'value' => __( 'Department', 'advanced-form-integration' ), 'description' => __( 'e.g. residential-sale, residential-let, commercial-sale, commercial-let.', 'advanced-form-integration' ) ), array( 'key' => 'address_street', 'value' => __( 'Street Address', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'address_postcode', 'value' => __( 'Postcode', 'advanced-form-integration' ), 'description' => '' ), ); wp_send_json_success( $fields ); } add_action( 'adfoin_propertyhive_job_queue', 'adfoin_propertyhive_job_queue', 10, 1 ); function adfoin_propertyhive_job_queue( $data ) { adfoin_propertyhive_send_data( $data['record'], $data['posted_data'] ); } /* * Handles creating a PropertyHive property listing */ function adfoin_propertyhive_send_data( $record, $posted_data ) { if ( ! post_type_exists( 'property' ) ) { return; } $record_data = json_decode( $record['data'], true ); if ( isset( $record_data['action_data']['cl'] ) && adfoin_check_conditional_logic( $record_data['action_data']['cl'], $posted_data ) ) { return; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $task = isset( $record['task'] ) ? $record['task'] : ''; if ( 'add_property' !== $task ) { return; } $prepared_data = array(); foreach ( $field_data as $key => $value ) { if ( '' === $key ) { continue; } $parsed_value = adfoin_get_parsed_values( $value, $posted_data ); if ( '' === $parsed_value || null === $parsed_value ) { continue; } $prepared_data[ $key ] = $parsed_value; } $request_payload = $prepared_data; $response_body = array( 'success' => false ); $status_code = 400; if ( empty( $prepared_data['title'] ) ) { $response_body['message'] = __( 'A property title is required.', 'advanced-form-integration' ); } else { $post_id = wp_insert_post( array( 'post_type' => 'property', 'post_title' => $prepared_data['title'], 'post_content' => isset( $prepared_data['description'] ) ? $prepared_data['description'] : '', 'post_status' => 'publish', ) ); if ( is_wp_error( $post_id ) || ! $post_id ) { $response_body['message'] = __( 'Failed to create the property. Please verify the supplied data.', 'advanced-form-integration' ); } else { $meta_map = array( 'price' => '_price', 'department' => '_department', 'address_street' => '_address_street', 'address_postcode' => '_address_postcode', ); foreach ( $meta_map as $field_key => $meta_key ) { if ( isset( $prepared_data[ $field_key ] ) ) { update_post_meta( $post_id, $meta_key, $prepared_data[ $field_key ] ); } } $status_code = 200; $response_body['success'] = true; $response_body['message'] = __( 'Property created successfully.', 'advanced-form-integration' ); $response_body['id'] = $post_id; } } $log_args = array( 'method' => 'LOCAL', 'body' => $request_payload, ); $log_response = array( 'response' => array( 'code' => $status_code, 'message' => $response_body['message'], ), 'body' => $response_body, ); adfoin_add_to_log( $log_response, 'propertyhive', $log_args, $record ); }