2019-09-28 03:41:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2019-10-01 03:04:49 +03:00
|
|
|
* This file handles the operations related to registering and handling pledge meta values for the CPT.
|
2019-09-28 03:41:19 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-01 03:04:49 +03:00
|
|
|
namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta;
|
|
|
|
|
|
|
|
use WordPressDotOrg\FiveForTheFuture;
|
2019-11-15 21:20:52 +02:00
|
|
|
use WordPressDotOrg\FiveForTheFuture\{ Contributor, Email, Pledge, PledgeForm, XProfile };
|
2019-09-28 03:41:19 +03:00
|
|
|
use WP_Post, WP_Error;
|
|
|
|
|
|
|
|
defined( 'WPINC' ) || die();
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
const META_PREFIX = FiveForTheFuture\PREFIX . '_';
|
2019-10-01 03:04:49 +03:00
|
|
|
|
2019-10-25 23:39:13 +03:00
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' );
|
2019-11-01 04:41:35 +02:00
|
|
|
add_action( 'init', __NAMESPACE__ . '\schedule_cron_jobs' );
|
2019-10-25 23:39:13 +03:00
|
|
|
add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' );
|
|
|
|
add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 );
|
|
|
|
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
|
2019-11-01 11:24:11 +02:00
|
|
|
add_action( 'transition_post_status', __NAMESPACE__ . '\maybe_update_single_cached_pledge_data', 10, 3 );
|
|
|
|
add_action( 'update_all_cached_pledge_data', __NAMESPACE__. '\update_all_cached_pledge_data' );
|
2019-10-19 02:56:21 +03:00
|
|
|
|
|
|
|
// Both hooks must be used because `updated` doesn't fire if the post meta didn't previously exist.
|
|
|
|
add_action( 'updated_postmeta', __NAMESPACE__ . '\update_generated_meta', 10, 4 );
|
|
|
|
add_action( 'added_post_meta', __NAMESPACE__ . '\update_generated_meta', 10, 4 );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
/**
|
2019-10-01 03:04:49 +03:00
|
|
|
* Define pledge meta fields and their properties.
|
2019-09-28 03:41:19 +03:00
|
|
|
*
|
2019-10-29 21:46:13 +02:00
|
|
|
* @param string $context Optional. The part of the config to return. 'user_input', 'generated', or 'all'.
|
|
|
|
*
|
2019-09-28 03:41:19 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-10-29 21:46:13 +02:00
|
|
|
function get_pledge_meta_config( $context = 'all' ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
$user_input = array(
|
2019-10-25 23:39:13 +03:00
|
|
|
'org-description' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
2019-10-30 22:09:17 +02:00
|
|
|
'sanitize_callback' => __NAMESPACE__ . '\sanitize_description',
|
2019-09-28 03:41:19 +03:00
|
|
|
'show_in_rest' => true,
|
2019-10-30 22:09:17 +02:00
|
|
|
'php_filter' => FILTER_UNSAFE_RAW,
|
2019-10-04 22:35:02 +03:00
|
|
|
),
|
2019-10-25 23:39:13 +03:00
|
|
|
'org-name' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
2019-09-28 03:41:19 +03:00
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
'show_in_rest' => true,
|
2019-10-08 18:21:50 +03:00
|
|
|
'php_filter' => FILTER_SANITIZE_STRING,
|
2019-10-04 22:35:02 +03:00
|
|
|
),
|
2019-10-25 23:39:13 +03:00
|
|
|
'org-url' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
2019-09-28 03:41:19 +03:00
|
|
|
'sanitize_callback' => 'esc_url_raw',
|
|
|
|
'show_in_rest' => true,
|
2019-10-04 22:35:02 +03:00
|
|
|
'php_filter' => FILTER_VALIDATE_URL,
|
|
|
|
),
|
2019-10-25 23:39:13 +03:00
|
|
|
'org-pledge-email' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => 'sanitize_email',
|
2019-09-28 03:41:19 +03:00
|
|
|
'show_in_rest' => false,
|
2019-10-08 18:21:50 +03:00
|
|
|
'php_filter' => FILTER_VALIDATE_EMAIL,
|
2019-10-04 22:35:02 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$generated = array(
|
2019-10-25 23:39:13 +03:00
|
|
|
'org-domain' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
2019-09-28 03:41:19 +03:00
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
'show_in_rest' => false,
|
2019-10-04 22:35:02 +03:00
|
|
|
),
|
2019-10-25 23:39:13 +03:00
|
|
|
'pledge-email-confirmed' => array(
|
2019-10-04 22:35:02 +03:00
|
|
|
'single' => true,
|
2019-09-28 03:41:19 +03:00
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
2019-10-04 22:35:02 +03:00
|
|
|
'show_in_rest' => false,
|
|
|
|
),
|
2019-10-25 23:39:13 +03:00
|
|
|
'pledge-confirmed-contributors' => array(
|
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'show_in_rest' => false,
|
|
|
|
),
|
2019-11-14 20:48:17 +02:00
|
|
|
'pledge-total-hours' => array(
|
2019-10-31 23:23:57 +02:00
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'show_in_rest' => false,
|
|
|
|
),
|
2019-10-04 22:35:02 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
switch ( $context ) {
|
|
|
|
case 'user_input':
|
|
|
|
$return = $user_input;
|
|
|
|
break;
|
|
|
|
case 'generated':
|
|
|
|
$return = $generated;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$return = array_merge( $user_input, $generated );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-30 22:09:17 +02:00
|
|
|
/**
|
|
|
|
* Sanitize description fields.
|
|
|
|
*
|
|
|
|
* @param string $insecure
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function sanitize_description( $insecure ) {
|
|
|
|
$secure = wp_kses_data( $insecure );
|
|
|
|
$secure = wpautop( $secure );
|
|
|
|
$secure = wp_unslash( wp_rel_nofollow( $secure ) );
|
|
|
|
|
|
|
|
return $secure;
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
/**
|
2019-10-01 03:04:49 +03:00
|
|
|
* Register post meta keys for the custom post type.
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2019-09-28 03:41:19 +03:00
|
|
|
*/
|
2019-10-01 03:04:49 +03:00
|
|
|
function register_pledge_meta() {
|
|
|
|
$meta = get_pledge_meta_config();
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
foreach ( $meta as $key => $args ) {
|
|
|
|
$meta_key = META_PREFIX . $key;
|
|
|
|
|
2019-10-01 03:04:49 +03:00
|
|
|
register_post_meta( Pledge\CPT_ID, $meta_key, $args );
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 04:41:35 +02:00
|
|
|
/**
|
|
|
|
* Schedule cron jobs.
|
|
|
|
*
|
|
|
|
* This needs to run on the `init` action, because Cavalcade isn't fully loaded before that, and events
|
|
|
|
* wouldn't be scheduled.
|
|
|
|
*
|
|
|
|
* @see https://dotorg.trac.wordpress.org/changeset/15351/
|
|
|
|
*/
|
|
|
|
function schedule_cron_jobs() {
|
2019-11-01 11:24:11 +02:00
|
|
|
if ( ! wp_next_scheduled( 'update_all_cached_pledge_data' ) ) {
|
|
|
|
wp_schedule_event( time(), 'hourly', 'update_all_cached_pledge_data' );
|
2019-11-01 04:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regularly update the cached data for all pledges.
|
|
|
|
*
|
|
|
|
* Outside of this cron job, it's only updated when a contributor post changes status, but it's possible for
|
|
|
|
* a contributor to edit their profile's # of hours at any time. If we didn't regularly check and update it,
|
|
|
|
* then it could be permanently out of date.
|
|
|
|
*/
|
2019-11-01 11:24:11 +02:00
|
|
|
function update_all_cached_pledge_data() {
|
2019-11-01 04:41:35 +02:00
|
|
|
$pledges = get_posts( array(
|
|
|
|
'post_type' => Pledge\CPT_ID,
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'numberposts' => 1000,
|
|
|
|
) );
|
|
|
|
|
|
|
|
foreach ( $pledges as $pledge ) {
|
2019-11-01 11:24:11 +02:00
|
|
|
update_single_cached_pledge_data( $pledge->ID );
|
2019-11-01 04:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
/**
|
2019-10-04 22:35:02 +03:00
|
|
|
* Adds meta boxes for the custom post type.
|
|
|
|
*
|
|
|
|
* @return void
|
2019-09-28 03:41:19 +03:00
|
|
|
*/
|
|
|
|
function add_meta_boxes() {
|
|
|
|
add_meta_box(
|
2019-10-04 22:35:02 +03:00
|
|
|
'pledge-email',
|
|
|
|
__( 'Pledge Email', 'wordpressorg' ),
|
|
|
|
__NAMESPACE__ . '\render_meta_boxes',
|
|
|
|
Pledge\CPT_ID,
|
|
|
|
'normal',
|
|
|
|
'high'
|
|
|
|
);
|
|
|
|
|
|
|
|
add_meta_box(
|
|
|
|
'org-info',
|
|
|
|
__( 'Organization Information', 'wordpressorg' ),
|
|
|
|
__NAMESPACE__ . '\render_meta_boxes',
|
|
|
|
Pledge\CPT_ID,
|
|
|
|
'normal',
|
|
|
|
'high'
|
|
|
|
);
|
|
|
|
|
|
|
|
add_meta_box(
|
|
|
|
'pledge-contributors',
|
|
|
|
__( 'Contributors', 'wordpressorg' ),
|
2019-10-01 03:04:49 +03:00
|
|
|
__NAMESPACE__ . '\render_meta_boxes',
|
|
|
|
Pledge\CPT_ID,
|
2019-09-28 03:41:19 +03:00
|
|
|
'normal',
|
2019-10-04 22:35:02 +03:00
|
|
|
'high'
|
2019-09-28 03:41:19 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the markup for all meta boxes
|
|
|
|
*
|
2019-10-01 03:04:49 +03:00
|
|
|
* @param WP_Post $pledge
|
2019-09-28 03:41:19 +03:00
|
|
|
* @param array $box
|
|
|
|
*/
|
2019-10-01 03:04:49 +03:00
|
|
|
function render_meta_boxes( $pledge, $box ) {
|
2019-10-09 01:29:35 +03:00
|
|
|
$readonly = ! current_user_can( 'edit_page', $pledge->ID );
|
2019-10-04 22:35:02 +03:00
|
|
|
|
2019-10-22 01:43:20 +03:00
|
|
|
$data = array();
|
2019-10-04 22:35:02 +03:00
|
|
|
foreach ( get_pledge_meta_config() as $key => $config ) {
|
|
|
|
$data[ $key ] = get_post_meta( $pledge->ID, META_PREFIX . $key, $config['single'] );
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:43:20 +03:00
|
|
|
$contributors = Contributor\get_pledge_contributors( $pledge->ID, 'all' );
|
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
echo '<div class="pledge-form">';
|
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
switch ( $box['id'] ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
case 'pledge-email':
|
|
|
|
require FiveForTheFuture\get_views_path() . 'inputs-pledge-org-email.php';
|
|
|
|
break;
|
2019-10-09 01:29:35 +03:00
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
case 'org-info':
|
|
|
|
require FiveForTheFuture\get_views_path() . 'inputs-pledge-org-info.php';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'pledge-contributors':
|
2019-10-25 06:06:47 +03:00
|
|
|
require FiveForTheFuture\get_views_path() . 'manage-contributors.php';
|
2019-09-28 03:41:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
echo '</div>';
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-04 22:35:02 +03:00
|
|
|
* Save the pledge data.
|
|
|
|
*
|
|
|
|
* This only fires when the pledge post itself is created or updated.
|
2019-09-28 03:41:19 +03:00
|
|
|
*
|
2019-10-01 03:04:49 +03:00
|
|
|
* @param int $pledge_id
|
|
|
|
* @param WP_Post $pledge
|
2019-09-28 03:41:19 +03:00
|
|
|
*/
|
2019-10-01 03:04:49 +03:00
|
|
|
function save_pledge( $pledge_id, $pledge ) {
|
2019-10-25 22:07:09 +03:00
|
|
|
$get_action = filter_input( INPUT_GET, 'action' );
|
|
|
|
$post_action = filter_input( INPUT_POST, 'action' );
|
2019-09-28 03:41:19 +03:00
|
|
|
$ignored_actions = array( 'trash', 'untrash', 'restore' );
|
|
|
|
|
2019-10-25 22:07:09 +03:00
|
|
|
/*
|
|
|
|
* This is only intended to run when the front end form and wp-admin forms are submitted, not when posts are
|
|
|
|
* programmatically updated.
|
|
|
|
*/
|
2019-10-26 19:07:23 +03:00
|
|
|
if ( 'Submit Pledge' !== $post_action && 'editpost' !== $post_action ) {
|
2019-10-25 22:07:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $get_action && in_array( $get_action, $ignored_actions, true ) ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
if ( ! $pledge instanceof WP_Post || Pledge\CPT_ID !== $pledge->post_type ) {
|
2019-09-28 03:41:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 20:48:17 +02:00
|
|
|
// if ( ! current_user_can( 'edit_pledge', $pledge_id ) ) {} -- todo re-enable once setup cap mapping or whatever.
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'auto-draft' === $pledge->post_status ) {
|
2019-09-28 03:41:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
$submitted_meta = PledgeForm\get_form_submission();
|
2019-10-04 22:35:02 +03:00
|
|
|
|
|
|
|
if ( is_wp_error( has_required_pledge_meta( $submitted_meta ) ) ) {
|
2019-09-28 03:41:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
save_pledge_meta( $pledge_id, $submitted_meta );
|
2019-10-26 20:59:06 +03:00
|
|
|
|
2019-10-27 07:18:28 +02:00
|
|
|
if ( filter_input( INPUT_POST, 'resend-pledge-confirmation' ) ) {
|
2019-11-15 21:20:52 +02:00
|
|
|
Email\send_pledge_confirmation_email(
|
2019-10-27 07:18:28 +02:00
|
|
|
filter_input( INPUT_GET, 'resend-pledge-id', FILTER_VALIDATE_INT ),
|
|
|
|
get_page_by_path( 'for-organizations' )->ID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-26 20:59:06 +03:00
|
|
|
if ( filter_input( INPUT_POST, 'resend-contributor-confirmation' ) ) {
|
2019-11-15 21:20:52 +02:00
|
|
|
Email\send_contributor_confirmation_emails(
|
2019-10-26 20:59:06 +03:00
|
|
|
$pledge_id,
|
|
|
|
filter_input( INPUT_GET, 'resend-contributor-id', FILTER_VALIDATE_INT )
|
|
|
|
);
|
|
|
|
}
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-04 22:35:02 +03:00
|
|
|
* Save the pledge's meta fields.
|
2019-09-28 03:41:19 +03:00
|
|
|
*
|
2019-10-01 03:04:49 +03:00
|
|
|
* @param int $pledge_id
|
2019-09-28 03:41:19 +03:00
|
|
|
* @param array $new_values
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2019-09-28 03:41:19 +03:00
|
|
|
*/
|
2019-10-01 03:04:49 +03:00
|
|
|
function save_pledge_meta( $pledge_id, $new_values ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
$config = get_pledge_meta_config();
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
foreach ( $new_values as $key => $value ) {
|
|
|
|
if ( array_key_exists( $key, $config ) ) {
|
|
|
|
$meta_key = META_PREFIX . $key;
|
2019-10-24 17:55:45 +03:00
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
// Since the sanitize callback is called during this function, it could still end up
|
|
|
|
// saving an empty value to the database.
|
2019-10-04 22:35:02 +03:00
|
|
|
update_post_meta( $pledge_id, $meta_key, $value );
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 22:35:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updated some generated meta values based on changes in user input meta values.
|
|
|
|
*
|
|
|
|
* This is hooked to the `updated_{$meta_type}_meta` action, which only fires if a submitted post meta value
|
|
|
|
* is different from the previous value. Thus here we assume the values of specific meta keys are changed
|
|
|
|
* when they come through this function.
|
|
|
|
*
|
|
|
|
* @param int $meta_id
|
|
|
|
* @param int $object_id
|
|
|
|
* @param string $meta_key
|
|
|
|
* @param mixed $_meta_value
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
|
2019-10-19 02:56:21 +03:00
|
|
|
$post_type = get_post_type( $object_id );
|
|
|
|
|
|
|
|
if ( Pledge\CPT_ID !== $post_type ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
switch ( $meta_key ) {
|
2019-10-19 02:56:21 +03:00
|
|
|
case META_PREFIX . 'org-name':
|
|
|
|
if ( 'updated_postmeta' === current_action() ) {
|
|
|
|
wp_update_post( array(
|
2019-10-29 21:46:13 +02:00
|
|
|
'ID' => $object_id,
|
2019-10-19 02:56:21 +03:00
|
|
|
'post_title' => $_meta_value,
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
case META_PREFIX . 'org-url':
|
|
|
|
$domain = get_normalized_domain_from_url( $_meta_value );
|
|
|
|
update_post_meta( $object_id, META_PREFIX . 'org-domain', $domain );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case META_PREFIX . 'pledge-email':
|
|
|
|
delete_post_meta( $object_id, META_PREFIX . 'pledge-email-confirmed' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-25 23:39:13 +03:00
|
|
|
/**
|
2019-11-01 04:41:35 +02:00
|
|
|
* Update the cached pledge data when a contributor post changes statuses.
|
2019-10-25 23:39:13 +03:00
|
|
|
*
|
|
|
|
* Note that contributor posts should always be trashed instead of deleted completely when a contributor is
|
|
|
|
* removed from a pledge.
|
|
|
|
*
|
|
|
|
* @param string $new_status
|
|
|
|
* @param string $old_status
|
|
|
|
* @param WP_Post $post
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-11-01 11:24:11 +02:00
|
|
|
function maybe_update_single_cached_pledge_data( $new_status, $old_status, WP_Post $post ) {
|
2019-10-25 23:39:13 +03:00
|
|
|
if ( Contributor\CPT_ID !== get_post_type( $post ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $new_status === $old_status ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pledge = get_post( $post->post_parent );
|
|
|
|
|
|
|
|
if ( $pledge instanceof WP_Post ) {
|
2019-11-01 11:24:11 +02:00
|
|
|
update_single_cached_pledge_data( $pledge->ID );
|
2019-11-01 04:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 23:23:57 +02:00
|
|
|
|
2019-11-01 04:41:35 +02:00
|
|
|
/**
|
|
|
|
* Update the cached data for the given pledge.
|
|
|
|
*
|
|
|
|
* This is saved so that it can be easily queried against, and also to make stats calculations easier.
|
|
|
|
*
|
2019-11-14 20:48:17 +02:00
|
|
|
* @param int $pledge_id
|
2019-11-01 04:41:35 +02:00
|
|
|
*/
|
2019-11-01 11:24:11 +02:00
|
|
|
function update_single_cached_pledge_data( $pledge_id ) {
|
2019-11-01 04:41:35 +02:00
|
|
|
$pledge_data = XProfile\get_aggregate_contributor_data_for_pledge( $pledge_id );
|
2019-10-25 23:39:13 +03:00
|
|
|
|
2019-11-01 04:41:35 +02:00
|
|
|
update_post_meta( $pledge_id, META_PREFIX . 'pledge-confirmed-contributors', $pledge_data['contributors'] );
|
|
|
|
update_post_meta( $pledge_id, META_PREFIX . 'pledge-total-hours', $pledge_data['hours'] );
|
2019-10-25 23:39:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
/**
|
|
|
|
* Check that an array contains values for all required keys.
|
|
|
|
*
|
|
|
|
* @return bool|WP_Error True if all required values are present. Otherwise WP_Error.
|
|
|
|
*/
|
|
|
|
function has_required_pledge_meta( array $submission ) {
|
|
|
|
$error = new WP_Error();
|
|
|
|
|
|
|
|
$required = array_keys( get_pledge_meta_config( 'user_input' ) );
|
|
|
|
|
|
|
|
foreach ( $required as $key ) {
|
|
|
|
if ( ! isset( $submission[ $key ] ) || is_null( $submission[ $key ] ) ) {
|
|
|
|
$error->add(
|
|
|
|
'required_field_empty',
|
|
|
|
sprintf(
|
|
|
|
__( 'The <code>%s</code> field does not have a value.', 'wporg' ),
|
|
|
|
sanitize_key( $key )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} elseif ( false === $submission[ $key ] ) {
|
|
|
|
$error->add(
|
|
|
|
'required_field_invalid',
|
|
|
|
sprintf(
|
|
|
|
__( 'The <code>%s</code> field has an invalid value.', 'wporg' ),
|
|
|
|
sanitize_key( $key )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $error->get_error_messages() ) ) {
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = '' ) {
|
|
|
|
// Get existing pledge, if it exists.
|
|
|
|
$pledge = get_post( $pledge_id );
|
|
|
|
|
|
|
|
$keys = get_pledge_meta_config( $context );
|
|
|
|
$meta = array();
|
|
|
|
|
|
|
|
// Get POST'd submission, if it exists.
|
2019-10-19 02:56:21 +03:00
|
|
|
$submission = PledgeForm\get_form_submission();
|
2019-10-09 01:29:35 +03:00
|
|
|
|
|
|
|
foreach ( $keys as $key => $config ) {
|
|
|
|
if ( isset( $submission[ $key ] ) ) {
|
|
|
|
$meta[ $key ] = $submission[ $key ];
|
2019-10-19 02:56:21 +03:00
|
|
|
} elseif ( $pledge instanceof WP_Post ) {
|
|
|
|
$meta_key = META_PREFIX . $key;
|
2019-10-09 01:29:35 +03:00
|
|
|
$meta[ $key ] = get_post_meta( $pledge->ID, $meta_key, true );
|
|
|
|
} else {
|
|
|
|
$meta[ $key ] = $config['default'] ?: '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
/**
|
|
|
|
* Isolate the domain from a given URL and remove the `www.` if necessary.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_normalized_domain_from_url( $url ) {
|
|
|
|
$domain = wp_parse_url( $url, PHP_URL_HOST );
|
|
|
|
$domain = preg_replace( '#^www\.#', '', $domain );
|
|
|
|
|
|
|
|
return $domain;
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
2019-10-09 01:29:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue CSS file for admin page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function enqueue_assets() {
|
|
|
|
$ver = filemtime( FiveForTheFuture\PATH . '/assets/css/admin.css' );
|
|
|
|
wp_register_style( '5ftf-admin', plugins_url( 'assets/css/admin.css', __DIR__ ), [], $ver );
|
|
|
|
|
|
|
|
$current_page = get_current_screen();
|
|
|
|
if ( Pledge\CPT_ID === $current_page->id ) {
|
|
|
|
wp_enqueue_style( '5ftf-admin' );
|
|
|
|
}
|
|
|
|
}
|