-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace WP\Notifications; | ||
|
||
use DateTime; | ||
use DateInterval; | ||
use DateTimeInterface; | ||
|
||
class Notice_Repository { | ||
|
||
/** | ||
* Container for the main instance of the class. | ||
* | ||
* @var Notice_Repository|null | ||
*/ | ||
private static $instance = null; | ||
|
||
/** | ||
* Supported collection query parameters. | ||
*/ | ||
protected $collection_query_params = array( 'count', 'offset' ); | ||
|
||
/** | ||
* Supported sort by parameters. | ||
*/ | ||
protected $sort_by_params = array( 'created_at' ); | ||
|
||
/** | ||
* Find a notice by channel name and user ID. | ||
* | ||
* @param string $channel_name Namespaced name of the channel to look for. | ||
* @param int $user_id User ID to look for. | ||
* | ||
* @return Notice|false Maybe a notice that match the user's ID and channel's | ||
* name, or false if not found. | ||
*/ | ||
public function find_by( string $channel_name, int $user_id ) { | ||
global $wpdb; | ||
|
||
$row = $wpdb->get_row( | ||
$wpdb->prepare( | ||
'SELECT `channel_name`, `user_id`, `snoozed_until` FROM `' . $wpdb->prefix . 'notifications_subscriptions` WHERE `channel_name` = %s AND `user_id` = %d', | ||
$channel_name, | ||
$user_id | ||
), | ||
ARRAY_A | ||
); | ||
|
||
if ( null === $row ) { | ||
return false; | ||
} | ||
|
||
return Subscription_Factory::get_instance()->make( $row ); | ||
} | ||
|
||
/** | ||
* Find notes by user ID. | ||
* | ||
* @param int $user_id User ID to look for. | ||
* | ||
* @return Subscription[] A collection of notices that match the user's ID, an empty array if none are found. | ||
*/ | ||
public function find_by_user_id( int $user_id, array $args ) { | ||
global $wpdb; | ||
|
||
$rows = $wpdb->get_results( | ||
$wpdb->prepare( | ||
"SELECT `channel_name`, `created_at`, `user_id`, `snoozed_until` FROM . $wpdb->prefix . 'notifications_subscriptions` WHERE `user_id` = %d", | ||
$user_id | ||
), | ||
ARRAY_A | ||
); | ||
|
||
if ( null === $rows ) { | ||
return false; | ||
} | ||
|
||
$subscriptions = array(); | ||
|
||
foreach ( $rows as $row ) { | ||
$subscription = Subscription_Factory::get_instance()->make( $row ); | ||
|
||
if ( ! ( false === $subscription ) ) { | ||
$subscriptions[] = $subscription; | ||
} | ||
} | ||
|
||
return $subscriptions; | ||
} | ||
|
||
/** | ||
* Insert a notice into the database. | ||
* | ||
* A notice is composed of possibly multiple message rows containing translated | ||
* message data matching the preferences of users subscribed to the channel, the | ||
* messages are related to users through the queue table. The queue table contains | ||
* all the data associated with an individual user's notification state. | ||
* | ||
* @return bool Whether insert is successful, | ||
*/ | ||
public function insert( Subscription $subscription ) { | ||
global $wpdb; | ||
|
||
$snoozed_until = $subscription->get_snoozed_until(); | ||
|
||
if ( $snoozed_until instanceof DateTime ) { | ||
$snoozed_until = $snoozed_until()->format( 'Y-m-d H:i:s' ); | ||
} | ||
|
||
// if ( ! Subscription_Factory::validate_user_id( $subscription ) ) { | ||
// return false; | ||
// } | ||
|
||
$result = $wpdb->query( | ||
$wpdb->prepare( | ||
'INSERT INTO `' . $wpdb->prefix . 'notifications_subscriptions` ( | ||
`channel_name`, | ||
`created_at`, | ||
`user_id`, | ||
`snoozed_until` | ||
) | ||
VALUES ( | ||
%s, | ||
UTC_TIMESTAMP(), | ||
%d, | ||
%s | ||
);', | ||
array( | ||
$subscription->get_channel_name(), | ||
$subscription->get_user_id(), | ||
$snoozed_until, | ||
), | ||
) | ||
); | ||
|
||
if ( ! ( false === $result ) ) { | ||
if ( 0 < $result ) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Utility method to retrieve the main instance of the class. | ||
* | ||
* The instance will be created if it does not exist yet. | ||
* | ||
* @return Notice_Repository The main instance. | ||
*/ | ||
public static function get_instance() { | ||
if ( null === self::$instance ) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters