Add a helper function to fetch pledge data

This commit is contained in:
Kelly Dwan 2019-10-08 12:54:00 -04:00
parent 0111c36db4
commit 61ef972391
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
2 changed files with 35 additions and 6 deletions

View file

@ -1,6 +1,6 @@
<?php <?php
/** /**
* * Render and process the pledge forms.
*/ */
namespace WordPressDotOrg\FiveForTheFuture\PledgeForm; namespace WordPressDotOrg\FiveForTheFuture\PledgeForm;
@ -25,6 +25,7 @@ function render_form_new() {
$action = filter_input( INPUT_POST, 'action' ); $action = filter_input( INPUT_POST, 'action' );
$messages = []; $messages = [];
$complete = false; $complete = false;
$data = PledgeMeta\get_pledge_meta();
if ( 'Submit Pledge' === $action ) { if ( 'Submit Pledge' === $action ) {
$processed = process_form_new(); $processed = process_form_new();
@ -93,6 +94,9 @@ function render_form_manage() {
$messages = []; $messages = [];
$updated = false; $updated = false;
// @todo Get pledge ID from somewhere.
$data = PledgeMeta\get_pledge_meta();
if ( 'Update Pledge' === $action ) { if ( 'Update Pledge' === $action ) {
$processed = process_form_manage(); $processed = process_form_manage();

View file

@ -209,7 +209,7 @@ function save_pledge( $pledge_id, $pledge ) {
return; return;
} }
if ( ! $pledge instanceof WP_Post || $pledge->post_type !== Pledge\CPT_ID ) { if ( ! $pledge instanceof WP_Post || Pledge\CPT_ID !== $pledge->post_type ) {
return; return;
} }
@ -217,7 +217,7 @@ function save_pledge( $pledge_id, $pledge ) {
return; return;
} }
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $pledge->post_status === 'auto-draft' ) { if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'auto-draft' === $pledge->post_status ) {
return; return;
} }
@ -279,6 +279,31 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value )
} }
} }
/**
* Get the metadata for a given pledge, or a default set if no pledge is provided.
*
* @param int $pledge_id
* @param string $context
* @return array Pledge data
*/
function get_pledge_meta( $pledge_id = 0, $context = '' ) {
$pledge = get_post( $pledge_id );
$keys = get_pledge_meta_config( $context );
$meta = array();
foreach ( $keys as $key => $config ) {
if ( ! $pledge instanceof WP_Post ) {
$meta[ $key ] = $config['default'] ?: '';
} else {
$meta_key = META_PREFIX . $key;
$meta[ $key ] = get_post_meta( $pledge->ID, $meta_key, true );
}
}
return $meta;
}
/** /**
* Isolate the domain from a given URL and remove the `www.` if necessary. * Isolate the domain from a given URL and remove the `www.` if necessary.
* *