PluginProbe
Stream – Activity Log & Audit Trail / 2.0.2
Stream – Activity Log & Audit Trail v2.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-wp-stream-feeds.php

class-wp-stream-feeds.php in Stream – Activity Log & Audit Trail 2.0.2, at classes/class-wp-stream-feeds.php

265 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WP_Stream_Feeds {
4
5 const FEED_QUERY_VAR = 'stream';
6 const FEED_KEY_QUERY_VAR = 'key';
7 const FEED_TYPE_QUERY_VAR = 'type';
8 const USER_FEED_OPTION_KEY = 'stream_user_feed_key';
9 const GENERATE_KEY_QUERY_VAR = 'stream_new_user_feed_key';
10
11 public static function load() {
12 if ( ! is_admin() ) {
13 $feed_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
14 }
15
16 if ( ! isset( WP_Stream_Settings::$options['general_private_feeds'] ) || ! WP_Stream_Settings::$options['general_private_feeds'] ) {
17 return;
18 }
19
20 add_action( 'show_user_profile', array( __CLASS__, 'save_user_feed_key' ) );
21 add_action( 'edit_user_profile', array( __CLASS__, 'save_user_feed_key' ) );
22
23 add_action( 'show_user_profile', array( __CLASS__, 'user_feed_key' ) );
24 add_action( 'edit_user_profile', array( __CLASS__, 'user_feed_key' ) );
25
26 // Generate new Stream Feed Key
27 add_action( 'wp_ajax_wp_stream_feed_key_generate', array( __CLASS__, 'generate_user_feed_key' ) );
28
29 add_feed( self::FEED_QUERY_VAR, array( __CLASS__, 'feed_template' ) );
30 }
31
32 /**
33 * Sends a new user key when the
34 *
35 * @return void/json
36 */
37 public static function generate_user_feed_key() {
38 check_ajax_referer( 'wp_stream_generate_key', 'nonce' );
39
40 $user_id = wp_stream_filter_input( INPUT_POST, 'user', FILTER_SANITIZE_NUMBER_INT );
41
42 if ( $user_id ) {
43 $feed_key = wp_generate_password( 32, false );
44 update_user_meta( $user_id, self::USER_FEED_OPTION_KEY, $feed_key );
45
46 $link = self::get_user_feed_url( $feed_key );
47 $xml_feed = add_query_arg( array( 'type' => 'json' ), $link );
48 $json_feed = add_query_arg( array( 'type' => 'json' ), $link );
49
50 wp_send_json_success(
51 array(
52 'message' => 'User feed key successfully generated.',
53 'feed_key' => $feed_key,
54 'xml_feed' => $xml_feed,
55 'json_feed' => $json_feed,
56 )
57 );
58 } else {
59 wp_send_json_error( 'User ID error' );
60 }
61 }
62
63 /**
64 * Generates and saves a unique key as user meta if the user does not
65 * already have a key, or has requested a new one.
66 *
67 * @action show_user_profile
68 * @action edit_user_profile
69 * @param WP_User $user
70 * @return void
71 */
72 public static function save_user_feed_key( $user ) {
73 $generate_key = wp_stream_filter_input( INPUT_GET, self::GENERATE_KEY_QUERY_VAR );
74 $nonce = wp_stream_filter_input( INPUT_GET, 'wp_stream_nonce' );
75
76 if ( ! $generate_key && get_user_meta( $user->ID, self::USER_FEED_OPTION_KEY, true ) ) {
77 return;
78 }
79
80 if ( $generate_key && ! wp_verify_nonce( $nonce, 'wp_stream_generate_key' ) ) {
81 return;
82 }
83
84 $feed_key = wp_generate_password( 32, false );
85
86 update_user_meta( $user->ID, self::USER_FEED_OPTION_KEY, $feed_key );
87 }
88
89 /**
90 * Output for Stream Feed URL field in user profiles.
91 *
92 * @action show_user_profile
93 * @action edit_user_profile
94 * @param WP_User $user
95 * @return string
96 */
97 public static function user_feed_key( $user ) {
98 if ( ! array_intersect( $user->roles, WP_Stream_Settings::$options['general_role_access'] ) ) {
99 return;
100 }
101
102 $key = get_user_meta( $user->ID, self::USER_FEED_OPTION_KEY, true );
103 $link = self::get_user_feed_url( $key );
104
105 $nonce = wp_create_nonce( 'wp_stream_generate_key' );
106 ?>
107 <table class="form-table">
108 <tr>
109 <th><label for="<?php echo esc_attr( self::USER_FEED_OPTION_KEY ) ?>"><?php esc_html_e( 'Stream Feeds Key', 'stream' ) ?></label></th>
110 <td>
111 <p class="wp-stream-feeds-key">
112 <?php wp_nonce_field( 'wp_stream_generate_key', 'wp_stream_generate_key_nonce' ) ?>
113 <input type="text" name="<?php echo esc_attr( self::USER_FEED_OPTION_KEY ) ?>" id="<?php echo esc_attr( self::USER_FEED_OPTION_KEY ) ?>" class="regular-text code" value="<?php echo esc_attr( $key ) ?>" readonly>
114 <small><a href="<?php echo esc_url( add_query_arg( array( self::GENERATE_KEY_QUERY_VAR => true, 'wp_stream_nonce' => $nonce ) ) ) ?>" id="<?php echo esc_attr( self::USER_FEED_OPTION_KEY ) ?>_generate"><?php esc_html_e( 'Generate new key', 'stream' ) ?></a></small>
115 <span class="spinner" style="display: none;"></span>
116 </p>
117 <p class="description"><?php esc_html_e( 'This is your private key used for accessing feeds of Stream Records securely. You can change your key at any time by generating a new one using the link above.', 'stream' ) ?></p>
118 <p class="wp-stream-feeds-links">
119 <a href="<?php echo esc_url( add_query_arg( array( 'type' => 'rss' ), $link ) ) ?>" class="rss-feed" target="_blank"><?php echo esc_html_e( 'RSS Feed', 'stream' ) ?></a>
120 |
121 <a href="<?php echo esc_url( add_query_arg( array( 'type' => 'atom' ), $link ) ) ?>" class="atom-feed" target="_blank"><?php echo esc_html_e( 'ATOM Feed', 'stream' ) ?></a>
122 |
123 <a href="<?php echo esc_url( add_query_arg( array( 'type' => 'json' ), $link ) ) ?>" class="json-feed" target="_blank"><?php echo esc_html_e( 'JSON Feed', 'stream' ) ?></a>
124 </p>
125 </td>
126 </tr>
127 </table>
128 <?php
129 }
130
131 /**
132 * Return Stream Feed URL
133 *
134 * @return string
135 */
136 public static function get_user_feed_url( $key ) {
137 $pretty_permalinks = get_option( 'permalink_structure' );
138 $query_var = self::FEED_QUERY_VAR;
139
140 if ( empty( $pretty_permalinks ) ) {
141 $link = add_query_arg(
142 array(
143 'feed' => $query_var,
144 self::FEED_KEY_QUERY_VAR => $key,
145 ),
146 home_url( '/' )
147 );
148 } else {
149 $link = add_query_arg(
150 array(
151 self::FEED_KEY_QUERY_VAR => $key,
152 ),
153 home_url(
154 sprintf(
155 '/feed/%s/',
156 $query_var
157 )
158 )
159 );
160 }
161
162 return $link;
163 }
164
165 /**
166 * Output for Stream Records as a feed.
167 *
168 * @return xml
169 */
170 public static function feed_template() {
171 $die_title = esc_html__( 'Access Denied', 'stream' );
172 $die_message = sprintf( '<h1>%s</h1><p>%s</p>', $die_title, esc_html__( "You don't have permission to view this feed, please contact your site Administrator.", 'stream' ) );
173 $query_var = self::FEED_QUERY_VAR;
174
175 $args = array(
176 'meta_key' => self::USER_FEED_OPTION_KEY,
177 'meta_value' => wp_stream_filter_input( INPUT_GET, self::FEED_KEY_QUERY_VAR ),
178 'number' => 1,
179 );
180 $user = get_users( $args );
181
182 if ( empty( $user ) ) {
183 wp_die( $die_message, $die_title );
184 }
185
186 if ( ! is_super_admin( $user[0]->ID ) ) {
187 $roles = isset( $user[0]->roles ) ? (array) $user[0]->roles : array();
188
189 if ( ! $roles || ! array_intersect( $roles, WP_Stream_Settings::$options['general_role_access'] ) ) {
190 wp_die( $die_message, $die_title );
191 }
192 }
193
194 $args = array(
195 'search' => wp_stream_filter_input( INPUT_GET, 'search' ),
196 'record_after' => wp_stream_filter_input( INPUT_GET, 'record_after' ), // Deprecated, use date_after instead
197 'date' => wp_stream_filter_input( INPUT_GET, 'date' ),
198 'date_from' => wp_stream_filter_input( INPUT_GET, 'date_from' ),
199 'date_to' => wp_stream_filter_input( INPUT_GET, 'date_to' ),
200 'date_after' => wp_stream_filter_input( INPUT_GET, 'date_after' ),
201 'date_before' => wp_stream_filter_input( INPUT_GET, 'date_before' ),
202 'record' => wp_stream_filter_input( INPUT_GET, 'record' ),
203 'record__in' => wp_stream_filter_input( INPUT_GET, 'record__in' ),
204 'record__not_in' => wp_stream_filter_input( INPUT_GET, 'record__not_in' ),
205 'records_per_page' => wp_stream_filter_input( INPUT_GET, 'records_per_page', FILTER_SANITIZE_NUMBER_INT ),
206 'order' => wp_stream_filter_input( INPUT_GET, 'order' ),
207 'orderby' => wp_stream_filter_input( INPUT_GET, 'orderby' ),
208 'meta' => wp_stream_filter_input( INPUT_GET, 'meta' ),
209 'fields' => wp_stream_filter_input( INPUT_GET, 'fields' ),
210 );
211
212 $properties = array(
213 'author',
214 'author_role',
215 'ip',
216 'object_id',
217 'connector',
218 'context',
219 'action',
220 );
221
222 foreach ( $properties as $property ) {
223 $args[ $property ] = wp_stream_filter_input( INPUT_GET, $property );
224 $args[ "{$property}__in" ] = wp_stream_filter_input( INPUT_GET, "{$property}__in" );
225 $args[ "{$property}__not_in" ] = wp_stream_filter_input( INPUT_GET, "{$property}__not_in" );
226 }
227
228 $records = wp_stream_query( $args );
229
230 $latest_record = isset( $records[0]->created ) ? $records[0]->created : null;
231
232 $records_admin_url = add_query_arg(
233 array(
234 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG,
235 ),
236 admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE )
237 );
238
239 $latest_link = null;
240
241 if ( isset( $records[0]->ID ) ) {
242 $latest_link = add_query_arg(
243 array(
244 'record__in' => $records[0]->ID,
245 ),
246 $records_admin_url
247 );
248 }
249
250 $domain = parse_url( $records_admin_url, PHP_URL_HOST );
251 $format = wp_stream_filter_input( INPUT_GET, self::FEED_TYPE_QUERY_VAR );
252
253 if ( 'atom' === $format ) {
254 require_once WP_STREAM_INC_DIR . 'feeds/atom.php';
255 } elseif ( 'json' === $format ) {
256 require_once WP_STREAM_INC_DIR . 'feeds/json.php';
257 } else {
258 require_once WP_STREAM_INC_DIR . 'feeds/rss-2.0.php';
259 }
260
261 exit;
262 }
263
264 }
265