__( 'Elementor', 'bongoto-woocommerce' ),
'woocommerce/woocommerce.php' => __( 'WooCommerce', 'bongoto-woocommerce' ),
// 'bongoto-seller/bongoto-seller.php' => __( 'Bongoto Seller', 'bongoto-woocommerce' ),
'bongoto-wc-addon/bongoto-wc-addon.php' => __( 'Bongoto WC Add-On', 'bongoto-woocommerce' ),
);
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$all = function_exists( 'get_plugins' ) ? get_plugins() : array();
foreach ( $expect as $file => $label ) {
// Not installed.
if ( ! isset( $all[ $file ] ) ) {
$missing[] = $label;
continue;
}
// Installed but inactive.
if ( ! bongoto_woocommerce_is_plugin_active( $file ) ) {
$missing[] = $label;
}
}
if ( ! empty( $missing ) ) {
echo '
';
echo esc_html__( 'Some recommended plugins are missing or inactive:', 'bongoto-woocommerce' ) . ' ';
echo '' . esc_html( implode( ', ', $missing ) ) . '. ';
echo esc_html__( 'Open the setup page to install or activate them.', 'bongoto-woocommerce' ) . ' ';
echo '' . esc_html__( 'Open Setup', 'bongoto-woocommerce' ) . '';
echo '
';
}
// Front page not set → show actionable notice.
$needs_front = ( 'page' !== get_option( 'show_on_front' ) ) || ! (int) get_option( 'page_on_front' );
if ( $needs_front ) {
echo '';
}
// One-time “Quick Setup” info after activation.
if ( get_option( BONGOTO_WC_SETUP_OPTION_DONE ) || get_option( BONGOTO_WC_SETUP_OPTION_DISMISS ) ) {
delete_transient( BONGOTO_WC_SETUP_TRANSIENT_SHOW );
return;
}
if ( get_transient( BONGOTO_WC_SETUP_TRANSIENT_SHOW ) ) {
$dismiss_url = wp_nonce_url( add_query_arg( 'bongoto_wc_setup_dismiss', '1' ), 'bongoto_wc_setup_dismiss' );
echo '';
}
}
);
/** Clear transient once done/dismissed. */
add_action(
'admin_init',
function () {
if ( get_option( BONGOTO_WC_SETUP_OPTION_DONE ) || get_option( BONGOTO_WC_SETUP_OPTION_DISMISS ) ) {
delete_transient( BONGOTO_WC_SETUP_TRANSIENT_SHOW );
}
}
);
/** Permanent dismiss handler. */
add_action(
'admin_init',
function () {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$nonce = isset( $_REQUEST['_wpnonce'] ) ? wp_unslash( $_REQUEST['_wpnonce'] ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['bongoto_wc_setup_dismiss'] ) && wp_verify_nonce( $nonce, 'bongoto_wc_setup_dismiss' ) ) {
delete_transient( BONGOTO_WC_SETUP_TRANSIENT_SHOW );
update_option( BONGOTO_WC_SETUP_OPTION_DISMISS, time() );
wp_safe_redirect( remove_query_arg( array( 'bongoto_wc_setup_dismiss', '_wpnonce' ) ) );
exit;
}
}
);
/* -------------------------------------------------------
* Register Setup page
* ----------------------------------------------------- */
add_action(
'admin_menu',
function () {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
add_theme_page(
__( 'Bongoto Setup', 'bongoto-woocommerce' ),
__( 'Bongoto Setup', 'bongoto-woocommerce' ),
'manage_options',
'bongoto-woocommerce-setup',
'bongoto_woocommerce_setup_render_page'
);
}
);
/* -------------------------------------------------------
* Helpers
* ----------------------------------------------------- */
/**
* Build action button row for a plugin.
*
* @param string $slug Plugin slug (wp.org).
* @param string $label Human label.
* @param bool $is_pro Is this a Pro addon?
* @param string $pro_url External URL (if Pro not from wp.org).
* @return string
*/
function bongoto_woocommerce_setup_plugin_row( string $slug, string $label, bool $is_pro = false, string $pro_url = '' ): string {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$installed = false;
$active = false;
$plugin_file = '';
$plugins = function_exists( 'get_plugins' ) ? get_plugins() : array();
foreach ( $plugins as $file => $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( 0 === strpos( $file, $slug . '/' ) ) {
$installed = true;
$plugin_file = $file;
$active = is_plugin_active( $file );
break;
}
}
$button_html = '';
if ( ! $installed ) {
if ( $is_pro && $pro_url ) {
$button_html = ''
. esc_html__( 'Get Pro', 'bongoto-woocommerce' ) . '';
} else {
$url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ), 'install-plugin_' . $slug );
$button_html = ''
. esc_html__( 'Install', 'bongoto-woocommerce' ) . '';
}
} elseif ( ! $active && $plugin_file ) {
$url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . $plugin_file ), 'activate-plugin_' . $plugin_file );
$button_html = ''
. esc_html__( 'Activate', 'bongoto-woocommerce' ) . '';
} else {
$button_html = ''
. esc_html__( 'Activated', 'bongoto-woocommerce' ) . '';
}
return ''
. '' . esc_html( $label ) . ''
. '' . $button_html . ''
. '';
}
/**
* Scan assets/elementor demos.
*
* @return array
*/
function bongoto_woocommerce_setup_list_demo_jsons(): array {
$dir = trailingslashit( BONGOTO_WC_DIR ) . 'assets/elementor';
$list = array();
if ( is_dir( $dir ) ) {
$files = glob( $dir . '/*.json' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_glob
if ( $files ) {
foreach ( $files as $abs ) {
$rel = str_replace( BONGOTO_WC_DIR . DIRECTORY_SEPARATOR, '', $abs );
$size = filesize( $abs ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
$list[] = array(
'abs' => $abs,
'rel' => $rel,
'size' => $size ? size_format( $size ) : '',
);
}
}
}
$default = $dir . '/digital-marketplace-01.json';
if ( file_exists( $default ) && ! array_filter(
$list,
function( $i ) {
return basename( $i['abs'] ) === 'digital-marketplace-01.json';
}
) ) {
$list[] = array(
'abs' => $default,
'rel' => 'assets/elementor/digital-marketplace-01.json',
'size' => size_format( filesize( $default ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
);
}
return $list;
}
/**
* Import JSON to a "Home" page and set as front page.
*
* @param string $abs_json Absolute path to JSON.
* @return string|WP_Error Page ID on success.
*/
function bongoto_woocommerce_setup_import_elementor_home( string $abs_json ) {
if ( ! file_exists( $abs_json ) ) {
return new WP_Error( 'missing_json', __( 'Selected JSON file not found.', 'bongoto-woocommerce' ) );
}
$json = file_get_contents( $abs_json ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( '' === trim( (string) $json ) ) {
return new WP_Error( 'empty_json', __( 'Selected JSON file is empty.', 'bongoto-woocommerce' ) );
}
$page = get_page_by_title( 'Home' );
if ( $page && 'page' === $page->post_type ) {
$page_id = (int) $page->ID;
} else {
$page_id = wp_insert_post(
array(
'post_title' => 'Home',
'post_type' => 'page',
'post_status' => 'publish',
'post_content'=> '',
)
);
if ( is_wp_error( $page_id ) ) {
return $page_id;
}
}
if ( class_exists( '\Elementor\Plugin' ) ) {
update_post_meta( $page_id, '_elementor_edit_mode', 'builder' );
update_post_meta( $page_id, '_elementor_template_type', 'wp-page' );
update_post_meta( $page_id, '_elementor_data', wp_slash( $json ) );
wp_update_post(
array(
'ID' => $page_id,
'post_content' => '',
)
);
} else {
$placeholder = ''
. esc_html__( 'Please install & activate Elementor to render this layout.', 'bongoto-woocommerce' )
. '
';
wp_update_post(
array(
'ID' => $page_id,
'post_content' => $placeholder,
)
);
}
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $page_id );
update_option( BONGOTO_WC_SETUP_OPTION_DONE, time() );
delete_transient( BONGOTO_WC_SETUP_TRANSIENT_SHOW );
return (string) $page_id;
}
/**
* Handle Import POST.
*/
function bongoto_woocommerce_setup_handle_import_post() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['bongoto_wc_import_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['bongoto_wc_import_nonce'] ), 'bongoto_wc_import' ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$rel = isset( $_POST['bongoto_demo_file'] ) ? sanitize_text_field( wp_unslash( $_POST['bongoto_demo_file'] ) ) : '';
if ( '' === $rel ) {
add_settings_error( 'bongoto_wc_setup', 'no_file', __( 'Please select a demo JSON file.', 'bongoto-woocommerce' ), 'error' );
return;
}
$abs = realpath( trailingslashit( BONGOTO_WC_DIR ) . ltrim( $rel, '/\\' ) );
$theme_root = realpath( BONGOTO_WC_DIR );
if ( ! $abs || 0 !== strpos( $abs, $theme_root ) ) {
add_settings_error( 'bongoto_wc_setup', 'bad_path', __( 'Invalid file path.', 'bongoto-woocommerce' ), 'error' );
return;
}
$res = bongoto_woocommerce_setup_import_elementor_home( $abs );
if ( is_wp_error( $res ) ) {
add_settings_error( 'bongoto_wc_setup', $res->get_error_code(), $res->get_error_message(), 'error' );
return;
}
$home_url = get_permalink( (int) $res );
// Redirect back to setup page with flags so JS can open the Home in a new tab
// and success notice with View Home button can be shown.
wp_safe_redirect(
add_query_arg(
array(
'page' => 'bongoto-woocommerce-setup',
'settings-updated' => 'true',
'bongoto_home_created' => '1',
'bongoto_home_url' => rawurlencode( $home_url ),
),
admin_url( 'themes.php' )
)
);
exit;
}
add_action( 'admin_init', 'bongoto_woocommerce_setup_handle_import_post' );
/**
* Render Setup page.
*/
function bongoto_woocommerce_setup_render_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Success message after import with "View Home" button.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['bongoto_home_created'] ) && '1' === $_GET['bongoto_home_created'] ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$home_url = isset( $_GET['bongoto_home_url'] ) ? esc_url( wp_unslash( $_GET['bongoto_home_url'] ) ) : home_url( '/' );
echo '';
}
settings_errors( 'bongoto_wc_setup' );
$demos = bongoto_woocommerce_setup_list_demo_jsons();
$current_front = (int) get_option( 'page_on_front' );
$grid_css = 'display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;max-width:1200px;';
$card_css = 'background:#fff;border:1px solid #e2e8f0;border-radius:8px;';
$pad_css = 'padding:16px 18px;';
$list_css = 'list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:10px;';
?>