Нет описания

Hooks.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Form\DisplayFormInWPContent;
  5. use MailPoet\Mailer\WordPress\WordpressMailerReplacer;
  6. use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
  7. use MailPoet\Segments\WP;
  8. use MailPoet\Settings\SettingsController;
  9. use MailPoet\Subscription\Comment;
  10. use MailPoet\Subscription\Form;
  11. use MailPoet\Subscription\Manage;
  12. use MailPoet\Subscription\Registration;
  13. use MailPoet\WP\Functions as WPFunctions;
  14. class Hooks {
  15. /** @var Form */
  16. private $subscriptionForm;
  17. /** @var Comment */
  18. private $subscriptionComment;
  19. /** @var Manage */
  20. private $subscriptionManage;
  21. /** @var Registration */
  22. private $subscriptionRegistration;
  23. /** @var SettingsController */
  24. private $settings;
  25. /** @var WPFunctions */
  26. private $wp;
  27. /** @var PostNotificationScheduler */
  28. private $postNotificationScheduler;
  29. /** @var WordpressMailerReplacer */
  30. private $wordpressMailerReplacer;
  31. /** @var DisplayFormInWPContent */
  32. private $displayFormInWPContent;
  33. /** @var WP */
  34. private $wpSegment;
  35. /** @var HooksWooCommerce */
  36. private $hooksWooCommerce;
  37. public function __construct(
  38. Form $subscriptionForm,
  39. Comment $subscriptionComment,
  40. Manage $subscriptionManage,
  41. Registration $subscriptionRegistration,
  42. SettingsController $settings,
  43. WPFunctions $wp,
  44. PostNotificationScheduler $postNotificationScheduler,
  45. WordpressMailerReplacer $wordpressMailerReplacer,
  46. DisplayFormInWPContent $displayFormInWPContent,
  47. HooksWooCommerce $hooksWooCommerce,
  48. WP $wpSegment
  49. ) {
  50. $this->subscriptionForm = $subscriptionForm;
  51. $this->subscriptionComment = $subscriptionComment;
  52. $this->subscriptionManage = $subscriptionManage;
  53. $this->subscriptionRegistration = $subscriptionRegistration;
  54. $this->settings = $settings;
  55. $this->wp = $wp;
  56. $this->postNotificationScheduler = $postNotificationScheduler;
  57. $this->wordpressMailerReplacer = $wordpressMailerReplacer;
  58. $this->displayFormInWPContent = $displayFormInWPContent;
  59. $this->wpSegment = $wpSegment;
  60. $this->hooksWooCommerce = $hooksWooCommerce;
  61. }
  62. public function init() {
  63. $this->setupWPUsers();
  64. $this->setupWooCommerceUsers();
  65. $this->setupWooCommercePurchases();
  66. $this->setupWooCommerceSubscriberEngagement();
  67. $this->setupImageSize();
  68. $this->setupListing();
  69. $this->setupSubscriptionEvents();
  70. $this->setupWooCommerceSubscriptionEvents();
  71. $this->setupPostNotifications();
  72. $this->setupWooCommerceSettings();
  73. $this->setupFooter();
  74. }
  75. public function initEarlyHooks() {
  76. $this->setupMailer();
  77. }
  78. public function setupSubscriptionEvents() {
  79. $subscribe = $this->settings->get('subscribe', []);
  80. // Subscribe in comments
  81. if (
  82. isset($subscribe['on_comment']['enabled'])
  83. &&
  84. (bool)$subscribe['on_comment']['enabled']
  85. ) {
  86. if ($this->wp->isUserLoggedIn()) {
  87. $this->wp->addAction(
  88. 'comment_form_field_comment',
  89. [$this->subscriptionComment, 'extendLoggedInForm']
  90. );
  91. } else {
  92. $this->wp->addAction(
  93. 'comment_form_after_fields',
  94. [$this->subscriptionComment, 'extendLoggedOutForm']
  95. );
  96. }
  97. $this->wp->addAction(
  98. 'comment_post',
  99. [$this->subscriptionComment, 'onSubmit'],
  100. 60,
  101. 2
  102. );
  103. $this->wp->addAction(
  104. 'wp_set_comment_status',
  105. [$this->subscriptionComment, 'onStatusUpdate'],
  106. 60,
  107. 2
  108. );
  109. }
  110. // Subscribe in registration form
  111. if (
  112. isset($subscribe['on_register']['enabled'])
  113. &&
  114. (bool)$subscribe['on_register']['enabled']
  115. ) {
  116. if (is_multisite()) {
  117. $this->wp->addAction(
  118. 'signup_extra_fields',
  119. [$this->subscriptionRegistration, 'extendForm']
  120. );
  121. $this->wp->addAction(
  122. 'wpmu_validate_user_signup',
  123. [$this->subscriptionRegistration, 'onMultiSiteRegister'],
  124. 60,
  125. 1
  126. );
  127. } else {
  128. $this->wp->addAction(
  129. 'register_form',
  130. [$this->subscriptionRegistration, 'extendForm']
  131. );
  132. // we need to process new users while they are registered.
  133. // We used `register_post` before but that is too soon
  134. // because if registration fails during `registration_errors` we will keep the user as subscriber.
  135. // So we are hooking to `registration_error` with a low priority.
  136. $this->wp->addFilter(
  137. 'registration_errors',
  138. [$this->subscriptionRegistration, 'onRegister'],
  139. 60,
  140. 3
  141. );
  142. }
  143. $this->wp->addAction(
  144. 'woocommerce_register_form',
  145. [$this->hooksWooCommerce, 'extendForm']
  146. );
  147. $this->wp->addFilter(
  148. 'woocommerce_registration_errors',
  149. [$this->hooksWooCommerce, 'onRegister'],
  150. 60,
  151. 3
  152. );
  153. }
  154. // Manage subscription
  155. $this->wp->addAction(
  156. 'admin_post_mailpoet_subscription_update',
  157. [$this->subscriptionManage, 'onSave']
  158. );
  159. $this->wp->addAction(
  160. 'admin_post_nopriv_mailpoet_subscription_update',
  161. [$this->subscriptionManage, 'onSave']
  162. );
  163. // Subscription form
  164. $this->wp->addAction(
  165. 'admin_post_mailpoet_subscription_form',
  166. [$this->subscriptionForm, 'onSubmit']
  167. );
  168. $this->wp->addAction(
  169. 'admin_post_nopriv_mailpoet_subscription_form',
  170. [$this->subscriptionForm, 'onSubmit']
  171. );
  172. $this->wp->addFilter(
  173. 'the_content',
  174. [$this->displayFormInWPContent, 'display']
  175. );
  176. }
  177. public function setupMailer() {
  178. $this->wp->addAction('plugins_loaded', [
  179. $this->wordpressMailerReplacer,
  180. 'replaceWordPressMailer',
  181. ]);
  182. $this->wp->addAction('login_init', [
  183. $this->wordpressMailerReplacer,
  184. 'replaceWordPressMailer',
  185. ]);
  186. $this->wp->addAction('lostpassword_post', [
  187. $this->wordpressMailerReplacer,
  188. 'replaceWordPressMailer',
  189. ]);
  190. }
  191. public function setupWooCommerceSubscriptionEvents() {
  192. $woocommerce = $this->settings->get('woocommerce', []);
  193. // WooCommerce: subscribe on checkout
  194. if (!empty($woocommerce['optin_on_checkout']['enabled'])) {
  195. $this->wp->addAction(
  196. 'woocommerce_checkout_before_terms_and_conditions',
  197. [$this->hooksWooCommerce, 'extendWooCommerceCheckoutForm']
  198. );
  199. }
  200. $this->wp->addAction(
  201. 'woocommerce_checkout_update_order_meta',
  202. [$this->hooksWooCommerce, 'subscribeOnCheckout'],
  203. 10, // this should execute after the WC sync call on the same hook
  204. 2
  205. );
  206. }
  207. public function setupWPUsers() {
  208. // WP Users synchronization
  209. $this->wp->addAction(
  210. 'user_register',
  211. [$this->wpSegment, 'synchronizeUser'],
  212. 6
  213. );
  214. $this->wp->addAction(
  215. 'added_existing_user',
  216. [$this->wpSegment, 'synchronizeUser'],
  217. 6
  218. );
  219. $this->wp->addAction(
  220. 'profile_update',
  221. [$this->wpSegment, 'synchronizeUser'],
  222. 6, 2
  223. );
  224. $this->wp->addAction(
  225. 'delete_user',
  226. [$this->wpSegment, 'synchronizeUser'],
  227. 1
  228. );
  229. // multisite
  230. $this->wp->addAction(
  231. 'deleted_user',
  232. [$this->wpSegment, 'synchronizeUser'],
  233. 1
  234. );
  235. $this->wp->addAction(
  236. 'remove_user_from_blog',
  237. [$this->wpSegment, 'synchronizeUser'],
  238. 1
  239. );
  240. }
  241. public function setupWooCommerceSettings() {
  242. $this->wp->addAction('woocommerce_settings_start', [
  243. $this->hooksWooCommerce,
  244. 'disableWooCommerceSettings',
  245. ]);
  246. }
  247. public function setupWooCommerceUsers() {
  248. // WooCommerce Customers synchronization
  249. $this->wp->addAction(
  250. 'woocommerce_created_customer',
  251. [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
  252. 7
  253. );
  254. $this->wp->addAction(
  255. 'woocommerce_new_customer',
  256. [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
  257. 7
  258. );
  259. $this->wp->addAction(
  260. 'woocommerce_update_customer',
  261. [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
  262. 7
  263. );
  264. $this->wp->addAction(
  265. 'woocommerce_delete_customer',
  266. [$this->hooksWooCommerce, 'synchronizeRegisteredCustomer'],
  267. 7
  268. );
  269. $this->wp->addAction(
  270. 'woocommerce_checkout_update_order_meta',
  271. [$this->hooksWooCommerce, 'synchronizeGuestCustomer'],
  272. 7
  273. );
  274. $this->wp->addAction(
  275. 'woocommerce_process_shop_order_meta',
  276. [$this->hooksWooCommerce, 'synchronizeGuestCustomer'],
  277. 7
  278. );
  279. }
  280. public function setupWooCommercePurchases() {
  281. // use both 'processing' and 'completed' states since payment hook and 'processing' status
  282. // may be skipped with some payment methods (cheque) or when state transitioned manually
  283. $acceptedOrderStates = WPFunctions::get()->applyFilters(
  284. 'mailpoet_purchase_order_states',
  285. ['processing', 'completed']
  286. );
  287. foreach ($acceptedOrderStates as $status) {
  288. WPFunctions::get()->addAction(
  289. 'woocommerce_order_status_' . $status,
  290. [$this->hooksWooCommerce, 'trackPurchase'],
  291. 10,
  292. 1
  293. );
  294. }
  295. }
  296. public function setupWooCommerceSubscriberEngagement() {
  297. $this->wp->addAction(
  298. 'woocommerce_new_order',
  299. [$this->hooksWooCommerce, 'updateSubscriberEngagement'],
  300. 7
  301. );
  302. }
  303. public function setupImageSize() {
  304. $this->wp->addFilter(
  305. 'image_size_names_choose',
  306. [$this, 'appendImageSize'],
  307. 10, 1
  308. );
  309. }
  310. public function appendImageSize($sizes) {
  311. return array_merge($sizes, [
  312. 'mailpoet_newsletter_max' => WPFunctions::get()->__('MailPoet Newsletter', 'mailpoet'),
  313. ]);
  314. }
  315. public function setupListing() {
  316. $this->wp->addFilter(
  317. 'set-screen-option',
  318. [$this, 'setScreenOption'],
  319. 10, 3
  320. );
  321. }
  322. public function setScreenOption($status, $option, $value) {
  323. if (preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
  324. return $value;
  325. } else {
  326. return $status;
  327. }
  328. }
  329. public function setupPostNotifications() {
  330. $this->wp->addAction(
  331. 'transition_post_status',
  332. [$this->postNotificationScheduler, 'transitionHook'],
  333. 10, 3
  334. );
  335. }
  336. public function setupFooter() {
  337. if (!Menu::isOnMailPoetAdminPage()) {
  338. return;
  339. }
  340. $this->wp->addFilter(
  341. 'admin_footer_text',
  342. [$this, 'setFooter'],
  343. 1, 1
  344. );
  345. }
  346. public function setFooter($text) {
  347. return '<a href="https://feedback.mailpoet.com/" rel="noopener noreferrer" target="_blank">Give feedback</a>';
  348. }
  349. }