Skip to content

Commit

Permalink
Merge pull request #683 from FarooqAbdullah/version-3.2
Browse files Browse the repository at this point in the history
Version 3.2
  • Loading branch information
the-wooninjas authored Sep 2, 2019
2 parents 361f53d + 9e59cca commit 263024a
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

#### 3.2
- New: Added option to update the existing achievements with regards the point types
- New: Added default thumbnail for the rank post
- Fix: Fixed conflict with scormcloud plugin
- Fix: Fixed the point and rank type pages 404 issue

#### 3.1
- New: Added option to upgrade db from plugin setting page
- New: An email will be sent to admin on completing the db upgrade
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ Note: You may have to update all the existing badges too in order to sync the po
* [BadgeOS Community add-on](https://wordpress.org/plugins/badgeos-community-add-on/ "BadgeOS Community add-on")
* [BadgeOS LearnDash](https://wordpress.org/plugins/badgeos-learndash-add-on/ "BadgeOS Auto LearnDash")

After updating the plugin to 3.0, You will need to update the users' earning and achievements. Follow the steps below to update them.

**Note: please clear the cache of the site before updating the user's earning and existing achievements.**

**Update users' earned achievements and points:**
- Update the BadgeOS to 3.1 or greater
- Go to Dashboard -> BadgeOS -> Point Type
- Create a new point type (if not created already)
- Go to Dashboard -> BadgeOS -> Settings
- Select the default point type and click on save settings button
- Click on the "Upgrade 3.0 DB" button

**Update existing achievements:**
- Update the BadgeOS to 3.2 or greater
- Go to Dashboard -> BadgeOS -> Point Type
- Create a new point type (if not created already)
- Go to Dashboard -> BadgeOS -> Settings
- Select the default point type and click on save settings button
- Click on the "Upgrade Achievements" button

[BadgeOS](http://badgeos.org "BadgeOS")™ turns your WordPress site into an achievement and badging system. Your site's users complete steps, demonstrate skills and knowledge, and earn digital badges. Easily define the achievements, organize the badge requirements any way you like, and choose from and combine a range of assessment options to determine whether each task or requirement has been achieved.

Create badges and set up the achievements to earn them. Badges are Mozilla OBI compatible and sharable via [Credly](https://credly.com/ "Credly.com"), the free web service for managing and displaying lifelong credentials.
Expand Down
10 changes: 6 additions & 4 deletions badgeos.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: http://www.badgeos.org/
* Description: BadgeOS lets your site’s users complete tasks and earn badges that recognize their achievement. Define achievements and choose from a range of options that determine when they're complete. Badges are Mozilla Open Badges (OBI) compatible through integration with the “Open Credit” API by Credly, the free web service for issuing, earning and sharing badges for lifelong achievement.
* Author: LearningTimes
* Version: 3.1
* Version: 3.2
* Author URI: https://credly.com/
* License: GNU AGPL
* Text Domain: badgeos
Expand Down Expand Up @@ -33,7 +33,7 @@ class BadgeOS {
*
* @var string
*/
public static $version = '3.1';
public static $version = '3.2';

function __construct() {
// Define plugin constants
Expand Down Expand Up @@ -184,6 +184,7 @@ function includes() {
* Move achivements from meta to db
*/
require_once( $this->directory_path . 'includes/meta-to-db.php' );
require_once( $this->directory_path . 'includes/achievement-upgrade.php' );

/**
* Point files
Expand Down Expand Up @@ -227,6 +228,7 @@ function includes() {
function register_scripts_and_styles() {
// Register scripts
wp_register_script( 'badgeos-admin-js', $this->directory_url . 'js/admin.js', array( 'jquery' ) );
wp_register_script( 'badgeos-admin-js', $this->directory_url . 'js/admin.js', array( 'jquery' ), '3.2', true );
wp_register_script( 'badgeos-credly', $this->directory_url . 'js/credly.js' );
wp_register_script( 'badgeos-achievements', $this->directory_url . 'js/badgeos-achievements.js', array( 'jquery' ), '1.1.0', true );
wp_register_script( 'credly-badge-builder', $this->directory_url . 'js/credly-badge-builder.js', array( 'jquery' ), '1.3.0', true );
Expand Down Expand Up @@ -554,8 +556,8 @@ function badgeos_is_debug_mode() {

}

if ( ! function_exists('write_log')) {
function write_log ( $log ) {
if ( ! function_exists('badgeos_write_log')) {
function badgeos_write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
Expand Down
Binary file added images/rank-main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions includes/achievement-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,6 @@ function badgeos_get_network_site_ids() {
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 )
Expand Down Expand Up @@ -950,7 +944,10 @@ function badgeos_flush_rewrite_on_published_achievement( $new_status, $old_statu
function badgeos_flush_rewrite_rules() {
badgeos_register_post_types();
badgeos_register_achievement_type_cpt();
flush_rewrite_rules();
badgeos_register_ranks_post_types();
badgeos_register_ranks_type_cpt();
badgeos_register_points_post_types();
flush_rewrite_rules();
}

/**
Expand Down
99 changes: 99 additions & 0 deletions includes/achievement-upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* Handles the migrate points to points fields ajax call
*
* @since 3.2
*/
function badgeos_migrate_fields_points_to_point_types_callback() {
ini_set( 'display_errors', 'On' );
error_reporting( E_ALL );
$action = ( isset( $_POST['action'] ) ? $_POST['action'] : '' );
if( $action !== 'badgeos_migrate_fields_points_to_point_types' && $action !== 'badgeos_migrate_fields_points_to_point_types' ) {
exit;
}
$badgeos_settings = get_option( 'badgeos_settings' );
$default_point_type = ( ! empty ( $badgeos_settings['default_point_type'] ) ) ? $badgeos_settings['default_point_type'] : '';

if( ! empty( $default_point_type ) ) {
if ( ! wp_next_scheduled ( 'cron_badgeos_update_old_points_to_point_types' ) ) {
wp_schedule_single_event( time(), 'cron_badgeos_update_old_points_to_point_types' );
echo '<img src="'.admin_url( 'images/spinner.gif' ).'" /> &nbsp;&nbsp;'.__( 'BadgeOS is updating points data as a background process. You will receive a confirmation email upon successful completion. You can continue exploring badgeos.', 'badgeos' );
} else {
echo __( 'Points updation process is already under process. Thanks.', 'badgeos' );
}

} else {
echo __( 'Default point type field is not defined. Kindly, configure the default point type field type first. Thanks.', 'badgeos' );
}
exit;
}

add_action( 'wp_ajax_badgeos_migrate_fields_points_to_point_types', 'badgeos_migrate_fields_points_to_point_types_callback' );

function cron_badgeos_update_old_points_to_point_types_callback() {

badgeos_update_old_points_to_point_types();
}
add_action( 'cron_badgeos_update_old_points_to_point_types', 'cron_badgeos_update_old_points_to_point_types_callback' );

function badgeos_update_old_points_to_point_types() {

$badgeos_settings = get_option( 'badgeos_settings' );
$default_point_type = ( ! empty ( $badgeos_settings['default_point_type'] ) ) ? $badgeos_settings['default_point_type'] : '';
if( ! empty( $default_point_type ) ) {
// Grab all of our achievement type posts
$achievement_types = get_posts( array(
'post_type' => 'achievement-type',
'posts_per_page' => -1,
) );

// Loop through each achievement type post and register it as a CPT
foreach ( $achievement_types as $achievement_type ) {

$achievements = get_posts( array(
'post_type' => $achievement_type->post_name,
'posts_per_page' => -1,
) );

foreach( $achievements as $achievement ) {

$achievement_id = $achievement->ID;
$points = get_post_meta( $achievement_id, '_badgeos_points', true );
if( isset( $points ) && ( ! is_array( $points ) || empty($points) ) ) {
$points_array = array();
$points_array[ '_badgeos_points' ] = intval( $points );
$points_array[ '_badgeos_points_type' ] = $default_point_type;
update_post_meta( $achievement_id, '_badgeos_points', $points_array );
}

$points_required = get_post_meta( $achievement_id, '_badgeos_points_required', true );
if( isset( $points_required ) && ( ! is_array( $points_required ) || empty($points_required) ) ) {
$points_req_array = array();
$points_req_array[ '_badgeos_points_required' ] = intval( $points_required );
$points_req_array[ '_badgeos_points_required_type' ] = $default_point_type;
update_post_meta( $achievement_id, '_badgeos_points_required', $points_req_array );
}
}
}

$from_title = get_bloginfo( 'name' );
$from_email = get_bloginfo( 'admin_email' );
$headers[] = 'From: '.$from_title.' <'.$from_email.'>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';

$body = '<table border="0" cellspacing="0" cellpadding="0" align="center">';
$body .= '<tr valign="top">';
$body .= '<td>';
$body .= '<p>'.__( 'Hi', 'badgeos' ).' '.$from_title.'</p>';
$body .= '<p>'.__( "Your site existing badges have been updated with regards to the point type.", 'badgeos' ).'</p>';
$body .= '</td></tr>';
$body .= '<tr valign="top"><td></td></tr>';
$body .= '<tr valign="top"><td>'.__( 'Thanks', 'badgeos' ).'</td></tr>';
$body .= '</table>';

wp_mail( $from_email, 'BadgeOS Points Format Upgrade', $body, $headers );

}
}
?>
21 changes: 20 additions & 1 deletion includes/admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function badgeos_settings_page() {
</tr>
<tr valign="top">
<td>
<p><?php _e( 'Please click on "Upgrade 3.0 DB" button below to shift data from the old meta based structure to new database structure.', 'badgeos' ); ?></p>
<p><?php _e( 'Please click on "Upgrade 3.0 DB" button below to update the users\' existing achievements and points in the badgeos table.', 'badgeos' ); ?></p>
</td>
</tr>
<tr valign="top">
Expand All @@ -386,6 +386,25 @@ function badgeos_settings_page() {
</tr>
</table>

<table class="form-table badgeos-migration-form-table">
<tr valign="top">
<td>
<h3><?php _e( 'BadgeOS Upgrade Achievement', 'badgeos' ); ?></h3>
</td>
</tr>
<tr valign="top">
<td>
<p><?php _e( 'Please click on "Upgrade Achievements" button below to update the existing achievement points with the point types.', 'badgeos' ); ?></p>
</td>
</tr>
<tr valign="top">
<td>
<input type="button" id="badgeos_migrate_fields_single_to_multi" class="button-primary" value="<?php _e( 'Upgrade Achievements', 'badgeos' ); ?>" />
<div class="badgeos_migrate_fields_single_to_multi_message"></div>
</td>
</tr>
</table>

</form>
</div>
<?php
Expand Down
2 changes: 1 addition & 1 deletion includes/meta-to-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function badgeos_update_from_meta_to_db_callback() {
$body .= '<tr valign="top">';
$body .= '<td>';
$body .= '<p>'.__( 'Hi', 'badgeos' ).' '.$from_title.'</p>';
$body .= '<p>'.__( "Your site points and achievements has been transferred from users' meta to points and achievement tables.", 'badgeos' ).'</p>';
$body .= '<p>'.__( "Your site users' existing achievements and points have been updated to the BadgeOS table.", 'badgeos' ).'</p>';
$body .= '</td></tr>';
$body .= '<tr valign="top"><td></td></tr>';
$body .= '<tr valign="top"><td>'.__( 'Thanks', 'badgeos' ).'</td></tr>';
Expand Down
16 changes: 16 additions & 0 deletions includes/points/point-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ function badgeos_get_users_points( $user_id = 0, $achievement_id = 0 ) {
}
}

/**
* Flush rewrite rules on publishing a rank
*
* @param $new_status
* @param $old_status
* @param $post
*/
function badgeos_flush_rewrite_on_published_poings( $new_status, $old_status, $post ) {

$settings = ( $exists = get_option( 'badgeos_settings' ) ) ? $exists : array();
if ( trim( $settings['points_main_post_type'] ) === $post->post_type && 'publish' === $new_status && 'publish' !== $old_status ) {
badgeos_flush_rewrite_rules();
}
}
add_action( 'transition_post_status', 'badgeos_flush_rewrite_on_published_poings', 10, 3 );

/**
* Return credit types
*
Expand Down
4 changes: 2 additions & 2 deletions includes/ranks/post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function badgeos_register_ranks_type_cpt() {
/**
* Determine whether this achievement type should be visible in the menu
*/
$show_in_menu = get_post_meta( $rank_type->ID, '_badgeos_show_in_menu', true ) ? 'badgeos_ranks' : false;
$show_in_menu = get_post_meta( strtolower( $rank_type->ID ), '_badgeos_show_in_menu', true ) ? 'badgeos_ranks' : false;

/**
* Register the post type
Expand All @@ -150,7 +150,7 @@ function badgeos_register_ranks_type_cpt() {
'show_ui' => current_user_can( badgeos_get_manager_capability() ),
'show_in_menu' => $show_in_menu,
'query_var' => true,
'rewrite' => array( 'slug' => $rank_type->post_name ),
'rewrite' => array( 'slug' => strtolower( $rank_type->post_name ) ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
Expand Down
89 changes: 88 additions & 1 deletion includes/ranks/rank-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,16 @@ function badgeos_flush_rewrite_on_published_rank( $new_status, $old_status, $pos
if ( trim( $settings['ranks_main_post_type'] ) === $post->post_type && 'publish' === $new_status && 'publish' !== $old_status ) {
badgeos_flush_rewrite_rules();
}

$rank_types = get_posts( array(
'post_type' => trim( $settings['ranks_main_post_type'] ),
'posts_per_page' => -1,
) );
foreach ( $rank_types as $rank_type ) {
if ( trim( $rank_type->post_name ) === $post->post_type && 'publish' === $new_status && 'publish' !== $old_status ) {
badgeos_flush_rewrite_rules();
}
}
}
add_action( 'transition_post_status', 'badgeos_flush_rewrite_on_published_rank', 10, 3 );

Expand Down Expand Up @@ -1868,4 +1878,81 @@ function badgeos_get_rank_image( $rank_id = 0, $rank_width = '50', $rank_height
}

return $ranks_image;
}
}

/**
* Set default ranks image on achievement post save
*
* @param integer $post_id The post ID of the post being saved
* @return mixed post ID if nothing to do, void otherwise.
*/
function badgeos_ranks_set_default_thumbnail( $post_id ) {

global $pagenow;

$badgeos_settings = get_option( 'badgeos_settings' );
if (
! (
badgeos_is_rank( $post_id )
|| $badgeos_settings['ranks_main_post_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;

// If there is no thumbnail set, load in our default image
if ( empty( $thumbnail_id ) ) {
global $wpdb;

// Grab the default image
$file = apply_filters( 'badgeos_default_rank_post_thumbnail', badgeos_get_directory_url().'images/rank-main.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 ( empty( $thumbnail_id ) || 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_rank( $post_id )
&& is_object( $achievement_type )
&& ! get_post_thumbnail_id( $achievement_type->ID )
) {
set_post_thumbnail( $achievement_type->ID, $thumbnail_id );
badgeos_flush_rewrite_rules();
}
}

// 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 );
badgeos_flush_rewrite_rules();
}
}
add_action( 'save_post', 'badgeos_ranks_set_default_thumbnail' );
Loading

0 comments on commit 263024a

Please sign in to comment.