wp_create_nonce( 'advaimma_save' ),
'strings' => array(
// translators: Button text to add a new hotspot point.
'addPoint' => esc_html__( 'Add Point', 'advance-image-mapping' ),
// translators: Button text to remove a hotspot point.
'remove' => esc_html__( 'Remove', 'advance-image-mapping' ),
),
)
);
}
/**
* Register CPT meta boxes.
*/
public function register_meta_boxes() {
add_meta_box(
'advaimma_meta',
esc_html__( 'Hotspot Builder', 'advance-image-mapping' ),
array( $this, 'meta_box_callback' ),
'advaimma_image_map',
'normal',
'high'
);
}
/**
* Meta box content.
*/
public function meta_box_callback( $post ) {
$image_id = get_post_meta( $post->ID, 'advaimma_image_id', true );
$spots = get_post_meta( $post->ID, 'advaimma_spots', true );
if ( ! is_array( $spots ) ) {
$spots = array();
}
wp_nonce_field( 'advaimma_meta_nonce', 'advaimma_meta_nonce' );
$image_url = $image_id ? wp_get_attachment_image_url( intval( $image_id ), 'full' ) : '';
?>
$s ) :
$x = isset( $s['x'] ) ? floatval( $s['x'] ) : 50;
$y = isset( $s['y'] ) ? floatval( $s['y'] ) : 50;
echo '
';
endforeach;
endif;
?>
post_type ) || 'advaimma_image_map' !== $post->post_type ) {
return;
}
// Verify nonce
if ( empty( $_POST['advaimma_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['advaimma_meta_nonce'] ) ), 'advaimma_meta_nonce' ) ) {
return;
}
// Avoid autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Permission check
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Save image ID
if ( isset( $_POST['advaimma_image_id'] ) ) {
$image_id = intval( wp_unslash( $_POST['advaimma_image_id'] ) );
if ( $image_id > 0 ) {
update_post_meta( $post_id, 'advaimma_image_id', $image_id );
} else {
delete_post_meta( $post_id, 'advaimma_image_id' );
}
}
// Save spots safely
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$raw_spots = isset( $_POST['advaimma_spots'] ) && is_array( $_POST['advaimma_spots'] ) ? wp_unslash( $_POST['advaimma_spots'] ) : array();
$spots = array();
$allowed_triggers = array( 'hover', 'click' );
ksort( $raw_spots );
foreach ( $raw_spots as $key => $s ) {
if ( ! is_array( $s ) ) {
continue;
}
$title = isset( $s['title'] ) ? sanitize_text_field( $s['title'] ) : '';
$content = isset( $s['content'] ) ? wp_kses_post( $s['content'] ) : '';
$icon = isset( $s['icon'] ) ? sanitize_text_field( $s['icon'] ) : '';
$icon_image = isset( $s['icon_image'] ) ? esc_url_raw( $s['icon_image'] ) : '';
$trigger = ( isset( $s['trigger'] ) && in_array( $s['trigger'], $allowed_triggers, true ) ) ? $s['trigger'] : 'hover';
$x = isset( $s['x'] ) ? floatval( $s['x'] ) : 50;
$y = isset( $s['y'] ) ? floatval( $s['y'] ) : 50;
if ( empty( $title ) && empty( $content ) && empty( $icon ) && empty( $icon_image ) && ! isset( $s['x'] ) && ! isset( $s['y'] ) ) {
continue;
}
$spots[] = array(
'title' => $title,
'content' => $content,
'icon' => $icon,
'icon_image' => $icon_image,
'trigger' => $trigger,
'x' => round( min( 100, max( 0, $x ) ), 4 ),
'y' => round( min( 100, max( 0, $y ) ), 4 ),
);
}
update_post_meta( $post_id, 'advaimma_spots', $spots );
}
}