/** * Load up our activity triggers so we can add actions to them * * @since 1.0.0 * @return void */ function badgeos_load_activity_triggers() { // Grab our activity triggers $activity_triggers = badgeos_get_activity_triggers(); // Loop through each achievement type and add triggers for unlocking them foreach (badgeos_get_achievement_types_slugs() as $achievement_type) { // Grab the post type object, and bail if it's not actually an object $post_type_object = get_post_type_object($achievement_type); if (!is_object($post_type_object)) { continue; } // Add trigger for unlocking ANY and ALL posts for each achievement type $activity_triggers['badgeos_unlock_' . $achievement_type] = sprintf(__('Unlocked a %s', 'badgeos'), $post_type_object->labels->singular_name); $activity_triggers['badgeos_unlock_all_' . $achievement_type] = sprintf(__('Unlocked all %s', 'badgeos'), $post_type_object->labels->name); } // Loop through each trigger and add our trigger event to the hook foreach ($activity_triggers as $trigger => $label) { add_action($trigger, 'badgeos_trigger_event', 10, 20); } }
/** * AJAX Handler for saving all steps * * @since 1.0.0 * @return void */ function badgeos_update_steps_ajax_handler() { // Only continue if we have any steps if (isset($_POST['steps'])) { // Grab our $wpdb global global $wpdb; // Setup an array for storing all our step titles // This lets us dynamically update the Label field when steps are saved $new_titles = array(); // Loop through each of the created steps foreach ($_POST['steps'] as $key => $step) { // Grab all of the relevant values of that step $step_id = $step['step_id']; $required_count = !empty($step['required_count']) ? $step['required_count'] : 1; $trigger_type = $step['trigger_type']; $achievement_type = $step['achievement_type']; // Clear all relation data $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->p2p} WHERE p2p_to=%d", $step_id)); delete_post_meta($step_id, '_badgeos_achievement_post'); // Flip between our requirement types and make an appropriate connection switch ($trigger_type) { // Connect the step to ANY of the given achievement type case 'any-achievement': $title = sprintf(__('any %s', 'badgeos'), $achievement_type); break; case 'all-achievements': $title = sprintf(__('all %s', 'badgeos'), $achievement_type); break; case 'specific-achievement': p2p_create_connection($step['achievement_type'] . '-to-step', array('from' => absint($step['achievement_post']), 'to' => $step_id, 'meta' => array('date' => current_time('mysql')))); $title = '"' . get_the_title($step['achievement_post']) . '"'; break; case 'badgeos_specific_new_comment': update_post_meta($step_id, '_badgeos_achievement_post', absint($step['achievement_post'])); $title = sprintf(__('comment on post %d', 'badgeos'), $step['achievement_post']); break; default: $triggers = badgeos_get_activity_triggers(); $title = $triggers[$trigger_type]; break; } // Update the step order p2p_update_meta(badgeos_get_p2p_id_from_child_id($step_id), 'order', $key); // Update our relevant meta update_post_meta($step_id, '_badgeos_count', $required_count); update_post_meta($step_id, '_badgeos_trigger_type', $trigger_type); update_post_meta($step_id, '_badgeos_achievement_type', $achievement_type); // Available hook for custom Activity Triggers $custom_title = sprintf(__('Earn %1$s %2$s.', 'badgeos'), $title, sprintf(_n('%d time', '%d times', $required_count), $required_count)); $custom_title = apply_filters('badgeos_save_step', $custom_title, $step_id, $step); // Update our original post with the new title $post_title = !empty($step['title']) ? $step['title'] : $custom_title; wp_update_post(array('ID' => $step_id, 'post_title' => $post_title)); // Add the title to our AJAX return $new_titles[$step_id] = stripslashes($post_title); } // Send back all our step titles echo json_encode($new_titles); } // Cave Johnson. We're done here. die; }