Skip to content

Commit

Permalink
Merge branch 'refs/heads/enhancement/issue-1571' into release/flotte-…
Browse files Browse the repository at this point in the history
…prerelease

# Conflicts:
#	assets/admin/js/admin.min.js
#	includes/Public.php
  • Loading branch information
hansmorb committed May 7, 2024
2 parents f8cda08 + 048b76d commit 69f3d77
Show file tree
Hide file tree
Showing 16 changed files with 855 additions and 238 deletions.
6 changes: 6 additions & 0 deletions assets/admin/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -5063,6 +5063,12 @@ a.disabled {
}

/** Migration Settings */
#upgrade-in-progress,
#upgrade-done {
margin-top: 1rem;
display: none;
}

#migration-in-progress,
#migration-state,
#migration-done {
Expand Down
29 changes: 29 additions & 0 deletions assets/admin/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,35 @@
(function($) {
"use strict";
$(function() {
if ($("#upgrade-fields").length == 0) {
$(".cmb2-id-upgrade-header").hide();
}
$("#cmb2-metabox-migration #run-upgrade").on("click", function(event) {
event.preventDefault();
$("#upgrade-in-progress").show();
$("#run-upgrade").hide();
let data = {
progress: {
task: 0,
page: 1
}
};
const runUpgrade = data => {
$.post(cb_ajax_run_upgrade.ajax_url, {
_ajax_nonce: cb_ajax_run_upgrade.nonce,
action: "cb_run_upgrade",
data: data
}, function(data) {
if (data.success) {
$("#upgrade-in-progress").hide();
$("#upgrade-done").show();
} else {
runUpgrade(data);
}
});
};
runUpgrade(data);
});
$("#cmb2-metabox-migration #migration-start").on("click", function(event) {
event.preventDefault();
$("#migration-state").show();
Expand Down
2 changes: 1 addition & 1 deletion assets/admin/js/admin.min.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions assets/admin/js/src/migration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
(function ($) {
'use strict';
$(function () {

//conditionally hide entire field group "upgrade" if render function has not been called
//(workaround for show_on_cb not working on field groups)
if ($('#upgrade-fields').length == 0) {
$('.cmb2-id-upgrade-header').hide();
}

$('#cmb2-metabox-migration #run-upgrade').on('click', function (event) {
event.preventDefault();
$('#upgrade-in-progress').show();
$('#run-upgrade').hide();
let data = {
'progress' : {
'task': 0,
'page': 1,
}
};

const runUpgrade = (data) => {
$.post(
cb_ajax_run_upgrade.ajax_url,
{
_ajax_nonce: cb_ajax_run_upgrade.nonce,
action: "cb_run_upgrade",
data: data
},
function (data) {
if (data.success) {
$('#upgrade-in-progress').hide();
$('#upgrade-done').show();
} else {
runUpgrade(data)
}
});
};
runUpgrade(data);
});

$('#cmb2-metabox-migration #migration-start').on('click', function (event) {
event.preventDefault();
$('#migration-state').show();
Expand Down
7 changes: 7 additions & 0 deletions assets/admin/sass/partials/_migration.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#upgrade-in-progress,
#upgrade-done,
{
margin-top: 1rem;
display: none;
}

#migration-in-progress,
#migration-state,
#migration-done
Expand Down
10 changes: 10 additions & 0 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ function commonsbooking_admin() {
)
);

// \CommonsBooking\Service\Upgrade Ajax tasks
wp_localize_script(
'cb-scripts-admin',
'cb_ajax_run_upgrade',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'cb_run_upgrade' ),
)
);

// Additional info for CMB2 to handle booking rules
wp_add_inline_script(
'cb-scripts-admin',
Expand Down
14 changes: 14 additions & 0 deletions includes/OptionsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,20 @@
'title' => __( 'Migration', 'commonsbooking' ),
'id' => 'migration',
'field_groups' => array(
'upgrade' => array(
'title' => esc_html__( 'Finish upgrade to latest version', 'commonsbooking' ),
'id' => 'upgrade',
'desc' => commonsbooking_sanitizeHTML( __( 'Click here to finish the upgrade to the latest version. <br> This needs to be done after updating the plugin to a new version. <br> If you do not do this, the plugin may not work correctly.', 'commonsbooking' ) ),
'fields' => [
array(
'name' => commonsbooking_sanitizeHTML( __( 'Finish upgrade', 'commonsbooking' ) ),
'id' => 'upgrade-custom-field',
'type' => 'text',
'render_row_cb' => array( Migration::class, 'renderUpgradeForm' ),
)
],
),
// migration cb1 -> cb2
'migration' => array(
'title' => esc_html__( 'Migrate from Commons Booking Version 0.X', 'commonsbooking' ),
'id' => 'migration',
Expand Down
1 change: 1 addition & 0 deletions includes/Public.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function commonsbooking_public() {
//migration AJAX
add_action( 'wp_ajax_cb_start_migration', array( Migration::class, 'migrateAll' ) );
add_action( 'wp_ajax_cb_start_booking_migration', array( \CommonsBooking\Migration\Booking::class, 'ajaxMigrate' ) );
add_action( 'wp_ajax_cb_run_upgrade', array( \CommonsBooking\Service\Upgrade::class, 'runAJAXUpgradeTasks' ) );

//getting bookable Location for item AJAX
add_action( 'wp_ajax_cb_get_bookable_location', array( \CommonsBooking\View\Booking::class, 'getLocationForItem_AJAX' ) );
Expand Down
49 changes: 49 additions & 0 deletions src/Repository/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,55 @@ public static function get(
}
}

/**
* Will get all timeframes in the database to perform mass operations on (like migrations).
*
* @param int $page
* @param int $perPage
* @param array $customArgs
*
* @return \stdClass Properties: array posts, int totalPosts, int totalPages, bool done
* @throws Exception
*/
public static function getAllPaginated(
int $page = 1,
int $perPage = 10,
array $customArgs = []
): \stdClass {
$args = [
'post_type' => \CommonsBooking\Wordpress\CustomPostType\Timeframe::getPostType(),
'paged' => $page,
'posts_per_page' => $perPage,
'orderby' => 'ID',
'order' => 'ASC',
];
$args = array_merge( $args, $customArgs );
$query = new \WP_Query( $args );

if ( $query->have_posts() ) {
$posts = $query->get_posts();
self::castPostsToModels( $posts );

return (object) (
[
'posts' => $posts,
'totalPosts' => $query->found_posts,
'totalPages' => $query->max_num_pages,
'done' => $page >= $query->max_num_pages
]
);
}

return (object) (
[
'posts' => [],
'totalPosts' => 0,
'totalPages' => 0,
'done' => true
]
);
}

/**
* Returns Post-IDs of timeframes by type(s), item(s), location(s)
*
Expand Down
Loading

0 comments on commit 69f3d77

Please sign in to comment.