/** * Add Badge Builder link to Featured Image meta box for achievement posts. * * @since 1.3.0 * * @param string $content Meta box content. * @param integer $post_id Post ID. * @return string Potentially updated content. */ function badgeos_badge_builder_filter_thumbnail_metabox($content, $post_id) { // Only add a link to achievement and achievement-type posts if (badgeos_is_achievement($post_id) || 'achievement-type' == get_post_type($post_id)) { // If no thumbnail, output standard badge builder link if (!has_post_thumbnail($post_id)) { $content .= '<p>' . badgeos_get_badge_builder_link(array('post_id' => $post_id, 'link_text' => __('Use Credly Badge Builder', 'badgeos'))) . '</p>'; // Otherwise, if thumbnail is a badge builder badge,` // output a "continue editing" link } else { $attachment_id = get_post_thumbnail_id($post_id); $continue = get_post_meta($attachment_id, '_credly_badge_meta', true); if ($continue) { $content .= '<p>' . badgeos_get_badge_builder_link(array('post_id' => $post_id, 'attachment_id' => $attachment_id, 'link_text' => __('Edit in Credly Badge Builder', 'badgeos'), 'continue' => $continue)) . '</p>'; } } } // Return the meta box content return $content; }
/** * Single Achievement Shortcode. * * @since 1.0.0 * * @param array $atts Shortcode attributes. * @return string HTML markup. */ function badgeos_achievement_shortcode($atts = array()) { // get the post id $atts = shortcode_atts(array('id' => get_the_ID()), $atts, 'badgeos_achievement'); // return if post id not specified if (empty($atts['id'])) { return; } wp_enqueue_style('badgeos-front'); wp_enqueue_script('badgeos-achievements'); // get the post content and format the badge display $achievement = get_post($atts['id']); $output = ''; // If we're dealing with an achievement post if (badgeos_is_achievement($achievement)) { $output .= '<div id="badgeos-single-achievement-container" class="badgeos-single-achievement">'; // necessary for the jquery click handler to be called $output .= badgeos_render_achievement($achievement); $output .= '</div>'; } // Return our rendered achievement return $output; }
/** * Change "Featured Image" to "Achievement Image" throughout media modal. * * @since 1.3.0 * * @param array $strings All strings passed to media modal. * @param object $post Post object. * @return array Potentially modified strings. */ function badgeos_media_modal_featured_image_text($strings = array(), $post = null) { if (is_object($post)) { if (badgeos_is_achievement($post->ID)) { $strings['setFeaturedImageTitle'] = __('Set Achievement Image', 'badgeos'); $strings['setFeaturedImage'] = __('Set achievement image', 'badgeos'); } elseif ('achievement-type' == $post->post_type) { $strings['setFeaturedImageTitle'] = __('Set Default Achievement Image', 'badgeos'); $strings['setFeaturedImage'] = __('Set default achievement image', 'badgeos'); } } return $strings; }
/** * Set default achievement image on achievement post save * * @since 1.2.0 * @param integer $post_id The post ID of the post being saved * @return mixed post ID if nothing to do, void otherwise. */ function badgeos_achievement_set_default_thumbnail($post_id) { global $pagenow; // Bail early if: // this IS NOT an achievement or achievement-type post // OR this IS an autosave // OR current user CAN NOT edit this post // OR the post already has a thumbnail // OR we've just loaded the new post page if (!(badgeos_is_achievement($post_id) || 'achievement-type' == get_post_type($post_id)) || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || !current_user_can('edit_post', $post_id) || has_post_thumbnail($post_id) || 'post-new.php' == $pagenow) { return $post_id; } $thumbnail_id = 0; // Get the thumbnail of our parent achievement if ('achievement-type' !== get_post_type($post_id)) { $achievement_type = get_page_by_path(get_post_type($post_id), OBJECT, 'achievement-type'); if ($achievement_type) { $thumbnail_id = get_post_thumbnail_id($achievement_type->ID); } } // If there is no thumbnail set, load in our default image if (empty($thumbnail_id)) { // Grab the default image $file = apply_filters('badgeos_default_achievement_post_thumbnail', 'https://credlyapp.s3.amazonaws.com/badges/af2e834c1e23ab30f1d672579d61c25a_15.png'); // Download file to temp location $tmp = download_url($file); // Set variables for storage // fix file filename for query strings preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches); $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if (is_wp_error($tmp)) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; } // Upload the image $thumbnail_id = media_handle_sideload($file_array, $post_id); // If upload errored, unlink the image file if (is_wp_error($thumbnail_id)) { @unlink($file_array['tmp_name']); // Otherwise, if the achievement type truly doesn't have // a thumbnail already, set this as its thumbnail, too. // We do this so that WP won't upload a duplicate version // of this image for every single achievement of this type. } elseif (badgeos_is_achievement($post_id) && is_object($achievement_type) && !get_post_thumbnail_id($achievement_type->ID)) { set_post_thumbnail($achievement_type->ID, $thumbnail_id); } } // Finally, if we have an image, set the thumbnail for our achievement if ($thumbnail_id && !is_wp_error($thumbnail_id)) { set_post_thumbnail($post_id, $thumbnail_id); } }
/** * Award an achievement to a user * * @since 1.0.0 * @param integer $achievement_id The given achievement ID to award * @param integer $user_id The given user's ID * @param string $this_trigger The trigger * @param integer $site_id The triggered site id * @param array $args The triggered args * @return mixed False on not achievement, void otherwise */ function badgeos_award_achievement_to_user($achievement_id = 0, $user_id = 0, $this_trigger = '', $site_id = 0, $args = array()) { global $wp_filter, $wp_version; // Sanity Check: ensure we're working with an achievement post if (!badgeos_is_achievement($achievement_id)) { return false; } // Use the current user ID if none specified if ($user_id == 0) { $user_id = wp_get_current_user()->ID; } // Get the current site ID none specified if (!$site_id) { $site_id = get_current_blog_id(); } // Setup our achievement object $achievement_object = badgeos_build_achievement_object($achievement_id); // Update user's earned achievements badgeos_update_user_achievements(array('user_id' => $user_id, 'new_achievements' => array($achievement_object))); // Log the earning of the award badgeos_post_log_entry($achievement_id, $user_id); // Available hook for unlocking any achievement of this achievement type do_action('badgeos_unlock_' . $achievement_object->post_type, $user_id, $achievement_id, $this_trigger, $site_id, $args); // Patch for WordPress to support recursive actions, specifically for badgeos_award_achievement // Because global iteration is fun, assuming we can get this fixed for WordPress 3.9 $is_recursed_filter = 'badgeos_award_achievement' == current_filter(); $current_key = null; // Get current position if ($is_recursed_filter) { $current_key = key($wp_filter['badgeos_award_achievement']); } // Available hook to do other things with each awarded achievement do_action('badgeos_award_achievement', $user_id, $achievement_id, $this_trigger, $site_id, $args); if ($is_recursed_filter) { reset($wp_filter['badgeos_award_achievement']); while (key($wp_filter['badgeos_award_achievement']) !== $current_key) { next($wp_filter['badgeos_award_achievement']); } } }
/** * Check if user is elligble to send an achievement to Credly. * * @since 1.3.4 * * @param integer $user_id User ID. * @param integer $achievement_id Achievement post ID. * @return bool True if achievement can be sent, otherwise false. */ function badgeos_can_user_send_achievement_to_credly($user_id = 0, $achievement_id = 0) { // If passed ID is not an achievement, bail here if (!badgeos_is_achievement($achievement_id)) { return false; } // If no user was specified, get the current user if (!$user_id) { $user_id = get_current_user_id(); } // Get all earned instances of this achievement $earned_achievements = badgeos_get_user_achievements(array('user_id' => $user_id, 'achievement_id' => $achievement_id)); // Loop through each earned instance if (!empty($earned_achievements)) { foreach ($earned_achievements as $key => $achievement) { // If this instance has not been sent to credly, it may be sent if (!badgeos_achievement_has_been_sent_to_credly($achievement)) { return true; } } } // No earned instances were eligable return false; }