Last active
July 6, 2020 18:56
-
-
Save MCKLtech/e92a7ba5950d800e62bf000205f3d618 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GroundhoggDisposable; | |
use Groundhogg\Contact; | |
/* | |
* Plugin Name: Groundhogg - Disposable Email Checker | |
* Plugin URI: https://www.wooninja.io/?utm_source=wp-plugins&utm_campaign=groundhogg-disposable&utm_medium=wp-dash | |
* Description: Remove disposable emails from your Groundhogg List | |
* Version: 1.0 | |
* Author: Colin Longworth | |
* Author URI: https://www.wooninja.io/?utm_source=wp-plugins&utm_campaign=groundhogg-disposable&utm_medium=wp-dash | |
* Text Domain: groundhogg-disposable | |
* Domain Path: /languages | |
* | |
* Groundhogg is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* any later version. | |
* | |
* Groundhogg is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
*/ | |
if (!defined('ABSPATH')) exit; | |
$disposable = new Groundhogg_Disposable(); | |
class Groundhogg_Disposable { | |
/** | |
* Should contacts be unsubscribed? | |
* | |
* Default: true | |
* | |
* @var bool | |
*/ | |
public $unsub = true; | |
/** | |
* Should contacts be deleted? | |
* | |
* Default: true | |
* | |
* @var bool | |
*/ | |
public $delete = true; | |
public function __construct() { | |
/** | |
* Hook to the creation and update processes in Groundhogg | |
*/ | |
add_action( 'groundhogg/contact/post_update', array( $this, 'check_contact' ), 99, 4 ); | |
add_action( 'groundhogg/contact/post_create', array( $this, 'check_contact' ), 99, 3 ); | |
} | |
/** | |
* Check a given contact against our list | |
* | |
* See the Contact class in Groundhogg for input parameters | |
* | |
* @param $contact_id | |
* @param $data | |
* @param $object | |
* @param bool $old_data | |
*/ | |
public function check_contact($contact_id, $data, $object, $old_data = false ) { | |
/* Store list in transient to reduce load */ | |
if ( false === ( $list = get_transient( 'groundhogg_disposable_list' ) ) ) { | |
$list = $this->fetchList(); | |
} | |
//List is empty, we can't run a check. Abort! | |
if(empty($list)) return; | |
//Load the contact | |
$contact = new Contact($contact_id); | |
//Check they ecist | |
if($contact->exists()) { | |
/* Extract the email and domain */ | |
$email = $contact->get_email(); | |
$domain = $this->fetchDomain($email); | |
if($domain) { | |
foreach($list as $id => $email_domain) { | |
if($email_domain == $domain) { | |
if($this->unsub) $contact->unsubscribe(); | |
if($this->delete) $contact->delete(); | |
break; | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Returns the domain from a given email e.g. example.com | |
* | |
* @param $email | |
* @return bool|mixed|string | |
*/ | |
private function fetchDomain($email) { | |
if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { | |
$parts = explode('@', $email); | |
return array_pop($parts); | |
} | |
return false; | |
} | |
/** | |
* Returns an array of disposabl emails from the given service provider | |
* | |
* @return array|mixed | |
*/ | |
public function fetchList() { | |
$url = apply_filters('groundhogg/disposable/list/url', 'https://cdn.jsdelivr.net/gh/andreis/disposable-email-domains@master/domains.json'); | |
$response = wp_remote_request( $url ); | |
//Check for success | |
if(!is_wp_error($response) && ($response['response']['code'] == 200 || $response['response']['code'] == 201)) { | |
$body = wp_remote_retrieve_body($response); | |
$list = json_decode($body); | |
if(count($list) > 0) { | |
set_transient( 'groundhogg_disposable_list', $list, DAY_IN_SECONDS * 7 ); | |
} | |
return $list; | |
} | |
else { | |
return []; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment