classification = $classification;
$this->image_renderer = $image_renderer;
}
public function register_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_filter( 'the_content', array( $this, 'filter_content' ), 20 );
add_filter( 'render_block_core/image', array( $this, 'filter_image_block' ), 20, 2 );
add_filter( 'wp_get_attachment_image', array( $this, 'filter_attachment_image' ), 20, 5 );
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_image_attributes' ), 20, 2 );
add_filter( 'wp_content_img_tag', array( $this, 'filter_content_image_tag' ), 20, 3 );
}
public static function get_settings() {
return wp_parse_args(
get_option( 'aiparently_settings', array() ),
array(
'enabled' => 1,
'position' => 'after',
'style' => 'badge',
'images_enabled' => 1,
'image_label_position' => 'right',
)
);
}
public function enqueue_styles() {
if ( ! is_admin() ) {
wp_enqueue_style( 'aiparently', AIPARENTLY_URL . 'assets/css/frontend.css', array(), AIPARENTLY_VERSION );
wp_enqueue_script( 'aiparently-frontend', AIPARENTLY_URL . 'assets/js/frontend.js', array(), AIPARENTLY_VERSION, true );
}
}
private function markup( array $decision, $context = 'content' ) {
if ( empty( $decision['required'] ) ) {
return '';
}
$settings = self::get_settings();
$classes = 'aiparently-disclosure aiparently-disclosure--' . sanitize_html_class( $settings['style'] );
$classes .= ' aiparently-disclosure--' . sanitize_html_class( $context );
$badge = sprintf(
'%4$s',
esc_attr( $classes ),
esc_attr( $decision['code'] ),
esc_attr( $decision['ruleset_version'] ),
esc_html( $decision['label'] )
);
if ( 'content' !== $context ) {
return $badge;
}
return '
' . $badge . '
';
}
public function filter_content( $content ) {
$settings = self::get_settings();
if ( empty( $settings['enabled'] ) || is_admin() || ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$post_id = get_the_ID();
if ( ! in_array( get_post_type( $post_id ), array( 'post', 'page', 'attachment' ), true ) ) {
return $content;
}
$markup = $this->markup( $this->classification->get_decision( $post_id ) );
if ( ! $markup ) {
return $content;
}
return 'before' === $settings['position'] ? $markup . $content : $content . $markup;
}
public function filter_image_block( $block_content, $block ) {
$post_id = isset( $block['attrs']['id'] ) ? absint( $block['attrs']['id'] ) : 0;
if ( ! $post_id ) {
return $block_content;
}
return $this->image_renderer->render_first_image( $post_id, $block_content );
}
public function filter_attachment_image( $html, $attachment_id, $size, $icon, $attr ) {
if ( is_admin() ) {
return $html;
}
return $this->image_renderer->render( $attachment_id, $html );
}
/**
* Handles raw images in classic/page-builder content through WordPress'
* per-image content filter. This is intentionally not a whole-page scan.
*/
public function filter_content_image_tag( $filtered_image, $context, $attachment_id ) {
if ( is_admin() || false !== strpos( $filtered_image, 'data-aiparently-processed' ) ) {
return $filtered_image;
}
$attachment_id = absint( $attachment_id );
if ( ! $attachment_id ) {
$attachment_id = $this->image_renderer->resolve_attachment_id( $filtered_image );
}
return $attachment_id
? $this->image_renderer->render( $attachment_id, $filtered_image )
: $filtered_image;
}
public function add_image_attributes( $attr, $attachment ) {
$decision = $this->classification->get_decision( $attachment->ID );
if ( ! empty( $decision['required'] ) ) {
$attr['data-aiparently-code'] = $decision['code'];
$attr['data-aiparently-ruleset'] = $decision['ruleset_version'];
}
return $attr;
}
}