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

Added required to the required fields #216

Merged
merged 23 commits into from May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 17 additions & 26 deletions includes/admin/onboarding.php
Expand Up @@ -64,35 +64,21 @@ function onboarding_action_handler() {
return;
}

$podcast_name = isset( $_POST['podcast-name'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-name'] ) ) : null;
$podcast_description = isset( $_POST['podcast-description'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-description'] ) ) : null;
$podcast_category = isset( $_POST['podcast-category'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-category'] ) ) : null;
$podcast_cover_id = isset( $_POST['podcast-cover-image-id'] ) ? absint( wp_unslash( $_POST['podcast-cover-image-id'] ) ) : null;

if ( empty( $podcast_name ) || empty( $podcast_category ) ) {
add_action(
'admin_notices',
function() use ( $podcast_name, $podcast_category ) {
?>
<div class="notice notice-error is-dismissible">
<?php if ( '' === $podcast_name ) : ?>
<p><?php esc_html_e( 'Show name is required.', 'simple-podcasting' ); ?></p>
<?php endif; ?>

<?php if ( '' === $podcast_category ) : ?>
<p><?php esc_html_e( 'Podcast category is required.', 'simple-podcasting' ); ?></p>
<?php endif; ?>
</div>
<?php
}
);

return;
}
$podcast_name = isset( $_POST['podcast-name'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-name'] ) ) : null;
$podcasting_talent_name = isset( $_POST['podcast-artist'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-artist'] ) ) : null;
$podcast_description = isset( $_POST['podcast-description'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-description'] ) ) : null;
$podcast_category = isset( $_POST['podcast-category'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-category'] ) ) : null;
$podcast_cover_id = isset( $_POST['podcast-cover-image-id'] ) ? absint( wp_unslash( $_POST['podcast-cover-image-id'] ) ) : null;

$result = wp_insert_term(
$podcast_name,
TAXONOMY_NAME
TAXONOMY_NAME,
[
// Add these args for validation.
'podcasting_talent_name' => $podcasting_talent_name,
'podcasting_summary' => $podcast_description,
'podcasting_image' => $podcast_cover_id,
]
);

if ( is_wp_error( $result ) ) {
Expand All @@ -109,6 +95,11 @@ function() use ( $result ) {
return;
}

/** Add podcast author. */
Sidsector9 marked this conversation as resolved.
Show resolved Hide resolved
if ( $podcasting_talent_name ) {
update_term_meta( $result['term_id'], 'podcasting_talent_name', $podcasting_talent_name );
}

/** Add podcast summary. */
if ( $podcast_description ) {
update_term_meta( $result['term_id'], 'podcasting_summary', $podcast_description );
Expand Down
15 changes: 11 additions & 4 deletions includes/admin/views/onboarding-page-one.php
Expand Up @@ -19,17 +19,24 @@
<div class="simple-podcasting__setting-description"><?php esc_html_e( 'What’s the title of your podcast show that listeners will see?', 'simple-podcasting' ); ?></div>
</div>

<!-- Show artist/author -->
<div class="simple-podcasting__setting">
<label class="simple-podcasting__setting-label"><?php esc_html_e( 'Show Artist / Author name*', 'simple-podcasting' ); ?></label>
<div class="simple-podcasting__setting-input"><input name="podcast-artist" required type="text" /></div>
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
<div class="simple-podcasting__setting-description"><?php esc_html_e( 'Who’s the artist or author of your podcast show that listeners will see?', 'simple-podcasting' ); ?></div>
</div>

<!-- Show description -->
<div class="simple-podcasting__setting">
<label class="simple-podcasting__setting-label"><?php esc_html_e( 'Show description', 'simple-podcasting' ); ?></label>
<div class="simple-podcasting__setting-input"><textarea name="podcast-description" rows="5"></textarea></div>
<label class="simple-podcasting__setting-label"><?php esc_html_e( 'Show description*', 'simple-podcasting' ); ?></label>
<div class="simple-podcasting__setting-input"><textarea name="podcast-description" rows="5" required></textarea></div>
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
<div class="simple-podcasting__setting-description"><?php esc_html_e( 'Briefly describe to your listeners what your podcast is about. (No HTML please)', 'simple-podcasting' ); ?></div>
</div>

<!-- Cover image -->
<div class="simple-podcasting__setting">
<input type="hidden" name="podcast-cover-image-id" value="">
<label class="simple-podcasting__setting-label"><?php esc_html_e( 'Cover image', 'simple-podcasting' ); ?></label>
<input type="hidden" name="podcast-cover-image-id" value="" required>
<label class="simple-podcasting__setting-label"><?php esc_html_e( 'Cover image*', 'simple-podcasting' ); ?></label>
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
<div id="simple-podcasting__cover-image-preview"></div>
<button type="button" class="simple-podcasting__btn simple-podcasting__btn--ghost" id="simple-podcasting__upload-cover-image"><?php esc_html_e( 'Select image', 'simple-podcasting' ); ?></button>
<div class="simple-podcasting__setting-description"><?php esc_html_e( 'Minimum size: 1400px x 1400 px — maximum size: 2048px x 2048px. Make sure the image is square so it will properly display within podcatcher apps.', 'simple-podcasting' ); ?></div>
Expand Down
71 changes: 68 additions & 3 deletions includes/datatypes.php
Expand Up @@ -524,7 +524,7 @@ function get_meta_fields() {
),
array(
'slug' => 'podcasting_talent_name',
'title' => __( 'Artist / Author name', 'simple-podcasting' ),
'title' => __( 'Artist / Author name (required)', 'simple-podcasting' ),
'type' => 'textfield',
),
array(
Expand All @@ -534,7 +534,7 @@ function get_meta_fields() {
),
array(
'slug' => 'podcasting_summary',
'title' => __( 'Summary', 'simple-podcasting' ),
'title' => __( 'Summary (required)', 'simple-podcasting' ),
'type' => 'textarea',
),
array(
Expand All @@ -560,7 +560,7 @@ function get_meta_fields() {
),
array(
'slug' => 'podcasting_image',
'title' => __( 'Cover image', 'simple-podcasting' ),
'title' => __( 'Cover image (required)', 'simple-podcasting' ),
'type' => 'image',
'description' => __( 'Minimum size: 1400px x 1400 px — maximum size: 2048px x 2048px', 'simple-podcasting' ),
),
Expand Down Expand Up @@ -848,3 +848,68 @@ function get_podcasting_language_options() {
)
);
}

/**
* Validate Podcast Taxonomy Fields.
*
* @param string $term Term.
* @param string $taxonomy Taxonomy Name.
* @param array $args List of arguments.
*
* @return string
*/
function validate_taxonomy_fields( $term, $taxonomy, $args ) {

Choose a reason for hiding this comment

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

On edit-tags.php?taxonomy=podcasting_podcasts&podcasts=true, if this function throws an error, it does not save selections for the following:

  • Cover image
  • Category 1-3

On admin.php?page=simple-podcasting-onboarding&step=1 if it throws a Cover image error, it resets the entire form.

// Bailout, if not the podcasts taxonomy.
if ( 'podcasting_podcasts' !== $taxonomy ) {
return $term;
}

$referer = sanitize_text_field( $_POST['_wp_http_referer'] );
$is_onboarding_step_1 = false !== strpos( $referer, 'page=simple-podcasting-onboarding&step=1' );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved

if ( ! $is_onboarding_step_1 && empty( trim( $term ) ) ) {
return new \WP_Error( 'empty_term_name', __( 'A podcast name is required.' ) );
Sidsector9 marked this conversation as resolved.
Show resolved Hide resolved
}

if ( $is_onboarding_step_1 ) {
$args['tag-name'] = sanitize_title( $_POST['podcast-name'] );
$args['podcasting_category_1'] = sanitize_text_field( $_POST['podcast-category'] );
Sidsector9 marked this conversation as resolved.
Show resolved Hide resolved
}

// Require podcast name.
if ( empty( trim( $args['tag-name'] ) ) ) {
return new \WP_Error( 'empty_term_name', __( 'A podcast name is required.' ) );
}

// Require podcast author name only on term edit screen.
if ( empty( trim( $args['podcasting_talent_name'] ) ) ) {
return new \WP_Error( 'empty_term_talent_name', __( 'A podcast artist or author name is required.' ) );
}

// Require podcast description.
if ( empty( trim( $args['podcasting_summary'] ) ) ) {
return new \WP_Error( 'empty_term_summary', __( 'A podcast summary is required.' ) );
}

// Require podcast image.
if ( empty( trim( $args['podcasting_image'] ) ) ) {
return new \WP_Error( 'empty_term_cover_image', __( 'A podcast cover image is required.' ) );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
}

// Require podcast category.
$is_missing_category = $is_onboarding_step_1 ?
empty( trim( $args['podcasting_category_1'] ) ) :
(
empty( trim( $args['podcasting_category_1'] ) ) &&
empty( trim( $args['podcasting_category_2'] ) ) &&
empty( trim( $args['podcasting_category_3'] ) )
);

if ( $is_missing_category ) {
ggutenberg marked this conversation as resolved.
Show resolved Hide resolved
return new \WP_Error( 'empty_term_category', __( 'A podcast category is required.' ) );
}

return $term;
}

add_filter( 'pre_insert_term', __NAMESPACE__ . '\validate_taxonomy_fields', 10, 3 );
Binary file added tests/cypress/fixtures/example.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions tests/cypress/integration/block.test.js
Expand Up @@ -4,8 +4,17 @@ describe('Admin can publish posts with podcast block', () => {
if (Cypress.env('HAS_BLOCK_EDITOR')) {
it('Can insert the block and publish the post', () => {
cy.login();

cy.createTerm(taxonomy, 'podcasting_podcasts');
cy.uploadMedia('tests/cypress/fixtures/example.jpg');
cy.createTerm(taxonomy, 'podcasting_podcasts', {
beforeSave: () => {
cy.get('#podcasting_talent_name').type('Person Doe');
cy.get('#podcasting_summary').type('Lorem ipsum dolor');
cy.get('#image-podcasting_image').click();
cy.get('#menu-item-browse').click();
cy.get('.attachments-wrapper').click();
cy.get('.media-button-select').click();
},
});

cy.visit('/wp-admin/post-new.php');
cy.closeWelcomeGuide();
Expand Down
13 changes: 10 additions & 3 deletions tests/cypress/integration/onboarding.test.js
Expand Up @@ -6,6 +6,7 @@ describe('Onboarding tests', () => {
});

beforeEach(() => {
cy.uploadMedia('tests/cypress/fixtures/example.jpg');
cy.activatePlugin('simple-podcasting');
cy.deleteAllTerms('podcasting_podcasts');
cy.deactivatePlugin('simple-podcasting');
Expand All @@ -29,8 +30,7 @@ describe('Onboarding tests', () => {
.closest('form')
.submit();

cy.contains('Show name is required.');
cy.contains('Podcast category is required.');
cy.contains('Taxonomy error: A podcast name is required..');
});

it('Should pass onboarding', () => {
Expand All @@ -39,8 +39,15 @@ describe('Onboarding tests', () => {

const podcastName = 'Onboarding ' + randomName();
cy.get('input[name=podcast-name]').click().type(podcastName);
cy.get('input[name="podcast-artist"]').type('Person Doe');
cy.get('textarea[name="podcast-description"]').type(
'Lorem ipsum dolor'
);
cy.get('#simple-podcasting__upload-cover-image').click();
cy.get('#menu-item-browse').click();
cy.get('.attachments-wrapper').click();
cy.get('.media-button-select').click();
cy.get('select[name=podcast-category]').select('Arts');

cy.get('#simple-podcasting__create-podcast-button')
.closest('form')
.submit();
Expand Down
19 changes: 18 additions & 1 deletion tests/cypress/integration/taxonomy.test.js
Expand Up @@ -30,7 +30,17 @@ describe('Admin can create and update podcast taxonomy', () => {
});

it('Can add a new taxonomy', () => {
cy.createTerm('Remote work', 'podcasting_podcasts');
cy.uploadMedia('tests/cypress/fixtures/example.jpg');
cy.createTerm('Remote work', 'podcasting_podcasts', {
beforeSave: () => {
cy.get('#podcasting_talent_name').type('Person Doe');
cy.get('#podcasting_summary').type('Lorem ipsum dolor');
cy.get('#image-podcasting_image').click();
cy.get('#menu-item-browse').click();
cy.get('.attachments-wrapper').click();
cy.get('.media-button-select').click();
},
});
cy.get('.row-title').should('have.text', 'Remote work');
});

Expand Down Expand Up @@ -76,9 +86,16 @@ describe('Admin can create and update podcast taxonomy', () => {
for (const [typeOfShowKey, typeOfShowName] of Object.entries(tests)) {
it(`Can add taxonomy with ${typeOfShowName} type of show`, () => {
const podcastName = 'Podcast ' + randomName();
cy.uploadMedia('tests/cypress/fixtures/example.jpg');
cy.createTerm(podcastName, 'podcasting_podcasts', {
beforeSave: () => {
cy.get('#podcasting_type_of_show').select(typeOfShowName);
cy.get('#podcasting_talent_name').type('Person Doe');
cy.get('#podcasting_summary').type('Lorem ipsum dolor');
cy.get('#image-podcasting_image').click();
cy.get('#menu-item-browse').click();
cy.get('.attachments-wrapper').click();
cy.get('.media-button-select').click();
},
}).then((term) => {
cy.visit(
Expand Down