| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Logging |
| 4 |
* |
| 5 |
* This contains the functions for Logging. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Friends; |
| 11 |
|
| 12 |
/** |
| 13 |
* This is the class for the Logging part of the Friends Plugin. |
| 14 |
* |
| 15 |
* @since 0.21 |
| 16 |
* |
| 17 |
* @package Friends |
| 18 |
* @author Alex Kirk |
| 19 |
*/ |
| 20 |
class Logging { |
| 21 |
/** |
| 22 |
* Contains a reference to the Friends class. |
| 23 |
* |
| 24 |
* @var Friends |
| 25 |
*/ |
| 26 |
private $friends; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor |
| 30 |
* |
| 31 |
* @param Friends $friends A reference to the Friends object. |
| 32 |
*/ |
| 33 |
public function __construct( Friends $friends ) { |
| 34 |
$this->friends = $friends; |
| 35 |
$this->register_hooks(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register the WordPress hooks |
| 40 |
*/ |
| 41 |
private function register_hooks() { |
| 42 |
add_action( 'friends_retrieved_new_posts', array( $this, 'log_feed_successfully_fetched' ), 10, 3 ); |
| 43 |
add_action( 'friends_retrieve_friends_error', array( $this, 'log_feed_error' ), 10, 2 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Log that a feed was sucessfully fetched. |
| 48 |
* |
| 49 |
* @param User_Feed $user_feed The user feed. |
| 50 |
* @param array $new_posts The new posts that were fetched |
| 51 |
* (potentially empty array). |
| 52 |
* @param array $modified_posts Posts in the feed that weere modified |
| 53 |
* (potentially empty array). |
| 54 |
*/ |
| 55 |
public function log_feed_successfully_fetched( User_Feed $user_feed, $new_posts, $modified_posts ) { |
| 56 |
// translators: %s is the number of new posts found. |
| 57 |
$last_log = sprintf( _n( 'Found %d new post.', 'Found %d new posts.', count( $new_posts ), 'friends' ), count( $new_posts ) ); |
| 58 |
if ( $modified_posts ) { |
| 59 |
// translators: %s is the number of new posts found. |
| 60 |
$last_log .= ' ' . sprintf( _n( '%d post was modified.', '%d posts were modified.', count( $modified_posts ), 'friends' ), count( $modified_posts ) ) . ' (' . implode( ', ', array_slice( $modified_posts, 0, 5 ) ) . ')'; |
| 61 |
} |
| 62 |
$user_feed->update_last_log( $last_log ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Log that an error occurred when fetching a feed. |
| 67 |
* |
| 68 |
* @param User_Feed $user_feed The user feed. |
| 69 |
* @param \WP_Error $error The error that occurred when |
| 70 |
* fetching the feed. |
| 71 |
*/ |
| 72 |
public function log_feed_error( User_Feed $user_feed, $error ) { |
| 73 |
$user_feed->update_last_log( $error->get_error_message() ); |
| 74 |
} |
| 75 |
} |
| 76 |
|