• File: page-context.php
  • Full Path: /home/slvgwrnz/public_html/wp-content/plugins/elementor/modules/audits/data/endpoints/page-context.php
  • Date Modified: 11/05/2026 9:17 AM
  • File size: 7.1 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace Elementor\Modules\Audits\Data\Endpoints;

use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
use Elementor\Core\Utils\Hints;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Page_Context extends Endpoint_Base {

	public function get_name(): string {
		return 'page-context';
	}

	public function get_format(): string {
		return 'audits/page-context';
	}

	public function get_items( $request ) {
		$document_id = (int) $request->get_param( 'document_id' );
		$image_size_requests = array_map( 'strval', (array) $request->get_param( 'image_size_requests' ) );

		$post = get_post( $document_id );
		$thumbnail_id = $post ? (int) get_post_thumbnail_id( $post ) : 0;
		$privacy_policy_url = get_privacy_policy_url();

		return [
			'site_identity' => [
				'site_name_set' => $this->is_site_name_set(),
				'site_description_set' => $this->is_site_description_set(),
				'site_logo_set' => has_custom_logo(),
				'site_favicon_set' => $this->is_site_favicon_set(),
			],
			'kit_id' => (int) get_option( 'elementor_active_kit' ),
			'kit_is_default_unchanged' => $this->is_default_kit_unchanged(),

			'post_title' => $post && '' !== $post->post_title ? $post->post_title : null,
			'post_excerpt' => $post && '' !== $post->post_excerpt ? $post->post_excerpt : null,
			'featured_image_id' => $thumbnail_id > 0 ? $thumbnail_id : null,

			'image_sizes' => $this->collect_image_sizes( $image_size_requests ),

			'is_noindex' => ! (bool) get_option( 'blog_public' ),
			'reading_settings_url' => admin_url( 'options-reading.php' ),

			'privacy_policy_url' => $privacy_policy_url ? $privacy_policy_url : null,
			'privacy_settings_url' => admin_url( 'options-privacy.php' ),
			'ally_plugin_active' => Hints::is_plugin_active( 'pojo-accessibility/pojo-accessibility.php' ),
			'ally_plugin_url' => Hints::get_plugin_action_url( 'pojo-accessibility' ),
			'ally_accessibility_statement_created' => $this->is_ally_accessibility_statement_created(),
			'ally_accessibility_statement_url' => admin_url( 'admin.php?page=accessibility-settings#accessibilityStatement' ),
			'ally_widget_settings_url' => admin_url( 'admin.php?page=accessibility-settings#capabilities' ),
			'ally_scan_url' => admin_url( 'admin.php?page=accessibility-settings#scans' ),
			'cookiez_plugin_active' => Hints::is_plugin_active( 'cookiez/cookiez.php' ),
			'cookiez_plugin_url' => admin_url( 'plugin-install.php?tab=plugin-information&plugin=cookiez' ),
			'cookiez_plugin_installed' => Hints::is_plugin_installed( 'cookiez/cookiez.php' ),
			'cookiez_plugin_action_url' => Hints::get_plugin_action_url( 'cookiez' ),
			'cookiez_scan_url' => admin_url( 'admin.php?page=cookiez-settings#cookie-management' ),
			'cookiez_consent_mode_settings_url' => admin_url( 'admin.php?page=cookiez-settings#settings' ),
			'image_optimization_plugin_active' => Hints::is_plugin_active( 'image-optimization/image-optimization.php' ),
			'image_optimization_plugin_url' => Hints::get_plugin_action_url( 'image-optimization' ),
			'image_optimization_settings_url' => admin_url( 'admin.php?page=image-optimization-settings' ),

			'frontend_url' => $this->get_published_frontend_url( $post ),
		];
	}

	private function get_published_frontend_url( $post ): ?string {
		if ( ! $post || 'publish' !== $post->post_status ) {
			return null;
		}

		$permalink = get_permalink( $post );

		return $permalink ? $permalink : null;
	}

	protected function register() {
		parent::register();
		$this->register_items_route();
	}

	private function collect_image_sizes( array $size_requests ): array {
		$result = [];

		foreach ( $size_requests as $request_key ) {
			[ $attachment_id, $size ] = $this->parse_size_request_key( $request_key );

			if ( ! $attachment_id || ! current_user_can( 'read_post', $attachment_id ) ) {
				continue;
			}

			if ( ! wp_attachment_is_image( $attachment_id ) ) {
				continue;
			}

			$result[ $request_key ] = $this->resolve_image_size_info( $attachment_id, $size );
		}

		return $result;
	}

	private function is_ally_accessibility_statement_created(): bool {
		$statement_data = get_option( 'ea11y_accessibility_statement_data' );

		return ! empty( $statement_data['pageId'] );
	}

	private function parse_size_request_key( string $request_key ): array {
		[ $attachment_id, $size ] = array_pad( explode( ':', $request_key, 2 ), 2, '' );

		return [ (int) $attachment_id, $size ? $size : 'full' ];
	}

	private function resolve_image_size_info( int $attachment_id, string $size ): array {
		$metadata = wp_get_attachment_metadata( $attachment_id );

		$base = [
			'width' => isset( $metadata['width'] ) ? (int) $metadata['width'] : 0,
			'height' => isset( $metadata['height'] ) ? (int) $metadata['height'] : 0,
			'mime' => (string) get_post_mime_type( $attachment_id ),
			'src' => (string) wp_get_attachment_url( $attachment_id ),
			'alt' => (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
		];

		$size_meta = 'full' !== $size ? ( $metadata['sizes'][ $size ] ?? null ) : null;

		if ( ! $size_meta ) {
			return array_merge( $base, [ 'filesize_bytes' => $this->get_original_filesize( $attachment_id ) ] );
		}

		if ( isset( $size_meta['filesize'] ) ) {
			return array_merge( $base, [
				'width' => (int) $size_meta['width'],
				'height' => (int) $size_meta['height'],
				'filesize_bytes' => (int) $size_meta['filesize'],
			] );
		}

		$sized_path = trailingslashit( dirname( get_attached_file( $attachment_id ) ) ) . $size_meta['file'];
		$sized_filesize = file_exists( $sized_path ) ? (int) filesize( $sized_path ) : $this->get_original_filesize( $attachment_id );

		return array_merge( $base, [
			'width' => (int) $size_meta['width'],
			'height' => (int) $size_meta['height'],
			'filesize_bytes' => $sized_filesize,
		] );
	}

	private function get_original_filesize( int $attachment_id ): int {
		$file_path = get_attached_file( $attachment_id );

		return $file_path && file_exists( $file_path ) ? (int) filesize( $file_path ) : 0;
	}

	private function is_default_kit_unchanged(): bool {
		$kit_id = (int) get_option( 'elementor_active_kit' );

		if ( ! $kit_id ) {
			return false;
		}

		$kit_post = get_post( $kit_id );

		if ( ! $kit_post ) {
			return false;
		}

		return $kit_post->post_date_gmt === $kit_post->post_modified_gmt;
	}

	private function is_site_name_set(): bool {
		return $this->is_non_default_string(
			(string) get_option( 'blogname' ),
			[ 'WordPress' ]
		);
	}

	private function is_site_description_set(): bool {
		return $this->is_non_default_string(
			(string) get_option( 'blogdescription' ),
			[ 'Just another WordPress site' ]
		);
	}

	private function is_site_favicon_set(): bool {
		$site_icon_id = (int) get_option( 'site_icon' );

		if ( $site_icon_id <= 0 ) {
			return false;
		}

		return wp_attachment_is_image( $site_icon_id );
	}

	private function is_non_default_string( string $value, array $blocked ): bool {
		$normalized = strtolower( trim( $value ) );

		if ( '' === $normalized ) {
			return false;
		}

		$blocked_normalized = array_map(
			static function ( $blocked_value ) {
				return strtolower( trim( (string) $blocked_value ) );
			},
			$blocked
		);

		return ! in_array( $normalized, $blocked_normalized, true );
	}
}