PluginProbe
Friends / trunk
Friends vtrunk
4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 2.9.2 All 87 releases
friends / includes / class-logging.php

class-logging.php in Friends trunk, at includes/class-logging.php

156 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * The custom post type for logging.
23 *
24 * @var string
25 */
26 const CPT = 'friends_log';
27
28 /**
29 * Contains a reference to the Friends class.
30 *
31 * @var Friends
32 */
33 private $friends;
34
35 /**
36 * Constructor
37 *
38 * @param Friends $friends A reference to the Friends object.
39 */
40 public function __construct( Friends $friends ) {
41 $this->friends = $friends;
42 $this->register_hooks();
43 }
44
45 /**
46 * Register the WordPress hooks
47 */
48 private function register_hooks() {
49 add_action( 'init', array( $this, 'register_post_type' ) );
50 add_action( 'friends_retrieved_new_posts', array( $this, 'log_feed_successfully_fetched' ), 10, 3 );
51 add_action( 'friends_retrieve_friends_error', array( $this, 'log_feed_error' ), 10, 2 );
52 add_action( 'friends_log', array( $this, 'log_entry' ), 10, 2 );
53 }
54
55 /**
56 * Register the custom post type for logging.
57 */
58 public function register_post_type() {
59 $args = array(
60 'labels' => array(
61 'name' => __( 'Friends Logs', 'friends' ),
62 'singular_name' => __( 'Friends Log', 'friends' ),
63 ),
64 'public' => false,
65 'show_ui' => false,
66 'show_in_menu' => false,
67 'supports' => array( 'title', 'editor' ),
68 );
69 register_post_type( self::CPT, $args );
70
71 register_post_meta(
72 self::CPT,
73 'type',
74 array(
75 'type' => 'string',
76 'single' => true,
77 )
78 );
79 register_post_meta(
80 self::CPT,
81 'module',
82 array(
83 'type' => 'string',
84 'single' => true,
85 )
86 );
87 }
88
89 /**
90 * Log that a feed was sucessfully fetched.
91 *
92 * @param User_Feed $user_feed The user feed.
93 * @param array $new_posts The new posts that were fetched
94 * (potentially empty array).
95 * @param array $modified_posts Posts in the feed that weere modified
96 * (potentially empty array).
97 */
98 public function log_feed_successfully_fetched( User_Feed $user_feed, $new_posts, $modified_posts ) {
99 // translators: %s is the number of new posts found.
100 $last_log = sprintf( _n( 'Found %d new post.', 'Found %d new posts.', count( $new_posts ), 'friends' ), count( $new_posts ) );
101 if ( $modified_posts ) {
102 // translators: %s is the number of new posts found.
103 $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 ) ) . ')';
104 }
105 $user_feed->update_last_log( $last_log );
106 }
107
108 /**
109 * Log that an error occurred when fetching a feed.
110 *
111 * @param User_Feed $user_feed The user feed.
112 * @param \WP_Error $error The error that occurred when
113 * fetching the feed.
114 */
115 public function log_feed_error( User_Feed $user_feed, $error ) {
116 $user_feed->update_last_log( $error->get_error_message() );
117 }
118
119 /**
120 * Save a log message.
121 *
122 * @param string $type The type of log message.
123 * @param string $message The message.
124 * @param array $details The details of the log message.
125 * @param string $module The module that generated the log message.
126 * @param int $user_id The ID of the user that generated the log message.
127 * @return int The ID of the log post.
128 */
129 public static function log( $type, $message, $details, $module, $user_id ) {
130 $post_id = wp_insert_post(
131 array(
132 'post_type' => self::CPT,
133 'post_title' => $message,
134 'post_content' => str_replace( '\\', '\\\\', wp_json_encode( $details, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ),
135 'post_author' => $user_id,
136 'post_status' => 'publish',
137 )
138 );
139
140 add_post_meta( $post_id, 'type', $type );
141 add_post_meta( $post_id, 'module', $module );
142
143 return $post_id;
144 }
145
146 public static function get_logs() {
147 $logs = get_posts(
148 array(
149 'post_type' => self::CPT,
150 'posts_per_page' => 100,
151 )
152 );
153 return $logs;
154 }
155 }
156