From dd981afe7bcdd28bd22fe096ee6e7780d413ac09 Mon Sep 17 00:00:00 2001 From: avonville Date: Thu, 19 Dec 2024 14:30:38 -0500 Subject: [PATCH 1/2] updated api to admin-ajax --- Generic_Plugin_Admin.php | 24 ++++++++++++++++++++++++ pub/js/options.js | 20 ++++++++++++-------- w3-total-cache-api.php | 1 + 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Generic_Plugin_Admin.php b/Generic_Plugin_Admin.php index 79b8cebcc..c5d542b90 100644 --- a/Generic_Plugin_Admin.php +++ b/Generic_Plugin_Admin.php @@ -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' ) ); @@ -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 ); + + wp_send_json( $posts, 200 ); + } + /** * Admin init (administrators only). */ diff --git a/pub/js/options.js b/pub/js/options.js index 7f9b26227..fbce28f8d 100644 --- a/pub/js/options.js +++ b/pub/js/options.js @@ -600,22 +600,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( "

No forum topics found.

" ); } else { // Create a list of topics - const $ul = jQuery( '' ); - jQuery.each( data, function( index, topic ) { + const $ul = jQuery( '' ); + const forumData = JSON.parse(data.body); + jQuery.each( forumData, function( index, topic ) { const $li = jQuery( '
  • ' ); const $link = jQuery( '' ).addClass('w3tc-control-after').attr( 'href', topic.link ).text( topic.title ).attr( 'target', '_blank' ); // Open in new tab const $icon = jQuery( '' ).addClass( 'dashicons dashicons-external' ); @@ -628,7 +631,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( "

    Error loading topics. Please try again later.

    " ); } }); diff --git a/w3-total-cache-api.php b/w3-total-cache-api.php index 61b3075ce..08cc36d63 100644 --- a/w3-total-cache-api.php +++ b/w3-total-cache-api.php @@ -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' ) ) { From aac63bf227e185009847696eb90072f9ff86541d Mon Sep 17 00:00:00 2001 From: Joe Cartonia Date: Fri, 20 Dec 2024 13:17:24 -0500 Subject: [PATCH 2/2] Increase timeout to 10 --- Generic_Plugin_Admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Generic_Plugin_Admin.php b/Generic_Plugin_Admin.php index c5d542b90..f9d6feaab 100644 --- a/Generic_Plugin_Admin.php +++ b/Generic_Plugin_Admin.php @@ -238,7 +238,7 @@ public function wp_ajax_w3tc_forums_api() { } $tag = Util_Request::get_string( 'tabId' ); - $posts = wp_remote_get( W3TC_BOLDGRID_FORUM_API . $tag ); + $posts = wp_remote_get( W3TC_BOLDGRID_FORUM_API . $tag, array( 'timeout' => 10 ) ); wp_send_json( $posts, 200 ); }