STRATO-apps/wordpress_03/app/wp-content/plugins/fluentformpro/src/Integrations/Zapier/Client.php

SHA-256: 09d1c422d6e0dc010c7d3998cc0e453f6d819244f0061c3653bf5288632a2c1c
<?php

namespace FluentFormPro\Integrations\Zapier;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

use Exception;
use FluentForm\Framework\Foundation\Application;

class Client
{
	use NotifyTrait;

	protected $app = null;
	protected $table = 'fluentform_form_meta';
	protected $metaKey = 'fluentform_zapier_feed';
	protected $baseUrl = 'https://hooks.zapier.com/hooks/catch/';

	public function __construct(Application $app)
	{
		$this->app = $app;
	}

	/**
	 * Get Zapier Feeds
	 * @return JSON Response
	 */
	public function getNotifications()
	{
		$hooks = wpFluent()
		->table($this->table)
		->where('form_id', intval($this->app->request->get('form_id')))
		->where('meta_key', $this->metaKey)
		->get();

		if ($hooks) {
			$hooks = array_map(function($hook) {
				$hook->value = json_decode($hook->value);
				return $hook;
			}, $hooks);
		}

		wp_send_json_success($hooks);
	}

	/**
	 * Save Zapier Feed
	 * @return JSON Response
	 */
	public function saveNotification()
	{
		$attributes = $this->app->request->all();
		
		$sanitizeMap = [
			'form_id' => 'intval',
			'id' => 'intval',
		];
		$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap);
		
		$request = $this->validate($attributes);

		if (empty($request['id'])) {
			$id = $this->insertNotification($request);
		} else {
			$id = $this->updateNotification($request);
		}

		wp_send_json_success(['id' => $id]);
	}

	protected function insertNotification($request)
	{
		return wpFluent()
		->table($this->table)
		->insertGetId([
			'meta_key' => $this->metaKey,
			'form_id' => $request['form_id'],
			'value' => $request['value']
		]);
	}

	protected function updateNotification($request)
	{
		wpFluent()
		->table($this->table)
		->where('id', $id = $request['id'])
		->update(['value' => $request['value']]);

		return $id;
	}

	/**
	 * Delete Zapier Feed
	 * @return JSON Response
	 */
	public function deleteNotification()
    {
    	try {
    		$id = intval($this->app->request->get('id'));
    		
    		wpFluent()
			->table($this->table)
			->where('id', $id)
			->delete();

	        wp_send_json_success();
    	} catch(Exception $e) {
    		wp_send_json_error();
    	}
    }

    protected function validate($request)
	{
		$validator = fluentValidator($request['value'], [
			'name' => 'required',
			'url' => 'required|url'
		]);

		if ($validator->validate()->fails()) {
			wp_send_json_error($validator->errors(), 422);
		}

		$request['value']['url'] = esc_url_raw($request['value']['url']);
		$request['value']['name'] = sanitize_text_field($request['value']['name']);
		
		$request['value'] = json_encode($request['value']);

		return $request;
	}
}