-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the new settings and rest controller files
- Loading branch information
Showing
2 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
projects/packages/publicize/src/jetpack-social-settings/class-rest-settings-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
/** | ||
* Class used to register REST API auto-conversion settings endpoints. | ||
* | ||
* @package automattic/jetpack-publicize | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Publicize\Jetpack_Social_Settings; | ||
|
||
use Automattic\Jetpack\Publicize\Social_Image_Generator\Templates; | ||
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', | ||
'/jetpack-social/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/jetpack-social/settings` | ||
* | ||
* @return WP_REST_Response | ||
*/ | ||
public function get_settings() { | ||
$settings = new Settings(); | ||
$response = $settings->get_settings(); | ||
|
||
return rest_ensure_response( $response ); | ||
} | ||
|
||
/** | ||
* POST `/jetpack/v4/jetpack-social/settings` | ||
* | ||
* @param WP_REST_Request $request The API request. | ||
* | ||
* @return WP_REST_Response|WP_Error | ||
*/ | ||
public function update_settings( $request ) { | ||
$settings = new Settings(); | ||
|
||
if ( isset( $request['autoConversionSettings'] ) ) { | ||
$settings->update_auto_conversion_settings( $request['autoConversionSettings'] ); | ||
} | ||
|
||
if ( isset( $request['socialImageGeneratorSettings'] ) ) { | ||
$settings->update_social_image_generator_settings( $request['socialImageGeneratorSettings'] ); | ||
} | ||
|
||
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( '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' => 'jetpack-social-settings', | ||
'type' => 'object', | ||
'properties' => array( | ||
'autoConversionSettings' => array( | ||
'description' => __( 'The auto-conversion settings for Jetpack Social', 'jetpack-publicize-pkg' ), | ||
'type' => 'object', | ||
'context' => array( 'view', 'edit' ), | ||
'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' ), | ||
), | ||
), | ||
), | ||
'socialImageGeneratorSettings' => array( | ||
'description' => __( 'The Social Image Generator settings for Jetpack Social', 'jetpack-publicize-pkg' ), | ||
'type' => 'object', | ||
'context' => array( 'view', 'edit' ), | ||
'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 ); | ||
} | ||
} |
187 changes: 187 additions & 0 deletions
187
projects/packages/publicize/src/jetpack-social-settings/class-settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
<?php | ||
/** | ||
* Settings class. | ||
* | ||
* @package automattic/jetpack-publicize | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Publicize\Jetpack_Social_Settings; | ||
|
||
use Automattic\Jetpack\Modules; | ||
use Automattic\Jetpack\Publicize\Social_Image_Generator\Templates; | ||
|
||
/** | ||
* This class is used to get and update Jetpack_Social_Settings. | ||
* Currently supported features: | ||
* - Social Image Generator | ||
* - Auto Conversion | ||
*/ | ||
class Settings { | ||
/** | ||
* Name of the database option. | ||
* | ||
* @var string | ||
*/ | ||
const OPTION_NAME = 'jetpack_social_settings'; | ||
|
||
/** | ||
* Array with the settings. | ||
* | ||
* @var array $settings | ||
*/ | ||
public $settings; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->settings = $this->get_settings(); | ||
|
||
if ( ! isset( $this->settings['socialImageGeneratorSettings'] ) ) { | ||
update_option( self::OPTION_NAME, $this->migrate_old_options( $this->settings ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Migrate old options to the new settings. Previously SIG settings were stored in the | ||
* jetpack_social_image_generator_settings option. Now they are stored in the jetpack_social_settings | ||
* together with the auto conversion settings. | ||
* | ||
* @return array | ||
*/ | ||
private function migrate_old_options() { | ||
$auto_conversion_settings = get_option( 'jetpack_social_settings' ); | ||
$sig_settings = get_option( 'jetpack_social_image_generator_settings' ); | ||
|
||
if ( empty( $auto_conversion_settings ) || ! is_array( $auto_conversion_settings ) ) { | ||
$auto_conversion_settings = array( | ||
'image' => true, | ||
); | ||
} | ||
|
||
if ( empty( $sig_settings ) || ! is_array( $sig_settings ) ) { | ||
$sig_settings = array( | ||
'enabled' => false, | ||
'defaults' => array( | ||
'template' => Templates::DEFAULT_TEMPLATE, | ||
), | ||
); | ||
} | ||
|
||
if ( ! isset( $sig_settings['defaults'] ) ) { | ||
$sig_settings['defaults'] = array( | ||
'template' => Templates::DEFAULT_TEMPLATE, | ||
); | ||
} | ||
|
||
return array( | ||
'autoConversionSettings' => $auto_conversion_settings, | ||
'socialImageGeneratorSettings' => $sig_settings, | ||
); | ||
} | ||
|
||
/** | ||
* Get the current settings. | ||
* | ||
* @param bool $with_available Whether to include the features availability in the response. | ||
* @return array | ||
*/ | ||
public function get_settings( $with_available = false ) { | ||
$settings = get_option( self::OPTION_NAME ); | ||
|
||
if ( empty( $settings ) || ! is_array( $settings ) ) { | ||
return array(); | ||
} | ||
|
||
// The feature cannot be enabled without Publicize. | ||
if ( ! ( new Modules() )->is_active( 'publicize' ) ) { | ||
$settings['autoConversionSettings']['image'] = false; | ||
$settings['socialImageGeneratorSettings']['enabled'] = false; | ||
} | ||
|
||
if ( $with_available ) { | ||
$settings['autoConversionSettings']['available'] = $this->is_auto_conversion_available(); | ||
$settings['socialImageGeneratorSettings']['available'] = $this->is_sig_available(); | ||
} | ||
|
||
return $settings; | ||
} | ||
|
||
/** | ||
* Update the settings. | ||
* | ||
* @param array $settings The new settings to update. | ||
*/ | ||
public function update_settings( $settings ) { | ||
$this->settings = $settings; | ||
return update_option( self::OPTION_NAME, $settings ); | ||
} | ||
|
||
/** | ||
* Update the auto conversion settings only respectively | ||
* | ||
* @param array $new_settings The new settings to update. | ||
*/ | ||
public function update_auto_conversion_settings( $new_settings ) { | ||
$settings = $this->get_settings(); | ||
$auto_conversion_settings = array_replace_recursive( $settings['autoConversionSettings'], $new_settings ); | ||
|
||
$settings['autoConversionSettings'] = $auto_conversion_settings; | ||
return $this->update_settings( $settings ); | ||
} | ||
|
||
/** | ||
* Update the social image generator settings only respectively | ||
* | ||
* @param array $new_settings The new settings to update. | ||
*/ | ||
public function update_social_image_generator_settings( $new_settings ) { | ||
$settings = $this->get_settings(); | ||
$sig_settings = array_replace_recursive( $settings['socialImageGeneratorSettings'], $new_settings ); | ||
|
||
$settings['socialImageGeneratorSettings'] = $sig_settings; | ||
return $this->update_settings( $settings ); | ||
} | ||
|
||
/** | ||
* Check if SIG is available. | ||
* | ||
* @return bool True if SIG is available, false otherwise. | ||
*/ | ||
public function is_sig_available() { | ||
global $publicize; | ||
|
||
if ( ! $publicize ) { | ||
return false; | ||
} | ||
|
||
return $publicize->has_social_image_generator_feature(); | ||
} | ||
|
||
/** | ||
* 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_auto_conversion_available( $type = 'image' ) { | ||
global $publicize; | ||
|
||
if ( ! $publicize ) { | ||
return false; | ||
} | ||
|
||
return $publicize->has_social_auto_conversion_feature( $type ); | ||
} | ||
|
||
/** | ||
* Get the default template. | ||
* | ||
* @return string | ||
*/ | ||
public function sig_get_default_template() { | ||
return isset( $this->settings['socialImageGeneratorSettings']['defaults']['template'] ) ? | ||
$this->settings['socialImageGeneratorSettings']['defaults']['template'] : Templates::DEFAULT_TEMPLATE; | ||
} | ||
} |