<commit_before><?php
/**
 * Postmark API handler
 *
 * @package bbSubscriptions
 * @subpackage Handlers
 */

require_once FALCON_PATH . '/vendor/postmark-inbound/lib/Postmark/Autoloader.php';
\Postmark\Autoloader::register();

/**
 * Postmark API handler
 *
 * @package bbSubscriptions
 * @subpackage Handlers
 */
class Falcon_Handler_Postmark implements Falcon_Handler {
	/**
	 * For the settings callbacks, we need to hold on to the current options
	 *
	 * @var array
	 */
	protected static $current_options = array();

	public function __construct($options) {
		if (empty($options) || empty($options['api_key'])) {
			throw new Exception(__('Postmark API key not set', 'falcon'));
		}
		$this->api_key = $options['api_key'];
	}

	public function send_mail( $users, $subject, $content, $options ) {
		$from = Falcon::get_from_address();
		if ( ! empty( $options['author'] ) ) {
			$from = sprintf( '%s <%s>', $options['author'], $from );
		}

		foreach ($users as $user) {
			$data = array(
				'From' => $from,
				'ReplyTo' => Falcon::get_reply_address( $options['id'], $user ),
				'To' => $user->user_email,
				'Subject' => $subject,
				'TextBody' => $content,
			);

			$this->send_single($data);
		}
	}

	protected function send_single($data) {
		$headers = array(
			'Accept' => 'application/json',
			'Content-Type' => 'application/json',
			'X-Postmark-Server-Token' => $this->api_key,
		);

		$response = wp_remote_post('http://api.postmarkapp.com/email', array(
			'headers' => $headers,
			'body' => json_encode($data)
		));

		$code = wp_remote_retrieve_response_code($response);
		switch ($code) {
			case 200:
				return true;
			case 401:
				throw new Exception(__('Invalid API key', 'falcon'), 401);
			case 422:
				throw new Exception(sprintf(__('Error with sent body: %s', 'falcon'), wp_remote_retrieve_body($response)), 422);
			case 500:
				throw new Exception(__('Postmark server error', 'falcon'), 500);
			default:
				throw new Exception(__('Unknown error', 'falcon'), $code);
		}
	}

	/**
	 * Check the inbox for replies
	 *
	 * Postmark sends POST requests when we receive an email instead
	 */
	public function check_inbox() {}

	public function handle_post() {
		$input = file_get_contents('php://input');
		if (empty($input)) {
			header('X-Fail: No input', true, 400);
			echo 'No input found.'; // intentionally not translated
			return;
		}
		file_put_contents('/tmp/postmark', $input);
		try {
			$inbound = new \Postmark\Inbound($input);
		}
		catch (\Postmark\InboundException $e) {
			header('X-Fail: Postmark problem', true, 400);
			echo $e->getMessage();
			return;
		}

		// The "Test" button sends an email from <EMAIL>
		if ($inbound->FromEmail() === '<EMAIL>') {
			echo 'Hello tester!'; // intentionally not translated
			return;
		}

		$reply = new bbSubscriptions_Reply();
		$reply->from = $inbound->FromEmail();
		$reply->subject = $inbound->Subject();
		$reply->body = $inbound->TextBody();

		$to = $inbound->Recipients();
		list($reply->topic, $reply->nonce) = bbSubscriptions_Reply::parse_to($to[0]->Email);

		$reply_id = $reply->insert();
		if ($reply_id === false) {
			header('X-Fail: No reply ID', true, 400);
			echo 'Reply could not be added?'; // intentionally not translated
			// Log this?
		}
	}

	/**
	 * Get a human-readable name for the handler
	 *
	 * This is used for the handler selector and is shown to the user.
	 * @return string
	 */
	public static function get_name() {
		return 'Postmark';
	}

	/**
	 * Output a description for the options
	 *
	 * If you have any extra information you want to tell the user, this is
	 * probably the best place for it to live.
	 */
	public static function options_section_header() {
?><commit_msg>More class updating<commit_after><?php
/**
 * Postmark API handler
 *
 * @package bbSubscriptions
 * @subpackage Handlers
 */

require_once FALCON_PATH . '/vendor/postmark-inbound/lib/Postmark/Autoloader.php';
\Postmark\Autoloader::register();

/**
 * Postmark API handler
 *
 * @package bbSubscriptions
 * @subpackage Handlers
 */
class Falcon_Handler_Postmark implements Falcon_Handler {
	/**
	 * For the settings callbacks, we need to hold on to the current options
	 *
	 * @var array
	 */
	protected static $current_options = array();

	public function __construct($options) {
		if (empty($options) || empty($options['api_key'])) {
			throw new Exception(__('Postmark API key not set', 'falcon'));
		}
		$this->api_key = $options['api_key'];
	}

	public function send_mail( $users, $subject, $content, $options ) {
		$from = Falcon::get_from_address();
		if ( ! empty( $options['author'] ) ) {
			$from = sprintf( '%s <%s>', $options['author'], $from );
		}

		foreach ($users as $user) {
			$data = array(
				'From' => $from,
				'ReplyTo' => Falcon::get_reply_address( $options['id'], $user ),
				'To' => $user->user_email,
				'Subject' => $subject,
				'TextBody' => $content,
			);

			$this->send_single($data);
		}
	}

	protected function send_single($data) {
		$headers = array(
			'Accept' => 'application/json',
			'Content-Type' => 'application/json',
			'X-Postmark-Server-Token' => $this->api_key,
		);

		$response = wp_remote_post('http://api.postmarkapp.com/email', array(
			'headers' => $headers,
			'body' => json_encode($data)
		));

		$code = wp_remote_retrieve_response_code($response);
		switch ($code) {
			case 200:
				return true;
			case 401:
				throw new Exception(__('Invalid API key', 'falcon'), 401);
			case 422:
				throw new Exception(sprintf(__('Error with sent body: %s', 'falcon'), wp_remote_retrieve_body($response)), 422);
			case 500:
				throw new Exception(__('Postmark server error', 'falcon'), 500);
			default:
				throw new Exception(__('Unknown error', 'falcon'), $code);
		}
	}

	/**
	 * Check the inbox for replies
	 *
	 * Postmark sends POST requests when we receive an email instead
	 */
	public function check_inbox() {}

	public function handle_post() {
		$input = file_get_contents('php://input');
		if (empty($input)) {
			header('X-Fail: No input', true, 400);
			echo 'No input found.'; // intentionally not translated
			return;
		}
		file_put_contents('/tmp/postmark', $input);
		try {
			$inbound = new \Postmark\Inbound($input);
		}
		catch (\Postmark\InboundException $e) {
			header('X-Fail: Postmark problem', true, 400);
			echo $e->getMessage();
			return;
		}

		// The "Test" button sends an email from <EMAIL>
		if ($inbound->FromEmail() === '<EMAIL>') {
			echo 'Hello tester!'; // intentionally not translated
			return;
		}

		$reply = new Falcon_Reply();
		$reply->from = $inbound->FromEmail();
		$reply->subject = $inbound->Subject();
		$reply->body = $inbound->TextBody();

		$to = $inbound->Recipients();
		list($reply->topic, $reply->nonce) = Falcon_Reply::parse_to($to[0]->Email);

		$reply_id = $reply->insert();
		if ($reply_id === false) {
			header('X-Fail: No reply ID', true, 400);
			echo 'Reply could not be added?'; // intentionally not translated
			// Log this?
		}
	}

	/**
	 * Get a human-readable name for the handler
	 *
	 * This is used for the handler selector and is shown to the user.
	 * @return string
	 */
	public static function get_name() {
		return 'Postmark';
	}

	/**
	 * Output a description for the options
	 *
	 * If you have any extra information you want to tell the user, this is
	 * probably the best place for it to live.
	 */
	public static function options_section_header() {
?>
{'fraction_non_alphanumeric': 0.16185594820609658, 'fraction_numerical': 0.008362557323981657, 'mean_word_length': 3.707936507936508, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2997255', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2368, 'n_words': 656}