<?php
require_once 'wp-load.php';

$author = get_user_by( 'login', 'author' );
$author_id = $author ? $author->ID : 1;

$PAYLOAD = getenv('VARIANT_PAYLOAD');

$data = array(
    array(
        'id'       => 'section_' . uniqid(),
        'elType'   => 'section',
        'settings' => array(),
        'elements' => array(
            array(
                'id'       => 'column_' . uniqid(),
                'elType'   => 'column',
                'settings' => array(),
                'elements' => array(
                    array(
                        'id'         => 'widget_' . uniqid(),
                        'elType'     => 'widget',
                        'widgetType' => 'jw-pricing-table',
                        'settings'   => array(
                            'title'          => 'Variant Plan',
                            'price'          => '9',
                            'price_prefix'   => '$',
                            'price_suffix'   => '/mo',
                            'features_list'  => array(
                                array(
                                    'item_text'     => $PAYLOAD,
                                    'item_included' => 'item-included',
                                ),
                                array(
                                    'item_text'     => 'Normal Feature',
                                    'item_included' => 'item-included',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
);

$page = array(
    'post_title'   => 'Pricing Table Variant Repro',
    'post_status'  => 'publish',
    'post_type'    => 'page',
    'post_author'  => $author_id,
);

$post_id = wp_insert_post( $page );
if ( is_wp_error( $post_id ) ) {
    echo 'INSERT_ERROR=' . $post_id->get_error_message() . "\n";
    exit( 1 );
}

update_post_meta( $post_id, '_elementor_edit_mode', 'builder' );
update_post_meta( $post_id, '_elementor_template_type', 'wp-page' );
update_post_meta( $post_id, '_elementor_data', wp_slash( wp_json_encode( $data ) ) );

echo 'POST_ID=' . $post_id . "\n";
echo 'URL=' . get_permalink( $post_id ) . "\n";
