Skip to content

Commit

Permalink
Update the current rest controllers to use the new Settings class
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjuhasz committed Nov 8, 2023
1 parent 44dca45 commit cfa1dab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Publicize\Auto_Conversion;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as SocialSettings;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Server;
Expand Down Expand Up @@ -47,21 +48,13 @@ public function register_routes() {
* @return WP_REST_Response
*/
public function get_auto_coversion_settings() {
$settings = new Settings();
$settings = ( new SocialSettings() )->get_settings();
$response = array();
$schema = $this->get_item_schema();
$properties = array_keys( $schema['properties'] );

if ( in_array( 'image', $properties, true ) ) {
$response['image'] = $settings->is_enabled( 'image' );
}

if ( in_array( 'video', $properties, true ) ) {
$response['video'] = $settings->is_enabled( 'video' );
}

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

return rest_ensure_response( $response );
Expand All @@ -75,18 +68,10 @@ public function get_auto_coversion_settings() {
* @return WP_REST_Response|WP_Error
*/
public function update_auto_coversion_settings( $request ) {
$settings = new Settings();
$social_settings = new SocialSettings();

if ( isset( $request['image'] ) ) {
$settings->set_enabled( 'image', $request['image'] );
}

if ( isset( $request['video'] ) ) {
$settings->set_enabled( 'video', $request['video'] );
}

if ( isset( $request['auto-conversion'] ) ) {
$settings->set_enabled( 'auto-conversion', $request['auto-conversion'] );
$social_settings->update_auto_conversion_settings( (array) json_decode( $request->get_body() ) );
}

return rest_ensure_response( $this->get_auto_coversion_settings() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Publicize\Social_Image_Generator;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as SocialSettings;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Server;
Expand Down Expand Up @@ -47,17 +48,17 @@ public function register_routes() {
* @return WP_REST_Response
*/
public function get_settings() {
$settings = new Settings();
$settings = ( new SocialSettings() )->get_settings();
$response = array();
$schema = $this->get_item_schema();
$properties = array_keys( $schema['properties'] );

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

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

return rest_ensure_response( $response );
Expand All @@ -71,14 +72,14 @@ public function get_settings() {
* @return WP_REST_Response|WP_Error
*/
public function update_settings( $request ) {
$settings = new Settings();
$social_settings = new SocialSettings();

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

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

return rest_ensure_response( $this->get_settings() );
Expand Down

0 comments on commit cfa1dab

Please sign in to comment.