Skip to content

GH-28 Refactor code to add setting fields from an array #32

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

sudhir-yadav
Copy link

@sudhir-yadav sudhir-yadav commented Feb 3, 2023

This PR refactors the current settings to a single array of fields in one setting.

@sudhir-yadav sudhir-yadav changed the title GH-28 Refactor code to add setting fields from an array [WIP]GH-28 Refactor code to add setting fields from an array Feb 3, 2023
@sudhir-yadav sudhir-yadav changed the title [WIP]GH-28 Refactor code to add setting fields from an array GH-28 Refactor code to add setting fields from an array Feb 3, 2023
@sudhir-yadav sudhir-yadav marked this pull request as ready for review February 3, 2023 10:44
Comment on lines +85 to +95
foreach ( $script_settings as $option_name => $settings ) {

add_settings_field(
$settings[ 'id' ],
$settings[ 'title' ],
$settings[ 'callback' ],
$settings[ 'page' ],
$settings[ 'section' ]
);

register_setting( 'rt-scripts-optimizer-settings', $option_name );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sudhir-yadav @pradeep910, This is the exact same code that we had earlier just shorten by adding a foreach loop. I think we need to register only 1 settings and fetch other settings by array key. Something similar to below code.

// Define default array
$defaults = array(
   'setting_1' => 'default_value_1',
   'setting_2' => 'default_value_2',
);

// Register the setting
register_setting( 'my_settings_group', 'my_array_setting', array(
   'type' => 'array',
   'default' => $defaults,
) );

// Update code that uses the setting
$options = get_option( 'my_array_setting', $defaults );
$setting_1 = $options['setting_1'];
$setting_2 = $options['setting_2'];

Of course we need to handle for the settings that are saved with standalone options after implementing above. This is my suggestion, and it would best because we need to call just one option and that would be stored in an array we can fetch by array key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also settings callback would be same, there would be no change, just option fetch from settings key.

@pradeep910
Copy link
Collaborator

@hbhalodia Rechecking this, is the changes relevant and settings work with all our features or we can archive this?

@hbhalodia
Copy link
Contributor

@hbhalodia Rechecking this, is the changes relevant and settings work with all our features or we can archive this?

Hi @pradeep910, I guess this are still relevant, and can be updated, but need to make sure it account backward compatibility. We can assign to someone which can take up this PR or create a new one for Issue - #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants