PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-publicize / src / class-publicize-base.php

class-publicize-base.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.7, at jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-base.php

2,130 lines 66.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Publicize_Base class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8 // phpcs:disable WordPress.NamingConventions.ValidVariableName
9
10 namespace Automattic\Jetpack\Publicize;
11
12 use Automattic\Jetpack\Connection\Client;
13 use Automattic\Jetpack\Current_Plan;
14 use Automattic\Jetpack\Paths;
15 use Automattic\Jetpack\Redirect;
16 use Automattic\Jetpack\Status;
17 use WP_Error;
18 use WP_Post;
19
20 /**
21 * Base class for Publicize.
22 */
23 abstract class Publicize_Base {
24
25 /**
26 * Services that are currently connected to the given user
27 * through Publicize.
28 *
29 * @var array
30 */
31 public $connected_services = array();
32
33 /**
34 * Services that are supported by publicize. They don't
35 * necessarily need to be connected to the current user.
36 *
37 * @var array
38 */
39 public $services;
40
41 /**
42 * Post meta key for admin page.
43 *
44 * @var string
45 */
46 public $ADMIN_PAGE = 'wpas';
47
48 /**
49 * Post meta key for post message.
50 *
51 * @var string
52 */
53 public $POST_MESS = '_wpas_mess';
54
55 /**
56 * Post meta key for the flagging when the post share feature is disabled.
57 *
58 * @var string
59 */
60 const POST_PUBLICIZE_FEATURE_ENABLED = '_wpas_feature_enabled';
61
62 /**
63 * Post meta key for Jetpack Social options.
64 *
65 * @var string
66 */
67 const POST_JETPACK_SOCIAL_OPTIONS = '_wpas_options';
68
69 /**
70 * Post meta key for per-connection customization overrides.
71 *
72 * @var string
73 */
74 const POST_CONNECTION_OVERRIDES = '_wpas_connection_overrides';
75
76 /**
77 * Post meta key for enabling per-network customization.
78 *
79 * @var string
80 */
81 const POST_CUSTOMIZE_PER_NETWORK = '_wpas_customize_per_network';
82
83 /**
84 * Attachment meta key for the social image focal point.
85 *
86 * Stored on the image (attachment), not the post, so a focal point set once is
87 * shared by every post that uses the image.
88 *
89 * @var string
90 */
91 const ATTACHMENT_IMAGE_FOCAL_POINT = '_jetpack_social_image_focal_point';
92
93 // Skip meta keys. We used to rely on _wpas_skip_ appended with the token_id to skip posts. But to support
94 // multiple connections for the same token, we are going to use the _wpas_skip_publicize_ which
95 // will be appended with the connection_id.
96 /**
97 * Token ID appended to indicate that a connection should NOT be publicized to.
98 *
99 * @var string
100 */
101 public $POST_SKIP = '_wpas_skip_';
102
103 /**
104 * Connection ID appended to indicate that a connection should NOT be publicized to.
105 *
106 * @var string
107 */
108 public $POST_SKIP_PUBLICIZE = '_wpas_skip_publicize_';
109
110 // Done meta keys.
111 /**
112 * Token ID appended to indicate a connection has already been publicized to.
113 *
114 * @var string
115 */
116 public $POST_DONE = '_wpas_done_';
117
118 /**
119 * Prefix for user authorization (used in publicize-wpcom.php)
120 *
121 * @var string
122 */
123 public $USER_AUTH = 'wpas_authorize';
124
125 /**
126 * Prefix for user opt.
127 *
128 * @var string
129 */
130 public $USER_OPT = 'wpas_';
131
132 /**
133 * Ready for Publicize to do its thing.
134 *
135 * @var string
136 */
137 public $PENDING = '_publicize_pending';
138
139 /**
140 * Array of external IDs where we've Publicized.
141 *
142 * @var string
143 */
144 public $POST_SERVICE_DONE = '_publicize_done_external';
145
146 /**
147 * Option key for dismissing Jetpack Social notices.
148 *
149 * @var string
150 */
151 const OPTION_JETPACK_SOCIAL_DISMISSED_NOTICES = 'jetpack_social_dismissed_notices';
152
153 /**
154 * The maximum size of an image that can be used as an Open Graph image.
155 *
156 * @var string
157 */
158 const OG_IMAGE_MAX_FILESIZE = 2000000; // 2MB.
159
160 /**
161 * Default pieces of the message used in constructing the
162 * content pushed out to other social networks.
163 */
164
165 /**
166 * Default prefix.
167 *
168 * @var string
169 */
170 public $default_prefix = '';
171
172 /**
173 * Default message.
174 *
175 * @var string
176 */
177 public $default_message = '%title%';
178
179 /**
180 * Default suffix.
181 *
182 * @var string
183 */
184 public $default_suffix = ' ';
185
186 /**
187 * What WP capability is require to create/delete global connections?
188 * All users with this cap can un-globalize all other global connections, and globalize any of their own
189 * Globalized connections cannot be unselected by users without this capability when publishing
190 *
191 * @var string
192 */
193 public $GLOBAL_CAP = 'publish_posts';
194
195 /**
196 * Sets up the basics of Publicize.
197 */
198 public function __construct() {
199 $this->default_message = self::build_sprintf(
200 array(
201 /**
202 * Filter the default Publicize message.
203 *
204 * @since 0.1.0
205 * @since-jetpack 2.0.0
206 *
207 * @param string $this->default_message Publicize's default message. Default is the post title.
208 */
209 apply_filters( 'wpas_default_message', $this->default_message ),
210 'title',
211 'url',
212 )
213 );
214
215 $this->default_prefix = self::build_sprintf(
216 array(
217 /**
218 * Filter the message prepended to the Publicize custom message.
219 *
220 * @since 0.1.0
221 * @since-jetpack 2.0.0
222 *
223 * @param string $this->default_prefix String prepended to the Publicize custom message.
224 */
225 apply_filters( 'wpas_default_prefix', $this->default_prefix ),
226 'url',
227 )
228 );
229
230 $this->default_suffix = self::build_sprintf(
231 array(
232 /**
233 * Filter the message appended to the Publicize custom message.
234 *
235 * @since 0.1.0
236 * @since-jetpack 2.0.0
237 *
238 * @param string $this->default_suffix String appended to the Publicize custom message.
239 */
240 apply_filters( 'wpas_default_suffix', $this->default_suffix ),
241 'url',
242 )
243 );
244
245 /**
246 * Filter the capability to change global Publicize connection options.
247 *
248 * All users with this cap can un-globalize all other global connections, and globalize any of their own
249 * Globalized connections cannot be unselected by users without this capability when publishing.
250 *
251 * @since 0.1.0
252 * @since-jetpack 2.2.1
253 *
254 * @param string $this->GLOBAL_CAP default capability in control of global Publicize connection options. Default to edit_others_posts.
255 */
256 $this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP );
257
258 // stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta,
259 // then check meta and publicize based on that. stage 3 implemented on wpcom.
260 add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 );
261 add_action( 'save_post', array( $this, 'save_meta' ), 20, 2 );
262
263 // Default checkbox state for each Connection.
264 add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 2 );
265
266 // Add images generated by Social Image Generator to the Open Graph tags on singular posts.
267 add_filter( 'jetpack_open_graph_tags', array( $this, 'add_jetpack_social_og_images' ), 12, 1 ); // $priority = 12, to run after the Twitter_Cards class adds its tags.
268
269 // Alter the "Post Publish" admin notice to mention the Connections we Publicized to.
270 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 );
271
272 // Custom priority to ensure post type support is added prior to thumbnail support being added to the theme.
273 add_action( 'init', array( $this, 'add_post_type_support' ), 8 );
274 add_action( 'init', array( $this, 'register_post_meta' ), 20 );
275
276 // The custom priority for this action ensures that any existing code that
277 // removes post-thumbnails support during 'init' continues to work.
278 add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 );
279 }
280
281 /**
282 * Services: Facebook, Twitter, etc.
283 */
284
285 /**
286 * Get services for the given blog and user.
287 *
288 * Can return all available services or just the ones with an active connection.
289 *
290 * @param string $filter Type of filter.
291 * 'all' (default) - Get all services available for connecting.
292 * 'connected' - Get all services currently connected.
293 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
294 * @param false|int $_user_id The user ID. Use false (default) for the current user.
295 * @return array
296 */
297 abstract public function get_services( $filter = 'all', $_blog_id = false, $_user_id = false );
298
299 /**
300 * Whether the site has the feature flag enabled.
301 *
302 * @deprecated 0.69.1 Use Current_Plan::supports() directly instead.
303 *
304 * @todo Remove this method After March 2026.
305 *
306 * @param string $flag_name The feature flag to check. Will be prefixed with 'jetpack_social_has_' for the option.
307 * @param string $feature_name The feature name to check for for the Current_Plan check, without the social- prefix.
308 * @return bool
309 */
310 public function has_feature_flag( $flag_name, $feature_name ): bool {
311 return Publicize_Script_Data::has_feature_flag( $feature_name );
312 }
313
314 /**
315 * Does the given user have a connection to the service on the given blog?
316 *
317 * @param string $service_name 'facebook', 'twitter', etc.
318 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
319 * @param false|int $_user_id The user ID. Use false (default) for the current user.
320 * @return bool
321 */
322 public function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) {
323 if ( ! $_blog_id ) {
324 $_blog_id = $this->blog_id();
325 }
326
327 if ( ! $_user_id ) {
328 $_user_id = $this->user_id();
329 }
330
331 $connections = $this->get_connections( $service_name, $_blog_id, $_user_id );
332 return is_array( $connections ) && count( $connections ) > 0;
333 }
334
335 /**
336 * Generates a connection URL.
337 *
338 * This is the URL, which, when visited by the user, starts the authentication
339 * process required to forge a connection.
340 *
341 * @param string $service_name 'facebook', 'twitter', etc.
342 * @return string
343 */
344 abstract public function connect_url( $service_name );
345
346 /**
347 * Generates a Connection refresh URL.
348 *
349 * This is the URL, which, when visited by the user, re-authenticates their
350 * connection to the service.
351 *
352 * @param string $service_name 'facebook', 'twitter', etc.
353 * @return string
354 */
355 abstract public function refresh_url( $service_name );
356
357 /**
358 * Generates a disconnection URL.
359 *
360 * This is the URL, which, when visited by the user, breaks their connection
361 * with the service.
362 *
363 * @param string $service_name 'facebook', 'twitter', etc.
364 * @param string $connection_id Connection ID.
365 * @return string
366 */
367 abstract public function disconnect_url( $service_name, $connection_id );
368
369 /**
370 * Returns a display name for the Service
371 *
372 * @param string $service_name 'facebook', 'twitter', etc.
373 * @return string
374 */
375 public static function get_service_label( $service_name ) {
376 switch ( $service_name ) {
377 case 'linkedin':
378 return 'LinkedIn';
379 case 'google_drive': // google-drive used to be called google_drive.
380 case 'google-drive':
381 return 'Google Drive';
382 case 'instagram-business':
383 return 'Instagram';
384 case 'twitter':
385 case 'facebook':
386 case 'tumblr':
387 default:
388 return ucfirst( $service_name );
389 }
390 }
391
392 /**
393 * Connections: For each Service, there can be multiple connections
394 * for a given user. For example, one user could be connected to Twitter
395 * as both @jetpack and as @wordpressdotcom
396 *
397 * For historical reasons, Connections are represented as an object
398 * on WordPress.com and as an array in Jetpack.
399 */
400
401 /**
402 * Get the active Connections of a Service
403 *
404 * @param string $service_name 'facebook', 'twitter', etc.
405 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
406 * @param false|int $_user_id The user ID. Use false (default) for the current user.
407 * @return false|object[]|array[] false if no connections exist
408 */
409 abstract public function get_connections( $service_name, $_blog_id = false, $_user_id = false );
410
411 /**
412 * Get a single Connection of a Service
413 *
414 * @param string $service_name 'facebook', 'twitter', etc.
415 * @param string $connection_id Connection ID.
416 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
417 * @param false|int $_user_id The user ID. Use false (default) for the current user.
418 * @return false|object[]|array[] false if no connections exist
419 */
420 abstract public function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false );
421
422 /**
423 * Get the Connection ID.
424 *
425 * Note that this is different than the Connection's uniqueid.
426 *
427 * Via a quirk of history, ID is globally unique and unique_id
428 * is only unique per site.
429 *
430 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
431 * @return string
432 */
433 abstract public function get_connection_id( $connection );
434
435 /**
436 * Get the Connection unique_id
437 *
438 * Note that this is different than the Connections ID.
439 *
440 * Via a quirk of history, ID is globally unique and unique_id
441 * is only unique per site.
442 *
443 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
444 * @return string
445 */
446 abstract public function get_connection_unique_id( $connection );
447
448 /**
449 * Get the Connection's Meta data
450 *
451 * @param object|array $connection Connection.
452 * @return array Connection Meta
453 */
454 abstract public function get_connection_meta( $connection );
455
456 /**
457 * Disconnect a Connection
458 *
459 * @param string $service_name 'facebook', 'twitter', etc.
460 * @param string $connection_id Connection ID.
461 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
462 * @param false|int $_user_id The user ID. Use false (default) for the current user.
463 * @param bool $force_delete Whether to skip permissions checks.
464 * @return false|void False on failure. Void on success.
465 */
466 abstract public function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false );
467
468 /**
469 * Globalizes a Connection
470 *
471 * @param string $connection_id Connection ID.
472 * @return bool Falsey on failure. Truthy on success.
473 */
474 abstract public function globalize_connection( $connection_id );
475
476 /**
477 * Unglobalizes a Connection
478 *
479 * @param string $connection_id Connection ID.
480 * @return bool Falsey on failure. Truthy on success.
481 */
482 abstract public function unglobalize_connection( $connection_id );
483
484 /**
485 * Returns an external URL to the Connection's profile
486 *
487 * @param string $service_name 'facebook', 'twitter', etc.
488 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
489 * @return false|string False on failure. URL on success.
490 */
491 public function get_profile_link( $service_name, $connection ) {
492 $cmeta = $this->get_connection_meta( $connection );
493
494 if ( isset( $cmeta['connection_data']['meta']['link'] ) ) {
495 if ( 'facebook' === $service_name && str_starts_with( wp_parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) {
496 // App-scoped Facebook user IDs are not usable profile links.
497 return false;
498 }
499
500 return $cmeta['connection_data']['meta']['link'];
501 }
502
503 if ( 'facebook' === $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
504 return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page'];
505 }
506
507 if ( 'instagram-business' === $service_name && isset( $cmeta['connection_data']['meta']['username'] ) ) {
508 return 'https://instagram.com/' . $cmeta['connection_data']['meta']['username'];
509 }
510
511 if ( 'linkedin' === $service_name ) {
512
513 $entity_type = $cmeta['connection_data']['meta']['entity_type'] ?? 'person';
514
515 if ( 'organization' === $entity_type && ! empty( $cmeta['connection_data']['meta']['external_name'] ) ) {
516 return 'https://www.linkedin.com/company/' . $cmeta['connection_data']['meta']['external_name'];
517 }
518
519 if ( 'person' === $entity_type && ! empty( $cmeta['external_name'] ) ) {
520 return 'https://www.linkedin.com/in/' . $cmeta['external_name'];
521 }
522 }
523
524 if ( 'threads' === $service_name && isset( $cmeta['external_name'] ) ) {
525 return 'https://www.threads.net/@' . $cmeta['external_name'];
526 }
527
528 if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) {
529 return 'https://mastodon.social/@' . $cmeta['external_name'];
530 }
531
532 if ( 'nextdoor' === $service_name && isset( $cmeta['external_id'] ) ) {
533 return 'https://nextdoor.com/profile/' . $cmeta['external_id'];
534 }
535
536 if ( 'tumblr' === $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
537 return 'https://' . $cmeta['connection_data']['meta']['tumblr_base_hostname'];
538 }
539
540 if ( 'twitter' === $service_name ) {
541 return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'.
542 }
543
544 if ( 'bluesky' === $service_name ) {
545 return 'https://bsky.app/profile/' . $cmeta['external_id'];
546 }
547
548 return false; // no fallback. we just won't link it.
549 }
550
551 /**
552 * Returns a display name for the Connection
553 *
554 * @param string $service_name 'facebook', 'twitter', etc.
555 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
556 * @return string
557 */
558 public function get_display_name( $service_name, $connection ) {
559 $cmeta = $this->get_connection_meta( $connection );
560
561 if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) {
562 return $cmeta['external_display'];
563 }
564
565 if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
566 return $cmeta['connection_data']['meta']['display_name'];
567 }
568
569 if ( 'tumblr' === $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
570 return $cmeta['connection_data']['meta']['tumblr_base_hostname'];
571 }
572
573 if ( 'twitter' === $service_name ) {
574 return $cmeta['external_display'];
575 }
576
577 $connection_display = $cmeta['external_display'];
578
579 if ( empty( $connection_display ) ) {
580 $connection_display = $cmeta['external_name'];
581 }
582
583 return $connection_display;
584 }
585
586 /**
587 * Returns the account name for the Connection. For services like Instagram, we need both the username and the account's name.
588 *
589 * @param string $service_name 'facebook', 'linkedin', etc.
590 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
591 * @return string
592 */
593 public function get_username( $service_name, $connection ) {
594 $handle = $this->get_external_handle( $service_name, $connection );
595
596 return $handle ?? $this->get_display_name( $service_name, $connection );
597 }
598
599 /**
600 * Returns the external handle for the Connection.
601 *
602 * @param string $service_name 'facebook', 'linkedin', etc.
603 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
604 * @return string|null
605 */
606 public function get_external_handle( $service_name, $connection ) {
607 $cmeta = $this->get_connection_meta( $connection );
608
609 switch ( $service_name ) {
610 case 'mastodon':
611 return $cmeta['external_display'] ?? null;
612
613 case 'bluesky':
614 case 'threads':
615 return $cmeta['external_name'] ?? null;
616
617 case 'instagram-business':
618 return $cmeta['connection_data']['meta']['username'] ?? null;
619
620 default:
621 return null;
622 }
623 }
624
625 /**
626 * Returns a profile picture for the Connection
627 *
628 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
629 * @return string
630 */
631 public function get_profile_picture( $connection ) {
632 $cmeta = $this->get_connection_meta( $connection );
633
634 if ( isset( $cmeta['profile_picture'] ) ) {
635 return $cmeta['profile_picture'];
636 }
637
638 return '';
639 }
640
641 /**
642 * Whether the user needs to select additional options after connecting
643 *
644 * @param string $service_name 'facebook', 'twitter', etc.
645 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
646 * @return bool
647 */
648 public function show_options_popup( $service_name, $connection ) {
649 $cmeta = $this->get_connection_meta( $connection );
650
651 // Always show if no selection has been made for Facebook.
652 if ( 'facebook' === $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
653 return true;
654 }
655
656 // Always show if no selection has been made for Tumblr.
657 if ( 'tumblr' === $service_name && empty( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
658 return true;
659 }
660
661 // if we have the specific connection info..
662 $id = ! empty( $_GET['id'] ) ? sanitize_text_field( wp_unslash( $_GET['id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
663
664 if ( $id ) {
665 if ( $cmeta['connection_data']['id'] === $id ) {
666 return true;
667 }
668 } else {
669 // Otherwise, just show if this is the completed step / first load.
670 // phpcs:disable WordPress.Security.NonceVerification.Recommended
671 $is_completed = ! empty( $_GET['action'] ) && 'completed' === $_GET['action'];
672 $service = ! empty( $_GET['service'] ) ? sanitize_text_field( wp_unslash( $_GET['service'] ) ) : false;
673 // phpcs:enable WordPress.Security.NonceVerification.Recommended
674
675 if ( $is_completed && $service_name === $service && ! in_array( $service, array( 'facebook', 'tumblr' ), true ) ) {
676 return true;
677 }
678 }
679
680 return false;
681 }
682
683 /**
684 * Check if a connection is global
685 *
686 * @param array $connection Connection data.
687 * @return bool Whether the connection is global.
688 */
689 public function is_global_connection( $connection ) {
690 return empty( $connection['connection_data']['user_id'] );
691 }
692
693 /**
694 * Whether the Connection is "valid" wrt Facebook's requirements.
695 *
696 * Must be connected to a Page (not a Profile).
697 * (Also returns true if we're in the middle of the connection process)
698 *
699 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
700 * @return bool
701 */
702 public function is_valid_facebook_connection( $connection ) {
703 if ( $this->is_connecting_connection( $connection ) ) {
704 return true;
705 }
706 $connection_meta = $this->get_connection_meta( $connection );
707 $connection_data = $connection_meta['connection_data'];
708 return isset( $connection_data['meta']['facebook_page'] );
709 }
710
711 /**
712 * LinkedIn needs to be reauthenticated to use v2 of their API.
713 * If it's using LinkedIn old API, it's an 'invalid' connection
714 *
715 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
716 * @return bool
717 */
718 public function is_invalid_linkedin_connection( $connection ) {
719 // LinkedIn API v1 included the profile link in the connection data.
720 $connection_meta = $this->get_connection_meta( $connection );
721 return isset( $connection_meta['connection_data']['meta']['profile_url'] );
722 }
723
724 /**
725 * Whether the Connection currently being connected
726 *
727 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
728 * @return bool
729 */
730 public function is_connecting_connection( $connection ) {
731 $connection_meta = $this->get_connection_meta( $connection );
732 $connection_data = $connection_meta['connection_data'];
733 return isset( $connection_data['meta']['options_responses'] );
734 }
735
736 /**
737 * Parse the error code returned by the XML-RPC API call.
738 *
739 * @param string $code Error code in numerical format.
740 *
741 * @return string Error code.
742 */
743 public function parse_connection_error_code( $code ) {
744 if ( 1 === $code ) {
745 return 'unsupported';
746 }
747
748 return 'broken';
749 }
750
751 /**
752 * Run connection tests on all Connections
753 *
754 * @return array {
755 * Array of connection test results.
756 *
757 * @type string 'connectionID' Connection identifier string that is unique for each connection
758 * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...)
759 * @type bool 'connectionTestPassed' Whether the connection test was successful
760 * @type string 'connectionTestMessage' Test success or error message
761 * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service
762 * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service
763 * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service.
764 * @type string 'unique_id' ID string representing connection
765 * }
766 */
767 public function get_publicize_conns_test_results() {
768 $test_results = array();
769
770 foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
771 foreach ( $connections as $connection ) {
772
773 $id = $this->get_connection_id( $connection );
774
775 $connection_test_error_code = '';
776 $connection_test_passed = true;
777 $connection_test_message = __( 'This connection is working correctly.', 'jetpack-publicize-pkg' );
778 $user_can_refresh = false;
779 $refresh_text = '';
780 $refresh_url = '';
781
782 $connection_test_result = true;
783 if ( method_exists( $this, 'test_connection' ) ) {
784 $connection_test_result = $this->test_connection( $service_name, $connection );
785 }
786
787 if ( is_wp_error( $connection_test_result ) ) {
788 $connection_test_passed = false;
789 $connection_test_message = $connection_test_result->get_error_message();
790 $error_data = $connection_test_result->get_error_data();
791 $error_code = $connection_test_result->get_error_code();
792
793 if ( ! empty( $error_data ) ) {
794 $user_can_refresh = $error_data['user_can_refresh'];
795 $refresh_text = $error_data['refresh_text'];
796 $refresh_url = $error_data['refresh_url'];
797 }
798 $connection_test_error_code = $connection_test_passed ? '' : $this->parse_connection_error_code( $error_code );
799 }
800 // Mark Facebook profiles as deprecated.
801 if ( 'facebook' === $service_name ) {
802 if ( ! $this->is_valid_facebook_connection( $connection ) ) {
803 $connection_test_passed = false;
804 $user_can_refresh = false;
805 $connection_test_message = __( 'Please select a Facebook Page to publish updates.', 'jetpack-publicize-pkg' );
806 }
807 }
808
809 // LinkedIn needs reauthentication to be compatible with v2 of their API.
810 if ( 'linkedin' === $service_name && $this->is_invalid_linkedin_connection( $connection ) ) {
811 $connection_test_passed = 'must_reauth';
812 $user_can_refresh = false;
813 $connection_test_message = esc_html__( 'Your LinkedIn connection needs to be reauthenticated to continue working – head to Sharing to take care of it.', 'jetpack-publicize-pkg' );
814 }
815
816 $unique_id = null;
817
818 if ( ! empty( $connection->unique_id ) ) {
819 $unique_id = $connection->unique_id;
820 } elseif ( ! empty( $connection['connection_data']['token_id'] ) ) {
821 $unique_id = $connection['connection_data']['token_id'];
822 }
823
824 $test_results[] = array(
825 'connectionID' => $id,
826 'serviceName' => $service_name,
827 'connectionTestPassed' => $connection_test_passed,
828 'connectionTestErrorCode' => $connection_test_error_code,
829 'connectionTestMessage' => esc_attr( $connection_test_message ),
830 'userCanRefresh' => $user_can_refresh,
831 'refreshText' => esc_attr( $refresh_text ),
832 'refreshURL' => $refresh_url,
833 'unique_id' => $unique_id,
834 );
835 }
836 }
837
838 return $test_results;
839 }
840
841 /**
842 * Run the connection test for the Connection
843 *
844 * @param string $service_name $service_name 'facebook', 'twitter', etc.
845 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
846 * @return WP_Error|true WP_Error on failure. True on success
847 */
848 abstract public function test_connection( $service_name, $connection );
849
850 /**
851 * Retrieves current list of connections and applies filters.
852 *
853 * Retrieves current available connections and checks if the connections
854 * have already been used to share current post. Finally, the checkbox
855 * form UI fields are calculated. This function exposes connection form
856 * data directly as array so it can be retrieved for static HTML generation
857 * or JSON consumption.
858 *
859 * @since 0.1.0
860 * @since-jetpack 6.7.0
861 *
862 * @param integer $selected_post_id Optional. Post ID to query connection status for.
863 *
864 * @return array {
865 * Array of UI setup data for connection list form.
866 *
867 * @type string 'unique_id' ID string representing connection
868 * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...)
869 * @type string 'service_label' Service Label (Facebook, Twitter, ...)
870 * @type string 'display_name' Connection's human-readable Username: "@jetpack"
871 * @type string 'profile_picture' Connection profile picture.
872 * @type bool 'enabled' Default value for the connection (e.g., for a checkbox).
873 * @type bool 'done' Has this connection already been publicized to?
874 * @type bool 'toggleable' Is the user allowed to change the value for the connection?
875 * @type bool 'global' Is this connection a global one?
876 * @type string 'external_id' External ID for the connection.
877 * }
878 */
879 public function get_filtered_connection_data( $selected_post_id = null ) {
880 $connection_list = array();
881
882 $post = get_post( $selected_post_id ); // Defaults to current post if $post_id is null.
883 // Handle case where there is no current post.
884 if ( ! empty( $post ) ) {
885 $post_id = $post->ID;
886 } else {
887 $post_id = null;
888 }
889
890 // We don't allow Publicizing to the same external id twice, to prevent spam.
891 $service_id_done = (array) get_post_meta( $post_id, $this->POST_SERVICE_DONE, true );
892
893 $connections = Connections::get_all_for_user();
894
895 if ( ! empty( $connections ) ) {
896
897 foreach ( $connections as $connection ) {
898 $service_name = $connection['service_name'];
899 $unique_id = $connection['id'];
900 $connection_id = $connection['connection_id'];
901 // Was this connection (OR, old-format service) already Publicized to?
902 $done = ! empty( $post ) && (
903 // Flags based on token_id.
904 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $unique_id, true )
905 ||
906 // Old flags.
907 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $service_name, true )
908 );
909
910 /**
911 * Filter whether a post should be publicized to a given service.
912 *
913 * @since 0.1.0
914 * @since-jetpack 2.0.0
915 *
916 * @param bool true Should the post be publicized to a given service? Default to true.
917 * @param int $post_id Post ID.
918 * @param string $service_name Service name.
919 * @param array $connection The connection data.
920 */
921 /* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
922 if ( ! apply_filters( 'wpas_submit_post?', true, $post_id, $service_name, $connection ) ) {
923 continue;
924 }
925
926 // Should we be skipping this one?
927 $skip = (
928 (
929 ! empty( $post )
930 &&
931 in_array( $post->post_status, array( 'publish', 'draft', 'future' ), true )
932 &&
933 (
934 // Flags based on token_id.
935 get_post_meta( $post->ID, $this->POST_SKIP . $unique_id, true )
936 ||
937 // Flags based on the connection_id.
938 get_post_meta( $post->ID, $this->POST_SKIP_PUBLICIZE . $connection_id, true )
939 ||
940 // Old flags.
941 get_post_meta( $post->ID, $this->POST_SKIP . $service_name, false )
942 )
943 )
944 ||
945 (
946 isset( $connection['external_id'] ) && ! empty( $service_id_done[ $service_name ][ $connection['external_id'] ] )
947 )
948 );
949
950 // Determine the state of the checkbox (on/off) and allow filtering.
951 $enabled = $done || ! $skip;
952 /**
953 * Filter the checkbox state of each Publicize connection appearing in the post editor.
954 *
955 * @since 0.1.0
956 * @since-jetpack 2.0.1
957 *
958 * @param bool $enabled Should the Publicize checkbox be enabled for a given service.
959 * @param int $post_id Post ID.
960 * @param string $service_name Service name.
961 * @param array $connection Array of connection details.
962 */
963 $enabled = apply_filters( 'publicize_checkbox_default', $enabled, $post_id, $service_name, $connection );
964
965 /**
966 * If this is a shared connection and this user doesn't have enough permissions to modify.
967 */
968 if ( ! $done && Connections::is_shared( $connection ) && ! current_user_can( $this->GLOBAL_CAP ) ) {
969 /**
970 * Filters the checkboxes for global connections with non-prilvedged users.
971 *
972 * @since 0.1.0
973 * @since-jetpack 3.7.0
974 *
975 * @param bool $enabled Indicates if this connection should be enabled. Default true.
976 * @param int $post_id ID of the current post
977 * @param string $service_name Name of the connection (Facebook, Twitter, etc)
978 * @param array $connection Array of data about the connection.
979 */
980 $enabled = apply_filters( 'publicize_checkbox_global_default', $enabled, $post_id, $service_name, $connection );
981 }
982
983 // Force the checkbox to be checked if the post was DONE, regardless of what the filter does.
984 if ( $done ) {
985 $enabled = true;
986 }
987
988 $connection_list[] = array_merge(
989 $connection,
990 array(
991 'enabled' => $enabled,
992 'done' => $done,
993 )
994 );
995 }
996 }
997
998 return $connection_list;
999 }
1000
1001 /**
1002 * Checks if post has already been shared by Publicize in the past.
1003 *
1004 * @since 0.1.0
1005 * @since-jetpack 6.7.0
1006 *
1007 * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing.
1008 *
1009 * @return bool True if post has already been shared by Publicize, false otherwise.
1010 */
1011 abstract public function post_is_done_sharing( $post_id = null );
1012
1013 /**
1014 * Retrieve the available Publicize services and their connect URLs.
1015 *
1016 * @since 0.1.0
1017 * @since-jetpack 6.7.0
1018 * @deprecated 0.88.0 Services and their connect URLs come from the `publicize/services` REST endpoint.
1019 *
1020 * @return array Always empty.
1021 */
1022 public function get_available_service_data() {
1023 _deprecated_function( __METHOD__, 'publicize-0.88.0' );
1024
1025 return array();
1026 }
1027
1028 /**
1029 * Site Data
1030 */
1031
1032 /**
1033 * Get user ID.
1034 *
1035 * @return int The current user's ID, or 0 if no user is logged in.
1036 */
1037 public function user_id() {
1038 return get_current_user_id();
1039 }
1040
1041 /**
1042 * Get site ID.
1043 *
1044 * @return int Site ID.
1045 */
1046 public function blog_id() {
1047 return get_current_blog_id();
1048 }
1049
1050 /**
1051 * Posts
1052 */
1053
1054 /**
1055 * Checks old and new status to see if the post should be flagged as
1056 * ready to Publicize.
1057 *
1058 * Attached to the `transition_post_status` filter.
1059 *
1060 * @param string $new_status New status.
1061 * @param string $old_status Old status.
1062 * @param WP_Post $post Post object.
1063 * @return void
1064 */
1065 abstract public function flag_post_for_publicize( $new_status, $old_status, $post );
1066
1067 /**
1068 * Ensures the Post internal post-type supports `publicize`
1069 *
1070 * This feature support flag is used by the REST API.
1071 */
1072 public function add_post_type_support() {
1073 add_post_type_support( 'post', 'publicize' );
1074 }
1075
1076 /**
1077 * Can the current user access Publicize Data.
1078 *
1079 * @param int $post_id 0 for general access. Post_ID for specific access.
1080 * @return bool
1081 */
1082 public function current_user_can_access_publicize_data( $post_id = 0 ) {
1083 /**
1084 * Filter what user capability is required to use the publicize form on the edit post page. Useful if publish post capability has been removed from role.
1085 *
1086 * @since 0.1.0
1087 * @since-jetpack 4.1.0
1088 *
1089 * @param string $capability User capability needed to use publicize
1090 */
1091 $capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' );
1092
1093 if ( 'publish_posts' === $capability && $post_id ) {
1094 return current_user_can( 'publish_post', $post_id );
1095 }
1096
1097 return current_user_can( $capability );
1098 }
1099
1100 /**
1101 * Auth callback for the protected ->POST_MESS post_meta
1102 *
1103 * @param int $object_id Post ID.
1104 * @return bool
1105 */
1106 public function message_meta_auth_callback( $object_id ) {
1107 return $this->current_user_can_access_publicize_data( $object_id );
1108 }
1109
1110 /**
1111 * Registers the post_meta for use in the REST API.
1112 *
1113 * Registers for each post type that with `publicize` feature support.
1114 */
1115 public function register_post_meta() {
1116 /*
1117 * Default the share-message meta to the saved global template
1118 */
1119 $message_default = $this->has_paid_features()
1120 ? ( new Jetpack_Social_Settings\Settings() )->get_message_template()
1121 : '';
1122
1123 $message_args = array(
1124 'type' => 'string',
1125 'description' => __( 'The message to use instead of the title when sharing to Jetpack Social services', 'jetpack-publicize-pkg' ),
1126 'single' => true,
1127 'default' => $message_default,
1128 'show_in_rest' => array(
1129 'name' => 'jetpack_publicize_message',
1130 ),
1131 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1132 );
1133
1134 $publicize_feature_enable_args = array(
1135 'type' => 'boolean',
1136 'description' => __( 'Whether or not the Share Post feature is enabled.', 'jetpack-publicize-pkg' ),
1137 'single' => true,
1138 'default' => true,
1139 'show_in_rest' => array(
1140 'name' => 'jetpack_publicize_feature_enabled',
1141 ),
1142 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1143 );
1144
1145 $already_shared_flag_args = array(
1146 'type' => 'boolean',
1147 'description' => __( 'Whether or not the post has already been shared.', 'jetpack-publicize-pkg' ),
1148 'single' => true,
1149 'default' => false,
1150 'show_in_rest' => array(
1151 'name' => 'jetpack_social_post_already_shared',
1152 ),
1153 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1154 );
1155
1156 $jetpack_social_options_args = array(
1157 'type' => 'object',
1158 'description' => __( 'Post options related to Jetpack Social.', 'jetpack-publicize-pkg' ),
1159 'single' => true,
1160 'default' => array(
1161 'image_generator_settings' => array(
1162 'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(),
1163 'default_image_id' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_image_id(),
1164 'font' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_font(),
1165 'enabled' => false,
1166 ),
1167 'version' => 2,
1168 ),
1169 'show_in_rest' => array(
1170 'name' => 'jetpack_social_options',
1171 'schema' => array(
1172 'type' => 'object',
1173 'properties' => array(
1174 'version' => array(
1175 'type' => 'number',
1176 ),
1177 'attached_media' => array(
1178 'type' => 'array',
1179 'items' => array(
1180 'type' => 'object',
1181 'properties' => array(
1182 'id' => array(
1183 'type' => 'number',
1184 ),
1185 'url' => array(
1186 'type' => 'string',
1187 ),
1188 'type' => array(
1189 'type' => 'string',
1190 ),
1191 ),
1192 ),
1193 ),
1194 'image_generator_settings' => array(
1195 'type' => 'object',
1196 'properties' => array(
1197 'enabled' => array(
1198 'type' => 'boolean',
1199 ),
1200 'custom_text' => array(
1201 'type' => 'string',
1202 ),
1203 'image_type' => array(
1204 'type' => 'string',
1205 ),
1206 'image_id' => array(
1207 'type' => 'number',
1208 ),
1209 'template' => array(
1210 'type' => 'string',
1211 ),
1212 'font' => array(
1213 'type' => 'string',
1214 ),
1215 'token' => array(
1216 'type' => 'string',
1217 ),
1218 'default_image_id' => array(
1219 'type' => 'number',
1220 ),
1221 ),
1222 ),
1223 'media_source' => array(
1224 'type' => 'string',
1225 'enum' => array( 'featured-image', 'sig', 'media-library', 'upload-video', 'none' ),
1226 ),
1227 ),
1228 ),
1229 ),
1230 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1231 );
1232
1233 $image_focal_point_args = array(
1234 'type' => 'object',
1235 'description' => __( 'The focal point of the image, used to crop social share variants.', 'jetpack-publicize-pkg' ),
1236 'single' => true,
1237 'default' => array(
1238 'x' => 0.5,
1239 'y' => 0.5,
1240 ),
1241 'show_in_rest' => array(
1242 'schema' => array(
1243 'type' => 'object',
1244 'required' => array( 'x', 'y' ),
1245 'properties' => array(
1246 'x' => array(
1247 'type' => 'number',
1248 'minimum' => 0,
1249 'maximum' => 1,
1250 ),
1251 'y' => array(
1252 'type' => 'number',
1253 'minimum' => 0,
1254 'maximum' => 1,
1255 ),
1256 ),
1257 'additionalProperties' => false,
1258 ),
1259 ),
1260 'auth_callback' => array( $this, 'image_focal_point_auth_callback' ),
1261 );
1262
1263 $connection_overrides_args = array(
1264 'type' => 'object',
1265 'description' => __( 'Per-connection customizations for message and media.', 'jetpack-publicize-pkg' ),
1266 'single' => true,
1267 'default' => array(),
1268 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1269 );
1270
1271 $customize_per_network_default = (
1272 $this->has_paid_features()
1273 && $this->any_connection_has_custom_template()
1274 );
1275
1276 $customize_per_network_args = array(
1277 'type' => 'boolean',
1278 'description' => __( 'Whether to enable per-network customization.', 'jetpack-publicize-pkg' ),
1279 'single' => true,
1280 'default' => $customize_per_network_default,
1281 'show_in_rest' => $this->has_paid_features(),
1282 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1283 );
1284
1285 foreach ( get_post_types() as $post_type ) {
1286 if ( ! $this->post_type_is_publicizeable( $post_type ) ) {
1287 continue;
1288 }
1289
1290 $message_args['object_subtype'] = $post_type;
1291 $publicize_feature_enable_args['object_subtype'] = $post_type;
1292 $already_shared_flag_args['object_subtype'] = $post_type;
1293 $jetpack_social_options_args['object_subtype'] = $post_type;
1294 $connection_overrides_args['object_subtype'] = $post_type;
1295 $customize_per_network_args['object_subtype'] = $post_type;
1296
1297 register_meta( 'post', $this->POST_MESS, $message_args );
1298 register_meta( 'post', self::POST_PUBLICIZE_FEATURE_ENABLED, $publicize_feature_enable_args );
1299 register_meta( 'post', $this->POST_DONE . 'all', $already_shared_flag_args );
1300 register_meta( 'post', self::POST_JETPACK_SOCIAL_OPTIONS, $jetpack_social_options_args );
1301 register_meta( 'post', self::POST_CONNECTION_OVERRIDES, $connection_overrides_args );
1302 register_meta( 'post', self::POST_CUSTOMIZE_PER_NETWORK, $customize_per_network_args );
1303 }
1304
1305 // The focal point lives on the image (attachment), not the post, so it is shared
1306 // by every post that uses the image. Registered once, not per publicizeable type.
1307 register_post_meta( 'attachment', self::ATTACHMENT_IMAGE_FOCAL_POINT, $image_focal_point_args );
1308 }
1309
1310 /**
1311 * Auth callback for the image focal point attachment meta.
1312 *
1313 * Writing the focal point edits the image, so it requires edit rights on the attachment.
1314 *
1315 * @param bool $allowed Whether the user can edit the meta. Unused; recomputed here.
1316 * @param string $meta_key The meta key. Unused.
1317 * @param int $object_id The attachment ID.
1318 * @return bool
1319 */
1320 public function image_focal_point_auth_callback( $allowed, $meta_key, $object_id ) {
1321 return current_user_can( 'edit_post', $object_id );
1322 }
1323
1324 /**
1325 * Whether any connection available to the current user has a custom message template.
1326 *
1327 * @return bool
1328 */
1329 protected function any_connection_has_custom_template() {
1330 foreach ( Connections::get_all_for_user() as $connection ) {
1331 if ( '' !== trim( (string) ( $connection['template'] ?? '' ) ) ) {
1332 return true;
1333 }
1334 }
1335
1336 return false;
1337 }
1338
1339 /**
1340 * Helper function to allow us to not publicize posts in certain contexts.
1341 *
1342 * @param WP_Post $post Post object.
1343 */
1344 public function should_submit_post_pre_checks( $post ) {
1345 $submit_post = true;
1346
1347 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
1348 $submit_post = false;
1349 }
1350
1351 if (
1352 defined( 'DOING_AUTOSAVE' )
1353 &&
1354 DOING_AUTOSAVE
1355 ) {
1356 $submit_post = false;
1357 }
1358
1359 // To stop quick edits from getting publicized.
1360 if ( did_action( 'wp_ajax_inline-save' ) ) {
1361 $submit_post = false;
1362 }
1363
1364 // phpcs:disable WordPress.Security.NonceVerification.Recommended
1365 if ( ! empty( $_GET['bulk_edit'] ) ) {
1366 $submit_post = false;
1367 }
1368 // phpcs:enable WordPress.Security.NonceVerification.Recommended
1369
1370 // - API/XML-RPC Test Posts
1371 if (
1372 (
1373 ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
1374 ||
1375 ( defined( 'APP_REQUEST' ) && constant( 'APP_REQUEST' ) )
1376 )
1377 &&
1378 str_starts_with( $post->post_title, 'Temporary Post Used For Theme Detection' )
1379 ) {
1380 $submit_post = false;
1381 }
1382
1383 // Only work with certain statuses (avoids inherits, auto drafts etc).
1384 if ( ! in_array( $post->post_status, array( 'publish', 'draft', 'future' ), true ) ) {
1385 $submit_post = false;
1386 }
1387
1388 // Don't publish password protected posts.
1389 if ( '' !== $post->post_password ) {
1390 $submit_post = false;
1391 }
1392
1393 return $submit_post;
1394 }
1395
1396 /**
1397 * Fires when a post is saved, checks conditions and saves state in postmeta so that it
1398 * can be picked up later by @see ::publicize_post() on WordPress.com codebase.
1399 *
1400 * Attached to the `save_post` action.
1401 *
1402 * @param int $post_id Post ID.
1403 * @param WP_Post $post Post object.
1404 */
1405 public function save_meta( $post_id, $post ) {
1406 $cron_user = null;
1407 $submit_post = true;
1408
1409 if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) {
1410 return;
1411 }
1412
1413 $submit_post = $this->should_submit_post_pre_checks( $post );
1414
1415 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're only checking if a value is set
1416 $admin_page = $_POST[ $this->ADMIN_PAGE ] ?? null;
1417
1418 // Did this request happen via wp-admin?
1419 $from_web = isset( $_SERVER['REQUEST_METHOD'] )
1420 &&
1421 'post' === strtolower( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) )
1422 &&
1423 ! empty( $admin_page );
1424
1425 // phpcs:ignore WordPress.Security.NonceVerification.Missing
1426 $title = isset( $_POST['wpas_title'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpas_title'] ) ) : null;
1427
1428 if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && ! empty( $title ) ) {
1429 update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) );
1430 }
1431
1432 // Change current user to provide context for get_services() if we're running during cron.
1433 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
1434 $cron_user = (int) $GLOBALS['user_ID'];
1435 wp_set_current_user( $post->post_author );
1436 }
1437
1438 /**
1439 * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered,
1440 * it will Publicize to everything *except* those marked for skipping.
1441 */
1442 foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
1443 foreach ( $connections as $connection ) {
1444 $connection_data = '';
1445 if ( is_object( $connection ) && method_exists( $connection, 'get_meta' ) ) {
1446 $connection_data = $connection->get_meta( 'connection_data' );
1447 } elseif ( ! empty( $connection['connection_data'] ) ) {
1448 $connection_data = $connection['connection_data'];
1449 }
1450
1451 /** This action is documented in modules/publicize/ui.php */
1452 /* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
1453 if ( false === apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) {
1454 delete_post_meta( $post_id, $this->PENDING );
1455 continue;
1456 }
1457
1458 // This was a wp-admin request, so we need to check the state of checkboxes.
1459 if ( $from_web ) {
1460 $connection_id = $this->get_connection_id( $connection );
1461 // Delete stray service-based post meta.
1462 delete_post_meta( $post_id, $this->POST_SKIP . $service_name );
1463
1464 // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition.
1465 if ( empty( $admin_page['submit'][ $connection_id ] ) ) {
1466 // Also make sure that the service-specific input isn't there.
1467 // If the user connected to a new service 'in-page' then a hidden field with the service
1468 // name is added, so we just assume they wanted to Publicize to that service.
1469 if ( empty( $admin_page['submit'][ $service_name ] ) ) {
1470 // Nothing seems to be checked, so we're going to mark this one to be skipped.
1471 update_post_meta( $post_id, $this->POST_SKIP_PUBLICIZE . $connection_id, 1 );
1472 continue;
1473 } else {
1474 // Clean up any stray post meta.
1475 delete_post_meta( $post_id, $this->POST_SKIP_PUBLICIZE . $connection_id );
1476 }
1477 } else {
1478 // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it.
1479 delete_post_meta( $post_id, $this->POST_SKIP_PUBLICIZE . $connection_id );
1480 }
1481 }
1482
1483 /**
1484 * Fires right before the post is processed for Publicize.
1485 * Users may hook in here and do anything else they need to after meta is written,
1486 * and before the post is processed for Publicize.
1487 *
1488 * @since 0.1.0
1489 * @since-jetpack 2.1.2
1490 *
1491 * @param bool $submit_post Should the post be publicized.
1492 * @param int $post->ID Post ID.
1493 * @param string $service_name Service name.
1494 * @param array $connection Array of connection details.
1495 */
1496 do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection );
1497 }
1498 }
1499
1500 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
1501 wp_set_current_user( $cron_user );
1502 }
1503
1504 // Next up will be ::publicize_post().
1505 }
1506
1507 /**
1508 * Alters the "Post Published" message to include information about where the post
1509 * was Publicized to.
1510 *
1511 * Attached to the `post_updated_messages` filter
1512 *
1513 * @param string[] $messages Array of messages.
1514 * @return string[]
1515 */
1516 public function update_published_message( $messages ) {
1517 global $post_type, $post_type_object, $post;
1518 if ( ! $this->post_type_is_publicizeable( $post_type ) ) {
1519 return $messages;
1520 }
1521
1522 // Bail early if the post is private.
1523 if ( 'publish' !== $post->post_status ) {
1524 return $messages;
1525 }
1526
1527 $view_post_link_html = '';
1528 $viewable = is_post_type_viewable( $post_type_object );
1529 if ( $viewable ) {
1530 /* phpcs:ignore WordPress.WP.I18n.MissingArgDomain, WordPress.Utils.I18nTextDomainFixer.MissingArgDomain */
1531 $view_text = esc_html__( 'View post' ); // Intentionally omitted domain.
1532
1533 if ( 'jetpack-portfolio' === $post_type ) {
1534 $view_text = esc_html__( 'View project', 'jetpack-publicize-pkg' );
1535 }
1536
1537 $view_post_link_html = sprintf(
1538 ' <a href="%1$s">%2$s</a>',
1539 esc_url( get_permalink( $post ) ),
1540 $view_text
1541 );
1542 }
1543
1544 $services = $this->get_publicizing_services( $post->ID );
1545 if ( empty( $services ) ) {
1546 return $messages;
1547 }
1548
1549 $labels = array();
1550 foreach ( $services as $service_name => $display_names ) {
1551 $labels[] = sprintf(
1552 /* translators: Service name is %1$s, and account name is %2$s. */
1553 esc_html__( '%1$s (%2$s)', 'jetpack-publicize-pkg' ),
1554 esc_html( $service_name ),
1555 esc_html( is_array( $display_names ) ? implode( ', ', $display_names ) : $display_names )
1556 );
1557 }
1558
1559 $messages['post'][6] = sprintf(
1560 /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack) */
1561 esc_html__( 'Post published and sharing on %1$s.', 'jetpack-publicize-pkg' ),
1562 implode( ', ', $labels )
1563 ) . $view_post_link_html;
1564
1565 if ( 'post' === $post_type && class_exists( 'Jetpack_Subscriptions' ) ) {
1566 $subscription = \Jetpack_Subscriptions::init();
1567 if ( $subscription->should_email_post_to_subscribers( $post ) ) {
1568 $messages['post'][6] = sprintf(
1569 /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack) */
1570 esc_html__( 'Post published, sending emails to subscribers and sharing post on %1$s.', 'jetpack-publicize-pkg' ),
1571 implode( ', ', $labels )
1572 ) . $view_post_link_html;
1573 }
1574 }
1575
1576 $messages['jetpack-portfolio'][6] = sprintf(
1577 /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack) */
1578 esc_html__( 'Project published and sharing project on %1$s.', 'jetpack-publicize-pkg' ),
1579 implode( ', ', $labels )
1580 ) . $view_post_link_html;
1581
1582 return $messages;
1583 }
1584
1585 /**
1586 * Get the Connections the Post was just Publicized to.
1587 *
1588 * Only reliable just after the Post was published.
1589 *
1590 * @param int $post_id Post ID.
1591 * @return string[] Array of Service display name => Connection display name
1592 */
1593 public function get_publicizing_services( $post_id ) {
1594 $services = array();
1595
1596 foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
1597 // services have multiple connections.
1598 foreach ( $connections as $connection ) {
1599 $connection_id = $this->get_connection_id( $connection );
1600 // Did we skip this connection?
1601 if ( get_post_meta( $post_id, $this->POST_SKIP_PUBLICIZE . $connection_id, true ) ) {
1602 continue;
1603 }
1604 $services[ static::get_service_label( $service_name ) ][] = $this->get_display_name( $service_name, $connection );
1605 }
1606 }
1607
1608 return $services;
1609 }
1610
1611 /**
1612 * Is the post Publicize-able?
1613 *
1614 * Only valid prior to Publicizing a Post.
1615 *
1616 * @param WP_Post $post Post to check.
1617 * @return bool
1618 */
1619 public function post_is_publicizeable( $post ) {
1620 if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) {
1621 return false;
1622 }
1623
1624 // This is more a precaution. To only publicize posts that are published. (Mostly relevant for Jetpack sites).
1625 if ( 'publish' !== $post->post_status ) {
1626 return false;
1627 }
1628
1629 // If it's not flagged as ready, then abort. @see ::flag_post_for_publicize().
1630 if ( ! get_post_meta( $post->ID, $this->PENDING, true ) ) {
1631 return false;
1632 }
1633
1634 return true;
1635 }
1636
1637 /**
1638 * Is a given post type Publicize-able?
1639 *
1640 * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via
1641 * the publicize_post_types array filter.
1642 *
1643 * @param string $post_type The post type to check.
1644 * @return bool True if the post type can be Publicized.
1645 */
1646 public function post_type_is_publicizeable( $post_type ) {
1647 if ( 'post' === $post_type ) {
1648 return true;
1649 }
1650
1651 return post_type_supports( $post_type, 'publicize' );
1652 }
1653
1654 /**
1655 * Already-published posts should not be Publicized by default. This filter sets checked to
1656 * false if a post has already been published.
1657 *
1658 * Attached to the `publicize_checkbox_default` filter
1659 *
1660 * @param bool $checked True if checkbox is checked, false otherwise.
1661 * @param int $post_id Post ID to set checkbox for.
1662 * @return bool
1663 */
1664 public function publicize_checkbox_default( $checked, $post_id ) {
1665 if ( 'publish' === get_post_status( $post_id ) ) {
1666 return false;
1667 }
1668
1669 return $checked;
1670 }
1671
1672 /**
1673 * Get the attached image for a post.
1674 *
1675 * @param int $post_id ID of the post to get attached media for.
1676 * @return array {
1677 * Array of image data, or empty array if no image is available.
1678 *
1679 * @type string $url Image source URL.
1680 * @type int $width Image width in pixels.
1681 * @type int $height Image height in pixels.
1682 * }
1683 */
1684 public function get_attached_media_image( $post_id ) {
1685 $options = get_post_meta( $post_id, self::POST_JETPACK_SOCIAL_OPTIONS, true );
1686
1687 if ( ! is_array( $options ) || empty( $options['attached_media'] ) || empty( $options['attached_media'][0]['id'] ) ) {
1688 return array();
1689 }
1690
1691 $media_id = $options['attached_media'][0]['id'];
1692
1693 if ( ! wp_attachment_is_image( $media_id ) ) {
1694 return array();
1695 }
1696
1697 $image = wp_get_attachment_image_src( $media_id, array( 1200, 1200 ) );
1698
1699 if ( ! $image ) {
1700 return array();
1701 }
1702
1703 return array(
1704 'url' => $image[0],
1705 'width' => $image[1],
1706 'height' => $image[2],
1707 );
1708 }
1709
1710 /**
1711 * Get the OpenGraph image for a post.
1712 *
1713 * @param int $post_id ID of the post to get OpenGraph image for.
1714 * @return array {
1715 * Array of image data, or empty array if no image is available.
1716 *
1717 * @type string $url Image source URL.
1718 * @type int $width Image width in pixels.
1719 * @type int $height Image height in pixels.
1720 * }
1721 */
1722 public function get_social_opengraph_image( $post_id ) {
1723 $generated_image_url = Social_Image_Generator\get_image_url( $post_id );
1724
1725 if ( ! empty( $generated_image_url ) ) {
1726 return array(
1727 'url' => $generated_image_url,
1728 'width' => 1200,
1729 'height' => 630,
1730 );
1731 }
1732
1733 $attached_media = $this->get_attached_media_image( $post_id );
1734
1735 if ( $attached_media ) {
1736 return $attached_media;
1737 }
1738
1739 $featured_image_id = get_post_thumbnail_id( $post_id );
1740
1741 if ( $featured_image_id && Current_Plan::supports( 'social-image-focal-point' ) ) {
1742 $featured_image = Focal_Point::get_cropped_image( $featured_image_id );
1743
1744 if ( $featured_image ) {
1745 return $featured_image;
1746 }
1747 }
1748
1749 return array();
1750 }
1751
1752 /**
1753 * Returns the image size in bytes of a remote image.
1754 *
1755 * @deprecated 0.66.3
1756 *
1757 * @param string $image_url Image URL.
1758 * @return integer|null $bytes Image size in bytes, or null if request failed.
1759 */
1760 public function get_remote_filesize( $image_url ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1761 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1762
1763 return null;
1764 }
1765
1766 /**
1767 * Returns the resized Photon URL for a given image.
1768 *
1769 * @deprecated 0.66.3
1770 *
1771 * @param string $image_url Image URL.
1772 * @param int $width Image width.
1773 * @param int $height Image height.
1774 * @return string
1775 */
1776 public function get_resized_image_url( $image_url, $width, $height ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1777 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1778
1779 return $image_url;
1780 }
1781
1782 /**
1783 * This function runs the image through Site Accelerator to compress it, and also scales it down if needed.
1784 *
1785 * @deprecated 0.66.3
1786 *
1787 * @param string $url Image URL.
1788 * @param int $width Image width.
1789 * @param int $height Image height.
1790 * @return array The compressed image data.
1791 */
1792 public function compress_and_scale_og_image( $url, $width, $height ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1793 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1794
1795 return array();
1796 }
1797
1798 /**
1799 * Reduce the filesize of an image by reducing the dimensions. Uses Photon.
1800 * Returns null if the image cannot be reduced enough.
1801 *
1802 * @deprecated 0.66.3
1803 *
1804 * @param string $url Image URL.
1805 * @param int $width Image width.
1806 * @param int $height Image height.
1807 * @param int $filesize Image filesize.
1808 * @param int $tries Number of times to try reducing the image size. Default is 5.
1809 * @return array|null
1810 */
1811 public function reduce_file_size( $url, $width, $height, $filesize, $tries = 5 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1812 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1813
1814 return null;
1815 }
1816
1817 /**
1818 * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags,
1819 * or to make the Jetpack open graph images pass restrictions.
1820 *
1821 * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1822 *
1823 * @param array $tags Current tags.
1824 *
1825 * @return array The modified tags.
1826 */
1827 public function jetpack_social_open_graph_filter( $tags ) {
1828 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1829 return $tags;
1830 }
1831
1832 /**
1833 * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1834 *
1835 * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1836 *
1837 * @param array $tags Current tags.
1838 * @param array $opengraph_image The Jetpack Social image data.
1839 *
1840 * @return array The modified tags.
1841 */
1842 public function add_jetpack_social_og_image( $tags, $opengraph_image ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1843 _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1844
1845 return $tags;
1846 }
1847
1848 /**
1849 * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1850 *
1851 * @param array $tags Current tags.
1852 */
1853 public function add_jetpack_social_og_images( $tags ) {
1854 if ( ! is_singular() ) {
1855 return $tags;
1856 }
1857
1858 $post_id = get_the_ID();
1859 if ( ! $post_id ) {
1860 return $tags;
1861 }
1862
1863 if ( ! post_type_supports( get_post_type( $post_id ), 'publicize' ) ) {
1864 return $tags;
1865 }
1866
1867 $opengraph_image = $this->get_social_opengraph_image( $post_id );
1868 if ( empty( $opengraph_image ) ) {
1869 return $tags;
1870 }
1871
1872 // If this code is running in Jetpack, we need to add Twitter cards.
1873 // Some active plugins disable Jetpack's Twitter Cards, so we need
1874 // to check if the class was instantiated before adding the cards.
1875 $needs_twitter_cards = class_exists( \Automattic\Jetpack\Post_Media\Twitter_Cards::class );
1876
1877 return array_merge(
1878 $tags,
1879 array(
1880 'og:image' => $opengraph_image['url'],
1881 'og:image:width' => $opengraph_image['width'],
1882 'og:image:height' => $opengraph_image['height'],
1883 ),
1884 $needs_twitter_cards ? array(
1885 'twitter:image' => $opengraph_image['url'],
1886 'twitter:card' => 'summary_large_image',
1887 ) : array()
1888 );
1889 }
1890
1891 /**
1892 * Util
1893 */
1894
1895 /**
1896 * Converts a Publicize message template string into a sprintf format string
1897 *
1898 * @param string[] $args Array of arguments.
1899 * 0 - The Publicize message template: 'Check out my post: %title% @ %url'
1900 * ... - The template tags 'title', 'url', etc.
1901 * @return string
1902 */
1903 protected static function build_sprintf( $args ) {
1904 $string = null;
1905 $search = array();
1906 $replace = array();
1907 foreach ( $args as $k => $arg ) {
1908 if ( 0 === $k ) {
1909 $string = $arg;
1910 continue;
1911 }
1912 $search[] = "%$arg%";
1913 $replace[] = "%$k\$s";
1914 }
1915 return str_replace( $search, $replace, $string );
1916 }
1917
1918 /**
1919 * Get the URL to the connections management page.
1920 *
1921 * @return string
1922 */
1923 public function publicize_connections_url() {
1924 $has_social_admin_page = defined( 'JETPACK_SOCIAL_PLUGIN_DIR' );
1925
1926 $page = $has_social_admin_page ? 'jetpack-social' : 'jetpack#/sharing';
1927
1928 return ( new Paths() )->admin_url( array( 'page' => $page ) );
1929 }
1930
1931 /**
1932 * Get the Jetpack Social info from the API.
1933 *
1934 * @param int $blog_id The WPCOM blog_id for the current blog.
1935 * @return array
1936 */
1937 public function get_api_data( $blog_id ) {
1938 static $api_data_response = null;
1939 $key = 'jetpack_social_api_data';
1940
1941 if ( isset( $api_data_response ) ) {
1942 return ! is_wp_error( $api_data_response ) ? $api_data_response : array();
1943 }
1944
1945 $response = Client::wpcom_json_api_request_as_blog(
1946 sprintf( 'sites/%d/jetpack-social', absint( $blog_id ) ),
1947 '2',
1948 array(
1949 'headers' => array( 'content-type' => 'application/json' ),
1950 'method' => 'GET',
1951 ),
1952 null,
1953 'wpcom'
1954 );
1955 $api_data_response = Publicize_Utils::make_proper_response( $response );
1956
1957 if ( ! is_wp_error( $api_data_response ) ) {
1958 set_transient( $key, $api_data_response, DAY_IN_SECONDS );
1959 return $api_data_response;
1960 }
1961
1962 $cached_response = get_transient( $key );
1963 if ( ! empty( $cached_response ) ) {
1964 return $cached_response;
1965 }
1966
1967 return array();
1968 }
1969
1970 /**
1971 * Check if enhanced publishing is enabled.
1972 *
1973 * @deprecated $$next-version use Automattic\Jetpack\Publicize\Publicize_Base\has_enhanced_publishing_feature instead.
1974 * @param int $blog_id The blog ID for the current blog.
1975 * @return bool
1976 */
1977 public function is_enhanced_publishing_enabled( $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1978 return $this->has_enhanced_publishing_feature();
1979 }
1980
1981 /**
1982 * Check if the enhanced publishing feature is enabled.
1983 *
1984 * @return bool
1985 */
1986 public function has_enhanced_publishing_feature() {
1987 return Current_Plan::supports( 'social-enhanced-publishing' );
1988 }
1989
1990 /**
1991 * Check if the social image generator is enabled.
1992 *
1993 * @deprecated 0.24.2 use Automattic\Jetpack\Publicize\Publicize_Base\has_social_image_generator_feature instead.
1994 * @param int $blog_id The blog ID for the current blog.
1995 * @return bool
1996 */
1997 public function is_social_image_generator_enabled( $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1998 return $this->has_social_image_generator_feature();
1999 }
2000
2001 /**
2002 * Check if the social image generator is enabled.
2003 *
2004 * @return bool
2005 */
2006 public function has_social_image_generator_feature() {
2007 return Current_Plan::supports( 'social-image-generator' );
2008 }
2009
2010 /**
2011 * Check if the auto-conversion feature is one of the active features.
2012 *
2013 * TODO: Remove this after certain releases of Jetpack v15.
2014 *
2015 * @param string $type Whether image or video.
2016 *
2017 * @return bool
2018 */
2019 public function has_social_auto_conversion_feature( $type ) {
2020 return Current_Plan::supports( "social-$type-auto-convert" );
2021 }
2022
2023 /**
2024 * Check if a connection is enabled.
2025 *
2026 * @param string $connection The connection name like 'instagram', 'mastodon', 'nextdoor' etc.
2027 *
2028 * @return bool
2029 */
2030 public function has_connection_feature( $connection ) {
2031 return Current_Plan::supports( "social-$connection-connection" );
2032 }
2033
2034 /**
2035 * Check if the new connections management is enabled is enabled.
2036 *
2037 * @return bool
2038 */
2039 public function has_connections_management_feature() {
2040 return Current_Plan::supports( 'social-connections-management' );
2041 }
2042
2043 /**
2044 * Get a list of additional connections that are supported by the current plan.
2045 *
2046 * @return array
2047 */
2048 public function get_supported_additional_connections() {
2049 $additional_connections = array();
2050
2051 if ( $this->has_connection_feature( 'threads' ) ) {
2052 $additional_connections[] = 'threads';
2053 }
2054
2055 return $additional_connections;
2056 }
2057
2058 /**
2059 * Check if we have a paid Jetpack Social plan.
2060 *
2061 * @param bool $refresh_from_wpcom Whether to force refresh the plan check.
2062 *
2063 * @return bool True if we have a paid plan, false otherwise.
2064 */
2065 public function has_paid_plan( $refresh_from_wpcom = false ) {
2066 static $has_paid_plan = null;
2067 if ( null === $has_paid_plan ) {
2068 $has_paid_plan = Current_Plan::supports( 'social-shares-1000', $refresh_from_wpcom );
2069 }
2070 return $has_paid_plan;
2071 }
2072
2073 /**
2074 * Check if we have paid features enabled.
2075 *
2076 * @return bool True if we have paid features, false otherwise.
2077 */
2078 public function has_paid_features() {
2079 return $this->has_enhanced_publishing_feature();
2080 }
2081
2082 /**
2083 * Get an array with all dismissed notices.
2084 *
2085 * @return array
2086 */
2087 public function get_dismissed_notices() {
2088 $dismissed_notices = get_option( self::OPTION_JETPACK_SOCIAL_DISMISSED_NOTICES );
2089
2090 if ( ! is_array( $dismissed_notices ) ) {
2091 return array();
2092 }
2093
2094 return $dismissed_notices;
2095 }
2096
2097 /**
2098 * Whether the current user can manage a connection.
2099 *
2100 * @param array $connection_data The connection data.
2101 *
2102 * @return bool
2103 */
2104 public static function can_manage_connection( $connection_data ) {
2105 return current_user_can( 'edit_others_posts' ) || get_current_user_id() === (int) $connection_data['user_id'];
2106 }
2107 }
2108
2109 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
2110
2111 /**
2112 * Get Calypso URL for Publicize connections.
2113 *
2114 * @return string
2115 */
2116 function publicize_calypso_url() {
2117 _deprecated_function( __METHOD__, '0.2.0', 'Publicize::publicize_connections_url' );
2118 return Redirect::get_url( 'calypso-marketing-connections', array( 'site' => ( new Status() )->get_site_suffix() ) );
2119 }
2120
2121 /**
2122 * Adds support for the post-thumbnails feature, regardless of underlying theme support.
2123 *
2124 * This ensures the featured image UI appears in the editor, allowing the user to
2125 * explicitly set an image for their social media post.
2126 */
2127 function add_theme_post_thumbnails_support() {
2128 add_theme_support( 'post-thumbnails', get_post_types_by_support( 'publicize' ) );
2129 }
2130