Menus
* - Green dismissible WordPress confirmation banners (`notice-success`) on every action
* - Partial tail-masking (`••••••••••` on last 10 chars) for saved Dynadot API & Webhook keys
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Rowshost_Admin {
private static $instance = null;
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action( 'admin_menu', array( $this, 'register_menus' ) );
add_action( 'admin_init', array( $this, 'handle_form_submissions' ) );
add_action( 'admin_notices', array( $this, 'render_admin_notices' ) );
// Native WordPress Appearance -> Menus Meta-Box ("Rowshost Endpoints")
add_action( 'admin_head-nav-menus.php', array( $this, 'register_nav_menu_metabox' ) );
// WooCommerce Product Data Tab for Hosting Plans
add_filter( 'woocommerce_product_data_tabs', array( $this, 'add_wc_product_tab' ) );
add_action( 'woocommerce_product_data_panels', array( $this, 'render_wc_product_panel' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'save_wc_product_meta' ) );
}
/* =========================================================================
* NATIVE WORDPRESS APPEARANCE -> MENUS ENDPOINTS META-BOX
* ========================================================================= */
public function register_nav_menu_metabox() {
add_meta_box(
'rowshost_nav_endpoints',
__( 'Rowshost Endpoints', 'rowshost' ),
array( $this, 'render_nav_menu_metabox' ),
'nav-menus',
'side',
'default'
);
}
public function render_nav_menu_metabox() {
global $_nav_menu_placeholder, $nav_menu_selected_id;
$_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
$endpoints = array(
'dashboard' => array( 'label' => 'Client Dashboard', 'url' => Rowshost_Plugin::get_portal_url() ),
'hosting' => array( 'label' => 'Hosting Plans', 'url' => Rowshost_Plugin::get_portal_url( 'hosting' ) ),
'domains' => array( 'label' => 'Domains & DNS', 'url' => Rowshost_Plugin::get_portal_url( 'domains' ) ),
'register-domain' => array( 'label' => 'New Domain', 'url' => Rowshost_Plugin::get_portal_url( 'register-domain' ) ),
'transfer-domain' => array( 'label' => 'Transfer Domain', 'url' => Rowshost_Plugin::get_portal_url( 'transfer-domain' ) ),
'billing' => array( 'label' => 'Subscriptions & Invoices', 'url' => Rowshost_Plugin::get_portal_url( 'billing' ) ),
'edit-account' => array( 'label' => 'Account & Billing', 'url' => Rowshost_Plugin::get_portal_url( 'edit-account' ) ),
'login' => array( 'label' => 'Client Login / Dashboard', 'url' => Rowshost_Plugin::get_portal_url() . '#rowshost-login' ),
'logout' => array( 'label' => 'Log Out', 'url' => home_url( '/#rowshost-logout' ) ),
);
?>
'Settings saved successfully.',
'synced' => 'WHM server packages and cPanel accounts synced.',
'linked' => 'cPanel account linked to customer.',
'emailed' => 'Hosting welcome email sent.',
'assigned' => 'Domain owner updated.',
'saved' => 'Subscription saved successfully.',
);
foreach ( $messages as $param => $text ) {
if ( ! empty( $_GET[ $param ] ) ) {
echo 'Rowshost: ' . esc_html( $text ) . '
';
break;
}
}
}
public function register_menus() {
add_menu_page(
__( 'Rowshost', 'rowshost' ),
__( 'Rowshost', 'rowshost' ),
'manage_options',
'rowshost',
array( $this, 'render_dashboard_page' ),
'dashicons-cloud',
56
);
add_submenu_page( 'rowshost', 'Dashboard', 'Dashboard', 'manage_options', 'rowshost', array( $this, 'render_dashboard_page' ) );
add_submenu_page( 'rowshost', 'WHM Servers', 'WHM Servers', 'manage_options', 'rowshost-servers', array( $this, 'render_servers_page' ) );
add_submenu_page( 'rowshost', 'cPanel Accounts', 'cPanel Accounts', 'manage_options', 'rowshost-hosting', array( $this, 'render_hosting_page' ) );
add_submenu_page( 'rowshost', 'Domains & Dynadot', 'Domains & Dynadot', 'manage_options', 'rowshost-domains', array( $this, 'render_domains_page' ) );
add_submenu_page( 'rowshost', 'Subscriptions', 'Subscriptions', 'manage_options', 'rowshost-subscriptions', array( $this, 'render_subscriptions_page' ) );
add_submenu_page( 'rowshost', 'Settings', 'Settings', 'manage_options', 'rowshost-settings', array( $this, 'render_settings_page' ) );
}
public function handle_form_submissions() {
if ( ! current_user_can( 'manage_options' ) || empty( $_POST['rowshost_action'] ) ) {
return;
}
$action = sanitize_key( $_POST['rowshost_action'] );
check_admin_referer( 'rowshost_admin_' . $action );
global $wpdb;
if ( 'save_server' === $action ) {
$server_id = absint( $_POST['server_id'] ?? 0 );
$raw_token = trim( wp_unslash( $_POST['whm_api_token'] ?? '' ) );
$data = array(
'name' => sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ),
'hostname' => sanitize_text_field( wp_unslash( $_POST['hostname'] ?? '' ) ),
'ip_address' => sanitize_text_field( wp_unslash( $_POST['ip_address'] ?? '' ) ),
'whm_username' => sanitize_text_field( wp_unslash( $_POST['whm_username'] ?? '' ) ),
'ns1' => sanitize_text_field( wp_unslash( $_POST['ns1'] ?? '' ) ),
'ns2' => sanitize_text_field( wp_unslash( $_POST['ns2'] ?? '' ) ),
'ns3' => sanitize_text_field( wp_unslash( $_POST['ns3'] ?? '' ) ),
'ns4' => sanitize_text_field( wp_unslash( $_POST['ns4'] ?? '' ) ),
);
if ( '' !== $raw_token ) {
$data['whm_api_token'] = Rowshost_Crypto::encrypt( $raw_token );
}
$table = Rowshost_DB::table( 'servers' );
if ( $server_id ) {
$wpdb->update( $table, $data, array( 'id' => $server_id ) );
} else {
$wpdb->insert( $table, $data );
$server_id = (int) $wpdb->insert_id;
}
if ( $server_id ) {
Rowshost_WHM::sync_packages( $server_id );
Rowshost_WHM::sync_accounts( $server_id );
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-servers', 'updated' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'sync_server' === $action ) {
$server_id = absint( $_POST['server_id'] ?? 0 );
if ( $server_id ) {
Rowshost_WHM::sync_packages( $server_id );
Rowshost_WHM::sync_accounts( $server_id );
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-servers', 'synced' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'link_cpanel' === $action ) {
$server_id = absint( $_POST['server_id'] ?? 0 );
$cpanel_username = sanitize_user( wp_unslash( $_POST['cpanel_username'] ?? '' ), true );
$user_id = absint( $_POST['user_id'] ?? 0 );
$send_welcome = ! empty( $_POST['send_welcome'] );
$cached = $wpdb->get_row(
$wpdb->prepare(
'SELECT * FROM ' . Rowshost_DB::table( 'cpanel_cache' ) . ' WHERE server_id = %d AND cpanel_username = %s',
$server_id,
$cpanel_username
)
);
if ( $cached && $user_id ) {
$wpdb->insert(
Rowshost_DB::table( 'hosting_accounts' ),
array(
'user_id' => $user_id,
'server_id' => $server_id,
'domain' => $cached->domain,
'cpanel_username' => $cached->cpanel_username,
'whm_package' => $cached->whm_package,
'status' => $cached->status,
)
);
$hosting_id = (int) $wpdb->insert_id;
if ( $send_welcome && $hosting_id ) {
Rowshost_Emails::send_hosting_welcome( $hosting_id );
}
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-hosting', 'linked' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'resend_welcome' === $action ) {
$hosting_id = absint( $_POST['hosting_id'] ?? 0 );
if ( $hosting_id ) {
Rowshost_Emails::send_hosting_welcome( $hosting_id );
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-hosting', 'emailed' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'save_dynadot' === $action ) {
update_option( 'rowshost_dynadot_mode', sanitize_key( $_POST['dynadot_mode'] ?? 'live' ) );
$api_key = trim( wp_unslash( $_POST['dynadot_api_key'] ?? '' ) );
$api_secret = trim( wp_unslash( $_POST['dynadot_api_secret'] ?? '' ) );
$wh_key = trim( wp_unslash( $_POST['dynadot_webhook_key'] ?? '' ) );
$wh_secret = trim( wp_unslash( $_POST['dynadot_webhook_secret'] ?? '' ) );
if ( '' !== $api_key && false === strpos( $api_key, '•••' ) ) {
update_option( 'rowshost_dynadot_api_key', Rowshost_Crypto::encrypt( $api_key ) );
}
if ( '' !== $api_secret && false === strpos( $api_secret, '•••' ) ) {
update_option( 'rowshost_dynadot_api_secret', Rowshost_Crypto::encrypt( $api_secret ) );
}
if ( '' !== $wh_key && false === strpos( $wh_key, '•••' ) ) {
update_option( 'rowshost_dynadot_webhook_key', Rowshost_Crypto::encrypt( $wh_key ) );
}
if ( '' !== $wh_secret && false === strpos( $wh_secret, '•••' ) ) {
update_option( 'rowshost_dynadot_webhook_secret', Rowshost_Crypto::encrypt( $wh_secret ) );
}
update_option( 'rowshost_dynadot_default_tlds', sanitize_text_field( wp_unslash( $_POST['default_tlds'] ?? 'com,net,org,io,ai,co,xyz,locker' ) ) );
update_option( 'rowshost_dynadot_ns1', sanitize_text_field( wp_unslash( $_POST['ns1'] ?? '' ) ) );
update_option( 'rowshost_dynadot_ns2', sanitize_text_field( wp_unslash( $_POST['ns2'] ?? '' ) ) );
update_option( 'rowshost_dynadot_ns3', sanitize_text_field( wp_unslash( $_POST['ns3'] ?? '' ) ) );
update_option( 'rowshost_dynadot_ns4', sanitize_text_field( wp_unslash( $_POST['ns4'] ?? '' ) ) );
update_option( 'rowshost_dynadot_default_privacy', sanitize_key( $_POST['default_privacy'] ?? 'full' ) );
update_option( 'rowshost_dynadot_transfer_lock', ! empty( $_POST['transfer_lock'] ) ? '1' : '0' );
update_option( 'rowshost_dynadot_reg_markup', (float) ( $_POST['reg_markup'] ?? 3.00 ) );
update_option( 'rowshost_dynadot_transfer_markup', (float) ( $_POST['transfer_markup'] ?? 0.00 ) );
if ( ! empty( $_POST['sync_now'] ) ) {
Rowshost_Dynadot::sync_domains();
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-domains', 'updated' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'assign_domain_owner' === $action ) {
$domain_id = absint( $_POST['domain_id'] ?? 0 );
$user_id = absint( $_POST['user_id'] ?? 0 );
if ( $domain_id ) {
$wpdb->update( Rowshost_DB::table( 'domains' ), array( 'user_id' => $user_id ), array( 'id' => $domain_id ) );
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-domains', 'assigned' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'save_subscription' === $action ) {
$sub_id = absint( $_POST['sub_id'] ?? 0 );
$user_id = absint( $_POST['user_id'] ?? 0 );
$service_type = sanitize_key( $_POST['service_type'] ?? 'hosting' );
$service_id = absint( $_POST['service_id'] ?? 0 );
$plan_label = sanitize_text_field( wp_unslash( $_POST['plan_label'] ?? '' ) );
$amount = (float) ( $_POST['amount'] ?? 0 );
$interval = max( 1, absint( $_POST['billing_interval'] ?? 1 ) );
$period = sanitize_key( $_POST['billing_period'] ?? 'month' );
$start_date = sanitize_text_field( wp_unslash( $_POST['start_date'] ?? gmdate( 'Y-m-d' ) ) ) . ' 00:00:00';
$next_due = sanitize_text_field( wp_unslash( $_POST['next_payment_date'] ?? gmdate( 'Y-m-d' ) ) ) . ' 00:00:00';
$status = sanitize_key( $_POST['status'] ?? 'active' );
$auto_charge = ! empty( $_POST['auto_charge'] ) ? 1 : 0;
$table = Rowshost_DB::table( 'subscriptions' );
if ( $sub_id ) {
$wpdb->update(
$table,
array(
'plan_label' => $plan_label,
'amount' => $amount,
'billing_interval' => $interval,
'billing_period' => $period,
'start_date' => $start_date,
'next_payment_date' => $next_due,
'status' => $status,
'auto_charge' => $auto_charge,
),
array( 'id' => $sub_id )
);
} elseif ( $user_id ) {
$new_id = Rowshost_Billing::create_subscription(
array(
'user_id' => $user_id,
'service_type' => $service_type,
'service_id' => $service_id,
'plan_label' => $plan_label,
'amount' => $amount,
'billing_interval' => $interval,
'billing_period' => $period,
'start_date' => $start_date,
'next_payment_date' => $next_due,
'status' => $status,
'auto_charge' => $auto_charge,
)
);
if ( $new_id && $service_id ) {
$svc_table = 'domain' === $service_type ? Rowshost_DB::table( 'domains' ) : Rowshost_DB::table( 'hosting_accounts' );
$wpdb->update( $svc_table, array( 'subscription_id' => $new_id ), array( 'id' => $service_id ) );
}
}
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-subscriptions', 'saved' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
if ( 'save_settings' === $action ) {
update_option( 'rowshost_portal_mode', sanitize_key( $_POST['portal_mode'] ?? 'shortcode' ) );
update_option( 'rowshost_portal_page_id', absint( $_POST['portal_page_id'] ?? 0 ) );
update_option( 'rowshost_grace_period_days', max( 1, absint( $_POST['grace_period_days'] ?? 5 ) ) );
flush_rewrite_rules();
wp_safe_redirect( add_query_arg( array( 'page' => 'rowshost-settings', 'updated' => 1 ), admin_url( 'admin.php' ) ) );
exit;
}
}
public function add_wc_product_tab( $tabs ) {
$tabs['rowshost_hosting'] = array(
'label' => __( 'Rowshost Hosting', 'rowshost' ),
'target' => 'rowshost_hosting_product_data',
'class' => array(),
'priority' => 25,
);
return $tabs;
}
public function render_wc_product_panel() {
global $post, $wpdb;
$servers = $wpdb->get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'servers' ) . ' ORDER BY name ASC' );
$is_hosting = get_post_meta( $post->ID, '_rowshost_is_hosting', true );
$server_id = absint( get_post_meta( $post->ID, '_rowshost_server_id', true ) );
$whm_package = get_post_meta( $post->ID, '_rowshost_whm_package', true );
$interval = get_post_meta( $post->ID, '_rowshost_billing_interval', true ) ?: '1';
$period = get_post_meta( $post->ID, '_rowshost_billing_period', true ) ?: 'month';
?>
'_rowshost_is_hosting',
'label' => __( 'Enable Hosting Provisioning', 'rowshost' ),
'description' => __( 'Automatically provision a cPanel account when this product is purchased.', 'rowshost' ),
'value' => $is_hosting,
)
);
$server_opts = array( '0' => __( '— Select WHM Server —', 'rowshost' ) );
$pkg_opts = array( '' => __( '— Select WHM Package —', 'rowshost' ) );
foreach ( $servers as $srv ) {
$server_opts[ $srv->id ] = $srv->name . ' (' . $srv->hostname . ')';
$cached_pkgs = json_decode( $srv->packages_cache ?: '[]', true );
if ( is_array( $cached_pkgs ) ) {
foreach ( $cached_pkgs as $p ) {
$pkg_opts[ $p['name'] ] = $p['name'] . ' [' . $srv->name . ']';
}
}
}
woocommerce_wp_select(
array(
'id' => '_rowshost_server_id',
'label' => __( 'WHM Server', 'rowshost' ),
'options' => $server_opts,
'value' => $server_id,
)
);
woocommerce_wp_select(
array(
'id' => '_rowshost_whm_package',
'label' => __( 'WHM Package', 'rowshost' ),
'options' => $pkg_opts,
'value' => $whm_package,
)
);
woocommerce_wp_text_input(
array(
'id' => '_rowshost_billing_interval',
'label' => __( 'Billing Interval', 'rowshost' ),
'type' => 'number',
'custom_attributes' => array( 'min' => 1, 'step' => 1 ),
'value' => $interval,
)
);
woocommerce_wp_select(
array(
'id' => '_rowshost_billing_period',
'label' => __( 'Billing Period', 'rowshost' ),
'options' => array(
'month' => __( 'Month(s)', 'rowshost' ),
'year' => __( 'Year(s)', 'rowshost' ),
),
'value' => $period,
)
);
?>
get_var( 'SELECT COUNT(*) FROM ' . Rowshost_DB::table( 'hosting_accounts' ) . " WHERE status = 'active'" );
$domain_count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . Rowshost_DB::table( 'domains' ) . " WHERE status = 'active'" );
$sub_count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . Rowshost_DB::table( 'subscriptions' ) . " WHERE status = 'active'" );
$mrr = (float) $wpdb->get_var( 'SELECT SUM(amount) FROM ' . Rowshost_DB::table( 'subscriptions' ) . " WHERE status = 'active' AND billing_period = 'month'" );
?>
get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'servers' ) . ' ORDER BY id DESC' );
?>
WHM Servers
Connected Servers
| Name | Hostname | Synced Packages | Last Synced | Action |
packages_cache ?: '[]', true );
?>
| name ); ?> |
hostname ); ?> |
|
last_synced ?: 'Never' ); ?> |
|
get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'hosting_accounts' ) . ' ORDER BY id DESC' );
$cached = $wpdb->get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'cpanel_cache' ) . ' ORDER BY cpanel_username ASC' );
$users = get_users( array( 'fields' => array( 'ID', 'user_login', 'user_email' ) ) );
?>
Hosting & cPanel Accounts
Assigned Customer Hosting Accounts
| Domain | cPanel User | Customer | Package | Status | Actions |
user_id );
?>
| domain ); ?> |
cpanel_username ); ?> |
user_email ) : 'Unassigned'; ?> |
whm_package ); ?> |
status ) ); ?> |
|
get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'domains' ) . ' ORDER BY domain_name ASC' );
$users = get_users( array( 'fields' => array( 'ID', 'user_login', 'user_email' ) ) );
$masked_api_key = $this->mask_secret_tail( Rowshost_Crypto::decrypt( get_option( 'rowshost_dynadot_api_key', '' ) ) );
$masked_api_secret = $this->mask_secret_tail( Rowshost_Crypto::decrypt( get_option( 'rowshost_dynadot_api_secret', '' ) ) );
$masked_wh_key = $this->mask_secret_tail( Rowshost_Crypto::decrypt( get_option( 'rowshost_dynadot_webhook_key', '' ) ) );
$masked_wh_secret = $this->mask_secret_tail( Rowshost_Crypto::decrypt( get_option( 'rowshost_dynadot_webhook_secret', '' ) ) );
$webhook_url = rest_url( 'rowshost/v1/dynadot-webhook' );
$handoff_url = add_query_arg(
array(
'rowshost_action' => 'add_domain',
'type' => 'register',
'years' => '1',
'domain' => '',
),
home_url( '/' )
);
?>
Domains & Dynadot Settings
Synced & Registered Domains ()
| Domain | Status | Expiration | Nameservers | Assigned Customer |
nameservers ?: '[]', true );
?>
| domain_name ); ?> |
status ) ); ?> |
expiration_date ? gmdate( 'Y-m-d', strtotime( $d->expiration_date ) ) : '—' ); ?> |
|
|
get_results( 'SELECT * FROM ' . Rowshost_DB::table( 'subscriptions' ) . ' ORDER BY next_payment_date ASC' );
$users = get_users( array( 'fields' => array( 'ID', 'user_login', 'user_email' ) ) );
?>
Subscriptions & Billing Due Dates
Active & Scheduled Subscriptions
| Customer | Service | Amount | Start Date | Next Due Date | Status |
user_id );
?>
| user_email ) : 'User #' . absint( $s->user_id ); ?> |
plan_label ); ?> |
$amount, 2 ) . ' / ' . $s->billing_period ); ?> |
start_date ) ) ); ?> |
next_payment_date ) ) ); ?> |
status ) ); ?> |
Rowshost General & Portal Settings