2022-04-02 10:26:41 +02:00
< ? php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Publicize_Base class .
*
* @ package automattic / jetpack
*/
2020-10-20 18:05:12 +02:00
// phpcs:disable WordPress.NamingConventions.ValidVariableName
2019-11-15 23:26:29 +01:00
2020-05-06 17:20:49 +02:00
use Automattic\Jetpack\Redirect ;
2020-12-10 14:04:11 +01:00
use Automattic\Jetpack\Status ;
2020-05-06 17:20:49 +02:00
2022-04-02 10:26:41 +02:00
/**
* Base class for Publicize .
*/
2019-11-15 23:26:29 +01:00
abstract class Publicize_Base {
/**
2022-04-02 10:26:41 +02:00
* Services that are currently connected to the given user
* through Publicize .
*
* @ var array
*/
2019-11-15 23:26:29 +01:00
public $connected_services = array ();
/**
2022-04-02 10:26:41 +02:00
* Services that are supported by publicize . They don ' t
* necessarily need to be connected to the current user .
*
* @ var array
*/
2019-11-15 23:26:29 +01:00
public $services ;
/**
2022-04-02 10:26:41 +02:00
* Post meta key for admin page .
*
* @ var string
*/
public $ADMIN_PAGE = 'wpas' ;
/**
* Post meta key for post message .
*
* @ var string
*/
public $POST_MESS = '_wpas_mess' ;
2020-10-20 18:05:12 +02:00
/**
* Post meta key for flagging when the post is a tweetstorm .
*
* @ var string
*/
public $POST_TWEETSTORM = '_wpas_is_tweetstorm' ;
2022-04-02 10:26:41 +02:00
/**
* Post meta key for the flagging when the post share feature is disabled .
*
* @ var string
*/
const POST_PUBLICIZE_FEATURE_ENABLED = '_wpas_feature_enabled' ;
/**
* Connection ID appended to indicate that a connection should NOT be publicized to .
*
* @ var string
*/
public $POST_SKIP = '_wpas_skip_' ;
/**
* Connection ID appended to indicate a connection has already been publicized to .
*
* @ var string
*/
public $POST_DONE = '_wpas_done_' ;
/**
* Prefix for user authorization ( used in publicize - wpcom . php )
*
* @ var string
*/
public $USER_AUTH = 'wpas_authorize' ;
/**
* Prefix for user opt .
*
* @ var string
*/
public $USER_OPT = 'wpas_' ;
/**
* Ready for Publicize to do its thing .
*
* @ var string
*/
public $PENDING = '_publicize_pending' ;
/**
* Array of external IDs where we ' ve Publicized .
*
* @ var string
*/
public $POST_SERVICE_DONE = '_publicize_done_external' ;
/**
* Default pieces of the message used in constructing the
* content pushed out to other social networks .
*/
2019-11-15 23:26:29 +01:00
/**
2022-04-02 10:26:41 +02:00
* Default prefix .
*
* @ var string
*/
public $default_prefix = '' ;
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
/**
* Default message .
*
* @ var string
*/
2019-11-15 23:26:29 +01:00
public $default_message = '%title%' ;
2022-04-02 10:26:41 +02:00
/**
* Default suffix .
*
* @ var string
*/
public $default_suffix = ' ' ;
2019-11-15 23:26:29 +01:00
/**
* What WP capability is require to create / delete global connections ?
* All users with this cap can un - globalize all other global connections , and globalize any of their own
* Globalized connections cannot be unselected by users without this capability when publishing
2022-04-02 10:26:41 +02:00
*
* @ var string
2019-11-15 23:26:29 +01:00
*/
public $GLOBAL_CAP = 'publish_posts' ;
/**
2022-04-02 10:26:41 +02:00
* Sets up the basics of Publicize .
*/
public function __construct () {
$this -> default_message = self :: build_sprintf (
array (
/**
* Filter the default Publicize message .
*
* @ module publicize
*
* @ since 2.0 . 0
*
* @ param string $this -> default_message Publicize ' s default message . Default is the post title .
*/
apply_filters ( 'wpas_default_message' , $this -> default_message ),
'title' ,
'url' ,
)
);
$this -> default_prefix = self :: build_sprintf (
array (
/**
* Filter the message prepended to the Publicize custom message .
*
* @ module publicize
*
* @ since 2.0 . 0
*
* @ param string $this -> default_prefix String prepended to the Publicize custom message .
*/
apply_filters ( 'wpas_default_prefix' , $this -> default_prefix ),
'url' ,
)
);
$this -> default_suffix = self :: build_sprintf (
array (
/**
* Filter the message appended to the Publicize custom message .
*
* @ module publicize
*
* @ since 2.0 . 0
*
* @ param string $this -> default_suffix String appended to the Publicize custom message .
*/
apply_filters ( 'wpas_default_suffix' , $this -> default_suffix ),
'url' ,
)
);
2019-11-15 23:26:29 +01:00
/**
* Filter the capability to change global Publicize connection options .
*
* All users with this cap can un - globalize all other global connections , and globalize any of their own
* Globalized connections cannot be unselected by users without this capability when publishing .
*
* @ module publicize
*
* @ since 2.2 . 1
*
* @ param string $this -> GLOBAL_CAP default capability in control of global Publicize connection options . Default to edit_others_posts .
*/
$this -> GLOBAL_CAP = apply_filters ( 'jetpack_publicize_global_connections_cap' , $this -> GLOBAL_CAP );
// stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta,
2022-04-02 10:26:41 +02:00
// then check meta and publicize based on that. stage 3 implemented on wpcom.
2019-11-15 23:26:29 +01:00
add_action ( 'transition_post_status' , array ( $this , 'flag_post_for_publicize' ), 10 , 3 );
2022-04-02 10:26:41 +02:00
add_action ( 'save_post' , array ( $this , 'save_meta' ), 20 , 2 );
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
// Default checkbox state for each Connection.
add_filter ( 'publicize_checkbox_default' , array ( $this , 'publicize_checkbox_default' ), 10 , 2 );
2019-11-15 23:26:29 +01:00
// Alter the "Post Publish" admin notice to mention the Connections we Publicized to.
add_filter ( 'post_updated_messages' , array ( $this , 'update_published_message' ), 20 , 1 );
2022-04-02 10:26:41 +02:00
// Connection test callback.
2019-11-15 23:26:29 +01:00
add_action ( 'wp_ajax_test_publicize_conns' , array ( $this , 'test_publicize_conns' ) );
add_action ( 'init' , array ( $this , 'add_post_type_support' ) );
add_action ( 'init' , array ( $this , 'register_post_meta' ), 20 );
add_action ( 'jetpack_register_gutenberg_extensions' , array ( $this , 'register_gutenberg_extension' ) );
}
2022-04-02 10:26:41 +02:00
/**
* Services : Facebook , Twitter , etc .
*/
2019-11-15 23:26:29 +01:00
/**
* Get services for the given blog and user .
*
* Can return all available services or just the ones with an active connection .
*
2022-04-02 10:26:41 +02:00
* @ param string $filter Type of filter .
* 'all' ( default ) - Get all services available for connecting .
* 'connected' - Get all services currently connected .
* @ param false | int $_blog_id The blog ID . Use false ( default ) for the current blog .
* @ param false | int $_user_id The user ID . Use false ( default ) for the current user .
2019-11-15 23:26:29 +01:00
* @ return array
*/
2022-04-02 10:26:41 +02:00
abstract public function get_services ( $filter = 'all' , $_blog_id = false , $_user_id = false );
2019-11-15 23:26:29 +01:00
/**
* Does the given user have a connection to the service on the given blog ?
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param false | int $_blog_id The blog ID . Use false ( default ) for the current blog .
* @ param false | int $_user_id The user ID . Use false ( default ) for the current user .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function is_enabled ( $service_name , $_blog_id = false , $_user_id = false ) {
if ( ! $_blog_id ) {
2019-11-15 23:26:29 +01:00
$_blog_id = $this -> blog_id ();
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
if ( ! $_user_id ) {
2019-11-15 23:26:29 +01:00
$_user_id = $this -> user_id ();
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
$connections = $this -> get_connections ( $service_name , $_blog_id , $_user_id );
return ( is_array ( $connections ) && count ( $connections ) > 0 ? true : false );
}
/**
* Generates a connection URL .
*
* This is the URL , which , when visited by the user , starts the authentication
* process required to forge a connection .
*
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ return string
*/
2022-04-02 10:26:41 +02:00
abstract public function connect_url ( $service_name );
2019-11-15 23:26:29 +01:00
/**
* Generates a Connection refresh URL .
*
* This is the URL , which , when visited by the user , re - authenticates their
* connection to the service .
*
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ return string
*/
2022-04-02 10:26:41 +02:00
abstract public function refresh_url ( $service_name );
2019-11-15 23:26:29 +01:00
/**
* Generates a disconnection URL .
*
* This is the URL , which , when visited by the user , breaks their connection
* with the service .
*
* @ param string $service_name 'facebook' , 'twitter' , etc .
2022-04-02 10:26:41 +02:00
* @ param string $connection_id Connection ID .
2019-11-15 23:26:29 +01:00
* @ return string
*/
2022-04-02 10:26:41 +02:00
abstract public function disconnect_url ( $service_name , $connection_id );
2019-11-15 23:26:29 +01:00
/**
* Returns a display name for the Service
*
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ return string
*/
public static function get_service_label ( $service_name ) {
switch ( $service_name ) {
case 'linkedin' :
return 'LinkedIn' ;
2020-03-03 18:49:45 +01:00
case 'google_drive' : // google-drive used to be called google_drive.
case 'google-drive' :
return 'Google Drive' ;
2019-11-15 23:26:29 +01:00
case 'twitter' :
case 'facebook' :
case 'tumblr' :
default :
return ucfirst ( $service_name );
}
}
2022-04-02 10:26:41 +02:00
/**
* Connections : For each Service , there can be multiple connections
* for a given user . For example , one user could be connected to Twitter
* as both @ jetpack and as @ wordpressdotcom
*
* For historical reasons , Connections are represented as an object
* on WordPress . com and as an array in Jetpack .
*/
2019-11-15 23:26:29 +01:00
/**
* Get the active Connections of a Service
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param false | int $_blog_id The blog ID . Use false ( default ) for the current blog .
* @ param false | int $_user_id The user ID . Use false ( default ) for the current user .
2019-11-15 23:26:29 +01:00
* @ return false | object [] | array [] false if no connections exist
*/
2022-04-02 10:26:41 +02:00
abstract public function get_connections ( $service_name , $_blog_id = false , $_user_id = false );
2019-11-15 23:26:29 +01:00
/**
* Get a single Connection of a Service
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param string $connection_id Connection ID .
* @ param false | int $_blog_id The blog ID . Use false ( default ) for the current blog .
* @ param false | int $_user_id The user ID . Use false ( default ) for the current user .
2019-11-15 23:26:29 +01:00
* @ return false | object [] | array [] false if no connections exist
*/
2022-04-02 10:26:41 +02:00
abstract public function get_connection ( $service_name , $connection_id , $_blog_id = false , $_user_id = false );
2019-11-15 23:26:29 +01:00
/**
* Get the Connection ID .
*
* Note that this is different than the Connection ' s uniqueid .
*
* Via a quirk of history , ID is globally unique and unique_id
* is only unique per site .
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return string
*/
2022-04-02 10:26:41 +02:00
abstract public function get_connection_id ( $connection );
2019-11-15 23:26:29 +01:00
/**
* Get the Connection unique_id
*
* Note that this is different than the Connections ID .
*
* Via a quirk of history , ID is globally unique and unique_id
* is only unique per site .
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return string
*/
2022-04-02 10:26:41 +02:00
abstract public function get_connection_unique_id ( $connection );
2019-11-15 23:26:29 +01:00
/**
* Get the Connection ' s Meta data
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection Connection .
2019-11-15 23:26:29 +01:00
* @ return array Connection Meta
*/
2022-04-02 10:26:41 +02:00
abstract public function get_connection_meta ( $connection );
2019-11-15 23:26:29 +01:00
/**
* Disconnect a Connection
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param string $connection_id Connection ID .
* @ param false | int $_blog_id The blog ID . Use false ( default ) for the current blog .
* @ param false | int $_user_id The user ID . Use false ( default ) for the current user .
* @ param bool $force_delete Whether to skip permissions checks .
2019-11-15 23:26:29 +01:00
* @ return false | void False on failure . Void on success .
*/
2022-04-02 10:26:41 +02:00
abstract public function disconnect ( $service_name , $connection_id , $_blog_id = false , $_user_id = false , $force_delete = false );
2019-11-15 23:26:29 +01:00
/**
* Globalizes a Connection
*
2022-04-02 10:26:41 +02:00
* @ param string $connection_id Connection ID .
2019-11-15 23:26:29 +01:00
* @ return bool Falsey on failure . Truthy on success .
*/
2022-04-02 10:26:41 +02:00
abstract public function globalize_connection ( $connection_id );
2019-11-15 23:26:29 +01:00
/**
* Unglobalizes a Connection
*
2022-04-02 10:26:41 +02:00
* @ param string $connection_id Connection ID .
2019-11-15 23:26:29 +01:00
* @ return bool Falsey on failure . Truthy on success .
*/
2022-04-02 10:26:41 +02:00
abstract public function unglobalize_connection ( $connection_id );
2019-11-15 23:26:29 +01:00
/**
* Returns an external URL to the Connection ' s profile
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return false | string False on failure . URL on success .
*/
2022-04-02 10:26:41 +02:00
public function get_profile_link ( $service_name , $connection ) {
2019-11-15 23:26:29 +01:00
$cmeta = $this -> get_connection_meta ( $connection );
if ( isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'link' ] ) ) {
2022-04-02 10:26:41 +02:00
if ( 'facebook' === $service_name && 0 === strpos ( wp_parse_url ( $cmeta [ 'connection_data' ][ 'meta' ][ 'link' ], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) {
// App-scoped Facebook user IDs are not usable profile links.
2019-11-15 23:26:29 +01:00
return false ;
}
return $cmeta [ 'connection_data' ][ 'meta' ][ 'link' ];
2022-04-02 10:26:41 +02:00
}
if ( 'facebook' === $service_name && isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'facebook_page' ] ) ) {
2019-11-15 23:26:29 +01:00
return 'https://facebook.com/' . $cmeta [ 'connection_data' ][ 'meta' ][ 'facebook_page' ];
2022-04-02 10:26:41 +02:00
}
if ( 'tumblr' === $service_name && isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'tumblr_base_hostname' ] ) ) {
return 'https://' . $cmeta [ 'connection_data' ][ 'meta' ][ 'tumblr_base_hostname' ];
}
if ( 'twitter' === $service_name ) {
return 'https://twitter.com/' . substr ( $cmeta [ 'external_display' ], 1 ); // Has a leading '@'.
}
if ( 'linkedin' === $service_name ) {
if ( ! isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'profile_url' ] ) ) {
2019-11-15 23:26:29 +01:00
return false ;
}
$profile_url_query = wp_parse_url ( $cmeta [ 'connection_data' ][ 'meta' ][ 'profile_url' ], PHP_URL_QUERY );
wp_parse_str ( $profile_url_query , $profile_url_query_args );
2022-04-02 10:26:41 +02:00
$id = null ;
2019-11-15 23:26:29 +01:00
if ( isset ( $profile_url_query_args [ 'key' ] ) ) {
$id = $profile_url_query_args [ 'key' ];
} elseif ( isset ( $profile_url_query_args [ 'id' ] ) ) {
$id = $profile_url_query_args [ 'id' ];
} else {
return false ;
}
2022-04-02 10:26:41 +02:00
return esc_url_raw ( add_query_arg ( 'id' , rawurlencode ( $id ), 'https://www.linkedin.com/profile/view' ) );
2019-11-15 23:26:29 +01:00
}
2022-04-02 10:26:41 +02:00
return false ; // no fallback. we just won't link it.
2019-11-15 23:26:29 +01:00
}
/**
* Returns a display name for the Connection
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return string
*/
2022-04-02 10:26:41 +02:00
public function get_display_name ( $service_name , $connection ) {
2019-11-15 23:26:29 +01:00
$cmeta = $this -> get_connection_meta ( $connection );
if ( isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'display_name' ] ) ) {
return $cmeta [ 'connection_data' ][ 'meta' ][ 'display_name' ];
2022-04-02 10:26:41 +02:00
}
if ( 'tumblr' === $service_name && isset ( $cmeta [ 'connection_data' ][ 'meta' ][ 'tumblr_base_hostname' ] ) ) {
return $cmeta [ 'connection_data' ][ 'meta' ][ 'tumblr_base_hostname' ];
}
if ( 'twitter' === $service_name ) {
2019-11-15 23:26:29 +01:00
return $cmeta [ 'external_display' ];
}
2022-04-02 10:26:41 +02:00
$connection_display = $cmeta [ 'external_display' ];
if ( empty ( $connection_display ) ) {
$connection_display = $cmeta [ 'external_name' ];
}
return $connection_display ;
}
/**
* Returns a profile picture for the Connection
*
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
* @ return string
*/
private function get_profile_picture ( $connection ) {
$cmeta = $this -> get_connection_meta ( $connection );
if ( isset ( $cmeta [ 'profile_picture' ] ) ) {
return $cmeta [ 'profile_picture' ];
}
return '' ;
2019-11-15 23:26:29 +01:00
}
/**
* Whether the user needs to select additional options after connecting
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name 'facebook' , 'twitter' , etc .
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function show_options_popup ( $service_name , $connection ) {
2019-11-15 23:26:29 +01:00
$cmeta = $this -> get_connection_meta ( $connection );
2022-04-02 10:26:41 +02:00
// Always show if no selection has been made for Facebook.
if ( 'facebook' === $service_name && empty ( $cmeta [ 'connection_data' ][ 'meta' ][ 'facebook_profile' ] ) && empty ( $cmeta [ 'connection_data' ][ 'meta' ][ 'facebook_page' ] ) ) {
2019-11-15 23:26:29 +01:00
return true ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
// Always show if no selection has been made for Tumblr.
if ( 'tumblr' === $service_name && empty ( $cmeta [ 'connection_data' ][ 'meta' ][ 'tumblr_base_hostname' ] ) ) {
2019-11-15 23:26:29 +01:00
return true ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
// if we have the specific connection info..
2022-04-02 10:26:41 +02:00
$id = ! empty ( $_GET [ 'id' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'id' ] ) ) : '' ; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $id ) {
if ( $cmeta [ 'connection_data' ][ 'id' ] === $id ) {
2019-11-15 23:26:29 +01:00
return true ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
} else {
2022-04-02 10:26:41 +02:00
// Otherwise, just show if this is the completed step / first load.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$is_completed = ! empty ( $_GET [ 'action' ] ) && 'completed' === $_GET [ 'action' ];
$service = ! empty ( $_GET [ 'service' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'service' ] ) ) : false ;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( $is_completed && $service_name === $service && ! in_array ( $service , array ( 'facebook' , 'tumblr' ), true ) ) {
2019-11-15 23:26:29 +01:00
return true ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
}
return false ;
}
2022-04-02 10:26:41 +02:00
/**
* Check if a connection is global
*
* @ param array $connection Connection data .
* @ return bool Whether the connection is global .
*/
public function is_global_connection ( $connection ) {
return empty ( $connection [ 'connection_data' ][ 'user_id' ] );
}
2019-11-15 23:26:29 +01:00
/**
* Whether the Connection is " valid " wrt Facebook ' s requirements .
*
* Must be connected to a Page ( not a Profile ) .
* ( Also returns true if we ' re in the middle of the connection process )
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function is_valid_facebook_connection ( $connection ) {
2019-11-15 23:26:29 +01:00
if ( $this -> is_connecting_connection ( $connection ) ) {
return true ;
}
$connection_meta = $this -> get_connection_meta ( $connection );
$connection_data = $connection_meta [ 'connection_data' ];
2022-04-02 10:26:41 +02:00
return isset ( $connection_data [ 'meta' ][ 'facebook_page' ] );
2019-11-15 23:26:29 +01:00
}
/**
* LinkedIn needs to be reauthenticated to use v2 of their API .
* If it 's using LinkedIn old API, it' s an 'invalid' connection
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function is_invalid_linkedin_connection ( $connection ) {
2019-11-15 23:26:29 +01:00
// LinkedIn API v1 included the profile link in the connection data.
$connection_meta = $this -> get_connection_meta ( $connection );
return isset ( $connection_meta [ 'connection_data' ][ 'meta' ][ 'profile_url' ] );
}
/**
* Whether the Connection currently being connected
*
2022-04-02 10:26:41 +02:00
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function is_connecting_connection ( $connection ) {
2019-11-15 23:26:29 +01:00
$connection_meta = $this -> get_connection_meta ( $connection );
$connection_data = $connection_meta [ 'connection_data' ];
2022-04-02 10:26:41 +02:00
return isset ( $connection_data [ 'meta' ][ 'options_responses' ] );
2019-11-15 23:26:29 +01:00
}
/**
* AJAX Handler to run connection tests on all Connections
2022-04-02 10:26:41 +02:00
*
2019-11-15 23:26:29 +01:00
* @ return void
*/
2022-04-02 10:26:41 +02:00
public function test_publicize_conns () {
2019-11-15 23:26:29 +01:00
wp_send_json_success ( $this -> get_publicize_conns_test_results () );
}
/**
* Run connection tests on all Connections
*
* @ return array {
* Array of connection test results .
*
* @ type string 'connectionID' Connection identifier string that is unique for each connection
* @ type string 'serviceName' Slug of the connection ' s service ( facebook , twitter , ... )
* @ type bool 'connectionTestPassed' Whether the connection test was successful
* @ type string 'connectionTestMessage' Test success or error message
* @ type bool 'userCanRefresh' Whether the user can re - authenticate their connection to the service
* @ type string 'refreshText' Message instructing user to re - authenticate their connection to the service
* @ type string 'refreshURL' URL , which , when visited by the user , re - authenticates their connection to the service .
* @ type string 'unique_id' ID string representing connection
* }
*/
2022-04-02 10:26:41 +02:00
public function get_publicize_conns_test_results () {
2019-11-15 23:26:29 +01:00
$test_results = array ();
foreach ( ( array ) $this -> get_services ( 'connected' ) as $service_name => $connections ) {
foreach ( $connections as $connection ) {
$id = $this -> get_connection_id ( $connection );
2022-04-02 10:26:41 +02:00
$connection_test_passed = true ;
$connection_test_message = __ ( 'This connection is working correctly.' , 'jetpack' );
$user_can_refresh = false ;
$refresh_text = '' ;
$refresh_url = '' ;
2019-11-15 23:26:29 +01:00
$connection_test_result = true ;
if ( method_exists ( $this , 'test_connection' ) ) {
$connection_test_result = $this -> test_connection ( $service_name , $connection );
}
if ( is_wp_error ( $connection_test_result ) ) {
2022-04-02 10:26:41 +02:00
$connection_test_passed = false ;
2019-11-15 23:26:29 +01:00
$connection_test_message = $connection_test_result -> get_error_message ();
2022-04-02 10:26:41 +02:00
$error_data = $connection_test_result -> get_error_data ();
2019-11-15 23:26:29 +01:00
$user_can_refresh = $error_data [ 'user_can_refresh' ];
2022-04-02 10:26:41 +02:00
$refresh_text = $error_data [ 'refresh_text' ];
$refresh_url = $error_data [ 'refresh_url' ];
2019-11-15 23:26:29 +01:00
}
2022-04-02 10:26:41 +02:00
// Mark Facebook profiles as deprecated.
2019-11-15 23:26:29 +01:00
if ( 'facebook' === $service_name ) {
if ( ! $this -> is_valid_facebook_connection ( $connection ) ) {
2022-04-02 10:26:41 +02:00
$connection_test_passed = false ;
$user_can_refresh = false ;
2019-11-15 23:26:29 +01:00
$connection_test_message = __ ( 'Please select a Facebook Page to publish updates.' , 'jetpack' );
}
}
2022-04-02 10:26:41 +02:00
// LinkedIn needs reauthentication to be compatible with v2 of their API.
2019-11-15 23:26:29 +01:00
if ( 'linkedin' === $service_name && $this -> is_invalid_linkedin_connection ( $connection ) ) {
2022-04-02 10:26:41 +02:00
$connection_test_passed = 'must_reauth' ;
$user_can_refresh = false ;
2019-11-15 23:26:29 +01:00
$connection_test_message = esc_html__ ( 'Your LinkedIn connection needs to be reauthenticated to continue working – head to Sharing to take care of it.' , 'jetpack' );
}
$unique_id = null ;
2022-04-02 10:26:41 +02:00
2019-11-15 23:26:29 +01:00
if ( ! empty ( $connection -> unique_id ) ) {
$unique_id = $connection -> unique_id ;
2022-04-02 10:26:41 +02:00
} elseif ( ! empty ( $connection [ 'connection_data' ][ 'token_id' ] ) ) {
2019-11-15 23:26:29 +01:00
$unique_id = $connection [ 'connection_data' ][ 'token_id' ];
}
$test_results [] = array (
'connectionID' => $id ,
'serviceName' => $service_name ,
'connectionTestPassed' => $connection_test_passed ,
'connectionTestMessage' => esc_attr ( $connection_test_message ),
'userCanRefresh' => $user_can_refresh ,
'refreshText' => esc_attr ( $refresh_text ),
'refreshURL' => $refresh_url ,
'unique_id' => $unique_id ,
);
}
}
return $test_results ;
}
/**
* Run the connection test for the Connection
*
2022-04-02 10:26:41 +02:00
* @ param string $service_name $service_name 'facebook' , 'twitter' , etc .
* @ param object | array $connection The Connection object ( WordPress . com ) or array ( Jetpack ) .
2019-11-15 23:26:29 +01:00
* @ return WP_Error | true WP_Error on failure . True on success
*/
2022-04-02 10:26:41 +02:00
abstract public function test_connection ( $service_name , $connection );
2019-11-15 23:26:29 +01:00
/**
* Retrieves current list of connections and applies filters .
*
* Retrieves current available connections and checks if the connections
* have already been used to share current post . Finally , the checkbox
* form UI fields are calculated . This function exposes connection form
* data directly as array so it can be retrieved for static HTML generation
* or JSON consumption .
*
* @ since 6.7 . 0
*
* @ param integer $selected_post_id Optional . Post ID to query connection status for .
*
* @ return array {
* Array of UI setup data for connection list form .
*
2022-04-02 10:26:41 +02:00
* @ type string 'unique_id' ID string representing connection
* @ type string 'service_name' Slug of the connection ' s service ( facebook , twitter , ... )
* @ type string 'service_label' Service Label ( Facebook , Twitter , ... )
* @ type string 'display_name' Connection ' s human - readable Username : " @jetpack "
* @ type string 'profile_picture' Connection profile picture .
* @ type bool 'enabled' Default value for the connection ( e . g . , for a checkbox ) .
* @ type bool 'done' Has this connection already been publicized to ?
* @ type bool 'toggleable' Is the user allowed to change the value for the connection ?
* @ type bool 'global' Is this connection a global one ?
2019-11-15 23:26:29 +01:00
* }
*/
public function get_filtered_connection_data ( $selected_post_id = null ) {
$connection_list = array ();
$post = get_post ( $selected_post_id ); // Defaults to current post if $post_id is null.
// Handle case where there is no current post.
if ( ! empty ( $post ) ) {
$post_id = $post -> ID ;
} else {
$post_id = null ;
}
$services = $this -> get_services ( 'connected' );
$all_done = $this -> post_is_done_sharing ( $post_id );
// We don't allow Publicizing to the same external id twice, to prevent spam.
$service_id_done = ( array ) get_post_meta ( $post_id , $this -> POST_SERVICE_DONE , true );
foreach ( $services as $service_name => $connections ) {
foreach ( $connections as $connection ) {
$connection_meta = $this -> get_connection_meta ( $connection );
$connection_data = $connection_meta [ 'connection_data' ];
$unique_id = $this -> get_connection_unique_id ( $connection );
// Was this connection (OR, old-format service) already Publicized to?
$done = ! empty ( $post ) && (
2022-04-02 10:26:41 +02:00
// New flags.
1 === ( int ) get_post_meta ( $post -> ID , $this -> POST_DONE . $unique_id , true )
2019-11-15 23:26:29 +01:00
||
2022-04-02 10:26:41 +02:00
// Old flags.
1 === ( int ) get_post_meta ( $post -> ID , $this -> POST_DONE . $service_name , true )
2019-11-15 23:26:29 +01:00
);
/**
* Filter whether a post should be publicized to a given service .
*
* @ module publicize
*
* @ since 2.0 . 0
*
* @ param bool true Should the post be publicized to a given service ? Default to true .
* @ param int $post_id Post ID .
* @ param string $service_name Service name .
* @ param array $connection_data Array of information about all Publicize details for the site .
*/
2022-04-02 10:26:41 +02:00
/* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
2019-11-15 23:26:29 +01:00
if ( ! apply_filters ( 'wpas_submit_post?' , true , $post_id , $service_name , $connection_data ) ) {
continue ;
}
// Should we be skipping this one?
$skip = (
(
! empty ( $post )
&&
2022-04-02 10:26:41 +02:00
in_array ( $post -> post_status , array ( 'publish' , 'draft' , 'future' ), true )
2019-11-15 23:26:29 +01:00
&&
(
2022-04-02 10:26:41 +02:00
// New flags.
2019-11-15 23:26:29 +01:00
get_post_meta ( $post -> ID , $this -> POST_SKIP . $unique_id , true )
||
2022-04-02 10:26:41 +02:00
// Old flags.
2019-11-15 23:26:29 +01:00
get_post_meta ( $post -> ID , $this -> POST_SKIP . $service_name )
)
)
||
(
is_array ( $connection )
&&
isset ( $connection_meta [ 'external_id' ] ) && ! empty ( $service_id_done [ $service_name ][ $connection_meta [ 'external_id' ] ] )
)
);
// If this one has already been publicized to, don't let it happen again.
$toggleable = ! $done && ! $all_done ;
// Determine the state of the checkbox (on/off) and allow filtering.
$enabled = $done || ! $skip ;
/**
* Filter the checkbox state of each Publicize connection appearing in the post editor .
*
* @ module publicize
*
* @ since 2.0 . 1
*
* @ param bool $enabled Should the Publicize checkbox be enabled for a given service .
* @ param int $post_id Post ID .
* @ param string $service_name Service name .
* @ param array $connection Array of connection details .
*/
$enabled = apply_filters ( 'publicize_checkbox_default' , $enabled , $post_id , $service_name , $connection );
/**
* If this is a global connection and this user doesn ' t have enough permissions to modify
* those connections , don ' t let them change it .
*/
2022-04-02 10:26:41 +02:00
if ( ! $done && $this -> is_global_connection ( $connection_meta ) && ! current_user_can ( $this -> GLOBAL_CAP ) ) {
2019-11-15 23:26:29 +01:00
$toggleable = false ;
/**
* Filters the checkboxes for global connections with non - prilvedged users .
*
* @ module publicize
*
* @ since 3.7 . 0
*
* @ param bool $enabled Indicates if this connection should be enabled . Default true .
* @ param int $post_id ID of the current post
* @ param string $service_name Name of the connection ( Facebook , Twitter , etc )
* @ param array $connection Array of data about the connection .
*/
$enabled = apply_filters ( 'publicize_checkbox_global_default' , $enabled , $post_id , $service_name , $connection );
}
// Force the checkbox to be checked if the post was DONE, regardless of what the filter does.
if ( $done ) {
$enabled = true ;
}
$connection_list [] = array (
2022-04-02 10:26:41 +02:00
'unique_id' => $unique_id ,
'service_name' => $service_name ,
'service_label' => $this -> get_service_label ( $service_name ),
'display_name' => $this -> get_display_name ( $service_name , $connection ),
'profile_picture' => $this -> get_profile_picture ( $connection ),
'enabled' => $enabled ,
'done' => $done ,
'toggleable' => $toggleable ,
'global' => 0 == $connection_data [ 'user_id' ], // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual,WordPress.PHP.StrictComparisons.LooseComparison -- Other types can be used at times.
2019-11-15 23:26:29 +01:00
);
}
}
return $connection_list ;
}
/**
* Checks if post has already been shared by Publicize in the past .
*
* @ since 6.7 . 0
*
* @ param integer $post_id Optional . Post ID to query connection status for : will use current post if missing .
*
* @ return bool True if post has already been shared by Publicize , false otherwise .
*/
abstract public function post_is_done_sharing ( $post_id = null );
/**
* Retrieves full list of available Publicize connection services .
*
* Retrieves current available publicize service connections
* with associated labels and URLs .
*
* @ since 6.7 . 0
*
* @ return array {
* Array of UI service connection data for all services
*
* @ type string 'name' Name of service .
* @ type string 'label' Display label for service .
* @ type string 'url' URL for adding connection to service .
* }
*/
2022-04-02 10:26:41 +02:00
public function get_available_service_data () {
2019-11-15 23:26:29 +01:00
$available_services = $this -> get_services ( 'all' );
$available_service_data = array ();
foreach ( $available_services as $service_name => $service ) {
$available_service_data [] = array (
'name' => $service_name ,
'label' => $this -> get_service_label ( $service_name ),
'url' => $this -> connect_url ( $service_name ),
);
}
return $available_service_data ;
}
2022-04-02 10:26:41 +02:00
/**
* Site Data
*/
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
/**
* Get user ID .
*
* @ return int The current user ' s ID , or 0 if no user is logged in .
*/
public function user_id () {
2019-11-15 23:26:29 +01:00
return get_current_user_id ();
}
2022-04-02 10:26:41 +02:00
/**
* Get site ID .
*
* @ return int Site ID .
*/
public function blog_id () {
2019-11-15 23:26:29 +01:00
return get_current_blog_id ();
}
2022-04-02 10:26:41 +02:00
/**
* Posts
*/
2019-11-15 23:26:29 +01:00
/**
* Checks old and new status to see if the post should be flagged as
* ready to Publicize .
*
* Attached to the `transition_post_status` filter .
*
2022-04-02 10:26:41 +02:00
* @ param string $new_status New status .
* @ param string $old_status Old status .
* @ param WP_Post $post Post object .
2019-11-15 23:26:29 +01:00
* @ return void
*/
2022-04-02 10:26:41 +02:00
abstract public function flag_post_for_publicize ( $new_status , $old_status , $post );
2019-11-15 23:26:29 +01:00
/**
* Ensures the Post internal post - type supports `publicize`
*
* This feature support flag is used by the REST API .
*/
2022-04-02 10:26:41 +02:00
public function add_post_type_support () {
2019-11-15 23:26:29 +01:00
add_post_type_support ( 'post' , 'publicize' );
}
/**
* Register the Publicize Gutenberg extension
*/
2022-04-02 10:26:41 +02:00
public function register_gutenberg_extension () {
2019-11-15 23:26:29 +01:00
// TODO: The `gutenberg/available-extensions` endpoint currently doesn't accept a post ID,
// so we cannot pass one to `$this->current_user_can_access_publicize_data()`.
if ( $this -> current_user_can_access_publicize_data () ) {
Jetpack_Gutenberg :: set_extension_available ( 'jetpack/publicize' );
} else {
Jetpack_Gutenberg :: set_extension_unavailable ( 'jetpack/publicize' , 'unauthorized' );
}
}
/**
* Can the current user access Publicize Data .
*
2022-04-02 10:26:41 +02:00
* @ param int $post_id 0 for general access . Post_ID for specific access .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function current_user_can_access_publicize_data ( $post_id = 0 ) {
2019-11-15 23:26:29 +01:00
/**
* 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 .
*
* @ module publicize
*
* @ since 4.1 . 0
*
* @ param string $capability User capability needed to use publicize
*/
$capability = apply_filters ( 'jetpack_publicize_capability' , 'publish_posts' );
if ( 'publish_posts' === $capability && $post_id ) {
return current_user_can ( 'publish_post' , $post_id );
}
return current_user_can ( $capability );
}
/**
* Auth callback for the protected -> POST_MESS post_meta
*
2022-04-02 10:26:41 +02:00
* @ param int $object_id Post ID .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function message_meta_auth_callback ( $object_id ) {
2019-11-15 23:26:29 +01:00
return $this -> current_user_can_access_publicize_data ( $object_id );
}
/**
2020-10-20 18:05:12 +02:00
* Registers the post_meta for use in the REST API .
2019-11-15 23:26:29 +01:00
*
* Registers for each post type that with `publicize` feature support .
*/
2022-04-02 10:26:41 +02:00
public function register_post_meta () {
2020-10-20 18:05:12 +02:00
$message_args = array (
'type' => 'string' ,
'description' => __ ( 'The message to use instead of the title when sharing to Publicize Services' , 'jetpack' ),
'single' => true ,
'default' => '' ,
'show_in_rest' => array (
'name' => 'jetpack_publicize_message' ,
),
'auth_callback' => array ( $this , 'message_meta_auth_callback' ),
);
$tweetstorm_args = array (
'type' => 'boolean' ,
'description' => __ ( 'Whether or not the post should be treated as a Twitter thread.' , 'jetpack' ),
'single' => true ,
'default' => false ,
'show_in_rest' => array (
'name' => 'jetpack_is_tweetstorm' ,
2019-11-15 23:26:29 +01:00
),
'auth_callback' => array ( $this , 'message_meta_auth_callback' ),
);
2022-04-02 10:26:41 +02:00
$publicize_feature_enable_args = array (
'type' => 'boolean' ,
'description' => __ ( 'Whether or not the Share Post feature is enabled.' , 'jetpack' ),
'single' => true ,
'default' => true ,
'show_in_rest' => array (
'name' => 'jetpack_publicize_feature_enabled' ,
),
'auth_callback' => array ( $this , 'message_meta_auth_callback' ),
);
2019-11-15 23:26:29 +01:00
foreach ( get_post_types () as $post_type ) {
if ( ! $this -> post_type_is_publicizeable ( $post_type ) ) {
continue ;
}
2022-04-02 10:26:41 +02:00
$message_args [ 'object_subtype' ] = $post_type ;
$tweetstorm_args [ 'object_subtype' ] = $post_type ;
$publicize_feature_enable_args [ 'object_subtype' ] = $post_type ;
2019-11-15 23:26:29 +01:00
2020-10-20 18:05:12 +02:00
register_meta ( 'post' , $this -> POST_MESS , $message_args );
register_meta ( 'post' , $this -> POST_TWEETSTORM , $tweetstorm_args );
2022-04-02 10:26:41 +02:00
register_meta ( 'post' , self :: POST_PUBLICIZE_FEATURE_ENABLED , $publicize_feature_enable_args );
2019-11-15 23:26:29 +01:00
}
}
/**
2022-06-16 14:01:47 +02:00
* Helper function to allow us to not publicize posts in certain contexts .
2019-11-15 23:26:29 +01:00
*
2022-04-02 10:26:41 +02:00
* @ param WP_Post $post Post object .
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function should_submit_post_pre_checks ( $post ) {
2019-11-15 23:26:29 +01:00
$submit_post = true ;
2022-04-02 10:26:41 +02:00
if ( defined ( 'WP_IMPORTING' ) && WP_IMPORTING ) {
2019-11-15 23:26:29 +01:00
$submit_post = false ;
}
if (
2022-06-16 14:01:47 +02:00
defined ( 'DOING_AUTOSAVE' )
2019-11-15 23:26:29 +01:00
&&
2022-06-16 14:01:47 +02:00
DOING_AUTOSAVE
2019-11-15 23:26:29 +01:00
) {
$submit_post = false ;
}
2022-06-16 14:01:47 +02:00
// To prevent quick edits from getting publicized.
if ( did_action ( 'wp_ajax_inline-save' ) ) {
$submit_post = false ;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
2022-04-02 10:26:41 +02:00
if ( ! empty ( $_GET [ 'bulk_edit' ] ) ) {
2019-11-15 23:26:29 +01:00
$submit_post = false ;
}
2022-04-02 10:26:41 +02:00
// phpcs:enable WordPress.Security.NonceVerification.Recommended
2019-11-15 23:26:29 +01:00
// - API/XML-RPC Test Posts
if (
(
defined ( 'XMLRPC_REQUEST' )
&&
XMLRPC_REQUEST
||
defined ( 'APP_REQUEST' )
&&
APP_REQUEST
)
&&
0 === strpos ( $post -> post_title , 'Temporary Post Used For Theme Detection' )
) {
$submit_post = false ;
}
2022-04-02 10:26:41 +02:00
// Only work with certain statuses (avoids inherits, auto drafts etc).
if ( ! in_array ( $post -> post_status , array ( 'publish' , 'draft' , 'future' ), true ) ) {
2019-11-15 23:26:29 +01:00
$submit_post = false ;
}
2022-04-02 10:26:41 +02:00
// Don't publish password protected posts.
2019-11-15 23:26:29 +01:00
if ( '' !== $post -> post_password ) {
$submit_post = false ;
}
2022-06-16 14:01:47 +02:00
return $submit_post ;
}
/**
* Fires when a post is saved , checks conditions and saves state in postmeta so that it
* can be picked up later by @ see :: publicize_post () on WordPress . com codebase .
*
* Attached to the `save_post` action .
*
* @ param int $post_id Post ID .
* @ param WP_Post $post Post object .
*/
public function save_meta ( $post_id , $post ) {
$cron_user = null ;
$submit_post = true ;
if ( ! $this -> post_type_is_publicizeable ( $post -> post_type ) ) {
return ;
}
$submit_post = $this -> should_submit_post_pre_checks ( $post );
2022-04-02 10:26:41 +02:00
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're only checking if a value is set
$admin_page = isset ( $_POST [ $this -> ADMIN_PAGE ] ) ? $_POST [ $this -> ADMIN_PAGE ] : null ;
2019-11-15 23:26:29 +01:00
// Did this request happen via wp-admin?
$from_web = isset ( $_SERVER [ 'REQUEST_METHOD' ] )
&&
2022-04-02 10:26:41 +02:00
'post' === strtolower ( sanitize_text_field ( wp_unslash ( $_SERVER [ 'REQUEST_METHOD' ] ) ) )
2019-11-15 23:26:29 +01:00
&&
2022-04-02 10:26:41 +02:00
! empty ( $admin_page );
// phpcs:ignore WordPress.Security.NonceVerification.Missing
2022-06-16 14:01:47 +02:00
$title = isset ( $_POST [ 'wpas_title' ] ) ? sanitize_textarea_field ( wp_unslash ( $_POST [ 'wpas_title' ] ) ) : null ;
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
if ( ( $from_web || defined ( 'POST_BY_EMAIL' ) ) && $title ) {
if ( empty ( $title ) ) {
2019-11-15 23:26:29 +01:00
delete_post_meta ( $post_id , $this -> POST_MESS );
} else {
2022-04-02 10:26:41 +02:00
update_post_meta ( $post_id , $this -> POST_MESS , trim ( stripslashes ( $title ) ) );
2019-11-15 23:26:29 +01:00
}
}
2022-04-02 10:26:41 +02:00
// Change current user to provide context for get_services() if we're running during cron.
2019-11-15 23:26:29 +01:00
if ( defined ( 'DOING_CRON' ) && DOING_CRON ) {
$cron_user = ( int ) $GLOBALS [ 'user_ID' ];
wp_set_current_user ( $post -> post_author );
}
/**
* In this phase , we mark connections that we want to SKIP . When Publicize is actually triggered ,
* it will Publicize to everything * except * those marked for skipping .
*/
foreach ( ( array ) $this -> get_services ( 'connected' ) as $service_name => $connections ) {
foreach ( $connections as $connection ) {
$connection_data = '' ;
2020-12-10 14:04:11 +01:00
if ( is_object ( $connection ) && method_exists ( $connection , 'get_meta' ) ) {
2019-11-15 23:26:29 +01:00
$connection_data = $connection -> get_meta ( 'connection_data' );
2020-12-10 14:04:11 +01:00
} elseif ( ! empty ( $connection [ 'connection_data' ] ) ) {
2019-11-15 23:26:29 +01:00
$connection_data = $connection [ 'connection_data' ];
2020-12-10 14:04:11 +01:00
}
2019-11-15 23:26:29 +01:00
/** This action is documented in modules/publicize/ui.php */
2022-04-02 10:26:41 +02:00
/* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
if ( false === apply_filters ( 'wpas_submit_post?' , $submit_post , $post_id , $service_name , $connection_data ) ) {
2019-11-15 23:26:29 +01:00
delete_post_meta ( $post_id , $this -> PENDING );
continue ;
}
2022-04-02 10:26:41 +02:00
if ( ! empty ( $connection -> unique_id ) ) {
2019-11-15 23:26:29 +01:00
$unique_id = $connection -> unique_id ;
2022-04-02 10:26:41 +02:00
} elseif ( ! empty ( $connection [ 'connection_data' ][ 'token_id' ] ) ) {
2019-11-15 23:26:29 +01:00
$unique_id = $connection [ 'connection_data' ][ 'token_id' ];
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
// This was a wp-admin request, so we need to check the state of checkboxes.
2019-11-15 23:26:29 +01:00
if ( $from_web ) {
2022-04-02 10:26:41 +02:00
// Delete stray service-based post meta.
2019-11-15 23:26:29 +01:00
delete_post_meta ( $post_id , $this -> POST_SKIP . $service_name );
2022-04-02 10:26:41 +02:00
// We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition.
if ( empty ( $admin_page [ 'submit' ][ $unique_id ] ) ) {
2019-11-15 23:26:29 +01:00
// Also make sure that the service-specific input isn't there.
// If the user connected to a new service 'in-page' then a hidden field with the service
// name is added, so we just assume they wanted to Publicize to that service.
2022-04-02 10:26:41 +02:00
if ( empty ( $admin_page [ 'submit' ][ $service_name ] ) ) {
// Nothing seems to be checked, so we're going to mark this one to be skipped.
2019-11-15 23:26:29 +01:00
update_post_meta ( $post_id , $this -> POST_SKIP . $unique_id , 1 );
continue ;
} else {
2022-04-02 10:26:41 +02:00
// Clean up any stray post meta.
2019-11-15 23:26:29 +01:00
delete_post_meta ( $post_id , $this -> POST_SKIP . $unique_id );
}
} else {
2022-04-02 10:26:41 +02:00
// The checkbox for this connection is explicitly checked -- make sure we DON'T skip it.
2019-11-15 23:26:29 +01:00
delete_post_meta ( $post_id , $this -> POST_SKIP . $unique_id );
}
}
/**
* Fires right before the post is processed for Publicize .
* Users may hook in here and do anything else they need to after meta is written ,
* and before the post is processed for Publicize .
*
* @ since 2.1 . 2
*
* @ param bool $submit_post Should the post be publicized .
* @ param int $post -> ID Post ID .
* @ param string $service_name Service name .
* @ param array $connection Array of connection details .
*/
do_action ( 'publicize_save_meta' , $submit_post , $post_id , $service_name , $connection );
}
}
if ( defined ( 'DOING_CRON' ) && DOING_CRON ) {
wp_set_current_user ( $cron_user );
}
2022-04-02 10:26:41 +02:00
// Next up will be ::publicize_post().
2019-11-15 23:26:29 +01:00
}
/**
* Alters the " Post Published " message to include information about where the post
* was Publicized to .
*
* Attached to the `post_updated_messages` filter
*
2022-04-02 10:26:41 +02:00
* @ param string [] $messages Array of messages .
2019-11-15 23:26:29 +01:00
* @ return string []
*/
public function update_published_message ( $messages ) {
global $post_type , $post_type_object , $post ;
if ( ! $this -> post_type_is_publicizeable ( $post_type ) ) {
return $messages ;
}
// Bail early if the post is private.
if ( 'publish' !== $post -> post_status ) {
return $messages ;
}
$view_post_link_html = '' ;
2022-04-02 10:26:41 +02:00
$viewable = is_post_type_viewable ( $post_type_object );
2019-11-15 23:26:29 +01:00
if ( $viewable ) {
2022-04-02 10:26:41 +02:00
/* phpcs:ignore WordPress.WP.I18n.MissingArgDomain, WordPress.Utils.I18nTextDomainFixer.MissingArgDomain */
$view_text = esc_html__ ( 'View post' ); // Intentionally omitted domain.
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
if ( 'jetpack-portfolio' === $post_type ) {
2019-11-15 23:26:29 +01:00
$view_text = esc_html__ ( 'View project' , 'jetpack' );
}
2022-04-02 10:26:41 +02:00
$view_post_link_html = sprintf (
' <a href="%1$s">%2$s</a>' ,
2019-11-15 23:26:29 +01:00
esc_url ( get_permalink ( $post ) ),
$view_text
);
}
$services = $this -> get_publicizing_services ( $post -> ID );
if ( empty ( $services ) ) {
return $messages ;
}
$labels = array ();
foreach ( $services as $service_name => $display_names ) {
$labels [] = sprintf (
/* translators: Service name is %1$s, and account name is %2$s. */
esc_html__ ( '%1$s (%2$s)' , 'jetpack' ),
esc_html ( $service_name ),
2022-04-02 10:26:41 +02:00
esc_html ( is_array ( $display_names ) ? implode ( ', ' , $display_names ) : $display_names )
2019-11-15 23:26:29 +01:00
);
}
$messages [ 'post' ][ 6 ] = sprintf (
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
esc_html__ ( 'Post published and sharing on %1$s.' , 'jetpack' ),
implode ( ', ' , $labels )
) . $view_post_link_html ;
2022-04-02 10:26:41 +02:00
if ( 'post' === $post_type && class_exists ( 'Jetpack_Subscriptions' ) ) {
2019-11-15 23:26:29 +01:00
$subscription = Jetpack_Subscriptions :: init ();
if ( $subscription -> should_email_post_to_subscribers ( $post ) ) {
$messages [ 'post' ][ 6 ] = sprintf (
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
esc_html__ ( 'Post published, sending emails to subscribers and sharing post on %1$s.' , 'jetpack' ),
implode ( ', ' , $labels )
) . $view_post_link_html ;
}
}
$messages [ 'jetpack-portfolio' ][ 6 ] = sprintf (
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
esc_html__ ( 'Project published and sharing project on %1$s.' , 'jetpack' ),
implode ( ', ' , $labels )
) . $view_post_link_html ;
return $messages ;
}
/**
* Get the Connections the Post was just Publicized to .
*
* Only reliable just after the Post was published .
*
2022-04-02 10:26:41 +02:00
* @ param int $post_id Post ID .
2019-11-15 23:26:29 +01:00
* @ return string [] Array of Service display name => Connection display name
*/
2022-04-02 10:26:41 +02:00
public function get_publicizing_services ( $post_id ) {
2019-11-15 23:26:29 +01:00
$services = array ();
foreach ( ( array ) $this -> get_services ( 'connected' ) as $service_name => $connections ) {
// services have multiple connections.
foreach ( $connections as $connection ) {
$unique_id = '' ;
2022-04-02 10:26:41 +02:00
if ( ! empty ( $connection -> unique_id ) ) {
2019-11-15 23:26:29 +01:00
$unique_id = $connection -> unique_id ;
2022-04-02 10:26:41 +02:00
} elseif ( ! empty ( $connection [ 'connection_data' ][ 'token_id' ] ) ) {
2019-11-15 23:26:29 +01:00
$unique_id = $connection [ 'connection_data' ][ 'token_id' ];
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
// Did we skip this connection?
2022-04-02 10:26:41 +02:00
if ( get_post_meta ( $post_id , $this -> POST_SKIP . $unique_id , true ) ) {
2019-11-15 23:26:29 +01:00
continue ;
}
$services [ $this -> get_service_label ( $service_name ) ][] = $this -> get_display_name ( $service_name , $connection );
}
}
return $services ;
}
/**
* Is the post Publicize - able ?
*
* Only valid prior to Publicizing a Post .
*
2022-04-02 10:26:41 +02:00
* @ param WP_Post $post Post to check .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function post_is_publicizeable ( $post ) {
if ( ! $this -> post_type_is_publicizeable ( $post -> post_type ) ) {
2019-11-15 23:26:29 +01:00
return false ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
2022-04-02 10:26:41 +02:00
// This is more a precaution. To only publicize posts that are published. (Mostly relevant for Jetpack sites).
2019-11-15 23:26:29 +01:00
if ( 'publish' !== $post -> post_status ) {
return false ;
}
2022-04-02 10:26:41 +02:00
// If it's not flagged as ready, then abort. @see ::flag_post_for_publicize().
if ( ! get_post_meta ( $post -> ID , $this -> PENDING , true ) ) {
2019-11-15 23:26:29 +01:00
return false ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
return true ;
}
/**
* Is a given post type Publicize - able ?
*
* Not every CPT lends itself to Publicize - ation . Allow CPTs to register by adding their CPT via
* the publicize_post_types array filter .
*
* @ param string $post_type The post type to check .
* @ return bool True if the post type can be Publicized .
*/
2022-04-02 10:26:41 +02:00
public function post_type_is_publicizeable ( $post_type ) {
if ( 'post' === $post_type ) {
2019-11-15 23:26:29 +01:00
return true ;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
return post_type_supports ( $post_type , 'publicize' );
}
/**
* Already - published posts should not be Publicized by default . This filter sets checked to
* false if a post has already been published .
*
* Attached to the `publicize_checkbox_default` filter
*
2022-04-02 10:26:41 +02:00
* @ param bool $checked True if checkbox is checked , false otherwise .
* @ param int $post_id Post ID to set checkbox for .
2019-11-15 23:26:29 +01:00
* @ return bool
*/
2022-04-02 10:26:41 +02:00
public function publicize_checkbox_default ( $checked , $post_id ) {
if ( 'publish' === get_post_status ( $post_id ) ) {
2019-11-15 23:26:29 +01:00
return false ;
}
return $checked ;
}
2022-04-02 10:26:41 +02:00
/**
* Util
*/
2019-11-15 23:26:29 +01:00
/**
* Converts a Publicize message template string into a sprintf format string
*
2022-04-02 10:26:41 +02:00
* @ param string [] $args Array of arguments .
2019-11-15 23:26:29 +01:00
* 0 - The Publicize message template : 'Check out my post: %title% @ %url'
* ... - The template tags 'title' , 'url' , etc .
* @ return string
*/
protected static function build_sprintf ( $args ) {
2022-04-02 10:26:41 +02:00
$search = array ();
2019-11-15 23:26:29 +01:00
$replace = array ();
foreach ( $args as $k => $arg ) {
2022-04-02 10:26:41 +02:00
if ( 0 === $k ) {
2019-11-15 23:26:29 +01:00
$string = $arg ;
continue ;
}
2022-04-02 10:26:41 +02:00
$search [] = " % $arg % " ;
2019-11-15 23:26:29 +01:00
$replace [] = " % $k\ $s " ;
}
return str_replace ( $search , $replace , $string );
}
2022-06-16 14:01:47 +02:00
/**
* Get Calypso URL for Publicize connections .
*
* @ param string $source The idenfitier of the place the function is called from .
* @ return string
*/
public function publicize_connections_url ( $source = 'calypso-marketing-connections' ) {
$allowed_sources = array ( 'jetpack-social-connections-admin-page' , 'jetpack-social-connections-classic-editor' , 'calypso-marketing-connections' );
$source = in_array ( $source , $allowed_sources , true ) ? $source : 'calypso-marketing-connections' ;
return Redirect :: get_url ( $source , array ( 'site' => ( new Status () ) -> get_site_suffix () ) );
}
2019-11-15 23:26:29 +01:00
}
2022-04-02 10:26:41 +02:00
/**
* Get Calypso URL for Publicize connections .
*
* @ return string
*/
2019-11-15 23:26:29 +01:00
function publicize_calypso_url () {
2022-06-16 14:01:47 +02:00
_deprecated_function ( __METHOD__ , '11.0' , 'Publicize::publicize_connections_url' );
2020-12-10 14:04:11 +01:00
return Redirect :: get_url ( 'calypso-marketing-connections' , array ( 'site' => ( new Status () ) -> get_site_suffix () ) );
2019-11-15 23:26:29 +01:00
}