2019-09-28 03:41:19 +03:00
|
|
|
<?php
|
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-09-28 03:41:19 +03:00
|
|
|
use WP_Error;
|
|
|
|
|
|
|
|
defined( 'WPINC' ) || die();
|
|
|
|
|
2019-10-01 03:04:49 +03:00
|
|
|
add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_shortcode' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
2019-10-02 00:17:02 +03:00
|
|
|
function render_shortcode() {
|
2019-09-28 03:41:19 +03:00
|
|
|
$action = filter_input( INPUT_POST, 'action' );
|
|
|
|
$messages = [];
|
|
|
|
$complete = false;
|
|
|
|
|
2019-10-02 00:17:02 +03:00
|
|
|
if ( 'Submit Pledge' === $action ) {
|
|
|
|
$processed = process_form();
|
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-02 00:17:02 +03:00
|
|
|
ob_start();
|
|
|
|
require FiveForTheFuture\PATH . 'views/pledge-form.php';
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-02 00:17:02 +03:00
|
|
|
return ob_get_clean();
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2019-10-02 00:17:02 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_input_filters() {
|
|
|
|
return array_merge(
|
|
|
|
// Inputs that correspond to meta values.
|
|
|
|
wp_list_pluck( PledgeMeta\get_pledge_meta_config(), 'php_filter' ),
|
|
|
|
// Inputs with no corresponding meta value.
|
|
|
|
array(
|
|
|
|
'contributor-wporg-usernames' => [
|
|
|
|
'filter' => FILTER_SANITIZE_STRING,
|
|
|
|
'flags' => FILTER_REQUIRE_ARRAY,
|
|
|
|
],
|
|
|
|
'pledge-agreement' => FILTER_VALIDATE_BOOLEAN,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2019-09-28 03:41:19 +03:00
|
|
|
*
|
|
|
|
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
|
|
|
|
*/
|
2019-10-02 00:17:02 +03:00
|
|
|
function process_form() {
|
|
|
|
$submission = filter_input_array( INPUT_POST, get_input_filters() );
|
|
|
|
|
|
|
|
$submission['org-domain'] = get_normalized_domain_from_url( $submission['org-url'] );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
2019-10-02 00:17:02 +03:00
|
|
|
if ( in_array( null, $submission, true ) || in_array( false, $submission, true ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_submission',
|
|
|
|
__( 'Some fields have missing or invalid information.', 'wporg' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_existing_pledge = has_existing_pledge( $submission['org-domain'] );
|
|
|
|
|
|
|
|
if ( $has_existing_pledge ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'existing_pledge',
|
|
|
|
__( 'A pledge already exists for this domain.', 'wporg' )
|
|
|
|
);
|
2019-09-28 03:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$name = sanitize_meta(
|
2019-10-02 00:17: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-01 03:04:49 +03:00
|
|
|
$created = create_new_pledge( $name );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
if ( is_wp_error( $created ) ) {
|
|
|
|
return $created;
|
|
|
|
}
|
|
|
|
|
2019-10-02 00:17:02 +03:00
|
|
|
PledgeMeta\save_pledge_meta( $created, $submission );
|
2019-09-28 03:41:19 +03:00
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
2019-10-02 00:17:02 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $domain
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function has_existing_pledge( $domain ) {
|
|
|
|
$matching_pledge = get_posts( array(
|
|
|
|
'post_type' => Pledge\CPT_ID,
|
|
|
|
'post_status' => array( 'pending', 'publish' ),
|
|
|
|
'meta_query' => array(
|
|
|
|
'key' => PledgeMeta\META_PREFIX . 'org-domain',
|
|
|
|
'value' => $domain,
|
|
|
|
'compare' => 'LIKE',
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
|
|
|
|
return ! empty( $matching_pledge );
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:41:19 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $name The name of the company to use as the post title.
|
|
|
|
*
|
|
|
|
* @return int|WP_Error Post ID on success. Otherwise WP_Error.
|
|
|
|
*/
|
2019-10-01 03:04:49 +03:00
|
|
|
function create_new_pledge( $name ) {
|
2019-09-28 03:41:19 +03:00
|
|
|
$args = [
|
2019-10-01 03:04:49 +03:00
|
|
|
'post_type' => Pledge\CPT_ID,
|
2019-09-28 03:41:19 +03:00
|
|
|
'post_title' => $name,
|
|
|
|
'post_status' => 'pending',
|
|
|
|
'post_author' => get_current_user_id(), // TODO is this how we want to do this?
|
|
|
|
];
|
|
|
|
|
|
|
|
return wp_insert_post( $args, true );
|
2019-10-01 03:04:49 +03:00
|
|
|
}
|