HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //proc/self/cwd/wp-content/plugins/woocommerce-zapier/src/API/Controller/PingController.php
<?php

namespace OM4\WooCommerceZapier\API\Controller;

use OM4\WooCommerceZapier\API\API;
use OM4\WooCommerceZapier\Logger;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
 * REST API controller class that gives WooCommerce Zapier app a performant way of
 * ensuring authentication credentials are still valid.
 *
 * @since 2.0.0
 */
class PingController extends WC_REST_Controller {

	/**
	 * Logger instance.
	 *
	 * @var Logger
	 */
	protected $logger;

	/**
	 * Constructor.
	 *
	 * @param Logger $logger Logger instance.
	 */
	public function __construct( Logger $logger ) {
		$this->logger = $logger;
	}

	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'ping';

	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	protected $namespace = API::REST_NAMESPACE;

	/**
	 * Registers the routes for the objects of the controller.
	 *
	 * @return void
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				'args' => array(),
				array(
					'methods'             => 'GET',
					'callback'            => array( $this, 'get_items' ),
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
				),
			)
		);
	}

	/**
	 * Check whether a given request has permission to ping.
	 *
	 * @param  WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_Error|boolean
	 */
	public function get_items_permissions_check( $request ) {
		// WooCommerce's REST API does not perform Basic Authentication if is_ssl() is false: https://github.com/woocommerce/woocommerce/blob/4.0.1/includes/class-wc-rest-authentication.php#L81.
		if ( ! is_ssl() ) {
			$this->error_log_request( $request );

			$this->logger->critical( 'WooCommerce REST API Basic Authentication was not performed during ping because is_ssl() returned false.' );

			return new WP_Error(
				'wc_zapier_not_implemented',
				__( 'Sorry, SSL is not configured correctly.', 'woocommerce-zapier' ),
				array( 'status' => 501 )
			);
		}

		if ( ! wc_rest_check_manager_permissions( 'webhooks', 'read' ) ) {
			$this->error_log_request( $request );

			if ( is_user_logged_in() ) {
				$user = wp_get_current_user();
				$this->logger->critical( 'Authentication attempt failed for user: %s. Insufficient user permissions.', $user->user_login );
				return new WP_Error(
					'wc_zapier_rest_cannot_authorize',
					__( 'This user does not have the correct permissions.', 'woocommerce-zapier' ),
					array( 'status' => 403 )
				);
			}

			$this->logger->critical( 'Authentication attempt failed. could not authenticate user.' );
			return new WP_Error(
				'wc_zapier_rest_cannot_view',
				__( 'Sorry, you cannot list resources.', 'woocommerce-zapier' ),
				array( 'status' => rest_authorization_required_code() )
			);
		}

		return true;
	}

	/**
	 * (Fast) ping response.
	 *
	 * @param WP_REST_Request $request The incoming request.
	 *
	 * @return WP_Error|WP_REST_Response
	 */
	public function get_items( $request ) {
		return rest_ensure_response( array() );
	}

	/**
	 * Log request and environment data as error.
	 *
	 * @param WP_REST_Request $request The incoming request.
	 *
	 * @return void
	 */
	protected function error_log_request( $request ) {
		$this->logger->error( 'Ping Endpoint Error.' );
		$this->logger->debug(
			'Current Filter %s. Route: %s. Query Params: %s. Request URI: %s.',
			array(
				current_filter(),
				$request->get_route(),
				wp_json_encode( $request->get_params() ),
				isset( $_SERVER['REQUEST_URI'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '',
			)
		);
	}
}