Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social | Add wpcom/v3/publicize/connections endpoint #40540

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions projects/packages/publicize/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
'file_suppressions' => [
'src/class-connections-post-field.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
'src/class-keyring-helper.php' => ['PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchDefault'],
'src/class-publicize-assets.php' => ['PhanNoopNew'],
'src/class-publicize-base.php' => ['PhanImpossibleCondition', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginSimplifyExpressionBool', 'PhanSuspiciousMagicConstant', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentNullable', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeMismatchDimFetch', 'PhanTypeMismatchReturn'],
'src/class-publicize-setup.php' => ['PhanTypeMismatchArgument'],
'src/class-publicize-ui.php' => ['PhanPluginDuplicateExpressionAssignmentOperation', 'PhanTypeMismatchReturnProbablyReal'],
'src/class-publicize.php' => ['PhanParamSignatureMismatch', 'PhanPossiblyUndeclaredVariable', 'PhanTypeMismatchArgument', 'PhanTypeMissingReturn'],
'src/class-rest-controller.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchReturnProbablyReal'],
'src/rest-endpoints/class-connections-controller.php' => ['PhanPluginMixedKeyNoKey'],
'src/social-image-generator/class-post-settings.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
'src/social-image-generator/class-rest-settings-controller.php' => ['PhanPluginMixedKeyNoKey'],
'src/social-image-generator/class-settings.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
Expand Down
1 change: 1 addition & 0 deletions projects/packages/publicize/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
__DIR__ . '/../../../plugins/jetpack/_inc/lib/admin-pages/class.jetpack-admin-page.php', // class Jetpack_Admin_Page
__DIR__ . '/../../../plugins/jetpack/modules/subscriptions.php', // class Jetpack_Subscriptions
__DIR__ . '/../../../plugins/jetpack/functions.global.php', // function jetpack_render_tos_blurb
__DIR__ . '/../../../plugins/jetpack/_inc/lib/core-api/load-wpcom-endpoints.php', // function wpcom_rest_api_v2_load_plugin
),
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added wpcom/v3/publicize/connections endpoint
170 changes: 170 additions & 0 deletions projects/packages/publicize/src/class-connection-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* Publicize Connection Fields class.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize;

/**
* Publicize Connection Fields class.
*/
class Connection_Fields {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods in this class are copied from Publicize_Base class which we can later on clean up to these methods as fallback after deprecating them.


/**
* Get the publicize instance - properly typed
*
* @return Publicize
*/
protected static function publicize() {
/**
* Publicize instance.
*
* @var Publicize $publicize
*/
global $publicize;

if ( ! $publicize && function_exists( 'publicize_init' ) ) {
// @phan-suppress-next-line PhanUndeclaredFunction - phan is dumb not to see the function_exists check
publicize_init();
}

return $publicize;
}

/**
* Get the meta of a connection.
*
* @param array|object $connection The connection.
* @return array
*/
protected static function get_connection_meta( $connection ) {

return self::publicize()->get_connection_meta( $connection );
}

/**
* Get the ID of a connection.
*
* @param array $connection The connection.
* @return string
*/
public static function get_connection_id( $connection ) {
return (string) self::publicize()->get_connection_id( $connection );
}

/**
* Returns a display name for the Connection
*
* @param string $service_name 'facebook', 'twitter', etc.
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
public static function get_display_name( $service_name, $connection ) {
return self::publicize()->get_display_name( $service_name, $connection );
}

/**
* Returns the external handle for the Connection.
*
* @param string $service_name 'facebook', 'linkedin', etc.
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
public static function get_external_handle( $service_name, $connection ) {
$cmeta = self::get_connection_meta( $connection );

switch ( $service_name ) {
case 'mastodon':
return $cmeta['external_display'] ?? '';

case 'bluesky':
case 'threads':
return $cmeta['external_name'] ?? '';

case 'instagram-business':
return $cmeta['connection_data']['meta']['username'] ?? '';

default:
return '';
}
}

/**
* Returns the external ID for the Connection.
*
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
public static function get_external_id( $connection ) {
$connection_meta = self::get_connection_meta( $connection );

return $connection_meta['external_id'] ?? '';
}

/**
* Returns an external URL to the Connection's profile
*
* @param string $service_name 'facebook', 'twitter', etc.
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return false|string False on failure. URL on success.
*/
public static function get_profile_link( $service_name, $connection ) {
return self::publicize()->get_profile_link( $service_name, $connection );
}

/**
* Returns a profile picture for the Connection
*
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
public static function get_profile_picture( $connection ) {
return self::publicize()->get_profile_picture( $connection );
}

/**
* Returns a display name for the Service
*
* @param string $service_name 'facebook', 'twitter', etc.
* @return string
*/
public static function get_service_label( $service_name ) {
return self::publicize()->get_service_label( $service_name );
}

/**
* Returns whether the Connection is shared
*
* @param array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return bool
*/
public static function is_shared( $connection ) {
return empty( self::get_user_id( $connection ) );
}

/**
* Returns the status for the Connection
*
* @param array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
public static function get_status( $connection ) {
return $connection['status'] ?? 'ok';
}

/**
* Returns the user ID for the Connection
*
* @param array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return int
*/
public static function get_user_id( $connection ) {
$connection_meta = self::get_connection_meta( $connection );

$connection_data = $connection_meta['connection_data'];

return (int) $connection_data['user_id'];
}
}
3 changes: 3 additions & 0 deletions projects/packages/publicize/src/class-publicize-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Publicize\Rest_Endpoints\Connections_Controller;

/**
* Publicize_Assets class.
*/
Expand All @@ -17,5 +19,6 @@ class Publicize_Assets {
*/
public static function configure() {
Publicize_Script_Data::configure();
new Connections_Controller();
}
}
8 changes: 4 additions & 4 deletions projects/packages/publicize/src/class-publicize-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ public function get_profile_link( $service_name, $connection ) {
return 'https://instagram.com/' . $cmeta['connection_data']['meta']['username'];
}

if ( 'threads' === $service_name && isset( $connection['external_name'] ) ) {
return 'https://www.threads.net/@' . $connection['external_name'];
if ( 'threads' === $service_name && isset( $cmeta['external_name'] ) ) {
return 'https://www.threads.net/@' . $cmeta['external_name'];
Comment on lines -500 to +501
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is to ensure it works on WPCOM sites where connection is not an array.

}

if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) {
Expand Down Expand Up @@ -527,7 +527,7 @@ public function get_profile_link( $service_name, $connection ) {
}

$profile_url_query = wp_parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY );
$profile_url_query_args = null;
$profile_url_query_args = array();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phan complained about this.

wp_parse_str( $profile_url_query, $profile_url_query_args );

$id = null;
Expand Down Expand Up @@ -608,7 +608,7 @@ public function get_username( $service_name, $connection ) {
* @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
* @return string
*/
private function get_profile_picture( $connection ) {
public function get_profile_picture( $connection ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it available externally for facade.

$cmeta = $this->get_connection_meta( $connection );

if ( isset( $cmeta['profile_picture'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Base Controller class.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize\Rest_Endpoints;

use Automattic\Jetpack\Status\Host;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;

/**
* Base controller for Publicize endpoints.
*/
abstract class Base_Controller extends WP_REST_Controller {

/**
* Constructor.
*/
public function __construct() {
$this->namespace = 'wpcom/v3';

$this->wpcom_is_wpcom_only_endpoint = true;
}

/**
* Check if we are on WPCOM.
*
* @return bool
*/
public static function is_wpcom() {
return ( new Host() )->is_wpcom_simple();
}

/**
* Filters out data based on ?_fields= request parameter
*
* @param array $item Item to prepare.
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_REST_Response filtered item
*/
public function prepare_item_for_response( $item, $request ) {
if ( ! is_callable( array( $this, 'get_fields_for_response' ) ) ) {
return rest_ensure_response( $item );
}

$fields = $this->get_fields_for_response( $request );

$response_data = array();
foreach ( $item as $field => $value ) {
if ( in_array( $field, $fields, true ) ) {
$response_data[ $field ] = $value;
}
}

return rest_ensure_response( $response_data );
}

/**
* Verify that user can access Publicize data
*
* @return true|WP_Error
*/
public function get_items_permission_check() {
global $publicize;

if ( ! $publicize ) {
return new WP_Error(
'publicize_not_available',
__( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack-publicize-pkg' ),
array( 'status' => rest_authorization_required_code() )
);
}

if ( $publicize->current_user_can_access_publicize_data() ) {
return true;
}

return new WP_Error(
'invalid_user_permission_publicize',
__( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack-publicize-pkg' ),
array( 'status' => rest_authorization_required_code() )
);
}
}
Loading
Loading