| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- namespace MailPoet\Config;
- if (!defined('ABSPATH')) exit;
- use MailPoet\Form\DisplayFormInWPContent;
- use MailPoet\Mailer\WordPress\WordpressMailerReplacer;
- use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
- use MailPoet\Segments\WP;
- use MailPoet\Settings\SettingsController;
- use MailPoet\Subscription\Comment;
- use MailPoet\Subscription\Form;
- use MailPoet\Subscription\Manage;
- use MailPoet\Subscription\Registration;
- use MailPoet\WP\Functions as WPFunctions;
- class Hooks {
- /** @var Form */
- private $subscriptionForm;
- /** @var Comment */
- private $subscriptionComment;
- /** @var Manage */
- private $subscriptionManage;
- /** @var Registration */
- private $subscriptionRegistration;
- /** @var SettingsController */
- private $settings;
- /** @var WPFunctions */
- private $wp;
- /** @var PostNotificationScheduler */
- private $postNotificationScheduler;
- /** @var WordpressMailerReplacer */
- private $wordpressMailerReplacer;
- /** @var DisplayFormInWPContent */
- private $displayFormInWPContent;
- /** @var WP */
- private $wpSegment;
- /** @var HooksWooCommerce */
- private $hooksWooCommerce;
- public function __construct(
- Form $subscriptionForm,
- Comment $subscriptionComment,
- Manage $subscriptionManage,
- Registration $subscriptionRegistration,
- SettingsController $settings,
- WPFunctions $wp,
- PostNotificationScheduler $postNotificationScheduler,
- WordpressMailerReplacer $wordpressMailerReplacer,
- DisplayFormInWPContent $displayFormInWPContent,
- HooksWooCommerce $hooksWooCommerce,
- WP $wpSegment
- ) {
- $this->subscriptionForm = $subscriptionForm;
- $this->subscriptionComment = $subscriptionComment;
- $this->subscriptionManage = $subscriptionManage;
- $this->subscriptionRegistration = $subscriptionRegistration;
- $this->settings = $settings;
- $this->wp = $wp;
- $this->postNotificationScheduler = $postNotificationScheduler;
- $this->wordpressMailerReplacer = $wordpressMailerReplacer;
- $this->displayFormInWPContent = $displayFormInWPContent;
- $this->wpSegment = $wpSegment;
- $this->hooksWooCommerce = $hooksWooCommerce;
- }
- public function init() {
- $this->setupWPUsers();
- $this->setupWooCommerceUsers();
- $this->setupWooCommercePurchases();
- $this->setupWooCommerceSubscriberEngagement();
- $this->setupImageSize();
- $this->setupListing();
- $this->setupSubscriptionEvents();
- $this->setupWooCommerceSubscriptionEvents();
- $this->setupPostNotifications();
- $this->setupWooCommerceSettings();
- $this->setupFooter();
- }
- public function initEarlyHooks() {
- $this->setupMailer();
- }
- public function setupSubscriptionEvents() {
- $subscribe = $this->settings->get('subscribe', []);
- // Subscribe in comments
- if (
- isset($subscribe['on_comment']['enabled'])
- &&
- (bool)$subscribe['on_comment']['enabled']
- ) {
- if ($this->wp->isUserLoggedIn()) {
- $this->wp->addAction(
- 'comment_form_field_comment',
- [$this->subscriptionComment, 'extendLoggedInForm']
- );
- } else {
- $this->wp->addAction(
- 'comment_form_after_fields',
- [$this->subscriptionComment, 'extendLoggedOutForm']
- );
- }
- $this->wp->addAction(
- 'comment_post',
- [$this->subscriptionComment, 'onSubmit'],
- 60,
- 2
- );
- $this->wp->addAction(
- 'wp_set_comment_status',
- [$this->subscriptionComment, 'onStatusUpdate'],
- 60,
- 2
- );
- }
- // Subscribe in registration form
- if (
- isset($subscribe['on_register']['enabled'])
- &&
- (bool)$subscribe['on_register']['enabled']
- ) {
- if (is_multisite()) {
- $this->wp->addAction(
- 'signup_extra_fields',
- [$this->subscriptionRegistration, 'extendForm']
- );
- $this->wp->addAction(
- 'wpmu_validate_user_signup',
- [$this->subscriptionRegistration, 'onMultiSiteRegister'],
- 60,
- 1
- );
- } else {
- $this->wp->addAction(
- 'register_form',
- [$this->subscriptionRegistration, 'extendForm']
- );
- // we need to process new users while they are registered.
- // We used `register_post` before but that is too soon
- // because if registration fails during `registration_errors` we will keep the user as subscriber.
- // So we are hooking to `registration_error` with a low priority.
- $this->wp->addFilter(
- 'registration_errors',
- [$this->subscriptionRegistration, 'onRegister'],
- 60,
- 3
- );
- }
- $this->wp->addAction(
- 'woocommerce_register_form',
- [$this->hooksWooCommerce, 'extendForm']
- );
- $this->wp->addFilter(
- 'woocommerce_registration_errors',
- [$this->hooksWooCommerce, 'onRegister'],
- 60,
- 3
- );
- }
- // Manage subscription
- $this->wp->addAction(
- 'admin_post_mailpoet_subscription_update',
- [$this->subscriptionManage, 'onSave']
- );
- $this->wp->addAction(
- 'admin_post_nopriv_mailpoet_subscription_update',
- [$this->subscriptionManage, 'onSave']
- );
- // Subscription form
- $this->wp->addAction(
- 'admin_post_mailpoet_subscription_form',
- [$this->subscriptionForm, 'onSubmit']
- );
- $this->wp->addAction(
- 'admin_post_nopriv_mailpoet_subscription_form',
- [$this->subscriptionForm, 'onSubmit']
- );
- $this->wp->addFilter(
- 'the_content',
- [$this->displayFormInWPContent, 'display']
- );
- }
- public function setupMailer() {
- $this->wp->addAction('plugins_loaded', [
- $this->wordpressMailerReplacer,
- 'replaceWordPressMailer',
- ]);
- $this->wp->addAction('login_init', [
- $this->wordpressMailerReplacer,
- 'replaceWordPressMailer',
- ]);
- $this->wp->addAction('lostpassword_post', [
- $this->wordpressMailerReplacer,
- 'replaceWordPressMailer',
- ]);
- }
- public function setupWooCommerceSubscriptionEvents() {
- $woocommerce = $this->settings->get('woocommerce', []);
- // WooCommerce: subscribe on checkout
- if (!empty($woocommerce['optin_on_checkout']['enabled'])) {
- $this->wp->addAction(
- 'woocommerce_checkout_before_terms_and_conditions',
- [$this->hooksWooCommerce, 'extendWooCommerceCheckoutForm']
- );
- }
- $this->wp->addAction(
- 'woocommerce_checkout_update_order_meta',
- [$this->hooksWooCommerce, 'subscribeOnCheckout'],
- 10, // this should execute after the WC sync call on the same hook
- 2
- );
- }
- public function setupWPUsers() {
- // WP Users synchronization
- $this->wp->addAction(
- 'user_register',
- [$this->wpSegment, 'synchronizeUser'],
- 6
- );
- $this->wp->addAction(
- 'added_existing_user',
- [$this->wpSegment, 'synchronizeUser'],
- 6
- );
- $this->wp->addAction(
- 'profile_update',
- [$this->wpSegment, 'synchronizeUser'],
- 6, 2
- );
- $this->wp->addAction(
- 'delete_user',
- [$this->wpSegment, 'synchronizeUser'],
- 1
- );
- // multisite
- $this->wp->addAction(
- 'deleted_user',
- [$this->wpSegment, 'synchronizeUser'],
- 1
- );
- $this->wp->addAction(
- 'remove_user_from_blog',
- [$this->wpSegment, 'synchronizeUser'],
- 1
- );
- }
- public function setupWooCommerceSettings() {
- $this->wp->addAction('woocommerce_settings_start', [
- $this->hooksWooCommerce,
- 'disableWooCommerceSettings',
- ]);
- }
- public function setupWooCommerceUsers() {
- // WooCommerce Customers synchronization
- $this->wp->addAction(
- 'woocommerce_created_customer',
- [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
- 7
- );
- $this->wp->addAction(
- 'woocommerce_new_customer',
- [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
- 7
- );
- $this->wp->addAction(
- 'woocommerce_update_customer',
- [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
- 7
- );
- $this->wp->addAction(
- 'woocommerce_delete_customer',
- [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
- 7
- );
- $this->wp->addAction(
- 'woocommerce_checkout_update_order_meta',
- [$this->hooksWooCommerce, 'synchronizeGuestCustomer'],
- 7
- );
- $this->wp->addAction(
- 'woocommerce_process_shop_order_meta',
- [$this->hooksWooCommerce, 'synchronizeGuestCustomer'],
- 7
- );
- }
- public function setupWooCommercePurchases() {
- // use both 'processing' and 'completed' states since payment hook and 'processing' status
- // may be skipped with some payment methods (cheque) or when state transitioned manually
- $acceptedOrderStates = WPFunctions::get()->applyFilters(
- 'mailpoet_purchase_order_states',
- ['processing', 'completed']
- );
- foreach ($acceptedOrderStates as $status) {
- WPFunctions::get()->addAction(
- 'woocommerce_order_status_' . $status,
- [$this->hooksWooCommerce, 'trackPurchase'],
- 10,
- 1
- );
- }
- }
- public function setupWooCommerceSubscriberEngagement() {
- $this->wp->addAction(
- 'woocommerce_new_order',
- [$this->hooksWooCommerce, 'updateSubscriberEngagement'],
- 7
- );
- }
- public function setupImageSize() {
- $this->wp->addFilter(
- 'image_size_names_choose',
- [$this, 'appendImageSize'],
- 10, 1
- );
- }
- public function appendImageSize($sizes) {
- return array_merge($sizes, [
- 'mailpoet_newsletter_max' => WPFunctions::get()->__('MailPoet Newsletter', 'mailpoet'),
- ]);
- }
- public function setupListing() {
- $this->wp->addFilter(
- 'set-screen-option',
- [$this, 'setScreenOption'],
- 10, 3
- );
- }
- public function setScreenOption($status, $option, $value) {
- if (preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
- return $value;
- } else {
- return $status;
- }
- }
- public function setupPostNotifications() {
- $this->wp->addAction(
- 'transition_post_status',
- [$this->postNotificationScheduler, 'transitionHook'],
- 10, 3
- );
- }
- public function setupFooter() {
- if (!Menu::isOnMailPoetAdminPage()) {
- return;
- }
- $this->wp->addFilter(
- 'admin_footer_text',
- [$this, 'setFooter'],
- 1, 1
- );
- }
- public function setFooter($text) {
- return '<a href="https://feedback.mailpoet.com/" rel="noopener noreferrer" target="_blank">Give feedback</a>';
- }
- }
|