diff --git a/CHANGELOG.md b/CHANGELOG.md index 10be4196..b0983bcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index bc30ea49..6ebe9fff 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/badgeos.php b/badgeos.php index 9a99c58d..2ef80315 100755 --- a/badgeos.php +++ b/badgeos.php @@ -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 @@ -33,7 +33,7 @@ class BadgeOS { * * @var string */ - public static $version = '3.1'; + public static $version = '3.2'; function __construct() { // Define plugin constants @@ -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 @@ -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 ); @@ -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 { diff --git a/images/rank-main.png b/images/rank-main.png new file mode 100644 index 00000000..4cb6bcec Binary files /dev/null and b/images/rank-main.png differ diff --git a/includes/achievement-functions.php b/includes/achievement-functions.php index 5cd1d799..0bb9ae62 100755 --- a/includes/achievement-functions.php +++ b/includes/achievement-functions.php @@ -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 ) @@ -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(); } /** diff --git a/includes/achievement-upgrade.php b/includes/achievement-upgrade.php new file mode 100644 index 00000000..05c1f219 --- /dev/null +++ b/includes/achievement-upgrade.php @@ -0,0 +1,99 @@ +   '.__( '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 = ''; + $body .= ''; + $body .= ''; + $body .= ''; + $body .= ''; + $body .= '
'; + $body .= '

'.__( 'Hi', 'badgeos' ).' '.$from_title.'

'; + $body .= '

'.__( "Your site existing badges have been updated with regards to the point type.", 'badgeos' ).'

'; + $body .= '
'.__( 'Thanks', 'badgeos' ).'
'; + + wp_mail( $from_email, 'BadgeOS Points Format Upgrade', $body, $headers ); + + } +} +?> \ No newline at end of file diff --git a/includes/admin-settings.php b/includes/admin-settings.php index b7116650..c817438f 100644 --- a/includes/admin-settings.php +++ b/includes/admin-settings.php @@ -367,7 +367,7 @@ function badgeos_settings_page() { -

+

@@ -386,6 +386,25 @@ function badgeos_settings_page() { + + + + + + + + + + +
+

+
+

+
+ +
+
+ '; $body .= ''; $body .= '

'.__( 'Hi', 'badgeos' ).' '.$from_title.'

'; - $body .= '

'.__( "Your site points and achievements has been transferred from users' meta to points and achievement tables.", 'badgeos' ).'

'; + $body .= '

'.__( "Your site users' existing achievements and points have been updated to the BadgeOS table.", 'badgeos' ).'

'; $body .= ''; $body .= ''; $body .= ''.__( 'Thanks', 'badgeos' ).''; diff --git a/includes/points/point-functions.php b/includes/points/point-functions.php index c56beb2d..c3ffbef7 100644 --- a/includes/points/point-functions.php +++ b/includes/points/point-functions.php @@ -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 * diff --git a/includes/ranks/post-types.php b/includes/ranks/post-types.php index 77198db7..cefac5c6 100644 --- a/includes/ranks/post-types.php +++ b/includes/ranks/post-types.php @@ -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 @@ -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, diff --git a/includes/ranks/rank-functions.php b/includes/ranks/rank-functions.php index ea0142a5..0b2664d8 100644 --- a/includes/ranks/rank-functions.php +++ b/includes/ranks/rank-functions.php @@ -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 ); @@ -1868,4 +1878,81 @@ function badgeos_get_rank_image( $rank_id = 0, $rank_width = '50', $rank_height } return $ranks_image; -} \ No newline at end of file +} + +/** + * 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' ); \ No newline at end of file diff --git a/js/admin.js b/js/admin.js index ba9cbe95..935b8c53 100644 --- a/js/admin.js +++ b/js/admin.js @@ -258,7 +258,7 @@ jQuery(document).ready(function($) { }); $( '#badgeos_migrate_meta_to_db' ).click( function() { - var confirmation = confirm( 'It will delete the old shifted data and shift data again from scratch.' ); + var confirmation = confirm( "It will update the users' existing achievements and points in the badgeos table." ); if( confirmation ) { var data = { 'action': 'badgeos_migrate_data_from_meta_to_db' @@ -269,8 +269,24 @@ jQuery(document).ready(function($) { } }); + $( '#badgeos_migrate_fields_single_to_multi' ).click( function() { + var confirmation = confirm( 'It will update the existing achievement points with the point types.' ); + if( confirmation ) { + var data = { + 'action': 'badgeos_migrate_fields_points_to_point_types' + }; + $.post( admin_js.ajax_url, data, function(response) { + $( '.badgeos_migrate_fields_single_to_multi_message' ).html( '

'+response+'

' ).slideDown(); + + /*setTimeout( function() { + $( '.badgeos_migrate_fields_single_to_multi_message' ).slideUp(); + }, 3000 );*/ + } ); + } + }); + $( '#badgeos_notice_update_from_meta_to_db' ).click( function() { - var confirmation = confirm( 'It will delete the old shifted data and shift data again from scratch.' ); + var confirmation = confirm( "It will update the users' existing achievements and points in the badgeos table" ); if( confirmation ) { var data = { 'action': 'badgeos_migrate_data_from_meta_to_db_notice' diff --git a/readme.txt b/readme.txt index 8bc85cc2..8464abfe 100755 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: http://badgeos.org/contribute/donate/ Tags: badge, badges, openbadges, learningtimes, OBI, mozilla, open badges, achievement, award, reward, engagement, submission, nomination, API, open credit, credit Requires at least: 4.0 Tested up to: 5.2.2 -Stable tag: 3.1 +Stable tag: 3.2 License: GNU AGPLv3 License URI: http://www.gnu.org/licenses/agpl-3.0.html @@ -22,6 +22,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, badging and engagement management system. Your 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. @@ -270,6 +290,12 @@ Thanks for asking! Please do share back code modifications or enhancements you == 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