action_priority; } public function load_hooks() { // } public function get_view() { ?>
data = []; } /** * Saves the request data and response data for every action into the DB * * @param $data * @param $response */ public function save_data( $data, $response ) { } /** * Checks the required fields for every action * * @param $data * @param $required_fields * * @return bool */ public function check_fields( $data, $required_fields ) { $bool = true; foreach ( $required_fields as $single_field ) { if ( false === isset( $data[ $single_field ] ) ) { $this->missing_field = $single_field; $bool = false; break; } } return $bool; } /** * Return the error * * @return array */ public function show_fields_error() { $message = __( 'Required Field Missing', 'wp-marketing-automations' ); $message .= ': ' . $this->missing_field; return array( 'bwfan_response' => $message, ); } /** * Sends a wp remote call * * @param $url * @param array $params * @param int $req_method * * @return array|mixed|object|string */ public function make_wp_requests( $url, $params = array(), $headers = array(), $req_method = 1 ) { $body = array( 'response' => 500, 'body' => __( 'Curl Error', 'wp-marketing-automations' ), ); // $req_method // 1 stands for get // 2 stands for post // 3 stands for delete $args = array( 'timeout' => 45, 'httpversion' => '1.0', 'blocking' => true, 'body' => $params, ); if ( is_array( $headers ) && count( $headers ) > 0 ) { $args['headers'] = $headers; } switch ( $req_method ) { case 2: $args['method'] = 'POST'; break; case 3: $args['method'] = 'DELETE'; break; case 4: $args['method'] = 'PUT'; break; case 5: $args['method'] = 'PATCH'; break; default: $args['method'] = 'GET'; break; } $response = wp_remote_request( $url, $args ); if ( ! is_wp_error( $response ) ) { $body = wp_remote_retrieve_body( $response ); $headers = wp_remote_retrieve_headers( $response ); if ( $this->is_json( $body ) ) { $body = json_decode( $body, true ); } $body = maybe_unserialize( $body ); if ( in_array( $response['response']['code'], $this->allowed_responses, true ) ) { $response_code = 200; } else { $response_code = $response['response']['code']; } $body = array( 'response' => intval( $response_code ), 'body' => $body, 'headers' => $headers, ); return $body; } $body['body'] = [ $response->get_error_message() ]; return $body; } /** * check if a string is json or not * * @param $string * * @return bool */ public function is_json( $string ) { json_decode( $string ); return ( json_last_error() == JSON_ERROR_NONE );//phpcs:ignore WordPress.PHP.StrictComparisons } public function parse_unsubscribe_link() { if ( false === $this->automation_id ) { return; } add_filter( 'bwfan_unsubscribe_link', [ $this, 'bwfan_unsubscribe_link_add_aid' ] ); } public function bwfan_unsubscribe_link_add_aid( $link ) { $link = add_query_arg( array( 'automation_id' => $this->automation_id, ), $link ); return $link; } /** * @param $integration_object BWFAN_Integration * @param $task_meta * * @return array */ public function make_data( $integration_object, $task_meta ) { return $task_meta; } /** * @param array $automation_data * @param array $step_data * * @return array */ public function make_v2_data( $automation_data, $step_data ) { return array(); } public function process_v2() { return $this->success_message(); } public function success_message( $message = '' ) { if ( empty( $message ) ) { $message = __( 'Action Executed Successfully!', 'wp-marketing-automations' ); } return array( 'status' => self::$RESPONSE_SUCCESS, 'message' => $message, ); } public function error_response( $message = '' ) { if ( empty( $message ) ) { $message = __( 'Unknown error occurred', 'wp-marketing-automations' ); } return array( 'status' => self::$RESPONSE_FAILED, 'message' => $message, ); } public function skipped_response( $message = '' ) { if ( empty( $message ) ) { $message = __( 'Unknown error occurred', 'wp-marketing-automations' ); } return array( 'status' => self::$RESPONSE_SKIPPED, 'message' => $message, ); } /** * Execute the current action. * Return 3 for successful execution , 4 for permanent failure. * * @param $action_data * * @return array */ public function execute_action( $action_data ) { $result = [ 'status' => 0, 'message' => __( 'Default Resource Message', 'wp-marketing-automations' ), ]; $integration = BWFAN_Core()->integration->get_integration( $action_data['integration_slug'] ); if ( is_null( $integration ) ) { return $result; } $this->set_data( $action_data['processed_data'] ); if ( $integration->need_connector() ) { $load_connector = WFCO_Load_Connectors::get_instance(); $call_class = $load_connector->get_call( $this->call ); if ( is_null( $call_class ) ) { return $result; } $call_class->set_data( $action_data['processed_data'] ); $result = $call_class->process(); $result = $integration->handle_response( $result, $this->connector, $this->call ); $result = $this->handle_response( $result, $call_class ); } else { $result = $this->process(); } return $result; } /** * @param $response * @param $call_object WFCO_Call * * @return mixed */ protected function handle_response( $response, $call_object = null ) { return $response; } public function process() { return ''; } /** * Get the merge tags which are array types and convert their values as comma separated string * * @param $dynamic_array * @param $integration_data * * @return array */ public function parse_merge_tags( $dynamic_array, $integration_data ) { $result = array(); foreach ( $dynamic_array as $key1 => $value ) { if ( is_array( $value ) && count( $value ) > 0 ) { $dynamic_array[ $key1 ] = implode( ', ', $value ); } } $result['parsed_merge_tags'] = $dynamic_array; $result['data'] = $integration_data; return $result; } public function parse_tags_fields( $dynamic_array, $integration_data ) { $result = array(); $new_merge_tags = array(); foreach ( $dynamic_array as $key1 => $value ) { $key = array_search( '{{' . $key1 . '}}', $integration_data, true ); if ( is_array( $integration_data ) && count( $integration_data ) > 0 ) {//phpcs:ignore WordPress.PHP.StrictComparisons foreach ( $integration_data as $possible_merge_tag ) { if ( is_array( $possible_merge_tag ) && count( $possible_merge_tag ) > 0 ) { foreach ( $possible_merge_tag as $p_m_t ) { if ( strpos( $p_m_t, '{{' . $key1 . '}}' ) !== false ) { if ( is_array( $value ) && count( $value ) > 0 ) { unset( $integration_data[ $key ] ); foreach ( $value as $key2 => $value2 ) { $integration_data[] = '{{tag_' . $key2 . '}}'; $new_merge_tags[ 'tag_' . $key2 ] = str_replace( '{{' . $key1 . '}}', $value2, $p_m_t ); } } elseif ( '' !== $value ) { $integration_data[] = '{{' . $key1 . '}}'; $new_merge_tags[ $key1 ] = str_replace( '{{' . $key1 . '}}', $value, $p_m_t ); } } } } else { if ( strpos( $possible_merge_tag, '{{' . $key1 . '}}' ) !== false ) { if ( is_array( $value ) && count( $value ) > 0 ) { unset( $integration_data[ $key ] ); foreach ( $value as $key2 => $value2 ) { $integration_data[] = '{{tag_' . $key2 . '}}'; $new_merge_tags[ 'tag_' . $key2 ] = str_replace( '{{' . $key1 . '}}', $value2, $possible_merge_tag ); } } elseif ( '' !== $value ) { $integration_data[] = '{{' . $key1 . '}}'; $new_merge_tags[ $key1 ] = str_replace( '{{' . $key1 . '}}', $value, $possible_merge_tag ); } } } } } } $integration_data = array_unique( $integration_data ); $result['parsed_merge_tags'] = $new_merge_tags; $result['data'] = $integration_data; return $result; } public function set_data_for_merge_tags( $task_meta ) { $event_slug = $task_meta['event_data']['event_slug']; BWFAN_Merge_Tag_Loader::set_data( array( 'automation_id' => $task_meta['automation_id'], ) ); $single_event = BWFAN_Core()->sources->get_event( $event_slug ); if ( $single_event instanceof BWFAN_Event ) { $single_event->set_merge_tags_data( $task_meta ); // This function is written in every event class } } public function get_class_slug() { return str_replace( 'bwfan_', '', strtolower( get_class( $this ) ) ); } public function before_executing_task() { } public function after_executing_task() { } public function get_integration_type() { return $this->integration_type; } public function set_integration_type( $type ) { $this->integration_type = $type; } public function get_included_events() { return $this->included_events; } public function get_excluded_events() { return $this->excluded_events; } /** * Return localize data of event for frontend UI * @return array */ public function get_localize_data() { $this->localize_data = [ 'included_events' => $this->included_events, 'excluded_events' => $this->excluded_events, 'action_name' => $this->action_name, 'action_desc' => $this->action_desc, 'slug' => $this->get_slug(), 'support_v1' => $this->support_v1, 'required' => $this->required_fields, 'support_language' => $this->support_language, ]; return apply_filters( 'bwfan_action_' . $this->get_slug() . '_localize_data', $this->localize_data, $this ); } /** * Returns actions data * * @return mixed */ public function get_action_data_for_api() { if ( ! $this->support_v2 ) { return []; } $data = $this->localize_data; $data['integration_slug'] = $this->integration_type; $data['fields'] = method_exists( $this, 'get_fields_schema' ) ? $this->get_fields_schema() : []; $data['default_values'] = method_exists( $this, 'get_default_values' ) ? $this->get_default_values() : []; $data['side_header'] = $this->support_sideheader; $data['support_v2'] = $this->support_v2; $data['analytics_mode'] = $this->analytics_mode; return $data; } /** * Returns action side schema * * @return array */ public function get_fields_schema() { return array(); } public function is_editor_supported() { return $this->is_editor_supported; } public function get_data() { return $this->data; } /** * Set the data for every action * * @param $data */ public function set_data( $data ) { $this->data = $data; } public function get_name() { return $this->action_name; } public function check_required_data( $data ) { return true; } public function get_action_retry_data() { return array( HOUR_IN_SECONDS, // 1 hr 6 * HOUR_IN_SECONDS, // 6 hrs 18 * HOUR_IN_SECONDS, // 18 hrs ); } public function __get( $key ) { if ( 'call' === $key ) { return 'wfco_' . $this->get_slug(); } } /** v2 Methods: START */ public function get_options( $search = '', $identifier = '' ) { return []; } /** v2 Methods: END */ /** * @param $response * * @return mixed */ public function handle_response_v2( $response ) { return $response; } /** * to avoid unserialize of the current class */ public function __wakeup() { throw new ErrorException( 'BWFAN_Core can`t converted to string' ); } /** * to avoid serialize of the current class */ public function __sleep() { throw new ErrorException( 'BWFAN_Core can`t converted to string' ); } /** * To avoid cloning of current class */ protected function __clone() { } }