| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Plugin Name: Jetpack Follow Link for o2
|
|---|
| 4 | Description: Easily subscribe to an o2 comment thread without commenting using a "Follow" action link like WordPress.com has
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | class o2_follow {
|
|---|
| 9 |
|
|---|
| 10 | // Use the old p2 meta key, for backwards compatiblity.
|
|---|
| 11 | const user_meta_key = 'jpflfp2_posts_following';
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Construct the plugin
|
|---|
| 15 | */
|
|---|
| 16 | function __construct() {
|
|---|
| 17 | add_action( 'init', array( $this, 'action_init' ) );
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | *
|
|---|
| 22 | */
|
|---|
| 23 | function action_init() {
|
|---|
| 24 |
|
|---|
| 25 | if ( ! current_theme_supports( 'o2' ) || ! class_exists( 'Jetpack_Subscriptions' ) ) {
|
|---|
| 26 | return;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // Only logged in users should be able to subscribe for now because we use the registered email address
|
|---|
| 30 | if ( !is_user_logged_in() )
|
|---|
| 31 | return;
|
|---|
| 32 |
|
|---|
| 33 | add_action( 'o2_post_form_extras', array( $this, 'subscription_o2_post_form' ) );
|
|---|
| 34 |
|
|---|
| 35 | add_action( 'init', array( $this, 'subscribe_o2_register_post_action_states' ) );
|
|---|
| 36 | add_filter( 'o2_filter_post_actions', array( $this, 'subscription_o2_add_comment_subscriber_link' ), 10, 2 );
|
|---|
| 37 |
|
|---|
| 38 | add_filter( 'o2_comment_form_extras', array( $this, 'subscribe_o2_comment_form' ), 10, 2 );
|
|---|
| 39 | add_filter( 'o2_options', array( $this, 'subscribe_add_o2_options' ) );
|
|---|
| 40 | add_filter( 'o2_post_fragment', array( $this, 'subscribe_add_o2_post_fragment' ), 10, 2 );
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | function subscribe_o2_comment_form( $comment_form_extras = '', $post_ID = false ) {
|
|---|
| 44 | if ( !$post_ID ) {
|
|---|
| 45 | global $post;
|
|---|
| 46 | $post_ID = $post->post_ID;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | $output = '<p class="comment-subscription-form">';
|
|---|
| 50 | $output .= '<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;"' . checked( $checked, true, false ) . '/> ';
|
|---|
| 51 | $output .= '<label class="subscribe-label" id="subscribe-label" for="subscribe" style="display: inline;">' . __( 'Notify me of new comments via email.' ) . '</label>';
|
|---|
| 52 | $output .= '</p>';
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | $output .= '<p class="post-subscription-form"><input type="checkbox" name="subscribe_blog" id="subscribe_blog" value="subscribe" style="width: auto;"/> ';
|
|---|
| 56 | $output .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog" style="display: inline;">'. __( 'Notify me of new posts via email.' ) .'</label>';
|
|---|
| 57 | $output .= '</p>';
|
|---|
| 58 |
|
|---|
| 59 | return $comment_form_extras . $output;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | function subscribe_add_o2_options( $options ) {
|
|---|
| 63 | global $wpdb;
|
|---|
| 64 |
|
|---|
| 65 | $subscribed_ids = (array)get_user_meta( wp_get_current_user()->ID, self::user_meta_key, true );
|
|---|
| 66 |
|
|---|
| 67 | $options['options']['followingBlog'] = false;
|
|---|
| 68 | $options['options']['followingAllComments'] = false;
|
|---|
| 69 | return $options;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | function subscribe_add_o2_post_fragment( $fragment = array(), $post_ID = false ) {
|
|---|
| 73 | global $wpdb;
|
|---|
| 74 | if ( !$post_ID ) {
|
|---|
| 75 | global $post;
|
|---|
| 76 | $post_ID = $post->post_ID;
|
|---|
| 77 | }
|
|---|
| 78 | if ( ! array_key_exists( 'postMeta', $fragment ) )
|
|---|
| 79 | $fragment['postMeta'] = array();
|
|---|
| 80 |
|
|---|
| 81 | $subscribed_ids = (array)get_user_meta( wp_get_current_user()->ID, self::user_meta_key, true );
|
|---|
| 82 | $fragment['postMeta']['isFollowing'] = in_array( get_the_ID(), $subscribed_ids );
|
|---|
| 83 | return $fragment;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * Add 'subscribe' checkbox to o2 post form
|
|---|
| 88 | *
|
|---|
| 89 | * @param string
|
|---|
| 90 | * @return void
|
|---|
| 91 | **/
|
|---|
| 92 | function subscription_o2_post_form( $post_form_extras = '' ) {
|
|---|
| 93 | $output = '';
|
|---|
| 94 | $label = __( 'Notify me of new comments via email.' );
|
|---|
| 95 | $output .= '<p style="margin-top: 1.5em;" class="comment-subscription-form"><input type="checkbox" name="post_subscribe" id="post_subscribe" value="post_subscribe" style="margin-left: .5em;"/>';
|
|---|
| 96 | $output .= '<label style="font-size: 1.2em; margin-bottom: .5em;" id="post_subscribe_label" for="post_subscribe"><small>' . $label . '</small></label>';
|
|---|
| 97 | $output .= '</p>';
|
|---|
| 98 |
|
|---|
| 99 | return $post_form_extras . $output;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | function subscribe_o2_register_post_action_states() {
|
|---|
| 103 | echo 1;
|
|---|
| 104 | if ( ! function_exists( 'o2_register_post_action_states' ) ) {
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 | echo 2;
|
|---|
| 108 | o2_register_post_action_states( 'follow',
|
|---|
| 109 | array(
|
|---|
| 110 | 'normal' => array(
|
|---|
| 111 | 'shortText' => __( 'Follow', 'o2' ),
|
|---|
| 112 | 'title' => __( 'Follow comments', 'o2' ),
|
|---|
| 113 | 'classes' => array(),
|
|---|
| 114 | 'genericon' => 'genericon-subscribe',
|
|---|
| 115 | 'nextState' => 'subscribed'
|
|---|
| 116 | ),
|
|---|
| 117 | 'subscribed' => array(
|
|---|
| 118 | 'shortText' => __( 'Following', 'o2' ),
|
|---|
| 119 | 'title' => __( 'Unfollow comments', 'o2' ),
|
|---|
| 120 | 'classes' => array( 'post-comments-subscribed' ),
|
|---|
| 121 | 'genericon' => 'genericon-unsubscribe',
|
|---|
| 122 | 'nextState' => 'normal'
|
|---|
| 123 | )
|
|---|
| 124 | )
|
|---|
| 125 | );
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | function subscription_o2_add_comment_subscriber_link( $actions, $id = false ) {
|
|---|
| 129 | global $post, $wpdb;
|
|---|
| 130 | if ( !$id ) {
|
|---|
| 131 | $id = $post->ID;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if ( comments_open( $id ) ) {
|
|---|
| 135 | $protocol = is_ssl() ? 'https' : 'http';
|
|---|
| 136 | $query_args = array();
|
|---|
| 137 | $initial_state = 'normal';
|
|---|
| 138 |
|
|---|
| 139 | $subscribed_ids = (array)get_user_meta( wp_get_current_user()->ID, self::user_meta_key, true );
|
|---|
| 140 |
|
|---|
| 141 | if ( in_array( $id, $subscribed_ids ) ) {
|
|---|
| 142 | $query_args = array(
|
|---|
| 143 | 'post-id' => $id,
|
|---|
| 144 | 'action' => 'post-comment-unsubscribe',
|
|---|
| 145 | );
|
|---|
| 146 | $initial_state = 'subscribed';
|
|---|
| 147 | } else {
|
|---|
| 148 | $query_args = array(
|
|---|
| 149 | 'post-id' => $id,
|
|---|
| 150 | 'action' => 'post-comment-subscribe',
|
|---|
| 151 | );
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | $link = add_query_arg( $query_args, home_url( '', $protocol ) );
|
|---|
| 155 | $link = wp_nonce_url( $link, 'post-comment-subscribe' );
|
|---|
| 156 |
|
|---|
| 157 | $actions[31] = array(
|
|---|
| 158 | 'action' => 'follow',
|
|---|
| 159 | 'href' => $link,
|
|---|
| 160 | 'classes' => array( 'subscription-link', 'o2-follow' ),
|
|---|
| 161 | 'rel' => false,
|
|---|
| 162 | 'initialState' => $initial_state
|
|---|
| 163 | );
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | return $actions;
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | global $o2_follow;
|
|---|
| 171 | $o2_follow = new o2_follow();
|
|---|