Changeset 819
- Timestamp:
- 08/27/2014 02:43:24 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php
r439 r819 16 16 */ 17 17 public function __construct() { 18 add_action( 'transition_post_status', array( $this, 'post_updated' ), 5, 3 ); 18 add_action( 'transition_post_status', array( $this, 'post_updated' ), 5, 3 ); 19 add_action( 'camptix_require_login_confirm_username', array( $this, 'attendee_registered' ), 10, 2 ); 19 20 } 20 21 … … 26 27 * This hooks in before the custom post types save their post meta fields, so that we can access the 27 28 * WordPress.org username from the previous revision and from the current one. 29 * 30 * @todo Maybe refactor this to work more like attendee_registered(), so the speaker/sponsor plugins just fire a 31 * hook when they're ready to send the notification, rather than this plugin having to be aware of (and 32 * coupled to) the internal logic of those plugins. 28 33 * 29 34 * @param string $new_status … … 31 36 * @param WP_Post $post 32 37 */ 33 function post_updated( $new_status, $old_status, $post ) {38 public function post_updated( $new_status, $old_status, $post ) { 34 39 if ( ! $this->is_post_notifiable( $post ) ) { 35 40 return; … … 37 42 38 43 if ( 'publish' == $new_status && 'publish' == $old_status ) { 39 $this->published_ post_updated( $post );44 $this->published_speaker_post_updated( $post ); 40 45 } elseif ( 'publish' == $new_status || 'publish' == $old_status ) { 41 $this-> post_published_or_unpublished( $new_status, $old_status, $post );46 $this->speaker_post_published_or_unpublished( $new_status, $old_status, $post ); 42 47 } 43 48 } … … 64 69 65 70 /** 66 * Updates the activity and associations of a profile when the WordPress.org username on a published post changes. 71 * Updates the activity and associations of a profile when the WordPress.org username on a published speaker 72 * or organizer post changes. 67 73 * 68 74 * @todo The handler doesn't support removing activity, but maybe do that here if support is added. … … 70 76 * @param WP_Post $post 71 77 */ 72 p ublic function published_post_updated( $post ) {73 $previous_user_id = $this->get_ wporg_user_id( $post, false);74 $new_user_id = $this->get_ wporg_user_id( $post );75 78 protected function published_speaker_post_updated( $post ) { 79 $previous_user_id = $this->get_saved_wporg_user_id( $post ); 80 $new_user_id = $this->get_new_wporg_user_id( $post ); 81 76 82 // There is no username, or it hasn't changed, so we don't need to do anything here. 77 83 if ( $previous_user_id === $new_user_id ) { … … 99 105 100 106 /** 101 * Adds new activity and associations to a user's profile when custom posts are published, and removes102 * associations when customposts are unpublished.107 * Adds new activity and associations to a user's profile when speaker or organizer posts are published, and 108 * removes associations, when speaker or organizer posts are unpublished. 103 109 * 104 110 * @todo The handler doesn't support removing activity, but maybe do that here if support is added. … … 108 114 * @param WP_Post $post 109 115 */ 110 p ublic functionpost_published_or_unpublished( $new_status, $old_status, $post ) {116 protected function speaker_post_published_or_unpublished( $new_status, $old_status, $post ) { 111 117 if ( 'publish' == $new_status ) { 112 118 $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) ); … … 116 122 // This makes sure that the association is removed from the same user that it was originally added to. 117 123 118 $user_id = $this->get_ wporg_user_id( $post, false);124 $user_id = $this->get_saved_wporg_user_id( $post ); 119 125 $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $user_id ) ); 120 126 } … … 122 128 123 129 /** 130 * Adds new activity to a user's profile when they register for a ticket. 131 * 132 * @todo Handle cases where the user changes, either from the admin editing the back-end post, or a from a 133 * different user updating via the edit token? 134 * @todo The handler doesn't support removing activity, but maybe do that here if support is added. 135 * 136 * @param int $attendee_id 137 * @param string $username 138 */ 139 public function attendee_registered( $attendee_id, $username ) { 140 $attendee = get_post( $attendee_id ); 141 $user_id = $this->get_saved_wporg_user_id( $attendee ); 142 143 $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $attendee, $user_id ) ); 144 } 145 146 /** 124 147 * Builds the payload for an activity notification based on a new post 125 148 * 126 149 * @param WP_Post $post 150 * @param int $user_id 151 * 127 152 * @return array|false 128 153 */ 129 protected function get_post_activity_payload( $post ) {154 protected function get_post_activity_payload( $post, $user_id = null ) { 130 155 $activity = false; 131 $user_id = $this->get_wporg_user_id( $post ); 132 $wordcamp = get_wordcamp_post(); 133 156 $wordcamp = get_wordcamp_post(); 157 158 if ( ! $user_id ) { 159 $user_id = $this->get_new_wporg_user_id( $post ); 160 } 161 134 162 if ( $user_id ) { 135 163 $activity = array( … … 152 180 $activity['organizer_id'] = $post->ID; 153 181 break; 182 183 case 'tix_attendee': 184 $activity['attendee_id'] = $post->ID; 185 break; 154 186 155 187 default: … … 174 206 175 207 if ( ! $user_id ) { 176 $user_id = $this->get_ wporg_user_id( $post );208 $user_id = $this->get_new_wporg_user_id( $post ); 177 209 } 178 210 … … 196 228 $association['association'] = 'wordcamp-organizer'; 197 229 break; 198 230 231 case 'tix_attendee': 199 232 default: 200 233 $association = false; … … 207 240 208 241 /** 209 * Get the WordPress.org user_id associated with a custom post 210 * 211 * When creating a new post, the user_id may be needed before the post meta is saved, 212 * so we first check the request for the latest value. 213 * 214 * @param WP_Post $post 215 * @param string $username_field 242 * Get the current WordPress.org user_id associated with a custom post 243 * 244 * This is called during the context of a post being updated, so the new username is the one submitted in 245 * the $_POST request, or the currently logged in user, as opposed to the user_id saved in the database. 246 * 247 * @param WP_Post $post 216 248 * @return false|int 217 249 */ 218 protected function get_wporg_user_id( $post, $username_field = 'wcpt-wporg-username' ) { 250 protected function get_new_wporg_user_id( $post ) { 251 $user_id = $user = false; 252 253 if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) && isset( $_POST['wcpt-wporg-username'] ) ) { 254 $user = get_user_by( 'login', $_POST['wcpt-wporg-username'] ); 255 } 256 257 if ( ! empty( $user->ID ) ) { 258 $user_id = $user->ID; 259 } 260 261 return $user_id; 262 } 263 264 /** 265 * Get the previous WordPress.org user_id associated with a custom post 266 * 267 * This is called during the context of a post being updated, so the saved username is the one saved in 268 * the database, as opposed to the one in the $_POST request or the currently logged in user. 269 * 270 * @param WP_Post $post 271 * @return false|int 272 */ 273 protected function get_saved_wporg_user_id( $post ) { 219 274 $user_id = false; 220 221 if ( $username_field && isset( $_POST[ $username_field ] ) ) { 222 $user = get_user_by( 'login', $_POST[ $username_field ] ); 223 224 if ( ! empty( $user->ID ) ) { 275 276 if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) ) { 277 $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true ); 278 } elseif ( 'tix_attendee' == $post->post_type ) { 279 $user = get_user_by( 'login', get_post_meta( $post->ID, 'tix_username', true ) ); 280 281 if ( is_a( $user, 'WP_User' ) ) { 225 282 $user_id = $user->ID; 226 283 } 227 } else { 228 $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true ); 229 } 230 284 } 285 231 286 return $user_id; 232 287 } … … 244 299 $response = $error = false; 245 300 246 // todo verify/update when can see actual responses from server247 248 301 if ( $body ) { 249 302 $response = wp_remote_post( $url, array( 'body' => $body ) );
Note: See TracChangeset
for help on using the changeset viewer.