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: Fix endpoint backwards compatibility #34566

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed backwards compatibility with Social store refactor
2 changes: 1 addition & 1 deletion projects/packages/publicize/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-publicize",
"version": "0.38.2",
"version": "0.38.3-alpha",
"description": "Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/publicize/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Class used to register REST API auto-conversion settings endpoints.
*
* Flagged to be removed after deprecation.
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
*
* @deprecated $$next_version$$
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize\Auto_Conversion;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as Jetpack_Social_Settings;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Server;

/**
* Defines our endpoints.
*/
class REST_Settings_Controller extends WP_REST_Controller {
/**
* Register REST API endpoints.
*
* @return void
*/
public function register_routes() {
register_rest_route(
'jetpack/v4',
'/auto-conversion/settings',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_auto_coversion_settings' ),
'permission_callback' => array( $this, 'settings_permissions_callback' ),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_auto_coversion_settings' ),
'permission_callback' => array( $this, 'settings_permissions_callback' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

/**
* GET `/jetpack/v4/auto-conversion/settings`
*
* @return WP_REST_Response
*/
public function get_auto_coversion_settings() {
$settings = ( new Jetpack_Social_Settings() )->get_settings();
$response = array();
$schema = $this->get_item_schema();
$properties = array_keys( $schema['properties'] );

if ( in_array( 'image', $properties, true ) ) {
$response['image'] = $settings['autoConversionSettings']['enabled'];
}

if ( in_array( 'auto-conversion', $properties, true ) ) {
$response['auto-conversion'] = $settings['autoConversionSettings']['enabled'];
}

return rest_ensure_response( $response );
}

/**
* POST `/jetpack/v4/auto-conversion/settings`
*
* @param WP_REST_Request $request The API request.
*
* @return WP_REST_Response|WP_Error
*/
public function update_auto_coversion_settings( $request ) {
$settings = new Jetpack_Social_Settings();

if ( isset( $request['image'] ) ) {
$settings->update_auto_conversion_setting( array( 'enabled' => $request['image'] ) );
}

return rest_ensure_response( $this->get_auto_coversion_settings() );
}

/**
* Check the permissions for accessing and updating the settings endpoint.
*
* @return bool|WP_Error True if user can manage options.
*/
public function settings_permissions_callback() {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error(
'rest_forbidden_context',
__( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-publicize-pkg' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Retrieves the settings schema, conforming to JSON Schema.
*
* @return array Schema data.
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'auto-conversion-settings',
'type' => 'object',
'properties' => array(
'image' => array(
'description' => __( 'Whether or not auto-conversion for images is enabled.', 'jetpack-publicize-pkg' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
),
'video' => array(
'description' => __( 'Whether or not auto-conversion for videos is enabled.', 'jetpack-publicize-pkg' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
),
'auto-conversion' => array(
'description' => __( 'Whether or not auto-conversion is enabled.', 'jetpack-publicize-pkg' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
),
),
);

return rest_default_additional_properties_to_false( $schema );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Settings class.
* Flagged to be removed after deprecation.
*
* @deprecated $$next_version$$
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize\Auto_Conversion;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as Jetpack_Social_Settings;

/**
* This class is used to get and update Auto_Conversion_Settings.
*/
class Settings {
/**
* Name of the database option.
*
* @var string
*/
const OPTION_NAME = 'jetpack_social_settings';

/**
* Array with auto conversion settings.
*
* @var array $settings
*/
public $settings;

/**
* Constructor.
*/
public function __construct() {
$this->settings = $this->get_settings();
}

/**
* Get the current auto conversion settings.
*
* @return array
*/
private function get_settings() {
$new_settings = ( new Jetpack_Social_Settings() )->get_settings();

return array(
'image' => $new_settings['autoConversionSettings']['enabled'],
);
}

/**
* Check if the auto conversion feature is available.
*
* @param string $type Whether video or image.
* @return bool True if available, false otherwise.
*/
public function is_available( $type ) {
return ( new Jetpack_Social_Settings() )->is_auto_conversion_available( $type );
}

/**
* Check if the auto conversion feature is enabled.
*
* @param string $type Whether video or image.
*
* @return bool True if the feature is enabled, false otherwise.
*/
public function is_enabled( $type ) {
if ( 'image' === $type ) {
$new_settings = ( new Jetpack_Social_Settings() )->get_settings();
return $new_settings['autoConversionSettings']['enabled'];
}
}
}
4 changes: 4 additions & 0 deletions projects/packages/publicize/src/class-publicize-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public static function on_jetpack_feature_publicize_enabled() {
add_action( 'rest_api_init', array( new Jetpack_Social_Settings\Settings(), 'register_settings' ) );
add_action( 'admin_init', array( new Jetpack_Social_Settings\Settings(), 'register_settings' ) );

// Flagged to be removed after deprecation.
// @deprecated $$next_version$$
add_action( 'rest_api_init', array( new Auto_Conversion\REST_Settings_Controller(), 'register_routes' ) );

( new Social_Image_Generator\Setup() )->init();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Class used to register REST API settings endpoints used by Social Image Generator.
*
* Flagged to be removed after deprecation.
*
* @deprecated $$next_version$$
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize\Social_Image_Generator;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as Jetpack_Social_Settings;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Server;

/**
* Defines our endpoints.
*/
class REST_Settings_Controller extends WP_REST_Controller {
/**
* Register REST API endpoints.
*
* @return void
*/
public function register_routes() {
register_rest_route(
'jetpack/v4',
'/social-image-generator/settings',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_settings' ),
'permission_callback' => array( $this, 'settings_permissions_callback' ),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_settings' ),
'permission_callback' => array( $this, 'settings_permissions_callback' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

/**
* GET `/jetpack/v4/social-image-generator/settings`
*
* @return WP_REST_Response
*/
public function get_settings() {
$settings = ( new Jetpack_Social_Settings() )->get_settings();
$response = array();
$schema = $this->get_item_schema();
$properties = array_keys( $schema['properties'] );

if ( in_array( 'enabled', $properties, true ) ) {
$response['enabled'] = $settings['socialImageGeneratorSettings']['enabled'];
}

if ( in_array( 'defaults', $properties, true ) ) {
$response['defaults'] = array( 'template' => $settings['socialImageGeneratorSettings']['template'] );
}

return rest_ensure_response( $response );
}

/**
* POST `/jetpack/v4/social-image-generator/settings`
*
* @param WP_REST_Request $request The API request.
*
* @return WP_REST_Response|WP_Error
*/
public function update_settings( $request ) {
$settings = new Jetpack_Social_Settings();

if ( isset( $request['enabled'] ) ) {
$settings->update_social_image_generator_settings( array( 'enabled' => $request['enabled'] ) );

}

if ( $request['defaults'] && $request['defaults']['template'] ) {
$settings->update_social_image_generator_settings( array( 'template' => $request['defaults']['template'] ) );
}

return rest_ensure_response( $this->get_settings() );
}

/**
* Check the permissions for accessing and updating the settings endpoint.
*
* @return bool|WP_Error True if user can manage options.
*/
public function settings_permissions_callback() {
if ( ! current_user_can( 'manage_options' ) ) {
return new WP_Error(
'rest_forbidden_context',
__( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-publicize-pkg' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Retrieves the Social Image Generator's settings schema, conforming to JSON Schema.
*
* @return array Schema data.
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'social-image-generator-settings',
'type' => 'object',
'properties' => array(
'enabled' => array(
'description' => __( 'Whether or not Social Image Generator is enabled.', 'jetpack-publicize-pkg' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
),
'defaults' => array(
'description' => __( 'The default settings for a new generated image.', 'jetpack-publicize-pkg' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'template' => array(
'type' => 'string',
'enum' => Templates::TEMPLATES,
),
),
),
),
);

return rest_default_additional_properties_to_false( $schema );
}
}
Loading
Loading