\d+)’, [
‘methods’ => ‘POST’,
‘callback’ => ‘i9se_apply_elementor_model_by_library_id’,
‘permission_callback’ => function ( WP_REST_Request $request ) {
$post_id = (int) $request[‘id’];
return current_user_can( ‘edit_post’, $post_id );
},
] );
} );
function i9se_apply_elementor_model_by_library_id( WP_REST_Request $request ) {
$post_id = (int) $request[‘id’];
if ( ! get_post( $post_id ) ) {
return new WP_Error( ‘i9se_post_not_found’, ‘Post/página não encontrado.’, [ ‘status’ => 404 ] );
}
$template_json = (string) $request->get_param( ‘template_json’ );
$titulo = sanitize_text_field( (string) $request->get_param( ‘titulo’ ) );
$container_title = sanitize_text_field( (string) $request->get_param( ‘container_title’ ) );
$corpo_html = wp_kses_post( (string) $request->get_param( ‘corpo_html’ ) );
$imagem_1_id = absint( $request->get_param( ‘imagem_1_id’ ) );
$imagem_2_id = absint( $request->get_param( ‘imagem_2_id’ ) );
$template = json_decode( $template_json, true );
if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $template ) ) {
return new WP_Error( ‘i9se_invalid_json’, ‘template_json inválido.’, [ ‘status’ => 400 ] );
}
if (
empty( $template[‘content’][0][‘elements’][0][‘elements’][0][‘settings’] ) ||
empty( $template[‘content’][0][‘elements’][0][‘elements’][1][‘settings’] ) ||
empty( $template[‘content’][0][‘elements’][1][‘elements’][0][‘settings’] ) ||
empty( $template[‘content’][0][‘elements’][2][‘elements’][0][‘settings’] )
) {
return new WP_Error( ‘i9se_invalid_template_structure’, ‘Estrutura do template não corresponde ao modelo esperado.’, [ ‘status’ => 400 ] );
}
$img1 = i9se_build_elementor_library_image( $imagem_1_id );
$img2 = i9se_build_elementor_library_image( $imagem_2_id );
if ( empty( $img2[‘url’] ) && ! empty( $img1[‘url’] ) ) {
$img2 = $img1;
}
$template[‘title’] = $titulo;
if ( ! empty( $container_title ) ) {
$template[‘content’][0][‘settings’][‘_title’] = $container_title;
}
$template[‘content’][0][‘elements’][0][‘elements’][0][‘settings’][‘title’] = $titulo;
$template[‘content’][0][‘elements’][0][‘elements’][1][‘settings’][‘editor’] = $corpo_html;
if ( ! empty( $img1[‘url’] ) ) {
$template[‘content’][0][‘elements’][1][‘elements’][0][‘settings’][‘image’] = $img1;
}
if ( ! empty( $img2[‘url’] ) ) {
$template[‘content’][0][‘elements’][2][‘elements’][0][‘settings’][‘image’] = $img2;
}
$editor_data = isset( $template[‘content’] ) ? $template[‘content’] : [];
$page_settings = isset( $template[‘page_settings’] ) ? $template[‘page_settings’] : [];
wp_update_post( [
‘ID’ => $post_id,
‘post_title’ => $titulo,
] );
update_post_meta(
$post_id,
‘_elementor_data’,
wp_slash( wp_json_encode( $editor_data, JSON_UNESCAPED_UNICODE ) )
);
update_post_meta(
$post_id,
‘_elementor_page_settings’,
$page_settings
);
update_post_meta( $post_id, ‘_elementor_edit_mode’, ‘builder’ );
if ( defined( ‘ELEMENTOR_VERSION’ ) ) {
update_post_meta( $post_id, ‘_elementor_version’, ELEMENTOR_VERSION );
}
return rest_ensure_response( [
‘success’ => true,
‘post_id’ => $post_id,
‘title’ => $titulo,
‘image_1’ => $img1,
‘image_2’ => $img2,
] );
}
function i9se_build_elementor_library_image( $attachment_id ) {
$attachment_id = absint( $attachment_id );
if ( ! $attachment_id ) {
return [
‘url’ => ”,
‘id’ => ”,
‘size’ => ‘full’,
‘alt’ => ”,
‘source’ => ‘library’,
];
}
$url = wp_get_attachment_url( $attachment_id );
if ( ! $url ) {
return [
‘url’ => ”,
‘id’ => ”,
‘size’ => ‘full’,
‘alt’ => ”,
‘source’ => ‘library’,
];
}
$alt = get_post_meta( $attachment_id, ‘_wp_attachment_image_alt’, true );
if ( ! $alt ) {
$alt = get_the_title( $attachment_id );
}
return [
‘url’ => esc_url_raw( $url ),
‘id’ => $attachment_id,
‘size’ => ‘full’,
‘alt’ => sanitize_text_field( $alt ),
‘source’ => ‘library’,
];
}
