PluginProbe
Friends / 2.9.1
Friends v2.9.1
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.9.1, at includes/class-automatic-status.php

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