| @@ -1,14 +1,14 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName) |
| 2 | 2 | /** |
| 3 | 3 | * Module Name: Newsletter |
| 4 | - * Module Description: Let visitors subscribe to new posts and comments via email | |
| 4 | + * Module Description: Grow your subscriber list and deliver your content directly to their email inbox. | |
| 5 | 5 | * Sort Order: 9 |
| 6 | 6 | * Recommendation Order: 8 |
| 7 | 7 | * First Introduced: 1.2 |
| 8 | 8 | * Requires Connection: Yes |
| 9 | 9 | * Requires User Connection: Yes |
| 10 | - * Auto Activate: No | |
| 10 | + * Auto Activate: Yes | |
| 11 | 11 | * Module Tags: Social |
| 12 | 12 | * Feature: Engagement |
| 13 | 13 | * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup, newsletter, creator |
| 14 | 14 | */ |
| @@ -17,12 +17,17 @@ | ||
| 17 | 17 | |
| 18 | 18 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 19 | 19 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 20 | 20 | use Automattic\Jetpack\Connection\XMLRPC_Async_Call; |
| 21 | +use Automattic\Jetpack\Newsletter\Settings as Newsletter_Settings; | |
| 21 | 22 | use Automattic\Jetpack\Redirect; |
| 22 | 23 | use Automattic\Jetpack\Status; |
| 23 | 24 | use Automattic\Jetpack\Status\Host; |
| 24 | 25 | |
| 26 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 27 | + exit( 0 ); | |
| 28 | +} | |
| 29 | + | |
| 25 | 30 | add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' ); |
| 26 | 31 | |
| 27 | 32 | // Loads the User Content Link Redirection feature. |
| 28 | 33 | require_once __DIR__ . '/subscriptions/jetpack-user-content-link-redirection.php'; |
| @@ -58,8 +63,10 @@ | ||
| 58 | 63 | } |
| 59 | 64 | |
| 60 | 65 | /** |
| 61 | 66 | * Main class file for the Subscriptions module. |
| 67 | + * | |
| 68 | + * @phan-constructor-used-for-side-effects | |
| 62 | 69 | */ |
| 63 | 70 | class Jetpack_Subscriptions { |
| 64 | 71 | /** |
| 65 | 72 | * Whether Jetpack has been instantiated or not. |
| @@ -128,8 +135,10 @@ | ||
| 128 | 135 | add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 18, 1 ); |
| 129 | 136 | |
| 130 | 137 | // Set "social_notifications_subscribe" option during the first-time activation. |
| 131 | 138 | add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_social_notifications_subscribe' ) ); |
| 139 | + add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_featured_image_in_email_default' ) ); | |
| 140 | + add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_newsletter_send_default' ) ); | |
| 132 | 141 | |
| 133 | 142 | // Hide subscription messaging in Publish panel for posts that were published in the past |
| 134 | 143 | add_action( 'init', array( $this, 'register_post_meta' ), 20 ); |
| 135 | 144 | add_action( 'transition_post_status', array( $this, 'maybe_set_first_published_status' ), 10, 3 ); |
| @@ -146,8 +155,11 @@ | ||
| 146 | 155 | ); |
| 147 | 156 | |
| 148 | 157 | // Track categories created through the category editor page |
| 149 | 158 | add_action( 'wp_ajax_add-tag', array( $this, 'track_newsletter_category_creation' ), 1 ); |
| 159 | + | |
| 160 | + $newsletter_settings = new Newsletter_Settings(); | |
| 161 | + $newsletter_settings::init(); | |
| 150 | 162 | } |
| 151 | 163 | |
| 152 | 164 | /** |
| 153 | 165 | * Jetpack_Subscriptions::xmlrpc_methods() |
| @@ -555,8 +567,9 @@ | ||
| 555 | 567 | |
| 556 | 568 | if ( $async ) { |
| 557 | 569 | XMLRPC_Async_Call::add_call( 'jetpack.subscribeToSite', 0, $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 558 | 570 | } else { |
| 571 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false | |
| 559 | 572 | $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 560 | 573 | } |
| 561 | 574 | } |
| 562 | 575 | |
| @@ -564,19 +577,24 @@ | ||
| 564 | 577 | return; |
| 565 | 578 | } |
| 566 | 579 | |
| 567 | 580 | // Call. |
| 581 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false, otherwise we return early | |
| 568 | 582 | $xml->query(); |
| 569 | 583 | |
| 584 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false, otherwise we return early | |
| 570 | 585 | if ( $xml->isError() ) { |
| 586 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false, otherwise we return early | |
| 571 | 587 | return $xml->get_jetpack_error(); |
| 572 | 588 | } |
| 573 | 589 | |
| 590 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false | |
| 574 | 591 | $responses = $xml->getResponse(); |
| 575 | 592 | |
| 576 | 593 | $r = array(); |
| 577 | 594 | foreach ( (array) $responses as $response ) { |
| 578 | 595 | if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) { |
| 596 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $xml is set when $async is false | |
| 579 | 597 | $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] ); |
| 580 | 598 | continue; |
| 581 | 599 | } |
| 582 | 600 | |
| @@ -694,9 +712,9 @@ | ||
| 694 | 712 | */ |
| 695 | 713 | do_action( 'jetpack_subscriptions_form_submission', $result ); |
| 696 | 714 | |
| 697 | 715 | wp_safe_redirect( "$redirect#$redirect_fragment" ); |
| 698 | - exit; | |
| 716 | + exit( 0 ); | |
| 699 | 717 | } |
| 700 | 718 | |
| 701 | 719 | /** |
| 702 | 720 | * Jetpack_Subscriptions::comment_subscribe_init() |
| @@ -792,8 +810,19 @@ | ||
| 792 | 810 | * @param int|string $comment_id Comment thread being subscribed to. |
| 793 | 811 | * @param string $approved Comment status. |
| 794 | 812 | */ |
| 795 | 813 | public function comment_subscribe_submit( $comment_id, $approved ) { |
| 814 | + /** | |
| 815 | + * Filters whether to skip comment subscription processing. | |
| 816 | + * | |
| 817 | + * @since 15.5 | |
| 818 | + * | |
| 819 | + * @param bool $skip Whether to skip comment subscription. Default false. | |
| 820 | + */ | |
| 821 | + if ( apply_filters( 'jetpack_subscription_comment_subscribe_skip', false ) ) { | |
| 822 | + return; | |
| 823 | + } | |
| 824 | + | |
| 796 | 825 | if ( 'spam' === $approved ) { |
| 797 | 826 | return; |
| 798 | 827 | } |
| 799 | 828 | |
| @@ -856,9 +885,9 @@ | ||
| 856 | 885 | public function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) { |
| 857 | 886 | $post_id = (int) $post_id; |
| 858 | 887 | |
| 859 | 888 | /** This filter is already documented in core/wp-includes/comment-functions.php */ |
| 860 | - $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); | |
| 889 | + $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', YEAR_IN_SECONDS ); | |
| 861 | 890 | |
| 862 | 891 | /** |
| 863 | 892 | * Filter the Jetpack Comment cookie path. |
| 864 | 893 | * |
| @@ -881,15 +910,15 @@ | ||
| 881 | 910 | */ |
| 882 | 911 | $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN ); |
| 883 | 912 | |
| 884 | 913 | if ( $subscribe_to_post && $post_id >= 0 ) { |
| 885 | - setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true ); | |
| 914 | + setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '1', time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true ); | |
| 886 | 915 | } else { |
| 887 | 916 | setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true ); |
| 888 | 917 | } |
| 889 | 918 | |
| 890 | 919 | if ( $subscribe_to_blog ) { |
| 891 | - setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true ); | |
| 920 | + setcookie( 'jetpack_blog_subscribe_' . self::$hash, '1', time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true ); | |
| 892 | 921 | } else { |
| 893 | 922 | setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true ); |
| 894 | 923 | } |
| 895 | 924 | } |
| @@ -907,8 +936,26 @@ | ||
| 907 | 936 | } |
| 908 | 937 | } |
| 909 | 938 | |
| 910 | 939 | /** |
| 940 | + * Set the featured image in email option to `1` when the Subscriptions module is activated in the first time. | |
| 941 | + * | |
| 942 | + * @return void | |
| 943 | + */ | |
| 944 | + public function set_featured_image_in_email_default() { | |
| 945 | + add_option( 'wpcom_featured_image_in_email', 1 ); | |
| 946 | + } | |
| 947 | + | |
| 948 | + /** | |
| 949 | + * Set the email post to subscribers default option to `1` when the Subscriptions module is activated for the first time. | |
| 950 | + * | |
| 951 | + * @return void | |
| 952 | + */ | |
| 953 | + public function set_newsletter_send_default() { | |
| 954 | + add_option( 'wpcom_newsletter_send_default', 1 ); | |
| 955 | + } | |
| 956 | + | |
| 957 | + /** | |
| 911 | 958 | * Save a flag when a post was ever published. |
| 912 | 959 | * |
| 913 | 960 | * It saves the post meta when the post was published and becomes a draft. |
| 914 | 961 | * Then this meta is used to hide subscription messaging in Publish panel. |
| @@ -917,8 +964,13 @@ | ||
| 917 | 964 | * @param string $old_status The "old" post status of the transition when saved. |
| 918 | 965 | * @param object $post obj The post object. |
| 919 | 966 | */ |
| 920 | 967 | public function maybe_set_first_published_status( $new_status, $old_status, $post ) { |
| 968 | + // Subscriptions are only available for posts so far. | |
| 969 | + if ( ! $post instanceof \WP_Post || 'post' !== $post->post_type ) { | |
| 970 | + return; | |
| 971 | + } | |
| 972 | + | |
| 921 | 973 | $was_post_ever_published = get_post_meta( $post->ID, '_jetpack_post_was_ever_published', true ); |
| 922 | 974 | if ( ! $was_post_ever_published && 'publish' === $old_status && 'draft' === $new_status ) { |
| 923 | 975 | update_post_meta( $post->ID, '_jetpack_post_was_ever_published', true ); |
| 924 | 976 | } |
| @@ -924,23 +976,23 @@ | ||
| 924 | 976 | } |
| 925 | 977 | } |
| 926 | 978 | |
| 927 | 979 | /** |
| 928 | - * Checks if the current user can publish posts. | |
| 980 | + * Checks if the current user can edit posts. | |
| 929 | 981 | * |
| 930 | 982 | * @return bool |
| 931 | 983 | */ |
| 932 | 984 | public function first_published_status_meta_auth_callback() { |
| 933 | 985 | /** |
| 934 | - * Filter the capability to view if a post was ever published in the Subscription Module. | |
| 986 | + * Filter the capability required to edit the "was ever published" post meta. | |
| 935 | 987 | * |
| 936 | 988 | * @module subscriptions |
| 937 | 989 | * |
| 938 | 990 | * @since 13.4 |
| 939 | 991 | * |
| 940 | - * @param string $capability User capability needed to view if a post was ever published. Default to publish_posts. | |
| 992 | + * @param string $capability User capability needed to edit the "was ever published" meta. Default to edit_posts. | |
| 941 | 993 | */ |
| 942 | - $capability = apply_filters( 'jetpack_subscriptions_post_was_ever_published_capability', 'publish_posts' ); | |
| 994 | + $capability = apply_filters( 'jetpack_subscriptions_post_was_ever_published_capability', 'edit_posts' ); | |
| 943 | 995 | if ( current_user_can( $capability ) ) { |
| 944 | 996 | return true; |
| 945 | 997 | } |
| 946 | 998 | return false; |
| @@ -950,16 +1002,17 @@ | ||
| 950 | 1002 | * Registers the 'post_was_ever_published' post meta for use in the REST API. |
| 951 | 1003 | */ |
| 952 | 1004 | public function register_post_meta() { |
| 953 | 1005 | $jetpack_post_was_ever_published = array( |
| 954 | - 'type' => 'boolean', | |
| 955 | - 'description' => __( 'Whether the post was ever published.', 'jetpack' ), | |
| 956 | - 'single' => true, | |
| 957 | - 'default' => false, | |
| 958 | - 'show_in_rest' => array( | |
| 1006 | + 'type' => 'boolean', | |
| 1007 | + 'description' => __( 'Whether the post was ever published.', 'jetpack' ), | |
| 1008 | + 'single' => true, | |
| 1009 | + 'default' => false, | |
| 1010 | + 'show_in_rest' => array( | |
| 959 | 1011 | 'name' => 'jetpack_post_was_ever_published', |
| 960 | 1012 | ), |
| 961 | - 'auth_callback' => array( $this, 'first_published_status_meta_auth_callback' ), | |
| 1013 | + 'auth_callback' => array( $this, 'first_published_status_meta_auth_callback' ), | |
| 1014 | + 'object_subtype' => 'post', // Subscriptions are only for the post post type so far, so we can limit this meta to posts only. | |
| 962 | 1015 | ); |
| 963 | 1016 | |
| 964 | 1017 | register_meta( 'post', '_jetpack_post_was_ever_published', $jetpack_post_was_ever_published ); |
| 965 | 1018 | } |
| @@ -968,13 +1021,24 @@ | ||
| 968 | 1021 | * Create a Subscribers menu displayed on self-hosted sites. |
| 969 | 1022 | * |
| 970 | 1023 | * - It is not displayed on WordPress.com sites. |
| 971 | 1024 | * - It directs you to Calypso to the existing Subscribers page. |
| 1025 | + * - It is retired once the Newsletter modernization filter is on, since the | |
| 1026 | + * unified Newsletter page then owns the Subscribers tab. | |
| 972 | 1027 | * |
| 973 | 1028 | * @return void |
| 974 | 1029 | */ |
| 975 | 1030 | public function add_subscribers_menu() { |
| 976 | 1031 | /* |
| 1032 | + * Referenced as a string literal (mirrors Newsletter\Settings::MODERNIZATION_FILTER) | |
| 1033 | + * to keep this bootstrap path safe if the packaged Newsletter Settings class does | |
| 1034 | + * not expose the constant yet. | |
| 1035 | + */ | |
| 1036 | + if ( apply_filters( 'rsm_jetpack_ui_modernization_newsletter', true ) ) { | |
| 1037 | + return; | |
| 1038 | + } | |
| 1039 | + | |
| 1040 | + /* | |
| 977 | 1041 | * Do not display any menu on WoA and WordPress.com Simple sites (unless Classic wp-admin is enabled). |
| 978 | 1042 | * They already get a menu item under Users via nav-unification. |
| 979 | 1043 | */ |
| 980 | 1044 | if ( ( new Host() )->is_wpcom_platform() && get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { |
| @@ -993,8 +1057,20 @@ | ||
| 993 | 1057 | ) { |
| 994 | 1058 | return; |
| 995 | 1059 | } |
| 996 | 1060 | |
| 1061 | + /** | |
| 1062 | + * Enables the new in development subscribers in wp-admin dashboard. | |
| 1063 | + * | |
| 1064 | + * @since 9.5.0 | |
| 1065 | + * | |
| 1066 | + * @param bool If the new dashboard is enabled. Defaults on for every site; hosts | |
| 1067 | + * can opt out with this filter. | |
| 1068 | + */ | |
| 1069 | + if ( apply_filters( 'jetpack_wp_admin_subscriber_management_enabled', true ) ) { | |
| 1070 | + return; | |
| 1071 | + } | |
| 1072 | + | |
| 997 | 1073 | $blog_id = Connection_Manager::get_site_id( true ); |
| 998 | 1074 | |
| 999 | 1075 | $link = Redirect::get_url( |
| 1000 | 1076 | 'jetpack-menu-jetpack-manage-subscribers', |
| @@ -1002,13 +1078,13 @@ | ||
| 1002 | 1078 | ); |
| 1003 | 1079 | |
| 1004 | 1080 | Admin_Menu::add_menu( |
| 1005 | 1081 | __( 'Subscribers', 'jetpack' ), |
| 1006 | - __( 'Subscribers', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>', | |
| 1082 | + __( 'Subscribers', 'jetpack' ) . ' <span aria-hidden="true">↗</span>', | |
| 1007 | 1083 | 'manage_options', |
| 1008 | 1084 | esc_url( $link ), |
| 1009 | 1085 | null, |
| 1010 | - 11 | |
| 1086 | + 100 | |
| 1011 | 1087 | ); |
| 1012 | 1088 | } |
| 1013 | 1089 | |
| 1014 | 1090 | /** |
| @@ -1046,4 +1122,9 @@ | ||
| 1046 | 1122 | |
| 1047 | 1123 | require __DIR__ . '/subscriptions/views.php'; |
| 1048 | 1124 | require __DIR__ . '/subscriptions/subscribe-modal/class-jetpack-subscribe-modal.php'; |
| 1049 | 1125 | require __DIR__ . '/subscriptions/subscribe-overlay/class-jetpack-subscribe-overlay.php'; |
| 1126 | +require __DIR__ . '/subscriptions/subscribe-floating-button/class-jetpack-subscribe-floating-button.php'; | |
| 1127 | +require __DIR__ . '/subscriptions/newsletter-widget/class-jetpack-newsletter-dashboard-widget.php'; | |
| 1128 | + | |
| 1129 | +require_once __DIR__ . '/subscriptions/abilities/class-newsletter-abilities.php'; | |
| 1130 | +\Automattic\Jetpack\Plugin\Abilities\Newsletter_Abilities::init(); | |