Remove REST API endpoints and related functionality from Oribi Sync

This commit is contained in:
Matt Batchelder
2026-02-21 13:07:41 -05:00
parent b01e7e0e88
commit 6c5e503eb2
4 changed files with 80 additions and 163 deletions

View File

@@ -8,27 +8,47 @@
if ( ! defined( 'ABSPATH' ) ) exit;
// ─── Admin bar pull button (front-end) ─────────────────────────────────────
// ─── Admin bar pull buttons (front-end) ─────────────────────────────────────
add_action( 'admin_bar_menu', function ( WP_Admin_Bar $wp_admin_bar ) {
// Front-end only, logged-in admins, singular pages/posts
if ( is_admin() ) return;
if ( ! is_user_logged_in() ) return;
if ( ! current_user_can( 'manage_options' ) ) return;
if ( ! is_singular() ) return;
$post = get_queried_object();
if ( ! $post instanceof WP_Post ) return;
// "Pull All" — visible everywhere on the front-end
$wp_admin_bar->add_node( [
'id' => 'oribi-sync-pull',
'title' => '<span class="ab-icon dashicons dashicons-download" aria-hidden="true"></span><span class="ab-label">Pull Page</span>',
'id' => 'oribi-sync-pull-all',
'title' => '<span class="ab-icon dashicons dashicons-update" aria-hidden="true"></span><span class="ab-label">Pull All</span>',
'href' => '#',
'meta' => [
'title' => 'Pull this page and theme from Git',
'title' => 'Pull all pages and theme from Git',
],
] );
// "Pull Page" — only on singular pages/posts
if ( is_singular() ) {
$post = get_queried_object();
if ( $post instanceof WP_Post ) {
$wp_admin_bar->add_node( [
'id' => 'oribi-sync-pull',
'title' => '<span class="ab-icon dashicons dashicons-download" aria-hidden="true"></span><span class="ab-label">Pull Page</span>',
'href' => '#',
'meta' => [
'title' => 'Pull this page and theme from Git',
],
] );
}
}
}, 100 );
// AJAX handler for the admin bar "Pull All" button
add_action( 'wp_ajax_oribi_sync_pull_all_pages', function () {
check_ajax_referer( 'oribi_sync_pull_all_pages' );
if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Permission denied.', 403 );
$result = oribi_sync_run();
$result['ok'] ? wp_send_json_success( $result ) : wp_send_json_error( $result, 500 );
} );
// AJAX handler for the admin bar pull button (no REST API exposure)
add_action( 'wp_ajax_oribi_sync_pull_page', function () {
check_ajax_referer( 'oribi_sync_pull_page' );
@@ -41,6 +61,57 @@ add_action( 'wp_ajax_oribi_sync_pull_page', function () {
$result['ok'] ? wp_send_json_success( $result ) : wp_send_json_error( $result, 500 );
} );
// Front-end script for the "Pull All" admin bar button
add_action( 'wp_footer', function () {
if ( ! is_user_logged_in() ) return;
if ( ! current_user_can( 'manage_options' ) ) return;
if ( ! is_admin_bar_showing() ) return;
$ajax_url = admin_url( 'admin-ajax.php' );
$nonce_all = wp_create_nonce( 'oribi_sync_pull_all_pages' );
?>
<script>
(function () {
'use strict';
var btn = document.getElementById('wp-admin-bar-oribi-sync-pull-all');
if (!btn) return;
btn.addEventListener('click', function (e) {
e.preventDefault();
var link = btn.querySelector('a');
var label = btn.querySelector('.ab-label');
if (link) { link.style.opacity = '0.5'; link.style.pointerEvents = 'none'; }
if (label) { label.textContent = 'Pulling…'; }
var data = new URLSearchParams({
action: 'oribi_sync_pull_all_pages',
_ajax_nonce: <?php echo wp_json_encode( $nonce_all ); ?>
});
fetch(<?php echo wp_json_encode( $ajax_url ); ?>, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: data.toString()
})
.then(function (r) { return r.json(); })
.then(function () {
var url = new URL(window.location.href);
url.searchParams.set('_nocache', Date.now());
window.location.replace(url.toString());
})
.catch(function (err) {
if (label) { label.textContent = 'Pull All'; }
if (link) { link.style.opacity = ''; link.style.pointerEvents = ''; }
alert('Oribi Sync pull failed: ' + err);
});
});
})();
</script>
<?php
} );
// Front-end script that wires up the admin bar pull button
add_action( 'wp_footer', function () {
if ( ! is_user_logged_in() ) return;