| @@ -18,8 +18,15 @@ | ||
| 18 | 18 | * @author Alex Kirk |
| 19 | 19 | */ |
| 20 | 20 | class Logging { |
| 21 | 21 | /** |
| 22 | + * The custom post type for logging. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + const CPT = 'friends_log'; | |
| 27 | + | |
| 28 | + /** | |
| 22 | 29 | * Contains a reference to the Friends class. |
| 23 | 30 | * |
| 24 | 31 | * @var Friends |
| 25 | 32 | */ |
| @@ -38,13 +45,49 @@ | ||
| 38 | 45 | /** |
| 39 | 46 | * Register the WordPress hooks |
| 40 | 47 | */ |
| 41 | 48 | private function register_hooks() { |
| 49 | + add_action( 'init', array( $this, 'register_post_type' ) ); | |
| 42 | 50 | add_action( 'friends_retrieved_new_posts', array( $this, 'log_feed_successfully_fetched' ), 10, 3 ); |
| 43 | 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 ); | |
| 44 | 53 | } |
| 45 | 54 | |
| 46 | 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 | + /** | |
| 47 | 90 | * Log that a feed was sucessfully fetched. |
| 48 | 91 | * |
| 49 | 92 | * @param User_Feed $user_feed The user feed. |
| 50 | 93 | * @param array $new_posts The new posts that were fetched |
| @@ -70,6 +113,43 @@ | ||
| 70 | 113 | * fetching the feed. |
| 71 | 114 | */ |
| 72 | 115 | public function log_feed_error( User_Feed $user_feed, $error ) { |
| 73 | 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; | |
| 74 | 154 | } |
| 75 | 155 | } |