Sin descripción

Pages.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace MailPoet\Settings;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Subscription;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. class Pages {
  7. public function __construct() {
  8. }
  9. public function init() {
  10. WPFunctions::get()->registerPostType('mailpoet_page', [
  11. 'labels' => [
  12. 'name' => WPFunctions::get()->__('MailPoet Page', 'mailpoet'),
  13. 'singular_name' => WPFunctions::get()->__('MailPoet Page', 'mailpoet'),
  14. ],
  15. 'public' => true,
  16. 'has_archive' => false,
  17. 'show_ui' => false,
  18. 'show_in_menu' => false,
  19. 'rewrite' => false,
  20. 'show_in_nav_menus' => false,
  21. 'can_export' => false,
  22. 'publicly_queryable' => true,
  23. 'exclude_from_search' => true,
  24. ]);
  25. }
  26. public static function createMailPoetPage() {
  27. WPFunctions::get()->removeAllActions('pre_post_update');
  28. WPFunctions::get()->removeAllActions('save_post');
  29. WPFunctions::get()->removeAllActions('wp_insert_post');
  30. $id = WPFunctions::get()->wpInsertPost([
  31. 'post_status' => 'publish',
  32. 'post_type' => 'mailpoet_page',
  33. 'post_author' => 1,
  34. 'post_content' => '[mailpoet_page]',
  35. 'post_title' => WPFunctions::get()->__('MailPoet Page', 'mailpoet'),
  36. 'post_name' => 'subscriptions',
  37. ]);
  38. return ((int)$id > 0) ? (int)$id : false;
  39. }
  40. public static function getDefaultMailPoetPage() {
  41. $wp = WPFunctions::get();
  42. $pages = $wp->getPosts([
  43. 'posts_per_page' => 1,
  44. 'orderby' => 'date',
  45. 'order' => 'DESC',
  46. 'post_type' => 'mailpoet_page',
  47. ]);
  48. $page = null;
  49. if (!empty($pages)) {
  50. $page = array_shift($pages);
  51. if (strpos($page->post_content, '[mailpoet_page]') === false) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  52. $page = null;
  53. }
  54. }
  55. return $page;
  56. }
  57. public static function getMailPoetPages() {
  58. return WPFunctions::get()->getPosts([
  59. 'post_type' => 'mailpoet_page',
  60. ]);
  61. }
  62. /**
  63. * @param int $id
  64. *
  65. * @return bool
  66. */
  67. public static function isMailpoetPage($id) {
  68. $mailpoetPages = static::getMailPoetPages();
  69. foreach ($mailpoetPages as $mailpoetPage) {
  70. if ($mailpoetPage->ID === $id) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. public static function getAll() {
  77. $allPages = array_merge(
  78. static::getMailPoetPages(),
  79. WPFunctions::get()->getPages()
  80. );
  81. $pages = [];
  82. foreach ($allPages as $page) {
  83. $pages[] = static::getPageData($page);
  84. }
  85. return $pages;
  86. }
  87. public static function getPageData($page) {
  88. $subscriptionUrlFactory = Subscription\SubscriptionUrlFactory::getInstance();
  89. return [
  90. 'id' => $page->ID,
  91. 'title' => $page->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  92. 'url' => [
  93. 'unsubscribe' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'unsubscribe'),
  94. 'manage' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'manage'),
  95. 'confirm' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'confirm'),
  96. 'confirm_unsubscribe' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'confirm_unsubscribe'),
  97. ],
  98. ];
  99. }
  100. }