array( 'type' => 'integer', 'minimum' => 1, ), 'name' => array( 'type' => 'string' ), ); } protected function output_properties(): array { return array( 'form_id' => array( 'type' => 'integer' ), 'removed' => array( 'type' => 'string' ), 'template' => array( 'type' => 'string' ), 'dangling_mail_tags' => array( 'type' => 'array' ), ); } /** * 'confirm' is intentionally absent — see Base_Contact_Form_7_Ability::ability(). */ protected function required_input(): array { return array( 'form_id', 'name' ); } protected function annotations(): array { return array( 'readonly' => false, 'destructive' => true, 'idempotent' => true, ); } protected function run( array $input ) { $form_id = absint( $input['form_id'] ?? 0 ); $form = Form_Repository::get( $form_id ); if ( is_wp_error( $form ) ) { return $form; } $allowed = Contact_Form_7_Guard::assert_can_edit( $form_id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $name = sanitize_text_field( (string) ( $input['name'] ?? '' ) ); $template = Form_Tag_Repository::remove_field( (string) $form->prop( 'form' ), $name ); if ( is_wp_error( $template ) ) { return $template; } $saved = Form_Repository::save( array( 'id' => $form_id, 'form' => $template, ) ); if ( is_wp_error( $saved ) ) { return $saved; } $fresh = Form_Repository::get( $form_id ); if ( is_wp_error( $fresh ) ) { return $fresh; } $dangling = array(); foreach ( array( 'mail', 'mail_2' ) as $which ) { $mail = (array) $fresh->prop( $which ); $body = isset( $mail['body'] ) ? (string) $mail['body'] : ''; foreach ( Form_Tag_Repository::unresolvable_tags( $fresh, $body ) as $tag ) { if ( ! in_array( $tag, $dangling, true ) ) { $dangling[] = $tag; } } } return array( 'form_id' => $form_id, 'removed' => $name, 'template' => (string) $fresh->prop( 'form' ), 'dangling_mail_tags' => $dangling, 'message' => array() === $dangling ? sprintf( /* translators: %s: field name */ __( 'Removed field "%s".', 'acrossai-abilities-manager' ), $name ) : sprintf( /* translators: 1: field name, 2: comma-separated tag names */ __( 'Removed field "%1$s". Mail templates still reference: %2$s — those tags now resolve to nothing.', 'acrossai-abilities-manager' ), $name, implode( ', ', $dangling ) ), ); } }