-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TSD Push Notification plugin #165
Conversation
…/edit/publish push notifications
… in tsd_pn_receiver
…shing push notification
…ush-notification/users/{token}` Ref: https://restfulapi.net/rest-put-vs-post/
…ding custom post type is being deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @hesyifei excellent work! I've added a few comments.
Few long-term things to keep in mind:
- Your SQL is great 👌, but I think it might be better to use an ORM such as Propel2 or wp-eloquent. This will help for future maintainability.
- Excellent function decomposition; I would really think we should work on adding unit tests using phpunit. This will also help for future maintainability; otherwise once we're gone, people may not touch this code and it may become legacy code then 😉
That being said, it is the end of the quarter and we have to be realistic, so I'm good with merging this and deploying this since it works, without necessarily doing these large rewrites. We will definitely need to work on unit tests though at least; I will work on getting at least a continuous integration pipeline running for these tests on this repo.
I think a solution going forward would be to spin this off into a separate plugin. It doesn't look like there are many Wordpress PN plugins out there, so spinning it off would be a great open source initiative that the Daily Tech Team could do. That way, we can get more contributors on the project, and who knows, maybe even the Expo team could pick it up. We could also assemble a team next quarter that would be in change of this!
Again, great work, and what I'm saying is mostly motivated by the fact that I think this code is well-written and could be spun off into a separate plugin for maintainability and helping others as well. Let me know what you think!
'not_found' => 'No Push Notifications found.', | ||
'not_found_in_trash' => 'No Push Notifications found in Trash.', | ||
'featured_image' => 'Push Notification Feature Image', // Overrides the “Featured Image” phrase for this post type. Added in 4.3 | ||
'set_featured_image' => 'Set feature image', // Overrides the “Set featured image” phrase for this post type. Added in 4.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hesyifei Can you remove these comments?
return []; | ||
} | ||
|
||
// TODO: Find better way to store category_ids, author_ids, location_ids. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this todo comment
'post_type' => 'tsd_pn_receiver', | ||
'post_title' => $token, | ||
'post_status' => "publish", | ||
/*'meta_input' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this commented-out block of code
tsd_create_update_pn_receiver( $post_id, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] ); | ||
} else { | ||
echo "WARNING: SHOULD NOT HAVE MORE THAN 1 POST WITH SAME NAME"; | ||
return []; // TODO: return error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe for returning errors, you can just use the WP_Error
object: https://wordpress.stackexchange.com/questions/253445/wp-rest-api-how-to-change-http-response-status-code
$post_id = tsd_create_new_pn_receiver( $user_token, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] ); | ||
} else if ( count( $this_user ) == 1 ) { | ||
$post_id = $this_user[0]->ID; | ||
tsd_create_update_pn_receiver( $post_id, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this line of code
|
||
// Ref: https://docs.expo.io/versions/latest/guides/push-notifications/#http2-api | ||
// TODO: "an array of up to 100 messages" - need divide 100 | ||
$response = wp_remote_post( "https://exp.host/--/api/v2/push/send", [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$response = wp_remote_post( "https://exp.host/--/api/v2/push/send", [ | |
$response = wp_safe_remote_post( "https://exp.host/--/api/v2/push/send", [ |
} | ||
|
||
tsd_pn_set_admin_notice( 'success', "Response: \n" . json_encode( $decoded_body ) . "\nYour message: \n" . json_encode( $message_body ) ); | ||
//wp_die( "Notification sent!<br />".$log_content, "Notification sent!", [ "response" => 200, "back_link" => true ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this
|
||
$charset_collate = $wpdb->get_charset_collate(); | ||
|
||
$sql = "CREATE TABLE $tsd_pn_db_table_name ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$sql = "CREATE TABLE $tsd_pn_db_table_name ( | |
$sql = "CREATE TABLE IF NOT EXISTS $tsd_pn_db_table_name ( |
$charset_collate = $wpdb->get_charset_collate(); | ||
|
||
$sql = "CREATE TABLE $tsd_pn_db_table_name ( | ||
id bigint(20) NOT NULL AUTO_INCREMENT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you can just use ints instead of bigints for these columns
); | ||
|
||
// DEBUG | ||
//update_option( "tsd_pn_debug_info", [] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this
return []; // TODO: return error; | ||
} | ||
|
||
if ( $post_id != -1 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( $post_id != -1 ) { | |
if ( $post_id !== -1 ) { |
$notification_data | ||
); | ||
|
||
// DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these two lines
Thanks for all the comments! 👍🏻 Since we moved this plugin to a separate repo TheStanfordDaily/tsd-push-notification, we will add TSD Push Notification Plugin as a submodule. See #167. The comments were all addressed in the new repo. See also TheStanfordDaily/tsd-push-notification#1, TheStanfordDaily/tsd-push-notification#2. Documentation was moved to https://github.com/TheStanfordDaily/tsd-push-notification/wiki. |
This plugin was moved to a separate repo TheStanfordDaily/tsd-push-notification.
For:
Data Structure
Suppose:
1144875
:1144876
:Then,
item_id
item_id
for:item_type
=list
Custom taxonomy
tsd_push_msg_receiver_group
’sterm_id
.Note that the strings used in
subscribing
(such as"breaking"
) are the custom taxonomy'sslug
.item_type
=category_ids
Category’s
term_id
.item_type
=author_ids
Author’s
ID
.item_type
=location_ids
Location’s
id
defined bytsd_locations_plugin_get_locations()
.Procedure of Sending Notification to Subscribers
When the “Publish” button of a normal post is clicked:
$notification_receiver_ids
$notification_receiver_ids
$notification_receiver_ids
<!--more-->
tag[ "post_id" => $post_id ]
array_unique( $notification_receiver_ids );
When the “Publish” button of a
tsd_push_msg
post is clicked:tsd_push_msg_receiver_group
s of the post$notification_receiver_ids
tsd_pn_
.For example, having a custom field called
tsd_pn_post_id
will addpost_id
and its value to the data.array_unique( $notification_receiver_ids );