Skip to content

Commit

Permalink
Update API to admin-ajax (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
avonville authored Jan 16, 2025
1 parent e1556ec commit 6a7321d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
24 changes: 24 additions & 0 deletions Generic_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function run() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_print_styles-toplevel_page_w3tc_dashboard', array( '\W3TC\Generic_Page_Dashboard', 'admin_print_styles_w3tc_dashboard' ) );
add_action( 'wp_ajax_w3tc_ajax', array( $this, 'wp_ajax_w3tc_ajax' ) );
add_action( 'wp_ajax_w3tc_forums_api', array( $this, 'wp_ajax_w3tc_forums_api' ), 10, 1 );

add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'admin_footer', array( $this, 'admin_footer' ) );
Expand Down Expand Up @@ -219,6 +220,29 @@ public function wp_ajax_w3tc_ajax() {
exit();
}

/**
* Forums API Callback
*
* This function reached out to the W3TC forums API to get the posts with the corresponding cache tag
* on boldgrid.com/support.
*
* @return void
*/
public function wp_ajax_w3tc_forums_api() {
if ( ! wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) ) {
wp_nonce_ays( 'w3tc' );
}

if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( 'no permissions', 403 );
}

$tag = Util_Request::get_string( 'tabId' );
$posts = wp_remote_get( W3TC_BOLDGRID_FORUM_API . $tag, array( 'timeout' => 10 ) );

wp_send_json( $posts );
}

/**
* Admin init (administrators only).
*/
Expand Down
67 changes: 40 additions & 27 deletions pub/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,45 +596,58 @@ jQuery(function() {

// Tutorial page forum links via API.
jQuery(document).on( 'click', '[data-tab-type="help"]', function() {

const $helpTab = jQuery( this ),
$inside = $helpTab.closest( ".postbox-tabs" ).find( ".inside" );
$forumTopicsContainer = $inside.find( '.help-forum-topics' );
isLoaded = $forumTopicsContainer.attr( 'data-loaded' ) === "1";
tabId = $forumTopicsContainer.attr( 'data-tab-id' );
const $helpTab = jQuery(this),
$inside = $helpTab.closest('.postbox-tabs').find('.inside');
$forumTopicsContainer = $inside.find('.help-forum-topics');
isLoaded = $forumTopicsContainer.attr('data-loaded') === '1';
tabId = $forumTopicsContainer.attr('data-tab-id');

// Check if topics are already loaded
if ( isLoaded ) return;
// Construct the API URL with the tab ID
const apiUrl = `https://boldgrid.com/support/wp-json/w3tc/v1/help_topics?tag=${tabId}`;
if (isLoaded) {
return;
}

// Fetch topics from the API
jQuery.ajax({
url: apiUrl,
method: 'GET',
dataType: 'json',
success: function( data ) {
// Check for errors or empty results
if ( Array.isArray( data ) && data.length === 0 ) {
$forumTopicsContainer.html( "<p>No forum topics found.</p>" );
url: ajaxurl,
method: 'POST',
data: {
action: 'w3tc_forums_api',
_wpnonce: w3tc_nonce[0],
tabId: tabId
},
success: function(data) {
// Check for timeout
if ( data.errors && data.errors.http_request_failed ) {
$forumTopicsContainer.html('HTTP Error:', data.errors.http_request_failed);
}
// Check for empty results
else if (Array.isArray(data) && data.length === 0) {
$forumTopicsContainer.html('<p>No forum topics found.</p>');
} else {
// Create a list of topics
const $ul = jQuery( '<ul></ul>' );
jQuery.each( data, function( index, topic ) {
const $li = jQuery( '<li></li>' );
const $link = jQuery( '<a></a>' ).addClass('w3tc-control-after').attr( 'href', topic.link ).text( topic.title ).attr( 'target', '_blank' ); // Open in new tab
const $icon = jQuery( '<span></span>' ).addClass( 'dashicons dashicons-external' );
$link.append( $icon );
$li.append( $link );
$ul.append( $li );
const $ul = jQuery('<ul></ul>');
const forumData = JSON.parse(data.body);
jQuery.each(forumData, function(index, topic) {
const $li = jQuery('<li></li>');
const $link = jQuery('<a></a>').addClass('w3tc-control-after').attr('href', topic.link).attr('target', '_blank'); // Open in new tab

// Decode HTML entities in topic.title
const decodedTitle = jQuery('<textarea />').html(topic.title).text();
$link.text(decodedTitle);

const $icon = jQuery('<span></span>').addClass('dashicons dashicons-external');
$link.append($icon);
$li.append($link);
$ul.append($li);
});
$forumTopicsContainer.html( $ul );
$forumTopicsContainer.html($ul);
}
// Mark topics as loaded to prevent duplicate requests
$forumTopicsContainer.attr( 'data-loaded', "1" );
$forumTopicsContainer.attr('data-loaded', '1');
},
error: function() {
$forumTopicsContainer.html( "<p>Error loading topics. Please try again later.</p>" );
$forumTopicsContainer.html('<p>Error loading topics. Please try again later.</p>');
}
});
});
Expand Down
1 change: 1 addition & 0 deletions w3-total-cache-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
define( 'W3TC_PARTNER_A2', 'https://api.w3-edge.com/v1/redirects/partners/a2' );
define( 'W3TC_PARTNER_CONVESIO', 'https://api.w3-edge.com/v1/redirects/partners/convesio' );
define( 'W3TC_PARTNER_DREAMHOST', 'https://api.w3-edge.com/v1/redirects/partners/dreamhost' );
define( 'W3TC_BOLDGRID_FORUM_API', 'https://www.boldgrid.com/support/wp-json/w3tc/v1/help_topics?tag=' );

// Admin notices from API.
if ( ! defined( 'W3TC_NOTICE_FEED' ) ) {
Expand Down

0 comments on commit 6a7321d

Please sign in to comment.