| 1 |
<?php |
| 2 |
/** |
| 3 |
* Type Factory |
| 4 |
* |
| 5 |
* @package NotificationX\Types |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Types; |
| 9 |
use NotificationX\GetInstance; |
| 10 |
|
| 11 |
/** |
| 12 |
* TypeFactory Class |
| 13 |
* @method static TypeFactory get_instance($args = null) |
| 14 |
*/ |
| 15 |
class TypeFactory { |
| 16 |
/** |
| 17 |
* Instance of Admin |
| 18 |
* |
| 19 |
* @var Admin |
| 20 |
*/ |
| 21 |
use GetInstance; |
| 22 |
|
| 23 |
public $types = [ |
| 24 |
'conversions' => 'NotificationX\Types\Conversions', |
| 25 |
'woocommerce_sales' => 'NotificationX\Types\WooCommerceSales', |
| 26 |
'comments' => 'NotificationX\Types\Comments', |
| 27 |
'reviews' => 'NotificationX\Types\Reviews', |
| 28 |
'download_stats' => 'NotificationX\Types\DownloadStats', |
| 29 |
'elearning' => 'NotificationX\Types\ELearning', |
| 30 |
'donation' => 'NotificationX\Types\Donations', |
| 31 |
'notification_bar' => 'NotificationX\Types\NotificationBar', |
| 32 |
'popup' => 'NotificationX\Types\Popup', |
| 33 |
'form' => 'NotificationX\Types\ContactForm', |
| 34 |
'email_subscription' => 'NotificationX\Types\EmailSubscription', |
| 35 |
'exit_intent' => 'NotificationX\Types\ExitIntent', |
| 36 |
'page_analytics' => 'NotificationX\Types\PageAnalytics', |
| 37 |
'custom' => 'NotificationX\Types\CustomNotification', |
| 38 |
'inline' => 'NotificationX\Types\Inline', |
| 39 |
'woocommerce_cart_peek' => 'NotificationX\Types\WooCommerceCartPeek', |
| 40 |
'flashing_tab' => 'NotificationX\Types\FlashingTab', |
| 41 |
'video' => 'NotificationX\Types\Video', |
| 42 |
'offer_announcement' => 'NotificationX\Types\OfferAnnouncement', |
| 43 |
'gdpr' => 'NotificationX\Types\GDPR', |
| 44 |
]; |
| 45 |
|
| 46 |
public $types_enabled = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initially Invoked when initialized. |
| 50 |
*/ |
| 51 |
public function __construct(){ |
| 52 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. |
| 53 |
$this->types = apply_filters( 'nx_types_classes', $this->types ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers a type. |
| 59 |
* |
| 60 |
* @param string $id |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function register_types($id){ |
| 64 |
if(!isset($this->types_enabled[$id]) && isset($this->types[$id])){ |
| 65 |
$type_class = $this->types[$id]; |
| 66 |
$obj = $type_class::get_instance(); |
| 67 |
return $this->add($obj); |
| 68 |
} |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enable a type. |
| 74 |
* |
| 75 |
* @param Type $types |
| 76 |
* @return Type |
| 77 |
*/ |
| 78 |
protected function add( $type) { |
| 79 |
$this->types_enabled[ $type->id ] = $type; |
| 80 |
return $type; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* This function is responsible for getting the type from loaded type. |
| 85 |
* |
| 86 |
* @param string $key |
| 87 |
* @return Types |
| 88 |
*/ |
| 89 |
public function get( $key ){ |
| 90 |
if( empty( $key ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
if(isset($this->types_enabled[$key])){ |
| 94 |
return isset( $this->types_enabled[ $key ] ) ? $this->types_enabled[ $key ] : false; |
| 95 |
} |
| 96 |
else if(isset( $this->types[ $key ] ) && $type_class = $this->types[ $key ]){ |
| 97 |
return $type_class::get_instance(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get all enabled types. |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public function get_all(){ |
| 107 |
return $this->types_enabled; |
| 108 |
} |
| 109 |
} |
| 110 |
|