class-llms-mailhawk.php 4.25 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
<?php
/**
 * MailHawk Connect
 *
 * @package LifterLMS/Admin/Classes
 *
 * @since 3.40.0
 * @version 3.40.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_MailHawk class
 *
 * @since 3.40.0
 */
class LLMS_MailHawk extends LLMS_Abstract_Email_Provider {

	/**
	 * LifterLMS MailHawk Partner ID.
	 *
	 * @var int
	 */
	const PARTNER_ID = 3;

	/**
	 * Connector's ID.
	 *
	 * @var string
	 */
	protected $id = 'mailhawk';

	/**
	 * Configures the response returned when `do_remote_install()` is successful.
	 *
	 * @since 3.40.0
	 *
	 * @return array
	 */
	protected function do_remote_install_success() {
		return array(
			'partner_id'   => self::PARTNER_ID,
			'register_url' => esc_url( trailingslashit( MAILHAWK_LICENSE_SERVER_URL ) ),
			'client_state' => esc_html( \MailHawk\Keys::instance()->state() ),
			'redirect_uri' => esc_url( \MailHawk\get_admin_mailhawk_uri() ),
		);
	}

	/**
	 * Retrieve the settings area HTML for the connect button
	 *
	 * @since 3.40.0
	 *
	 * @return string
	 */
	protected function get_connect_setting() {

		if ( $this->is_connected() ) {

			$ret = array(
				__( 'Your site is connected to MailHawk.', 'lifterlms' ),
			);

			$settings_url = esc_url( admin_url( '/tools.php?page=mailhawk' ) );

			if ( function_exists( '\MailHawk\mailhawk_is_suspended' ) && ! \MailHawk\mailhawk_is_suspended() ) {
				$ret[] = sprintf(
					// Translators: %1$s = Opening anchor tag to WP MailHawk Settings; Opening anchor tag to MailHawk.io account page; %2$s = Closing anchor tag.
					__( '%1$sView settings%3$s or %2$smanage your account%3$s.', 'lifterlms' ),
					'<a href="' . $settings_url . '">',
					'<a href="https://mailhawk.io/account/" target="_blank" rel="noopener noreferrer">',
					'</a>'
				);
			} else {
				$ret[] = sprintf(
					// Translators: %1$s = Opening anchor tag; %2$s = Closing anchor tag.
					'<em>' . __( 'Email sending is currently disabled. %1$sVisit MailHawk Settings%2$s to enable sending.', 'lifterlms' ) . '</em>',
					'<a href="' . $settings_url . '">',
					'</a>'
				);
			}

			return '<p>' . implode( ' ', $ret ) . '</p>';

		}

		return sprintf( '<button type="button" class="button button-primary big-button" id="llms-mailhawk-connect"><span class="dashicons dashicons-email-alt"></span> %s</button>', __( 'Connect MailHawk', 'lifterlms' ) );

	}

	/**
	 * Retrieve description text to be used in the settings area.
	 *
	 * @since 3.40.0
	 *
	 * @return string
	 */
	protected function get_description() {

		return sprintf(
			// Translators: %s = Anchor tag html linking to MailHawk.io.
			__( 'Never worry about sending email again. %s takes care of everything for you starting for a small monthly fee.', 'lifterlms' ),
			'<a href="https://lifterlikes.com/mailhawk" target="_blank">' . $this->get_title() . '</a>'
		);

	}

	/**
	 * Retrieve the connector's name / title.
	 *
	 * @since 3.40.0
	 *
	 * @return string
	 */
	protected function get_title() {
		return __( 'MailHawk', 'lifterlms' );
	}

	/**
	 * Determines if MailHawk is installed and connected for sending.
	 *
	 * @since 3.40.0
	 *
	 * @return boolean
	 */
	protected function is_connected() {
		return ( function_exists( '\MailHawk\mailhawk_is_connected' ) && \MailHawk\mailhawk_is_connected() );
	}

	/**
	 * Determines if connector plugin is installed
	 *
	 * @since 3.40.0
	 *
	 * @return boolean
	 */
	protected function is_installed() {
		return defined( 'MAILHAWK_VERSION' );
	}

	/**
	 * Output some quick and dirty inline JS.
	 *
	 * @since 3.40.0
	 *
	 * @return void
	 */
	public function output_js( $additional_js = '' ) {

		if ( ! $this->should_output_inline() ) {
			return;
		}

		?>
		<script>
			jQuery( '#llms-mailhawk-connect' ).on( 'click', function( e ) {

				e.preventDefault();

				LLMS.Spinner.start( jQuery( this ), 'small' );

				var data = {
					action: 'llms_mailhawk_remote_install',
					_llms_mailhawk_nonce: '<?php echo wp_create_nonce( 'llms-mailhawk-install' ); ?>',
				};

				window.llms.emailConnectors.remoteInstall( jQuery( this ), data, function( res ) {

					window.llms.emailConnectors.registerClient( res.register_url, {
						'mailhawk_plugin_signup': 'yes',
						'state': res.client_state,
						'redirect_uri': res.redirect_uri,
						'partner_id': res.partner_id
					} );

				} );

			} );
		</script>
		<?php

	}

}

return new LLMS_MailHawk();