2019-09-28 03:41:19 +03:00
|
|
|
<?php
|
2019-10-01 03:04:49 +03:00
|
|
|
/**
|
2019-10-09 01:29:35 +03:00
|
|
|
* Render and process the pledge forms.
|
2019-10-01 03:04:49 +03:00
|
|
|
*/
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
namespace WordPressDotOrg\FiveForTheFuture\PledgeForm;
|
2019-10-01 03:04:49 +03:00
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
use WordPressDotOrg\FiveForTheFuture;
|
2019-10-01 03:04:49 +03:00
|
|
|
use WordPressDotOrg\FiveForTheFuture\Pledge;
|
|
|
|
use WordPressDotOrg\FiveForTheFuture\PledgeMeta;
|
2019-10-22 01:43:20 +03:00
|
|
|
use WordPressDotOrg\FiveForTheFuture\Contributor;
|
2019-10-19 02:56:21 +03:00
|
|
|
use WP_Error, WP_Post, WP_User;
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
defined( 'WPINC' ) || die();
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
// Todo make this into simple optionless blocks instead?
|
|
|
|
add_shortcode( '5ftf_pledge_form_new', __NAMESPACE__ . '\render_form_new' );
|
|
|
|
add_shortcode( '5ftf_pledge_form_manage', __NAMESPACE__ . '\render_form_manage' );
|
2019-10-01 03:04:49 +03:00
|
|
|
|
|
|
|
/**
|
2019-10-04 22:35:02 +03:00
|
|
|
* Render the form(s) for creating new pledges.
|
2019-10-01 03:04:49 +03:00
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
2019-10-04 22:35:02 +03:00
|
|
|
function render_form_new() {
|
2019-09-28 03:41:19 +03:00
|
|
|
$action = filter_input( INPUT_POST, 'action' );
|
2019-10-19 02:56:21 +03:00
|
|
|
$data = get_form_submission();
|
2019-09-28 03:41:19 +03:00
|
|
|
$messages = [];
|
|
|
|
$complete = false;
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
if ( 'Submit Pledge' === $action ) {
|
|
|
|
$processed = process_form_new();
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
if ( is_wp_error( $processed ) ) {
|
|
|
|
$messages = array_merge( $messages, $processed->get_error_messages() );
|
|
|
|
} elseif ( 'success' === $processed ) {
|
|
|
|
$complete = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
ob_start();
|
2019-10-09 01:29:35 +03:00
|
|
|
$readonly = false;
|
2019-10-04 22:35:02 +03:00
|
|
|
require FiveForTheFuture\PATH . 'views/form-pledge-new.php';
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
return ob_get_clean();
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-19 02:56:21 +03:00
|
|
|
* Process a submission from the New Pledge form.
|
2019-09-28 03:41:19 +03:00
|
|
|
*
|
|
|
|
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
|
|
|
|
*/
|
2019-10-04 22:35:02 +03:00
|
|
|
function process_form_new() {
|
2019-10-19 02:56:21 +03:00
|
|
|
$submission = get_form_submission();
|
2019-10-04 22:35:02 +03:00
|
|
|
|
|
|
|
$has_required = PledgeMeta\has_required_pledge_meta( $submission );
|
|
|
|
|
|
|
|
if ( is_wp_error( $has_required ) ) {
|
|
|
|
return $has_required;
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
$email = sanitize_meta(
|
|
|
|
PledgeMeta\META_PREFIX . 'org-pledge-email',
|
|
|
|
$submission['org-pledge-email'],
|
|
|
|
'post',
|
|
|
|
Pledge\CPT_ID
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( has_existing_pledge( $email, 'email' ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'existing_pledge_email',
|
|
|
|
__( 'This email address is already connected to an existing pledge.', 'wporg' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
$domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
if ( has_existing_pledge( $domain, 'domain' ) ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
return new WP_Error(
|
2019-10-19 02:56:21 +03:00
|
|
|
'existing_pledge_domain',
|
2019-10-04 22:35:02 +03:00
|
|
|
__( 'A pledge already exists for this domain.', 'wporg' )
|
|
|
|
);
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:43:20 +03:00
|
|
|
$contributors = parse_contributors( $submission['pledge-contributors'] );
|
2019-10-19 02:56:21 +03:00
|
|
|
|
|
|
|
if ( is_wp_error( $contributors ) ) {
|
|
|
|
return $contributors;
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
$name = sanitize_meta(
|
2019-10-04 22:35:02 +03:00
|
|
|
PledgeMeta\META_PREFIX . 'org-name',
|
|
|
|
$submission['org-name'],
|
2019-09-28 03:41:19 +03:00
|
|
|
'post',
|
2019-10-01 03:04:49 +03:00
|
|
|
Pledge\CPT_ID
|
2019-09-28 03:41:19 +03:00
|
|
|
);
|
|
|
|
|
2019-10-22 01:43:20 +03:00
|
|
|
$new_pledge_id = Pledge\create_new_pledge( $name );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-22 01:43:20 +03:00
|
|
|
if ( is_wp_error( $new_pledge_id ) ) {
|
|
|
|
return $new_pledge_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $contributors as $wporg_username ) {
|
|
|
|
Contributor\create_new_contributor( $wporg_username, $new_pledge_id );
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
/**
|
|
|
|
* Render the form(s) for managing existing pledges.
|
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
function render_form_manage() {
|
|
|
|
$action = filter_input( INPUT_POST, 'action' );
|
|
|
|
$messages = [];
|
|
|
|
$updated = false;
|
|
|
|
|
2019-10-09 01:29:35 +03:00
|
|
|
// @todo Get pledge ID from somewhere.
|
|
|
|
$data = PledgeMeta\get_pledge_meta();
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
if ( 'Update Pledge' === $action ) {
|
|
|
|
$processed = process_form_manage();
|
|
|
|
|
|
|
|
if ( is_wp_error( $processed ) ) {
|
|
|
|
$messages = array_merge( $messages, $processed->get_error_messages() );
|
|
|
|
} elseif ( 'success' === $processed ) {
|
|
|
|
$updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
2019-10-09 01:29:35 +03:00
|
|
|
$readonly = false;
|
2019-10-04 22:35:02 +03:00
|
|
|
require FiveForTheFuture\PATH . 'views/form-pledge-manage.php';
|
|
|
|
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-19 02:56:21 +03:00
|
|
|
* Process a submission from the Manage Existing Pledge form.
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
2019-10-21 21:10:55 +03:00
|
|
|
* TODO This doesn't actually update any data yet when the form is submitted.
|
|
|
|
*
|
2019-10-04 22:35:02 +03:00
|
|
|
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
|
|
|
|
*/
|
|
|
|
function process_form_manage() {
|
2019-10-19 02:56:21 +03:00
|
|
|
$submission = get_form_submission();
|
2019-10-04 22:35:02 +03:00
|
|
|
|
|
|
|
$has_required = PledgeMeta\has_required_pledge_meta( $submission );
|
|
|
|
|
|
|
|
if ( is_wp_error( $has_required ) ) {
|
|
|
|
return $has_required;
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
$email = sanitize_meta(
|
|
|
|
PledgeMeta\META_PREFIX . 'org-pledge-email',
|
|
|
|
$submission['org-pledge-email'],
|
|
|
|
'post',
|
|
|
|
Pledge\CPT_ID
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( has_existing_pledge( $email, 'email' ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'existing_pledge_email',
|
|
|
|
__( 'This email address is already connected to an existing pledge.', 'wporg' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
$domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] );
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
if ( has_existing_pledge( $domain, 'domain' ) ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
return new WP_Error(
|
|
|
|
'existing_pledge',
|
|
|
|
__( 'A pledge already exists for this domain.', 'wporg' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-19 02:56:21 +03:00
|
|
|
* Get and sanitize $_POST values from a form submission.
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
2019-10-19 02:56:21 +03:00
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
function get_form_submission() {
|
|
|
|
$input_filters = array_merge(
|
|
|
|
// Inputs that correspond to meta values.
|
|
|
|
wp_list_pluck( PledgeMeta\get_pledge_meta_config( 'user_input' ), 'php_filter' ),
|
|
|
|
// Inputs with no corresponding meta value.
|
|
|
|
array(
|
2019-10-22 01:43:20 +03:00
|
|
|
'pledge-contributors' => FILTER_SANITIZE_STRING,
|
2019-10-19 02:56:21 +03:00
|
|
|
'pledge-agreement' => FILTER_VALIDATE_BOOLEAN,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return filter_input_array( INPUT_POST, $input_filters );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a key value against existing pledges to see if one already exists.
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
2019-10-19 02:56:21 +03:00
|
|
|
* @param string $key The value to match against other pledges.
|
|
|
|
* @param string $key_type The type of value being matched. `email` or `domain`.
|
|
|
|
* @param int $current_pledge_id Optional. The post ID of the pledge to compare against others.
|
2019-10-04 22:35:02 +03:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-10-19 02:56:21 +03:00
|
|
|
function has_existing_pledge( $key, $key_type, int $current_pledge_id = 0 ) {
|
2019-10-04 22:35:02 +03:00
|
|
|
$args = array(
|
|
|
|
'post_type' => Pledge\CPT_ID,
|
2019-10-19 02:56:21 +03:00
|
|
|
'post_status' => array( 'draft', 'pending', 'publish' ),
|
2019-10-04 22:35:02 +03:00
|
|
|
);
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
switch ( $key_type ) {
|
|
|
|
case 'email':
|
|
|
|
$args['meta_query'] = array(
|
|
|
|
array(
|
|
|
|
'key' => PledgeMeta\META_PREFIX . 'org-pledge-email',
|
|
|
|
'value' => $key,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'domain':
|
|
|
|
$args['meta_query'] = array(
|
|
|
|
array(
|
|
|
|
'key' => PledgeMeta\META_PREFIX . 'org-domain',
|
|
|
|
'value' => $key,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:35:02 +03:00
|
|
|
if ( $current_pledge_id ) {
|
|
|
|
$args['exclude'] = array( $current_pledge_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
$matching_pledge = get_posts( $args );
|
|
|
|
|
|
|
|
return ! empty( $matching_pledge );
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:56:21 +03:00
|
|
|
/**
|
|
|
|
* Ensure each item in a list of usernames is valid and corresponds to a user.
|
|
|
|
*
|
|
|
|
* @param string $contributors A comma-separated list of username strings.
|
|
|
|
*
|
|
|
|
* @return array|WP_Error An array of sanitized wporg usernames on success. Otherwise WP_Error.
|
|
|
|
*/
|
|
|
|
function parse_contributors( $contributors ) {
|
|
|
|
$invalid_contributors = array();
|
|
|
|
$sanitized_contributors = array();
|
|
|
|
|
|
|
|
$contributors = explode( ',', $contributors );
|
|
|
|
|
|
|
|
foreach ( $contributors as $wporg_username ) {
|
|
|
|
$sanitized_username = sanitize_user( $wporg_username );
|
|
|
|
$user = get_user_by( 'login', $sanitized_username );
|
|
|
|
|
|
|
|
if ( $user instanceof WP_User ) {
|
|
|
|
$sanitized_contributors[] = $sanitized_username;
|
|
|
|
} else {
|
|
|
|
$invalid_contributors[] = $wporg_username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $invalid_contributors ) ) {
|
|
|
|
/* translators: Used between sponsor names in a list, there is a space after the comma. */
|
|
|
|
$item_separator = _x( ', ', 'list item separator', 'wporg' );
|
|
|
|
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_contributor',
|
|
|
|
sprintf(
|
|
|
|
/* translators: %s is a list of usernames. */
|
|
|
|
__( 'The following contributor usernames are not valid: %s', 'wporg' ),
|
|
|
|
implode( $item_separator, $invalid_contributors )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $sanitized_contributors ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'contributor_required',
|
|
|
|
__( 'The pledge must have at least one contributor username.', 'wporg' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sanitized_contributors = array_unique( $sanitized_contributors );
|
|
|
|
|
|
|
|
return $sanitized_contributors;
|
|
|
|
}
|