mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-07-06 02:45:43 +03:00
Manage Pledge: Enable Manage Form functionality
* Fix issue with fetching data in `get_pledge_meta` when no submission has been POST'd * Rename to `subset`, since `context` is also used elsewhere * Show form content when there are form errors * Fix warnings on new pledge form * Skip wpautop before save, so editors don’t need to edit HTML unless they want to
This commit is contained in:
parent
b898dc23f3
commit
60b5ca5d2a
4 changed files with 165 additions and 72 deletions
|
@ -28,34 +28,38 @@ add_action( 'added_post_meta', __NAMESPACE__ . '\update_generated_meta', 10, 4
|
|||
/**
|
||||
* Define pledge meta fields and their properties.
|
||||
*
|
||||
* @param string $context Optional. The part of the config to return. 'user_input', 'generated', or 'all'.
|
||||
* @param string $subset Optional. The part of the config to return: 'user_input', 'generated', or 'all'.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function get_pledge_meta_config( $context = 'all' ) {
|
||||
function get_pledge_meta_config( $subset = 'all' ) {
|
||||
$user_input = array(
|
||||
'org-description' => array(
|
||||
'single' => true,
|
||||
'sanitize_callback' => __NAMESPACE__ . '\sanitize_description',
|
||||
'show_in_rest' => true,
|
||||
'context' => array( 'create', 'update' ),
|
||||
'php_filter' => FILTER_UNSAFE_RAW,
|
||||
),
|
||||
'org-name' => array(
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
'show_in_rest' => true,
|
||||
'context' => array( 'create', 'update' ),
|
||||
'php_filter' => FILTER_SANITIZE_STRING,
|
||||
),
|
||||
'org-url' => array(
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'esc_url_raw',
|
||||
'show_in_rest' => true,
|
||||
'context' => array( 'create', 'update' ),
|
||||
'php_filter' => FILTER_VALIDATE_URL,
|
||||
),
|
||||
'org-pledge-email' => array(
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'sanitize_email',
|
||||
'show_in_rest' => false,
|
||||
'context' => array( 'create' ),
|
||||
'php_filter' => FILTER_VALIDATE_EMAIL,
|
||||
),
|
||||
);
|
||||
|
@ -83,7 +87,7 @@ function get_pledge_meta_config( $context = 'all' ) {
|
|||
),
|
||||
);
|
||||
|
||||
switch ( $context ) {
|
||||
switch ( $subset ) {
|
||||
case 'user_input':
|
||||
$return = $user_input;
|
||||
break;
|
||||
|
@ -107,7 +111,6 @@ function get_pledge_meta_config( $context = 'all' ) {
|
|||
*/
|
||||
function sanitize_description( $insecure ) {
|
||||
$secure = wp_kses_data( $insecure );
|
||||
$secure = wpautop( $secure );
|
||||
$secure = wp_unslash( wp_rel_nofollow( $secure ) );
|
||||
|
||||
return $secure;
|
||||
|
@ -243,12 +246,13 @@ function save_pledge( $pledge_id, $pledge ) {
|
|||
$get_action = filter_input( INPUT_GET, 'action' );
|
||||
$post_action = filter_input( INPUT_POST, 'action' );
|
||||
$ignored_actions = array( 'trash', 'untrash', 'restore' );
|
||||
$context = ( 'editpost' === $post_action ) ? 'update' : 'create';
|
||||
|
||||
/*
|
||||
* This is only intended to run when the front end form and wp-admin forms are submitted, not when posts are
|
||||
* programmatically updated.
|
||||
*/
|
||||
if ( 'Submit Pledge' !== $post_action && 'editpost' !== $post_action ) {
|
||||
if ( ! in_array( $post_action, [ 'Submit Pledge', 'editpost' ], true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -268,7 +272,7 @@ function save_pledge( $pledge_id, $pledge ) {
|
|||
|
||||
$submitted_meta = PledgeForm\get_form_submission();
|
||||
|
||||
if ( is_wp_error( has_required_pledge_meta( $submitted_meta ) ) ) {
|
||||
if ( is_wp_error( has_required_pledge_meta( $submitted_meta, $context ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -294,6 +298,10 @@ function save_pledge_meta( $pledge_id, $new_values ) {
|
|||
$config = get_pledge_meta_config();
|
||||
|
||||
foreach ( $new_values as $key => $value ) {
|
||||
// A null value can happen if the submission form did not have a given field.
|
||||
if ( is_null( $value ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( array_key_exists( $key, $config ) ) {
|
||||
$meta_key = META_PREFIX . $key;
|
||||
|
||||
|
@ -391,14 +399,22 @@ function update_single_cached_pledge_data( $pledge_id ) {
|
|||
/**
|
||||
* Check that an array contains values for all required keys.
|
||||
*
|
||||
* @return bool|WP_Error True if all required values are present. Otherwise WP_Error.
|
||||
* @param array $submission Form submission data.
|
||||
* @param string $context Whether this is a new pledge (`create`) or an edit to an existing one (`update`).
|
||||
*
|
||||
* @return true|WP_Error True if all required values are present. Otherwise WP_Error.
|
||||
*/
|
||||
function has_required_pledge_meta( array $submission ) {
|
||||
function has_required_pledge_meta( array $submission, $context ) {
|
||||
$error = new WP_Error();
|
||||
|
||||
$required = array_keys( get_pledge_meta_config( 'user_input' ) );
|
||||
$meta_config = get_pledge_meta_config( 'user_input' );
|
||||
$required = array_keys( $meta_config );
|
||||
|
||||
foreach ( $required as $key ) {
|
||||
if ( ! in_array( $context, $meta_config[ $key ]['context'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! isset( $submission[ $key ] ) || is_null( $submission[ $key ] ) ) {
|
||||
$error->add(
|
||||
'required_field_empty',
|
||||
|
@ -428,19 +444,23 @@ function has_required_pledge_meta( array $submission ) {
|
|||
/**
|
||||
* Get the metadata for a given pledge, or a default set if no pledge is provided.
|
||||
*
|
||||
* @param int $pledge_id
|
||||
* @param string $context
|
||||
* @param int $pledge_id Pledge to fetch data from.
|
||||
* @param string $subset Optional. The part of the config to return: 'user_input', 'generated', or 'all'.
|
||||
*
|
||||
* @return array Pledge data
|
||||
*/
|
||||
function get_pledge_meta( $pledge_id = 0, $context = '' ) {
|
||||
function get_pledge_meta( $pledge_id = 0, $subset = '' ) {
|
||||
// Get existing pledge, if it exists.
|
||||
$pledge = get_post( $pledge_id );
|
||||
|
||||
$keys = get_pledge_meta_config( $context );
|
||||
$keys = get_pledge_meta_config( $subset );
|
||||
$meta = array();
|
||||
|
||||
// Get POST'd submission, if it exists.
|
||||
$submission = PledgeForm\get_form_submission();
|
||||
if ( isset( $submission['empty_post'] ) && $submission['empty_post'] ) {
|
||||
$submission = array();
|
||||
}
|
||||
|
||||
foreach ( $keys as $key => $config ) {
|
||||
if ( isset( $submission[ $key ] ) ) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue