Skip to content

Commit

Permalink
3.3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Sep 20, 2023
0 parents commit e5687d0
Show file tree
Hide file tree
Showing 222 changed files with 23,672 additions and 0 deletions.
84 changes: 84 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* This file initializes plugin, once PHP version check passes
*
* IMPORTANT, plugins_loaded, priority 1 is the earliest hook you can use.
*/

use NFMailchimp\EmailCRM\Mailchimp\MailchimpApi;
use NFMailchimp\NinjaForms\Mailchimp\Admin\DiagnosticScreen;
/**
* Load the plugin and initialize container
*
* @since 2.0.0
*/
add_action('plugins_loaded', 'ninjaFormsMailchimpBoostrap', 5);

/**
* Instantiate NinjaFormsMailchimp instance
*/
function ninjaFormsMailchimpBoostrap() {

$autoloader = dirname(__FILE__) . '/vendor/autoload.php';
if (file_exists($autoloader)) {
include_once $autoloader;
}

$instance = new \NFMailchimp\NinjaForms\Mailchimp\NinjaFormsMailchimp();

$mailchimpApi = new MailchimpApi();

$instance->setMailchimpApi($mailchimpApi);

/**
* Runs directly after creation of main plugin instance
*/
do_action('nf_mailchimp_init', $instance);
}


/**
* Initialize required functionality for NinjaFormsMailchimp instance
*
* - Add NF Bridge to provide NF functionality
* - Initialize WP REST API endpoints
* - Add subscribe action
* - register opt-in field
* @since 3.2.1
*/
add_action('nf_mailchimp_init', function (\NFMailchimp\NinjaForms\Mailchimp\NinjaFormsMailchimp $instance) {

// Provide NF core functionality
setNfBridge($instance);

// Initialize WordPress REST API endpoints
add_action('rest_api_init', [$instance, 'initApi']);

// Register the Subscribe to Mailchimp action
$instance->addSubscribeAction();

// Adds modal box to autogenerate form
add_filter('ninja_forms_new_form_templates', array($instance, 'registerAutogenerateModal'));

add_filter( 'ninja_forms_register_fields', array( $instance, 'registerOptIn' ) );

add_action( 'ninja_forms_loaded', array( $instance, 'setupAdmin' ) );

add_filter('nf_react_table_extra_value_keys', array($instance, 'addMetabox'));

(new DiagnosticScreen())->registerHooks();
}, 2);

/**
* Bind an NF Bridge to the NinjaFormsMailchimp instance
* @param \NFMailchimp\NinjaForms\Mailchimp\NinjaFormsMailchimp $instance
*/
function setNfBridge(\NFMailchimp\NinjaForms\Mailchimp\NinjaFormsMailchimp $instance) {

$nfBridge = (new NFMailchimp\EmailCRM\NfBridge\NfBridge())
->registerServices();
$instance->setNfBridge($nfBridge);
}


114 changes: 114 additions & 0 deletions lib/Mailchimp/ApiRequests/GetAudienceDefinitionData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace NFMailchimp\EmailCRM\Mailchimp\ApiRequests;

use NFMailchimp\EmailCRM\Mailchimp\Entities\AudienceDefinition;
use NFMailchimp\EmailCRM\Mailchimp\Entities\SingleList;

use NFMailchimp\EmailCRM\Mailchimp\MailchimpApi;

use NFMailchimp\EmailCRM\Mailchimp\ApiRequests\GetMergeFields;
use NFMailchimp\EmailCRM\Mailchimp\ApiRequests\GetInterestCategories;
use NFMailchimp\EmailCRM\Mailchimp\ApiRequests\GetSegments;


/**
* Constructs Audience Definition with data retrieved through API
*
*/
class GetAudienceDefinitionData
{
/** @var AudienceDefinition */
private $audienceDefinition;

/** @var MailchimpApi */
protected $mailchimpApi;

/**
* List Id
* @var string
*/
protected $listId;

/**
* Construct NfMailchimpLists action
* @param MailchimpApi $mailchimpApi
*/
public function __construct(MailchimpApi $mailchimpApi)
{
$this->mailchimpApi = $mailchimpApi;
}


/**
* Construct AudienceDefinition from retrieved data
* @return AudienceDefinition
*/
public function handle(SingleList $singleList): AudienceDefinition
{

$this->audienceDefinition = new AudienceDefinition();

$this->audienceDefinition->addList($singleList);
$this->getMergeFields($singleList->getListId());
$this->getInterestCategories($singleList->getListId());
$this->addAllInterestCategories($singleList->getListId());
$this->getSegmentsViaRemoteApi($singleList->getListId());
return $this->audienceDefinition;
}

/**
* Adds merge fields retrieved through API to Audience Def
*/
protected function getMergeFields(string $listId):void
{
$mergeFieldsAction = new GetMergeFields($this->mailchimpApi);
$mergeFields = $mergeFieldsAction->requestMergeFields($listId);
$this->audienceDefinition->addMergeFields($mergeFields);
}

/**
* Adds interest categories retrieved through API to Audience Def
*/
protected function getInterestCategories(string $listId):void
{
$interestCategoriesAction = new GetInterestCategories($this->mailchimpApi);
$interestCategories = $interestCategoriesAction->requestInterestCategories($listId);
$this->audienceDefinition->addInterestCategories($interestCategories);
}

/**
* Iterate through interest categories and append interests from each category
*/
protected function addAllInterestCategories(string $listId)
{
foreach ($this->audienceDefinition->interestCategories->getInterestCategories() as $interestCategory) {
$interestCategoryId = $interestCategory->getId();
$this->appendInterests($listId, $interestCategoryId);
}
}

/**
* Add interests retrieved throcugh API to Audience Definition
*
* @param string $listId
* @param string $interestCategoryId
* @return void
*/
protected function appendInterests(string $listId, string $interestCategoryId)
{
$interestsAction = new GetInterests($this->mailchimpApi);
$interests = $interestsAction->requestInterests($listId, $interestCategoryId);
$this->audienceDefinition->appendInterests($interests);
}

/**
* Adds Tags retrieved through API to Audience Definition
*/
protected function getSegmentsViaRemoteApi(string $listId)
{
$getSegmentsAction = new GetSegments($this->mailchimpApi);
$segments = $getSegmentsAction->requestSegments($listId);
$this->audienceDefinition->addTags($segments);
}
}
42 changes: 42 additions & 0 deletions lib/Mailchimp/ApiRequests/GetInterestCategories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace NFMailchimp\EmailCRM\Mailchimp\ApiRequests;

use NFMailchimp\EmailCRM\Mailchimp\MailchimpApi;
use NFMailchimp\EmailCRM\Mailchimp\Contracts\GetInterestCategoriesFromApi;

use NFMailchimp\EmailCRM\Mailchimp\Entities\InterestCategories;

/**
* Gets a collection of interest categories from remote API
*/
class GetInterestCategories implements GetInterestCategoriesFromApi
{

/** @var MailchimpApi */
protected $mailchimpApi;

/**
* @param MailchimpApi $mailchimpApi
*/
public function __construct(MailchimpApi $mailchimpApi)
{
$this->mailchimpApi = $mailchimpApi;
}

/** @inheritDoc */
public function requestInterestCategories(string $listId): InterestCategories
{
try {
$r = $this->mailchimpApi->getInterestCategories($listId, ['count' => 500]);
$interestCategories = InterestCategories::fromArray((array) $r->categories);

$interestCategories->setListId($listId);

return $interestCategories;
} catch (\Exception $e) {
error_log(self::class . '::' . __FUNCTION__).' - ' . $e->getMessage();
return new InterestCategories();
}
}
}
67 changes: 67 additions & 0 deletions lib/Mailchimp/ApiRequests/GetInterests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace NFMailchimp\EmailCRM\Mailchimp\ApiRequests;

use NFMailchimp\EmailCRM\Mailchimp\MailchimpApi;

use NFMailchimp\EmailCRM\Mailchimp\Contracts\GetInterestsFromApi;
use NFMailchimp\EmailCRM\Mailchimp\Entities\Interests;

/**
* Gets a collection of interests from remote API
*/
class GetInterests implements GetInterestsFromApi
{

/** @var MailchimpApi */
protected $mailchimpApi;

/**
* List Id
* @var string
*/
protected $listId;

/**
* @param MailchimpApi $mailchimpApi API client library
*/
public function __construct(MailchimpApi $mailchimpApi)
{
$this->mailchimpApi = $mailchimpApi;
}

/** @inheritdoc */
public function requestInterests(string $listId, string $interestCategoryId): Interests
{
try {
$r = $this->mailchimpApi->getInterests($listId, $interestCategoryId, ['count' => 500]);
$interests = Interests::fromArray((array) $r->interests);
$interests->setListId($listId);
return $interests;
} catch (\Exception $e) {

$this->logException($e, __FUNCTION__);

return new Interests();
}
}

/**
* Log the exception with traceability
*
* @param \Exception $e
* @param string $functionName
* @return void
*/
protected function logException(\Exception $e, string $functionName): void
{
error_log($this->constructLogMessage($e, $functionName));
}

protected function constructLogMessage(\Exception $e, string $functionName): string
{
$return = self::class . '::' . $functionName . ' - ' . $e->getMessage();

return $return;
}
}
47 changes: 47 additions & 0 deletions lib/Mailchimp/ApiRequests/GetLists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace NFMailchimp\EmailCRM\Mailchimp\ApiRequests;

use NFMailchimp\EmailCRM\Mailchimp\MailchimpApi;
use NFMailchimp\EmailCRM\Mailchimp\Contracts\GetsListsFromApi;
use NFMailchimp\EmailCRM\Mailchimp\Entities\Lists;

/**
* Retrieves Mailchimp lists structured as Lists entity
*/
class GetLists implements GetsListsFromApi
{

/**
* @var MailchimpApi
*/
protected $mailchimpApi;

/**
* @param MailchimpApi $mailchimpApi
*/
public function __construct(MailchimpApi $mailchimpApi)
{
$this->mailchimpApi = $mailchimpApi;
}

/**
* Request collection of lists from remote API
*
* If exception is returned, return empty Lists
*
* @return Lists
*/
public function requestLists(): Lists
{
try {

$response = $this->mailchimpApi->getLists(['count' => 500]);
return Lists::fromArray((array) $response->lists);
} catch (\Exception $e) {

error_log(self::class . '::' . __FUNCTION__).' - ' . $e->getMessage();
return new Lists();
}
}
}
Loading

0 comments on commit e5687d0

Please sign in to comment.