PluginProbe
Friends / 2.7.4
Friends v2.7.4
4.3.2 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 All 88 releases
friends / includes / class-automatic-status.php

class-automatic-status.php in Friends 2.7.4, at includes/class-automatic-status.php

381 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Friends Automatic Status
4 *
5 * This contains the functions for the Automatic Status.
6 *
7 * @package Friends
8 */
9
10 namespace Friends;
11
12 /**
13 * This is the class for the Friends Plugin Automatic Status.
14 *
15 * @since 0.6
16 *
17 * @package Friends
18 * @author Alex Kirk
19 */
20 class Automatic_Status {
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( 'admin_menu', array( $this, 'admin_menu' ), 20 );
43 add_action( 'friends_admin_tabs', array( $this, 'admin_tabs' ), 20 );
44 add_filter( 'handle_bulk_actions-edit-post', array( $this, 'bulk_publish' ), 10, 3 );
45
46 if ( ! get_option( 'friends_automatic_status_disabled' ) ) {
47 return;
48 }
49 add_action( 'friends_user_post_reaction', array( $this, 'post_reaction' ), 10, 2 );
50 add_action( 'set_user_role', array( $this, 'new_friend_user' ), 20, 3 );
51 add_action( 'set', array( $this, 'new_friend_user' ), 10, 2 );
52 }
53
54 /**
55 * Add the admin menu to the sidebar.
56 */
57 public function admin_menu() {
58 $unread_badge = $this->friends->admin->get_unread_badge();
59
60 $menu_title = __( 'Friends', 'friends' ) . $unread_badge;
61 $page_type = sanitize_title( $menu_title );
62
63 add_submenu_page(
64 'friends',
65 __( 'Automatic Status', 'friends' ),
66 __( 'Automatic Status', 'friends' ),
67 Friends::required_menu_role(),
68 'friends-auto-status',
69 array( $this, 'validate_drafts' )
70 );
71
72 add_action( 'load-' . $page_type . '_page_friends-auto-status', array( $this, 'redirect_to_post_format_url' ) );
73 add_action( 'load-' . $page_type . '_page_friends-auto-status', array( $this, 'process_settings' ) );
74 }
75
76 /**
77 * Add the admin menu to the tabs.
78 *
79 * @param array $menu The menu.
80 *
81 * @return array The modified menu.
82 */
83 public function admin_tabs( array $menu ) {
84 $menu[ __( 'Automatic Status', 'friends' ) ] = 'friends-auto-status';
85 return $menu;
86 }
87
88 public function redirect_to_post_format_url() {
89 if ( empty( $_GET['post_format'] ) ) {
90 wp_safe_redirect(
91 add_query_arg(
92 array(
93 'post_format' => 'status',
94 'post_status' => 'draft',
95 'post_author' => get_current_user_id(),
96 ),
97 self_admin_url( 'admin.php?page=friends-auto-status' )
98 )
99 );
100 exit;
101 }
102 }
103
104 public function process_settings() {
105 if ( empty( $_REQUEST ) || ! isset( $_REQUEST['_wpnonce'] ) ) {
106 return;
107 }
108
109 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'friends-automatic-status' ) ) {
110 return;
111 }
112
113 if ( isset( $_POST['enabled'] ) && $_POST['enabled'] ) {
114 delete_option( 'friends_automatic_status_disabled' );
115 } else {
116 update_option( 'friends_automatic_status_disabled', 1 );
117 }
118
119 if ( isset( $_GET['_wp_http_referer'] ) ) {
120 wp_safe_redirect( wp_get_referer() );
121 } else {
122 wp_safe_redirect( add_query_arg( 'updated', '1', remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
123 }
124 }
125
126 /**
127 * This displays the Automatically Generated Statuses admin page.
128 */
129 public function validate_drafts() {
130 add_filter(
131 'manage_edit-post_columns',
132 function( $columns ) {
133 unset( $columns['categories'] );
134 unset( $columns['tags'] );
135 unset( $columns['comments'] );
136 return $columns;
137 },
138 30,
139 3
140 );
141
142 add_filter(
143 'post_row_actions',
144 function( $actions, $post ) {
145 unset( $actions['view'], $actions['inline hide-if-no-js'] );
146 if ( 'publish' === $post->post_status ) {
147 $actions['publish'] = '<em>' . __( 'Published' ) . '</em>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
148 } else {
149 $actions['publish'] = sprintf(
150 '<a href="%s" aria-label="%s">%s</a>',
151 esc_url(
152 add_query_arg(
153 array(
154 'post_status' => 'draft',
155 'post_format' => 'status',
156 'post_type' => 'post',
157 '_wpnonce' => wp_create_nonce( 'bulk-posts' ),
158 '_wp_http_referer' => self_admin_url( 'admin.php?page=friends-auto-status' ),
159 'action' => 'publish',
160 'post[]' => $post->ID,
161 ),
162 self_admin_url( 'edit.php' )
163 )
164 ),
165 /* translators: %s: Post title. */
166 esc_attr( sprintf( __( 'Publish &#8220;%s&#8221;' ), $post->title ) ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
167 __( 'Publish' ) // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
168 );
169 }
170 if ( 'private' === $post->post_status ) {
171 $actions['publish-private'] = '<em>' . __( 'Privately Published' ) . '</em>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
172 } else {
173 $actions['publish-private'] = sprintf(
174 '<a href="%s" aria-label="%s">%s</a>',
175 esc_url(
176 add_query_arg(
177 array(
178 'post_status' => 'draft',
179 'post_format' => 'status',
180 'post_type' => 'post',
181 '_wpnonce' => wp_create_nonce( 'bulk-posts' ),
182 '_wp_http_referer' => self_admin_url( 'admin.php?page=friends-auto-status' ),
183 'action' => 'publish-private',
184 'post[]' => $post->ID,
185 ),
186 self_admin_url( 'edit.php' )
187 )
188 ),
189 /* translators: %s: Post title. */
190 esc_attr( sprintf( __( 'Privately Publish &#8220;%s&#8221;', 'friends' ), $post->title ) ),
191 __( 'Publish Privately', 'friends' )
192 );
193 }
194 return $actions;
195 },
196 10,
197 2
198 );
199 _get_list_table( 'WP_Posts_List_Table' );
200 require_once __DIR__ . '/class-automatic-status-list-table.php';
201
202 $post_type = 'post';
203 $post_type_object = get_post_type_object( $post_type );
204
205 $wp_list_table = new Automatic_Status_List_Table();
206 $wp_list_table->prepare_items();
207
208 wp_enqueue_script( 'inline-edit-post' );
209 wp_enqueue_script( 'heartbeat' );
210
211 $bulk_counts = array(
212 'updated' => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
213 'locked' => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
214 'deleted' => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
215 'trashed' => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
216 'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
217 );
218
219 $bulk_messages = array();
220 $bulk_messages['post'] = array(
221 /* translators: %s: Number of posts. */
222 'updated' => _n( '%s post updated.', '%s posts updated.', $bulk_counts['updated'] ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
223 'locked' => ( 1 === $bulk_counts['locked'] ) ? __( '1 post not updated, somebody is editing it.' ) : // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
224 /* translators: %s: Number of posts. */
225 _n( '%s post not updated, somebody is editing it.', '%s posts not updated, somebody is editing them.', $bulk_counts['locked'] ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
226 /* translators: %s: Number of posts. */
227 'deleted' => _n( '%s post permanently deleted.', '%s posts permanently deleted.', $bulk_counts['deleted'] ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
228 /* translators: %s: Number of posts. */
229 'trashed' => _n( '%s post moved to the Trash.', '%s posts moved to the Trash.', $bulk_counts['trashed'] ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
230 /* translators: %s: Number of posts. */
231 'untrashed' => _n( '%s post restored from the Trash.', '%s posts restored from the Trash.', $bulk_counts['untrashed'] ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
232 );
233 $bulk_messages = apply_filters( 'bulk_post_updated_messages', $bulk_messages, $bulk_counts );
234 $bulk_counts = array_filter( $bulk_counts );
235
236 Friends::template_loader()->get_template_part(
237 'admin/settings-header',
238 null,
239 array(
240 'active' => 'friends-auto-status',
241 'title' => __( 'Friends', 'friends' ),
242 )
243 );
244
245 if ( isset( $_GET['updated'] ) ) {
246 ?>
247 <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'Settings were updated.', 'friends' ); ?></p></div>
248 <?php
249 } elseif ( isset( $_GET['error'] ) ) {
250 ?>
251 <div id="message" class="updated error is-dismissible"><p><?php esc_html_e( 'An error occurred.', 'friends' ); ?></p></div>
252 <?php
253 }
254 Friends::template_loader()->get_template_part( 'admin/automatic-status-list-table', false, compact( 'wp_list_table', 'post_type' ) );
255 Friends::template_loader()->get_template_part( 'admin/settings-footer' );
256 }
257
258 /**
259 * This implements bulk publishing for the Automatically Generated Statuses page.
260 *
261 * @param string $sendback The redirect URL.
262 * @param string $doaction The action being taken.
263 * @param array $post_ids The items to take the action on.
264 *
265 * @return string A potentially modified redirect URL.
266 */
267 public function bulk_publish( $sendback, $doaction, $post_ids ) {
268 if ( 'publish' === $doaction ) {
269 $done = array(
270 'published' => 0,
271 );
272 foreach ( $post_ids as $post_id ) {
273 wp_publish_post( $post_id );
274 $done['published']++;
275 }
276
277 $sendback = add_query_arg( $done, $sendback );
278 } elseif ( 'publish-private' === $doaction ) {
279 $done = array(
280 'privately-published' => 0,
281 );
282 foreach ( $post_ids as $post_id ) {
283 wp_update_post(
284 array(
285 'ID' => $post_id,
286 'post_status' => 'private',
287 )
288 );
289 $done['privately-published']++;
290 }
291
292 $sendback = add_query_arg( $done, $sendback );
293 }
294 return $sendback;
295 }
296
297 /**
298 * Adds a status post.
299 *
300 * @param string $text The text.
301 *
302 * @return int|\WP_Error The post ID or a \WP_Error.
303 */
304 private function add_status( $text ) {
305 $user_id = get_current_user_id();
306 if ( ! $user_id ) {
307 $user_id = Friends::get_main_friend_user_id();
308 }
309
310 $new_post_id = wp_insert_post(
311 array(
312 'post_type' => 'post',
313 'post_status' => 'draft',
314 'post_author' => $user_id,
315 'tax_input' => array( 'post_format' => 'post-format-status' ),
316 'post_content' => '<!-- wp:paragraph -->' . PHP_EOL . '<p>' . $text . '</p>' . PHP_EOL . '<!-- /wp:paragraph -->',
317 )
318 );
319 if ( ! is_wp_error( $new_post_id ) ) {
320 wp_set_post_terms( $new_post_id, 'post-format-status', 'post_format' );
321 }
322 return $new_post_id;
323 }
324
325 /**
326 * Create a status based on a post reaction.
327 *
328 * @param int $post_id The post ID.
329 * @param string $emoji The emoji.
330 */
331 public function post_reaction( $post_id, $emoji ) {
332 $post = get_post( $post_id );
333 $title = get_the_title( $post );
334 if ( empty( $title ) ) {
335 $title = get_the_excerpt( $post );
336 }
337 $this->add_status(
338 sprintf(
339 // translators: %1$s is an emoji, %2$s is a linked post title.
340 __( 'I just reacted with %1$s on %2$s.', 'friends' ),
341 $emoji,
342 '<a href="' . esc_url( get_permalink( $post ) ) . '">' . esc_html( $title ) . '</a>'
343 )
344 );
345 }
346
347 /**
348 * Create a status based on a friend user status change.
349 *
350 * @param int $user_id The user id.
351 * @param string $new_role The new role.
352 * @param string $old_roles The old roles.
353 */
354 public function new_friend_user( $user_id, $new_role, $old_roles ) {
355 $friend_user = new User( $user_id );
356 $link = '<a href="' . esc_url( $friend_user->user_url ) . '">' . esc_html( $friend_user->display_name ) . '</a>';
357
358 if ( 'friend' === $new_role || 'acquaintance' === $new_role ) {
359 $this->add_status(
360 sprintf(
361 // translators: %s is a new friend.
362 __( "I'm now friends with %s.", 'friends' ),
363 $link
364 )
365 );
366 return;
367 }
368
369 if ( 'subscription' === $new_role ) {
370 $this->add_status(
371 sprintf(
372 // translators: %s is a new friend.
373 __( "I've just subscribed %s.", 'friends' ),
374 $link
375 )
376 );
377 return;
378 }
379 }
380 }
381