Charsets.php
3 years ago
Hosts.php
2 months ago
Pages.php
9 months ago
SettingsChangeHandler.php
2 weeks ago
SettingsController.php
4 days ago
SettingsRepository.php
1 year ago
TrackingConfig.php
5 months ago
UserFlagsController.php
1 year ago
UserFlagsRepository.php
3 years ago
index.php
3 years ago
Pages.php
138 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Settings; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Subscription; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class Pages { |
| 12 | const PAGE_SUBSCRIPTIONS = 'subscriptions'; |
| 13 | const PAGE_CAPTCHA = 'captcha'; |
| 14 | const PAGE_TITLE = 'MailPoet Page'; |
| 15 | |
| 16 | public function __construct() { |
| 17 | } |
| 18 | |
| 19 | public function init() { |
| 20 | WPFunctions::get()->registerPostType('mailpoet_page', [ |
| 21 | 'labels' => [ |
| 22 | 'name' => self::PAGE_TITLE, |
| 23 | 'singular_name' => self::PAGE_TITLE, |
| 24 | ], |
| 25 | 'public' => true, |
| 26 | 'has_archive' => false, |
| 27 | 'show_ui' => false, |
| 28 | 'show_in_menu' => false, |
| 29 | 'rewrite' => false, |
| 30 | 'show_in_nav_menus' => false, |
| 31 | 'can_export' => false, |
| 32 | 'publicly_queryable' => true, |
| 33 | 'exclude_from_search' => true, |
| 34 | 'capability_type' => 'page', |
| 35 | ]); |
| 36 | |
| 37 | WPFunctions::get()->addFilter('next_post_link', [$this, 'disableNavigationLinks']); |
| 38 | WPFunctions::get()->addFilter('previous_post_link', [$this, 'disableNavigationLinks']); |
| 39 | } |
| 40 | |
| 41 | public function disableNavigationLinks($output) { |
| 42 | if (is_singular('mailpoet_page')) { |
| 43 | return ''; // Return an empty string to remove navigation links |
| 44 | } |
| 45 | return $output; |
| 46 | } |
| 47 | |
| 48 | public static function createMailPoetPage($postName) { |
| 49 | WPFunctions::get()->removeAllActions('pre_post_update'); |
| 50 | WPFunctions::get()->removeAllActions('save_post'); |
| 51 | WPFunctions::get()->removeAllActions('wp_insert_post'); |
| 52 | |
| 53 | $id = WPFunctions::get()->wpInsertPost([ |
| 54 | 'post_status' => 'publish', |
| 55 | 'post_type' => 'mailpoet_page', |
| 56 | 'post_author' => 1, |
| 57 | 'post_content' => '[mailpoet_page]', |
| 58 | 'post_title' => self::PAGE_TITLE, |
| 59 | 'post_name' => $postName, |
| 60 | ]); |
| 61 | |
| 62 | return ((int)$id > 0) ? (int)$id : false; |
| 63 | } |
| 64 | |
| 65 | public static function getMailPoetPage($postName) { |
| 66 | $wp = WPFunctions::get(); |
| 67 | $pages = $wp->getPosts([ |
| 68 | 'posts_per_page' => 1, |
| 69 | 'orderby' => 'date', |
| 70 | 'order' => 'DESC', |
| 71 | 'post_type' => 'mailpoet_page', |
| 72 | 'post_name__in' => [$postName], |
| 73 | ]); |
| 74 | |
| 75 | $page = null; |
| 76 | if (!empty($pages)) { |
| 77 | $page = array_shift($pages); |
| 78 | if (strpos($page->post_content, '[mailpoet_page]') === false) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 79 | $page = null; |
| 80 | } |
| 81 | } |
| 82 | return $page; |
| 83 | } |
| 84 | |
| 85 | public static function getMailPoetPages() { |
| 86 | return WPFunctions::get()->getPosts([ |
| 87 | 'post_type' => 'mailpoet_page', |
| 88 | 'post_name__in' => [self::PAGE_SUBSCRIPTIONS], |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param int $id |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public static function isMailpoetPage($id) { |
| 98 | $mailpoetPages = static::getMailPoetPages(); |
| 99 | foreach ($mailpoetPages as $mailpoetPage) { |
| 100 | if ($mailpoetPage->ID === $id) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | public static function getAll() { |
| 108 | $allPages = array_merge( |
| 109 | static::getMailPoetPages(), |
| 110 | WPFunctions::get()->getPages() |
| 111 | ); |
| 112 | |
| 113 | $pages = []; |
| 114 | foreach ($allPages as $page) { |
| 115 | $pages[] = static::getPageData($page); |
| 116 | } |
| 117 | |
| 118 | return $pages; |
| 119 | } |
| 120 | |
| 121 | public static function getPageData($page) { |
| 122 | $subscriptionUrlFactory = Subscription\SubscriptionUrlFactory::getInstance(); |
| 123 | $captchaUrlFactory = \MailPoet\DI\ContainerWrapper::getInstance()->get(\MailPoet\Captcha\CaptchaUrlFactory::class); |
| 124 | return [ |
| 125 | 'id' => $page->ID, |
| 126 | 'title' => $page->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 127 | 'url' => [ |
| 128 | 'unsubscribe' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'unsubscribe'), |
| 129 | 'manage' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'manage'), |
| 130 | 'confirm' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'confirm'), |
| 131 | 'confirm_unsubscribe' => $subscriptionUrlFactory->getSubscriptionUrl($page, 'confirm_unsubscribe'), |
| 132 | 're_engagement' => $subscriptionUrlFactory->getSubscriptionUrl($page, 're_engagement'), |
| 133 | 'captcha' => $captchaUrlFactory->getCaptchaPreviewUrl($page), |
| 134 | ], |
| 135 | ]; |
| 136 | } |
| 137 | } |
| 138 |