<commit_before><?php
/**
 * Custom Achievement Steps UI
 *
 * @package BadgeOS
 * @subpackage Achievements
 * @author Credly, LLC
 * @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
 * @link https://credly.com
 */

/**
 * Add our Steps JS to the Badge post editor
 *
 * @since 1.0.0
 */
function badgeos_steps_ui_admin_scripts() {
    global $post_type;
    if ( in_array( $post_type, badgeos_get_achievement_types_slugs() ) ) {
    	wp_enqueue_script( 'badgeos-steps-ui', $GLOBALS['badgeos']->directory_url . 'js/steps-ui.js', array( 'jquery-ui-sortable' ), '2.0.0' );
    }
}
add_action( 'admin_print_scripts-post-new.php', 'badgeos_steps_ui_admin_scripts', 11 );
add_action( 'admin_print_scripts-post.php', 'badgeos_steps_ui_admin_scripts', 11 );

/**
 * Adds our Steps metabox to the Badge post editor
 *
 * @since  1.0.0
 */
function badgeos_add_steps_ui_meta_box() {
	$achievement_types = badgeos_get_achievement_types_slugs();
	foreach ( $achievement_types as $achievement_type ) {
		add_meta_box( 'badgeos_steps_ui', apply_filters( 'badgeos_steps_ui_title', __( 'Required Steps', 'badgeos' ) ), 'badgeos_steps_ui_meta_box', $achievement_type, 'advanced', 'high' );
	}
}
add_action( 'admin_menu', 'badgeos_add_steps_ui_meta_box', 99 );

/**
 * Renders the HTML for meta box, refreshes whenever a new step is added
 *
 * @since  1.0.0
 * @param  object $post The current post object
 */
function badgeos_steps_ui_meta_box( $post ) {

	// Grab our $wpdb global
	global $wpdb;

	// Grab our Badge's required steps
	$required_steps = get_posts( array(
		'post_type'           => 'step',
		'posts_per_page'      => -1,
		'suppress_filters'    => false,
		'connected_direction' => 'to',
		'connected_type'      => 'step-to-' . $post->post_type,
		'connected_items'     => $post->ID,
	));

	// Loop through each step and set the sort order
	foreach ( $required_steps as $required_step ) {
		$required_step->order = get_step_menu_order( $required_step->ID );
	}

	// Sort the steps by their order
	uasort( $required_steps, 'badgeos_compare_step_order' );

	echo '<p>' .__( 'Define the required "steps" for this achievement to be considered complete. Use the "Label" field to optionally customize the titles of each step.', 'badgeos' ). '</p>';

	// Concatenate our step output
	echo '<ul id="steps_list">';
	foreach ( $required_steps as $step ) {
		badgeos_steps_ui_html( $step->ID, $post->ID );
	}
	echo '</ul>';

	// Render our buttons
	echo '<input style="margin-right: 1em" class="button" type="button" onclick="badgeos_add_new_step(' . $post->ID . ');" value="' . apply_filters( 'badgeos_steps_ui_add_new', __( 'Add New Step', 'badgeos' ) ) . '">';
	echo '<input class="button-primary" type="button" onclick="badgeos_update_steps();" value="' . apply_filters( 'badgeos_steps_ui_save_all', __( 'Save All Steps', 'badgeos' ) ) . '">';
	echo '<img class="save-steps-spinner" src="' . admin_url( '/images/wpspin_light.gif' ) . '" style="margin-left: 10px; display: none;" />';

}

/**
 * Helper function for generating the HTML output for configuring a given step
 *
 * @since  1.0.0
 * @param  integer $step_id The given step's ID
 * @param  integer $post_id The given step's parent $post ID
 * @return string           The concatenated HTML input for the step
 */
function badgeos_steps_ui_html( $step_id, $post_id ) {

	// Grab our step's requirements and measurement
	$requirements      = badgeos_get_step_requirements( $step_id );
	$count             = !empty( $requirements['count'] ) ? $requirements['count'] : 1;
	$achievement_types = badgeos_get_achievement_types_slugs();
?>

	<li class="step-row step-<?php echo $step_id; ?>" data-step-id="<?php echo $step_id; ?>">
		<div class="step-handle"></div>
		<a class="delete-step" href="javascript: badgeos_delete_step( <?php echo $step_id; ?> );"><?php _e( 'Delete', 'badgeos' ); ?></a>
		<input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
		<input type="hidden" name="order" value="<?php echo get_step_menu_order( $step_id ); ?>" />

		<?php echo apply_filters( 'badgeos_steps_ui_html_require_text', __( 'Require', 'badgeos' ), $step_id, $post_id ); ?>

		<?php do_action( 'badgeos_steps_ui_html_after_require_text', $step_id, $post_id ); ?>

		<select class="select-trigger-type" data-step-id="<?php echo $step_id; ?>">
			<?php
				foreach ( badgeos_get_activity_triggers() as $value => $label ) {
					echo '<option value="' . esc_attr( $value ) . '" ' . selected( $requirements['trigger_type'], $value, false ) . '>' . esc_html( $label ) . '</option>';
				}
			?>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_trigger_type', $step_id, $post_id ); ?>

		<select class="select-achievement-type select-achievement-type-<?php echo $step_id; ?>">
			<option value=""></option>
			<?php
				foreach ( $achievement_types as $achievement_type ) {
					echo '<option value="' . $achievement_type . '" ' . selected( $requirements['achievement_type'], $achievement_type, false ) . '>' . ucfirst( $achievement_type ) . '</option>';
				}
			?>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_achievement_type', $step_id, $post_id ); ?>

		<select class="select-achievement-post select-achievement-post-<?php echo $step_id; ?>">
			<option value=""></option>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_achievement_post', $step_id, $post_id ); ?>

		<input class="required-count" type="text" size="2" maxlength="2" value="<?php echo $count; ?>" placeholder="1">
		<?php echo apply_filters( 'badgeos_steps_ui_html_count_text', __( 'time(s).', 'badgeos' ), $step_id, $post_id ); ?>

		<?php do_action( 'badgeos_steps_ui_html_after_count_text', $step_id, $post_id ); ?>

		<div class="step-title"><label for="step-<?php echo $step_id; ?>-title">Label:</label> <input type="text" name="step-title" id="step-<?php echo $step_id; ?>-title" class="title" value="<?php echo get_the_title( $step_id ); ?>" /></div>
		<span class="spinner spinner-step-<?php echo $step_id;?>"></span>
	</li>
	<?php
}

/**
 * Get all the requirements of a given step
 *
 * @since  1.0.0
 * @param  integer $step_id The given step's post ID
 * @return array|bool       An array of all the step requirements if it has any, false if not
 */
function badgeos_get_step_requirements( $step_id ) {

	// Setup our default requirements array, assume we require nothing
	$requirements = array(
		'count'            => absint( get_post_meta( $step_id, '_badgeos_count', true ) ),
		'trigger_type'     => get_post_meta( $step_id, '_badgeos_trigger_type', true ),
		'achievement_type' => get_post_meta( $step_id, '_badgeos_achievement_type', true ),
		'achievement_post' => ''
	);

	// If the step requires a specific achievement
	if ( !empty( $requirements['achievement_type'] ) ) {
		$connected_activities = get_posts( array(
			'post_type'        => $requirements['achievement_type'],
			'posts_per_page'   => 1,
			'suppress_filters' => false,
			'connected_type'   => $requirements['achievement_type'] . '-to-step',
			'connected_to'     => $step_id
		));
		if ( ! empty( $connected_activities ) )
			$requirements['achievement_post'] = $connected_activities[0]->ID;
	}

	// Available filter for overriding elsewhere<commit_msg>Suppress erroneous errors in our get_posts() call<commit_after><?php
/**
 * Custom Achievement Steps UI
 *
 * @package BadgeOS
 * @subpackage Achievements
 * @author Credly, LLC
 * @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
 * @link https://credly.com
 */

/**
 * Add our Steps JS to the Badge post editor
 *
 * @since 1.0.0
 */
function badgeos_steps_ui_admin_scripts() {
    global $post_type;
    if ( in_array( $post_type, badgeos_get_achievement_types_slugs() ) ) {
    	wp_enqueue_script( 'badgeos-steps-ui', $GLOBALS['badgeos']->directory_url . 'js/steps-ui.js', array( 'jquery-ui-sortable' ), '2.0.0' );
    }
}
add_action( 'admin_print_scripts-post-new.php', 'badgeos_steps_ui_admin_scripts', 11 );
add_action( 'admin_print_scripts-post.php', 'badgeos_steps_ui_admin_scripts', 11 );

/**
 * Adds our Steps metabox to the Badge post editor
 *
 * @since  1.0.0
 */
function badgeos_add_steps_ui_meta_box() {
	$achievement_types = badgeos_get_achievement_types_slugs();
	foreach ( $achievement_types as $achievement_type ) {
		add_meta_box( 'badgeos_steps_ui', apply_filters( 'badgeos_steps_ui_title', __( 'Required Steps', 'badgeos' ) ), 'badgeos_steps_ui_meta_box', $achievement_type, 'advanced', 'high' );
	}
}
add_action( 'admin_menu', 'badgeos_add_steps_ui_meta_box', 99 );

/**
 * Renders the HTML for meta box, refreshes whenever a new step is added
 *
 * @since  1.0.0
 * @param  object $post The current post object
 */
function badgeos_steps_ui_meta_box( $post ) {

	// Grab our $wpdb global
	global $wpdb;

	// Grab our Badge's required steps
	$required_steps = get_posts( array(
		'post_type'           => 'step',
		'posts_per_page'      => -1,
		'suppress_filters'    => false,
		'connected_direction' => 'to',
		'connected_type'      => 'step-to-' . $post->post_type,
		'connected_items'     => $post->ID,
	));

	// Loop through each step and set the sort order
	foreach ( $required_steps as $required_step ) {
		$required_step->order = get_step_menu_order( $required_step->ID );
	}

	// Sort the steps by their order
	uasort( $required_steps, 'badgeos_compare_step_order' );

	echo '<p>' .__( 'Define the required "steps" for this achievement to be considered complete. Use the "Label" field to optionally customize the titles of each step.', 'badgeos' ). '</p>';

	// Concatenate our step output
	echo '<ul id="steps_list">';
	foreach ( $required_steps as $step ) {
		badgeos_steps_ui_html( $step->ID, $post->ID );
	}
	echo '</ul>';

	// Render our buttons
	echo '<input style="margin-right: 1em" class="button" type="button" onclick="badgeos_add_new_step(' . $post->ID . ');" value="' . apply_filters( 'badgeos_steps_ui_add_new', __( 'Add New Step', 'badgeos' ) ) . '">';
	echo '<input class="button-primary" type="button" onclick="badgeos_update_steps();" value="' . apply_filters( 'badgeos_steps_ui_save_all', __( 'Save All Steps', 'badgeos' ) ) . '">';
	echo '<img class="save-steps-spinner" src="' . admin_url( '/images/wpspin_light.gif' ) . '" style="margin-left: 10px; display: none;" />';

}

/**
 * Helper function for generating the HTML output for configuring a given step
 *
 * @since  1.0.0
 * @param  integer $step_id The given step's ID
 * @param  integer $post_id The given step's parent $post ID
 * @return string           The concatenated HTML input for the step
 */
function badgeos_steps_ui_html( $step_id, $post_id ) {

	// Grab our step's requirements and measurement
	$requirements      = badgeos_get_step_requirements( $step_id );
	$count             = !empty( $requirements['count'] ) ? $requirements['count'] : 1;
	$achievement_types = badgeos_get_achievement_types_slugs();
?>

	<li class="step-row step-<?php echo $step_id; ?>" data-step-id="<?php echo $step_id; ?>">
		<div class="step-handle"></div>
		<a class="delete-step" href="javascript: badgeos_delete_step( <?php echo $step_id; ?> );"><?php _e( 'Delete', 'badgeos' ); ?></a>
		<input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
		<input type="hidden" name="order" value="<?php echo get_step_menu_order( $step_id ); ?>" />

		<?php echo apply_filters( 'badgeos_steps_ui_html_require_text', __( 'Require', 'badgeos' ), $step_id, $post_id ); ?>

		<?php do_action( 'badgeos_steps_ui_html_after_require_text', $step_id, $post_id ); ?>

		<select class="select-trigger-type" data-step-id="<?php echo $step_id; ?>">
			<?php
				foreach ( badgeos_get_activity_triggers() as $value => $label ) {
					echo '<option value="' . esc_attr( $value ) . '" ' . selected( $requirements['trigger_type'], $value, false ) . '>' . esc_html( $label ) . '</option>';
				}
			?>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_trigger_type', $step_id, $post_id ); ?>

		<select class="select-achievement-type select-achievement-type-<?php echo $step_id; ?>">
			<option value=""></option>
			<?php
				foreach ( $achievement_types as $achievement_type ) {
					echo '<option value="' . $achievement_type . '" ' . selected( $requirements['achievement_type'], $achievement_type, false ) . '>' . ucfirst( $achievement_type ) . '</option>';
				}
			?>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_achievement_type', $step_id, $post_id ); ?>

		<select class="select-achievement-post select-achievement-post-<?php echo $step_id; ?>">
			<option value=""></option>
		</select>

		<?php do_action( 'badgeos_steps_ui_html_after_achievement_post', $step_id, $post_id ); ?>

		<input class="required-count" type="text" size="2" maxlength="2" value="<?php echo $count; ?>" placeholder="1">
		<?php echo apply_filters( 'badgeos_steps_ui_html_count_text', __( 'time(s).', 'badgeos' ), $step_id, $post_id ); ?>

		<?php do_action( 'badgeos_steps_ui_html_after_count_text', $step_id, $post_id ); ?>

		<div class="step-title"><label for="step-<?php echo $step_id; ?>-title">Label:</label> <input type="text" name="step-title" id="step-<?php echo $step_id; ?>-title" class="title" value="<?php echo get_the_title( $step_id ); ?>" /></div>
		<span class="spinner spinner-step-<?php echo $step_id;?>"></span>
	</li>
	<?php
}

/**
 * Get all the requirements of a given step
 *
 * @since  1.0.0
 * @param  integer $step_id The given step's post ID
 * @return array|bool       An array of all the step requirements if it has any, false if not
 */
function badgeos_get_step_requirements( $step_id ) {

	// Setup our default requirements array, assume we require nothing
	$requirements = array(
		'count'            => absint( get_post_meta( $step_id, '_badgeos_count', true ) ),
		'trigger_type'     => get_post_meta( $step_id, '_badgeos_trigger_type', true ),
		'achievement_type' => get_post_meta( $step_id, '_badgeos_achievement_type', true ),
		'achievement_post' => ''
	);

	// If the step requires a specific achievement
	if ( !empty( $requirements['achievement_type'] ) ) {
		$connected_activities = @get_posts( array(
			'post_type'        => $requirements['achievement_type'],
			'posts_per_page'   => 1,
			'suppress_filters' => false,
			'connected_type'   => $requirements['achievement_type'] . '-to-step',
			'connected_to'     => $step_id
		));
		if ( ! empty( $connected_activities ) )
			$requirements['achievement_post'] = $connected_activities[0]->ID;
	}

	// Available filter for overriding elsewhere
{'fraction_non_alphanumeric': 0.16412134614055698, 'fraction_numerical': 0.00497546817773478, 'mean_word_length': 4.492220113851992, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 175, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1837495', 'n_tokens_mistral': 5373, 'n_tokens_neox': 5091, 'n_words': 1149}