Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated api to admin-ajax #1019

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 200 );
}

/**
* Admin init (administrators only).
*/
Expand Down
20 changes: 12 additions & 8 deletions pub/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,22 +605,25 @@ jQuery(function() {

// 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}`;

// Fetch topics from the API
jQuery.ajax({
url: apiUrl,
method: 'GET',
dataType: 'json',
url: ajaxurl,
method: 'POST',
data: {
action: 'w3tc_forums_api',
_wpnonce: w3tc_nonce[0],
tabId: tabId
},
success: function( data ) {
// Check for errors or empty results
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 $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 ).text( topic.title ).attr( 'target', '_blank' ); // Open in new tab
const $icon = jQuery( '<span></span>' ).addClass( 'dashicons dashicons-external' );
Expand All @@ -633,7 +636,8 @@ jQuery(function() {
// Mark topics as loaded to prevent duplicate requests
$forumTopicsContainer.attr( 'data-loaded', "1" );
},
error: function() {
error: function( data ) {
console.log( data );
$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