Skip to content

Commit

Permalink
add helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Nov 16, 2023
1 parent 6bb3d18 commit b7ae2dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
45 changes: 35 additions & 10 deletions includes/Classifai/Features/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ abstract public function setup_fields_sections();
*
* @return array
*/
abstract public function get_default_settings();
abstract protected function get_default_settings();

/**
* Returns the providers supported by the feature.
*
* @return array
*/
abstract public function get_providers();
abstract protected function get_providers();

/**
* Sanitizes the settings before saving.
Expand All @@ -95,7 +95,7 @@ abstract public function get_providers();
*
* @return array
*/
abstract public function sanitize_settings( $settings );
abstract protected function sanitize_settings( $settings );

/**
* Returns true if the feature meets all the criteria to be enabled. False otherwise.
Expand Down Expand Up @@ -149,7 +149,7 @@ public function get_option_name() {
public function get_settings( $index = false ) {
$defaults = $this->get_default_settings();
$settings = get_option( $this->get_option_name(), [] );
$settings = wp_parse_args( $settings, $defaults );
$settings = $this->merge_settings( $settings, $defaults );

if ( $index && isset( $settings[ $index ] ) ) {
return $settings[ $index ];
Expand All @@ -158,16 +158,39 @@ public function get_settings( $index = false ) {
return $settings;
}

/**
* Merges the data settings with the default settings recursively,
*
* @internal
*
* @param array $settings Settings data from the database.
* @param array $default Default feature and providers settings data.
*
* @return array
*/
protected function merge_settings( $settings = [], $default = [] ) {
foreach ( $default as $key => $value ) {
if ( ! isset( $settings[ $key ] ) ) {
$settings[ $key ] = $default[ $key ];
} else {
if ( is_array( $value ) ) {
$settings[ $key ] = $this->merge_settings( $settings[ $key ], $default[ $key ] );
}
}
}

return $settings;
}

/**
* Returns the instance of the provider set for the feature.
*
* @param string $provider_id The ID of the provider.
*
* @return \Classifai\Providers
*/
public function get_feature_provider_instance( $provider_id = '' ) {
$new_settings = $this->get_settings();
$provider_id = $provider_id ? $provider_id : $new_settings['provider'];
protected function get_feature_provider_instance( $provider_id = '' ) {
$provider_id = $provider_id ? $provider_id : $this->get_settings( 'provider' );
$provider_instance = find_provider_class( $this->provider_instances ?? [], $provider_id );

if ( is_wp_error( $provider_instance ) ) {
Expand Down Expand Up @@ -434,6 +457,7 @@ public function render_select( $args ) {
* @param array $args The args passed to add_settings_field
*/
public function render_checkbox_group( array $args = array() ) {
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings();

// Iterate through all of our options.
Expand All @@ -455,12 +479,13 @@ public function render_checkbox_group( array $args = array() ) {
printf(
'<p>
<label for="%1$s_%2$s_%3$s">
<input type="hidden" name="%1$s[%2$s][%3$s]" value="0" />
<input type="checkbox" id="%1$s_%2$s_%3$s" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />
%5$s
<input type="hidden" name="%1$s%2$s[%3$s][%4$s]" value="0" />
<input type="checkbox" id="%1$s_%3$s_%4$s" name="%1$s%2$s[%3$s][%4$s]" value="%4$s" %5$s />
%6$s
</label>
</p>',
esc_attr( $this->get_option_name() ),
$option_index ? '[' . esc_attr( $option_index ) . ']' : '',
esc_attr( $args['label_for'] ),
esc_attr( $option_value ),
checked( $value, $option_value, false ),
Expand Down
18 changes: 18 additions & 0 deletions includes/Classifai/Features/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ public function get_default_settings() {
];
}

/**
* The list of post types that TTS supports.
*
* @return array Supported Post Types.
*/
public function get_tts_supported_post_types() {
$selected = $this->get_settings( 'post_types' );
$post_types = [];

foreach ( $selected as $post_type => $enabled ) {
if ( ! empty( $enabled ) ) {
$post_types[] = $post_type;
}
}

return $post_types;
}

/**
* Sanitizes the settings before saving.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/Speech.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function register() {
add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
add_action( 'save_post', [ $this, 'save_post_metadata' ], 5 );

foreach ( get_tts_supported_post_types() as $post_type ) {
foreach ( $this->feature_instance->get_tts_supported_post_types() as $post_type ) {
add_action( 'rest_insert_' . $post_type, [ $this, 'rest_handle_audio' ], 10, 2 );
}

Expand Down
9 changes: 0 additions & 9 deletions includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ public function get_settings( $index = false ) {
return $settings;
}

/**
* Returns the default settings.
*
* @return array
*/
public function get_default_settings() {
return [];
}

/**
* Common entry point for all REST endpoints for this provider.
* This is called by the Service.
Expand Down

0 comments on commit b7ae2dd

Please sign in to comment.