-
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.
- Loading branch information
Showing
4 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
251 changes: 251 additions & 0 deletions
251
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,251 @@ | ||
<?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_PREFIX = 'jetpack_social_'; | ||
// cSpell:ignore AUTOCONVERT | ||
const AUTOCONVERT_IMAGES = 'autoconvert_images'; | ||
const IMAGE_GENERATOR_SETTINGS = 'image_generator_settings'; | ||
|
||
const DEFAULT_IMAGE_GENERATOR_SETTINGS = array( | ||
'enabled' => false, | ||
'template' => Templates::DEFAULT_TEMPLATE, | ||
); | ||
|
||
const DEFAULT_AUTOCONVERT_IMAGES_SETTINGS = array( | ||
'enabled' => true, | ||
); | ||
|
||
/** | ||
* 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. | ||
* | ||
* TODO: Work out if this is possible on plugin upgrade | ||
* | ||
* @return void | ||
*/ | ||
private function migrate_old_option() { | ||
$auto_conversion_settings = get_option( 'jetpack_social_settings' ); | ||
if ( ! empty( $auto_conversion_settings['image'] ) ) { | ||
update_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, array( 'enabled' => $auto_conversion_settings['image'] ) ); | ||
delete_option( 'jetpack_social_settings' ); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
$sig_settings = get_option( 'jetpack_social_image_generator_settings' ); | ||
if ( isset( $sig_settings['defaults']['template'] ) ) { | ||
update_option( | ||
self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, | ||
array( | ||
'enabled' => $sig_settings['enabled'], | ||
'template' => $sig_settings['defaults']['template'], | ||
) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Register the settings. | ||
* | ||
* @return void | ||
*/ | ||
public function register_settings() { | ||
register_setting( | ||
'general', | ||
self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, | ||
array( | ||
'default' => array( | ||
'enabled' => true, | ||
), | ||
'show_in_rest' => array( | ||
'schema' => array( | ||
'type' => 'object', | ||
'properties' => array( | ||
'enabled' => array( | ||
'type' => 'boolean', | ||
), | ||
), | ||
), | ||
), | ||
'type' => 'boolean', | ||
) | ||
); | ||
|
||
register_setting( | ||
'general', | ||
self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, | ||
array( | ||
'type' => 'object', | ||
'default' => array( | ||
'enabled' => false, | ||
'template' => Templates::DEFAULT_TEMPLATE, | ||
), | ||
'show_in_rest' => array( | ||
'schema' => array( | ||
'type' => 'object', | ||
'properties' => array( | ||
'enabled' => array( | ||
'type' => 'boolean', | ||
), | ||
'template' => array( | ||
'type' => 'string', | ||
), | ||
), | ||
), | ||
), | ||
) | ||
); | ||
|
||
add_filter( 'rest_pre_update_setting', array( $this, 'update_settings' ), 10, 3 ); | ||
} | ||
|
||
/** | ||
* Get the current settings. | ||
* | ||
* @param bool $with_available Whether to include the available status of the features. | ||
* | ||
* @return array | ||
*/ | ||
public function get_settings( $with_available = false ) { | ||
$this->migrate_old_option(); | ||
|
||
$settings = array( | ||
'autoConversionSettings' => get_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, self::DEFAULT_AUTOCONVERT_IMAGES_SETTINGS ), | ||
'socialImageGeneratorSettings' => get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, self::DEFAULT_IMAGE_GENERATOR_SETTINGS ), | ||
); | ||
|
||
// The feature cannot be enabled without Publicize. | ||
if ( ! ( new Modules() )->is_active( 'publicize' ) ) { | ||
$settings['autoConversionSettings']['enabled'] = 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 bool $updated The updated settings. | ||
* @param string $name The name of the setting. | ||
* @param mixed $value The value of the setting. | ||
* | ||
* @return bool | ||
*/ | ||
public function update_settings( $updated, $name, $value ) { | ||
if ( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES === $name ) { | ||
return $this->update_auto_conversion_setting( $value ); | ||
} | ||
|
||
if ( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS === $name ) { | ||
return $this->update_social_image_generator_settings( $value ); | ||
} | ||
return $updated; | ||
} | ||
|
||
/** | ||
* Update the auto conversion settings. | ||
* | ||
* @param array $new_setting The new settings. | ||
* | ||
* @return bool | ||
*/ | ||
public function update_auto_conversion_setting( $new_setting ) { | ||
$this->migrate_old_option(); | ||
$auto_conversion_settings = get_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES ); | ||
|
||
if ( empty( $auto_conversion_settings ) || ! is_array( $auto_conversion_settings ) ) { | ||
$auto_conversion_settings = self::DEFAULT_AUTOCONVERT_IMAGES_SETTINGS; | ||
} | ||
|
||
return update_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, array_replace_recursive( $auto_conversion_settings, $new_setting ) ); | ||
} | ||
|
||
/** | ||
* Update the social image generator settings. | ||
* | ||
* @param array $new_setting The new settings. | ||
* | ||
* @return bool | ||
*/ | ||
public function update_social_image_generator_settings( $new_setting ) { | ||
$this->migrate_old_option(); | ||
$sig_settings = get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS ); | ||
|
||
if ( empty( $sig_settings ) || ! is_array( $sig_settings ) ) { | ||
$sig_settings = self::DEFAULT_IMAGE_GENERATOR_SETTINGS; | ||
} | ||
|
||
return update_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, array_replace_recursive( $sig_settings, $new_setting ) ); | ||
} | ||
|
||
/** | ||
* 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() { | ||
$sig_settings = get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS ); | ||
if ( empty( $sig_settings ) || ! is_array( $sig_settings ) ) { | ||
$sig_settings = self::DEFAULT_IMAGE_GENERATOR_SETTINGS; | ||
} | ||
return $sig_settings['template']; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.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,98 @@ | ||
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | ||
/** | ||
* Testing the Settings class. | ||
* | ||
* @package automattic/jetpack-publicize | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Publicize; | ||
|
||
use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as SocialSettings; | ||
use WorDBless\BaseTestCase; | ||
use WorDBless\Options as WorDBless_Options; | ||
use WorDBless\Posts as WorDBless_Posts; | ||
use WorDBless\Users as WorDBless_Users; | ||
|
||
/** | ||
* Testing the Settings class. | ||
*/ | ||
class Auto_Conversion_Test extends BaseTestCase { | ||
/** | ||
* Instance of the Settings class. | ||
* | ||
* @var Settings $settings | ||
*/ | ||
protected $settings; | ||
|
||
/** | ||
* Initialize tests | ||
* | ||
* @before | ||
*/ | ||
public function set_up() { | ||
add_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); | ||
global $publicize; | ||
$publicize = $this->getMockBuilder( Publicize::class )->setMethods( array( 'has_social_auto_conversion_feature' ) )->getMock(); | ||
$publicize->method( 'has_social_auto_conversion_feature' ) | ||
->withAnyParameters() | ||
->willReturn( true ); | ||
$this->settings = new SocialSettings(); | ||
$this->settings->register_settings(); | ||
} | ||
|
||
/** | ||
* Tear down | ||
* | ||
* @after | ||
*/ | ||
public function tear_down() { | ||
wp_set_current_user( 0 ); | ||
|
||
global $publicize; | ||
$publicize = new Publicize(); | ||
|
||
remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); | ||
WorDBless_Options::init()->clear_options(); | ||
WorDBless_Posts::init()->clear_all_posts(); | ||
WorDBless_Users::init()->clear_all_users(); | ||
} | ||
|
||
/** | ||
* Mock Publicize being active. | ||
* | ||
* @return array | ||
*/ | ||
public function mock_publicize_being_active() { | ||
return array( 'publicize' ); | ||
} | ||
|
||
/** | ||
* Test that Auto-Conversion is available based on the plan check. | ||
*/ | ||
public function test_correctly_returns_available_status() { | ||
$this->assertTrue( $this->settings->is_auto_conversion_available() ); | ||
} | ||
|
||
/** | ||
* Test that it correctly returns enabled or disabled. | ||
*/ | ||
public function test_correctly_returns_enabled_status() { | ||
$auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; | ||
$this->assertTrue( $auto_conversion_settings['enabled'] ); | ||
$this->assertFalse( isset( $auto_conversion_settings['video'] ) ? $auto_conversion_settings['video'] : false ); | ||
} | ||
|
||
/** | ||
* Test that it correctly returns enabled or disabled. | ||
*/ | ||
public function test_correctly_updates_enabled_status() { | ||
$this->settings->update_auto_conversion_setting( array( 'enabled' => false ) ); | ||
$auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; | ||
$this->assertFalse( $auto_conversion_settings['enabled'] ); | ||
|
||
$this->settings->update_auto_conversion_setting( array( 'video' => true ) ); | ||
$auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; | ||
$this->assertFalse( $auto_conversion_settings['enabled'] ); | ||
$this->assertTrue( $auto_conversion_settings['video'] ); | ||
} | ||
} |
Oops, something went wrong.
Part of the reason that we were using one setting with a complex structure was because the option has been added to the list of the ones which are sync'd. Regardless, it should probably have had another key in there about auto-conversion, rather than having
image
at the top level, but there will be backend changes needed with this change to ensure we're using the right option, and this new option is being sync'd correctly.It might be worth getting together with @spsiddarthan on this.