← Back
Editing: class-main-handler.php
<?php namespace GD_cORe\handlers; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * The admin-specific functionality of the plugin. * * Defines the plugin name, version, and two examples hooks for how to * enqueue the admin-specific stylesheet and JavaScript. * * @package GD_cORe * @subpackage GD_cORe/admin */ class Main { const WORDPRESS_URL = "https://downloads.wordpress.org/plugin/"; const WORDPRESS_API = "https://api.wordpress.org/plugins/info/1.0/"; const PLUGIN_MAIN = "admin.php?page=gd-core"; const PLUGIN_PAGE = "admin.php?page=gd-core-plugins"; const PLUGIN_THEME = "admin.php?page=gd-core-themes"; const PLUGIN_AUDITS = "admin.php?page=gd-core-audits"; public function __construct(){ } public function getAllowedEfforts(){ return array('V3', 'all', 'V2'); } /** * Makes an HTTP request to the specified endpoint using WordPress HTTP API * * @param string $endpoint The API endpoint to call * @param string $type The HTTP request method (GET, POST, DELETE) * @param mixed $data Optional data to send with the request * @param bool $external Whether this is an external request (WordPress.org) * @param string $slug Optional plugin slug for WordPress.org requests * @param string $version Optional plugin version for WordPress.org requests * @param string $url Optional complete URL to override endpoint construction * @return mixed Response data or WP_Error on failure * @since 4.0.0 **/ public function gdcore_http_request($endpoint, $type = 'GET', $data = null, $external = false, $slug = null, $version = null, $url = null) { $response = $this->gdcore_http_response($endpoint, $type, $data, $external, $slug, $version, $url); // Check for errors if (is_wp_error($response)) { return $response; // Return the WP_Error object } // Do not treat proxy error envelopes as successful settings or scans. if (!$external && !$url && \GD_cORe_Site_Auth::is_proxy_route($type, $endpoint)) { $status = (int) wp_remote_retrieve_response_code($response); if ($status < 200 || $status >= 300) { return new \WP_Error('proxy_request_failed', 'Site service is unavailable.'); } } return wp_remote_retrieve_body($response); } /** * Same as gdcore_http_request(), but returns the full WordPress HTTP * response so callers can check the status code. * * The settings GET, Lighthouse report POST and Lighthouse report reads * always go through the signed Public API proxy and fail locally if the * site cannot sign. Every other request (plugins, themes, external, custom * URL) is direct and unsigned. * * @return array|\WP_Error * @since 4.2.0 **/ public function gdcore_http_response($endpoint, $type = 'GET', $data = null, $external = false, $slug = null, $version = null, $url = null) { $proxy = !$url && !$external && \GD_cORe_Site_Auth::is_proxy_route($type, $endpoint); if ($proxy) { $operation = \GD_cORe_Site_Auth::proxy_operation($type, $endpoint, $data ?? ''); if (is_wp_error($operation)) { return $operation; } $url = $operation['url']; } // Construct the URL if not provided if (!$url) { if ($external) { if ($slug) { if ($version == 0) { $url = self::WORDPRESS_URL . "$slug.zip"; } elseif ($version > 0) { $url = self::WORDPRESS_URL . "$slug.$version.zip"; } } else { return new \WP_Error('missing_slug', 'Plugin slug is required for external requests'); } } else { $base_url = GD_CORE_API_BASE; $url = $base_url . $endpoint; } } // Set up request arguments $args = array( 'timeout' => 30, // Set an appropriate timeout 'sslverify' => true, // Enable SSL verification for security ); if ($proxy) { $args['redirection'] = 0; } $args['headers'] = array( 'x-app-key' => 'wordpress-site', 'User-Agent' => 'GDCorePlugin/' . GD_cORe_VERSION, ); if ($proxy) { $args['headers'] = array_merge($args['headers'], $operation['headers']); } // Handle different request types switch ($type) { case 'POST': $args['method'] = 'POST'; if ($data) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = $data; } $response = wp_remote_post($url, $args); break; case 'DELETE': $args['method'] = 'DELETE'; $response = wp_remote_request($url, $args); break; case 'GET': default: $response = wp_remote_get($url, $args); } // Never propagate transport diagnostics (which may contain request details). return $proxy && is_wp_error($response) ? new \WP_Error('proxy_request_failed', 'Site service is unavailable.') : $response; } }
Save File
Cancel