Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/badges #1438

Merged
merged 12 commits into from
Dec 12, 2024
2 changes: 2 additions & 0 deletions public_html/wp-content/plugins/camptix/camptix.php
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ function __construct() {

require( dirname( __FILE__ ) . '/inc/class-camptix-addon.php' );
require( dirname( __FILE__ ) . '/inc/class-camptix-payment-method.php' );
require( dirname( __FILE__ ) . '/inc/class-camptix-badges.php' );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( dirname( __FILE__ ) . '/inc/class-wp-cli-commands.php' );
@@ -2032,6 +2033,7 @@ function format_name_string( $name_string, $given_name, $surname ) {
function admin_menu() {
add_submenu_page( 'edit.php?post_type=tix_ticket', __( 'Tools', 'wordcamporg' ), __( 'Tools', 'wordcamporg' ), $this->caps['manage_tools'], 'camptix_tools', array( $this, 'menu_tools' ) );
add_submenu_page( 'edit.php?post_type=tix_ticket', __( 'Setup', 'wordcamporg' ), __( 'Setup', 'wordcamporg' ), $this->caps['manage_options'], 'camptix_options', array( $this, 'menu_setup' ) );
add_submenu_page( 'edit.php?post_type=tix_ticket', __( 'Profile Badges', 'wordcamporg' ), __( 'Profile Badges', 'wordcamporg' ), $this->caps['manage_options'], 'camptix_badges', 'Camptix\Profile_Badges\menu_badges' );
remove_submenu_page( 'edit.php?post_type=tix_ticket', 'post-new.php?post_type=tix_ticket' );
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Camptix\Profile_Badges;

use WordPressdotorg\Profiles;

/**
* Process submission and returns appropriate message
*
* @return string
*/
function process_badges() {

if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'badge-submission' ) ) {
return __( 'Invalid request', 'wordcamporg' );
}

$usernames = sanitize_text_field( $_POST['usernames'] );
$operation = sanitize_text_field( $_POST['operation'] );
$badge = sanitize_text_field( $_POST['badge_name'] );

$valid_operations = [ 'add', 'remove' ];
$valid_badges = [ 'wordcamp-volunteer' ];

if ( ! in_array( $operation, $valid_operations ) ) {
return sprintf( __( 'Invalid badge operation used, valid commands are: %s', 'wordcamporg' ), implode( ',', $valid_operations ) );
}

if ( ! in_array( $badge, $valid_badges ) ) {
return __( 'Invalid badge', 'wordcamporg' );
}

if ( empty( $usernames ) ) {
return __( 'You must supply a list of usernames', 'wordcamporg' );
}

$users = explode( "\n", $usernames );

Profiles\badge_api( $operation, $badge, $users );

// Badge_api doesn't return anything apart from a success message, so lets guess how many items were updated.
$count = count( $users );

return sprintf( _n( '%s badge updated', '%s badges updated', $count, 'wordcamporg' ), number_format_i18n( $count ) );
}

/**
* Outputs manage badge admin screen, and allows processing of submit.
*/
function menu_badges() {
if ( isset( $_GET['badge-submit'] ) && ( 1 == $_GET['badge-submit'] ) ) {
$output = process_badges();
wp_admin_notice( $output );
}

// If adding more badges, make sure to add them to the validation check in `process_badges`.
?>
<div class="wrap">
<h1><?php esc_html_e( 'Profile Badge Management', 'wordcamporg' ); ?></h1>
<p><?php esc_html_e( 'This tool allows a limited number of badges to be managed on wordpress.org profiles', 'wordcamporg' ); ?></p>
</div>
<form method="post" action="<?php echo esc_url( add_query_arg( 'badge-submit', '1' ) ); ?>">
<div>
<select name="badge_name">
<option value="wordcamp-volunteer"><?php esc_html_e( 'WordCamp Volunteer', 'wordcamporg' ); ?></option>
</select>
<select name="operation">
<option value="add"><?php esc_html_e( 'Add', 'wordcamporg' ); ?></option>
<option value="remove"><?php esc_html_e( 'Remove', 'wordcamporg' ); ?></option>
</select>
</div>
<div class="wrap">
<textarea name="usernames" cols="50" rows="20" placeholder="<?php esc_attr_e( 'Input usernames, 1 per row', 'wordcamporg' ); ?>"></textarea>
</div>
<input type="hidden" name="action" value="badge_submission" />
<?php wp_nonce_field( 'badge-submission' ); ?>
<div><?php submit_button( __( 'Submit', 'wordcamporg' ) ); ?></div>
</form>
<?php
}