Skip to content
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

Show the default prompt as the first prompt in our settings #610

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public function setup_fields_sections() {
'label_for' => 'generate_excerpt_prompt',
'placeholder' => $this->generate_excerpt_prompt,
'default_value' => $default_settings['generate_excerpt_prompt'],
'description' => __( "Enter your custom prompt. Note the following variables that can be used in the prompt and will be replaced with content: {{WORDS}} will be replaced with the desired excerpt length setting. {{TITLE}} will be replaced with the item's title.", 'classifai' ),
'description' => __( "Note the following variables that can be used in the prompt and will be replaced with content: {{WORDS}} will be replaced with the desired excerpt length setting. {{TITLE}} will be replaced with the item's title.", 'classifai' ),
dkotter marked this conversation as resolved.
Show resolved Hide resolved
]
);

Expand Down Expand Up @@ -501,7 +501,6 @@ public function setup_fields_sections() {
'label_for' => 'generate_title_prompt',
'placeholder' => $this->generate_title_prompt,
'default_value' => $default_settings['generate_title_prompt'],
'description' => __( 'Enter a custom prompt, if desired.', 'classifai' ),
]
);

Expand Down Expand Up @@ -570,7 +569,6 @@ public function setup_fields_sections() {
'label_for' => 'shrink_content_prompt',
'placeholder' => $this->shrink_content_prompt,
'default_value' => $default_settings['shrink_content_prompt'],
'description' => __( 'Enter a custom prompt, if desired.', 'classifai' ),
]
);

Expand All @@ -585,7 +583,6 @@ public function setup_fields_sections() {
'label_for' => 'grow_content_prompt',
'placeholder' => $this->grow_content_prompt,
'default_value' => $default_settings['grow_content_prompt'],
'description' => __( 'Enter a custom prompt, if desired.', 'classifai' ),
]
);
}
Expand Down Expand Up @@ -713,36 +710,36 @@ public function get_default_settings() {
'length' => $excerpt_length,
'generate_excerpt_prompt' => array(
array(
'title' => esc_html__( 'Default', 'classifai' ),
'prompt' => '',
'default' => 1,
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->generate_excerpt_prompt,
'original' => 1,
),
),
'enable_titles' => false,
'title_roles' => array_keys( $editable_roles ),
'number_titles' => 1,
'generate_title_prompt' => array(
array(
'title' => esc_html__( 'Default', 'classifai' ),
'prompt' => '',
'default' => 1,
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->generate_title_prompt,
'original' => 1,
),
),
'enable_resize_content' => false,
'resize_content_roles' => array_keys( $editable_roles ),
'number_resize_content' => 1,
'shrink_content_prompt' => array(
array(
'title' => esc_html__( 'Default', 'classifai' ),
'prompt' => '',
'default' => 1,
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->shrink_content_prompt,
'original' => 1,
),
),
'grow_content_prompt' => array(
array(
'title' => esc_html__( 'Default', 'classifai' ),
'prompt' => '',
'default' => 1,
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->grow_content_prompt,
'original' => 1,
),
),
];
Expand Down Expand Up @@ -1219,9 +1216,10 @@ function ( $prompt ) use ( &$has_default ) {
}

return array(
'title' => sanitize_text_field( $prompt['title'] ),
'prompt' => sanitize_textarea_field( $prompt['prompt'] ),
'default' => absint( $default ),
'title' => sanitize_text_field( $prompt['title'] ),
'prompt' => sanitize_textarea_field( $prompt['prompt'] ),
'default' => absint( $default ),
'original' => absint( $prompt['original'] ),
);
},
$prompts
Expand Down Expand Up @@ -1251,14 +1249,14 @@ public function get_default_prompt( array $prompts ): ?string {
$prompt_data = array_filter(
$prompts,
function ( $prompt ) {
return $prompt['default'];
return $prompt['default'] && ! $prompt['original'];
}
);

if ( ! empty( $prompt_data ) ) {
$default_prompt = current( $prompt_data )['prompt'];
} elseif ( ! empty( $prompts[0]['prompt'] ) ) {
// If there is no default, use the first prompt.
} elseif ( ! empty( $prompts[0]['prompt'] ) && ! $prompts[0]['original'] ) {
// If there is no default, use the first prompt, unless it's the original prompt.
$default_prompt = $prompts[0]['prompt'];
}
}
Expand Down
36 changes: 29 additions & 7 deletions includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class="<?php echo esc_attr( $class ); ?>"
public function render_prompt_repeater_field( array $args ): void {
$option_index = $args['option_index'] ?? false;
$setting_index = $this->get_settings( $option_index );
$value = $setting_index[ $args['label_for'] ] ?? '';
$prompts = $setting_index[ $args['label_for'] ] ?? '';
$class = $args['class'] ?? 'large-text';
$placeholder = $args['placeholder'] ?? '';
$field_name_prefix = sprintf(
Expand All @@ -249,20 +249,40 @@ public function render_prompt_repeater_field( array $args ): void {
$args['label_for']
);

$value = ( empty( $value ) && isset( $args['default_value'] ) ) ? $args['default_value'] : $value;
$prompts = empty( $prompts ) && isset( $args['default_value'] ) ? $args['default_value'] : $prompts;

$prompt_count = count( $value );
$prompt_count = count( $prompts );
$field_index = 0;
?>

<?php foreach ( $value as $prompt ) : ?>
<?php $is_default_prompt = 1 === $prompt['default']; ?>
<?php foreach ( $prompts as $prompt ) : ?>
<?php
$is_default_prompt = ( isset( $prompt['default'] ) && 1 === $prompt['default'] ) || 1 === $prompt_count;
$is_original_prompt = isset( $prompt['original'] ) && 1 === $prompt['original'];
?>

<fieldset class="classifai-field-type-prompt-setting">
<?php if ( $is_original_prompt ) : ?>
<p class="classifai-original-prompt">
<?php
printf(
/* translators: %1$s is replaced with <strong>; %2$s with </strong>; %3$s with prompt. */
esc_html__( '%1$sClassifAI default prompt%2$s: %3$s', 'classifai' ),
'<strong>',
'</strong>',
esc_html( $placeholder )
);
?>
</p>
<?php endif; ?>

<input type="hidden"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][default]" ); ?>"
value="<?php echo esc_attr( $prompt['default'] ?? '' ); ?>"
class="js-setting-field__default">
<input type="hidden"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][original]" ); ?>"
value="<?php echo esc_attr( $prompt['original'] ?? '' ); ?>">
Copy link
Contributor

Choose a reason for hiding this comment

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

@dkotter I suggest use the trinary short syntax ?: instead of the null coalescing operator??, because original value can not be null, right?

Copy link
Collaborator Author

@dkotter dkotter Nov 6, 2023

Choose a reason for hiding this comment

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

So short ternary's aren't allowed by our PHPCS config so we would need to change this to ! empty( $prompt['original'] ? $prompt['original'] : '' which I'm fine with changing that if we feel it's better. The goal here was to be defensive for a situation where this array value wasn't set (which should never happen) and using the null coalescing operator would catch that.

<label>
<?php esc_html_e( 'Title', 'classifai' ); ?>&nbsp;*
<span class="dashicons dashicons-editor-help"
Expand All @@ -271,6 +291,7 @@ class="js-setting-field__default">
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][title]" ); ?>"
placeholder="<?php esc_attr_e( 'Prompt title', 'classifai' ); ?>"
value="<?php echo esc_attr( $prompt['title'] ?? '' ); ?>"
<?php echo $is_original_prompt ? 'readonly' : ''; ?>
required>
</label>

Expand All @@ -281,18 +302,19 @@ class="<?php echo esc_attr( $class ); ?>"
rows="4"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][prompt]" ); ?>"
placeholder="<?php echo esc_attr( $placeholder ); ?>"
<?php echo $is_original_prompt ? 'readonly' : ''; ?>
><?php echo esc_textarea( $prompt['prompt'] ?? '' ); ?></textarea>
</label>

<div class="actions-rows">
<a href="#" class="action__set_default <?php echo $is_default_prompt ? 'selected' : ''; ?>">
<?php if ( $is_default_prompt ) : ?>
<?php esc_html_e( 'Default Prompt', 'classifai' ); ?>
<?php esc_html_e( 'Default prompt', 'classifai' ); ?>
<?php else : ?>
<?php esc_html_e( 'Set as default prompt', 'classifai' ); ?>
<?php endif; ?>
</a>
<a href="#" class="action__remove_prompt" style="<?php echo 1 === $prompt_count ? 'display:none;' : ''; ?>">
<a href="#" class="action__remove_prompt" style="<?php echo 1 === $prompt_count || $is_original_prompt ? 'display:none;' : ''; ?>">
<?php esc_html_e( 'Trash', 'classifai' ); ?>
</a>
</div>
Expand Down
20 changes: 10 additions & 10 deletions src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
// Reset form fields.
fields.forEach( ( field ) => {
field.value = '';
field.removeAttribute( 'readonly' );

// Add index to field name.
field.name = field.name.replace(
Expand Down Expand Up @@ -200,7 +201,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
// Set selected class.
e.target.classList.add( 'selected' );

e.target.textContent = __( 'Default Prompt', 'classifai' );
e.target.textContent = __( 'Default prompt', 'classifai' );

// Set default value.
$newPromptFieldset.querySelector(
Expand Down Expand Up @@ -283,20 +284,19 @@ document.addEventListener( 'DOMContentLoaded', function () {
'.classifai-field-type-prompt-setting'
);

// Show remove button if fieldset is single.
if (
1 === $sibling.parentElement.querySelectorAll( 'fieldset' ).length
) {
$sibling.parentElement.querySelector(
'.action__remove_prompt'
).style.display = 'block';
}

const $newPromptFieldset = $promptFieldsetTemplate.cloneNode( true );

resetInputFields( $newPromptFieldset, $sibling.closest( 'tr' ) );
attachEventPromptFieldset( $newPromptFieldset );

$newPromptFieldset
.querySelector( '.classifai-original-prompt' )
.remove();

$newPromptFieldset.querySelector(
'.action__remove_prompt'
).style.display = 'block';

$sibling.insertAdjacentElement( 'afterend', $newPromptFieldset );

return $newPromptFieldset;
Expand Down
31 changes: 19 additions & 12 deletions src/scss/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -251,23 +251,30 @@ input.classifai-button {
line-height: 1.4;
}

.classifai-field-type-prompt-setting{
.classifai-field-type-prompt-setting {
padding: 5px 20px;
border: 1px solid #f0f0f1;
position: relative;
display: flex;
flex-direction:column;
max-width: 600px;

&:nth-child(2n + 1){
&:first-child {
> input,
> label {
display: none;
}
}

&:nth-child(2n + 1) {
background-color: #f0f0f1;
}

label{
label {
display: block;
}

input, textarea{
input, textarea {
margin-right: 0;
display: block;
}
Expand All @@ -277,44 +284,44 @@ input.classifai-button {
}

/* Style for prompt action items container */
.actions-rows{
.actions-rows {
display: flex;
margin: 0.35em 0 .5em!important;

/* Style for prompt actions */
a{
a {
cursor: pointer;
text-decoration: none;
margin-left: 8px;

&.action__remove_prompt{
&.action__remove_prompt {
color: #b32d2e;
}
}

/* action items separator */
a:before{
a:before {
content: '|';
color: #a7aaad;
}

/* Styling for default prompt first action */
a:first-child{
a:first-child {
margin-left: 0;

&.selected{
&.selected {
color: #a7aaad;
pointer-events: none;
}

&:before{
&:before {
content: '';
}
}

}

+ button{
+ button {
margin-top: 10px;
}
}
Expand Down
Loading
Loading