Skip to content

Commit

Permalink
Update initial states + previous usages
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjuhasz committed Nov 14, 2023
1 parent f37dd1a commit d00d07e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 52 deletions.
2 changes: 1 addition & 1 deletion projects/packages/publicize/src/class-publicize-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ public function register_post_meta() {
'single' => true,
'default' => array(
'image_generator_settings' => array(
'template' => ( new Social_Image_Generator\Settings() )->get_default_template(),
'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(),
'enabled' => false,
),
),
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/publicize/src/class-publicize-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static function on_jetpack_feature_publicize_enabled() {
add_action( 'rest_api_init', array( new Connections_Post_Field(), 'register_fields' ), 5 );
add_action( 'rest_api_init', array( new REST_Controller(), 'register_rest_routes' ) );
add_action( 'current_screen', array( static::class, 'init_sharing_limits' ) );
add_action( 'rest_api_init', array( new Auto_Conversion\REST_Settings_Controller(), 'register_routes' ) );
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' ) );

( new Social_Image_Generator\Setup() )->init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Automattic\Jetpack\Publicize\Social_Image_Generator;

use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings;

/**
* Class for setting up Social Image Generator-related functionality.
*/
Expand All @@ -15,15 +17,14 @@ class Setup {
* Initialise SIG-related functionality.
*/
public function init() {
if ( ! ( new Settings() )->is_available() ) {
if ( ! ( new Settings() )->is_sig_available() ) {
return;
}

// Be wary of any code that you add to this file, since this function is called on plugin load.
// We're using the `wp_after_insert_post` hook because we need access to the updated post meta. By using the default priority
// of 10 we make sure that our code runs before Sync processes the post.
add_action( 'wp_after_insert_post', array( $this, 'generate_token_on_save' ), 10, 3 );
add_action( 'rest_api_init', array( new REST_Settings_Controller(), 'register_routes' ) );
add_action( 'rest_api_init', array( new REST_Token_Controller(), 'register_routes' ) );
}

Expand Down Expand Up @@ -75,7 +76,7 @@ public function generate_token_on_save( $post_id, $post, $update ) {

$settings = new Settings();

if ( ! $settings->is_available() ) {
if ( ! $settings->is_sig_available() ) {
return;
}

Expand All @@ -88,7 +89,7 @@ public function generate_token_on_save( $post_id, $post, $update ) {
if (
! $update &&
'auto-draft' === $post->post_status &&
$settings->is_enabled() &&
$settings->get_settings()['socialImageGeneratorSettings']['enabled'] &&

This comment has been minimized.

Copy link
@pablinos

pablinos Nov 27, 2023

Contributor

Do you think we should put this in a helper function on the settings class? is_social_image_generator_enabled or something? It would mean we wouldn't have to remember the structure.

This comment has been minimized.

Copy link
@pablinos

pablinos Nov 27, 2023

Contributor

I think it would be good to avoid the boolean parameter on get_settings if we can. It seems like we want to get the available value in this context too, so we could get that further up and check the available and enabled keys. If we do change it to is_social_image_generator_enabled or something we could get the single value instead.

This comment has been minimized.

Copy link
@gmjuhasz

gmjuhasz Nov 30, 2023

Author Contributor

That parameter comes handy for populating the initial state, where with the usage of it I can just pass the whole get_settings( true ) to the initial state, without having to create the array there. Should I just always return the available props? What do you think?

cc. @pablinos if you not get notifications on this

empty( $post_settings->get_settings( true ) )
) {
$post_settings->update_setting( 'enabled', true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,14 @@ private static function get_gutenberg_initial_state() {
* @return array|null
*/
public static function get_publicize_initial_state() {
$sig_settings = new Automattic\Jetpack\Publicize\Social_Image_Generator\Settings();
$auto_conversion_settings = new Automattic\Jetpack\Publicize\Auto_Conversion\Settings();
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
$settings = $jetpack_social_settings->get_settings( true );

if ( empty( $sig_settings ) && empty( $auto_conversion_settings ) ) {
if ( empty( $settings ) ) {
return null;
}

return array(
'socialImageGeneratorSettings' => array(
'available' => $sig_settings->is_available(),
'enabled' => $sig_settings->is_enabled(),
'defaultTemplate' => $sig_settings->get_default_template(),
),
'autoConversionSettings' => array(
'available' => $auto_conversion_settings->is_available( 'image' ),
'image' => $auto_conversion_settings->is_enabled( 'image' ),
),
);
return $settings;
}

/**
Expand Down
15 changes: 6 additions & 9 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,24 +732,21 @@ public static function enqueue_block_editor_assets() {
);

if ( Jetpack::is_module_active( 'publicize' ) && function_exists( 'publicize_init' ) ) {
$publicize = publicize_init();
$sig_settings = new Automattic\Jetpack\Publicize\Social_Image_Generator\Settings();
$auto_conversion_settings = new Automattic\Jetpack\Publicize\Auto_Conversion\Settings();
$publicize = publicize_init();
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
$settings = $jetpack_social_settings->get_settings( true );

$initial_state['social'] = array(
'sharesData' => $publicize->get_publicize_shares_info( $blog_id ),
'hasPaidPlan' => $publicize->has_paid_plan(),
'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(),
'isSocialImageGeneratorAvailable' => $sig_settings->is_available(),
'isSocialImageGeneratorEnabled' => $sig_settings->is_enabled(),
'isSocialImageGeneratorAvailable' => $settings['socialImageGeneratorSettings']['available'],
'isSocialImageGeneratorEnabled' => $settings['socialImageGeneratorSettings']['enabled'],
'dismissedNotices' => $publicize->get_dismissed_notices(),
'isInstagramConnectionSupported' => $publicize->has_instagram_connection_feature(),
'isMastodonConnectionSupported' => $publicize->has_mastodon_connection_feature(),
'isNextdoorConnectionSupported' => $publicize->has_nextdoor_connection_feature(),
'autoConversionSettings' => array(
'available' => $auto_conversion_settings->is_available( 'image' ),
'image' => $auto_conversion_settings->is_enabled( 'image' ),
),
'autoConversionSettings' => $settings['autoConversionSettings'],
'jetpackSharingSettingsUrl' => esc_url_raw( admin_url( 'admin.php?page=jetpack#/sharing' ) ),
);
}
Expand Down
35 changes: 12 additions & 23 deletions projects/plugins/social/src/class-jetpack-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ public function initial_state() {
);

if ( $this->is_connected() ) {
$sig_settings = new Automattic\Jetpack\Publicize\Social_Image_Generator\Settings();
$auto_conversion_settings = new Automattic\Jetpack\Publicize\Auto_Conversion\Settings();
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
$settings = $jetpack_social_settings->get_settings( true );

$state = array_merge(
$state,
array(
'jetpackSettings' => array(
'jetpackSettings' => array(
'publicize_active' => self::is_publicize_active(),
'show_pricing_page' => self::should_show_pricing_page(),
'showNudge' => ! $publicize->has_paid_plan( true ),
Expand All @@ -245,21 +245,13 @@ public function initial_state() {
'isMastodonConnectionSupported' => $publicize->has_mastodon_connection_feature(),
'isNextdoorConnectionSupported' => $publicize->has_nextdoor_connection_feature(),
),
'connectionData' => array(
'connectionData' => array(
'connections' => $publicize->get_all_connections_for_user(), // TODO: Sanitize the array
'adminUrl' => esc_url_raw( $publicize->publicize_connections_url( 'jetpack-social-connections-admin-page' ) ),
),
'sharesData' => $publicize->get_publicize_shares_info( Jetpack_Options::get_option( 'id' ) ),
'socialImageGeneratorSettings' => array(
'available' => $sig_settings->is_available(),
'enabled' => $sig_settings->is_enabled(),
'defaultTemplate' => $sig_settings->get_default_template(),
),
'autoConversionSettings' => array(
'available' => $auto_conversion_settings->is_available( 'image' ),
'image' => $auto_conversion_settings->is_enabled( 'image' ),
),
)
'sharesData' => $publicize->get_publicize_shares_info( Jetpack_Options::get_option( 'id' ) ),
),
$settings
);
}

Expand Down Expand Up @@ -326,8 +318,8 @@ class_exists( 'Jetpack' ) ||

Assets::enqueue_script( 'jetpack-social-editor' );

$sig_settings = ( new Automattic\Jetpack\Publicize\Social_Image_Generator\Settings() );
$auto_conversion_settings = ( new Automattic\Jetpack\Publicize\Auto_Conversion\Settings() );
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
$settings = $jetpack_social_settings->get_settings( true );

wp_localize_script(
'jetpack-social-editor',
Expand All @@ -344,12 +336,9 @@ class_exists( 'Jetpack' ) ||
),
'hasPaidPlan' => $publicize->has_paid_plan(),
'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(),
'isSocialImageGeneratorAvailable' => $sig_settings->is_available(),
'isSocialImageGeneratorEnabled' => $sig_settings->is_enabled(),
'autoConversionSettings' => array(
'available' => $auto_conversion_settings->is_available( 'image' ),
'image' => $auto_conversion_settings->is_enabled( 'image' ),
),
'isSocialImageGeneratorAvailable' => $settings['socialImageGeneratorSettings']['available'],
'isSocialImageGeneratorEnabled' => $settings['socialImageGeneratorSettings']['enabled'],
'autoConversionSettings' => $settings['autoConversionSettings'],
'dismissedNotices' => $publicize->get_dismissed_notices(),
'isInstagramConnectionSupported' => $publicize->has_instagram_connection_feature(),
'isMastodonConnectionSupported' => $publicize->has_mastodon_connection_feature(),
Expand Down

0 comments on commit d00d07e

Please sign in to comment.