| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Audits\Data\Endpoints; |
| 4 |
|
| 5 |
use Elementor\Control_Media; |
| 6 |
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base; |
| 7 |
use Elementor\Core\Utils\Hints; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Page_Context extends Endpoint_Base { |
| 14 |
|
| 15 |
public function get_name(): string { |
| 16 |
return 'page-context'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_format(): string { |
| 20 |
return 'audits/page-context'; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_items( $request ) { |
| 24 |
$document_id = (int) $request->get_param( 'document_id' ); |
| 25 |
$attachment_ids = array_map( 'intval', (array) $request->get_param( 'attachment_ids' ) ); |
| 26 |
|
| 27 |
$post = get_post( $document_id ); |
| 28 |
$thumbnail_id = $post ? (int) get_post_thumbnail_id( $post ) : 0; |
| 29 |
$privacy_policy_url = get_privacy_policy_url(); |
| 30 |
|
| 31 |
return [ |
| 32 |
'site_identity' => [ |
| 33 |
'site_name_set' => $this->is_site_name_set(), |
| 34 |
'site_description_set' => $this->is_site_description_set(), |
| 35 |
'site_logo_set' => has_custom_logo(), |
| 36 |
'site_favicon_set' => $this->is_site_favicon_set(), |
| 37 |
], |
| 38 |
'kit_id' => (int) get_option( 'elementor_active_kit' ), |
| 39 |
'kit_is_default_unchanged' => $this->is_default_kit_unchanged(), |
| 40 |
|
| 41 |
'post_title' => $post && '' !== $post->post_title ? $post->post_title : null, |
| 42 |
'post_excerpt' => $post && '' !== $post->post_excerpt ? $post->post_excerpt : null, |
| 43 |
'featured_image_id' => $thumbnail_id > 0 ? $thumbnail_id : null, |
| 44 |
|
| 45 |
'image_sizes' => $this->collect_image_sizes( $attachment_ids ), |
| 46 |
|
| 47 |
'is_noindex' => ! (bool) get_option( 'blog_public' ), |
| 48 |
'reading_settings_url' => admin_url( 'options-reading.php' ), |
| 49 |
|
| 50 |
'privacy_policy_url' => $privacy_policy_url ? $privacy_policy_url : null, |
| 51 |
'privacy_settings_url' => admin_url( 'options-privacy.php' ), |
| 52 |
'ally_plugin_active' => Hints::is_plugin_active( 'pojo-accessibility/pojo-accessibility.php' ), |
| 53 |
'ally_plugin_url' => admin_url( 'plugin-install.php?tab=plugin-information&plugin=pojo-accessibility' ), |
| 54 |
'cookiez_plugin_active' => Hints::is_plugin_active( 'cookiez/cookiez.php' ), |
| 55 |
'cookiez_plugin_url' => admin_url( 'plugin-install.php?tab=plugin-information&plugin=cookiez' ), |
| 56 |
'image_optimization_plugin_active' => Hints::is_plugin_active( 'image-optimization/image-optimization.php' ), |
| 57 |
'image_optimization_plugin_url' => Hints::get_plugin_action_url( 'image-optimization' ), |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
protected function register() { |
| 62 |
parent::register(); |
| 63 |
$this->register_items_route(); |
| 64 |
} |
| 65 |
|
| 66 |
private function collect_image_sizes( array $attachment_ids ): array { |
| 67 |
$result = []; |
| 68 |
|
| 69 |
foreach ( $attachment_ids as $attachment_id ) { |
| 70 |
if ( ! current_user_can( 'read_post', $attachment_id ) ) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 79 |
$file_path = get_attached_file( $attachment_id ); |
| 80 |
$mime = get_post_mime_type( $attachment_id ); |
| 81 |
$src = wp_get_attachment_url( $attachment_id ); |
| 82 |
|
| 83 |
$result[ $attachment_id ] = [ |
| 84 |
'width' => isset( $metadata['width'] ) ? (int) $metadata['width'] : 0, |
| 85 |
'height' => isset( $metadata['height'] ) ? (int) $metadata['height'] : 0, |
| 86 |
'filesize_bytes' => $file_path && file_exists( $file_path ) ? (int) filesize( $file_path ) : 0, |
| 87 |
'mime' => $mime ? $mime : '', |
| 88 |
'src' => $src ? $src : '', |
| 89 |
'alt' => Control_Media::get_image_alt( [ 'id' => $attachment_id ] ), |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
return $result; |
| 94 |
} |
| 95 |
|
| 96 |
private function is_default_kit_unchanged(): bool { |
| 97 |
$kit_id = (int) get_option( 'elementor_active_kit' ); |
| 98 |
|
| 99 |
if ( ! $kit_id ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
$kit_post = get_post( $kit_id ); |
| 104 |
|
| 105 |
if ( ! $kit_post ) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
return $kit_post->post_date_gmt === $kit_post->post_modified_gmt; |
| 110 |
} |
| 111 |
|
| 112 |
private function is_site_name_set(): bool { |
| 113 |
return $this->is_non_default_string( |
| 114 |
(string) get_option( 'blogname' ), |
| 115 |
[ 'WordPress' ] |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
private function is_site_description_set(): bool { |
| 120 |
return $this->is_non_default_string( |
| 121 |
(string) get_option( 'blogdescription' ), |
| 122 |
[ 'Just another WordPress site' ] |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
private function is_site_favicon_set(): bool { |
| 127 |
$site_icon_id = (int) get_option( 'site_icon' ); |
| 128 |
|
| 129 |
if ( $site_icon_id <= 0 ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
return wp_attachment_is_image( $site_icon_id ); |
| 134 |
} |
| 135 |
|
| 136 |
private function is_non_default_string( string $value, array $blocked ): bool { |
| 137 |
$normalized = strtolower( trim( $value ) ); |
| 138 |
|
| 139 |
if ( '' === $normalized ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
$blocked_normalized = array_map( |
| 144 |
static function ( $blocked_value ) { |
| 145 |
return strtolower( trim( (string) $blocked_value ) ); |
| 146 |
}, |
| 147 |
$blocked |
| 148 |
); |
| 149 |
|
| 150 |
return ! in_array( $normalized, $blocked_normalized, true ); |
| 151 |
} |
| 152 |
} |
| 153 |
|